ASTUnit.h 34 KB

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