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-c/Index.h"
18 #include "clang/AST/ASTContext.h"
19 #include "clang/Basic/FileManager.h"
20 #include "clang/Basic/FileSystemOptions.h"
21 #include "clang/Basic/LangOptions.h"
22 #include "clang/Basic/SourceManager.h"
23 #include "clang/Basic/TargetOptions.h"
24 #include "clang/Lex/HeaderSearchOptions.h"
25 #include "clang/Lex/ModuleLoader.h"
26 #include "clang/Lex/PreprocessingRecord.h"
27 #include "clang/Sema/CodeCompleteConsumer.h"
28 #include "clang/Serialization/ASTBitCodes.h"
29 #include "llvm/ADT/IntrusiveRefCntPtr.h"
30 #include "llvm/ADT/SmallVector.h"
31 #include "llvm/ADT/StringMap.h"
32 #include "llvm/Support/MD5.h"
33 #include "llvm/Support/Path.h"
38 #include <sys/types.h>
50 class CodeCompleteConsumer;
51 class CompilerInvocation;
52 class CompilerInstance;
54 class DiagnosticsEngine;
59 class PCHContainerOperations;
62 class ASTFrontendAction;
63 class ASTDeserializationListener;
65 /// \brief Utility class for loading a ASTContext from an AST file.
67 class ASTUnit : public ModuleLoader {
69 struct StandaloneFixIt {
70 std::pair<unsigned, unsigned> RemoveRange;
71 std::pair<unsigned, unsigned> InsertFromRange;
72 std::string CodeToInsert;
73 bool BeforePreviousInsertions;
76 struct StandaloneDiagnostic {
78 DiagnosticsEngine::Level Level;
82 std::vector<std::pair<unsigned, unsigned> > Ranges;
83 std::vector<StandaloneFixIt> FixIts;
87 std::shared_ptr<LangOptions> LangOpts;
88 IntrusiveRefCntPtr<DiagnosticsEngine> Diagnostics;
89 IntrusiveRefCntPtr<FileManager> FileMgr;
90 IntrusiveRefCntPtr<SourceManager> SourceMgr;
91 std::unique_ptr<HeaderSearch> HeaderInfo;
92 IntrusiveRefCntPtr<TargetInfo> Target;
93 IntrusiveRefCntPtr<Preprocessor> PP;
94 IntrusiveRefCntPtr<ASTContext> Ctx;
95 std::shared_ptr<TargetOptions> TargetOpts;
96 IntrusiveRefCntPtr<HeaderSearchOptions> HSOpts;
97 IntrusiveRefCntPtr<ASTReader> Reader;
98 bool HadModuleLoaderFatalFailure;
100 struct ASTWriterData;
101 std::unique_ptr<ASTWriterData> WriterData;
103 FileSystemOptions FileSystemOpts;
105 /// \brief The AST consumer that received information about the translation
106 /// unit as it was parsed or loaded.
107 std::unique_ptr<ASTConsumer> Consumer;
109 /// \brief The semantic analysis object used to type-check the translation
111 std::unique_ptr<Sema> TheSema;
113 /// Optional owned invocation, just used to make the invocation used in
114 /// LoadFromCommandLine available.
115 IntrusiveRefCntPtr<CompilerInvocation> Invocation;
117 // OnlyLocalDecls - when true, walking this AST should only visit declarations
118 // that come from the AST itself, not from included precompiled headers.
119 // FIXME: This is temporary; eventually, CIndex will always do this.
122 /// \brief Whether to capture any diagnostics produced.
123 bool CaptureDiagnostics;
125 /// \brief Track whether the main file was loaded from an AST or not.
128 /// \brief What kind of translation unit this AST represents.
129 TranslationUnitKind TUKind;
131 /// \brief Whether we should time each operation.
134 /// \brief Whether the ASTUnit should delete the remapped buffers.
135 bool OwnsRemappedFileBuffers;
137 /// Track the top-level decls which appeared in an ASTUnit which was loaded
138 /// from a source file.
140 // FIXME: This is just an optimization hack to avoid deserializing large parts
141 // of a PCH file when using the Index library on an ASTUnit loaded from
142 // source. In the long term we should make the Index library use efficient and
143 // more scalable search mechanisms.
144 std::vector<Decl*> TopLevelDecls;
146 /// \brief Sorted (by file offset) vector of pairs of file offset/Decl.
147 typedef SmallVector<std::pair<unsigned, Decl *>, 64> LocDeclsTy;
148 typedef llvm::DenseMap<FileID, LocDeclsTy *> FileDeclsTy;
150 /// \brief Map from FileID to the file-level declarations that it contains.
151 /// The files and decls are only local (and non-preamble) ones.
152 FileDeclsTy FileDecls;
154 /// The name of the original source file used to generate this ASTUnit.
155 std::string OriginalSourceFile;
157 /// \brief The set of diagnostics produced when creating the preamble.
158 SmallVector<StandaloneDiagnostic, 4> PreambleDiagnostics;
160 /// \brief The set of diagnostics produced when creating this
161 /// translation unit.
162 SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
164 /// \brief The set of diagnostics produced when failing to parse, e.g. due
165 /// to failure to load the PCH.
166 SmallVector<StoredDiagnostic, 4> FailedParseDiagnostics;
168 /// \brief The number of stored diagnostics that come from the driver
171 /// Diagnostics that come from the driver are retained from one parse to
173 unsigned NumStoredDiagnosticsFromDriver;
175 /// \brief Counter that determines when we want to try building a
176 /// precompiled preamble.
178 /// If zero, we will never build a precompiled preamble. Otherwise,
179 /// it's treated as a counter that decrements each time we reparse
180 /// without the benefit of a precompiled preamble. When it hits 1,
181 /// we'll attempt to rebuild the precompiled header. This way, if
182 /// building the precompiled preamble fails, we won't try again for
183 /// some number of calls.
184 unsigned PreambleRebuildCounter;
188 const FileEntry *File;
189 std::vector<char> Buffer;
190 mutable unsigned NumLines;
193 PreambleData() : File(nullptr), NumLines(0) { }
195 void assign(const FileEntry *F, const char *begin, const char *end) {
197 Buffer.assign(begin, end);
201 void clear() { Buffer.clear(); File = nullptr; NumLines = 0; }
203 size_t size() const { return Buffer.size(); }
204 bool empty() const { return Buffer.empty(); }
206 const char *getBufferStart() const { return &Buffer[0]; }
208 unsigned getNumLines() const {
215 SourceRange getSourceRange(const SourceManager &SM) const {
216 SourceLocation FileLoc = SM.getLocForStartOfFile(SM.getPreambleFileID());
217 return SourceRange(FileLoc, FileLoc.getLocWithOffset(size()-1));
221 void countLines() const;
224 const PreambleData &getPreambleData() const {
228 /// Data used to determine if a file used in the preamble has been changed.
229 struct PreambleFileHash {
230 /// All files have size set.
233 /// Modification time is set for files that are on disk. For memory
234 /// buffers it is zero.
237 /// Memory buffers have MD5 instead of modification time. We don't
238 /// compute MD5 for on-disk files because we hope that modification time is
239 /// enough to tell if the file was changed.
240 llvm::MD5::MD5Result MD5;
242 static PreambleFileHash createForFile(off_t Size, time_t ModTime);
243 static PreambleFileHash
244 createForMemoryBuffer(const llvm::MemoryBuffer *Buffer);
246 friend bool operator==(const PreambleFileHash &LHS,
247 const PreambleFileHash &RHS);
249 friend bool operator!=(const PreambleFileHash &LHS,
250 const PreambleFileHash &RHS) {
251 return !(LHS == RHS);
256 /// \brief The contents of the preamble that has been precompiled to
258 PreambleData Preamble;
260 /// \brief Whether the preamble ends at the start of a new line.
262 /// Used to inform the lexer as to whether it's starting at the beginning of
263 /// a line after skipping the preamble.
264 bool PreambleEndsAtStartOfLine;
266 /// \brief Keeps track of the files that were used when computing the
267 /// preamble, with both their buffer size and their modification time.
269 /// If any of the files have changed from one compile to the next,
270 /// the preamble must be thrown away.
271 llvm::StringMap<PreambleFileHash> FilesInPreamble;
273 /// \brief When non-NULL, this is the buffer used to store the contents of
274 /// the main file when it has been padded for use with the precompiled
276 std::unique_ptr<llvm::MemoryBuffer> SavedMainFileBuffer;
278 /// \brief When non-NULL, this is the buffer used to store the
279 /// contents of the preamble when it has been padded to build the
280 /// precompiled preamble.
281 std::unique_ptr<llvm::MemoryBuffer> PreambleBuffer;
283 /// \brief The number of warnings that occurred while parsing the preamble.
285 /// This value will be used to restore the state of the \c DiagnosticsEngine
286 /// object when re-using the precompiled preamble. Note that only the
287 /// number of warnings matters, since we will not save the preamble
288 /// when any errors are present.
289 unsigned NumWarningsInPreamble;
291 /// \brief A list of the serialization ID numbers for each of the top-level
292 /// declarations parsed within the precompiled preamble.
293 std::vector<serialization::DeclID> TopLevelDeclsInPreamble;
295 /// \brief Whether we should be caching code-completion results.
296 bool ShouldCacheCodeCompletionResults : 1;
298 /// \brief Whether to include brief documentation within the set of code
299 /// completions cached.
300 bool IncludeBriefCommentsInCodeCompletion : 1;
302 /// \brief True if non-system source files should be treated as volatile
303 /// (likely to change while trying to use them).
304 bool UserFilesAreVolatile : 1;
306 /// \brief The language options used when we load an AST file.
307 LangOptions ASTFileLangOpts;
309 static void ConfigureDiags(IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
310 ASTUnit &AST, bool CaptureDiagnostics);
312 void TranslateStoredDiagnostics(FileManager &FileMgr,
313 SourceManager &SrcMan,
314 const SmallVectorImpl<StandaloneDiagnostic> &Diags,
315 SmallVectorImpl<StoredDiagnostic> &Out);
317 void clearFileLevelDecls();
320 /// \brief A cached code-completion result, which may be introduced in one of
321 /// many different contexts.
322 struct CachedCodeCompletionResult {
323 /// \brief The code-completion string corresponding to this completion
325 CodeCompletionString *Completion;
327 /// \brief A bitmask that indicates which code-completion contexts should
328 /// contain this completion result.
330 /// The bits in the bitmask correspond to the values of
331 /// CodeCompleteContext::Kind. To map from a completion context kind to a
332 /// bit, shift 1 by that number of bits. Many completions can occur in
333 /// several different contexts.
334 uint64_t ShowInContexts;
336 /// \brief The priority given to this code-completion result.
339 /// \brief The libclang cursor kind corresponding to this code-completion
343 /// \brief The availability of this code-completion result.
344 CXAvailabilityKind Availability;
346 /// \brief The simplified type class for a non-macro completion result.
347 SimplifiedTypeClass TypeClass;
349 /// \brief The type of a non-macro completion result, stored as a unique
350 /// integer used by the string map of cached completion types.
352 /// This value will be zero if the type is not known, or a unique value
353 /// determined by the formatted type string. Se \c CachedCompletionTypes
354 /// for more information.
358 /// \brief Retrieve the mapping from formatted type names to unique type
360 llvm::StringMap<unsigned> &getCachedCompletionTypes() {
361 return CachedCompletionTypes;
364 /// \brief Retrieve the allocator used to cache global code completions.
365 IntrusiveRefCntPtr<GlobalCodeCompletionAllocator>
366 getCachedCompletionAllocator() {
367 return CachedCompletionAllocator;
370 CodeCompletionTUInfo &getCodeCompletionTUInfo() {
372 CCTUInfo.reset(new CodeCompletionTUInfo(
373 new GlobalCodeCompletionAllocator));
378 /// \brief Allocator used to store cached code completions.
379 IntrusiveRefCntPtr<GlobalCodeCompletionAllocator>
380 CachedCompletionAllocator;
382 std::unique_ptr<CodeCompletionTUInfo> CCTUInfo;
384 /// \brief The set of cached code-completion results.
385 std::vector<CachedCodeCompletionResult> CachedCompletionResults;
387 /// \brief A mapping from the formatted type name to a unique number for that
388 /// type, which is used for type equality comparisons.
389 llvm::StringMap<unsigned> CachedCompletionTypes;
391 /// \brief A string hash of the top-level declaration and macro definition
392 /// names processed the last time that we reparsed the file.
394 /// This hash value is used to determine when we need to refresh the
395 /// global code-completion cache.
396 unsigned CompletionCacheTopLevelHashValue;
398 /// \brief A string hash of the top-level declaration and macro definition
399 /// names processed the last time that we reparsed the precompiled preamble.
401 /// This hash value is used to determine when we need to refresh the
402 /// global code-completion cache after a rebuild of the precompiled preamble.
403 unsigned PreambleTopLevelHashValue;
405 /// \brief The current hash value for the top-level declaration and macro
407 unsigned CurrentTopLevelHashValue;
409 /// \brief Bit used by CIndex to mark when a translation unit may be in an
410 /// inconsistent state, and is not safe to free.
411 unsigned UnsafeToFree : 1;
413 /// \brief Cache any "global" code-completion results, so that we can avoid
414 /// recomputing them with each completion.
415 void CacheCodeCompletionResults();
417 /// \brief Clear out and deallocate
418 void ClearCachedCompletionResults();
420 ASTUnit(const ASTUnit &) = delete;
421 void operator=(const ASTUnit &) = delete;
423 explicit ASTUnit(bool MainFileIsAST);
425 void CleanTemporaryFiles();
426 bool Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
427 std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer);
429 struct ComputedPreamble {
430 llvm::MemoryBuffer *Buffer;
431 std::unique_ptr<llvm::MemoryBuffer> Owner;
433 bool PreambleEndsAtStartOfLine;
434 ComputedPreamble(llvm::MemoryBuffer *Buffer,
435 std::unique_ptr<llvm::MemoryBuffer> Owner, unsigned Size,
436 bool PreambleEndsAtStartOfLine)
437 : Buffer(Buffer), Owner(std::move(Owner)), Size(Size),
438 PreambleEndsAtStartOfLine(PreambleEndsAtStartOfLine) {}
439 ComputedPreamble(ComputedPreamble &&C)
440 : Buffer(C.Buffer), Owner(std::move(C.Owner)), Size(C.Size),
441 PreambleEndsAtStartOfLine(C.PreambleEndsAtStartOfLine) {}
443 ComputedPreamble ComputePreamble(CompilerInvocation &Invocation,
446 std::unique_ptr<llvm::MemoryBuffer> getMainBufferWithPrecompiledPreamble(
447 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
448 const CompilerInvocation &PreambleInvocationIn, bool AllowRebuild = true,
449 unsigned MaxLines = 0);
450 void RealizeTopLevelDeclsFromPreamble();
452 /// \brief Transfers ownership of the objects (like SourceManager) from
453 /// \param CI to this ASTUnit.
454 void transferASTDataFromCompilerInstance(CompilerInstance &CI);
456 /// \brief Allows us to assert that ASTUnit is not being used concurrently,
457 /// which is not supported.
459 /// Clients should create instances of the ConcurrencyCheck class whenever
460 /// using the ASTUnit in a way that isn't intended to be concurrent, which is
461 /// just about any usage.
462 /// Becomes a noop in release mode; only useful for debug mode checking.
463 class ConcurrencyState {
464 void *Mutex; // a llvm::sys::MutexImpl in debug;
473 ConcurrencyState ConcurrencyCheckValue;
476 class ConcurrencyCheck {
480 explicit ConcurrencyCheck(ASTUnit &Self)
483 Self.ConcurrencyCheckValue.start();
485 ~ConcurrencyCheck() {
486 Self.ConcurrencyCheckValue.finish();
489 friend class ConcurrencyCheck;
493 bool isMainFileAST() const { return MainFileIsAST; }
495 bool isUnsafeToFree() const { return UnsafeToFree; }
496 void setUnsafeToFree(bool Value) { UnsafeToFree = Value; }
498 const DiagnosticsEngine &getDiagnostics() const { return *Diagnostics; }
499 DiagnosticsEngine &getDiagnostics() { return *Diagnostics; }
501 const SourceManager &getSourceManager() const { return *SourceMgr; }
502 SourceManager &getSourceManager() { return *SourceMgr; }
504 const Preprocessor &getPreprocessor() const { return *PP; }
505 Preprocessor &getPreprocessor() { return *PP; }
507 const ASTContext &getASTContext() const { return *Ctx; }
508 ASTContext &getASTContext() { return *Ctx; }
510 void setASTContext(ASTContext *ctx) { Ctx = ctx; }
511 void setPreprocessor(Preprocessor *pp);
513 bool hasSema() const { return (bool)TheSema; }
514 Sema &getSema() const {
515 assert(TheSema && "ASTUnit does not have a Sema object!");
519 const LangOptions &getLangOpts() const {
520 assert(LangOpts && " ASTUnit does not have language options");
524 const FileManager &getFileManager() const { return *FileMgr; }
525 FileManager &getFileManager() { return *FileMgr; }
527 const FileSystemOptions &getFileSystemOpts() const { return FileSystemOpts; }
529 StringRef getOriginalSourceFileName() {
530 return OriginalSourceFile;
533 ASTMutationListener *getASTMutationListener();
534 ASTDeserializationListener *getDeserializationListener();
536 /// \brief Add a temporary file that the ASTUnit depends on.
538 /// This file will be erased when the ASTUnit is destroyed.
539 void addTemporaryFile(StringRef TempFile);
541 bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
543 bool getOwnsRemappedFileBuffers() const { return OwnsRemappedFileBuffers; }
544 void setOwnsRemappedFileBuffers(bool val) { OwnsRemappedFileBuffers = val; }
546 StringRef getMainFileName() const;
548 /// \brief If this ASTUnit came from an AST file, returns the filename for it.
549 StringRef getASTFileName() const;
551 typedef std::vector<Decl *>::iterator top_level_iterator;
553 top_level_iterator top_level_begin() {
554 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
555 if (!TopLevelDeclsInPreamble.empty())
556 RealizeTopLevelDeclsFromPreamble();
557 return TopLevelDecls.begin();
560 top_level_iterator top_level_end() {
561 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
562 if (!TopLevelDeclsInPreamble.empty())
563 RealizeTopLevelDeclsFromPreamble();
564 return TopLevelDecls.end();
567 std::size_t top_level_size() const {
568 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
569 return TopLevelDeclsInPreamble.size() + TopLevelDecls.size();
572 bool top_level_empty() const {
573 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
574 return TopLevelDeclsInPreamble.empty() && TopLevelDecls.empty();
577 /// \brief Add a new top-level declaration.
578 void addTopLevelDecl(Decl *D) {
579 TopLevelDecls.push_back(D);
582 /// \brief Add a new local file-level declaration.
583 void addFileLevelDecl(Decl *D);
585 /// \brief Get the decls that are contained in a file in the Offset/Length
586 /// range. \p Length can be 0 to indicate a point at \p Offset instead of
588 void findFileRegionDecls(FileID File, unsigned Offset, unsigned Length,
589 SmallVectorImpl<Decl *> &Decls);
591 /// \brief Add a new top-level declaration, identified by its ID in
592 /// the precompiled preamble.
593 void addTopLevelDeclFromPreamble(serialization::DeclID D) {
594 TopLevelDeclsInPreamble.push_back(D);
597 /// \brief Retrieve a reference to the current top-level name hash value.
599 /// Note: This is used internally by the top-level tracking action
600 unsigned &getCurrentTopLevelHashValue() { return CurrentTopLevelHashValue; }
602 /// \brief Get the source location for the given file:line:col triplet.
604 /// The difference with SourceManager::getLocation is that this method checks
605 /// whether the requested location points inside the precompiled preamble
606 /// in which case the returned source location will be a "loaded" one.
607 SourceLocation getLocation(const FileEntry *File,
608 unsigned Line, unsigned Col) const;
610 /// \brief Get the source location for the given file:offset pair.
611 SourceLocation getLocation(const FileEntry *File, unsigned Offset) const;
613 /// \brief If \p Loc is a loaded location from the preamble, returns
614 /// the corresponding local location of the main file, otherwise it returns
616 SourceLocation mapLocationFromPreamble(SourceLocation Loc);
618 /// \brief If \p Loc is a local location of the main file but inside the
619 /// preamble chunk, returns the corresponding loaded location from the
620 /// preamble, otherwise it returns \p Loc.
621 SourceLocation mapLocationToPreamble(SourceLocation Loc);
623 bool isInPreambleFileID(SourceLocation Loc);
624 bool isInMainFileID(SourceLocation Loc);
625 SourceLocation getStartOfMainFileID();
626 SourceLocation getEndOfPreambleFileID();
628 /// \see mapLocationFromPreamble.
629 SourceRange mapRangeFromPreamble(SourceRange R) {
630 return SourceRange(mapLocationFromPreamble(R.getBegin()),
631 mapLocationFromPreamble(R.getEnd()));
634 /// \see mapLocationToPreamble.
635 SourceRange mapRangeToPreamble(SourceRange R) {
636 return SourceRange(mapLocationToPreamble(R.getBegin()),
637 mapLocationToPreamble(R.getEnd()));
640 // Retrieve the diagnostics associated with this AST
641 typedef StoredDiagnostic *stored_diag_iterator;
642 typedef const StoredDiagnostic *stored_diag_const_iterator;
643 stored_diag_const_iterator stored_diag_begin() const {
644 return StoredDiagnostics.begin();
646 stored_diag_iterator stored_diag_begin() {
647 return StoredDiagnostics.begin();
649 stored_diag_const_iterator stored_diag_end() const {
650 return StoredDiagnostics.end();
652 stored_diag_iterator stored_diag_end() {
653 return StoredDiagnostics.end();
655 unsigned stored_diag_size() const { return StoredDiagnostics.size(); }
657 stored_diag_iterator stored_diag_afterDriver_begin() {
658 if (NumStoredDiagnosticsFromDriver > StoredDiagnostics.size())
659 NumStoredDiagnosticsFromDriver = 0;
660 return StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver;
663 typedef std::vector<CachedCodeCompletionResult>::iterator
664 cached_completion_iterator;
666 cached_completion_iterator cached_completion_begin() {
667 return CachedCompletionResults.begin();
670 cached_completion_iterator cached_completion_end() {
671 return CachedCompletionResults.end();
674 unsigned cached_completion_size() const {
675 return CachedCompletionResults.size();
678 /// \brief Returns an iterator range for the local preprocessing entities
679 /// of the local Preprocessor, if this is a parsed source file, or the loaded
680 /// preprocessing entities of the primary module if this is an AST file.
681 llvm::iterator_range<PreprocessingRecord::iterator>
682 getLocalPreprocessingEntities() const;
684 /// \brief Type for a function iterating over a number of declarations.
685 /// \returns true to continue iteration and false to abort.
686 typedef bool (*DeclVisitorFn)(void *context, const Decl *D);
688 /// \brief Iterate over local declarations (locally parsed if this is a parsed
689 /// source file or the loaded declarations of the primary module if this is an
691 /// \returns true if the iteration was complete or false if it was aborted.
692 bool visitLocalTopLevelDecls(void *context, DeclVisitorFn Fn);
694 /// \brief Get the PCH file if one was included.
695 const FileEntry *getPCHFile();
697 /// \brief Returns true if the ASTUnit was constructed from a serialized
701 std::unique_ptr<llvm::MemoryBuffer>
702 getBufferForFile(StringRef Filename, std::string *ErrorStr = nullptr);
704 /// \brief Determine what kind of translation unit this AST represents.
705 TranslationUnitKind getTranslationUnitKind() const { return TUKind; }
707 /// \brief A mapping from a file name to the memory buffer that stores the
708 /// remapped contents of that file.
709 typedef std::pair<std::string, llvm::MemoryBuffer *> RemappedFile;
711 /// \brief Create a ASTUnit. Gets ownership of the passed CompilerInvocation.
712 static ASTUnit *create(CompilerInvocation *CI,
713 IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
714 bool CaptureDiagnostics,
715 bool UserFilesAreVolatile);
717 /// \brief Create a ASTUnit from an AST file.
719 /// \param Filename - The AST file to load.
721 /// \param PCHContainerOps - The PCHContainerOperations to use for loading and
722 /// creating modules.
723 /// \param Diags - The diagnostics engine to use for reporting errors; its
724 /// lifetime is expected to extend past that of the returned ASTUnit.
726 /// \returns - The initialized ASTUnit or null if the AST failed to load.
727 static std::unique_ptr<ASTUnit> LoadFromASTFile(
728 const std::string &Filename,
729 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
730 IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
731 const FileSystemOptions &FileSystemOpts, bool OnlyLocalDecls = false,
732 ArrayRef<RemappedFile> RemappedFiles = None,
733 bool CaptureDiagnostics = false, bool AllowPCHWithCompilerErrors = false,
734 bool UserFilesAreVolatile = false);
737 /// \brief Helper function for \c LoadFromCompilerInvocation() and
738 /// \c LoadFromCommandLine(), which loads an AST from a compiler invocation.
740 /// \param PrecompilePreamble Whether to precompile the preamble of this
741 /// translation unit, to improve the performance of reparsing.
743 /// \returns \c true if a catastrophic failure occurred (which means that the
744 /// \c ASTUnit itself is invalid), or \c false otherwise.
745 bool LoadFromCompilerInvocation(
746 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
747 bool PrecompilePreamble);
751 /// \brief Create an ASTUnit from a source file, via a CompilerInvocation
752 /// object, by invoking the optionally provided ASTFrontendAction.
754 /// \param CI - The compiler invocation to use; it must have exactly one input
755 /// source file. The ASTUnit takes ownership of the CompilerInvocation object.
757 /// \param PCHContainerOps - The PCHContainerOperations to use for loading and
758 /// creating modules.
760 /// \param Diags - The diagnostics engine to use for reporting errors; its
761 /// lifetime is expected to extend past that of the returned ASTUnit.
763 /// \param Action - The ASTFrontendAction to invoke. Its ownership is not
766 /// \param Unit - optionally an already created ASTUnit. Its ownership is not
769 /// \param Persistent - if true the returned ASTUnit will be complete.
770 /// false means the caller is only interested in getting info through the
771 /// provided \see Action.
773 /// \param ErrAST - If non-null and parsing failed without any AST to return
774 /// (e.g. because the PCH could not be loaded), this accepts the ASTUnit
775 /// mainly to allow the caller to see the diagnostics.
776 /// This will only receive an ASTUnit if a new one was created. If an already
777 /// created ASTUnit was passed in \p Unit then the caller can check that.
779 static ASTUnit *LoadFromCompilerInvocationAction(
780 CompilerInvocation *CI,
781 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
782 IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
783 ASTFrontendAction *Action = nullptr, ASTUnit *Unit = nullptr,
784 bool Persistent = true, StringRef ResourceFilesPath = StringRef(),
785 bool OnlyLocalDecls = false, bool CaptureDiagnostics = false,
786 bool PrecompilePreamble = false, bool CacheCodeCompletionResults = false,
787 bool IncludeBriefCommentsInCodeCompletion = false,
788 bool UserFilesAreVolatile = false,
789 std::unique_ptr<ASTUnit> *ErrAST = nullptr);
791 /// LoadFromCompilerInvocation - Create an ASTUnit from a source file, via a
792 /// CompilerInvocation object.
794 /// \param CI - The compiler invocation to use; it must have exactly one input
795 /// source file. The ASTUnit takes ownership of the CompilerInvocation object.
797 /// \param PCHContainerOps - The PCHContainerOperations to use for loading and
798 /// creating modules.
800 /// \param Diags - The diagnostics engine to use for reporting errors; its
801 /// lifetime is expected to extend past that of the returned ASTUnit.
803 // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
804 // shouldn't need to specify them at construction time.
805 static std::unique_ptr<ASTUnit> LoadFromCompilerInvocation(
806 CompilerInvocation *CI,
807 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
808 IntrusiveRefCntPtr<DiagnosticsEngine> Diags, bool OnlyLocalDecls = false,
809 bool CaptureDiagnostics = false, bool PrecompilePreamble = false,
810 TranslationUnitKind TUKind = TU_Complete,
811 bool CacheCodeCompletionResults = false,
812 bool IncludeBriefCommentsInCodeCompletion = false,
813 bool UserFilesAreVolatile = false);
815 /// LoadFromCommandLine - Create an ASTUnit from a vector of command line
816 /// arguments, which must specify exactly one source file.
818 /// \param ArgBegin - The beginning of the argument vector.
820 /// \param ArgEnd - The end of the argument vector.
822 /// \param PCHContainerOps - The PCHContainerOperations to use for loading and
823 /// creating modules.
825 /// \param Diags - The diagnostics engine to use for reporting errors; its
826 /// lifetime is expected to extend past that of the returned ASTUnit.
828 /// \param ResourceFilesPath - The path to the compiler resource files.
830 /// \param ErrAST - If non-null and parsing failed without any AST to return
831 /// (e.g. because the PCH could not be loaded), this accepts the ASTUnit
832 /// mainly to allow the caller to see the diagnostics.
834 // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
835 // shouldn't need to specify them at construction time.
836 static ASTUnit *LoadFromCommandLine(
837 const char **ArgBegin, const char **ArgEnd,
838 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
839 IntrusiveRefCntPtr<DiagnosticsEngine> Diags, StringRef ResourceFilesPath,
840 bool OnlyLocalDecls = false, bool CaptureDiagnostics = false,
841 ArrayRef<RemappedFile> RemappedFiles = None,
842 bool RemappedFilesKeepOriginalName = true,
843 bool PrecompilePreamble = false, TranslationUnitKind TUKind = TU_Complete,
844 bool CacheCodeCompletionResults = false,
845 bool IncludeBriefCommentsInCodeCompletion = false,
846 bool AllowPCHWithCompilerErrors = false, bool SkipFunctionBodies = false,
847 bool UserFilesAreVolatile = false, bool ForSerialization = false,
848 std::unique_ptr<ASTUnit> *ErrAST = nullptr);
850 /// \brief Reparse the source files using the same command-line options that
851 /// were originally used to produce this translation unit.
853 /// \returns True if a failure occurred that causes the ASTUnit not to
854 /// contain any translation-unit information, false otherwise.
855 bool Reparse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
856 ArrayRef<RemappedFile> RemappedFiles = None);
858 /// \brief Perform code completion at the given file, line, and
859 /// column within this translation unit.
861 /// \param File The file in which code completion will occur.
863 /// \param Line The line at which code completion will occur.
865 /// \param Column The column at which code completion will occur.
867 /// \param IncludeMacros Whether to include macros in the code-completion
870 /// \param IncludeCodePatterns Whether to include code patterns (such as a
871 /// for loop) in the code-completion results.
873 /// \param IncludeBriefComments Whether to include brief documentation within
874 /// the set of code completions returned.
876 /// FIXME: The Diag, LangOpts, SourceMgr, FileMgr, StoredDiagnostics, and
877 /// OwnedBuffers parameters are all disgusting hacks. They will go away.
878 void CodeComplete(StringRef File, unsigned Line, unsigned Column,
879 ArrayRef<RemappedFile> RemappedFiles, bool IncludeMacros,
880 bool IncludeCodePatterns, bool IncludeBriefComments,
881 CodeCompleteConsumer &Consumer,
882 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
883 DiagnosticsEngine &Diag, LangOptions &LangOpts,
884 SourceManager &SourceMgr, FileManager &FileMgr,
885 SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
886 SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers);
888 /// \brief Save this translation unit to a file with the given name.
890 /// \returns true if there was a file error or false if the save was
892 bool Save(StringRef File);
894 /// \brief Serialize this translation unit with the given output stream.
896 /// \returns True if an error occurred, false otherwise.
897 bool serialize(raw_ostream &OS);
899 ModuleLoadResult loadModule(SourceLocation ImportLoc, ModuleIdPath Path,
900 Module::NameVisibilityKind Visibility,
901 bool IsInclusionDirective) override {
902 // ASTUnit doesn't know how to load modules (not that this matters).
903 return ModuleLoadResult();
906 void makeModuleVisible(Module *Mod, Module::NameVisibilityKind Visibility,
907 SourceLocation ImportLoc) override {}
909 GlobalModuleIndex *loadGlobalModuleIndex(SourceLocation TriggerLoc) override
911 bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override