1 //===--- ASTUnit.h - ASTUnit utility ----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // ASTUnit utility class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_FRONTEND_ASTUNIT_H
15 #define LLVM_CLANG_FRONTEND_ASTUNIT_H
17 #include "clang/Lex/PreprocessingRecord.h"
18 #include "clang/Basic/SourceManager.h"
19 #include "llvm/ADT/IntrusiveRefCntPtr.h"
20 #include "llvm/ADT/OwningPtr.h"
21 #include "clang/Basic/FileManager.h"
22 #include "clang/Index/ASTLocation.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/System/Path.h"
37 class CompilerInvocation;
49 /// \brief Utility class for loading a ASTContext from a PCH file.
53 typedef std::map<FileID, std::vector<PreprocessedEntity *> >
54 PreprocessedEntitiesByFileMap;
56 llvm::IntrusiveRefCntPtr<Diagnostic> Diagnostics;
57 llvm::OwningPtr<FileManager> FileMgr;
58 llvm::OwningPtr<SourceManager> SourceMgr;
59 llvm::OwningPtr<HeaderSearch> HeaderInfo;
60 llvm::OwningPtr<TargetInfo> Target;
61 llvm::OwningPtr<Preprocessor> PP;
62 llvm::OwningPtr<ASTContext> Ctx;
64 /// Optional owned invocation, just used to make the invocation used in
65 /// LoadFromCommandLine available.
66 llvm::OwningPtr<CompilerInvocation> Invocation;
68 // OnlyLocalDecls - when true, walking this AST should only visit declarations
69 // that come from the AST itself, not from included precompiled headers.
70 // FIXME: This is temporary; eventually, CIndex will always do this.
73 /// Track whether the main file was loaded from an AST or not.
76 /// Track the top-level decls which appeared in an ASTUnit which was loaded
77 /// from a source file.
79 // FIXME: This is just an optimization hack to avoid deserializing large parts
80 // of a PCH file when using the Index library on an ASTUnit loaded from
81 // source. In the long term we should make the Index library use efficient and
82 // more scalable search mechanisms.
83 std::vector<Decl*> TopLevelDecls;
85 /// The name of the original source file used to generate this ASTUnit.
86 std::string OriginalSourceFile;
88 // Critical optimization when using clang_getCursor().
91 /// \brief The set of diagnostics produced when creating this
93 llvm::SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
95 /// \brief Temporary files that should be removed when the ASTUnit is
97 llvm::SmallVector<llvm::sys::Path, 4> TemporaryFiles;
99 /// \brief A mapping from file IDs to the set of preprocessed entities
100 /// stored in that file.
102 /// FIXME: This is just an optimization hack to avoid searching through
103 /// many preprocessed entities during cursor traversal in the CIndex library.
104 /// Ideally, we would just be able to perform a binary search within the
105 /// list of preprocessed entities.
106 PreprocessedEntitiesByFileMap PreprocessedEntitiesByFile;
108 /// \brief Simple hack to allow us to assert that ASTUnit is not being
109 /// used concurrently, which is not supported.
111 /// Clients should create instances of the ConcurrencyCheck class whenever
112 /// using the ASTUnit in a way that isn't intended to be concurrent, which is
113 /// just about any usage.
114 unsigned int ConcurrencyCheckValue;
115 static const unsigned int CheckLocked = 28573289;
116 static const unsigned int CheckUnlocked = 9803453;
118 ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT
119 ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT
121 explicit ASTUnit(bool MainFileIsAST);
124 class ConcurrencyCheck {
125 volatile ASTUnit &Self;
128 explicit ConcurrencyCheck(ASTUnit &Self)
131 assert(Self.ConcurrencyCheckValue == CheckUnlocked &&
132 "Concurrent access to ASTUnit!");
133 Self.ConcurrencyCheckValue = CheckLocked;
136 ~ConcurrencyCheck() {
137 Self.ConcurrencyCheckValue = CheckUnlocked;
140 friend class ConcurrencyCheck;
144 bool isMainFileAST() const { return MainFileIsAST; }
146 const Diagnostic &getDiagnostics() const { return *Diagnostics; }
147 Diagnostic &getDiagnostics() { return *Diagnostics; }
149 const SourceManager &getSourceManager() const { return *SourceMgr; }
150 SourceManager &getSourceManager() { return *SourceMgr; }
152 const Preprocessor &getPreprocessor() const { return *PP.get(); }
153 Preprocessor &getPreprocessor() { return *PP.get(); }
155 const ASTContext &getASTContext() const { return *Ctx.get(); }
156 ASTContext &getASTContext() { return *Ctx.get(); }
158 const FileManager &getFileManager() const { return *FileMgr; }
159 FileManager &getFileManager() { return *FileMgr; }
161 const std::string &getOriginalSourceFileName();
162 const std::string &getPCHFileName();
164 /// \brief Add a temporary file that the ASTUnit depends on.
166 /// This file will be erased when the ASTUnit is destroyed.
167 void addTemporaryFile(const llvm::sys::Path &TempFile) {
168 TemporaryFiles.push_back(TempFile);
171 bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
173 void setLastASTLocation(ASTLocation ALoc) { LastLoc = ALoc; }
174 ASTLocation getLastASTLocation() const { return LastLoc; }
176 std::vector<Decl*> &getTopLevelDecls() {
177 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
178 return TopLevelDecls;
180 const std::vector<Decl*> &getTopLevelDecls() const {
181 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
182 return TopLevelDecls;
185 /// \brief Retrieve the mapping from File IDs to the preprocessed entities
186 /// within that file.
187 PreprocessedEntitiesByFileMap &getPreprocessedEntitiesByFile() {
188 return PreprocessedEntitiesByFile;
191 // Retrieve the diagnostics associated with this AST
192 typedef const StoredDiagnostic *stored_diag_iterator;
193 stored_diag_iterator stored_diag_begin() const {
194 return StoredDiagnostics.begin();
196 stored_diag_iterator stored_diag_end() const {
197 return StoredDiagnostics.end();
199 unsigned stored_diag_size() const { return StoredDiagnostics.size(); }
201 llvm::SmallVector<StoredDiagnostic, 4> &getStoredDiagnostics() {
202 return StoredDiagnostics;
205 /// \brief A mapping from a file name to the memory buffer that stores the
206 /// remapped contents of that file.
207 typedef std::pair<std::string, const llvm::MemoryBuffer *> RemappedFile;
209 /// \brief Create a ASTUnit from a PCH file.
211 /// \param Filename - The PCH file to load.
213 /// \param Diags - The diagnostics engine to use for reporting errors; its
214 /// lifetime is expected to extend past that of the returned ASTUnit.
216 /// \returns - The initialized ASTUnit or null if the PCH failed to load.
217 static ASTUnit *LoadFromPCHFile(const std::string &Filename,
218 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
219 bool OnlyLocalDecls = false,
220 RemappedFile *RemappedFiles = 0,
221 unsigned NumRemappedFiles = 0,
222 bool CaptureDiagnostics = false);
224 /// LoadFromCompilerInvocation - Create an ASTUnit from a source file, via a
225 /// CompilerInvocation object.
227 /// \param CI - The compiler invocation to use; it must have exactly one input
228 /// source file. The ASTUnit takes ownership of the CompilerInvocation object.
230 /// \param Diags - The diagnostics engine to use for reporting errors; its
231 /// lifetime is expected to extend past that of the returned ASTUnit.
233 // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
234 // shouldn't need to specify them at construction time.
235 static ASTUnit *LoadFromCompilerInvocation(CompilerInvocation *CI,
236 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
237 bool OnlyLocalDecls = false,
238 bool CaptureDiagnostics = false);
240 /// LoadFromCommandLine - Create an ASTUnit from a vector of command line
241 /// arguments, which must specify exactly one source file.
243 /// \param ArgBegin - The beginning of the argument vector.
245 /// \param ArgEnd - The end of the argument vector.
247 /// \param Diags - The diagnostics engine to use for reporting errors; its
248 /// lifetime is expected to extend past that of the returned ASTUnit.
250 /// \param ResourceFilesPath - The path to the compiler resource files.
252 // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
253 // shouldn't need to specify them at construction time.
254 static ASTUnit *LoadFromCommandLine(const char **ArgBegin,
256 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
257 llvm::StringRef ResourceFilesPath,
258 bool OnlyLocalDecls = false,
259 RemappedFile *RemappedFiles = 0,
260 unsigned NumRemappedFiles = 0,
261 bool CaptureDiagnostics = false);