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/Index/ASTLocation.h"
18 #include "clang/Serialization/ASTBitCodes.h"
19 #include "clang/Sema/Sema.h"
20 #include "clang/Sema/CodeCompleteConsumer.h"
21 #include "clang/Lex/PreprocessingRecord.h"
22 #include "clang/Basic/SourceManager.h"
23 #include "clang/Basic/FileManager.h"
24 #include "clang/Basic/FileSystemOptions.h"
25 #include "clang-c/Index.h"
26 #include "llvm/ADT/IntrusiveRefCntPtr.h"
27 #include "llvm/ADT/OwningPtr.h"
28 #include "llvm/ADT/SmallVector.h"
29 #include "llvm/ADT/StringMap.h"
30 #include "llvm/Support/Path.h"
36 #include <sys/types.h>
44 class CodeCompleteConsumer;
45 class CompilerInvocation;
57 /// \brief Allocator for a cached set of global code completions.
58 class GlobalCodeCompletionAllocator
59 : public CodeCompletionAllocator,
60 public llvm::RefCountedBase<GlobalCodeCompletionAllocator>
65 /// \brief Utility class for loading a ASTContext from an AST file.
69 typedef std::map<FileID, std::vector<PreprocessedEntity *> >
70 PreprocessedEntitiesByFileMap;
73 llvm::IntrusiveRefCntPtr<Diagnostic> Diagnostics;
74 llvm::OwningPtr<FileManager> FileMgr;
75 llvm::OwningPtr<SourceManager> SourceMgr;
76 llvm::OwningPtr<HeaderSearch> HeaderInfo;
77 llvm::OwningPtr<TargetInfo> Target;
78 llvm::OwningPtr<Preprocessor> PP;
79 llvm::OwningPtr<ASTContext> Ctx;
81 FileSystemOptions FileSystemOpts;
83 /// \brief The AST consumer that received information about the translation
84 /// unit as it was parsed or loaded.
85 llvm::OwningPtr<ASTConsumer> Consumer;
87 /// \brief The semantic analysis object used to type-check the translation
89 llvm::OwningPtr<Sema> TheSema;
91 /// Optional owned invocation, just used to make the invocation used in
92 /// LoadFromCommandLine available.
93 llvm::OwningPtr<CompilerInvocation> Invocation;
95 /// \brief The set of target features.
97 /// FIXME: each time we reparse, we need to restore the set of target
98 /// features from this vector, because TargetInfo::CreateTargetInfo()
99 /// mangles the target options in place. Yuck!
100 std::vector<std::string> TargetFeatures;
102 // OnlyLocalDecls - when true, walking this AST should only visit declarations
103 // that come from the AST itself, not from included precompiled headers.
104 // FIXME: This is temporary; eventually, CIndex will always do this.
107 /// \brief Whether to capture any diagnostics produced.
108 bool CaptureDiagnostics;
110 /// \brief Track whether the main file was loaded from an AST or not.
113 /// \brief Whether this AST represents a complete translation unit.
114 bool CompleteTranslationUnit;
116 /// \brief Whether we should time each operation.
119 /// Track the top-level decls which appeared in an ASTUnit which was loaded
120 /// from a source file.
122 // FIXME: This is just an optimization hack to avoid deserializing large parts
123 // of a PCH file when using the Index library on an ASTUnit loaded from
124 // source. In the long term we should make the Index library use efficient and
125 // more scalable search mechanisms.
126 std::vector<Decl*> TopLevelDecls;
128 /// \brief The list of preprocessed entities which appeared when the ASTUnit
131 /// FIXME: This is just an optimization hack to avoid deserializing large
132 /// parts of a PCH file while performing a walk or search. In the long term,
133 /// we should provide more scalable search mechanisms.
134 std::vector<PreprocessedEntity *> PreprocessedEntities;
136 /// The name of the original source file used to generate this ASTUnit.
137 std::string OriginalSourceFile;
139 // Critical optimization when using clang_getCursor().
142 /// \brief The set of diagnostics produced when creating this
143 /// translation unit.
144 llvm::SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
146 /// \brief The number of stored diagnostics that come from the driver
149 /// Diagnostics that come from the driver are retained from one parse to
151 unsigned NumStoredDiagnosticsFromDriver;
153 /// \brief Temporary files that should be removed when the ASTUnit is
155 llvm::SmallVector<llvm::sys::Path, 4> TemporaryFiles;
157 /// \brief A mapping from file IDs to the set of preprocessed entities
158 /// stored in that file.
160 /// FIXME: This is just an optimization hack to avoid searching through
161 /// many preprocessed entities during cursor traversal in the CIndex library.
162 /// Ideally, we would just be able to perform a binary search within the
163 /// list of preprocessed entities.
164 PreprocessedEntitiesByFileMap PreprocessedEntitiesByFile;
166 /// \brief Simple hack to allow us to assert that ASTUnit is not being
167 /// used concurrently, which is not supported.
169 /// Clients should create instances of the ConcurrencyCheck class whenever
170 /// using the ASTUnit in a way that isn't intended to be concurrent, which is
171 /// just about any usage.
172 unsigned int ConcurrencyCheckValue;
173 static const unsigned int CheckLocked = 28573289;
174 static const unsigned int CheckUnlocked = 9803453;
176 /// \brief Counter that determines when we want to try building a
177 /// precompiled preamble.
179 /// If zero, we will never build a precompiled preamble. Otherwise,
180 /// it's treated as a counter that decrements each time we reparse
181 /// without the benefit of a precompiled preamble. When it hits 1,
182 /// we'll attempt to rebuild the precompiled header. This way, if
183 /// building the precompiled preamble fails, we won't try again for
184 /// some number of calls.
185 unsigned PreambleRebuildCounter;
187 /// \brief The file in which the precompiled preamble is stored.
188 std::string PreambleFile;
190 /// \brief The contents of the preamble that has been precompiled to
192 std::vector<char> Preamble;
194 /// \brief Whether the preamble ends at the start of a new line.
196 /// Used to inform the lexer as to whether it's starting at the beginning of
197 /// a line after skipping the preamble.
198 bool PreambleEndsAtStartOfLine;
200 /// \brief The size of the source buffer that we've reserved for the main
201 /// file within the precompiled preamble.
202 unsigned PreambleReservedSize;
204 /// \brief Keeps track of the files that were used when computing the
205 /// preamble, with both their buffer size and their modification time.
207 /// If any of the files have changed from one compile to the next,
208 /// the preamble must be thrown away.
209 llvm::StringMap<std::pair<off_t, time_t> > FilesInPreamble;
211 /// \brief When non-NULL, this is the buffer used to store the contents of
212 /// the main file when it has been padded for use with the precompiled
214 llvm::MemoryBuffer *SavedMainFileBuffer;
216 /// \brief When non-NULL, this is the buffer used to store the
217 /// contents of the preamble when it has been padded to build the
218 /// precompiled preamble.
219 llvm::MemoryBuffer *PreambleBuffer;
221 /// \brief The number of warnings that occurred while parsing the preamble.
223 /// This value will be used to restore the state of the \c Diagnostic object
224 /// when re-using the precompiled preamble. Note that only the
225 /// number of warnings matters, since we will not save the preamble
226 /// when any errors are present.
227 unsigned NumWarningsInPreamble;
229 /// \brief The number of diagnostics that were stored when parsing
230 /// the precompiled preamble.
232 /// This value is used to determine how many of the stored
233 /// diagnostics should be retained when reparsing in the presence of
234 /// a precompiled preamble.
235 unsigned NumStoredDiagnosticsInPreamble;
237 /// \brief A list of the serialization ID numbers for each of the top-level
238 /// declarations parsed within the precompiled preamble.
239 std::vector<serialization::DeclID> TopLevelDeclsInPreamble;
241 /// \brief A list of the offsets into the precompiled preamble which
242 /// correspond to preprocessed entities.
243 std::vector<uint64_t> PreprocessedEntitiesInPreamble;
245 /// \brief Whether we should be caching code-completion results.
246 bool ShouldCacheCodeCompletionResults;
248 static void ConfigureDiags(llvm::IntrusiveRefCntPtr<Diagnostic> &Diags,
249 const char **ArgBegin, const char **ArgEnd,
250 ASTUnit &AST, bool CaptureDiagnostics);
253 /// \brief A cached code-completion result, which may be introduced in one of
254 /// many different contexts.
255 struct CachedCodeCompletionResult {
256 /// \brief The code-completion string corresponding to this completion
258 CodeCompletionString *Completion;
260 /// \brief A bitmask that indicates which code-completion contexts should
261 /// contain this completion result.
263 /// The bits in the bitmask correspond to the values of
264 /// CodeCompleteContext::Kind. To map from a completion context kind to a
265 /// bit, subtract one from the completion context kind and shift 1 by that
266 /// number of bits. Many completions can occur in several different
268 unsigned ShowInContexts;
270 /// \brief The priority given to this code-completion result.
273 /// \brief The libclang cursor kind corresponding to this code-completion
277 /// \brief The availability of this code-completion result.
278 CXAvailabilityKind Availability;
280 /// \brief The simplified type class for a non-macro completion result.
281 SimplifiedTypeClass TypeClass;
283 /// \brief The type of a non-macro completion result, stored as a unique
284 /// integer used by the string map of cached completion types.
286 /// This value will be zero if the type is not known, or a unique value
287 /// determined by the formatted type string. Se \c CachedCompletionTypes
288 /// for more information.
292 /// \brief Retrieve the mapping from formatted type names to unique type
294 llvm::StringMap<unsigned> &getCachedCompletionTypes() {
295 return CachedCompletionTypes;
298 /// \brief Retrieve the allocator used to cache global code completions.
299 llvm::IntrusiveRefCntPtr<GlobalCodeCompletionAllocator>
300 getCachedCompletionAllocator() {
301 return CachedCompletionAllocator;
305 /// \brief Allocator used to store cached code completions.
306 llvm::IntrusiveRefCntPtr<GlobalCodeCompletionAllocator>
307 CachedCompletionAllocator;
309 /// \brief The set of cached code-completion results.
310 std::vector<CachedCodeCompletionResult> CachedCompletionResults;
312 /// \brief A mapping from the formatted type name to a unique number for that
313 /// type, which is used for type equality comparisons.
314 llvm::StringMap<unsigned> CachedCompletionTypes;
316 /// \brief A string hash of the top-level declaration and macro definition
317 /// names processed the last time that we reparsed the file.
319 /// This hash value is used to determine when we need to refresh the
320 /// global code-completion cache.
321 unsigned CompletionCacheTopLevelHashValue;
323 /// \brief A string hash of the top-level declaration and macro definition
324 /// names processed the last time that we reparsed the precompiled preamble.
326 /// This hash value is used to determine when we need to refresh the
327 /// global code-completion cache after a rebuild of the precompiled preamble.
328 unsigned PreambleTopLevelHashValue;
330 /// \brief The current hash value for the top-level declaration and macro
332 unsigned CurrentTopLevelHashValue;
334 /// \brief Bit used by CIndex to mark when a translation unit may be in an
335 /// inconsistent state, and is not safe to free.
336 unsigned UnsafeToFree : 1;
338 /// \brief Cache any "global" code-completion results, so that we can avoid
339 /// recomputing them with each completion.
340 void CacheCodeCompletionResults();
342 /// \brief Clear out and deallocate
343 void ClearCachedCompletionResults();
345 ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT
346 ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT
348 explicit ASTUnit(bool MainFileIsAST);
350 void CleanTemporaryFiles();
351 bool Parse(llvm::MemoryBuffer *OverrideMainBuffer);
353 std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> >
354 ComputePreamble(CompilerInvocation &Invocation,
355 unsigned MaxLines, bool &CreatedBuffer);
357 llvm::MemoryBuffer *getMainBufferWithPrecompiledPreamble(
358 CompilerInvocation PreambleInvocation,
359 bool AllowRebuild = true,
360 unsigned MaxLines = 0);
361 void RealizeTopLevelDeclsFromPreamble();
362 void RealizePreprocessedEntitiesFromPreamble();
365 class ConcurrencyCheck {
366 volatile ASTUnit &Self;
369 explicit ConcurrencyCheck(ASTUnit &Self)
372 assert(Self.ConcurrencyCheckValue == CheckUnlocked &&
373 "Concurrent access to ASTUnit!");
374 Self.ConcurrencyCheckValue = CheckLocked;
377 ~ConcurrencyCheck() {
378 Self.ConcurrencyCheckValue = CheckUnlocked;
381 friend class ConcurrencyCheck;
385 bool isMainFileAST() const { return MainFileIsAST; }
387 bool isUnsafeToFree() const { return UnsafeToFree; }
388 void setUnsafeToFree(bool Value) { UnsafeToFree = Value; }
390 const Diagnostic &getDiagnostics() const { return *Diagnostics; }
391 Diagnostic &getDiagnostics() { return *Diagnostics; }
393 const SourceManager &getSourceManager() const { return *SourceMgr; }
394 SourceManager &getSourceManager() { return *SourceMgr; }
396 const Preprocessor &getPreprocessor() const { return *PP.get(); }
397 Preprocessor &getPreprocessor() { return *PP.get(); }
399 const ASTContext &getASTContext() const { return *Ctx.get(); }
400 ASTContext &getASTContext() { return *Ctx.get(); }
402 bool hasSema() const { return TheSema; }
403 Sema &getSema() const {
404 assert(TheSema && "ASTUnit does not have a Sema object!");
408 const FileManager &getFileManager() const { return *FileMgr; }
409 FileManager &getFileManager() { return *FileMgr; }
411 const FileSystemOptions &getFileSystemOpts() const { return FileSystemOpts; }
413 const std::string &getOriginalSourceFileName();
414 const std::string &getASTFileName();
416 /// \brief Add a temporary file that the ASTUnit depends on.
418 /// This file will be erased when the ASTUnit is destroyed.
419 void addTemporaryFile(const llvm::sys::Path &TempFile) {
420 TemporaryFiles.push_back(TempFile);
423 bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
425 /// \brief Retrieve the maximum PCH level of declarations that a
426 /// traversal of the translation unit should consider.
427 unsigned getMaxPCHLevel() const;
429 void setLastASTLocation(ASTLocation ALoc) { LastLoc = ALoc; }
430 ASTLocation getLastASTLocation() const { return LastLoc; }
433 llvm::StringRef getMainFileName() const;
435 typedef std::vector<Decl *>::iterator top_level_iterator;
437 top_level_iterator top_level_begin() {
438 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
439 if (!TopLevelDeclsInPreamble.empty())
440 RealizeTopLevelDeclsFromPreamble();
441 return TopLevelDecls.begin();
444 top_level_iterator top_level_end() {
445 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
446 if (!TopLevelDeclsInPreamble.empty())
447 RealizeTopLevelDeclsFromPreamble();
448 return TopLevelDecls.end();
451 std::size_t top_level_size() const {
452 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
453 return TopLevelDeclsInPreamble.size() + TopLevelDecls.size();
456 bool top_level_empty() const {
457 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
458 return TopLevelDeclsInPreamble.empty() && TopLevelDecls.empty();
461 /// \brief Add a new top-level declaration.
462 void addTopLevelDecl(Decl *D) {
463 TopLevelDecls.push_back(D);
466 /// \brief Add a new top-level declaration, identified by its ID in
467 /// the precompiled preamble.
468 void addTopLevelDeclFromPreamble(serialization::DeclID D) {
469 TopLevelDeclsInPreamble.push_back(D);
472 /// \brief Retrieve a reference to the current top-level name hash value.
474 /// Note: This is used internally by the top-level tracking action
475 unsigned &getCurrentTopLevelHashValue() { return CurrentTopLevelHashValue; }
477 typedef std::vector<PreprocessedEntity *>::iterator pp_entity_iterator;
479 pp_entity_iterator pp_entity_begin();
480 pp_entity_iterator pp_entity_end();
482 /// \brief Add a new preprocessed entity that's stored at the given offset
483 /// in the precompiled preamble.
484 void addPreprocessedEntityFromPreamble(uint64_t Offset) {
485 PreprocessedEntitiesInPreamble.push_back(Offset);
488 /// \brief Retrieve the mapping from File IDs to the preprocessed entities
489 /// within that file.
490 PreprocessedEntitiesByFileMap &getPreprocessedEntitiesByFile() {
491 return PreprocessedEntitiesByFile;
494 // Retrieve the diagnostics associated with this AST
495 typedef const StoredDiagnostic *stored_diag_iterator;
496 stored_diag_iterator stored_diag_begin() const {
497 return StoredDiagnostics.begin();
499 stored_diag_iterator stored_diag_end() const {
500 return StoredDiagnostics.end();
502 unsigned stored_diag_size() const { return StoredDiagnostics.size(); }
504 llvm::SmallVector<StoredDiagnostic, 4> &getStoredDiagnostics() {
505 return StoredDiagnostics;
508 typedef std::vector<CachedCodeCompletionResult>::iterator
509 cached_completion_iterator;
511 cached_completion_iterator cached_completion_begin() {
512 return CachedCompletionResults.begin();
515 cached_completion_iterator cached_completion_end() {
516 return CachedCompletionResults.end();
519 unsigned cached_completion_size() const {
520 return CachedCompletionResults.size();
523 llvm::MemoryBuffer *getBufferForFile(llvm::StringRef Filename,
524 std::string *ErrorStr = 0);
526 /// \brief Whether this AST represents a complete translation unit.
528 /// If false, this AST is only a partial translation unit, e.g., one
529 /// that might still be used as a precompiled header or preamble.
530 bool isCompleteTranslationUnit() const { return CompleteTranslationUnit; }
532 /// \brief A mapping from a file name to the memory buffer that stores the
533 /// remapped contents of that file.
534 typedef std::pair<std::string, const llvm::MemoryBuffer *> RemappedFile;
536 /// \brief Create a ASTUnit from an AST file.
538 /// \param Filename - The AST file to load.
540 /// \param Diags - The diagnostics engine to use for reporting errors; its
541 /// lifetime is expected to extend past that of the returned ASTUnit.
543 /// \returns - The initialized ASTUnit or null if the AST failed to load.
544 static ASTUnit *LoadFromASTFile(const std::string &Filename,
545 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
546 const FileSystemOptions &FileSystemOpts,
547 bool OnlyLocalDecls = false,
548 RemappedFile *RemappedFiles = 0,
549 unsigned NumRemappedFiles = 0,
550 bool CaptureDiagnostics = false);
553 /// \brief Helper function for \c LoadFromCompilerInvocation() and
554 /// \c LoadFromCommandLine(), which loads an AST from a compiler invocation.
556 /// \param PrecompilePreamble Whether to precompile the preamble of this
557 /// translation unit, to improve the performance of reparsing.
559 /// \returns \c true if a catastrophic failure occurred (which means that the
560 /// \c ASTUnit itself is invalid), or \c false otherwise.
561 bool LoadFromCompilerInvocation(bool PrecompilePreamble);
565 /// LoadFromCompilerInvocation - Create an ASTUnit from a source file, via a
566 /// CompilerInvocation object.
568 /// \param CI - The compiler invocation to use; it must have exactly one input
569 /// source file. The ASTUnit takes ownership of the CompilerInvocation object.
571 /// \param Diags - The diagnostics engine to use for reporting errors; its
572 /// lifetime is expected to extend past that of the returned ASTUnit.
574 // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
575 // shouldn't need to specify them at construction time.
576 static ASTUnit *LoadFromCompilerInvocation(CompilerInvocation *CI,
577 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
578 bool OnlyLocalDecls = false,
579 bool CaptureDiagnostics = false,
580 bool PrecompilePreamble = false,
581 bool CompleteTranslationUnit = true,
582 bool CacheCodeCompletionResults = false);
584 /// LoadFromCommandLine - Create an ASTUnit from a vector of command line
585 /// arguments, which must specify exactly one source file.
587 /// \param ArgBegin - The beginning of the argument vector.
589 /// \param ArgEnd - The end of the argument vector.
591 /// \param Diags - The diagnostics engine to use for reporting errors; its
592 /// lifetime is expected to extend past that of the returned ASTUnit.
594 /// \param ResourceFilesPath - The path to the compiler resource files.
596 // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
597 // shouldn't need to specify them at construction time.
598 static ASTUnit *LoadFromCommandLine(const char **ArgBegin,
600 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
601 llvm::StringRef ResourceFilesPath,
602 bool OnlyLocalDecls = false,
603 bool CaptureDiagnostics = false,
604 RemappedFile *RemappedFiles = 0,
605 unsigned NumRemappedFiles = 0,
606 bool PrecompilePreamble = false,
607 bool CompleteTranslationUnit = true,
608 bool CacheCodeCompletionResults = false,
609 bool CXXPrecompilePreamble = false,
610 bool CXXChainedPCH = false);
612 /// \brief Reparse the source files using the same command-line options that
613 /// were originally used to produce this translation unit.
615 /// \returns True if a failure occurred that causes the ASTUnit not to
616 /// contain any translation-unit information, false otherwise.
617 bool Reparse(RemappedFile *RemappedFiles = 0,
618 unsigned NumRemappedFiles = 0);
620 /// \brief Perform code completion at the given file, line, and
621 /// column within this translation unit.
623 /// \param File The file in which code completion will occur.
625 /// \param Line The line at which code completion will occur.
627 /// \param Column The column at which code completion will occur.
629 /// \param IncludeMacros Whether to include macros in the code-completion
632 /// \param IncludeCodePatterns Whether to include code patterns (such as a
633 /// for loop) in the code-completion results.
635 /// FIXME: The Diag, LangOpts, SourceMgr, FileMgr, StoredDiagnostics, and
636 /// OwnedBuffers parameters are all disgusting hacks. They will go away.
637 void CodeComplete(llvm::StringRef File, unsigned Line, unsigned Column,
638 RemappedFile *RemappedFiles, unsigned NumRemappedFiles,
639 bool IncludeMacros, bool IncludeCodePatterns,
640 CodeCompleteConsumer &Consumer,
641 Diagnostic &Diag, LangOptions &LangOpts,
642 SourceManager &SourceMgr, FileManager &FileMgr,
643 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
644 llvm::SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers);
646 /// \brief Save this translation unit to a file with the given name.
648 /// \returns True if an error occurred, false otherwise.
649 bool Save(llvm::StringRef File);