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::IntrusiveRefCntPtr<FileManager> FileMgr;
75 llvm::IntrusiveRefCntPtr<SourceManager> SourceMgr;
76 llvm::OwningPtr<HeaderSearch> HeaderInfo;
77 llvm::IntrusiveRefCntPtr<TargetInfo> Target;
78 llvm::IntrusiveRefCntPtr<Preprocessor> PP;
79 llvm::IntrusiveRefCntPtr<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::IntrusiveRefCntPtr<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 /// \brief Whether the ASTUnit should delete the remapped buffers.
120 bool OwnsRemappedFileBuffers;
122 /// Track the top-level decls which appeared in an ASTUnit which was loaded
123 /// from a source file.
125 // FIXME: This is just an optimization hack to avoid deserializing large parts
126 // of a PCH file when using the Index library on an ASTUnit loaded from
127 // source. In the long term we should make the Index library use efficient and
128 // more scalable search mechanisms.
129 std::vector<Decl*> TopLevelDecls;
131 /// \brief The list of preprocessed entities which appeared when the ASTUnit
134 /// FIXME: This is just an optimization hack to avoid deserializing large
135 /// parts of a PCH file while performing a walk or search. In the long term,
136 /// we should provide more scalable search mechanisms.
137 std::vector<PreprocessedEntity *> PreprocessedEntities;
139 /// The name of the original source file used to generate this ASTUnit.
140 std::string OriginalSourceFile;
142 // Critical optimization when using clang_getCursor().
145 /// \brief The set of diagnostics produced when creating this
146 /// translation unit.
147 llvm::SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
149 /// \brief The number of stored diagnostics that come from the driver
152 /// Diagnostics that come from the driver are retained from one parse to
154 unsigned NumStoredDiagnosticsFromDriver;
156 /// \brief Temporary files that should be removed when the ASTUnit is
158 llvm::SmallVector<llvm::sys::Path, 4> TemporaryFiles;
160 /// \brief A mapping from file IDs to the set of preprocessed entities
161 /// stored in that file.
163 /// FIXME: This is just an optimization hack to avoid searching through
164 /// many preprocessed entities during cursor traversal in the CIndex library.
165 /// Ideally, we would just be able to perform a binary search within the
166 /// list of preprocessed entities.
167 PreprocessedEntitiesByFileMap PreprocessedEntitiesByFile;
169 /// \brief Simple hack to allow us to assert that ASTUnit is not being
170 /// used concurrently, which is not supported.
172 /// Clients should create instances of the ConcurrencyCheck class whenever
173 /// using the ASTUnit in a way that isn't intended to be concurrent, which is
174 /// just about any usage.
175 unsigned int ConcurrencyCheckValue;
176 static const unsigned int CheckLocked = 28573289;
177 static const unsigned int CheckUnlocked = 9803453;
179 /// \brief Counter that determines when we want to try building a
180 /// precompiled preamble.
182 /// If zero, we will never build a precompiled preamble. Otherwise,
183 /// it's treated as a counter that decrements each time we reparse
184 /// without the benefit of a precompiled preamble. When it hits 1,
185 /// we'll attempt to rebuild the precompiled header. This way, if
186 /// building the precompiled preamble fails, we won't try again for
187 /// some number of calls.
188 unsigned PreambleRebuildCounter;
190 /// \brief The file in which the precompiled preamble is stored.
191 std::string PreambleFile;
193 /// \brief The contents of the preamble that has been precompiled to
195 std::vector<char> Preamble;
197 /// \brief Whether the preamble ends at the start of a new line.
199 /// Used to inform the lexer as to whether it's starting at the beginning of
200 /// a line after skipping the preamble.
201 bool PreambleEndsAtStartOfLine;
203 /// \brief The size of the source buffer that we've reserved for the main
204 /// file within the precompiled preamble.
205 unsigned PreambleReservedSize;
207 /// \brief Keeps track of the files that were used when computing the
208 /// preamble, with both their buffer size and their modification time.
210 /// If any of the files have changed from one compile to the next,
211 /// the preamble must be thrown away.
212 llvm::StringMap<std::pair<off_t, time_t> > FilesInPreamble;
214 /// \brief When non-NULL, this is the buffer used to store the contents of
215 /// the main file when it has been padded for use with the precompiled
217 llvm::MemoryBuffer *SavedMainFileBuffer;
219 /// \brief When non-NULL, this is the buffer used to store the
220 /// contents of the preamble when it has been padded to build the
221 /// precompiled preamble.
222 llvm::MemoryBuffer *PreambleBuffer;
224 /// \brief The number of warnings that occurred while parsing the preamble.
226 /// This value will be used to restore the state of the \c Diagnostic object
227 /// when re-using the precompiled preamble. Note that only the
228 /// number of warnings matters, since we will not save the preamble
229 /// when any errors are present.
230 unsigned NumWarningsInPreamble;
232 /// \brief The number of diagnostics that were stored when parsing
233 /// the precompiled preamble.
235 /// This value is used to determine how many of the stored
236 /// diagnostics should be retained when reparsing in the presence of
237 /// a precompiled preamble.
238 unsigned NumStoredDiagnosticsInPreamble;
240 /// \brief A list of the serialization ID numbers for each of the top-level
241 /// declarations parsed within the precompiled preamble.
242 std::vector<serialization::DeclID> TopLevelDeclsInPreamble;
244 /// \brief A list of the offsets into the precompiled preamble which
245 /// correspond to preprocessed entities.
246 std::vector<uint64_t> PreprocessedEntitiesInPreamble;
248 /// \brief Whether we should be caching code-completion results.
249 bool ShouldCacheCodeCompletionResults;
251 static void ConfigureDiags(llvm::IntrusiveRefCntPtr<Diagnostic> &Diags,
252 const char **ArgBegin, const char **ArgEnd,
253 ASTUnit &AST, bool CaptureDiagnostics);
256 /// \brief A cached code-completion result, which may be introduced in one of
257 /// many different contexts.
258 struct CachedCodeCompletionResult {
259 /// \brief The code-completion string corresponding to this completion
261 CodeCompletionString *Completion;
263 /// \brief A bitmask that indicates which code-completion contexts should
264 /// contain this completion result.
266 /// The bits in the bitmask correspond to the values of
267 /// CodeCompleteContext::Kind. To map from a completion context kind to a
268 /// bit, subtract one from the completion context kind and shift 1 by that
269 /// number of bits. Many completions can occur in several different
271 unsigned ShowInContexts;
273 /// \brief The priority given to this code-completion result.
276 /// \brief The libclang cursor kind corresponding to this code-completion
280 /// \brief The availability of this code-completion result.
281 CXAvailabilityKind Availability;
283 /// \brief The simplified type class for a non-macro completion result.
284 SimplifiedTypeClass TypeClass;
286 /// \brief The type of a non-macro completion result, stored as a unique
287 /// integer used by the string map of cached completion types.
289 /// This value will be zero if the type is not known, or a unique value
290 /// determined by the formatted type string. Se \c CachedCompletionTypes
291 /// for more information.
295 /// \brief Retrieve the mapping from formatted type names to unique type
297 llvm::StringMap<unsigned> &getCachedCompletionTypes() {
298 return CachedCompletionTypes;
301 /// \brief Retrieve the allocator used to cache global code completions.
302 llvm::IntrusiveRefCntPtr<GlobalCodeCompletionAllocator>
303 getCachedCompletionAllocator() {
304 return CachedCompletionAllocator;
308 /// \brief Allocator used to store cached code completions.
309 llvm::IntrusiveRefCntPtr<GlobalCodeCompletionAllocator>
310 CachedCompletionAllocator;
312 /// \brief The set of cached code-completion results.
313 std::vector<CachedCodeCompletionResult> CachedCompletionResults;
315 /// \brief A mapping from the formatted type name to a unique number for that
316 /// type, which is used for type equality comparisons.
317 llvm::StringMap<unsigned> CachedCompletionTypes;
319 /// \brief A string hash of the top-level declaration and macro definition
320 /// names processed the last time that we reparsed the file.
322 /// This hash value is used to determine when we need to refresh the
323 /// global code-completion cache.
324 unsigned CompletionCacheTopLevelHashValue;
326 /// \brief A string hash of the top-level declaration and macro definition
327 /// names processed the last time that we reparsed the precompiled preamble.
329 /// This hash value is used to determine when we need to refresh the
330 /// global code-completion cache after a rebuild of the precompiled preamble.
331 unsigned PreambleTopLevelHashValue;
333 /// \brief The current hash value for the top-level declaration and macro
335 unsigned CurrentTopLevelHashValue;
337 /// \brief Bit used by CIndex to mark when a translation unit may be in an
338 /// inconsistent state, and is not safe to free.
339 unsigned UnsafeToFree : 1;
341 /// \brief Cache any "global" code-completion results, so that we can avoid
342 /// recomputing them with each completion.
343 void CacheCodeCompletionResults();
345 /// \brief Clear out and deallocate
346 void ClearCachedCompletionResults();
348 ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT
349 ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT
351 explicit ASTUnit(bool MainFileIsAST);
353 void CleanTemporaryFiles();
354 bool Parse(llvm::MemoryBuffer *OverrideMainBuffer);
356 std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> >
357 ComputePreamble(CompilerInvocation &Invocation,
358 unsigned MaxLines, bool &CreatedBuffer);
360 llvm::MemoryBuffer *getMainBufferWithPrecompiledPreamble(
361 CompilerInvocation PreambleInvocation,
362 bool AllowRebuild = true,
363 unsigned MaxLines = 0);
364 void RealizeTopLevelDeclsFromPreamble();
365 void RealizePreprocessedEntitiesFromPreamble();
368 class ConcurrencyCheck {
369 volatile ASTUnit &Self;
372 explicit ConcurrencyCheck(ASTUnit &Self)
375 assert(Self.ConcurrencyCheckValue == CheckUnlocked &&
376 "Concurrent access to ASTUnit!");
377 Self.ConcurrencyCheckValue = CheckLocked;
380 ~ConcurrencyCheck() {
381 Self.ConcurrencyCheckValue = CheckUnlocked;
384 friend class ConcurrencyCheck;
388 bool isMainFileAST() const { return MainFileIsAST; }
390 bool isUnsafeToFree() const { return UnsafeToFree; }
391 void setUnsafeToFree(bool Value) { UnsafeToFree = Value; }
393 const Diagnostic &getDiagnostics() const { return *Diagnostics; }
394 Diagnostic &getDiagnostics() { return *Diagnostics; }
396 const SourceManager &getSourceManager() const { return *SourceMgr; }
397 SourceManager &getSourceManager() { return *SourceMgr; }
399 const Preprocessor &getPreprocessor() const { return *PP; }
400 Preprocessor &getPreprocessor() { return *PP; }
402 const ASTContext &getASTContext() const { return *Ctx; }
403 ASTContext &getASTContext() { return *Ctx; }
405 bool hasSema() const { return TheSema; }
406 Sema &getSema() const {
407 assert(TheSema && "ASTUnit does not have a Sema object!");
411 const FileManager &getFileManager() const { return *FileMgr; }
412 FileManager &getFileManager() { return *FileMgr; }
414 const FileSystemOptions &getFileSystemOpts() const { return FileSystemOpts; }
416 const std::string &getOriginalSourceFileName();
417 const std::string &getASTFileName();
419 /// \brief Add a temporary file that the ASTUnit depends on.
421 /// This file will be erased when the ASTUnit is destroyed.
422 void addTemporaryFile(const llvm::sys::Path &TempFile) {
423 TemporaryFiles.push_back(TempFile);
426 bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
428 bool getOwnsRemappedFileBuffers() const { return OwnsRemappedFileBuffers; }
429 void setOwnsRemappedFileBuffers(bool val) { OwnsRemappedFileBuffers = val; }
431 /// \brief Retrieve the maximum PCH level of declarations that a
432 /// traversal of the translation unit should consider.
433 unsigned getMaxPCHLevel() const;
435 void setLastASTLocation(ASTLocation ALoc) { LastLoc = ALoc; }
436 ASTLocation getLastASTLocation() const { return LastLoc; }
439 llvm::StringRef getMainFileName() const;
441 typedef std::vector<Decl *>::iterator top_level_iterator;
443 top_level_iterator top_level_begin() {
444 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
445 if (!TopLevelDeclsInPreamble.empty())
446 RealizeTopLevelDeclsFromPreamble();
447 return TopLevelDecls.begin();
450 top_level_iterator top_level_end() {
451 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
452 if (!TopLevelDeclsInPreamble.empty())
453 RealizeTopLevelDeclsFromPreamble();
454 return TopLevelDecls.end();
457 std::size_t top_level_size() const {
458 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
459 return TopLevelDeclsInPreamble.size() + TopLevelDecls.size();
462 bool top_level_empty() const {
463 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
464 return TopLevelDeclsInPreamble.empty() && TopLevelDecls.empty();
467 /// \brief Add a new top-level declaration.
468 void addTopLevelDecl(Decl *D) {
469 TopLevelDecls.push_back(D);
472 /// \brief Add a new top-level declaration, identified by its ID in
473 /// the precompiled preamble.
474 void addTopLevelDeclFromPreamble(serialization::DeclID D) {
475 TopLevelDeclsInPreamble.push_back(D);
478 /// \brief Retrieve a reference to the current top-level name hash value.
480 /// Note: This is used internally by the top-level tracking action
481 unsigned &getCurrentTopLevelHashValue() { return CurrentTopLevelHashValue; }
483 typedef std::vector<PreprocessedEntity *>::iterator pp_entity_iterator;
485 pp_entity_iterator pp_entity_begin();
486 pp_entity_iterator pp_entity_end();
488 /// \brief Add a new preprocessed entity that's stored at the given offset
489 /// in the precompiled preamble.
490 void addPreprocessedEntityFromPreamble(uint64_t Offset) {
491 PreprocessedEntitiesInPreamble.push_back(Offset);
494 /// \brief Retrieve the mapping from File IDs to the preprocessed entities
495 /// within that file.
496 PreprocessedEntitiesByFileMap &getPreprocessedEntitiesByFile() {
497 return PreprocessedEntitiesByFile;
500 // Retrieve the diagnostics associated with this AST
501 typedef const StoredDiagnostic *stored_diag_iterator;
502 stored_diag_iterator stored_diag_begin() const {
503 return StoredDiagnostics.begin();
505 stored_diag_iterator stored_diag_end() const {
506 return StoredDiagnostics.end();
508 unsigned stored_diag_size() const { return StoredDiagnostics.size(); }
510 llvm::SmallVector<StoredDiagnostic, 4> &getStoredDiagnostics() {
511 return StoredDiagnostics;
514 typedef std::vector<CachedCodeCompletionResult>::iterator
515 cached_completion_iterator;
517 cached_completion_iterator cached_completion_begin() {
518 return CachedCompletionResults.begin();
521 cached_completion_iterator cached_completion_end() {
522 return CachedCompletionResults.end();
525 unsigned cached_completion_size() const {
526 return CachedCompletionResults.size();
529 llvm::MemoryBuffer *getBufferForFile(llvm::StringRef Filename,
530 std::string *ErrorStr = 0);
532 /// \brief Whether this AST represents a complete translation unit.
534 /// If false, this AST is only a partial translation unit, e.g., one
535 /// that might still be used as a precompiled header or preamble.
536 bool isCompleteTranslationUnit() const { return CompleteTranslationUnit; }
538 typedef llvm::PointerUnion<const char *, const llvm::MemoryBuffer *>
540 /// \brief A mapping from a file name to the memory buffer that stores the
541 /// remapped contents of that file.
542 typedef std::pair<std::string, FilenameOrMemBuf> RemappedFile;
544 /// \brief Create a ASTUnit. Gets ownership of the passed CompilerInvocation.
545 static ASTUnit *create(CompilerInvocation *CI,
546 llvm::IntrusiveRefCntPtr<Diagnostic> Diags);
548 /// \brief Create a ASTUnit from an AST file.
550 /// \param Filename - The AST file to load.
552 /// \param Diags - The diagnostics engine to use for reporting errors; its
553 /// lifetime is expected to extend past that of the returned ASTUnit.
555 /// \returns - The initialized ASTUnit or null if the AST failed to load.
556 static ASTUnit *LoadFromASTFile(const std::string &Filename,
557 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
558 const FileSystemOptions &FileSystemOpts,
559 bool OnlyLocalDecls = false,
560 RemappedFile *RemappedFiles = 0,
561 unsigned NumRemappedFiles = 0,
562 bool CaptureDiagnostics = false);
565 /// \brief Helper function for \c LoadFromCompilerInvocation() and
566 /// \c LoadFromCommandLine(), which loads an AST from a compiler invocation.
568 /// \param PrecompilePreamble Whether to precompile the preamble of this
569 /// translation unit, to improve the performance of reparsing.
571 /// \returns \c true if a catastrophic failure occurred (which means that the
572 /// \c ASTUnit itself is invalid), or \c false otherwise.
573 bool LoadFromCompilerInvocation(bool PrecompilePreamble);
577 /// LoadFromCompilerInvocation - Create an ASTUnit from a source file, via a
578 /// CompilerInvocation object.
580 /// \param CI - The compiler invocation to use; it must have exactly one input
581 /// source file. The ASTUnit takes ownership of the CompilerInvocation object.
583 /// \param Diags - The diagnostics engine to use for reporting errors; its
584 /// lifetime is expected to extend past that of the returned ASTUnit.
586 // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
587 // shouldn't need to specify them at construction time.
588 static ASTUnit *LoadFromCompilerInvocation(CompilerInvocation *CI,
589 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
590 bool OnlyLocalDecls = false,
591 bool CaptureDiagnostics = false,
592 bool PrecompilePreamble = false,
593 bool CompleteTranslationUnit = true,
594 bool CacheCodeCompletionResults = false);
596 /// LoadFromCommandLine - Create an ASTUnit from a vector of command line
597 /// arguments, which must specify exactly one source file.
599 /// \param ArgBegin - The beginning of the argument vector.
601 /// \param ArgEnd - The end of the argument vector.
603 /// \param Diags - The diagnostics engine to use for reporting errors; its
604 /// lifetime is expected to extend past that of the returned ASTUnit.
606 /// \param ResourceFilesPath - The path to the compiler resource files.
608 // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
609 // shouldn't need to specify them at construction time.
610 static ASTUnit *LoadFromCommandLine(const char **ArgBegin,
612 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
613 llvm::StringRef ResourceFilesPath,
614 bool OnlyLocalDecls = false,
615 bool CaptureDiagnostics = false,
616 RemappedFile *RemappedFiles = 0,
617 unsigned NumRemappedFiles = 0,
618 bool RemappedFilesKeepOriginalName = true,
619 bool PrecompilePreamble = false,
620 bool CompleteTranslationUnit = true,
621 bool CacheCodeCompletionResults = false,
622 bool CXXPrecompilePreamble = false,
623 bool CXXChainedPCH = false);
625 /// \brief Reparse the source files using the same command-line options that
626 /// were originally used to produce this translation unit.
628 /// \returns True if a failure occurred that causes the ASTUnit not to
629 /// contain any translation-unit information, false otherwise.
630 bool Reparse(RemappedFile *RemappedFiles = 0,
631 unsigned NumRemappedFiles = 0);
633 /// \brief Perform code completion at the given file, line, and
634 /// column within this translation unit.
636 /// \param File The file in which code completion will occur.
638 /// \param Line The line at which code completion will occur.
640 /// \param Column The column at which code completion will occur.
642 /// \param IncludeMacros Whether to include macros in the code-completion
645 /// \param IncludeCodePatterns Whether to include code patterns (such as a
646 /// for loop) in the code-completion results.
648 /// FIXME: The Diag, LangOpts, SourceMgr, FileMgr, StoredDiagnostics, and
649 /// OwnedBuffers parameters are all disgusting hacks. They will go away.
650 void CodeComplete(llvm::StringRef File, unsigned Line, unsigned Column,
651 RemappedFile *RemappedFiles, unsigned NumRemappedFiles,
652 bool IncludeMacros, bool IncludeCodePatterns,
653 CodeCompleteConsumer &Consumer,
654 Diagnostic &Diag, LangOptions &LangOpts,
655 SourceManager &SourceMgr, FileManager &FileMgr,
656 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
657 llvm::SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers);
659 /// \brief Save this translation unit to a file with the given name.
661 /// \returns True if an error occurred, false otherwise.
662 bool Save(llvm::StringRef File);
664 /// \brief Serialize this translation unit with the given output stream.
666 /// \returns True if an error occurred, false otherwise.
667 bool serialize(llvm::raw_ostream &OS);