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