ASTUnit.h revision cc5888d833caf90ebda37f24da40d2cd06b4d820
14b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis//===--- ASTUnit.h - ASTUnit utility ----------------------------*- C++ -*-===//
24b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis//
34b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis//                     The LLVM Compiler Infrastructure
44b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis//
54b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis// This file is distributed under the University of Illinois Open Source
64b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis// License. See LICENSE.TXT for details.
74b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis//
84b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
94b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis//
104b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis// ASTUnit utility class.
114b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis//
124b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis//===----------------------------------------------------------------------===//
134b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis
140853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis#ifndef LLVM_CLANG_FRONTEND_ASTUNIT_H
150853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis#define LLVM_CLANG_FRONTEND_ASTUNIT_H
160853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
174ae8f298b1ea51b4c2234f9148e2e4349c9bdd23Douglas Gregor#include "clang/Lex/PreprocessingRecord.h"
1831b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar#include "clang/Basic/SourceManager.h"
1928019772db70d4547be05a042eb950bc910f134fDouglas Gregor#include "llvm/ADT/IntrusiveRefCntPtr.h"
200853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis#include "llvm/ADT/OwningPtr.h"
2136c4464ba6cfc2a63dc67c493ef2f5ab2aea09ccSteve Naroff#include "clang/Basic/FileManager.h"
22f96b524306ccfa623235d375deee79637bd38f29Steve Naroff#include "clang/Index/ASTLocation.h"
23a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor#include "llvm/ADT/SmallVector.h"
24cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor#include "llvm/ADT/StringMap.h"
25313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor#include "llvm/System/Path.h"
26385103b79c5338a2be5da0ca70652400bc267371Douglas Gregor#include "llvm/Support/Timer.h"
27788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor#include <map>
280853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis#include <string>
29f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar#include <vector>
30f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar#include <cassert>
314db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor#include <utility>
32cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor#include <sys/types.h>
334db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor
344db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregornamespace llvm {
354db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor  class MemoryBuffer;
364db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor}
370853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
380853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidisnamespace clang {
39521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass ASTContext;
40521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass CompilerInvocation;
41521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass Decl;
42521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass Diagnostic;
43521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass FileEntry;
44521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass FileManager;
45521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass HeaderSearch;
46521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass Preprocessor;
47521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass SourceManager;
48521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarclass TargetInfo;
490853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
50f96b524306ccfa623235d375deee79637bd38f29Steve Naroffusing namespace idx;
5144c181aec37789f25f6c15543c164416f72e562aDouglas Gregor
520853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis/// \brief Utility class for loading a ASTContext from a PCH file.
530853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis///
540853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidisclass ASTUnit {
55788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregorpublic:
56788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  typedef std::map<FileID, std::vector<PreprocessedEntity *> >
57788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor    PreprocessedEntitiesByFileMap;
58788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregorprivate:
5928019772db70d4547be05a042eb950bc910f134fDouglas Gregor  llvm::IntrusiveRefCntPtr<Diagnostic> Diagnostics;
60405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  llvm::OwningPtr<FileManager>      FileMgr;
61405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  llvm::OwningPtr<SourceManager>    SourceMgr;
620853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  llvm::OwningPtr<HeaderSearch>     HeaderInfo;
630853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  llvm::OwningPtr<TargetInfo>       Target;
640853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  llvm::OwningPtr<Preprocessor>     PP;
650853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  llvm::OwningPtr<ASTContext>       Ctx;
664ae8f298b1ea51b4c2234f9148e2e4349c9bdd23Douglas Gregor
67807b06157a1a5c050520fc194d32f16d22d423a8Daniel Dunbar  /// Optional owned invocation, just used to make the invocation used in
68807b06157a1a5c050520fc194d32f16d22d423a8Daniel Dunbar  /// LoadFromCommandLine available.
69807b06157a1a5c050520fc194d32f16d22d423a8Daniel Dunbar  llvm::OwningPtr<CompilerInvocation> Invocation;
70807b06157a1a5c050520fc194d32f16d22d423a8Daniel Dunbar
717d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  // OnlyLocalDecls - when true, walking this AST should only visit declarations
727d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  // that come from the AST itself, not from included precompiled headers.
737d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  // FIXME: This is temporary; eventually, CIndex will always do this.
747d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  bool                              OnlyLocalDecls;
75c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar
76abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// \brief Whether to capture any diagnostics produced.
77abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  bool CaptureDiagnostics;
78abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor
79f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  /// Track whether the main file was loaded from an AST or not.
80c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar  bool MainFileIsAST;
81c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar
82f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  /// Track the top-level decls which appeared in an ASTUnit which was loaded
83f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  /// from a source file.
84f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  //
85f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // FIXME: This is just an optimization hack to avoid deserializing large parts
86f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // of a PCH file when using the Index library on an ASTUnit loaded from
87f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // source. In the long term we should make the Index library use efficient and
88f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  // more scalable search mechanisms.
89f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  std::vector<Decl*> TopLevelDecls;
90f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
9168d40e2d16b9fadba386853d6bbb60089291fdc5Daniel Dunbar  /// The name of the original source file used to generate this ASTUnit.
9268d40e2d16b9fadba386853d6bbb60089291fdc5Daniel Dunbar  std::string OriginalSourceFile;
9368d40e2d16b9fadba386853d6bbb60089291fdc5Daniel Dunbar
94f96b524306ccfa623235d375deee79637bd38f29Steve Naroff  // Critical optimization when using clang_getCursor().
95f96b524306ccfa623235d375deee79637bd38f29Steve Naroff  ASTLocation LastLoc;
96f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
97a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  /// \brief The set of diagnostics produced when creating this
98a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  /// translation unit.
99405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  llvm::SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
100a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
101313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  /// \brief Temporary files that should be removed when the ASTUnit is
102313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  /// destroyed.
103313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  llvm::SmallVector<llvm::sys::Path, 4> TemporaryFiles;
104bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
105788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// \brief A mapping from file IDs to the set of preprocessed entities
106788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// stored in that file.
107788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  ///
108788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// FIXME: This is just an optimization hack to avoid searching through
109788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// many preprocessed entities during cursor traversal in the CIndex library.
110788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// Ideally, we would just be able to perform a binary search within the
111788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// list of preprocessed entities.
112788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  PreprocessedEntitiesByFileMap PreprocessedEntitiesByFile;
113788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor
114bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  /// \brief Simple hack to allow us to assert that ASTUnit is not being
115bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  /// used concurrently, which is not supported.
116bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  ///
117bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  /// Clients should create instances of the ConcurrencyCheck class whenever
118bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  /// using the ASTUnit in a way that isn't intended to be concurrent, which is
119bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  /// just about any usage.
120bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  unsigned int ConcurrencyCheckValue;
121bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  static const unsigned int CheckLocked = 28573289;
122bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  static const unsigned int CheckUnlocked = 9803453;
123175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor
124175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  /// \brief The file in which the precompiled preamble is stored.
125385103b79c5338a2be5da0ca70652400bc267371Douglas Gregor  std::string PreambleFile;
126175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor
127175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  /// \brief The contents of the preamble that has been precompiled to
128175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  /// \c PreambleFile.
129175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  std::vector<char> Preamble;
130175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor
131f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// \brief Whether the preamble ends at the start of a new line.
132f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  ///
133f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// Used to inform the lexer as to whether it's starting at the beginning of
134f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// a line after skipping the preamble.
135f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  bool PreambleEndsAtStartOfLine;
136f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor
137175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  /// \brief The size of the source buffer that we've reserved for the main
138175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  /// file within the precompiled preamble.
139175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor  unsigned PreambleReservedSize;
140cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor
141cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  /// \brief Keeps track of the files that were used when computing the
142cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  /// preamble, with both their buffer size and their modification time.
143cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  ///
144cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  /// If any of the files have changed from one compile to the next,
145cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  /// the preamble must be thrown away.
146cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  llvm::StringMap<std::pair<off_t, time_t> > FilesInPreamble;
147cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor
14828233428da1ebec20c893d6297ae3191318940ddDouglas Gregor  /// \brief When non-NULL, this is the buffer used to store the contents of
14928233428da1ebec20c893d6297ae3191318940ddDouglas Gregor  /// the main file when it has been padded for use with the precompiled
15028233428da1ebec20c893d6297ae3191318940ddDouglas Gregor  /// preamble.
15128233428da1ebec20c893d6297ae3191318940ddDouglas Gregor  llvm::MemoryBuffer *SavedMainFileBuffer;
15228233428da1ebec20c893d6297ae3191318940ddDouglas Gregor
153385103b79c5338a2be5da0ca70652400bc267371Douglas Gregor  /// \brief The group of timers associated with this translation unit.
154cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  llvm::OwningPtr<llvm::TimerGroup> TimerGroup;
155385103b79c5338a2be5da0ca70652400bc267371Douglas Gregor
156385103b79c5338a2be5da0ca70652400bc267371Douglas Gregor  /// \brief The timers we've created from the various parses, reparses, etc.
157385103b79c5338a2be5da0ca70652400bc267371Douglas Gregor  /// involved in this translation unit.
158385103b79c5338a2be5da0ca70652400bc267371Douglas Gregor  std::vector<llvm::Timer *> Timers;
159385103b79c5338a2be5da0ca70652400bc267371Douglas Gregor
16031b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT
16131b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT
162bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
1633687e9d3a5dbfa9963af02a49a2b139d91310813Douglas Gregor  explicit ASTUnit(bool MainFileIsAST);
1648b96253907c47141af0b7b2a44a368748d006a87Douglas Gregor
165abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  void CleanTemporaryFiles();
166754f3490c5b0f5d83361f001bc87944f23644abbDouglas Gregor  bool Parse(llvm::MemoryBuffer *OverrideMainBuffer);
167175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor
168f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> >
169f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  ComputePreamble(CompilerInvocation &Invocation, bool &CreatedBuffer);
170175c4a9aa61f4449f27b729737e4438684ac6d92Douglas Gregor
171754f3490c5b0f5d83361f001bc87944f23644abbDouglas Gregor  llvm::MemoryBuffer *BuildPrecompiledPreamble();
172abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor
1730853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidispublic:
174bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  class ConcurrencyCheck {
175bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    volatile ASTUnit &Self;
176bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
177bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  public:
178bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    explicit ConcurrencyCheck(ASTUnit &Self)
179bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor      : Self(Self)
180bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    {
181bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor      assert(Self.ConcurrencyCheckValue == CheckUnlocked &&
182bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor             "Concurrent access to ASTUnit!");
183bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor      Self.ConcurrencyCheckValue = CheckLocked;
184bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    }
185bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
186bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    ~ConcurrencyCheck() {
187bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor      Self.ConcurrencyCheckValue = CheckUnlocked;
188bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor    }
189bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  };
190bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor  friend class ConcurrencyCheck;
191bdf6062bc10aa3b73b16402b440b8073310acd06Douglas Gregor
1920853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  ~ASTUnit();
1930853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
194c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar  bool isMainFileAST() const { return MainFileIsAST; }
195c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar
1963687e9d3a5dbfa9963af02a49a2b139d91310813Douglas Gregor  const Diagnostic &getDiagnostics() const { return *Diagnostics; }
1973687e9d3a5dbfa9963af02a49a2b139d91310813Douglas Gregor  Diagnostic &getDiagnostics()             { return *Diagnostics; }
1983687e9d3a5dbfa9963af02a49a2b139d91310813Douglas Gregor
199405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  const SourceManager &getSourceManager() const { return *SourceMgr; }
200405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor        SourceManager &getSourceManager()       { return *SourceMgr; }
2010853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
2020853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  const Preprocessor &getPreprocessor() const { return *PP.get(); }
2030853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis        Preprocessor &getPreprocessor()       { return *PP.get(); }
2041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2050853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  const ASTContext &getASTContext() const { return *Ctx.get(); }
2060853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis        ASTContext &getASTContext()       { return *Ctx.get(); }
2070853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
208405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  const FileManager &getFileManager() const { return *FileMgr; }
209405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor        FileManager &getFileManager()       { return *FileMgr; }
210f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
21177accc11f04ed4ff9afd4e27d430144d4714be56Steve Naroff  const std::string &getOriginalSourceFileName();
212e19944c93961b7618f4f3f3185f698f46369ea54Steve Naroff  const std::string &getPCHFileName();
213b85bca2676b433ae555db09de4dd2823ff13b856Zhongxing Xu
214313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  /// \brief Add a temporary file that the ASTUnit depends on.
215313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  ///
216313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  /// This file will be erased when the ASTUnit is destroyed.
217313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  void addTemporaryFile(const llvm::sys::Path &TempFile) {
218313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor    TemporaryFiles.push_back(TempFile);
219313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor  }
220313e26c4e81f0e467490a530548450f4c824a6c4Douglas Gregor
2217d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor  bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
222f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
223f96b524306ccfa623235d375deee79637bd38f29Steve Naroff  void setLastASTLocation(ASTLocation ALoc) { LastLoc = ALoc; }
224f96b524306ccfa623235d375deee79637bd38f29Steve Naroff  ASTLocation getLastASTLocation() const { return LastLoc; }
225f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
226f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  std::vector<Decl*> &getTopLevelDecls() {
227f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
228f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar    return TopLevelDecls;
229f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  }
230f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  const std::vector<Decl*> &getTopLevelDecls() const {
231f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar    assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
232f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar    return TopLevelDecls;
233f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar  }
234f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar
235788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// \brief Retrieve the mapping from File IDs to the preprocessed entities
236788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  /// within that file.
237788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  PreprocessedEntitiesByFileMap &getPreprocessedEntitiesByFile() {
238788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor    return PreprocessedEntitiesByFile;
239788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor  }
240788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor
241a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  // Retrieve the diagnostics associated with this AST
242405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  typedef const StoredDiagnostic *stored_diag_iterator;
243405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  stored_diag_iterator stored_diag_begin() const {
244405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor    return StoredDiagnostics.begin();
245405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  }
246405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  stored_diag_iterator stored_diag_end() const {
247405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor    return StoredDiagnostics.end();
248405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  }
249405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  unsigned stored_diag_size() const { return StoredDiagnostics.size(); }
250405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor
251405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor  llvm::SmallVector<StoredDiagnostic, 4> &getStoredDiagnostics() {
252405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor    return StoredDiagnostics;
253a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  }
254a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
2554db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor  /// \brief A mapping from a file name to the memory buffer that stores the
2564db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor  /// remapped contents of that file.
2574db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor  typedef std::pair<std::string, const llvm::MemoryBuffer *> RemappedFile;
2584db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor
2590853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  /// \brief Create a ASTUnit from a PCH file.
2600853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  ///
26131b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  /// \param Filename - The PCH file to load.
2620853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  ///
2635262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// \param Diags - The diagnostics engine to use for reporting errors; its
2645262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// lifetime is expected to extend past that of the returned ASTUnit.
26531b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  ///
26631b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar  /// \returns - The initialized ASTUnit or null if the PCH failed to load.
2670853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis  static ASTUnit *LoadFromPCHFile(const std::string &Filename,
26828019772db70d4547be05a042eb950bc910f134fDouglas Gregor                                  llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
2695cf48766d626ff6b223acc9d4b7e415ca8480836Ted Kremenek                                  bool OnlyLocalDecls = false,
2704db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor                                  RemappedFile *RemappedFiles = 0,
271a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor                                  unsigned NumRemappedFiles = 0,
272a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor                                  bool CaptureDiagnostics = false);
273521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar
274521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  /// LoadFromCompilerInvocation - Create an ASTUnit from a source file, via a
275521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  /// CompilerInvocation object.
276521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  ///
277521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  /// \param CI - The compiler invocation to use; it must have exactly one input
278f7acc37450d59ef751df73acb91de73850cc6517Daniel Dunbar  /// source file. The ASTUnit takes ownership of the CompilerInvocation object.
279521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  ///
2805262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// \param Diags - The diagnostics engine to use for reporting errors; its
2815262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// lifetime is expected to extend past that of the returned ASTUnit.
282521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  //
283521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
284521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar  // shouldn't need to specify them at construction time.
285f7acc37450d59ef751df73acb91de73850cc6517Daniel Dunbar  static ASTUnit *LoadFromCompilerInvocation(CompilerInvocation *CI,
28628019772db70d4547be05a042eb950bc910f134fDouglas Gregor                                     llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
287a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor                                             bool OnlyLocalDecls = false,
28844c181aec37789f25f6c15543c164416f72e562aDouglas Gregor                                             bool CaptureDiagnostics = false,
28944c181aec37789f25f6c15543c164416f72e562aDouglas Gregor                                             bool PrecompilePreamble = false);
290521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar
2917b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// LoadFromCommandLine - Create an ASTUnit from a vector of command line
2927b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// arguments, which must specify exactly one source file.
2937b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
2947b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// \param ArgBegin - The beginning of the argument vector.
2957b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
2967b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  /// \param ArgEnd - The end of the argument vector.
2977b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
2985262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// \param Diags - The diagnostics engine to use for reporting errors; its
2995262fda30b876c8aae95f2eb92e349418d6b14bbDaniel Dunbar  /// lifetime is expected to extend past that of the returned ASTUnit.
3007b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  ///
301869824e87940f97b87064db2df2861e82e08a8c6Daniel Dunbar  /// \param ResourceFilesPath - The path to the compiler resource files.
3027b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  //
3037b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
3047b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  // shouldn't need to specify them at construction time.
3057b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar  static ASTUnit *LoadFromCommandLine(const char **ArgBegin,
3067b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar                                      const char **ArgEnd,
30728019772db70d4547be05a042eb950bc910f134fDouglas Gregor                                    llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
308869824e87940f97b87064db2df2861e82e08a8c6Daniel Dunbar                                      llvm::StringRef ResourceFilesPath,
3097b55668db7618334cc40011d3c1e128524d89462Daniel Dunbar                                      bool OnlyLocalDecls = false,
3104db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor                                      RemappedFile *RemappedFiles = 0,
311a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor                                      unsigned NumRemappedFiles = 0,
31244c181aec37789f25f6c15543c164416f72e562aDouglas Gregor                                      bool CaptureDiagnostics = false,
31344c181aec37789f25f6c15543c164416f72e562aDouglas Gregor                                      bool PrecompilePreamble = false);
314abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor
315abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// \brief Reparse the source files using the same command-line options that
316abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// were originally used to produce this translation unit.
317abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  ///
318abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// \returns True if a failure occurred that causes the ASTUnit not to
319abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  /// contain any translation-unit information, false otherwise.
320abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  bool Reparse(RemappedFile *RemappedFiles = 0,
321abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor               unsigned NumRemappedFiles = 0);
3220853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis};
3230853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
3240853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis} // namespace clang
3250853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis
3260853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis#endif
327