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