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