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