1 //===- ASTUnit.h - ASTUnit utility ------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // ASTUnit utility class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CLANG_FRONTEND_ASTUNIT_H
14 #define LLVM_CLANG_FRONTEND_ASTUNIT_H
15 
16 #include "clang-c/Index.h"
17 #include "clang/AST/ASTContext.h"
18 #include "clang/Basic/Diagnostic.h"
19 #include "clang/Basic/FileSystemOptions.h"
20 #include "clang/Basic/LLVM.h"
21 #include "clang/Basic/LangOptions.h"
22 #include "clang/Basic/SourceLocation.h"
23 #include "clang/Basic/SourceManager.h"
24 #include "clang/Basic/TargetOptions.h"
25 #include "clang/Lex/HeaderSearchOptions.h"
26 #include "clang/Lex/ModuleLoader.h"
27 #include "clang/Lex/PreprocessingRecord.h"
28 #include "clang/Sema/CodeCompleteConsumer.h"
29 #include "clang/Serialization/ASTBitCodes.h"
30 #include "clang/Frontend/PrecompiledPreamble.h"
31 #include "llvm/ADT/ArrayRef.h"
32 #include "llvm/ADT/DenseMap.h"
33 #include "llvm/ADT/IntrusiveRefCntPtr.h"
34 #include "llvm/ADT/STLExtras.h"
35 #include "llvm/ADT/SmallVector.h"
36 #include "llvm/ADT/StringMap.h"
37 #include "llvm/ADT/StringRef.h"
38 #include "llvm/ADT/iterator_range.h"
39 #include <cassert>
40 #include <cstddef>
41 #include <cstdint>
42 #include <memory>
43 #include <optional>
44 #include <string>
45 #include <utility>
46 #include <vector>
47 
48 namespace llvm {
49 
50 class MemoryBuffer;
51 
52 namespace vfs {
53 
54 class FileSystem;
55 
56 } // namespace vfs
57 } // namespace llvm
58 
59 namespace clang {
60 
61 class ASTContext;
62 class ASTDeserializationListener;
63 class ASTMutationListener;
64 class ASTReader;
65 class CompilerInstance;
66 class CompilerInvocation;
67 class Decl;
68 class FileEntry;
69 class FileManager;
70 class FrontendAction;
71 class HeaderSearch;
72 class InputKind;
73 class InMemoryModuleCache;
74 class PCHContainerOperations;
75 class PCHContainerReader;
76 class Preprocessor;
77 class PreprocessorOptions;
78 class Sema;
79 class TargetInfo;
80 
81 /// \brief Enumerates the available scopes for skipping function bodies.
82 enum class SkipFunctionBodiesScope { None, Preamble, PreambleAndMainFile };
83 
84 /// \brief Enumerates the available kinds for capturing diagnostics.
85 enum class CaptureDiagsKind { None, All, AllWithoutNonErrorsFromIncludes };
86 
87 /// Utility class for loading a ASTContext from an AST file.
88 class ASTUnit {
89 public:
90   struct StandaloneFixIt {
91     std::pair<unsigned, unsigned> RemoveRange;
92     std::pair<unsigned, unsigned> InsertFromRange;
93     std::string CodeToInsert;
94     bool BeforePreviousInsertions;
95   };
96 
97   struct StandaloneDiagnostic {
98     unsigned ID;
99     DiagnosticsEngine::Level Level;
100     std::string Message;
101     std::string Filename;
102     unsigned LocOffset;
103     std::vector<std::pair<unsigned, unsigned>> Ranges;
104     std::vector<StandaloneFixIt> FixIts;
105   };
106 
107 private:
108   std::shared_ptr<LangOptions>            LangOpts;
109   IntrusiveRefCntPtr<DiagnosticsEngine>   Diagnostics;
110   IntrusiveRefCntPtr<FileManager>         FileMgr;
111   IntrusiveRefCntPtr<SourceManager>       SourceMgr;
112   IntrusiveRefCntPtr<InMemoryModuleCache> ModuleCache;
113   std::unique_ptr<HeaderSearch>           HeaderInfo;
114   IntrusiveRefCntPtr<TargetInfo>          Target;
115   std::shared_ptr<Preprocessor>           PP;
116   IntrusiveRefCntPtr<ASTContext>          Ctx;
117   std::shared_ptr<TargetOptions>          TargetOpts;
118   std::shared_ptr<HeaderSearchOptions>    HSOpts;
119   std::shared_ptr<PreprocessorOptions>    PPOpts;
120   IntrusiveRefCntPtr<ASTReader> Reader;
121   bool HadModuleLoaderFatalFailure = false;
122 
123   struct ASTWriterData;
124   std::unique_ptr<ASTWriterData> WriterData;
125 
126   FileSystemOptions FileSystemOpts;
127 
128   /// The AST consumer that received information about the translation
129   /// unit as it was parsed or loaded.
130   std::unique_ptr<ASTConsumer> Consumer;
131 
132   /// The semantic analysis object used to type-check the translation
133   /// unit.
134   std::unique_ptr<Sema> TheSema;
135 
136   /// Optional owned invocation, just used to make the invocation used in
137   /// LoadFromCommandLine available.
138   std::shared_ptr<CompilerInvocation> Invocation;
139 
140   /// Fake module loader: the AST unit doesn't need to load any modules.
141   TrivialModuleLoader ModuleLoader;
142 
143   // OnlyLocalDecls - when true, walking this AST should only visit declarations
144   // that come from the AST itself, not from included precompiled headers.
145   // FIXME: This is temporary; eventually, CIndex will always do this.
146   bool OnlyLocalDecls = false;
147 
148   /// Whether to capture any diagnostics produced.
149   CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::None;
150 
151   /// Track whether the main file was loaded from an AST or not.
152   bool MainFileIsAST;
153 
154   /// What kind of translation unit this AST represents.
155   TranslationUnitKind TUKind = TU_Complete;
156 
157   /// Whether we should time each operation.
158   bool WantTiming;
159 
160   /// Whether the ASTUnit should delete the remapped buffers.
161   bool OwnsRemappedFileBuffers = true;
162 
163   /// Track the top-level decls which appeared in an ASTUnit which was loaded
164   /// from a source file.
165   //
166   // FIXME: This is just an optimization hack to avoid deserializing large parts
167   // of a PCH file when using the Index library on an ASTUnit loaded from
168   // source. In the long term we should make the Index library use efficient and
169   // more scalable search mechanisms.
170   std::vector<Decl*> TopLevelDecls;
171 
172   /// Sorted (by file offset) vector of pairs of file offset/Decl.
173   using LocDeclsTy = SmallVector<std::pair<unsigned, Decl *>, 64>;
174   using FileDeclsTy = llvm::DenseMap<FileID, std::unique_ptr<LocDeclsTy>>;
175 
176   /// Map from FileID to the file-level declarations that it contains.
177   /// The files and decls are only local (and non-preamble) ones.
178   FileDeclsTy FileDecls;
179 
180   /// The name of the original source file used to generate this ASTUnit.
181   std::string OriginalSourceFile;
182 
183   /// The set of diagnostics produced when creating the preamble.
184   SmallVector<StandaloneDiagnostic, 4> PreambleDiagnostics;
185 
186   /// The set of diagnostics produced when creating this
187   /// translation unit.
188   SmallVector<StoredDiagnostic, 4> StoredDiagnostics;
189 
190   /// The set of diagnostics produced when failing to parse, e.g. due
191   /// to failure to load the PCH.
192   SmallVector<StoredDiagnostic, 4> FailedParseDiagnostics;
193 
194   /// The number of stored diagnostics that come from the driver
195   /// itself.
196   ///
197   /// Diagnostics that come from the driver are retained from one parse to
198   /// the next.
199   unsigned NumStoredDiagnosticsFromDriver = 0;
200 
201   /// Counter that determines when we want to try building a
202   /// precompiled preamble.
203   ///
204   /// If zero, we will never build a precompiled preamble. Otherwise,
205   /// it's treated as a counter that decrements each time we reparse
206   /// without the benefit of a precompiled preamble. When it hits 1,
207   /// we'll attempt to rebuild the precompiled header. This way, if
208   /// building the precompiled preamble fails, we won't try again for
209   /// some number of calls.
210   unsigned PreambleRebuildCountdown = 0;
211 
212   /// Counter indicating how often the preamble was build in total.
213   unsigned PreambleCounter = 0;
214 
215   /// Cache pairs "filename - source location"
216   ///
217   /// Cache contains only source locations from preamble so it is
218   /// guaranteed that they stay valid when the SourceManager is recreated.
219   /// This cache is used when loading preamble to increase performance
220   /// of that loading. It must be cleared when preamble is recreated.
221   llvm::StringMap<SourceLocation> PreambleSrcLocCache;
222 
223   /// The contents of the preamble.
224   std::optional<PrecompiledPreamble> Preamble;
225 
226   /// When non-NULL, this is the buffer used to store the contents of
227   /// the main file when it has been padded for use with the precompiled
228   /// preamble.
229   std::unique_ptr<llvm::MemoryBuffer> SavedMainFileBuffer;
230 
231   /// The number of warnings that occurred while parsing the preamble.
232   ///
233   /// This value will be used to restore the state of the \c DiagnosticsEngine
234   /// object when re-using the precompiled preamble. Note that only the
235   /// number of warnings matters, since we will not save the preamble
236   /// when any errors are present.
237   unsigned NumWarningsInPreamble = 0;
238 
239   /// A list of the serialization ID numbers for each of the top-level
240   /// declarations parsed within the precompiled preamble.
241   std::vector<serialization::DeclID> TopLevelDeclsInPreamble;
242 
243   /// Whether we should be caching code-completion results.
244   bool ShouldCacheCodeCompletionResults : 1;
245 
246   /// Whether to include brief documentation within the set of code
247   /// completions cached.
248   bool IncludeBriefCommentsInCodeCompletion : 1;
249 
250   /// True if non-system source files should be treated as volatile
251   /// (likely to change while trying to use them).
252   bool UserFilesAreVolatile : 1;
253 
254   static void ConfigureDiags(IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
255                              ASTUnit &AST, CaptureDiagsKind CaptureDiagnostics);
256 
257   void TranslateStoredDiagnostics(FileManager &FileMgr,
258                                   SourceManager &SrcMan,
259                       const SmallVectorImpl<StandaloneDiagnostic> &Diags,
260                             SmallVectorImpl<StoredDiagnostic> &Out);
261 
262   void clearFileLevelDecls();
263 
264 public:
265   /// A cached code-completion result, which may be introduced in one of
266   /// many different contexts.
267   struct CachedCodeCompletionResult {
268     /// The code-completion string corresponding to this completion
269     /// result.
270     CodeCompletionString *Completion;
271 
272     /// A bitmask that indicates which code-completion contexts should
273     /// contain this completion result.
274     ///
275     /// The bits in the bitmask correspond to the values of
276     /// CodeCompleteContext::Kind. To map from a completion context kind to a
277     /// bit, shift 1 by that number of bits. Many completions can occur in
278     /// several different contexts.
279     uint64_t ShowInContexts;
280 
281     /// The priority given to this code-completion result.
282     unsigned Priority;
283 
284     /// The libclang cursor kind corresponding to this code-completion
285     /// result.
286     CXCursorKind Kind;
287 
288     /// The availability of this code-completion result.
289     CXAvailabilityKind Availability;
290 
291     /// The simplified type class for a non-macro completion result.
292     SimplifiedTypeClass TypeClass;
293 
294     /// The type of a non-macro completion result, stored as a unique
295     /// integer used by the string map of cached completion types.
296     ///
297     /// This value will be zero if the type is not known, or a unique value
298     /// determined by the formatted type string. Se \c CachedCompletionTypes
299     /// for more information.
300     unsigned Type;
301   };
302 
303   /// Retrieve the mapping from formatted type names to unique type
304   /// identifiers.
305   llvm::StringMap<unsigned> &getCachedCompletionTypes() {
306     return CachedCompletionTypes;
307   }
308 
309   /// Retrieve the allocator used to cache global code completions.
310   std::shared_ptr<GlobalCodeCompletionAllocator>
311   getCachedCompletionAllocator() {
312     return CachedCompletionAllocator;
313   }
314 
315   CodeCompletionTUInfo &getCodeCompletionTUInfo() {
316     if (!CCTUInfo)
317       CCTUInfo = std::make_unique<CodeCompletionTUInfo>(
318           std::make_shared<GlobalCodeCompletionAllocator>());
319     return *CCTUInfo;
320   }
321 
322 private:
323   /// Allocator used to store cached code completions.
324   std::shared_ptr<GlobalCodeCompletionAllocator> CachedCompletionAllocator;
325 
326   std::unique_ptr<CodeCompletionTUInfo> CCTUInfo;
327 
328   /// The set of cached code-completion results.
329   std::vector<CachedCodeCompletionResult> CachedCompletionResults;
330 
331   /// A mapping from the formatted type name to a unique number for that
332   /// type, which is used for type equality comparisons.
333   llvm::StringMap<unsigned> CachedCompletionTypes;
334 
335   /// A string hash of the top-level declaration and macro definition
336   /// names processed the last time that we reparsed the file.
337   ///
338   /// This hash value is used to determine when we need to refresh the
339   /// global code-completion cache.
340   unsigned CompletionCacheTopLevelHashValue = 0;
341 
342   /// A string hash of the top-level declaration and macro definition
343   /// names processed the last time that we reparsed the precompiled preamble.
344   ///
345   /// This hash value is used to determine when we need to refresh the
346   /// global code-completion cache after a rebuild of the precompiled preamble.
347   unsigned PreambleTopLevelHashValue = 0;
348 
349   /// The current hash value for the top-level declaration and macro
350   /// definition names
351   unsigned CurrentTopLevelHashValue = 0;
352 
353   /// Bit used by CIndex to mark when a translation unit may be in an
354   /// inconsistent state, and is not safe to free.
355   unsigned UnsafeToFree : 1;
356 
357   /// \brief Enumerator specifying the scope for skipping function bodies.
358   SkipFunctionBodiesScope SkipFunctionBodies = SkipFunctionBodiesScope::None;
359 
360   /// Cache any "global" code-completion results, so that we can avoid
361   /// recomputing them with each completion.
362   void CacheCodeCompletionResults();
363 
364   /// Clear out and deallocate
365   void ClearCachedCompletionResults();
366 
367   explicit ASTUnit(bool MainFileIsAST);
368 
369   bool Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
370              std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer,
371              IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS);
372 
373   std::unique_ptr<llvm::MemoryBuffer> getMainBufferWithPrecompiledPreamble(
374       std::shared_ptr<PCHContainerOperations> PCHContainerOps,
375       CompilerInvocation &PreambleInvocationIn,
376       IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS, bool AllowRebuild = true,
377       unsigned MaxLines = 0);
378   void RealizeTopLevelDeclsFromPreamble();
379 
380   /// Transfers ownership of the objects (like SourceManager) from
381   /// \param CI to this ASTUnit.
382   void transferASTDataFromCompilerInstance(CompilerInstance &CI);
383 
384   /// Allows us to assert that ASTUnit is not being used concurrently,
385   /// which is not supported.
386   ///
387   /// Clients should create instances of the ConcurrencyCheck class whenever
388   /// using the ASTUnit in a way that isn't intended to be concurrent, which is
389   /// just about any usage.
390   /// Becomes a noop in release mode; only useful for debug mode checking.
391   class ConcurrencyState {
392     void *Mutex; // a std::recursive_mutex in debug;
393 
394   public:
395     ConcurrencyState();
396     ~ConcurrencyState();
397 
398     void start();
399     void finish();
400   };
401   ConcurrencyState ConcurrencyCheckValue;
402 
403 public:
404   friend class ConcurrencyCheck;
405 
406   class ConcurrencyCheck {
407     ASTUnit &Self;
408 
409   public:
410     explicit ConcurrencyCheck(ASTUnit &Self) : Self(Self) {
411       Self.ConcurrencyCheckValue.start();
412     }
413 
414     ~ConcurrencyCheck() {
415       Self.ConcurrencyCheckValue.finish();
416     }
417   };
418 
419   ASTUnit(const ASTUnit &) = delete;
420   ASTUnit &operator=(const ASTUnit &) = delete;
421   ~ASTUnit();
422 
423   bool isMainFileAST() const { return MainFileIsAST; }
424 
425   bool isUnsafeToFree() const { return UnsafeToFree; }
426   void setUnsafeToFree(bool Value) { UnsafeToFree = Value; }
427 
428   const DiagnosticsEngine &getDiagnostics() const { return *Diagnostics; }
429   DiagnosticsEngine &getDiagnostics() { return *Diagnostics; }
430 
431   const SourceManager &getSourceManager() const { return *SourceMgr; }
432   SourceManager &getSourceManager() { return *SourceMgr; }
433 
434   const Preprocessor &getPreprocessor() const { return *PP; }
435   Preprocessor &getPreprocessor() { return *PP; }
436   std::shared_ptr<Preprocessor> getPreprocessorPtr() const { return PP; }
437 
438   const ASTContext &getASTContext() const { return *Ctx; }
439   ASTContext &getASTContext() { return *Ctx; }
440 
441   void setASTContext(ASTContext *ctx) { Ctx = ctx; }
442   void setPreprocessor(std::shared_ptr<Preprocessor> pp);
443 
444   /// Enable source-range based diagnostic messages.
445   ///
446   /// If diagnostic messages with source-range information are to be expected
447   /// and AST comes not from file (e.g. after LoadFromCompilerInvocation) this
448   /// function has to be called.
449   /// The function is to be called only once and the AST should be associated
450   /// with the same source file afterwards.
451   void enableSourceFileDiagnostics();
452 
453   bool hasSema() const { return (bool)TheSema; }
454 
455   Sema &getSema() const {
456     assert(TheSema && "ASTUnit does not have a Sema object!");
457     return *TheSema;
458   }
459 
460   const LangOptions &getLangOpts() const {
461     assert(LangOpts && "ASTUnit does not have language options");
462     return *LangOpts;
463   }
464 
465   const HeaderSearchOptions &getHeaderSearchOpts() const {
466     assert(HSOpts && "ASTUnit does not have header search options");
467     return *HSOpts;
468   }
469 
470   const PreprocessorOptions &getPreprocessorOpts() const {
471     assert(PPOpts && "ASTUnit does not have preprocessor options");
472     return *PPOpts;
473   }
474 
475   const FileManager &getFileManager() const { return *FileMgr; }
476   FileManager &getFileManager() { return *FileMgr; }
477 
478   const FileSystemOptions &getFileSystemOpts() const { return FileSystemOpts; }
479 
480   IntrusiveRefCntPtr<ASTReader> getASTReader() const;
481 
482   StringRef getOriginalSourceFileName() const {
483     return OriginalSourceFile;
484   }
485 
486   ASTMutationListener *getASTMutationListener();
487   ASTDeserializationListener *getDeserializationListener();
488 
489   bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
490 
491   bool getOwnsRemappedFileBuffers() const { return OwnsRemappedFileBuffers; }
492   void setOwnsRemappedFileBuffers(bool val) { OwnsRemappedFileBuffers = val; }
493 
494   StringRef getMainFileName() const;
495 
496   /// If this ASTUnit came from an AST file, returns the filename for it.
497   StringRef getASTFileName() const;
498 
499   using top_level_iterator = std::vector<Decl *>::iterator;
500 
501   top_level_iterator top_level_begin() {
502     assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
503     if (!TopLevelDeclsInPreamble.empty())
504       RealizeTopLevelDeclsFromPreamble();
505     return TopLevelDecls.begin();
506   }
507 
508   top_level_iterator top_level_end() {
509     assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
510     if (!TopLevelDeclsInPreamble.empty())
511       RealizeTopLevelDeclsFromPreamble();
512     return TopLevelDecls.end();
513   }
514 
515   std::size_t top_level_size() const {
516     assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
517     return TopLevelDeclsInPreamble.size() + TopLevelDecls.size();
518   }
519 
520   bool top_level_empty() const {
521     assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!");
522     return TopLevelDeclsInPreamble.empty() && TopLevelDecls.empty();
523   }
524 
525   /// Add a new top-level declaration.
526   void addTopLevelDecl(Decl *D) {
527     TopLevelDecls.push_back(D);
528   }
529 
530   /// Add a new local file-level declaration.
531   void addFileLevelDecl(Decl *D);
532 
533   /// Get the decls that are contained in a file in the Offset/Length
534   /// range. \p Length can be 0 to indicate a point at \p Offset instead of
535   /// a range.
536   void findFileRegionDecls(FileID File, unsigned Offset, unsigned Length,
537                            SmallVectorImpl<Decl *> &Decls);
538 
539   /// Retrieve a reference to the current top-level name hash value.
540   ///
541   /// Note: This is used internally by the top-level tracking action
542   unsigned &getCurrentTopLevelHashValue() { return CurrentTopLevelHashValue; }
543 
544   /// Get the source location for the given file:line:col triplet.
545   ///
546   /// The difference with SourceManager::getLocation is that this method checks
547   /// whether the requested location points inside the precompiled preamble
548   /// in which case the returned source location will be a "loaded" one.
549   SourceLocation getLocation(const FileEntry *File,
550                              unsigned Line, unsigned Col) const;
551 
552   /// Get the source location for the given file:offset pair.
553   SourceLocation getLocation(const FileEntry *File, unsigned Offset) const;
554 
555   /// If \p Loc is a loaded location from the preamble, returns
556   /// the corresponding local location of the main file, otherwise it returns
557   /// \p Loc.
558   SourceLocation mapLocationFromPreamble(SourceLocation Loc) const;
559 
560   /// If \p Loc is a local location of the main file but inside the
561   /// preamble chunk, returns the corresponding loaded location from the
562   /// preamble, otherwise it returns \p Loc.
563   SourceLocation mapLocationToPreamble(SourceLocation Loc) const;
564 
565   bool isInPreambleFileID(SourceLocation Loc) const;
566   bool isInMainFileID(SourceLocation Loc) const;
567   SourceLocation getStartOfMainFileID() const;
568   SourceLocation getEndOfPreambleFileID() const;
569 
570   /// \see mapLocationFromPreamble.
571   SourceRange mapRangeFromPreamble(SourceRange R) const {
572     return SourceRange(mapLocationFromPreamble(R.getBegin()),
573                        mapLocationFromPreamble(R.getEnd()));
574   }
575 
576   /// \see mapLocationToPreamble.
577   SourceRange mapRangeToPreamble(SourceRange R) const {
578     return SourceRange(mapLocationToPreamble(R.getBegin()),
579                        mapLocationToPreamble(R.getEnd()));
580   }
581 
582   unsigned getPreambleCounterForTests() const { return PreambleCounter; }
583 
584   // Retrieve the diagnostics associated with this AST
585   using stored_diag_iterator = StoredDiagnostic *;
586   using stored_diag_const_iterator = const StoredDiagnostic *;
587 
588   stored_diag_const_iterator stored_diag_begin() const {
589     return StoredDiagnostics.begin();
590   }
591 
592   stored_diag_iterator stored_diag_begin() {
593     return StoredDiagnostics.begin();
594   }
595 
596   stored_diag_const_iterator stored_diag_end() const {
597     return StoredDiagnostics.end();
598   }
599 
600   stored_diag_iterator stored_diag_end() {
601     return StoredDiagnostics.end();
602   }
603 
604   unsigned stored_diag_size() const { return StoredDiagnostics.size(); }
605 
606   stored_diag_iterator stored_diag_afterDriver_begin() {
607     if (NumStoredDiagnosticsFromDriver > StoredDiagnostics.size())
608       NumStoredDiagnosticsFromDriver = 0;
609     return StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver;
610   }
611 
612   using cached_completion_iterator =
613       std::vector<CachedCodeCompletionResult>::iterator;
614 
615   cached_completion_iterator cached_completion_begin() {
616     return CachedCompletionResults.begin();
617   }
618 
619   cached_completion_iterator cached_completion_end() {
620     return CachedCompletionResults.end();
621   }
622 
623   unsigned cached_completion_size() const {
624     return CachedCompletionResults.size();
625   }
626 
627   /// Returns an iterator range for the local preprocessing entities
628   /// of the local Preprocessor, if this is a parsed source file, or the loaded
629   /// preprocessing entities of the primary module if this is an AST file.
630   llvm::iterator_range<PreprocessingRecord::iterator>
631   getLocalPreprocessingEntities() const;
632 
633   /// Type for a function iterating over a number of declarations.
634   /// \returns true to continue iteration and false to abort.
635   using DeclVisitorFn = bool (*)(void *context, const Decl *D);
636 
637   /// Iterate over local declarations (locally parsed if this is a parsed
638   /// source file or the loaded declarations of the primary module if this is an
639   /// AST file).
640   /// \returns true if the iteration was complete or false if it was aborted.
641   bool visitLocalTopLevelDecls(void *context, DeclVisitorFn Fn);
642 
643   /// Get the PCH file if one was included.
644   const FileEntry *getPCHFile();
645 
646   /// Returns true if the ASTUnit was constructed from a serialized
647   /// module file.
648   bool isModuleFile() const;
649 
650   std::unique_ptr<llvm::MemoryBuffer>
651   getBufferForFile(StringRef Filename, std::string *ErrorStr = nullptr);
652 
653   /// Determine what kind of translation unit this AST represents.
654   TranslationUnitKind getTranslationUnitKind() const { return TUKind; }
655 
656   /// Determine the input kind this AST unit represents.
657   InputKind getInputKind() const;
658 
659   /// A mapping from a file name to the memory buffer that stores the
660   /// remapped contents of that file.
661   using RemappedFile = std::pair<std::string, llvm::MemoryBuffer *>;
662 
663   /// Create a ASTUnit. Gets ownership of the passed CompilerInvocation.
664   static std::unique_ptr<ASTUnit>
665   create(std::shared_ptr<CompilerInvocation> CI,
666          IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
667          CaptureDiagsKind CaptureDiagnostics, bool UserFilesAreVolatile);
668 
669   enum WhatToLoad {
670     /// Load options and the preprocessor state.
671     LoadPreprocessorOnly,
672 
673     /// Load the AST, but do not restore Sema state.
674     LoadASTOnly,
675 
676     /// Load everything, including Sema.
677     LoadEverything
678   };
679 
680   /// Create a ASTUnit from an AST file.
681   ///
682   /// \param Filename - The AST file to load.
683   ///
684   /// \param PCHContainerRdr - The PCHContainerOperations to use for loading and
685   /// creating modules.
686   /// \param Diags - The diagnostics engine to use for reporting errors; its
687   /// lifetime is expected to extend past that of the returned ASTUnit.
688   ///
689   /// \returns - The initialized ASTUnit or null if the AST failed to load.
690   static std::unique_ptr<ASTUnit>
691   LoadFromASTFile(const std::string &Filename,
692                   const PCHContainerReader &PCHContainerRdr, WhatToLoad ToLoad,
693                   IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
694                   const FileSystemOptions &FileSystemOpts,
695                   bool UseDebugInfo = false, bool OnlyLocalDecls = false,
696                   CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::None,
697                   bool AllowASTWithCompilerErrors = false,
698                   bool UserFilesAreVolatile = false,
699                   IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS =
700                       llvm::vfs::getRealFileSystem());
701 
702 private:
703   /// Helper function for \c LoadFromCompilerInvocation() and
704   /// \c LoadFromCommandLine(), which loads an AST from a compiler invocation.
705   ///
706   /// \param PrecompilePreambleAfterNParses After how many parses the preamble
707   /// of this translation unit should be precompiled, to improve the performance
708   /// of reparsing. Set to zero to disable preambles.
709   ///
710   /// \param VFS - A llvm::vfs::FileSystem to be used for all file accesses.
711   /// Note that preamble is saved to a temporary directory on a RealFileSystem,
712   /// so in order for it to be loaded correctly, VFS should have access to
713   /// it(i.e., be an overlay over RealFileSystem).
714   ///
715   /// \returns \c true if a catastrophic failure occurred (which means that the
716   /// \c ASTUnit itself is invalid), or \c false otherwise.
717   bool LoadFromCompilerInvocation(
718       std::shared_ptr<PCHContainerOperations> PCHContainerOps,
719       unsigned PrecompilePreambleAfterNParses,
720       IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS);
721 
722 public:
723   /// Create an ASTUnit from a source file, via a CompilerInvocation
724   /// object, by invoking the optionally provided ASTFrontendAction.
725   ///
726   /// \param CI - The compiler invocation to use; it must have exactly one input
727   /// source file. The ASTUnit takes ownership of the CompilerInvocation object.
728   ///
729   /// \param PCHContainerOps - The PCHContainerOperations to use for loading and
730   /// creating modules.
731   ///
732   /// \param Diags - The diagnostics engine to use for reporting errors; its
733   /// lifetime is expected to extend past that of the returned ASTUnit.
734   ///
735   /// \param Action - The ASTFrontendAction to invoke. Its ownership is not
736   /// transferred.
737   ///
738   /// \param Unit - optionally an already created ASTUnit. Its ownership is not
739   /// transferred.
740   ///
741   /// \param Persistent - if true the returned ASTUnit will be complete.
742   /// false means the caller is only interested in getting info through the
743   /// provided \see Action.
744   ///
745   /// \param ErrAST - If non-null and parsing failed without any AST to return
746   /// (e.g. because the PCH could not be loaded), this accepts the ASTUnit
747   /// mainly to allow the caller to see the diagnostics.
748   /// This will only receive an ASTUnit if a new one was created. If an already
749   /// created ASTUnit was passed in \p Unit then the caller can check that.
750   ///
751   static ASTUnit *LoadFromCompilerInvocationAction(
752       std::shared_ptr<CompilerInvocation> CI,
753       std::shared_ptr<PCHContainerOperations> PCHContainerOps,
754       IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
755       FrontendAction *Action = nullptr, ASTUnit *Unit = nullptr,
756       bool Persistent = true, StringRef ResourceFilesPath = StringRef(),
757       bool OnlyLocalDecls = false,
758       CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::None,
759       unsigned PrecompilePreambleAfterNParses = 0,
760       bool CacheCodeCompletionResults = false,
761       bool UserFilesAreVolatile = false,
762       std::unique_ptr<ASTUnit> *ErrAST = nullptr);
763 
764   /// LoadFromCompilerInvocation - Create an ASTUnit from a source file, via a
765   /// CompilerInvocation object.
766   ///
767   /// \param CI - The compiler invocation to use; it must have exactly one input
768   /// source file. The ASTUnit takes ownership of the CompilerInvocation object.
769   ///
770   /// \param PCHContainerOps - The PCHContainerOperations to use for loading and
771   /// creating modules.
772   ///
773   /// \param Diags - The diagnostics engine to use for reporting errors; its
774   /// lifetime is expected to extend past that of the returned ASTUnit.
775   //
776   // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
777   // shouldn't need to specify them at construction time.
778   static std::unique_ptr<ASTUnit> LoadFromCompilerInvocation(
779       std::shared_ptr<CompilerInvocation> CI,
780       std::shared_ptr<PCHContainerOperations> PCHContainerOps,
781       IntrusiveRefCntPtr<DiagnosticsEngine> Diags, FileManager *FileMgr,
782       bool OnlyLocalDecls = false,
783       CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::None,
784       unsigned PrecompilePreambleAfterNParses = 0,
785       TranslationUnitKind TUKind = TU_Complete,
786       bool CacheCodeCompletionResults = false,
787       bool IncludeBriefCommentsInCodeCompletion = false,
788       bool UserFilesAreVolatile = false);
789 
790   /// LoadFromCommandLine - Create an ASTUnit from a vector of command line
791   /// arguments, which must specify exactly one source file.
792   ///
793   /// \param ArgBegin - The beginning of the argument vector.
794   ///
795   /// \param ArgEnd - The end of the argument vector.
796   ///
797   /// \param PCHContainerOps - The PCHContainerOperations to use for loading and
798   /// creating modules.
799   ///
800   /// \param Diags - The diagnostics engine to use for reporting errors; its
801   /// lifetime is expected to extend past that of the returned ASTUnit.
802   ///
803   /// \param ResourceFilesPath - The path to the compiler resource files.
804   ///
805   /// \param ModuleFormat - If provided, uses the specific module format.
806   ///
807   /// \param ErrAST - If non-null and parsing failed without any AST to return
808   /// (e.g. because the PCH could not be loaded), this accepts the ASTUnit
809   /// mainly to allow the caller to see the diagnostics.
810   ///
811   /// \param VFS - A llvm::vfs::FileSystem to be used for all file accesses.
812   /// Note that preamble is saved to a temporary directory on a RealFileSystem,
813   /// so in order for it to be loaded correctly, VFS should have access to
814   /// it(i.e., be an overlay over RealFileSystem). RealFileSystem will be used
815   /// if \p VFS is nullptr.
816   ///
817   // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
818   // shouldn't need to specify them at construction time.
819   static ASTUnit *LoadFromCommandLine(
820       const char **ArgBegin, const char **ArgEnd,
821       std::shared_ptr<PCHContainerOperations> PCHContainerOps,
822       IntrusiveRefCntPtr<DiagnosticsEngine> Diags, StringRef ResourceFilesPath,
823       bool OnlyLocalDecls = false,
824       CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::None,
825       ArrayRef<RemappedFile> RemappedFiles = std::nullopt,
826       bool RemappedFilesKeepOriginalName = true,
827       unsigned PrecompilePreambleAfterNParses = 0,
828       TranslationUnitKind TUKind = TU_Complete,
829       bool CacheCodeCompletionResults = false,
830       bool IncludeBriefCommentsInCodeCompletion = false,
831       bool AllowPCHWithCompilerErrors = false,
832       SkipFunctionBodiesScope SkipFunctionBodies =
833           SkipFunctionBodiesScope::None,
834       bool SingleFileParse = false, bool UserFilesAreVolatile = false,
835       bool ForSerialization = false,
836       bool RetainExcludedConditionalBlocks = false,
837       std::optional<StringRef> ModuleFormat = std::nullopt,
838       std::unique_ptr<ASTUnit> *ErrAST = nullptr,
839       IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS = nullptr);
840 
841   /// Reparse the source files using the same command-line options that
842   /// were originally used to produce this translation unit.
843   ///
844   /// \param VFS - A llvm::vfs::FileSystem to be used for all file accesses.
845   /// Note that preamble is saved to a temporary directory on a RealFileSystem,
846   /// so in order for it to be loaded correctly, VFS should give an access to
847   /// this(i.e. be an overlay over RealFileSystem).
848   /// FileMgr->getVirtualFileSystem() will be used if \p VFS is nullptr.
849   ///
850   /// \returns True if a failure occurred that causes the ASTUnit not to
851   /// contain any translation-unit information, false otherwise.
852   bool Reparse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
853                ArrayRef<RemappedFile> RemappedFiles = std::nullopt,
854                IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS = nullptr);
855 
856   /// Free data that will be re-generated on the next parse.
857   ///
858   /// Preamble-related data is not affected.
859   void ResetForParse();
860 
861   /// Perform code completion at the given file, line, and
862   /// column within this translation unit.
863   ///
864   /// \param File The file in which code completion will occur.
865   ///
866   /// \param Line The line at which code completion will occur.
867   ///
868   /// \param Column The column at which code completion will occur.
869   ///
870   /// \param IncludeMacros Whether to include macros in the code-completion
871   /// results.
872   ///
873   /// \param IncludeCodePatterns Whether to include code patterns (such as a
874   /// for loop) in the code-completion results.
875   ///
876   /// \param IncludeBriefComments Whether to include brief documentation within
877   /// the set of code completions returned.
878   ///
879   /// FIXME: The Diag, LangOpts, SourceMgr, FileMgr, StoredDiagnostics, and
880   /// OwnedBuffers parameters are all disgusting hacks. They will go away.
881   void CodeComplete(StringRef File, unsigned Line, unsigned Column,
882                     ArrayRef<RemappedFile> RemappedFiles, bool IncludeMacros,
883                     bool IncludeCodePatterns, bool IncludeBriefComments,
884                     CodeCompleteConsumer &Consumer,
885                     std::shared_ptr<PCHContainerOperations> PCHContainerOps,
886                     DiagnosticsEngine &Diag, LangOptions &LangOpts,
887                     SourceManager &SourceMgr, FileManager &FileMgr,
888                     SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
889                     SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers);
890 
891   /// Save this translation unit to a file with the given name.
892   ///
893   /// \returns true if there was a file error or false if the save was
894   /// successful.
895   bool Save(StringRef File);
896 
897   /// Serialize this translation unit with the given output stream.
898   ///
899   /// \returns True if an error occurred, false otherwise.
900   bool serialize(raw_ostream &OS);
901 };
902 
903 } // namespace clang
904 
905 #endif // LLVM_CLANG_FRONTEND_ASTUNIT_H
906