ASTUnit.cpp revision c042edd54face617a3b9d0b4b9d5a3ff229d0f48
1//===--- ASTUnit.cpp - ASTUnit utility ------------------------------------===// 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 Implementation. 11// 12//===----------------------------------------------------------------------===// 13 14#include "clang/Frontend/ASTUnit.h" 15#include "clang/AST/ASTContext.h" 16#include "clang/AST/ASTConsumer.h" 17#include "clang/AST/DeclVisitor.h" 18#include "clang/AST/TypeOrdering.h" 19#include "clang/AST/StmtVisitor.h" 20#include "clang/Frontend/CompilerInstance.h" 21#include "clang/Frontend/FrontendActions.h" 22#include "clang/Frontend/FrontendDiagnostic.h" 23#include "clang/Frontend/FrontendOptions.h" 24#include "clang/Frontend/MultiplexConsumer.h" 25#include "clang/Frontend/Utils.h" 26#include "clang/Serialization/ASTReader.h" 27#include "clang/Serialization/ASTWriter.h" 28#include "clang/Lex/HeaderSearch.h" 29#include "clang/Lex/Preprocessor.h" 30#include "clang/Basic/TargetOptions.h" 31#include "clang/Basic/TargetInfo.h" 32#include "clang/Basic/Diagnostic.h" 33#include "llvm/ADT/ArrayRef.h" 34#include "llvm/ADT/StringExtras.h" 35#include "llvm/ADT/StringSet.h" 36#include "llvm/Support/Atomic.h" 37#include "llvm/Support/MemoryBuffer.h" 38#include "llvm/Support/Host.h" 39#include "llvm/Support/Path.h" 40#include "llvm/Support/raw_ostream.h" 41#include "llvm/Support/Timer.h" 42#include "llvm/Support/FileSystem.h" 43#include "llvm/Support/Mutex.h" 44#include "llvm/Support/MutexGuard.h" 45#include "llvm/Support/CrashRecoveryContext.h" 46#include <cstdlib> 47#include <cstdio> 48#include <sys/stat.h> 49using namespace clang; 50 51using llvm::TimeRecord; 52 53namespace { 54 class SimpleTimer { 55 bool WantTiming; 56 TimeRecord Start; 57 std::string Output; 58 59 public: 60 explicit SimpleTimer(bool WantTiming) : WantTiming(WantTiming) { 61 if (WantTiming) 62 Start = TimeRecord::getCurrentTime(); 63 } 64 65 void setOutput(const Twine &Output) { 66 if (WantTiming) 67 this->Output = Output.str(); 68 } 69 70 ~SimpleTimer() { 71 if (WantTiming) { 72 TimeRecord Elapsed = TimeRecord::getCurrentTime(); 73 Elapsed -= Start; 74 llvm::errs() << Output << ':'; 75 Elapsed.print(Elapsed, llvm::errs()); 76 llvm::errs() << '\n'; 77 } 78 } 79 }; 80 81 struct OnDiskData { 82 /// \brief The file in which the precompiled preamble is stored. 83 std::string PreambleFile; 84 85 /// \brief Temporary files that should be removed when the ASTUnit is 86 /// destroyed. 87 SmallVector<llvm::sys::Path, 4> TemporaryFiles; 88 89 /// \brief Erase temporary files. 90 void CleanTemporaryFiles(); 91 92 /// \brief Erase the preamble file. 93 void CleanPreambleFile(); 94 95 /// \brief Erase temporary files and the preamble file. 96 void Cleanup(); 97 }; 98} 99 100static llvm::sys::SmartMutex<false> &getOnDiskMutex() { 101 static llvm::sys::SmartMutex<false> M(/* recursive = */ true); 102 return M; 103} 104 105static void cleanupOnDiskMapAtExit(void); 106 107typedef llvm::DenseMap<const ASTUnit *, OnDiskData *> OnDiskDataMap; 108static OnDiskDataMap &getOnDiskDataMap() { 109 static OnDiskDataMap M; 110 static bool hasRegisteredAtExit = false; 111 if (!hasRegisteredAtExit) { 112 hasRegisteredAtExit = true; 113 atexit(cleanupOnDiskMapAtExit); 114 } 115 return M; 116} 117 118static void cleanupOnDiskMapAtExit(void) { 119 // Use the mutex because there can be an alive thread destroying an ASTUnit. 120 llvm::MutexGuard Guard(getOnDiskMutex()); 121 OnDiskDataMap &M = getOnDiskDataMap(); 122 for (OnDiskDataMap::iterator I = M.begin(), E = M.end(); I != E; ++I) { 123 // We don't worry about freeing the memory associated with OnDiskDataMap. 124 // All we care about is erasing stale files. 125 I->second->Cleanup(); 126 } 127} 128 129static OnDiskData &getOnDiskData(const ASTUnit *AU) { 130 // We require the mutex since we are modifying the structure of the 131 // DenseMap. 132 llvm::MutexGuard Guard(getOnDiskMutex()); 133 OnDiskDataMap &M = getOnDiskDataMap(); 134 OnDiskData *&D = M[AU]; 135 if (!D) 136 D = new OnDiskData(); 137 return *D; 138} 139 140static void erasePreambleFile(const ASTUnit *AU) { 141 getOnDiskData(AU).CleanPreambleFile(); 142} 143 144static void removeOnDiskEntry(const ASTUnit *AU) { 145 // We require the mutex since we are modifying the structure of the 146 // DenseMap. 147 llvm::MutexGuard Guard(getOnDiskMutex()); 148 OnDiskDataMap &M = getOnDiskDataMap(); 149 OnDiskDataMap::iterator I = M.find(AU); 150 if (I != M.end()) { 151 I->second->Cleanup(); 152 delete I->second; 153 M.erase(AU); 154 } 155} 156 157static void setPreambleFile(const ASTUnit *AU, llvm::StringRef preambleFile) { 158 getOnDiskData(AU).PreambleFile = preambleFile; 159} 160 161static const std::string &getPreambleFile(const ASTUnit *AU) { 162 return getOnDiskData(AU).PreambleFile; 163} 164 165void OnDiskData::CleanTemporaryFiles() { 166 for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I) 167 TemporaryFiles[I].eraseFromDisk(); 168 TemporaryFiles.clear(); 169} 170 171void OnDiskData::CleanPreambleFile() { 172 if (!PreambleFile.empty()) { 173 llvm::sys::Path(PreambleFile).eraseFromDisk(); 174 PreambleFile.clear(); 175 } 176} 177 178void OnDiskData::Cleanup() { 179 CleanTemporaryFiles(); 180 CleanPreambleFile(); 181} 182 183struct ASTUnit::ASTWriterData { 184 SmallString<128> Buffer; 185 llvm::BitstreamWriter Stream; 186 ASTWriter Writer; 187 188 ASTWriterData() : Stream(Buffer), Writer(Stream) { } 189}; 190 191void ASTUnit::clearFileLevelDecls() { 192 for (FileDeclsTy::iterator 193 I = FileDecls.begin(), E = FileDecls.end(); I != E; ++I) 194 delete I->second; 195 FileDecls.clear(); 196} 197 198void ASTUnit::CleanTemporaryFiles() { 199 getOnDiskData(this).CleanTemporaryFiles(); 200} 201 202void ASTUnit::addTemporaryFile(const llvm::sys::Path &TempFile) { 203 getOnDiskData(this).TemporaryFiles.push_back(TempFile); 204} 205 206/// \brief After failing to build a precompiled preamble (due to 207/// errors in the source that occurs in the preamble), the number of 208/// reparses during which we'll skip even trying to precompile the 209/// preamble. 210const unsigned DefaultPreambleRebuildInterval = 5; 211 212/// \brief Tracks the number of ASTUnit objects that are currently active. 213/// 214/// Used for debugging purposes only. 215static llvm::sys::cas_flag ActiveASTUnitObjects; 216 217ASTUnit::ASTUnit(bool _MainFileIsAST) 218 : Reader(0), OnlyLocalDecls(false), CaptureDiagnostics(false), 219 MainFileIsAST(_MainFileIsAST), 220 TUKind(TU_Complete), WantTiming(getenv("LIBCLANG_TIMING")), 221 OwnsRemappedFileBuffers(true), 222 NumStoredDiagnosticsFromDriver(0), 223 PreambleRebuildCounter(0), SavedMainFileBuffer(0), PreambleBuffer(0), 224 NumWarningsInPreamble(0), 225 ShouldCacheCodeCompletionResults(false), 226 IncludeBriefCommentsInCodeCompletion(false), UserFilesAreVolatile(false), 227 CompletionCacheTopLevelHashValue(0), 228 PreambleTopLevelHashValue(0), 229 CurrentTopLevelHashValue(0), 230 UnsafeToFree(false) { 231 if (getenv("LIBCLANG_OBJTRACKING")) { 232 llvm::sys::AtomicIncrement(&ActiveASTUnitObjects); 233 fprintf(stderr, "+++ %d translation units\n", ActiveASTUnitObjects); 234 } 235} 236 237ASTUnit::~ASTUnit() { 238 clearFileLevelDecls(); 239 240 // Clean up the temporary files and the preamble file. 241 removeOnDiskEntry(this); 242 243 // Free the buffers associated with remapped files. We are required to 244 // perform this operation here because we explicitly request that the 245 // compiler instance *not* free these buffers for each invocation of the 246 // parser. 247 if (Invocation.getPtr() && OwnsRemappedFileBuffers) { 248 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts(); 249 for (PreprocessorOptions::remapped_file_buffer_iterator 250 FB = PPOpts.remapped_file_buffer_begin(), 251 FBEnd = PPOpts.remapped_file_buffer_end(); 252 FB != FBEnd; 253 ++FB) 254 delete FB->second; 255 } 256 257 delete SavedMainFileBuffer; 258 delete PreambleBuffer; 259 260 ClearCachedCompletionResults(); 261 262 if (getenv("LIBCLANG_OBJTRACKING")) { 263 llvm::sys::AtomicDecrement(&ActiveASTUnitObjects); 264 fprintf(stderr, "--- %d translation units\n", ActiveASTUnitObjects); 265 } 266} 267 268void ASTUnit::setPreprocessor(Preprocessor *pp) { PP = pp; } 269 270/// \brief Determine the set of code-completion contexts in which this 271/// declaration should be shown. 272static unsigned getDeclShowContexts(NamedDecl *ND, 273 const LangOptions &LangOpts, 274 bool &IsNestedNameSpecifier) { 275 IsNestedNameSpecifier = false; 276 277 if (isa<UsingShadowDecl>(ND)) 278 ND = dyn_cast<NamedDecl>(ND->getUnderlyingDecl()); 279 if (!ND) 280 return 0; 281 282 uint64_t Contexts = 0; 283 if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND) || 284 isa<ClassTemplateDecl>(ND) || isa<TemplateTemplateParmDecl>(ND)) { 285 // Types can appear in these contexts. 286 if (LangOpts.CPlusPlus || !isa<TagDecl>(ND)) 287 Contexts |= (1LL << CodeCompletionContext::CCC_TopLevel) 288 | (1LL << CodeCompletionContext::CCC_ObjCIvarList) 289 | (1LL << CodeCompletionContext::CCC_ClassStructUnion) 290 | (1LL << CodeCompletionContext::CCC_Statement) 291 | (1LL << CodeCompletionContext::CCC_Type) 292 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression); 293 294 // In C++, types can appear in expressions contexts (for functional casts). 295 if (LangOpts.CPlusPlus) 296 Contexts |= (1LL << CodeCompletionContext::CCC_Expression); 297 298 // In Objective-C, message sends can send interfaces. In Objective-C++, 299 // all types are available due to functional casts. 300 if (LangOpts.CPlusPlus || isa<ObjCInterfaceDecl>(ND)) 301 Contexts |= (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver); 302 303 // In Objective-C, you can only be a subclass of another Objective-C class 304 if (isa<ObjCInterfaceDecl>(ND)) 305 Contexts |= (1LL << CodeCompletionContext::CCC_ObjCInterfaceName); 306 307 // Deal with tag names. 308 if (isa<EnumDecl>(ND)) { 309 Contexts |= (1LL << CodeCompletionContext::CCC_EnumTag); 310 311 // Part of the nested-name-specifier in C++0x. 312 if (LangOpts.CPlusPlus0x) 313 IsNestedNameSpecifier = true; 314 } else if (RecordDecl *Record = dyn_cast<RecordDecl>(ND)) { 315 if (Record->isUnion()) 316 Contexts |= (1LL << CodeCompletionContext::CCC_UnionTag); 317 else 318 Contexts |= (1LL << CodeCompletionContext::CCC_ClassOrStructTag); 319 320 if (LangOpts.CPlusPlus) 321 IsNestedNameSpecifier = true; 322 } else if (isa<ClassTemplateDecl>(ND)) 323 IsNestedNameSpecifier = true; 324 } else if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) { 325 // Values can appear in these contexts. 326 Contexts = (1LL << CodeCompletionContext::CCC_Statement) 327 | (1LL << CodeCompletionContext::CCC_Expression) 328 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression) 329 | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver); 330 } else if (isa<ObjCProtocolDecl>(ND)) { 331 Contexts = (1LL << CodeCompletionContext::CCC_ObjCProtocolName); 332 } else if (isa<ObjCCategoryDecl>(ND)) { 333 Contexts = (1LL << CodeCompletionContext::CCC_ObjCCategoryName); 334 } else if (isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) { 335 Contexts = (1LL << CodeCompletionContext::CCC_Namespace); 336 337 // Part of the nested-name-specifier. 338 IsNestedNameSpecifier = true; 339 } 340 341 return Contexts; 342} 343 344void ASTUnit::CacheCodeCompletionResults() { 345 if (!TheSema) 346 return; 347 348 SimpleTimer Timer(WantTiming); 349 Timer.setOutput("Cache global code completions for " + getMainFileName()); 350 351 // Clear out the previous results. 352 ClearCachedCompletionResults(); 353 354 // Gather the set of global code completions. 355 typedef CodeCompletionResult Result; 356 SmallVector<Result, 8> Results; 357 CachedCompletionAllocator = new GlobalCodeCompletionAllocator; 358 TheSema->GatherGlobalCodeCompletions(*CachedCompletionAllocator, 359 getCodeCompletionTUInfo(), Results); 360 361 // Translate global code completions into cached completions. 362 llvm::DenseMap<CanQualType, unsigned> CompletionTypes; 363 364 for (unsigned I = 0, N = Results.size(); I != N; ++I) { 365 switch (Results[I].Kind) { 366 case Result::RK_Declaration: { 367 bool IsNestedNameSpecifier = false; 368 CachedCodeCompletionResult CachedResult; 369 CachedResult.Completion = Results[I].CreateCodeCompletionString(*TheSema, 370 *CachedCompletionAllocator, 371 getCodeCompletionTUInfo(), 372 IncludeBriefCommentsInCodeCompletion); 373 CachedResult.ShowInContexts = getDeclShowContexts(Results[I].Declaration, 374 Ctx->getLangOpts(), 375 IsNestedNameSpecifier); 376 CachedResult.Priority = Results[I].Priority; 377 CachedResult.Kind = Results[I].CursorKind; 378 CachedResult.Availability = Results[I].Availability; 379 380 // Keep track of the type of this completion in an ASTContext-agnostic 381 // way. 382 QualType UsageType = getDeclUsageType(*Ctx, Results[I].Declaration); 383 if (UsageType.isNull()) { 384 CachedResult.TypeClass = STC_Void; 385 CachedResult.Type = 0; 386 } else { 387 CanQualType CanUsageType 388 = Ctx->getCanonicalType(UsageType.getUnqualifiedType()); 389 CachedResult.TypeClass = getSimplifiedTypeClass(CanUsageType); 390 391 // Determine whether we have already seen this type. If so, we save 392 // ourselves the work of formatting the type string by using the 393 // temporary, CanQualType-based hash table to find the associated value. 394 unsigned &TypeValue = CompletionTypes[CanUsageType]; 395 if (TypeValue == 0) { 396 TypeValue = CompletionTypes.size(); 397 CachedCompletionTypes[QualType(CanUsageType).getAsString()] 398 = TypeValue; 399 } 400 401 CachedResult.Type = TypeValue; 402 } 403 404 CachedCompletionResults.push_back(CachedResult); 405 406 /// Handle nested-name-specifiers in C++. 407 if (TheSema->Context.getLangOpts().CPlusPlus && 408 IsNestedNameSpecifier && !Results[I].StartsNestedNameSpecifier) { 409 // The contexts in which a nested-name-specifier can appear in C++. 410 uint64_t NNSContexts 411 = (1LL << CodeCompletionContext::CCC_TopLevel) 412 | (1LL << CodeCompletionContext::CCC_ObjCIvarList) 413 | (1LL << CodeCompletionContext::CCC_ClassStructUnion) 414 | (1LL << CodeCompletionContext::CCC_Statement) 415 | (1LL << CodeCompletionContext::CCC_Expression) 416 | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver) 417 | (1LL << CodeCompletionContext::CCC_EnumTag) 418 | (1LL << CodeCompletionContext::CCC_UnionTag) 419 | (1LL << CodeCompletionContext::CCC_ClassOrStructTag) 420 | (1LL << CodeCompletionContext::CCC_Type) 421 | (1LL << CodeCompletionContext::CCC_PotentiallyQualifiedName) 422 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression); 423 424 if (isa<NamespaceDecl>(Results[I].Declaration) || 425 isa<NamespaceAliasDecl>(Results[I].Declaration)) 426 NNSContexts |= (1LL << CodeCompletionContext::CCC_Namespace); 427 428 if (unsigned RemainingContexts 429 = NNSContexts & ~CachedResult.ShowInContexts) { 430 // If there any contexts where this completion can be a 431 // nested-name-specifier but isn't already an option, create a 432 // nested-name-specifier completion. 433 Results[I].StartsNestedNameSpecifier = true; 434 CachedResult.Completion 435 = Results[I].CreateCodeCompletionString(*TheSema, 436 *CachedCompletionAllocator, 437 getCodeCompletionTUInfo(), 438 IncludeBriefCommentsInCodeCompletion); 439 CachedResult.ShowInContexts = RemainingContexts; 440 CachedResult.Priority = CCP_NestedNameSpecifier; 441 CachedResult.TypeClass = STC_Void; 442 CachedResult.Type = 0; 443 CachedCompletionResults.push_back(CachedResult); 444 } 445 } 446 break; 447 } 448 449 case Result::RK_Keyword: 450 case Result::RK_Pattern: 451 // Ignore keywords and patterns; we don't care, since they are so 452 // easily regenerated. 453 break; 454 455 case Result::RK_Macro: { 456 CachedCodeCompletionResult CachedResult; 457 CachedResult.Completion 458 = Results[I].CreateCodeCompletionString(*TheSema, 459 *CachedCompletionAllocator, 460 getCodeCompletionTUInfo(), 461 IncludeBriefCommentsInCodeCompletion); 462 CachedResult.ShowInContexts 463 = (1LL << CodeCompletionContext::CCC_TopLevel) 464 | (1LL << CodeCompletionContext::CCC_ObjCInterface) 465 | (1LL << CodeCompletionContext::CCC_ObjCImplementation) 466 | (1LL << CodeCompletionContext::CCC_ObjCIvarList) 467 | (1LL << CodeCompletionContext::CCC_ClassStructUnion) 468 | (1LL << CodeCompletionContext::CCC_Statement) 469 | (1LL << CodeCompletionContext::CCC_Expression) 470 | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver) 471 | (1LL << CodeCompletionContext::CCC_MacroNameUse) 472 | (1LL << CodeCompletionContext::CCC_PreprocessorExpression) 473 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression) 474 | (1LL << CodeCompletionContext::CCC_OtherWithMacros); 475 476 CachedResult.Priority = Results[I].Priority; 477 CachedResult.Kind = Results[I].CursorKind; 478 CachedResult.Availability = Results[I].Availability; 479 CachedResult.TypeClass = STC_Void; 480 CachedResult.Type = 0; 481 CachedCompletionResults.push_back(CachedResult); 482 break; 483 } 484 } 485 } 486 487 // Save the current top-level hash value. 488 CompletionCacheTopLevelHashValue = CurrentTopLevelHashValue; 489} 490 491void ASTUnit::ClearCachedCompletionResults() { 492 CachedCompletionResults.clear(); 493 CachedCompletionTypes.clear(); 494 CachedCompletionAllocator = 0; 495} 496 497namespace { 498 499/// \brief Gathers information from ASTReader that will be used to initialize 500/// a Preprocessor. 501class ASTInfoCollector : public ASTReaderListener { 502 Preprocessor &PP; 503 ASTContext &Context; 504 LangOptions &LangOpt; 505 HeaderSearch &HSI; 506 IntrusiveRefCntPtr<TargetOptions> &TargetOpts; 507 IntrusiveRefCntPtr<TargetInfo> &Target; 508 std::string &Predefines; 509 unsigned &Counter; 510 511 unsigned NumHeaderInfos; 512 513 bool InitializedLanguage; 514public: 515 ASTInfoCollector(Preprocessor &PP, ASTContext &Context, LangOptions &LangOpt, 516 HeaderSearch &HSI, 517 IntrusiveRefCntPtr<TargetOptions> &TargetOpts, 518 IntrusiveRefCntPtr<TargetInfo> &Target, 519 std::string &Predefines, 520 unsigned &Counter) 521 : PP(PP), Context(Context), LangOpt(LangOpt), HSI(HSI), 522 TargetOpts(TargetOpts), Target(Target), 523 Predefines(Predefines), Counter(Counter), NumHeaderInfos(0), 524 InitializedLanguage(false) {} 525 526 virtual bool ReadLanguageOptions(const LangOptions &LangOpts, 527 bool Complain) { 528 if (InitializedLanguage) 529 return false; 530 531 LangOpt = LangOpts; 532 InitializedLanguage = true; 533 534 updated(); 535 return false; 536 } 537 538 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts, 539 bool Complain) { 540 // If we've already initialized the target, don't do it again. 541 if (Target) 542 return false; 543 544 this->TargetOpts = new TargetOptions(TargetOpts); 545 Target = TargetInfo::CreateTargetInfo(PP.getDiagnostics(), 546 *this->TargetOpts); 547 548 updated(); 549 return false; 550 } 551 552 virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers, 553 StringRef OriginalFileName, 554 std::string &SuggestedPredefines, 555 FileManager &FileMgr, 556 bool Complain) { 557 Predefines = Buffers[0].Data; 558 for (unsigned I = 1, N = Buffers.size(); I != N; ++I) { 559 Predefines += Buffers[I].Data; 560 } 561 return false; 562 } 563 564 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID) { 565 HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++); 566 } 567 568 virtual void ReadCounter(const serialization::ModuleFile &M, unsigned Value) { 569 Counter = Value; 570 } 571 572private: 573 void updated() { 574 if (!Target || !InitializedLanguage) 575 return; 576 577 // Inform the target of the language options. 578 // 579 // FIXME: We shouldn't need to do this, the target should be immutable once 580 // created. This complexity should be lifted elsewhere. 581 Target->setForcedLangOptions(LangOpt); 582 583 // Initialize the preprocessor. 584 PP.Initialize(*Target); 585 586 // Initialize the ASTContext 587 Context.InitBuiltinTypes(*Target); 588 } 589}; 590 591class StoredDiagnosticConsumer : public DiagnosticConsumer { 592 SmallVectorImpl<StoredDiagnostic> &StoredDiags; 593 594public: 595 explicit StoredDiagnosticConsumer( 596 SmallVectorImpl<StoredDiagnostic> &StoredDiags) 597 : StoredDiags(StoredDiags) { } 598 599 virtual void HandleDiagnostic(DiagnosticsEngine::Level Level, 600 const Diagnostic &Info); 601 602 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const { 603 // Just drop any diagnostics that come from cloned consumers; they'll 604 // have different source managers anyway. 605 // FIXME: We'd like to be able to capture these somehow, even if it's just 606 // file/line/column, because they could occur when parsing module maps or 607 // building modules on-demand. 608 return new IgnoringDiagConsumer(); 609 } 610}; 611 612/// \brief RAII object that optionally captures diagnostics, if 613/// there is no diagnostic client to capture them already. 614class CaptureDroppedDiagnostics { 615 DiagnosticsEngine &Diags; 616 StoredDiagnosticConsumer Client; 617 DiagnosticConsumer *PreviousClient; 618 619public: 620 CaptureDroppedDiagnostics(bool RequestCapture, DiagnosticsEngine &Diags, 621 SmallVectorImpl<StoredDiagnostic> &StoredDiags) 622 : Diags(Diags), Client(StoredDiags), PreviousClient(0) 623 { 624 if (RequestCapture || Diags.getClient() == 0) { 625 PreviousClient = Diags.takeClient(); 626 Diags.setClient(&Client); 627 } 628 } 629 630 ~CaptureDroppedDiagnostics() { 631 if (Diags.getClient() == &Client) { 632 Diags.takeClient(); 633 Diags.setClient(PreviousClient); 634 } 635 } 636}; 637 638} // anonymous namespace 639 640void StoredDiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level Level, 641 const Diagnostic &Info) { 642 // Default implementation (Warnings/errors count). 643 DiagnosticConsumer::HandleDiagnostic(Level, Info); 644 645 StoredDiags.push_back(StoredDiagnostic(Level, Info)); 646} 647 648const std::string &ASTUnit::getOriginalSourceFileName() { 649 return OriginalSourceFile; 650} 651 652ASTDeserializationListener *ASTUnit::getDeserializationListener() { 653 if (WriterData) 654 return &WriterData->Writer; 655 return 0; 656} 657 658llvm::MemoryBuffer *ASTUnit::getBufferForFile(StringRef Filename, 659 std::string *ErrorStr) { 660 assert(FileMgr); 661 return FileMgr->getBufferForFile(Filename, ErrorStr); 662} 663 664/// \brief Configure the diagnostics object for use with ASTUnit. 665void ASTUnit::ConfigureDiags(IntrusiveRefCntPtr<DiagnosticsEngine> &Diags, 666 const char **ArgBegin, const char **ArgEnd, 667 ASTUnit &AST, bool CaptureDiagnostics) { 668 if (!Diags.getPtr()) { 669 // No diagnostics engine was provided, so create our own diagnostics object 670 // with the default options. 671 DiagnosticConsumer *Client = 0; 672 if (CaptureDiagnostics) 673 Client = new StoredDiagnosticConsumer(AST.StoredDiagnostics); 674 Diags = CompilerInstance::createDiagnostics(new DiagnosticOptions(), 675 ArgEnd-ArgBegin, 676 ArgBegin, Client, 677 /*ShouldOwnClient=*/true, 678 /*ShouldCloneClient=*/false); 679 } else if (CaptureDiagnostics) { 680 Diags->setClient(new StoredDiagnosticConsumer(AST.StoredDiagnostics)); 681 } 682} 683 684ASTUnit *ASTUnit::LoadFromASTFile(const std::string &Filename, 685 IntrusiveRefCntPtr<DiagnosticsEngine> Diags, 686 const FileSystemOptions &FileSystemOpts, 687 bool OnlyLocalDecls, 688 RemappedFile *RemappedFiles, 689 unsigned NumRemappedFiles, 690 bool CaptureDiagnostics, 691 bool AllowPCHWithCompilerErrors, 692 bool UserFilesAreVolatile) { 693 OwningPtr<ASTUnit> AST(new ASTUnit(true)); 694 695 // Recover resources if we crash before exiting this method. 696 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit> 697 ASTUnitCleanup(AST.get()); 698 llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine, 699 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> > 700 DiagCleanup(Diags.getPtr()); 701 702 ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics); 703 704 AST->OnlyLocalDecls = OnlyLocalDecls; 705 AST->CaptureDiagnostics = CaptureDiagnostics; 706 AST->Diagnostics = Diags; 707 AST->FileMgr = new FileManager(FileSystemOpts); 708 AST->UserFilesAreVolatile = UserFilesAreVolatile; 709 AST->SourceMgr = new SourceManager(AST->getDiagnostics(), 710 AST->getFileManager(), 711 UserFilesAreVolatile); 712 AST->HSOpts = new HeaderSearchOptions(); 713 714 AST->HeaderInfo.reset(new HeaderSearch(AST->HSOpts, 715 AST->getFileManager(), 716 AST->getDiagnostics(), 717 AST->ASTFileLangOpts, 718 /*Target=*/0)); 719 720 for (unsigned I = 0; I != NumRemappedFiles; ++I) { 721 FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second; 722 if (const llvm::MemoryBuffer * 723 memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) { 724 // Create the file entry for the file that we're mapping from. 725 const FileEntry *FromFile 726 = AST->getFileManager().getVirtualFile(RemappedFiles[I].first, 727 memBuf->getBufferSize(), 728 0); 729 if (!FromFile) { 730 AST->getDiagnostics().Report(diag::err_fe_remap_missing_from_file) 731 << RemappedFiles[I].first; 732 delete memBuf; 733 continue; 734 } 735 736 // Override the contents of the "from" file with the contents of 737 // the "to" file. 738 AST->getSourceManager().overrideFileContents(FromFile, memBuf); 739 740 } else { 741 const char *fname = fileOrBuf.get<const char *>(); 742 const FileEntry *ToFile = AST->FileMgr->getFile(fname); 743 if (!ToFile) { 744 AST->getDiagnostics().Report(diag::err_fe_remap_missing_to_file) 745 << RemappedFiles[I].first << fname; 746 continue; 747 } 748 749 // Create the file entry for the file that we're mapping from. 750 const FileEntry *FromFile 751 = AST->getFileManager().getVirtualFile(RemappedFiles[I].first, 752 ToFile->getSize(), 753 0); 754 if (!FromFile) { 755 AST->getDiagnostics().Report(diag::err_fe_remap_missing_from_file) 756 << RemappedFiles[I].first; 757 delete memBuf; 758 continue; 759 } 760 761 // Override the contents of the "from" file with the contents of 762 // the "to" file. 763 AST->getSourceManager().overrideFileContents(FromFile, ToFile); 764 } 765 } 766 767 // Gather Info for preprocessor construction later on. 768 769 HeaderSearch &HeaderInfo = *AST->HeaderInfo.get(); 770 std::string Predefines; 771 unsigned Counter; 772 773 OwningPtr<ASTReader> Reader; 774 775 AST->PP = new Preprocessor(AST->getDiagnostics(), AST->ASTFileLangOpts, 776 /*Target=*/0, AST->getSourceManager(), HeaderInfo, 777 *AST, 778 /*IILookup=*/0, 779 /*OwnsHeaderSearch=*/false, 780 /*DelayInitialization=*/true); 781 Preprocessor &PP = *AST->PP; 782 783 AST->Ctx = new ASTContext(AST->ASTFileLangOpts, 784 AST->getSourceManager(), 785 /*Target=*/0, 786 PP.getIdentifierTable(), 787 PP.getSelectorTable(), 788 PP.getBuiltinInfo(), 789 /* size_reserve = */0, 790 /*DelayInitialization=*/true); 791 ASTContext &Context = *AST->Ctx; 792 793 bool disableValid = false; 794 if (::getenv("LIBCLANG_DISABLE_PCH_VALIDATION")) 795 disableValid = true; 796 Reader.reset(new ASTReader(PP, Context, 797 /*isysroot=*/"", 798 /*DisableValidation=*/disableValid, 799 /*DisableStatCache=*/false, 800 AllowPCHWithCompilerErrors)); 801 802 // Recover resources if we crash before exiting this method. 803 llvm::CrashRecoveryContextCleanupRegistrar<ASTReader> 804 ReaderCleanup(Reader.get()); 805 806 Reader->setListener(new ASTInfoCollector(*AST->PP, Context, 807 AST->ASTFileLangOpts, HeaderInfo, 808 AST->TargetOpts, AST->Target, 809 Predefines, Counter)); 810 811 switch (Reader->ReadAST(Filename, serialization::MK_MainFile, 812 ASTReader::ARR_None)) { 813 case ASTReader::Success: 814 break; 815 816 case ASTReader::Failure: 817 case ASTReader::OutOfDate: 818 case ASTReader::VersionMismatch: 819 case ASTReader::ConfigurationMismatch: 820 case ASTReader::HadErrors: 821 AST->getDiagnostics().Report(diag::err_fe_unable_to_load_pch); 822 return NULL; 823 } 824 825 AST->OriginalSourceFile = Reader->getOriginalSourceFile(); 826 827 PP.setPredefines(Reader->getSuggestedPredefines()); 828 PP.setCounterValue(Counter); 829 830 // Attach the AST reader to the AST context as an external AST 831 // source, so that declarations will be deserialized from the 832 // AST file as needed. 833 ASTReader *ReaderPtr = Reader.get(); 834 OwningPtr<ExternalASTSource> Source(Reader.take()); 835 836 // Unregister the cleanup for ASTReader. It will get cleaned up 837 // by the ASTUnit cleanup. 838 ReaderCleanup.unregister(); 839 840 Context.setExternalSource(Source); 841 842 // Create an AST consumer, even though it isn't used. 843 AST->Consumer.reset(new ASTConsumer); 844 845 // Create a semantic analysis object and tell the AST reader about it. 846 AST->TheSema.reset(new Sema(PP, Context, *AST->Consumer)); 847 AST->TheSema->Initialize(); 848 ReaderPtr->InitializeSema(*AST->TheSema); 849 AST->Reader = ReaderPtr; 850 851 return AST.take(); 852} 853 854namespace { 855 856/// \brief Preprocessor callback class that updates a hash value with the names 857/// of all macros that have been defined by the translation unit. 858class MacroDefinitionTrackerPPCallbacks : public PPCallbacks { 859 unsigned &Hash; 860 861public: 862 explicit MacroDefinitionTrackerPPCallbacks(unsigned &Hash) : Hash(Hash) { } 863 864 virtual void MacroDefined(const Token &MacroNameTok, const MacroInfo *MI) { 865 Hash = llvm::HashString(MacroNameTok.getIdentifierInfo()->getName(), Hash); 866 } 867}; 868 869/// \brief Add the given declaration to the hash of all top-level entities. 870void AddTopLevelDeclarationToHash(Decl *D, unsigned &Hash) { 871 if (!D) 872 return; 873 874 DeclContext *DC = D->getDeclContext(); 875 if (!DC) 876 return; 877 878 if (!(DC->isTranslationUnit() || DC->getLookupParent()->isTranslationUnit())) 879 return; 880 881 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) { 882 if (ND->getIdentifier()) 883 Hash = llvm::HashString(ND->getIdentifier()->getName(), Hash); 884 else if (DeclarationName Name = ND->getDeclName()) { 885 std::string NameStr = Name.getAsString(); 886 Hash = llvm::HashString(NameStr, Hash); 887 } 888 return; 889 } 890} 891 892class TopLevelDeclTrackerConsumer : public ASTConsumer { 893 ASTUnit &Unit; 894 unsigned &Hash; 895 896public: 897 TopLevelDeclTrackerConsumer(ASTUnit &_Unit, unsigned &Hash) 898 : Unit(_Unit), Hash(Hash) { 899 Hash = 0; 900 } 901 902 void handleTopLevelDecl(Decl *D) { 903 if (!D) 904 return; 905 906 // FIXME: Currently ObjC method declarations are incorrectly being 907 // reported as top-level declarations, even though their DeclContext 908 // is the containing ObjC @interface/@implementation. This is a 909 // fundamental problem in the parser right now. 910 if (isa<ObjCMethodDecl>(D)) 911 return; 912 913 AddTopLevelDeclarationToHash(D, Hash); 914 Unit.addTopLevelDecl(D); 915 916 handleFileLevelDecl(D); 917 } 918 919 void handleFileLevelDecl(Decl *D) { 920 Unit.addFileLevelDecl(D); 921 if (NamespaceDecl *NSD = dyn_cast<NamespaceDecl>(D)) { 922 for (NamespaceDecl::decl_iterator 923 I = NSD->decls_begin(), E = NSD->decls_end(); I != E; ++I) 924 handleFileLevelDecl(*I); 925 } 926 } 927 928 bool HandleTopLevelDecl(DeclGroupRef D) { 929 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) 930 handleTopLevelDecl(*it); 931 return true; 932 } 933 934 // We're not interested in "interesting" decls. 935 void HandleInterestingDecl(DeclGroupRef) {} 936 937 void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) { 938 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) 939 handleTopLevelDecl(*it); 940 } 941 942 virtual ASTDeserializationListener *GetASTDeserializationListener() { 943 return Unit.getDeserializationListener(); 944 } 945}; 946 947class TopLevelDeclTrackerAction : public ASTFrontendAction { 948public: 949 ASTUnit &Unit; 950 951 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, 952 StringRef InFile) { 953 CI.getPreprocessor().addPPCallbacks( 954 new MacroDefinitionTrackerPPCallbacks(Unit.getCurrentTopLevelHashValue())); 955 return new TopLevelDeclTrackerConsumer(Unit, 956 Unit.getCurrentTopLevelHashValue()); 957 } 958 959public: 960 TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {} 961 962 virtual bool hasCodeCompletionSupport() const { return false; } 963 virtual TranslationUnitKind getTranslationUnitKind() { 964 return Unit.getTranslationUnitKind(); 965 } 966}; 967 968class PrecompilePreambleConsumer : public PCHGenerator { 969 ASTUnit &Unit; 970 unsigned &Hash; 971 std::vector<Decl *> TopLevelDecls; 972 973public: 974 PrecompilePreambleConsumer(ASTUnit &Unit, const Preprocessor &PP, 975 StringRef isysroot, raw_ostream *Out) 976 : PCHGenerator(PP, "", 0, isysroot, Out), Unit(Unit), 977 Hash(Unit.getCurrentTopLevelHashValue()) { 978 Hash = 0; 979 } 980 981 virtual bool HandleTopLevelDecl(DeclGroupRef D) { 982 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) { 983 Decl *D = *it; 984 // FIXME: Currently ObjC method declarations are incorrectly being 985 // reported as top-level declarations, even though their DeclContext 986 // is the containing ObjC @interface/@implementation. This is a 987 // fundamental problem in the parser right now. 988 if (isa<ObjCMethodDecl>(D)) 989 continue; 990 AddTopLevelDeclarationToHash(D, Hash); 991 TopLevelDecls.push_back(D); 992 } 993 return true; 994 } 995 996 virtual void HandleTranslationUnit(ASTContext &Ctx) { 997 PCHGenerator::HandleTranslationUnit(Ctx); 998 if (!Unit.getDiagnostics().hasErrorOccurred()) { 999 // Translate the top-level declarations we captured during 1000 // parsing into declaration IDs in the precompiled 1001 // preamble. This will allow us to deserialize those top-level 1002 // declarations when requested. 1003 for (unsigned I = 0, N = TopLevelDecls.size(); I != N; ++I) 1004 Unit.addTopLevelDeclFromPreamble( 1005 getWriter().getDeclID(TopLevelDecls[I])); 1006 } 1007 } 1008}; 1009 1010class PrecompilePreambleAction : public ASTFrontendAction { 1011 ASTUnit &Unit; 1012 1013public: 1014 explicit PrecompilePreambleAction(ASTUnit &Unit) : Unit(Unit) {} 1015 1016 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, 1017 StringRef InFile) { 1018 std::string Sysroot; 1019 std::string OutputFile; 1020 raw_ostream *OS = 0; 1021 if (GeneratePCHAction::ComputeASTConsumerArguments(CI, InFile, Sysroot, 1022 OutputFile, 1023 OS)) 1024 return 0; 1025 1026 if (!CI.getFrontendOpts().RelocatablePCH) 1027 Sysroot.clear(); 1028 1029 CI.getPreprocessor().addPPCallbacks( 1030 new MacroDefinitionTrackerPPCallbacks(Unit.getCurrentTopLevelHashValue())); 1031 return new PrecompilePreambleConsumer(Unit, CI.getPreprocessor(), Sysroot, 1032 OS); 1033 } 1034 1035 virtual bool hasCodeCompletionSupport() const { return false; } 1036 virtual bool hasASTFileSupport() const { return false; } 1037 virtual TranslationUnitKind getTranslationUnitKind() { return TU_Prefix; } 1038}; 1039 1040} 1041 1042static void checkAndRemoveNonDriverDiags(SmallVectorImpl<StoredDiagnostic> & 1043 StoredDiagnostics) { 1044 // Get rid of stored diagnostics except the ones from the driver which do not 1045 // have a source location. 1046 for (unsigned I = 0; I < StoredDiagnostics.size(); ++I) { 1047 if (StoredDiagnostics[I].getLocation().isValid()) { 1048 StoredDiagnostics.erase(StoredDiagnostics.begin()+I); 1049 --I; 1050 } 1051 } 1052} 1053 1054static void checkAndSanitizeDiags(SmallVectorImpl<StoredDiagnostic> & 1055 StoredDiagnostics, 1056 SourceManager &SM) { 1057 // The stored diagnostic has the old source manager in it; update 1058 // the locations to refer into the new source manager. Since we've 1059 // been careful to make sure that the source manager's state 1060 // before and after are identical, so that we can reuse the source 1061 // location itself. 1062 for (unsigned I = 0, N = StoredDiagnostics.size(); I < N; ++I) { 1063 if (StoredDiagnostics[I].getLocation().isValid()) { 1064 FullSourceLoc Loc(StoredDiagnostics[I].getLocation(), SM); 1065 StoredDiagnostics[I].setLocation(Loc); 1066 } 1067 } 1068} 1069 1070/// Parse the source file into a translation unit using the given compiler 1071/// invocation, replacing the current translation unit. 1072/// 1073/// \returns True if a failure occurred that causes the ASTUnit not to 1074/// contain any translation-unit information, false otherwise. 1075bool ASTUnit::Parse(llvm::MemoryBuffer *OverrideMainBuffer) { 1076 delete SavedMainFileBuffer; 1077 SavedMainFileBuffer = 0; 1078 1079 if (!Invocation) { 1080 delete OverrideMainBuffer; 1081 return true; 1082 } 1083 1084 // Create the compiler instance to use for building the AST. 1085 OwningPtr<CompilerInstance> Clang(new CompilerInstance()); 1086 1087 // Recover resources if we crash before exiting this method. 1088 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> 1089 CICleanup(Clang.get()); 1090 1091 IntrusiveRefCntPtr<CompilerInvocation> 1092 CCInvocation(new CompilerInvocation(*Invocation)); 1093 1094 Clang->setInvocation(CCInvocation.getPtr()); 1095 OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].File; 1096 1097 // Set up diagnostics, capturing any diagnostics that would 1098 // otherwise be dropped. 1099 Clang->setDiagnostics(&getDiagnostics()); 1100 1101 // Create the target instance. 1102 Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(), 1103 Clang->getTargetOpts())); 1104 if (!Clang->hasTarget()) { 1105 delete OverrideMainBuffer; 1106 return true; 1107 } 1108 1109 // Inform the target of the language options. 1110 // 1111 // FIXME: We shouldn't need to do this, the target should be immutable once 1112 // created. This complexity should be lifted elsewhere. 1113 Clang->getTarget().setForcedLangOptions(Clang->getLangOpts()); 1114 1115 assert(Clang->getFrontendOpts().Inputs.size() == 1 && 1116 "Invocation must have exactly one source file!"); 1117 assert(Clang->getFrontendOpts().Inputs[0].Kind != IK_AST && 1118 "FIXME: AST inputs not yet supported here!"); 1119 assert(Clang->getFrontendOpts().Inputs[0].Kind != IK_LLVM_IR && 1120 "IR inputs not support here!"); 1121 1122 // Configure the various subsystems. 1123 // FIXME: Should we retain the previous file manager? 1124 LangOpts = &Clang->getLangOpts(); 1125 FileSystemOpts = Clang->getFileSystemOpts(); 1126 FileMgr = new FileManager(FileSystemOpts); 1127 SourceMgr = new SourceManager(getDiagnostics(), *FileMgr, 1128 UserFilesAreVolatile); 1129 TheSema.reset(); 1130 Ctx = 0; 1131 PP = 0; 1132 Reader = 0; 1133 1134 // Clear out old caches and data. 1135 TopLevelDecls.clear(); 1136 clearFileLevelDecls(); 1137 CleanTemporaryFiles(); 1138 1139 if (!OverrideMainBuffer) { 1140 checkAndRemoveNonDriverDiags(StoredDiagnostics); 1141 TopLevelDeclsInPreamble.clear(); 1142 } 1143 1144 // Create a file manager object to provide access to and cache the filesystem. 1145 Clang->setFileManager(&getFileManager()); 1146 1147 // Create the source manager. 1148 Clang->setSourceManager(&getSourceManager()); 1149 1150 // If the main file has been overridden due to the use of a preamble, 1151 // make that override happen and introduce the preamble. 1152 PreprocessorOptions &PreprocessorOpts = Clang->getPreprocessorOpts(); 1153 if (OverrideMainBuffer) { 1154 PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer); 1155 PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size(); 1156 PreprocessorOpts.PrecompiledPreambleBytes.second 1157 = PreambleEndsAtStartOfLine; 1158 PreprocessorOpts.ImplicitPCHInclude = getPreambleFile(this); 1159 PreprocessorOpts.DisablePCHValidation = true; 1160 1161 // The stored diagnostic has the old source manager in it; update 1162 // the locations to refer into the new source manager. Since we've 1163 // been careful to make sure that the source manager's state 1164 // before and after are identical, so that we can reuse the source 1165 // location itself. 1166 checkAndSanitizeDiags(StoredDiagnostics, getSourceManager()); 1167 1168 // Keep track of the override buffer; 1169 SavedMainFileBuffer = OverrideMainBuffer; 1170 } 1171 1172 OwningPtr<TopLevelDeclTrackerAction> Act( 1173 new TopLevelDeclTrackerAction(*this)); 1174 1175 // Recover resources if we crash before exiting this method. 1176 llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction> 1177 ActCleanup(Act.get()); 1178 1179 if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) 1180 goto error; 1181 1182 if (OverrideMainBuffer) { 1183 std::string ModName = getPreambleFile(this); 1184 TranslateStoredDiagnostics(Clang->getModuleManager(), ModName, 1185 getSourceManager(), PreambleDiagnostics, 1186 StoredDiagnostics); 1187 } 1188 1189 if (!Act->Execute()) 1190 goto error; 1191 1192 transferASTDataFromCompilerInstance(*Clang); 1193 1194 Act->EndSourceFile(); 1195 1196 FailedParseDiagnostics.clear(); 1197 1198 return false; 1199 1200error: 1201 // Remove the overridden buffer we used for the preamble. 1202 if (OverrideMainBuffer) { 1203 delete OverrideMainBuffer; 1204 SavedMainFileBuffer = 0; 1205 } 1206 1207 // Keep the ownership of the data in the ASTUnit because the client may 1208 // want to see the diagnostics. 1209 transferASTDataFromCompilerInstance(*Clang); 1210 FailedParseDiagnostics.swap(StoredDiagnostics); 1211 StoredDiagnostics.clear(); 1212 NumStoredDiagnosticsFromDriver = 0; 1213 return true; 1214} 1215 1216/// \brief Simple function to retrieve a path for a preamble precompiled header. 1217static std::string GetPreamblePCHPath() { 1218 // FIXME: This is lame; sys::Path should provide this function (in particular, 1219 // it should know how to find the temporary files dir). 1220 // FIXME: This is really lame. I copied this code from the Driver! 1221 // FIXME: This is a hack so that we can override the preamble file during 1222 // crash-recovery testing, which is the only case where the preamble files 1223 // are not necessarily cleaned up. 1224 const char *TmpFile = ::getenv("CINDEXTEST_PREAMBLE_FILE"); 1225 if (TmpFile) 1226 return TmpFile; 1227 1228 std::string Error; 1229 const char *TmpDir = ::getenv("TMPDIR"); 1230 if (!TmpDir) 1231 TmpDir = ::getenv("TEMP"); 1232 if (!TmpDir) 1233 TmpDir = ::getenv("TMP"); 1234#ifdef LLVM_ON_WIN32 1235 if (!TmpDir) 1236 TmpDir = ::getenv("USERPROFILE"); 1237#endif 1238 if (!TmpDir) 1239 TmpDir = "/tmp"; 1240 llvm::sys::Path P(TmpDir); 1241 P.createDirectoryOnDisk(true); 1242 P.appendComponent("preamble"); 1243 P.appendSuffix("pch"); 1244 if (P.makeUnique(/*reuse_current=*/false, /*ErrMsg*/0)) 1245 return std::string(); 1246 1247 return P.str(); 1248} 1249 1250/// \brief Compute the preamble for the main file, providing the source buffer 1251/// that corresponds to the main file along with a pair (bytes, start-of-line) 1252/// that describes the preamble. 1253std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> > 1254ASTUnit::ComputePreamble(CompilerInvocation &Invocation, 1255 unsigned MaxLines, bool &CreatedBuffer) { 1256 FrontendOptions &FrontendOpts = Invocation.getFrontendOpts(); 1257 PreprocessorOptions &PreprocessorOpts = Invocation.getPreprocessorOpts(); 1258 CreatedBuffer = false; 1259 1260 // Try to determine if the main file has been remapped, either from the 1261 // command line (to another file) or directly through the compiler invocation 1262 // (to a memory buffer). 1263 llvm::MemoryBuffer *Buffer = 0; 1264 llvm::sys::PathWithStatus MainFilePath(FrontendOpts.Inputs[0].File); 1265 if (const llvm::sys::FileStatus *MainFileStatus = MainFilePath.getFileStatus()) { 1266 // Check whether there is a file-file remapping of the main file 1267 for (PreprocessorOptions::remapped_file_iterator 1268 M = PreprocessorOpts.remapped_file_begin(), 1269 E = PreprocessorOpts.remapped_file_end(); 1270 M != E; 1271 ++M) { 1272 llvm::sys::PathWithStatus MPath(M->first); 1273 if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) { 1274 if (MainFileStatus->uniqueID == MStatus->uniqueID) { 1275 // We found a remapping. Try to load the resulting, remapped source. 1276 if (CreatedBuffer) { 1277 delete Buffer; 1278 CreatedBuffer = false; 1279 } 1280 1281 Buffer = getBufferForFile(M->second); 1282 if (!Buffer) 1283 return std::make_pair((llvm::MemoryBuffer*)0, 1284 std::make_pair(0, true)); 1285 CreatedBuffer = true; 1286 } 1287 } 1288 } 1289 1290 // Check whether there is a file-buffer remapping. It supercedes the 1291 // file-file remapping. 1292 for (PreprocessorOptions::remapped_file_buffer_iterator 1293 M = PreprocessorOpts.remapped_file_buffer_begin(), 1294 E = PreprocessorOpts.remapped_file_buffer_end(); 1295 M != E; 1296 ++M) { 1297 llvm::sys::PathWithStatus MPath(M->first); 1298 if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) { 1299 if (MainFileStatus->uniqueID == MStatus->uniqueID) { 1300 // We found a remapping. 1301 if (CreatedBuffer) { 1302 delete Buffer; 1303 CreatedBuffer = false; 1304 } 1305 1306 Buffer = const_cast<llvm::MemoryBuffer *>(M->second); 1307 } 1308 } 1309 } 1310 } 1311 1312 // If the main source file was not remapped, load it now. 1313 if (!Buffer) { 1314 Buffer = getBufferForFile(FrontendOpts.Inputs[0].File); 1315 if (!Buffer) 1316 return std::make_pair((llvm::MemoryBuffer*)0, std::make_pair(0, true)); 1317 1318 CreatedBuffer = true; 1319 } 1320 1321 return std::make_pair(Buffer, Lexer::ComputePreamble(Buffer, 1322 *Invocation.getLangOpts(), 1323 MaxLines)); 1324} 1325 1326static llvm::MemoryBuffer *CreatePaddedMainFileBuffer(llvm::MemoryBuffer *Old, 1327 unsigned NewSize, 1328 StringRef NewName) { 1329 llvm::MemoryBuffer *Result 1330 = llvm::MemoryBuffer::getNewUninitMemBuffer(NewSize, NewName); 1331 memcpy(const_cast<char*>(Result->getBufferStart()), 1332 Old->getBufferStart(), Old->getBufferSize()); 1333 memset(const_cast<char*>(Result->getBufferStart()) + Old->getBufferSize(), 1334 ' ', NewSize - Old->getBufferSize() - 1); 1335 const_cast<char*>(Result->getBufferEnd())[-1] = '\n'; 1336 1337 return Result; 1338} 1339 1340/// \brief Attempt to build or re-use a precompiled preamble when (re-)parsing 1341/// the source file. 1342/// 1343/// This routine will compute the preamble of the main source file. If a 1344/// non-trivial preamble is found, it will precompile that preamble into a 1345/// precompiled header so that the precompiled preamble can be used to reduce 1346/// reparsing time. If a precompiled preamble has already been constructed, 1347/// this routine will determine if it is still valid and, if so, avoid 1348/// rebuilding the precompiled preamble. 1349/// 1350/// \param AllowRebuild When true (the default), this routine is 1351/// allowed to rebuild the precompiled preamble if it is found to be 1352/// out-of-date. 1353/// 1354/// \param MaxLines When non-zero, the maximum number of lines that 1355/// can occur within the preamble. 1356/// 1357/// \returns If the precompiled preamble can be used, returns a newly-allocated 1358/// buffer that should be used in place of the main file when doing so. 1359/// Otherwise, returns a NULL pointer. 1360llvm::MemoryBuffer *ASTUnit::getMainBufferWithPrecompiledPreamble( 1361 const CompilerInvocation &PreambleInvocationIn, 1362 bool AllowRebuild, 1363 unsigned MaxLines) { 1364 1365 IntrusiveRefCntPtr<CompilerInvocation> 1366 PreambleInvocation(new CompilerInvocation(PreambleInvocationIn)); 1367 FrontendOptions &FrontendOpts = PreambleInvocation->getFrontendOpts(); 1368 PreprocessorOptions &PreprocessorOpts 1369 = PreambleInvocation->getPreprocessorOpts(); 1370 1371 bool CreatedPreambleBuffer = false; 1372 std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> > NewPreamble 1373 = ComputePreamble(*PreambleInvocation, MaxLines, CreatedPreambleBuffer); 1374 1375 // If ComputePreamble() Take ownership of the preamble buffer. 1376 OwningPtr<llvm::MemoryBuffer> OwnedPreambleBuffer; 1377 if (CreatedPreambleBuffer) 1378 OwnedPreambleBuffer.reset(NewPreamble.first); 1379 1380 if (!NewPreamble.second.first) { 1381 // We couldn't find a preamble in the main source. Clear out the current 1382 // preamble, if we have one. It's obviously no good any more. 1383 Preamble.clear(); 1384 erasePreambleFile(this); 1385 1386 // The next time we actually see a preamble, precompile it. 1387 PreambleRebuildCounter = 1; 1388 return 0; 1389 } 1390 1391 if (!Preamble.empty()) { 1392 // We've previously computed a preamble. Check whether we have the same 1393 // preamble now that we did before, and that there's enough space in 1394 // the main-file buffer within the precompiled preamble to fit the 1395 // new main file. 1396 if (Preamble.size() == NewPreamble.second.first && 1397 PreambleEndsAtStartOfLine == NewPreamble.second.second && 1398 NewPreamble.first->getBufferSize() < PreambleReservedSize-2 && 1399 memcmp(Preamble.getBufferStart(), NewPreamble.first->getBufferStart(), 1400 NewPreamble.second.first) == 0) { 1401 // The preamble has not changed. We may be able to re-use the precompiled 1402 // preamble. 1403 1404 // Check that none of the files used by the preamble have changed. 1405 bool AnyFileChanged = false; 1406 1407 // First, make a record of those files that have been overridden via 1408 // remapping or unsaved_files. 1409 llvm::StringMap<std::pair<off_t, time_t> > OverriddenFiles; 1410 for (PreprocessorOptions::remapped_file_iterator 1411 R = PreprocessorOpts.remapped_file_begin(), 1412 REnd = PreprocessorOpts.remapped_file_end(); 1413 !AnyFileChanged && R != REnd; 1414 ++R) { 1415 struct stat StatBuf; 1416 if (FileMgr->getNoncachedStatValue(R->second, StatBuf)) { 1417 // If we can't stat the file we're remapping to, assume that something 1418 // horrible happened. 1419 AnyFileChanged = true; 1420 break; 1421 } 1422 1423 OverriddenFiles[R->first] = std::make_pair(StatBuf.st_size, 1424 StatBuf.st_mtime); 1425 } 1426 for (PreprocessorOptions::remapped_file_buffer_iterator 1427 R = PreprocessorOpts.remapped_file_buffer_begin(), 1428 REnd = PreprocessorOpts.remapped_file_buffer_end(); 1429 !AnyFileChanged && R != REnd; 1430 ++R) { 1431 // FIXME: Should we actually compare the contents of file->buffer 1432 // remappings? 1433 OverriddenFiles[R->first] = std::make_pair(R->second->getBufferSize(), 1434 0); 1435 } 1436 1437 // Check whether anything has changed. 1438 for (llvm::StringMap<std::pair<off_t, time_t> >::iterator 1439 F = FilesInPreamble.begin(), FEnd = FilesInPreamble.end(); 1440 !AnyFileChanged && F != FEnd; 1441 ++F) { 1442 llvm::StringMap<std::pair<off_t, time_t> >::iterator Overridden 1443 = OverriddenFiles.find(F->first()); 1444 if (Overridden != OverriddenFiles.end()) { 1445 // This file was remapped; check whether the newly-mapped file 1446 // matches up with the previous mapping. 1447 if (Overridden->second != F->second) 1448 AnyFileChanged = true; 1449 continue; 1450 } 1451 1452 // The file was not remapped; check whether it has changed on disk. 1453 struct stat StatBuf; 1454 if (FileMgr->getNoncachedStatValue(F->first(), StatBuf)) { 1455 // If we can't stat the file, assume that something horrible happened. 1456 AnyFileChanged = true; 1457 } else if (StatBuf.st_size != F->second.first || 1458 StatBuf.st_mtime != F->second.second) 1459 AnyFileChanged = true; 1460 } 1461 1462 if (!AnyFileChanged) { 1463 // Okay! We can re-use the precompiled preamble. 1464 1465 // Set the state of the diagnostic object to mimic its state 1466 // after parsing the preamble. 1467 getDiagnostics().Reset(); 1468 ProcessWarningOptions(getDiagnostics(), 1469 PreambleInvocation->getDiagnosticOpts()); 1470 getDiagnostics().setNumWarnings(NumWarningsInPreamble); 1471 1472 // Create a version of the main file buffer that is padded to 1473 // buffer size we reserved when creating the preamble. 1474 return CreatePaddedMainFileBuffer(NewPreamble.first, 1475 PreambleReservedSize, 1476 FrontendOpts.Inputs[0].File); 1477 } 1478 } 1479 1480 // If we aren't allowed to rebuild the precompiled preamble, just 1481 // return now. 1482 if (!AllowRebuild) 1483 return 0; 1484 1485 // We can't reuse the previously-computed preamble. Build a new one. 1486 Preamble.clear(); 1487 PreambleDiagnostics.clear(); 1488 erasePreambleFile(this); 1489 PreambleRebuildCounter = 1; 1490 } else if (!AllowRebuild) { 1491 // We aren't allowed to rebuild the precompiled preamble; just 1492 // return now. 1493 return 0; 1494 } 1495 1496 // If the preamble rebuild counter > 1, it's because we previously 1497 // failed to build a preamble and we're not yet ready to try 1498 // again. Decrement the counter and return a failure. 1499 if (PreambleRebuildCounter > 1) { 1500 --PreambleRebuildCounter; 1501 return 0; 1502 } 1503 1504 // Create a temporary file for the precompiled preamble. In rare 1505 // circumstances, this can fail. 1506 std::string PreamblePCHPath = GetPreamblePCHPath(); 1507 if (PreamblePCHPath.empty()) { 1508 // Try again next time. 1509 PreambleRebuildCounter = 1; 1510 return 0; 1511 } 1512 1513 // We did not previously compute a preamble, or it can't be reused anyway. 1514 SimpleTimer PreambleTimer(WantTiming); 1515 PreambleTimer.setOutput("Precompiling preamble"); 1516 1517 // Create a new buffer that stores the preamble. The buffer also contains 1518 // extra space for the original contents of the file (which will be present 1519 // when we actually parse the file) along with more room in case the file 1520 // grows. 1521 PreambleReservedSize = NewPreamble.first->getBufferSize(); 1522 if (PreambleReservedSize < 4096) 1523 PreambleReservedSize = 8191; 1524 else 1525 PreambleReservedSize *= 2; 1526 1527 // Save the preamble text for later; we'll need to compare against it for 1528 // subsequent reparses. 1529 StringRef MainFilename = PreambleInvocation->getFrontendOpts().Inputs[0].File; 1530 Preamble.assign(FileMgr->getFile(MainFilename), 1531 NewPreamble.first->getBufferStart(), 1532 NewPreamble.first->getBufferStart() 1533 + NewPreamble.second.first); 1534 PreambleEndsAtStartOfLine = NewPreamble.second.second; 1535 1536 delete PreambleBuffer; 1537 PreambleBuffer 1538 = llvm::MemoryBuffer::getNewUninitMemBuffer(PreambleReservedSize, 1539 FrontendOpts.Inputs[0].File); 1540 memcpy(const_cast<char*>(PreambleBuffer->getBufferStart()), 1541 NewPreamble.first->getBufferStart(), Preamble.size()); 1542 memset(const_cast<char*>(PreambleBuffer->getBufferStart()) + Preamble.size(), 1543 ' ', PreambleReservedSize - Preamble.size() - 1); 1544 const_cast<char*>(PreambleBuffer->getBufferEnd())[-1] = '\n'; 1545 1546 // Remap the main source file to the preamble buffer. 1547 llvm::sys::PathWithStatus MainFilePath(FrontendOpts.Inputs[0].File); 1548 PreprocessorOpts.addRemappedFile(MainFilePath.str(), PreambleBuffer); 1549 1550 // Tell the compiler invocation to generate a temporary precompiled header. 1551 FrontendOpts.ProgramAction = frontend::GeneratePCH; 1552 // FIXME: Generate the precompiled header into memory? 1553 FrontendOpts.OutputFile = PreamblePCHPath; 1554 PreprocessorOpts.PrecompiledPreambleBytes.first = 0; 1555 PreprocessorOpts.PrecompiledPreambleBytes.second = false; 1556 1557 // Create the compiler instance to use for building the precompiled preamble. 1558 OwningPtr<CompilerInstance> Clang(new CompilerInstance()); 1559 1560 // Recover resources if we crash before exiting this method. 1561 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> 1562 CICleanup(Clang.get()); 1563 1564 Clang->setInvocation(&*PreambleInvocation); 1565 OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].File; 1566 1567 // Set up diagnostics, capturing all of the diagnostics produced. 1568 Clang->setDiagnostics(&getDiagnostics()); 1569 1570 // Create the target instance. 1571 Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(), 1572 Clang->getTargetOpts())); 1573 if (!Clang->hasTarget()) { 1574 llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk(); 1575 Preamble.clear(); 1576 PreambleRebuildCounter = DefaultPreambleRebuildInterval; 1577 PreprocessorOpts.eraseRemappedFile( 1578 PreprocessorOpts.remapped_file_buffer_end() - 1); 1579 return 0; 1580 } 1581 1582 // Inform the target of the language options. 1583 // 1584 // FIXME: We shouldn't need to do this, the target should be immutable once 1585 // created. This complexity should be lifted elsewhere. 1586 Clang->getTarget().setForcedLangOptions(Clang->getLangOpts()); 1587 1588 assert(Clang->getFrontendOpts().Inputs.size() == 1 && 1589 "Invocation must have exactly one source file!"); 1590 assert(Clang->getFrontendOpts().Inputs[0].Kind != IK_AST && 1591 "FIXME: AST inputs not yet supported here!"); 1592 assert(Clang->getFrontendOpts().Inputs[0].Kind != IK_LLVM_IR && 1593 "IR inputs not support here!"); 1594 1595 // Clear out old caches and data. 1596 getDiagnostics().Reset(); 1597 ProcessWarningOptions(getDiagnostics(), Clang->getDiagnosticOpts()); 1598 checkAndRemoveNonDriverDiags(StoredDiagnostics); 1599 TopLevelDecls.clear(); 1600 TopLevelDeclsInPreamble.clear(); 1601 1602 // Create a file manager object to provide access to and cache the filesystem. 1603 Clang->setFileManager(new FileManager(Clang->getFileSystemOpts())); 1604 1605 // Create the source manager. 1606 Clang->setSourceManager(new SourceManager(getDiagnostics(), 1607 Clang->getFileManager())); 1608 1609 OwningPtr<PrecompilePreambleAction> Act; 1610 Act.reset(new PrecompilePreambleAction(*this)); 1611 if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) { 1612 llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk(); 1613 Preamble.clear(); 1614 PreambleRebuildCounter = DefaultPreambleRebuildInterval; 1615 PreprocessorOpts.eraseRemappedFile( 1616 PreprocessorOpts.remapped_file_buffer_end() - 1); 1617 return 0; 1618 } 1619 1620 Act->Execute(); 1621 Act->EndSourceFile(); 1622 1623 if (Diagnostics->hasErrorOccurred()) { 1624 // There were errors parsing the preamble, so no precompiled header was 1625 // generated. Forget that we even tried. 1626 // FIXME: Should we leave a note for ourselves to try again? 1627 llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk(); 1628 Preamble.clear(); 1629 TopLevelDeclsInPreamble.clear(); 1630 PreambleRebuildCounter = DefaultPreambleRebuildInterval; 1631 PreprocessorOpts.eraseRemappedFile( 1632 PreprocessorOpts.remapped_file_buffer_end() - 1); 1633 return 0; 1634 } 1635 1636 // Transfer any diagnostics generated when parsing the preamble into the set 1637 // of preamble diagnostics. 1638 PreambleDiagnostics.clear(); 1639 PreambleDiagnostics.insert(PreambleDiagnostics.end(), 1640 stored_diag_afterDriver_begin(), stored_diag_end()); 1641 checkAndRemoveNonDriverDiags(StoredDiagnostics); 1642 1643 // Keep track of the preamble we precompiled. 1644 setPreambleFile(this, FrontendOpts.OutputFile); 1645 NumWarningsInPreamble = getDiagnostics().getNumWarnings(); 1646 1647 // Keep track of all of the files that the source manager knows about, 1648 // so we can verify whether they have changed or not. 1649 FilesInPreamble.clear(); 1650 SourceManager &SourceMgr = Clang->getSourceManager(); 1651 const llvm::MemoryBuffer *MainFileBuffer 1652 = SourceMgr.getBuffer(SourceMgr.getMainFileID()); 1653 for (SourceManager::fileinfo_iterator F = SourceMgr.fileinfo_begin(), 1654 FEnd = SourceMgr.fileinfo_end(); 1655 F != FEnd; 1656 ++F) { 1657 const FileEntry *File = F->second->OrigEntry; 1658 if (!File || F->second->getRawBuffer() == MainFileBuffer) 1659 continue; 1660 1661 FilesInPreamble[File->getName()] 1662 = std::make_pair(F->second->getSize(), File->getModificationTime()); 1663 } 1664 1665 PreambleRebuildCounter = 1; 1666 PreprocessorOpts.eraseRemappedFile( 1667 PreprocessorOpts.remapped_file_buffer_end() - 1); 1668 1669 // If the hash of top-level entities differs from the hash of the top-level 1670 // entities the last time we rebuilt the preamble, clear out the completion 1671 // cache. 1672 if (CurrentTopLevelHashValue != PreambleTopLevelHashValue) { 1673 CompletionCacheTopLevelHashValue = 0; 1674 PreambleTopLevelHashValue = CurrentTopLevelHashValue; 1675 } 1676 1677 return CreatePaddedMainFileBuffer(NewPreamble.first, 1678 PreambleReservedSize, 1679 FrontendOpts.Inputs[0].File); 1680} 1681 1682void ASTUnit::RealizeTopLevelDeclsFromPreamble() { 1683 std::vector<Decl *> Resolved; 1684 Resolved.reserve(TopLevelDeclsInPreamble.size()); 1685 ExternalASTSource &Source = *getASTContext().getExternalSource(); 1686 for (unsigned I = 0, N = TopLevelDeclsInPreamble.size(); I != N; ++I) { 1687 // Resolve the declaration ID to an actual declaration, possibly 1688 // deserializing the declaration in the process. 1689 Decl *D = Source.GetExternalDecl(TopLevelDeclsInPreamble[I]); 1690 if (D) 1691 Resolved.push_back(D); 1692 } 1693 TopLevelDeclsInPreamble.clear(); 1694 TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end()); 1695} 1696 1697void ASTUnit::transferASTDataFromCompilerInstance(CompilerInstance &CI) { 1698 // Steal the created target, context, and preprocessor. 1699 TheSema.reset(CI.takeSema()); 1700 Consumer.reset(CI.takeASTConsumer()); 1701 Ctx = &CI.getASTContext(); 1702 PP = &CI.getPreprocessor(); 1703 CI.setSourceManager(0); 1704 CI.setFileManager(0); 1705 Target = &CI.getTarget(); 1706 Reader = CI.getModuleManager(); 1707} 1708 1709StringRef ASTUnit::getMainFileName() const { 1710 return Invocation->getFrontendOpts().Inputs[0].File; 1711} 1712 1713ASTUnit *ASTUnit::create(CompilerInvocation *CI, 1714 IntrusiveRefCntPtr<DiagnosticsEngine> Diags, 1715 bool CaptureDiagnostics, 1716 bool UserFilesAreVolatile) { 1717 OwningPtr<ASTUnit> AST; 1718 AST.reset(new ASTUnit(false)); 1719 ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics); 1720 AST->Diagnostics = Diags; 1721 AST->Invocation = CI; 1722 AST->FileSystemOpts = CI->getFileSystemOpts(); 1723 AST->FileMgr = new FileManager(AST->FileSystemOpts); 1724 AST->UserFilesAreVolatile = UserFilesAreVolatile; 1725 AST->SourceMgr = new SourceManager(AST->getDiagnostics(), *AST->FileMgr, 1726 UserFilesAreVolatile); 1727 1728 return AST.take(); 1729} 1730 1731ASTUnit *ASTUnit::LoadFromCompilerInvocationAction(CompilerInvocation *CI, 1732 IntrusiveRefCntPtr<DiagnosticsEngine> Diags, 1733 ASTFrontendAction *Action, 1734 ASTUnit *Unit, 1735 bool Persistent, 1736 StringRef ResourceFilesPath, 1737 bool OnlyLocalDecls, 1738 bool CaptureDiagnostics, 1739 bool PrecompilePreamble, 1740 bool CacheCodeCompletionResults, 1741 bool IncludeBriefCommentsInCodeCompletion, 1742 bool UserFilesAreVolatile, 1743 OwningPtr<ASTUnit> *ErrAST) { 1744 assert(CI && "A CompilerInvocation is required"); 1745 1746 OwningPtr<ASTUnit> OwnAST; 1747 ASTUnit *AST = Unit; 1748 if (!AST) { 1749 // Create the AST unit. 1750 OwnAST.reset(create(CI, Diags, CaptureDiagnostics, UserFilesAreVolatile)); 1751 AST = OwnAST.get(); 1752 } 1753 1754 if (!ResourceFilesPath.empty()) { 1755 // Override the resources path. 1756 CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath; 1757 } 1758 AST->OnlyLocalDecls = OnlyLocalDecls; 1759 AST->CaptureDiagnostics = CaptureDiagnostics; 1760 if (PrecompilePreamble) 1761 AST->PreambleRebuildCounter = 2; 1762 AST->TUKind = Action ? Action->getTranslationUnitKind() : TU_Complete; 1763 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults; 1764 AST->IncludeBriefCommentsInCodeCompletion 1765 = IncludeBriefCommentsInCodeCompletion; 1766 1767 // Recover resources if we crash before exiting this method. 1768 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit> 1769 ASTUnitCleanup(OwnAST.get()); 1770 llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine, 1771 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> > 1772 DiagCleanup(Diags.getPtr()); 1773 1774 // We'll manage file buffers ourselves. 1775 CI->getPreprocessorOpts().RetainRemappedFileBuffers = true; 1776 CI->getFrontendOpts().DisableFree = false; 1777 ProcessWarningOptions(AST->getDiagnostics(), CI->getDiagnosticOpts()); 1778 1779 // Create the compiler instance to use for building the AST. 1780 OwningPtr<CompilerInstance> Clang(new CompilerInstance()); 1781 1782 // Recover resources if we crash before exiting this method. 1783 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> 1784 CICleanup(Clang.get()); 1785 1786 Clang->setInvocation(CI); 1787 AST->OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].File; 1788 1789 // Set up diagnostics, capturing any diagnostics that would 1790 // otherwise be dropped. 1791 Clang->setDiagnostics(&AST->getDiagnostics()); 1792 1793 // Create the target instance. 1794 Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(), 1795 Clang->getTargetOpts())); 1796 if (!Clang->hasTarget()) 1797 return 0; 1798 1799 // Inform the target of the language options. 1800 // 1801 // FIXME: We shouldn't need to do this, the target should be immutable once 1802 // created. This complexity should be lifted elsewhere. 1803 Clang->getTarget().setForcedLangOptions(Clang->getLangOpts()); 1804 1805 assert(Clang->getFrontendOpts().Inputs.size() == 1 && 1806 "Invocation must have exactly one source file!"); 1807 assert(Clang->getFrontendOpts().Inputs[0].Kind != IK_AST && 1808 "FIXME: AST inputs not yet supported here!"); 1809 assert(Clang->getFrontendOpts().Inputs[0].Kind != IK_LLVM_IR && 1810 "IR inputs not supported here!"); 1811 1812 // Configure the various subsystems. 1813 AST->TheSema.reset(); 1814 AST->Ctx = 0; 1815 AST->PP = 0; 1816 AST->Reader = 0; 1817 1818 // Create a file manager object to provide access to and cache the filesystem. 1819 Clang->setFileManager(&AST->getFileManager()); 1820 1821 // Create the source manager. 1822 Clang->setSourceManager(&AST->getSourceManager()); 1823 1824 ASTFrontendAction *Act = Action; 1825 1826 OwningPtr<TopLevelDeclTrackerAction> TrackerAct; 1827 if (!Act) { 1828 TrackerAct.reset(new TopLevelDeclTrackerAction(*AST)); 1829 Act = TrackerAct.get(); 1830 } 1831 1832 // Recover resources if we crash before exiting this method. 1833 llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction> 1834 ActCleanup(TrackerAct.get()); 1835 1836 if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) { 1837 AST->transferASTDataFromCompilerInstance(*Clang); 1838 if (OwnAST && ErrAST) 1839 ErrAST->swap(OwnAST); 1840 1841 return 0; 1842 } 1843 1844 if (Persistent && !TrackerAct) { 1845 Clang->getPreprocessor().addPPCallbacks( 1846 new MacroDefinitionTrackerPPCallbacks(AST->getCurrentTopLevelHashValue())); 1847 std::vector<ASTConsumer*> Consumers; 1848 if (Clang->hasASTConsumer()) 1849 Consumers.push_back(Clang->takeASTConsumer()); 1850 Consumers.push_back(new TopLevelDeclTrackerConsumer(*AST, 1851 AST->getCurrentTopLevelHashValue())); 1852 Clang->setASTConsumer(new MultiplexConsumer(Consumers)); 1853 } 1854 if (!Act->Execute()) { 1855 AST->transferASTDataFromCompilerInstance(*Clang); 1856 if (OwnAST && ErrAST) 1857 ErrAST->swap(OwnAST); 1858 1859 return 0; 1860 } 1861 1862 // Steal the created target, context, and preprocessor. 1863 AST->transferASTDataFromCompilerInstance(*Clang); 1864 1865 Act->EndSourceFile(); 1866 1867 if (OwnAST) 1868 return OwnAST.take(); 1869 else 1870 return AST; 1871} 1872 1873bool ASTUnit::LoadFromCompilerInvocation(bool PrecompilePreamble) { 1874 if (!Invocation) 1875 return true; 1876 1877 // We'll manage file buffers ourselves. 1878 Invocation->getPreprocessorOpts().RetainRemappedFileBuffers = true; 1879 Invocation->getFrontendOpts().DisableFree = false; 1880 ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts()); 1881 1882 llvm::MemoryBuffer *OverrideMainBuffer = 0; 1883 if (PrecompilePreamble) { 1884 PreambleRebuildCounter = 2; 1885 OverrideMainBuffer 1886 = getMainBufferWithPrecompiledPreamble(*Invocation); 1887 } 1888 1889 SimpleTimer ParsingTimer(WantTiming); 1890 ParsingTimer.setOutput("Parsing " + getMainFileName()); 1891 1892 // Recover resources if we crash before exiting this method. 1893 llvm::CrashRecoveryContextCleanupRegistrar<llvm::MemoryBuffer> 1894 MemBufferCleanup(OverrideMainBuffer); 1895 1896 return Parse(OverrideMainBuffer); 1897} 1898 1899ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI, 1900 IntrusiveRefCntPtr<DiagnosticsEngine> Diags, 1901 bool OnlyLocalDecls, 1902 bool CaptureDiagnostics, 1903 bool PrecompilePreamble, 1904 TranslationUnitKind TUKind, 1905 bool CacheCodeCompletionResults, 1906 bool IncludeBriefCommentsInCodeCompletion, 1907 bool UserFilesAreVolatile) { 1908 // Create the AST unit. 1909 OwningPtr<ASTUnit> AST; 1910 AST.reset(new ASTUnit(false)); 1911 ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics); 1912 AST->Diagnostics = Diags; 1913 AST->OnlyLocalDecls = OnlyLocalDecls; 1914 AST->CaptureDiagnostics = CaptureDiagnostics; 1915 AST->TUKind = TUKind; 1916 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults; 1917 AST->IncludeBriefCommentsInCodeCompletion 1918 = IncludeBriefCommentsInCodeCompletion; 1919 AST->Invocation = CI; 1920 AST->UserFilesAreVolatile = UserFilesAreVolatile; 1921 1922 // Recover resources if we crash before exiting this method. 1923 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit> 1924 ASTUnitCleanup(AST.get()); 1925 llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine, 1926 llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> > 1927 DiagCleanup(Diags.getPtr()); 1928 1929 return AST->LoadFromCompilerInvocation(PrecompilePreamble)? 0 : AST.take(); 1930} 1931 1932ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin, 1933 const char **ArgEnd, 1934 IntrusiveRefCntPtr<DiagnosticsEngine> Diags, 1935 StringRef ResourceFilesPath, 1936 bool OnlyLocalDecls, 1937 bool CaptureDiagnostics, 1938 RemappedFile *RemappedFiles, 1939 unsigned NumRemappedFiles, 1940 bool RemappedFilesKeepOriginalName, 1941 bool PrecompilePreamble, 1942 TranslationUnitKind TUKind, 1943 bool CacheCodeCompletionResults, 1944 bool IncludeBriefCommentsInCodeCompletion, 1945 bool AllowPCHWithCompilerErrors, 1946 bool SkipFunctionBodies, 1947 bool UserFilesAreVolatile, 1948 bool ForSerialization, 1949 OwningPtr<ASTUnit> *ErrAST) { 1950 if (!Diags.getPtr()) { 1951 // No diagnostics engine was provided, so create our own diagnostics object 1952 // with the default options. 1953 Diags = CompilerInstance::createDiagnostics(new DiagnosticOptions(), 1954 ArgEnd - ArgBegin, 1955 ArgBegin); 1956 } 1957 1958 SmallVector<StoredDiagnostic, 4> StoredDiagnostics; 1959 1960 IntrusiveRefCntPtr<CompilerInvocation> CI; 1961 1962 { 1963 1964 CaptureDroppedDiagnostics Capture(CaptureDiagnostics, *Diags, 1965 StoredDiagnostics); 1966 1967 CI = clang::createInvocationFromCommandLine( 1968 llvm::makeArrayRef(ArgBegin, ArgEnd), 1969 Diags); 1970 if (!CI) 1971 return 0; 1972 } 1973 1974 // Override any files that need remapping 1975 for (unsigned I = 0; I != NumRemappedFiles; ++I) { 1976 FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second; 1977 if (const llvm::MemoryBuffer * 1978 memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) { 1979 CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, memBuf); 1980 } else { 1981 const char *fname = fileOrBuf.get<const char *>(); 1982 CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, fname); 1983 } 1984 } 1985 PreprocessorOptions &PPOpts = CI->getPreprocessorOpts(); 1986 PPOpts.RemappedFilesKeepOriginalName = RemappedFilesKeepOriginalName; 1987 PPOpts.AllowPCHWithCompilerErrors = AllowPCHWithCompilerErrors; 1988 1989 // Override the resources path. 1990 CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath; 1991 1992 CI->getFrontendOpts().SkipFunctionBodies = SkipFunctionBodies; 1993 1994 // Create the AST unit. 1995 OwningPtr<ASTUnit> AST; 1996 AST.reset(new ASTUnit(false)); 1997 ConfigureDiags(Diags, ArgBegin, ArgEnd, *AST, CaptureDiagnostics); 1998 AST->Diagnostics = Diags; 1999 Diags = 0; // Zero out now to ease cleanup during crash recovery. 2000 AST->FileSystemOpts = CI->getFileSystemOpts(); 2001 AST->FileMgr = new FileManager(AST->FileSystemOpts); 2002 AST->OnlyLocalDecls = OnlyLocalDecls; 2003 AST->CaptureDiagnostics = CaptureDiagnostics; 2004 AST->TUKind = TUKind; 2005 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults; 2006 AST->IncludeBriefCommentsInCodeCompletion 2007 = IncludeBriefCommentsInCodeCompletion; 2008 AST->UserFilesAreVolatile = UserFilesAreVolatile; 2009 AST->NumStoredDiagnosticsFromDriver = StoredDiagnostics.size(); 2010 AST->StoredDiagnostics.swap(StoredDiagnostics); 2011 AST->Invocation = CI; 2012 if (ForSerialization) 2013 AST->WriterData.reset(new ASTWriterData()); 2014 CI = 0; // Zero out now to ease cleanup during crash recovery. 2015 2016 // Recover resources if we crash before exiting this method. 2017 llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit> 2018 ASTUnitCleanup(AST.get()); 2019 2020 if (AST->LoadFromCompilerInvocation(PrecompilePreamble)) { 2021 // Some error occurred, if caller wants to examine diagnostics, pass it the 2022 // ASTUnit. 2023 if (ErrAST) { 2024 AST->StoredDiagnostics.swap(AST->FailedParseDiagnostics); 2025 ErrAST->swap(AST); 2026 } 2027 return 0; 2028 } 2029 2030 return AST.take(); 2031} 2032 2033bool ASTUnit::Reparse(RemappedFile *RemappedFiles, unsigned NumRemappedFiles) { 2034 if (!Invocation) 2035 return true; 2036 2037 clearFileLevelDecls(); 2038 2039 SimpleTimer ParsingTimer(WantTiming); 2040 ParsingTimer.setOutput("Reparsing " + getMainFileName()); 2041 2042 // Remap files. 2043 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts(); 2044 PPOpts.DisableStatCache = true; 2045 for (PreprocessorOptions::remapped_file_buffer_iterator 2046 R = PPOpts.remapped_file_buffer_begin(), 2047 REnd = PPOpts.remapped_file_buffer_end(); 2048 R != REnd; 2049 ++R) { 2050 delete R->second; 2051 } 2052 Invocation->getPreprocessorOpts().clearRemappedFiles(); 2053 for (unsigned I = 0; I != NumRemappedFiles; ++I) { 2054 FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second; 2055 if (const llvm::MemoryBuffer * 2056 memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) { 2057 Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, 2058 memBuf); 2059 } else { 2060 const char *fname = fileOrBuf.get<const char *>(); 2061 Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, 2062 fname); 2063 } 2064 } 2065 2066 // If we have a preamble file lying around, or if we might try to 2067 // build a precompiled preamble, do so now. 2068 llvm::MemoryBuffer *OverrideMainBuffer = 0; 2069 if (!getPreambleFile(this).empty() || PreambleRebuildCounter > 0) 2070 OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(*Invocation); 2071 2072 // Clear out the diagnostics state. 2073 getDiagnostics().Reset(); 2074 ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts()); 2075 if (OverrideMainBuffer) 2076 getDiagnostics().setNumWarnings(NumWarningsInPreamble); 2077 2078 // Parse the sources 2079 bool Result = Parse(OverrideMainBuffer); 2080 2081 // If we're caching global code-completion results, and the top-level 2082 // declarations have changed, clear out the code-completion cache. 2083 if (!Result && ShouldCacheCodeCompletionResults && 2084 CurrentTopLevelHashValue != CompletionCacheTopLevelHashValue) 2085 CacheCodeCompletionResults(); 2086 2087 // We now need to clear out the completion info related to this translation 2088 // unit; it'll be recreated if necessary. 2089 CCTUInfo.reset(); 2090 2091 return Result; 2092} 2093 2094//----------------------------------------------------------------------------// 2095// Code completion 2096//----------------------------------------------------------------------------// 2097 2098namespace { 2099 /// \brief Code completion consumer that combines the cached code-completion 2100 /// results from an ASTUnit with the code-completion results provided to it, 2101 /// then passes the result on to 2102 class AugmentedCodeCompleteConsumer : public CodeCompleteConsumer { 2103 uint64_t NormalContexts; 2104 ASTUnit &AST; 2105 CodeCompleteConsumer &Next; 2106 2107 public: 2108 AugmentedCodeCompleteConsumer(ASTUnit &AST, CodeCompleteConsumer &Next, 2109 const CodeCompleteOptions &CodeCompleteOpts) 2110 : CodeCompleteConsumer(CodeCompleteOpts, Next.isOutputBinary()), 2111 AST(AST), Next(Next) 2112 { 2113 // Compute the set of contexts in which we will look when we don't have 2114 // any information about the specific context. 2115 NormalContexts 2116 = (1LL << CodeCompletionContext::CCC_TopLevel) 2117 | (1LL << CodeCompletionContext::CCC_ObjCInterface) 2118 | (1LL << CodeCompletionContext::CCC_ObjCImplementation) 2119 | (1LL << CodeCompletionContext::CCC_ObjCIvarList) 2120 | (1LL << CodeCompletionContext::CCC_Statement) 2121 | (1LL << CodeCompletionContext::CCC_Expression) 2122 | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver) 2123 | (1LL << CodeCompletionContext::CCC_DotMemberAccess) 2124 | (1LL << CodeCompletionContext::CCC_ArrowMemberAccess) 2125 | (1LL << CodeCompletionContext::CCC_ObjCPropertyAccess) 2126 | (1LL << CodeCompletionContext::CCC_ObjCProtocolName) 2127 | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression) 2128 | (1LL << CodeCompletionContext::CCC_Recovery); 2129 2130 if (AST.getASTContext().getLangOpts().CPlusPlus) 2131 NormalContexts |= (1LL << CodeCompletionContext::CCC_EnumTag) 2132 | (1LL << CodeCompletionContext::CCC_UnionTag) 2133 | (1LL << CodeCompletionContext::CCC_ClassOrStructTag); 2134 } 2135 2136 virtual void ProcessCodeCompleteResults(Sema &S, 2137 CodeCompletionContext Context, 2138 CodeCompletionResult *Results, 2139 unsigned NumResults); 2140 2141 virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, 2142 OverloadCandidate *Candidates, 2143 unsigned NumCandidates) { 2144 Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates); 2145 } 2146 2147 virtual CodeCompletionAllocator &getAllocator() { 2148 return Next.getAllocator(); 2149 } 2150 2151 virtual CodeCompletionTUInfo &getCodeCompletionTUInfo() { 2152 return Next.getCodeCompletionTUInfo(); 2153 } 2154 }; 2155} 2156 2157/// \brief Helper function that computes which global names are hidden by the 2158/// local code-completion results. 2159static void CalculateHiddenNames(const CodeCompletionContext &Context, 2160 CodeCompletionResult *Results, 2161 unsigned NumResults, 2162 ASTContext &Ctx, 2163 llvm::StringSet<llvm::BumpPtrAllocator> &HiddenNames){ 2164 bool OnlyTagNames = false; 2165 switch (Context.getKind()) { 2166 case CodeCompletionContext::CCC_Recovery: 2167 case CodeCompletionContext::CCC_TopLevel: 2168 case CodeCompletionContext::CCC_ObjCInterface: 2169 case CodeCompletionContext::CCC_ObjCImplementation: 2170 case CodeCompletionContext::CCC_ObjCIvarList: 2171 case CodeCompletionContext::CCC_ClassStructUnion: 2172 case CodeCompletionContext::CCC_Statement: 2173 case CodeCompletionContext::CCC_Expression: 2174 case CodeCompletionContext::CCC_ObjCMessageReceiver: 2175 case CodeCompletionContext::CCC_DotMemberAccess: 2176 case CodeCompletionContext::CCC_ArrowMemberAccess: 2177 case CodeCompletionContext::CCC_ObjCPropertyAccess: 2178 case CodeCompletionContext::CCC_Namespace: 2179 case CodeCompletionContext::CCC_Type: 2180 case CodeCompletionContext::CCC_Name: 2181 case CodeCompletionContext::CCC_PotentiallyQualifiedName: 2182 case CodeCompletionContext::CCC_ParenthesizedExpression: 2183 case CodeCompletionContext::CCC_ObjCInterfaceName: 2184 break; 2185 2186 case CodeCompletionContext::CCC_EnumTag: 2187 case CodeCompletionContext::CCC_UnionTag: 2188 case CodeCompletionContext::CCC_ClassOrStructTag: 2189 OnlyTagNames = true; 2190 break; 2191 2192 case CodeCompletionContext::CCC_ObjCProtocolName: 2193 case CodeCompletionContext::CCC_MacroName: 2194 case CodeCompletionContext::CCC_MacroNameUse: 2195 case CodeCompletionContext::CCC_PreprocessorExpression: 2196 case CodeCompletionContext::CCC_PreprocessorDirective: 2197 case CodeCompletionContext::CCC_NaturalLanguage: 2198 case CodeCompletionContext::CCC_SelectorName: 2199 case CodeCompletionContext::CCC_TypeQualifiers: 2200 case CodeCompletionContext::CCC_Other: 2201 case CodeCompletionContext::CCC_OtherWithMacros: 2202 case CodeCompletionContext::CCC_ObjCInstanceMessage: 2203 case CodeCompletionContext::CCC_ObjCClassMessage: 2204 case CodeCompletionContext::CCC_ObjCCategoryName: 2205 // We're looking for nothing, or we're looking for names that cannot 2206 // be hidden. 2207 return; 2208 } 2209 2210 typedef CodeCompletionResult Result; 2211 for (unsigned I = 0; I != NumResults; ++I) { 2212 if (Results[I].Kind != Result::RK_Declaration) 2213 continue; 2214 2215 unsigned IDNS 2216 = Results[I].Declaration->getUnderlyingDecl()->getIdentifierNamespace(); 2217 2218 bool Hiding = false; 2219 if (OnlyTagNames) 2220 Hiding = (IDNS & Decl::IDNS_Tag); 2221 else { 2222 unsigned HiddenIDNS = (Decl::IDNS_Type | Decl::IDNS_Member | 2223 Decl::IDNS_Namespace | Decl::IDNS_Ordinary | 2224 Decl::IDNS_NonMemberOperator); 2225 if (Ctx.getLangOpts().CPlusPlus) 2226 HiddenIDNS |= Decl::IDNS_Tag; 2227 Hiding = (IDNS & HiddenIDNS); 2228 } 2229 2230 if (!Hiding) 2231 continue; 2232 2233 DeclarationName Name = Results[I].Declaration->getDeclName(); 2234 if (IdentifierInfo *Identifier = Name.getAsIdentifierInfo()) 2235 HiddenNames.insert(Identifier->getName()); 2236 else 2237 HiddenNames.insert(Name.getAsString()); 2238 } 2239} 2240 2241 2242void AugmentedCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &S, 2243 CodeCompletionContext Context, 2244 CodeCompletionResult *Results, 2245 unsigned NumResults) { 2246 // Merge the results we were given with the results we cached. 2247 bool AddedResult = false; 2248 uint64_t InContexts = 2249 Context.getKind() == CodeCompletionContext::CCC_Recovery 2250 ? NormalContexts : (1LL << Context.getKind()); 2251 // Contains the set of names that are hidden by "local" completion results. 2252 llvm::StringSet<llvm::BumpPtrAllocator> HiddenNames; 2253 typedef CodeCompletionResult Result; 2254 SmallVector<Result, 8> AllResults; 2255 for (ASTUnit::cached_completion_iterator 2256 C = AST.cached_completion_begin(), 2257 CEnd = AST.cached_completion_end(); 2258 C != CEnd; ++C) { 2259 // If the context we are in matches any of the contexts we are 2260 // interested in, we'll add this result. 2261 if ((C->ShowInContexts & InContexts) == 0) 2262 continue; 2263 2264 // If we haven't added any results previously, do so now. 2265 if (!AddedResult) { 2266 CalculateHiddenNames(Context, Results, NumResults, S.Context, 2267 HiddenNames); 2268 AllResults.insert(AllResults.end(), Results, Results + NumResults); 2269 AddedResult = true; 2270 } 2271 2272 // Determine whether this global completion result is hidden by a local 2273 // completion result. If so, skip it. 2274 if (C->Kind != CXCursor_MacroDefinition && 2275 HiddenNames.count(C->Completion->getTypedText())) 2276 continue; 2277 2278 // Adjust priority based on similar type classes. 2279 unsigned Priority = C->Priority; 2280 CodeCompletionString *Completion = C->Completion; 2281 if (!Context.getPreferredType().isNull()) { 2282 if (C->Kind == CXCursor_MacroDefinition) { 2283 Priority = getMacroUsagePriority(C->Completion->getTypedText(), 2284 S.getLangOpts(), 2285 Context.getPreferredType()->isAnyPointerType()); 2286 } else if (C->Type) { 2287 CanQualType Expected 2288 = S.Context.getCanonicalType( 2289 Context.getPreferredType().getUnqualifiedType()); 2290 SimplifiedTypeClass ExpectedSTC = getSimplifiedTypeClass(Expected); 2291 if (ExpectedSTC == C->TypeClass) { 2292 // We know this type is similar; check for an exact match. 2293 llvm::StringMap<unsigned> &CachedCompletionTypes 2294 = AST.getCachedCompletionTypes(); 2295 llvm::StringMap<unsigned>::iterator Pos 2296 = CachedCompletionTypes.find(QualType(Expected).getAsString()); 2297 if (Pos != CachedCompletionTypes.end() && Pos->second == C->Type) 2298 Priority /= CCF_ExactTypeMatch; 2299 else 2300 Priority /= CCF_SimilarTypeMatch; 2301 } 2302 } 2303 } 2304 2305 // Adjust the completion string, if required. 2306 if (C->Kind == CXCursor_MacroDefinition && 2307 Context.getKind() == CodeCompletionContext::CCC_MacroNameUse) { 2308 // Create a new code-completion string that just contains the 2309 // macro name, without its arguments. 2310 CodeCompletionBuilder Builder(getAllocator(), getCodeCompletionTUInfo(), 2311 CCP_CodePattern, C->Availability); 2312 Builder.AddTypedTextChunk(C->Completion->getTypedText()); 2313 Priority = CCP_CodePattern; 2314 Completion = Builder.TakeString(); 2315 } 2316 2317 AllResults.push_back(Result(Completion, Priority, C->Kind, 2318 C->Availability)); 2319 } 2320 2321 // If we did not add any cached completion results, just forward the 2322 // results we were given to the next consumer. 2323 if (!AddedResult) { 2324 Next.ProcessCodeCompleteResults(S, Context, Results, NumResults); 2325 return; 2326 } 2327 2328 Next.ProcessCodeCompleteResults(S, Context, AllResults.data(), 2329 AllResults.size()); 2330} 2331 2332 2333 2334void ASTUnit::CodeComplete(StringRef File, unsigned Line, unsigned Column, 2335 RemappedFile *RemappedFiles, 2336 unsigned NumRemappedFiles, 2337 bool IncludeMacros, 2338 bool IncludeCodePatterns, 2339 bool IncludeBriefComments, 2340 CodeCompleteConsumer &Consumer, 2341 DiagnosticsEngine &Diag, LangOptions &LangOpts, 2342 SourceManager &SourceMgr, FileManager &FileMgr, 2343 SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics, 2344 SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers) { 2345 if (!Invocation) 2346 return; 2347 2348 SimpleTimer CompletionTimer(WantTiming); 2349 CompletionTimer.setOutput("Code completion @ " + File + ":" + 2350 Twine(Line) + ":" + Twine(Column)); 2351 2352 IntrusiveRefCntPtr<CompilerInvocation> 2353 CCInvocation(new CompilerInvocation(*Invocation)); 2354 2355 FrontendOptions &FrontendOpts = CCInvocation->getFrontendOpts(); 2356 CodeCompleteOptions &CodeCompleteOpts = FrontendOpts.CodeCompleteOpts; 2357 PreprocessorOptions &PreprocessorOpts = CCInvocation->getPreprocessorOpts(); 2358 2359 CodeCompleteOpts.IncludeMacros = IncludeMacros && 2360 CachedCompletionResults.empty(); 2361 CodeCompleteOpts.IncludeCodePatterns = IncludeCodePatterns; 2362 CodeCompleteOpts.IncludeGlobals = CachedCompletionResults.empty(); 2363 CodeCompleteOpts.IncludeBriefComments = IncludeBriefComments; 2364 2365 assert(IncludeBriefComments == this->IncludeBriefCommentsInCodeCompletion); 2366 2367 FrontendOpts.CodeCompletionAt.FileName = File; 2368 FrontendOpts.CodeCompletionAt.Line = Line; 2369 FrontendOpts.CodeCompletionAt.Column = Column; 2370 2371 // Set the language options appropriately. 2372 LangOpts = *CCInvocation->getLangOpts(); 2373 2374 OwningPtr<CompilerInstance> Clang(new CompilerInstance()); 2375 2376 // Recover resources if we crash before exiting this method. 2377 llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> 2378 CICleanup(Clang.get()); 2379 2380 Clang->setInvocation(&*CCInvocation); 2381 OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].File; 2382 2383 // Set up diagnostics, capturing any diagnostics produced. 2384 Clang->setDiagnostics(&Diag); 2385 ProcessWarningOptions(Diag, CCInvocation->getDiagnosticOpts()); 2386 CaptureDroppedDiagnostics Capture(true, 2387 Clang->getDiagnostics(), 2388 StoredDiagnostics); 2389 2390 // Create the target instance. 2391 Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(), 2392 Clang->getTargetOpts())); 2393 if (!Clang->hasTarget()) { 2394 Clang->setInvocation(0); 2395 return; 2396 } 2397 2398 // Inform the target of the language options. 2399 // 2400 // FIXME: We shouldn't need to do this, the target should be immutable once 2401 // created. This complexity should be lifted elsewhere. 2402 Clang->getTarget().setForcedLangOptions(Clang->getLangOpts()); 2403 2404 assert(Clang->getFrontendOpts().Inputs.size() == 1 && 2405 "Invocation must have exactly one source file!"); 2406 assert(Clang->getFrontendOpts().Inputs[0].Kind != IK_AST && 2407 "FIXME: AST inputs not yet supported here!"); 2408 assert(Clang->getFrontendOpts().Inputs[0].Kind != IK_LLVM_IR && 2409 "IR inputs not support here!"); 2410 2411 2412 // Use the source and file managers that we were given. 2413 Clang->setFileManager(&FileMgr); 2414 Clang->setSourceManager(&SourceMgr); 2415 2416 // Remap files. 2417 PreprocessorOpts.clearRemappedFiles(); 2418 PreprocessorOpts.RetainRemappedFileBuffers = true; 2419 for (unsigned I = 0; I != NumRemappedFiles; ++I) { 2420 FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second; 2421 if (const llvm::MemoryBuffer * 2422 memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) { 2423 PreprocessorOpts.addRemappedFile(RemappedFiles[I].first, memBuf); 2424 OwnedBuffers.push_back(memBuf); 2425 } else { 2426 const char *fname = fileOrBuf.get<const char *>(); 2427 PreprocessorOpts.addRemappedFile(RemappedFiles[I].first, fname); 2428 } 2429 } 2430 2431 // Use the code completion consumer we were given, but adding any cached 2432 // code-completion results. 2433 AugmentedCodeCompleteConsumer *AugmentedConsumer 2434 = new AugmentedCodeCompleteConsumer(*this, Consumer, CodeCompleteOpts); 2435 Clang->setCodeCompletionConsumer(AugmentedConsumer); 2436 2437 Clang->getFrontendOpts().SkipFunctionBodies = true; 2438 2439 // If we have a precompiled preamble, try to use it. We only allow 2440 // the use of the precompiled preamble if we're if the completion 2441 // point is within the main file, after the end of the precompiled 2442 // preamble. 2443 llvm::MemoryBuffer *OverrideMainBuffer = 0; 2444 if (!getPreambleFile(this).empty()) { 2445 using llvm::sys::FileStatus; 2446 llvm::sys::PathWithStatus CompleteFilePath(File); 2447 llvm::sys::PathWithStatus MainPath(OriginalSourceFile); 2448 if (const FileStatus *CompleteFileStatus = CompleteFilePath.getFileStatus()) 2449 if (const FileStatus *MainStatus = MainPath.getFileStatus()) 2450 if (CompleteFileStatus->getUniqueID() == MainStatus->getUniqueID() && 2451 Line > 1) 2452 OverrideMainBuffer 2453 = getMainBufferWithPrecompiledPreamble(*CCInvocation, false, 2454 Line - 1); 2455 } 2456 2457 // If the main file has been overridden due to the use of a preamble, 2458 // make that override happen and introduce the preamble. 2459 PreprocessorOpts.DisableStatCache = true; 2460 StoredDiagnostics.insert(StoredDiagnostics.end(), 2461 stored_diag_begin(), 2462 stored_diag_afterDriver_begin()); 2463 if (OverrideMainBuffer) { 2464 PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer); 2465 PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size(); 2466 PreprocessorOpts.PrecompiledPreambleBytes.second 2467 = PreambleEndsAtStartOfLine; 2468 PreprocessorOpts.ImplicitPCHInclude = getPreambleFile(this); 2469 PreprocessorOpts.DisablePCHValidation = true; 2470 2471 OwnedBuffers.push_back(OverrideMainBuffer); 2472 } else { 2473 PreprocessorOpts.PrecompiledPreambleBytes.first = 0; 2474 PreprocessorOpts.PrecompiledPreambleBytes.second = false; 2475 } 2476 2477 // Disable the preprocessing record 2478 PreprocessorOpts.DetailedRecord = false; 2479 2480 OwningPtr<SyntaxOnlyAction> Act; 2481 Act.reset(new SyntaxOnlyAction); 2482 if (Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) { 2483 if (OverrideMainBuffer) { 2484 std::string ModName = getPreambleFile(this); 2485 TranslateStoredDiagnostics(Clang->getModuleManager(), ModName, 2486 getSourceManager(), PreambleDiagnostics, 2487 StoredDiagnostics); 2488 } 2489 Act->Execute(); 2490 Act->EndSourceFile(); 2491 } 2492 2493 checkAndSanitizeDiags(StoredDiagnostics, getSourceManager()); 2494} 2495 2496bool ASTUnit::Save(StringRef File) { 2497 // Write to a temporary file and later rename it to the actual file, to avoid 2498 // possible race conditions. 2499 SmallString<128> TempPath; 2500 TempPath = File; 2501 TempPath += "-%%%%%%%%"; 2502 int fd; 2503 if (llvm::sys::fs::unique_file(TempPath.str(), fd, TempPath, 2504 /*makeAbsolute=*/false)) 2505 return true; 2506 2507 // FIXME: Can we somehow regenerate the stat cache here, or do we need to 2508 // unconditionally create a stat cache when we parse the file? 2509 llvm::raw_fd_ostream Out(fd, /*shouldClose=*/true); 2510 2511 serialize(Out); 2512 Out.close(); 2513 if (Out.has_error()) { 2514 Out.clear_error(); 2515 return true; 2516 } 2517 2518 if (llvm::sys::fs::rename(TempPath.str(), File)) { 2519 bool exists; 2520 llvm::sys::fs::remove(TempPath.str(), exists); 2521 return true; 2522 } 2523 2524 return false; 2525} 2526 2527static bool serializeUnit(ASTWriter &Writer, 2528 SmallVectorImpl<char> &Buffer, 2529 Sema &S, 2530 bool hasErrors, 2531 raw_ostream &OS) { 2532 Writer.WriteAST(S, 0, std::string(), 0, "", hasErrors); 2533 2534 // Write the generated bitstream to "Out". 2535 if (!Buffer.empty()) 2536 OS.write(Buffer.data(), Buffer.size()); 2537 2538 return false; 2539} 2540 2541bool ASTUnit::serialize(raw_ostream &OS) { 2542 bool hasErrors = getDiagnostics().hasErrorOccurred(); 2543 2544 if (WriterData) 2545 return serializeUnit(WriterData->Writer, WriterData->Buffer, 2546 getSema(), hasErrors, OS); 2547 2548 SmallString<128> Buffer; 2549 llvm::BitstreamWriter Stream(Buffer); 2550 ASTWriter Writer(Stream); 2551 return serializeUnit(Writer, Buffer, getSema(), hasErrors, OS); 2552} 2553 2554typedef ContinuousRangeMap<unsigned, int, 2> SLocRemap; 2555 2556static void TranslateSLoc(SourceLocation &L, SLocRemap &Remap) { 2557 unsigned Raw = L.getRawEncoding(); 2558 const unsigned MacroBit = 1U << 31; 2559 L = SourceLocation::getFromRawEncoding((Raw & MacroBit) | 2560 ((Raw & ~MacroBit) + Remap.find(Raw & ~MacroBit)->second)); 2561} 2562 2563void ASTUnit::TranslateStoredDiagnostics( 2564 ASTReader *MMan, 2565 StringRef ModName, 2566 SourceManager &SrcMgr, 2567 const SmallVectorImpl<StoredDiagnostic> &Diags, 2568 SmallVectorImpl<StoredDiagnostic> &Out) { 2569 // The stored diagnostic has the old source manager in it; update 2570 // the locations to refer into the new source manager. We also need to remap 2571 // all the locations to the new view. This includes the diag location, any 2572 // associated source ranges, and the source ranges of associated fix-its. 2573 // FIXME: There should be a cleaner way to do this. 2574 2575 SmallVector<StoredDiagnostic, 4> Result; 2576 Result.reserve(Diags.size()); 2577 assert(MMan && "Don't have a module manager"); 2578 serialization::ModuleFile *Mod = MMan->ModuleMgr.lookup(ModName); 2579 assert(Mod && "Don't have preamble module"); 2580 SLocRemap &Remap = Mod->SLocRemap; 2581 for (unsigned I = 0, N = Diags.size(); I != N; ++I) { 2582 // Rebuild the StoredDiagnostic. 2583 const StoredDiagnostic &SD = Diags[I]; 2584 SourceLocation L = SD.getLocation(); 2585 TranslateSLoc(L, Remap); 2586 FullSourceLoc Loc(L, SrcMgr); 2587 2588 SmallVector<CharSourceRange, 4> Ranges; 2589 Ranges.reserve(SD.range_size()); 2590 for (StoredDiagnostic::range_iterator I = SD.range_begin(), 2591 E = SD.range_end(); 2592 I != E; ++I) { 2593 SourceLocation BL = I->getBegin(); 2594 TranslateSLoc(BL, Remap); 2595 SourceLocation EL = I->getEnd(); 2596 TranslateSLoc(EL, Remap); 2597 Ranges.push_back(CharSourceRange(SourceRange(BL, EL), I->isTokenRange())); 2598 } 2599 2600 SmallVector<FixItHint, 2> FixIts; 2601 FixIts.reserve(SD.fixit_size()); 2602 for (StoredDiagnostic::fixit_iterator I = SD.fixit_begin(), 2603 E = SD.fixit_end(); 2604 I != E; ++I) { 2605 FixIts.push_back(FixItHint()); 2606 FixItHint &FH = FixIts.back(); 2607 FH.CodeToInsert = I->CodeToInsert; 2608 SourceLocation BL = I->RemoveRange.getBegin(); 2609 TranslateSLoc(BL, Remap); 2610 SourceLocation EL = I->RemoveRange.getEnd(); 2611 TranslateSLoc(EL, Remap); 2612 FH.RemoveRange = CharSourceRange(SourceRange(BL, EL), 2613 I->RemoveRange.isTokenRange()); 2614 } 2615 2616 Result.push_back(StoredDiagnostic(SD.getLevel(), SD.getID(), 2617 SD.getMessage(), Loc, Ranges, FixIts)); 2618 } 2619 Result.swap(Out); 2620} 2621 2622static inline bool compLocDecl(std::pair<unsigned, Decl *> L, 2623 std::pair<unsigned, Decl *> R) { 2624 return L.first < R.first; 2625} 2626 2627void ASTUnit::addFileLevelDecl(Decl *D) { 2628 assert(D); 2629 2630 // We only care about local declarations. 2631 if (D->isFromASTFile()) 2632 return; 2633 2634 SourceManager &SM = *SourceMgr; 2635 SourceLocation Loc = D->getLocation(); 2636 if (Loc.isInvalid() || !SM.isLocalSourceLocation(Loc)) 2637 return; 2638 2639 // We only keep track of the file-level declarations of each file. 2640 if (!D->getLexicalDeclContext()->isFileContext()) 2641 return; 2642 2643 SourceLocation FileLoc = SM.getFileLoc(Loc); 2644 assert(SM.isLocalSourceLocation(FileLoc)); 2645 FileID FID; 2646 unsigned Offset; 2647 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc); 2648 if (FID.isInvalid()) 2649 return; 2650 2651 LocDeclsTy *&Decls = FileDecls[FID]; 2652 if (!Decls) 2653 Decls = new LocDeclsTy(); 2654 2655 std::pair<unsigned, Decl *> LocDecl(Offset, D); 2656 2657 if (Decls->empty() || Decls->back().first <= Offset) { 2658 Decls->push_back(LocDecl); 2659 return; 2660 } 2661 2662 LocDeclsTy::iterator 2663 I = std::upper_bound(Decls->begin(), Decls->end(), LocDecl, compLocDecl); 2664 2665 Decls->insert(I, LocDecl); 2666} 2667 2668void ASTUnit::findFileRegionDecls(FileID File, unsigned Offset, unsigned Length, 2669 SmallVectorImpl<Decl *> &Decls) { 2670 if (File.isInvalid()) 2671 return; 2672 2673 if (SourceMgr->isLoadedFileID(File)) { 2674 assert(Ctx->getExternalSource() && "No external source!"); 2675 return Ctx->getExternalSource()->FindFileRegionDecls(File, Offset, Length, 2676 Decls); 2677 } 2678 2679 FileDeclsTy::iterator I = FileDecls.find(File); 2680 if (I == FileDecls.end()) 2681 return; 2682 2683 LocDeclsTy &LocDecls = *I->second; 2684 if (LocDecls.empty()) 2685 return; 2686 2687 LocDeclsTy::iterator 2688 BeginIt = std::lower_bound(LocDecls.begin(), LocDecls.end(), 2689 std::make_pair(Offset, (Decl*)0), compLocDecl); 2690 if (BeginIt != LocDecls.begin()) 2691 --BeginIt; 2692 2693 // If we are pointing at a top-level decl inside an objc container, we need 2694 // to backtrack until we find it otherwise we will fail to report that the 2695 // region overlaps with an objc container. 2696 while (BeginIt != LocDecls.begin() && 2697 BeginIt->second->isTopLevelDeclInObjCContainer()) 2698 --BeginIt; 2699 2700 LocDeclsTy::iterator 2701 EndIt = std::upper_bound(LocDecls.begin(), LocDecls.end(), 2702 std::make_pair(Offset+Length, (Decl*)0), 2703 compLocDecl); 2704 if (EndIt != LocDecls.end()) 2705 ++EndIt; 2706 2707 for (LocDeclsTy::iterator DIt = BeginIt; DIt != EndIt; ++DIt) 2708 Decls.push_back(DIt->second); 2709} 2710 2711SourceLocation ASTUnit::getLocation(const FileEntry *File, 2712 unsigned Line, unsigned Col) const { 2713 const SourceManager &SM = getSourceManager(); 2714 SourceLocation Loc = SM.translateFileLineCol(File, Line, Col); 2715 return SM.getMacroArgExpandedLocation(Loc); 2716} 2717 2718SourceLocation ASTUnit::getLocation(const FileEntry *File, 2719 unsigned Offset) const { 2720 const SourceManager &SM = getSourceManager(); 2721 SourceLocation FileLoc = SM.translateFileLineCol(File, 1, 1); 2722 return SM.getMacroArgExpandedLocation(FileLoc.getLocWithOffset(Offset)); 2723} 2724 2725/// \brief If \arg Loc is a loaded location from the preamble, returns 2726/// the corresponding local location of the main file, otherwise it returns 2727/// \arg Loc. 2728SourceLocation ASTUnit::mapLocationFromPreamble(SourceLocation Loc) { 2729 FileID PreambleID; 2730 if (SourceMgr) 2731 PreambleID = SourceMgr->getPreambleFileID(); 2732 2733 if (Loc.isInvalid() || Preamble.empty() || PreambleID.isInvalid()) 2734 return Loc; 2735 2736 unsigned Offs; 2737 if (SourceMgr->isInFileID(Loc, PreambleID, &Offs) && Offs < Preamble.size()) { 2738 SourceLocation FileLoc 2739 = SourceMgr->getLocForStartOfFile(SourceMgr->getMainFileID()); 2740 return FileLoc.getLocWithOffset(Offs); 2741 } 2742 2743 return Loc; 2744} 2745 2746/// \brief If \arg Loc is a local location of the main file but inside the 2747/// preamble chunk, returns the corresponding loaded location from the 2748/// preamble, otherwise it returns \arg Loc. 2749SourceLocation ASTUnit::mapLocationToPreamble(SourceLocation Loc) { 2750 FileID PreambleID; 2751 if (SourceMgr) 2752 PreambleID = SourceMgr->getPreambleFileID(); 2753 2754 if (Loc.isInvalid() || Preamble.empty() || PreambleID.isInvalid()) 2755 return Loc; 2756 2757 unsigned Offs; 2758 if (SourceMgr->isInFileID(Loc, SourceMgr->getMainFileID(), &Offs) && 2759 Offs < Preamble.size()) { 2760 SourceLocation FileLoc = SourceMgr->getLocForStartOfFile(PreambleID); 2761 return FileLoc.getLocWithOffset(Offs); 2762 } 2763 2764 return Loc; 2765} 2766 2767bool ASTUnit::isInPreambleFileID(SourceLocation Loc) { 2768 FileID FID; 2769 if (SourceMgr) 2770 FID = SourceMgr->getPreambleFileID(); 2771 2772 if (Loc.isInvalid() || FID.isInvalid()) 2773 return false; 2774 2775 return SourceMgr->isInFileID(Loc, FID); 2776} 2777 2778bool ASTUnit::isInMainFileID(SourceLocation Loc) { 2779 FileID FID; 2780 if (SourceMgr) 2781 FID = SourceMgr->getMainFileID(); 2782 2783 if (Loc.isInvalid() || FID.isInvalid()) 2784 return false; 2785 2786 return SourceMgr->isInFileID(Loc, FID); 2787} 2788 2789SourceLocation ASTUnit::getEndOfPreambleFileID() { 2790 FileID FID; 2791 if (SourceMgr) 2792 FID = SourceMgr->getPreambleFileID(); 2793 2794 if (FID.isInvalid()) 2795 return SourceLocation(); 2796 2797 return SourceMgr->getLocForEndOfFile(FID); 2798} 2799 2800SourceLocation ASTUnit::getStartOfMainFileID() { 2801 FileID FID; 2802 if (SourceMgr) 2803 FID = SourceMgr->getMainFileID(); 2804 2805 if (FID.isInvalid()) 2806 return SourceLocation(); 2807 2808 return SourceMgr->getLocForStartOfFile(FID); 2809} 2810 2811std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator> 2812ASTUnit::getLocalPreprocessingEntities() const { 2813 if (isMainFileAST()) { 2814 serialization::ModuleFile & 2815 Mod = Reader->getModuleManager().getPrimaryModule(); 2816 return Reader->getModulePreprocessedEntities(Mod); 2817 } 2818 2819 if (PreprocessingRecord *PPRec = PP->getPreprocessingRecord()) 2820 return std::make_pair(PPRec->local_begin(), PPRec->local_end()); 2821 2822 return std::make_pair(PreprocessingRecord::iterator(), 2823 PreprocessingRecord::iterator()); 2824} 2825 2826bool ASTUnit::visitLocalTopLevelDecls(void *context, DeclVisitorFn Fn) { 2827 if (isMainFileAST()) { 2828 serialization::ModuleFile & 2829 Mod = Reader->getModuleManager().getPrimaryModule(); 2830 ASTReader::ModuleDeclIterator MDI, MDE; 2831 llvm::tie(MDI, MDE) = Reader->getModuleFileLevelDecls(Mod); 2832 for (; MDI != MDE; ++MDI) { 2833 if (!Fn(context, *MDI)) 2834 return false; 2835 } 2836 2837 return true; 2838 } 2839 2840 for (ASTUnit::top_level_iterator TL = top_level_begin(), 2841 TLEnd = top_level_end(); 2842 TL != TLEnd; ++TL) { 2843 if (!Fn(context, *TL)) 2844 return false; 2845 } 2846 2847 return true; 2848} 2849 2850namespace { 2851struct PCHLocatorInfo { 2852 serialization::ModuleFile *Mod; 2853 PCHLocatorInfo() : Mod(0) {} 2854}; 2855} 2856 2857static bool PCHLocator(serialization::ModuleFile &M, void *UserData) { 2858 PCHLocatorInfo &Info = *static_cast<PCHLocatorInfo*>(UserData); 2859 switch (M.Kind) { 2860 case serialization::MK_Module: 2861 return true; // skip dependencies. 2862 case serialization::MK_PCH: 2863 Info.Mod = &M; 2864 return true; // found it. 2865 case serialization::MK_Preamble: 2866 return false; // look in dependencies. 2867 case serialization::MK_MainFile: 2868 return false; // look in dependencies. 2869 } 2870 2871 return true; 2872} 2873 2874const FileEntry *ASTUnit::getPCHFile() { 2875 if (!Reader) 2876 return 0; 2877 2878 PCHLocatorInfo Info; 2879 Reader->getModuleManager().visit(PCHLocator, &Info); 2880 if (Info.Mod) 2881 return Info.Mod->File; 2882 2883 return 0; 2884} 2885 2886bool ASTUnit::isModuleFile() { 2887 return isMainFileAST() && !ASTFileLangOpts.CurrentModule.empty(); 2888} 2889 2890void ASTUnit::PreambleData::countLines() const { 2891 NumLines = 0; 2892 if (empty()) 2893 return; 2894 2895 for (std::vector<char>::const_iterator 2896 I = Buffer.begin(), E = Buffer.end(); I != E; ++I) { 2897 if (*I == '\n') 2898 ++NumLines; 2899 } 2900 if (Buffer.back() != '\n') 2901 ++NumLines; 2902} 2903 2904#ifndef NDEBUG 2905ASTUnit::ConcurrencyState::ConcurrencyState() { 2906 Mutex = new llvm::sys::MutexImpl(/*recursive=*/true); 2907} 2908 2909ASTUnit::ConcurrencyState::~ConcurrencyState() { 2910 delete static_cast<llvm::sys::MutexImpl *>(Mutex); 2911} 2912 2913void ASTUnit::ConcurrencyState::start() { 2914 bool acquired = static_cast<llvm::sys::MutexImpl *>(Mutex)->tryacquire(); 2915 assert(acquired && "Concurrent access to ASTUnit!"); 2916} 2917 2918void ASTUnit::ConcurrencyState::finish() { 2919 static_cast<llvm::sys::MutexImpl *>(Mutex)->release(); 2920} 2921 2922#else // NDEBUG 2923 2924ASTUnit::ConcurrencyState::ConcurrencyState() {} 2925ASTUnit::ConcurrencyState::~ConcurrencyState() {} 2926void ASTUnit::ConcurrencyState::start() {} 2927void ASTUnit::ConcurrencyState::finish() {} 2928 2929#endif 2930