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/FileSystemOptions.h"
20 #include "clang/Basic/LangOptions.h"
21 #include "clang/Basic/SourceManager.h"
22 #include "clang/Basic/TargetOptions.h"
23 #include "clang/Lex/HeaderSearchOptions.h"
24 #include "clang/Lex/ModuleLoader.h"
25 #include "clang/Lex/PreprocessingRecord.h"
26 #include "clang/Sema/CodeCompleteConsumer.h"
27 #include "clang/Serialization/ASTBitCodes.h"
28 #include "clang/Frontend/PrecompiledPreamble.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"
36 #include <sys/types.h>
48 class CompilerInvocation;
49 class CompilerInstance;
51 class DiagnosticsEngine;
56 class MemoryBufferCache;
58 class PreprocessorOptions;
59 class PCHContainerOperations;
60 class PCHContainerReader;
63 class ASTDeserializationListener;
69 /// \brief Utility class for loading a ASTContext from an AST file.
73 struct StandaloneFixIt {
74 std::pair<unsigned, unsigned> RemoveRange;
75 std::pair<unsigned, unsigned> InsertFromRange;
76 std::string CodeToInsert;
77 bool BeforePreviousInsertions;
80 struct StandaloneDiagnostic {
82 DiagnosticsEngine::Level Level;
86 std::vector<std::pair<unsigned, unsigned> > Ranges;
87 std::vector<StandaloneFixIt> FixIts;
91 std::shared_ptr<LangOptions> LangOpts;
92 IntrusiveRefCntPtr<DiagnosticsEngine> Diagnostics;
93 IntrusiveRefCntPtr<FileManager> FileMgr;
94 IntrusiveRefCntPtr<SourceManager> SourceMgr;
95 IntrusiveRefCntPtr<MemoryBufferCache> PCMCache;
96 std::unique_ptr<HeaderSearch> HeaderInfo;
97 IntrusiveRefCntPtr<TargetInfo> Target;
98 std::shared_ptr<Preprocessor> PP;
99 IntrusiveRefCntPtr<ASTContext> Ctx;
100 std::shared_ptr<TargetOptions> TargetOpts;
101 std::shared_ptr<HeaderSearchOptions> HSOpts;
102 std::shared_ptr<PreprocessorOptions> PPOpts;
103 IntrusiveRefCntPtr<ASTReader> Reader;
104 bool HadModuleLoaderFatalFailure;
106 struct ASTWriterData;
107 std::unique_ptr<ASTWriterData> WriterData;
109 FileSystemOptions FileSystemOpts;
111 /// \brief The AST consumer that received information about the translation
112 /// unit as it was parsed or loaded.
113 std::unique_ptr<ASTConsumer> Consumer;
115 /// \brief The semantic analysis object used to type-check the translation
117 std::unique_ptr<Sema> TheSema;
119 /// Optional owned invocation, just used to make the invocation used in
120 /// LoadFromCommandLine available.
121 std::shared_ptr<CompilerInvocation> Invocation;
123 /// Fake module loader: the AST unit doesn't need to load any modules.
124 TrivialModuleLoader ModuleLoader;
126 // OnlyLocalDecls - when true, walking this AST should only visit declarations
127 // that come from the AST itself, not from included precompiled headers.
128 // FIXME: This is temporary; eventually, CIndex will always do this.
131 /// \brief Whether to capture any diagnostics produced.
132 bool CaptureDiagnostics;
134 /// \brief Track whether the main file was loaded from an AST or not.
137 /// \brief What kind of translation unit this AST represents.
138 TranslationUnitKind TUKind;
140 /// \brief Whether we should time each operation.
143 /// \brief Whether the ASTUnit should delete the remapped buffers.
144 bool OwnsRemappedFileBuffers;
146 /// Track the top-level decls which appeared in an ASTUnit which was loaded
147 /// from a source file.
149 // FIXME: This is just an optimization hack to avoid deserializing large parts
150 // of a PCH file when using the Index library on an ASTUnit loaded from
151 // source. In the long term we should make the Index library use efficient and
152 // more scalable search mechanisms.
153 std::vector<Decl*> TopLevelDecls;
155 /// \brief Sorted (by file offset) vector of pairs of file offset/Decl.
156 typedef SmallVector<std::pair<unsigned, Decl *>, 64> LocDeclsTy;
157 typedef llvm::DenseMap<FileID, LocDeclsTy *> FileDeclsTy;
159 /// \brief Map from FileID to the file-level declarations that it contains.
160 /// The files and decls are only local (and non-preamble) ones.
161 FileDeclsTy FileDecls;
163 /// The name of the original source file used to generate this ASTUnit.
164 std::string OriginalSourceFile;
166 /// \brief The set of diagnostics produced when creating the preamble.
167 SmallVector<StandaloneDiagnostic, 4> PreambleDiagnostics;
169 /// \brief The set of diagnostics produced when creating this
170 /// translation unit.
171 SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
173 /// \brief The set of diagnostics produced when failing to parse, e.g. due
174 /// to failure to load the PCH.
175 SmallVector<StoredDiagnostic, 4> FailedParseDiagnostics;
177 /// \brief The number of stored diagnostics that come from the driver
180 /// Diagnostics that come from the driver are retained from one parse to
182 unsigned NumStoredDiagnosticsFromDriver;
184 /// \brief Counter that determines when we want to try building a
185 /// precompiled preamble.
187 /// If zero, we will never build a precompiled preamble. Otherwise,
188 /// it's treated as a counter that decrements each time we reparse
189 /// without the benefit of a precompiled preamble. When it hits 1,
190 /// we'll attempt to rebuild the precompiled header. This way, if
191 /// building the precompiled preamble fails, we won't try again for
192 /// some number of calls.
193 unsigned PreambleRebuildCounter;
195 /// \brief Cache pairs "filename - source location"
197 /// Cache contains only source locations from preamble so it is
198 /// guaranteed that they stay valid when the SourceManager is recreated.
199 /// This cache is used when loading preambule to increase performance
200 /// of that loading. It must be cleared when preamble is recreated.
201 llvm::StringMap<SourceLocation> PreambleSrcLocCache;
204 /// The contents of the preamble.
205 llvm::Optional<PrecompiledPreamble> Preamble;
207 /// \brief When non-NULL, this is the buffer used to store the contents of
208 /// the main file when it has been padded for use with the precompiled
210 std::unique_ptr<llvm::MemoryBuffer> SavedMainFileBuffer;
212 /// \brief The number of warnings that occurred while parsing the preamble.
214 /// This value will be used to restore the state of the \c DiagnosticsEngine
215 /// object when re-using the precompiled preamble. Note that only the
216 /// number of warnings matters, since we will not save the preamble
217 /// when any errors are present.
218 unsigned NumWarningsInPreamble;
220 /// \brief A list of the serialization ID numbers for each of the top-level
221 /// declarations parsed within the precompiled preamble.
222 std::vector<serialization::DeclID> TopLevelDeclsInPreamble;
224 /// \brief Whether we should be caching code-completion results.
225 bool ShouldCacheCodeCompletionResults : 1;
227 /// \brief Whether to include brief documentation within the set of code
228 /// completions cached.
229 bool IncludeBriefCommentsInCodeCompletion : 1;
231 /// \brief True if non-system source files should be treated as volatile
232 /// (likely to change while trying to use them).
233 bool UserFilesAreVolatile : 1;
235 static void ConfigureDiags(IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
236 ASTUnit &AST, bool CaptureDiagnostics);
238 void TranslateStoredDiagnostics(FileManager &FileMgr,
239 SourceManager &SrcMan,
240 const SmallVectorImpl<StandaloneDiagnostic> &Diags,
241 SmallVectorImpl<StoredDiagnostic> &Out);
243 void clearFileLevelDecls();
246 /// \brief A cached code-completion result, which may be introduced in one of
247 /// many different contexts.
248 struct CachedCodeCompletionResult {
249 /// \brief The code-completion string corresponding to this completion
251 CodeCompletionString *Completion;
253 /// \brief A bitmask that indicates which code-completion contexts should
254 /// contain this completion result.
256 /// The bits in the bitmask correspond to the values of
257 /// CodeCompleteContext::Kind. To map from a completion context kind to a
258 /// bit, shift 1 by that number of bits. Many completions can occur in
259 /// several different contexts.
260 uint64_t ShowInContexts;
262 /// \brief The priority given to this code-completion result.
265 /// \brief The libclang cursor kind corresponding to this code-completion
269 /// \brief The availability of this code-completion result.
270 CXAvailabilityKind Availability;
272 /// \brief The simplified type class for a non-macro completion result.
273 SimplifiedTypeClass TypeClass;
275 /// \brief The type of a non-macro completion result, stored as a unique
276 /// integer used by the string map of cached completion types.
278 /// This value will be zero if the type is not known, or a unique value
279 /// determined by the formatted type string. Se \c CachedCompletionTypes
280 /// for more information.
284 /// \brief Retrieve the mapping from formatted type names to unique type
286 llvm::StringMap<unsigned> &getCachedCompletionTypes() {
287 return CachedCompletionTypes;
290 /// \brief Retrieve the allocator used to cache global code completions.
291 std::shared_ptr<GlobalCodeCompletionAllocator>
292 getCachedCompletionAllocator() {
293 return CachedCompletionAllocator;
296 CodeCompletionTUInfo &getCodeCompletionTUInfo() {
298 CCTUInfo = llvm::make_unique<CodeCompletionTUInfo>(
299 std::make_shared<GlobalCodeCompletionAllocator>());
304 /// \brief Allocator used to store cached code completions.
305 std::shared_ptr<GlobalCodeCompletionAllocator> CachedCompletionAllocator;
307 std::unique_ptr<CodeCompletionTUInfo> CCTUInfo;
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 &) = delete;
346 void operator=(const ASTUnit &) = delete;
348 explicit ASTUnit(bool MainFileIsAST);
350 bool Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
351 std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer,
352 IntrusiveRefCntPtr<vfs::FileSystem> VFS);
354 std::unique_ptr<llvm::MemoryBuffer> getMainBufferWithPrecompiledPreamble(
355 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
356 const CompilerInvocation &PreambleInvocationIn,
357 IntrusiveRefCntPtr<vfs::FileSystem> VFS, bool AllowRebuild = true,
358 unsigned MaxLines = 0);
359 void RealizeTopLevelDeclsFromPreamble();
361 /// \brief Transfers ownership of the objects (like SourceManager) from
362 /// \param CI to this ASTUnit.
363 void transferASTDataFromCompilerInstance(CompilerInstance &CI);
365 /// \brief Allows us to assert that ASTUnit is not being used concurrently,
366 /// which is not supported.
368 /// Clients should create instances of the ConcurrencyCheck class whenever
369 /// using the ASTUnit in a way that isn't intended to be concurrent, which is
370 /// just about any usage.
371 /// Becomes a noop in release mode; only useful for debug mode checking.
372 class ConcurrencyState {
373 void *Mutex; // a llvm::sys::MutexImpl in debug;
382 ConcurrencyState ConcurrencyCheckValue;
385 class ConcurrencyCheck {
389 explicit ConcurrencyCheck(ASTUnit &Self)
392 Self.ConcurrencyCheckValue.start();
394 ~ConcurrencyCheck() {
395 Self.ConcurrencyCheckValue.finish();
398 friend class ConcurrencyCheck;
402 bool isMainFileAST() const { return MainFileIsAST; }
404 bool isUnsafeToFree() const { return UnsafeToFree; }
405 void setUnsafeToFree(bool Value) { UnsafeToFree = Value; }
407 const DiagnosticsEngine &getDiagnostics() const { return *Diagnostics; }
408 DiagnosticsEngine &getDiagnostics() { return *Diagnostics; }
410 const SourceManager &getSourceManager() const { return *SourceMgr; }
411 SourceManager &getSourceManager() { return *SourceMgr; }
413 const Preprocessor &getPreprocessor() const { return *PP; }
414 Preprocessor &getPreprocessor() { return *PP; }
415 std::shared_ptr<Preprocessor> getPreprocessorPtr() const { return PP; }
417 const ASTContext &getASTContext() const { return *Ctx; }
418 ASTContext &getASTContext() { return *Ctx; }
420 void setASTContext(ASTContext *ctx) { Ctx = ctx; }
421 void setPreprocessor(std::shared_ptr<Preprocessor> pp);
423 bool hasSema() const { return (bool)TheSema; }
424 Sema &getSema() const {
425 assert(TheSema && "ASTUnit does not have a Sema object!");
429 const LangOptions &getLangOpts() const {
430 assert(LangOpts && "ASTUnit does not have language options");
434 const HeaderSearchOptions &getHeaderSearchOpts() const {
435 assert(HSOpts && "ASTUnit does not have header search options");
439 const PreprocessorOptions &getPreprocessorOpts() const {
440 assert(PPOpts && "ASTUnit does not have preprocessor options");
444 const FileManager &getFileManager() const { return *FileMgr; }
445 FileManager &getFileManager() { return *FileMgr; }
447 const FileSystemOptions &getFileSystemOpts() const { return FileSystemOpts; }
449 IntrusiveRefCntPtr<ASTReader> getASTReader() const;
451 StringRef getOriginalSourceFileName() const {
452 return OriginalSourceFile;
455 ASTMutationListener *getASTMutationListener();
456 ASTDeserializationListener *getDeserializationListener();
458 bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
460 bool getOwnsRemappedFileBuffers() const { return OwnsRemappedFileBuffers; }
461 void setOwnsRemappedFileBuffers(bool val) { OwnsRemappedFileBuffers = val; }
463 StringRef getMainFileName() const;
465 /// \brief If this ASTUnit came from an AST file, returns the filename for it.
466 StringRef getASTFileName() const;
468 typedef std::vector<Decl *>::iterator top_level_iterator;
470 top_level_iterator top_level_begin() {
471 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
472 if (!TopLevelDeclsInPreamble.empty())
473 RealizeTopLevelDeclsFromPreamble();
474 return TopLevelDecls.begin();
477 top_level_iterator top_level_end() {
478 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
479 if (!TopLevelDeclsInPreamble.empty())
480 RealizeTopLevelDeclsFromPreamble();
481 return TopLevelDecls.end();
484 std::size_t top_level_size() const {
485 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
486 return TopLevelDeclsInPreamble.size() + TopLevelDecls.size();
489 bool top_level_empty() const {
490 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
491 return TopLevelDeclsInPreamble.empty() && TopLevelDecls.empty();
494 /// \brief Add a new top-level declaration.
495 void addTopLevelDecl(Decl *D) {
496 TopLevelDecls.push_back(D);
499 /// \brief Add a new local file-level declaration.
500 void addFileLevelDecl(Decl *D);
502 /// \brief Get the decls that are contained in a file in the Offset/Length
503 /// range. \p Length can be 0 to indicate a point at \p Offset instead of
505 void findFileRegionDecls(FileID File, unsigned Offset, unsigned Length,
506 SmallVectorImpl<Decl *> &Decls);
508 /// \brief Retrieve a reference to the current top-level name hash value.
510 /// Note: This is used internally by the top-level tracking action
511 unsigned &getCurrentTopLevelHashValue() { return CurrentTopLevelHashValue; }
513 /// \brief Get the source location for the given file:line:col triplet.
515 /// The difference with SourceManager::getLocation is that this method checks
516 /// whether the requested location points inside the precompiled preamble
517 /// in which case the returned source location will be a "loaded" one.
518 SourceLocation getLocation(const FileEntry *File,
519 unsigned Line, unsigned Col) const;
521 /// \brief Get the source location for the given file:offset pair.
522 SourceLocation getLocation(const FileEntry *File, unsigned Offset) const;
524 /// \brief If \p Loc is a loaded location from the preamble, returns
525 /// the corresponding local location of the main file, otherwise it returns
527 SourceLocation mapLocationFromPreamble(SourceLocation Loc) const;
529 /// \brief If \p Loc is a local location of the main file but inside the
530 /// preamble chunk, returns the corresponding loaded location from the
531 /// preamble, otherwise it returns \p Loc.
532 SourceLocation mapLocationToPreamble(SourceLocation Loc) const;
534 bool isInPreambleFileID(SourceLocation Loc) const;
535 bool isInMainFileID(SourceLocation Loc) const;
536 SourceLocation getStartOfMainFileID() const;
537 SourceLocation getEndOfPreambleFileID() const;
539 /// \see mapLocationFromPreamble.
540 SourceRange mapRangeFromPreamble(SourceRange R) const {
541 return SourceRange(mapLocationFromPreamble(R.getBegin()),
542 mapLocationFromPreamble(R.getEnd()));
545 /// \see mapLocationToPreamble.
546 SourceRange mapRangeToPreamble(SourceRange R) const {
547 return SourceRange(mapLocationToPreamble(R.getBegin()),
548 mapLocationToPreamble(R.getEnd()));
551 // Retrieve the diagnostics associated with this AST
552 typedef StoredDiagnostic *stored_diag_iterator;
553 typedef const StoredDiagnostic *stored_diag_const_iterator;
554 stored_diag_const_iterator stored_diag_begin() const {
555 return StoredDiagnostics.begin();
557 stored_diag_iterator stored_diag_begin() {
558 return StoredDiagnostics.begin();
560 stored_diag_const_iterator stored_diag_end() const {
561 return StoredDiagnostics.end();
563 stored_diag_iterator stored_diag_end() {
564 return StoredDiagnostics.end();
566 unsigned stored_diag_size() const { return StoredDiagnostics.size(); }
568 stored_diag_iterator stored_diag_afterDriver_begin() {
569 if (NumStoredDiagnosticsFromDriver > StoredDiagnostics.size())
570 NumStoredDiagnosticsFromDriver = 0;
571 return StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver;
574 typedef std::vector<CachedCodeCompletionResult>::iterator
575 cached_completion_iterator;
577 cached_completion_iterator cached_completion_begin() {
578 return CachedCompletionResults.begin();
581 cached_completion_iterator cached_completion_end() {
582 return CachedCompletionResults.end();
585 unsigned cached_completion_size() const {
586 return CachedCompletionResults.size();
589 /// \brief Returns an iterator range for the local preprocessing entities
590 /// of the local Preprocessor, if this is a parsed source file, or the loaded
591 /// preprocessing entities of the primary module if this is an AST file.
592 llvm::iterator_range<PreprocessingRecord::iterator>
593 getLocalPreprocessingEntities() const;
595 /// \brief Type for a function iterating over a number of declarations.
596 /// \returns true to continue iteration and false to abort.
597 typedef bool (*DeclVisitorFn)(void *context, const Decl *D);
599 /// \brief Iterate over local declarations (locally parsed if this is a parsed
600 /// source file or the loaded declarations of the primary module if this is an
602 /// \returns true if the iteration was complete or false if it was aborted.
603 bool visitLocalTopLevelDecls(void *context, DeclVisitorFn Fn);
605 /// \brief Get the PCH file if one was included.
606 const FileEntry *getPCHFile();
608 /// \brief Returns true if the ASTUnit was constructed from a serialized
610 bool isModuleFile() const;
612 std::unique_ptr<llvm::MemoryBuffer>
613 getBufferForFile(StringRef Filename, std::string *ErrorStr = nullptr);
615 /// \brief Determine what kind of translation unit this AST represents.
616 TranslationUnitKind getTranslationUnitKind() const { return TUKind; }
618 /// \brief Determine the input kind this AST unit represents.
619 InputKind getInputKind() const;
621 /// \brief A mapping from a file name to the memory buffer that stores the
622 /// remapped contents of that file.
623 typedef std::pair<std::string, llvm::MemoryBuffer *> RemappedFile;
625 /// \brief Create a ASTUnit. Gets ownership of the passed CompilerInvocation.
626 static std::unique_ptr<ASTUnit>
627 create(std::shared_ptr<CompilerInvocation> CI,
628 IntrusiveRefCntPtr<DiagnosticsEngine> Diags, bool CaptureDiagnostics,
629 bool UserFilesAreVolatile);
632 /// Load options and the preprocessor state.
633 LoadPreprocessorOnly,
634 /// Load the AST, but do not restore Sema state.
636 /// Load everything, including Sema.
640 /// \brief Create a ASTUnit from an AST file.
642 /// \param Filename - The AST file to load.
644 /// \param PCHContainerRdr - The PCHContainerOperations to use for loading and
645 /// creating modules.
646 /// \param Diags - The diagnostics engine to use for reporting errors; its
647 /// lifetime is expected to extend past that of the returned ASTUnit.
649 /// \returns - The initialized ASTUnit or null if the AST failed to load.
650 static std::unique_ptr<ASTUnit> LoadFromASTFile(
651 const std::string &Filename, const PCHContainerReader &PCHContainerRdr,
652 WhatToLoad ToLoad, IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
653 const FileSystemOptions &FileSystemOpts, bool UseDebugInfo = false,
654 bool OnlyLocalDecls = false, ArrayRef<RemappedFile> RemappedFiles = None,
655 bool CaptureDiagnostics = false, bool AllowPCHWithCompilerErrors = false,
656 bool UserFilesAreVolatile = false);
659 /// \brief Helper function for \c LoadFromCompilerInvocation() and
660 /// \c LoadFromCommandLine(), which loads an AST from a compiler invocation.
662 /// \param PrecompilePreambleAfterNParses After how many parses the preamble
663 /// of this translation unit should be precompiled, to improve the performance
664 /// of reparsing. Set to zero to disable preambles.
666 /// \param VFS - A vfs::FileSystem to be used for all file accesses. Note that
667 /// preamble is saved to a temporary directory on a RealFileSystem, so in order
668 /// for it to be loaded correctly, VFS should have access to it(i.e., be an
669 /// overlay over RealFileSystem).
671 /// \returns \c true if a catastrophic failure occurred (which means that the
672 /// \c ASTUnit itself is invalid), or \c false otherwise.
673 bool LoadFromCompilerInvocation(
674 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
675 unsigned PrecompilePreambleAfterNParses,
676 IntrusiveRefCntPtr<vfs::FileSystem> VFS);
680 /// \brief Create an ASTUnit from a source file, via a CompilerInvocation
681 /// object, by invoking the optionally provided ASTFrontendAction.
683 /// \param CI - The compiler invocation to use; it must have exactly one input
684 /// source file. The ASTUnit takes ownership of the CompilerInvocation object.
686 /// \param PCHContainerOps - The PCHContainerOperations to use for loading and
687 /// creating modules.
689 /// \param Diags - The diagnostics engine to use for reporting errors; its
690 /// lifetime is expected to extend past that of the returned ASTUnit.
692 /// \param Action - The ASTFrontendAction to invoke. Its ownership is not
695 /// \param Unit - optionally an already created ASTUnit. Its ownership is not
698 /// \param Persistent - if true the returned ASTUnit will be complete.
699 /// false means the caller is only interested in getting info through the
700 /// provided \see Action.
702 /// \param ErrAST - If non-null and parsing failed without any AST to return
703 /// (e.g. because the PCH could not be loaded), this accepts the ASTUnit
704 /// mainly to allow the caller to see the diagnostics.
705 /// This will only receive an ASTUnit if a new one was created. If an already
706 /// created ASTUnit was passed in \p Unit then the caller can check that.
708 static ASTUnit *LoadFromCompilerInvocationAction(
709 std::shared_ptr<CompilerInvocation> CI,
710 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
711 IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
712 FrontendAction *Action = nullptr, ASTUnit *Unit = nullptr,
713 bool Persistent = true, StringRef ResourceFilesPath = StringRef(),
714 bool OnlyLocalDecls = false, bool CaptureDiagnostics = false,
715 unsigned PrecompilePreambleAfterNParses = 0,
716 bool CacheCodeCompletionResults = false,
717 bool IncludeBriefCommentsInCodeCompletion = false,
718 bool UserFilesAreVolatile = false,
719 std::unique_ptr<ASTUnit> *ErrAST = nullptr);
721 /// LoadFromCompilerInvocation - Create an ASTUnit from a source file, via a
722 /// CompilerInvocation object.
724 /// \param CI - The compiler invocation to use; it must have exactly one input
725 /// source file. The ASTUnit takes ownership of the CompilerInvocation object.
727 /// \param PCHContainerOps - The PCHContainerOperations to use for loading and
728 /// creating modules.
730 /// \param Diags - The diagnostics engine to use for reporting errors; its
731 /// lifetime is expected to extend past that of the returned ASTUnit.
733 // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
734 // shouldn't need to specify them at construction time.
735 static std::unique_ptr<ASTUnit> LoadFromCompilerInvocation(
736 std::shared_ptr<CompilerInvocation> CI,
737 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
738 IntrusiveRefCntPtr<DiagnosticsEngine> Diags, FileManager *FileMgr,
739 bool OnlyLocalDecls = false, bool CaptureDiagnostics = false,
740 unsigned PrecompilePreambleAfterNParses = 0,
741 TranslationUnitKind TUKind = TU_Complete,
742 bool CacheCodeCompletionResults = false,
743 bool IncludeBriefCommentsInCodeCompletion = false,
744 bool UserFilesAreVolatile = false);
746 /// LoadFromCommandLine - Create an ASTUnit from a vector of command line
747 /// arguments, which must specify exactly one source file.
749 /// \param ArgBegin - The beginning of the argument vector.
751 /// \param ArgEnd - The end of the argument vector.
753 /// \param PCHContainerOps - The PCHContainerOperations to use for loading and
754 /// creating modules.
756 /// \param Diags - The diagnostics engine to use for reporting errors; its
757 /// lifetime is expected to extend past that of the returned ASTUnit.
759 /// \param ResourceFilesPath - The path to the compiler resource files.
761 /// \param ModuleFormat - If provided, uses the specific module format.
763 /// \param ErrAST - If non-null and parsing failed without any AST to return
764 /// (e.g. because the PCH could not be loaded), this accepts the ASTUnit
765 /// mainly to allow the caller to see the diagnostics.
767 /// \param VFS - A vfs::FileSystem to be used for all file accesses. Note that
768 /// preamble is saved to a temporary directory on a RealFileSystem, so in order
769 /// for it to be loaded correctly, VFS should have access to it(i.e., be an
770 /// overlay over RealFileSystem). RealFileSystem will be used if \p VFS is nullptr.
772 // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
773 // shouldn't need to specify them at construction time.
774 static ASTUnit *LoadFromCommandLine(
775 const char **ArgBegin, const char **ArgEnd,
776 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
777 IntrusiveRefCntPtr<DiagnosticsEngine> Diags, StringRef ResourceFilesPath,
778 bool OnlyLocalDecls = false, bool CaptureDiagnostics = false,
779 ArrayRef<RemappedFile> RemappedFiles = None,
780 bool RemappedFilesKeepOriginalName = true,
781 unsigned PrecompilePreambleAfterNParses = 0,
782 TranslationUnitKind TUKind = TU_Complete,
783 bool CacheCodeCompletionResults = false,
784 bool IncludeBriefCommentsInCodeCompletion = false,
785 bool AllowPCHWithCompilerErrors = false, bool SkipFunctionBodies = false,
786 bool SingleFileParse = false,
787 bool UserFilesAreVolatile = false, bool ForSerialization = false,
788 llvm::Optional<StringRef> ModuleFormat = llvm::None,
789 std::unique_ptr<ASTUnit> *ErrAST = nullptr,
790 IntrusiveRefCntPtr<vfs::FileSystem> VFS = nullptr);
792 /// \brief Reparse the source files using the same command-line options that
793 /// were originally used to produce this translation unit.
795 /// \param VFS - A vfs::FileSystem to be used for all file accesses. Note that
796 /// preamble is saved to a temporary directory on a RealFileSystem, so in order
797 /// for it to be loaded correctly, VFS should give an access to this(i.e. be an
798 /// overlay over RealFileSystem). FileMgr->getVirtualFileSystem() will be used if
799 /// \p VFS is nullptr.
801 /// \returns True if a failure occurred that causes the ASTUnit not to
802 /// contain any translation-unit information, false otherwise.
803 bool Reparse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
804 ArrayRef<RemappedFile> RemappedFiles = None,
805 IntrusiveRefCntPtr<vfs::FileSystem> VFS = nullptr);
807 /// \brief Free data that will be re-generated on the next parse.
809 /// Preamble-related data is not affected.
810 void ResetForParse();
812 /// \brief Perform code completion at the given file, line, and
813 /// column within this translation unit.
815 /// \param File The file in which code completion will occur.
817 /// \param Line The line at which code completion will occur.
819 /// \param Column The column at which code completion will occur.
821 /// \param IncludeMacros Whether to include macros in the code-completion
824 /// \param IncludeCodePatterns Whether to include code patterns (such as a
825 /// for loop) in the code-completion results.
827 /// \param IncludeBriefComments Whether to include brief documentation within
828 /// the set of code completions returned.
830 /// FIXME: The Diag, LangOpts, SourceMgr, FileMgr, StoredDiagnostics, and
831 /// OwnedBuffers parameters are all disgusting hacks. They will go away.
832 void CodeComplete(StringRef File, unsigned Line, unsigned Column,
833 ArrayRef<RemappedFile> RemappedFiles, bool IncludeMacros,
834 bool IncludeCodePatterns, bool IncludeBriefComments,
835 CodeCompleteConsumer &Consumer,
836 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
837 DiagnosticsEngine &Diag, LangOptions &LangOpts,
838 SourceManager &SourceMgr, FileManager &FileMgr,
839 SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
840 SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers);
842 /// \brief Save this translation unit to a file with the given name.
844 /// \returns true if there was a file error or false if the save was
846 bool Save(StringRef File);
848 /// \brief Serialize this translation unit with the given output stream.
850 /// \returns True if an error occurred, false otherwise.
851 bool serialize(raw_ostream &OS);