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