ASTUnit.cpp revision 32be4a588fbb87d0d163ead49c42f5438bf0b2b7
1//===--- ASTUnit.cpp - ASTUnit utility ------------------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// ASTUnit Implementation. 11// 12//===----------------------------------------------------------------------===// 13 14#include "clang/Frontend/ASTUnit.h" 15#include "clang/AST/ASTContext.h" 16#include "clang/AST/ASTConsumer.h" 17#include "clang/AST/DeclVisitor.h" 18#include "clang/AST/TypeOrdering.h" 19#include "clang/AST/StmtVisitor.h" 20#include "clang/Driver/Compilation.h" 21#include "clang/Driver/Driver.h" 22#include "clang/Driver/Job.h" 23#include "clang/Driver/Tool.h" 24#include "clang/Frontend/CompilerInstance.h" 25#include "clang/Frontend/FrontendActions.h" 26#include "clang/Frontend/FrontendDiagnostic.h" 27#include "clang/Frontend/FrontendOptions.h" 28#include "clang/Frontend/Utils.h" 29#include "clang/Serialization/ASTReader.h" 30#include "clang/Serialization/ASTWriter.h" 31#include "clang/Lex/HeaderSearch.h" 32#include "clang/Lex/Preprocessor.h" 33#include "clang/Basic/TargetOptions.h" 34#include "clang/Basic/TargetInfo.h" 35#include "clang/Basic/Diagnostic.h" 36#include "llvm/ADT/StringSet.h" 37#include "llvm/Support/MemoryBuffer.h" 38#include "llvm/System/Host.h" 39#include "llvm/System/Path.h" 40#include "llvm/Support/raw_ostream.h" 41#include "llvm/Support/Timer.h" 42#include <cstdlib> 43#include <cstdio> 44#include <sys/stat.h> 45using namespace clang; 46 47/// \brief After failing to build a precompiled preamble (due to 48/// errors in the source that occurs in the preamble), the number of 49/// reparses during which we'll skip even trying to precompile the 50/// preamble. 51const unsigned DefaultPreambleRebuildInterval = 5; 52 53ASTUnit::ASTUnit(bool _MainFileIsAST) 54 : CaptureDiagnostics(false), MainFileIsAST(_MainFileIsAST), 55 CompleteTranslationUnit(true), ConcurrencyCheckValue(CheckUnlocked), 56 PreambleRebuildCounter(0), SavedMainFileBuffer(0), PreambleBuffer(0), 57 ShouldCacheCodeCompletionResults(false), 58 NumTopLevelDeclsAtLastCompletionCache(0), 59 CacheCodeCompletionCoolDown(0), 60 UnsafeToFree(false) { 61} 62 63ASTUnit::~ASTUnit() { 64 ConcurrencyCheckValue = CheckLocked; 65 CleanTemporaryFiles(); 66 if (!PreambleFile.empty()) 67 llvm::sys::Path(PreambleFile).eraseFromDisk(); 68 69 // Free the buffers associated with remapped files. We are required to 70 // perform this operation here because we explicitly request that the 71 // compiler instance *not* free these buffers for each invocation of the 72 // parser. 73 if (Invocation.get()) { 74 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts(); 75 for (PreprocessorOptions::remapped_file_buffer_iterator 76 FB = PPOpts.remapped_file_buffer_begin(), 77 FBEnd = PPOpts.remapped_file_buffer_end(); 78 FB != FBEnd; 79 ++FB) 80 delete FB->second; 81 } 82 83 delete SavedMainFileBuffer; 84 delete PreambleBuffer; 85 86 ClearCachedCompletionResults(); 87 88 if (TimerGroup) 89 TimerGroup->printAll(llvm::errs()); 90 91 for (unsigned I = 0, N = Timers.size(); I != N; ++I) 92 delete Timers[I]; 93} 94 95void ASTUnit::CleanTemporaryFiles() { 96 for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I) 97 TemporaryFiles[I].eraseFromDisk(); 98 TemporaryFiles.clear(); 99} 100 101/// \brief Determine the set of code-completion contexts in which this 102/// declaration should be shown. 103static unsigned getDeclShowContexts(NamedDecl *ND, 104 const LangOptions &LangOpts, 105 bool &IsNestedNameSpecifier) { 106 IsNestedNameSpecifier = false; 107 108 if (isa<UsingShadowDecl>(ND)) 109 ND = dyn_cast<NamedDecl>(ND->getUnderlyingDecl()); 110 if (!ND) 111 return 0; 112 113 unsigned Contexts = 0; 114 if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND) || 115 isa<ClassTemplateDecl>(ND) || isa<TemplateTemplateParmDecl>(ND)) { 116 // Types can appear in these contexts. 117 if (LangOpts.CPlusPlus || !isa<TagDecl>(ND)) 118 Contexts |= (1 << (CodeCompletionContext::CCC_TopLevel - 1)) 119 | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1)) 120 | (1 << (CodeCompletionContext::CCC_ClassStructUnion - 1)) 121 | (1 << (CodeCompletionContext::CCC_Statement - 1)) 122 | (1 << (CodeCompletionContext::CCC_Type - 1)) 123 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1)); 124 125 // In C++, types can appear in expressions contexts (for functional casts). 126 if (LangOpts.CPlusPlus) 127 Contexts |= (1 << (CodeCompletionContext::CCC_Expression - 1)); 128 129 // In Objective-C, message sends can send interfaces. In Objective-C++, 130 // all types are available due to functional casts. 131 if (LangOpts.CPlusPlus || isa<ObjCInterfaceDecl>(ND)) 132 Contexts |= (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1)); 133 134 // Deal with tag names. 135 if (isa<EnumDecl>(ND)) { 136 Contexts |= (1 << (CodeCompletionContext::CCC_EnumTag - 1)); 137 138 // Part of the nested-name-specifier in C++0x. 139 if (LangOpts.CPlusPlus0x) 140 IsNestedNameSpecifier = true; 141 } else if (RecordDecl *Record = dyn_cast<RecordDecl>(ND)) { 142 if (Record->isUnion()) 143 Contexts |= (1 << (CodeCompletionContext::CCC_UnionTag - 1)); 144 else 145 Contexts |= (1 << (CodeCompletionContext::CCC_ClassOrStructTag - 1)); 146 147 if (LangOpts.CPlusPlus) 148 IsNestedNameSpecifier = true; 149 } else if (isa<ClassTemplateDecl>(ND)) 150 IsNestedNameSpecifier = true; 151 } else if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) { 152 // Values can appear in these contexts. 153 Contexts = (1 << (CodeCompletionContext::CCC_Statement - 1)) 154 | (1 << (CodeCompletionContext::CCC_Expression - 1)) 155 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1)) 156 | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1)); 157 } else if (isa<ObjCProtocolDecl>(ND)) { 158 Contexts = (1 << (CodeCompletionContext::CCC_ObjCProtocolName - 1)); 159 } else if (isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) { 160 Contexts = (1 << (CodeCompletionContext::CCC_Namespace - 1)); 161 162 // Part of the nested-name-specifier. 163 IsNestedNameSpecifier = true; 164 } 165 166 return Contexts; 167} 168 169void ASTUnit::CacheCodeCompletionResults() { 170 if (!TheSema) 171 return; 172 173 llvm::Timer *CachingTimer = 0; 174 if (TimerGroup.get()) { 175 CachingTimer = new llvm::Timer("Cache global code completions", 176 *TimerGroup); 177 CachingTimer->startTimer(); 178 Timers.push_back(CachingTimer); 179 } 180 181 // Clear out the previous results. 182 ClearCachedCompletionResults(); 183 184 // Gather the set of global code completions. 185 typedef CodeCompletionResult Result; 186 llvm::SmallVector<Result, 8> Results; 187 TheSema->GatherGlobalCodeCompletions(Results); 188 189 // Translate global code completions into cached completions. 190 llvm::DenseMap<CanQualType, unsigned> CompletionTypes; 191 192 for (unsigned I = 0, N = Results.size(); I != N; ++I) { 193 switch (Results[I].Kind) { 194 case Result::RK_Declaration: { 195 bool IsNestedNameSpecifier = false; 196 CachedCodeCompletionResult CachedResult; 197 CachedResult.Completion = Results[I].CreateCodeCompletionString(*TheSema); 198 CachedResult.ShowInContexts = getDeclShowContexts(Results[I].Declaration, 199 Ctx->getLangOptions(), 200 IsNestedNameSpecifier); 201 CachedResult.Priority = Results[I].Priority; 202 CachedResult.Kind = Results[I].CursorKind; 203 CachedResult.Availability = Results[I].Availability; 204 205 // Keep track of the type of this completion in an ASTContext-agnostic 206 // way. 207 QualType UsageType = getDeclUsageType(*Ctx, Results[I].Declaration); 208 if (UsageType.isNull()) { 209 CachedResult.TypeClass = STC_Void; 210 CachedResult.Type = 0; 211 } else { 212 CanQualType CanUsageType 213 = Ctx->getCanonicalType(UsageType.getUnqualifiedType()); 214 CachedResult.TypeClass = getSimplifiedTypeClass(CanUsageType); 215 216 // Determine whether we have already seen this type. If so, we save 217 // ourselves the work of formatting the type string by using the 218 // temporary, CanQualType-based hash table to find the associated value. 219 unsigned &TypeValue = CompletionTypes[CanUsageType]; 220 if (TypeValue == 0) { 221 TypeValue = CompletionTypes.size(); 222 CachedCompletionTypes[QualType(CanUsageType).getAsString()] 223 = TypeValue; 224 } 225 226 CachedResult.Type = TypeValue; 227 } 228 229 CachedCompletionResults.push_back(CachedResult); 230 231 /// Handle nested-name-specifiers in C++. 232 if (TheSema->Context.getLangOptions().CPlusPlus && 233 IsNestedNameSpecifier && !Results[I].StartsNestedNameSpecifier) { 234 // The contexts in which a nested-name-specifier can appear in C++. 235 unsigned NNSContexts 236 = (1 << (CodeCompletionContext::CCC_TopLevel - 1)) 237 | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1)) 238 | (1 << (CodeCompletionContext::CCC_ClassStructUnion - 1)) 239 | (1 << (CodeCompletionContext::CCC_Statement - 1)) 240 | (1 << (CodeCompletionContext::CCC_Expression - 1)) 241 | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1)) 242 | (1 << (CodeCompletionContext::CCC_EnumTag - 1)) 243 | (1 << (CodeCompletionContext::CCC_UnionTag - 1)) 244 | (1 << (CodeCompletionContext::CCC_ClassOrStructTag - 1)) 245 | (1 << (CodeCompletionContext::CCC_Type - 1)) 246 | (1 << (CodeCompletionContext::CCC_PotentiallyQualifiedName - 1)) 247 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1)); 248 249 if (isa<NamespaceDecl>(Results[I].Declaration) || 250 isa<NamespaceAliasDecl>(Results[I].Declaration)) 251 NNSContexts |= (1 << (CodeCompletionContext::CCC_Namespace - 1)); 252 253 if (unsigned RemainingContexts 254 = NNSContexts & ~CachedResult.ShowInContexts) { 255 // If there any contexts where this completion can be a 256 // nested-name-specifier but isn't already an option, create a 257 // nested-name-specifier completion. 258 Results[I].StartsNestedNameSpecifier = true; 259 CachedResult.Completion = Results[I].CreateCodeCompletionString(*TheSema); 260 CachedResult.ShowInContexts = RemainingContexts; 261 CachedResult.Priority = CCP_NestedNameSpecifier; 262 CachedResult.TypeClass = STC_Void; 263 CachedResult.Type = 0; 264 CachedCompletionResults.push_back(CachedResult); 265 } 266 } 267 break; 268 } 269 270 case Result::RK_Keyword: 271 case Result::RK_Pattern: 272 // Ignore keywords and patterns; we don't care, since they are so 273 // easily regenerated. 274 break; 275 276 case Result::RK_Macro: { 277 CachedCodeCompletionResult CachedResult; 278 CachedResult.Completion = Results[I].CreateCodeCompletionString(*TheSema); 279 CachedResult.ShowInContexts 280 = (1 << (CodeCompletionContext::CCC_TopLevel - 1)) 281 | (1 << (CodeCompletionContext::CCC_ObjCInterface - 1)) 282 | (1 << (CodeCompletionContext::CCC_ObjCImplementation - 1)) 283 | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1)) 284 | (1 << (CodeCompletionContext::CCC_ClassStructUnion - 1)) 285 | (1 << (CodeCompletionContext::CCC_Statement - 1)) 286 | (1 << (CodeCompletionContext::CCC_Expression - 1)) 287 | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1)) 288 | (1 << (CodeCompletionContext::CCC_MacroNameUse - 1)) 289 | (1 << (CodeCompletionContext::CCC_PreprocessorExpression - 1)) 290 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1)); 291 292 293 CachedResult.Priority = Results[I].Priority; 294 CachedResult.Kind = Results[I].CursorKind; 295 CachedResult.Availability = Results[I].Availability; 296 CachedResult.TypeClass = STC_Void; 297 CachedResult.Type = 0; 298 CachedCompletionResults.push_back(CachedResult); 299 break; 300 } 301 } 302 Results[I].Destroy(); 303 } 304 305 if (CachingTimer) 306 CachingTimer->stopTimer(); 307 308 // Make a note of the state when we performed this caching. 309 NumTopLevelDeclsAtLastCompletionCache = top_level_size(); 310 CacheCodeCompletionCoolDown = 15; 311} 312 313void ASTUnit::ClearCachedCompletionResults() { 314 for (unsigned I = 0, N = CachedCompletionResults.size(); I != N; ++I) 315 delete CachedCompletionResults[I].Completion; 316 CachedCompletionResults.clear(); 317 CachedCompletionTypes.clear(); 318} 319 320namespace { 321 322/// \brief Gathers information from ASTReader that will be used to initialize 323/// a Preprocessor. 324class ASTInfoCollector : public ASTReaderListener { 325 LangOptions &LangOpt; 326 HeaderSearch &HSI; 327 std::string &TargetTriple; 328 std::string &Predefines; 329 unsigned &Counter; 330 331 unsigned NumHeaderInfos; 332 333public: 334 ASTInfoCollector(LangOptions &LangOpt, HeaderSearch &HSI, 335 std::string &TargetTriple, std::string &Predefines, 336 unsigned &Counter) 337 : LangOpt(LangOpt), HSI(HSI), TargetTriple(TargetTriple), 338 Predefines(Predefines), Counter(Counter), NumHeaderInfos(0) {} 339 340 virtual bool ReadLanguageOptions(const LangOptions &LangOpts) { 341 LangOpt = LangOpts; 342 return false; 343 } 344 345 virtual bool ReadTargetTriple(llvm::StringRef Triple) { 346 TargetTriple = Triple; 347 return false; 348 } 349 350 virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers, 351 llvm::StringRef OriginalFileName, 352 std::string &SuggestedPredefines) { 353 Predefines = Buffers[0].Data; 354 for (unsigned I = 1, N = Buffers.size(); I != N; ++I) { 355 Predefines += Buffers[I].Data; 356 } 357 return false; 358 } 359 360 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID) { 361 HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++); 362 } 363 364 virtual void ReadCounter(unsigned Value) { 365 Counter = Value; 366 } 367}; 368 369class StoredDiagnosticClient : public DiagnosticClient { 370 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags; 371 372public: 373 explicit StoredDiagnosticClient( 374 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags) 375 : StoredDiags(StoredDiags) { } 376 377 virtual void HandleDiagnostic(Diagnostic::Level Level, 378 const DiagnosticInfo &Info); 379}; 380 381/// \brief RAII object that optionally captures diagnostics, if 382/// there is no diagnostic client to capture them already. 383class CaptureDroppedDiagnostics { 384 Diagnostic &Diags; 385 StoredDiagnosticClient Client; 386 DiagnosticClient *PreviousClient; 387 388public: 389 CaptureDroppedDiagnostics(bool RequestCapture, Diagnostic &Diags, 390 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags) 391 : Diags(Diags), Client(StoredDiags), PreviousClient(0) 392 { 393 if (RequestCapture || Diags.getClient() == 0) { 394 PreviousClient = Diags.takeClient(); 395 Diags.setClient(&Client); 396 } 397 } 398 399 ~CaptureDroppedDiagnostics() { 400 if (Diags.getClient() == &Client) { 401 Diags.takeClient(); 402 Diags.setClient(PreviousClient); 403 } 404 } 405}; 406 407} // anonymous namespace 408 409void StoredDiagnosticClient::HandleDiagnostic(Diagnostic::Level Level, 410 const DiagnosticInfo &Info) { 411 StoredDiags.push_back(StoredDiagnostic(Level, Info)); 412} 413 414const std::string &ASTUnit::getOriginalSourceFileName() { 415 return OriginalSourceFile; 416} 417 418const std::string &ASTUnit::getASTFileName() { 419 assert(isMainFileAST() && "Not an ASTUnit from an AST file!"); 420 return static_cast<ASTReader *>(Ctx->getExternalSource())->getFileName(); 421} 422 423ASTUnit *ASTUnit::LoadFromASTFile(const std::string &Filename, 424 llvm::IntrusiveRefCntPtr<Diagnostic> Diags, 425 bool OnlyLocalDecls, 426 RemappedFile *RemappedFiles, 427 unsigned NumRemappedFiles, 428 bool CaptureDiagnostics) { 429 llvm::OwningPtr<ASTUnit> AST(new ASTUnit(true)); 430 431 if (!Diags.getPtr()) { 432 // No diagnostics engine was provided, so create our own diagnostics object 433 // with the default options. 434 DiagnosticOptions DiagOpts; 435 Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0); 436 } 437 438 AST->CaptureDiagnostics = CaptureDiagnostics; 439 AST->OnlyLocalDecls = OnlyLocalDecls; 440 AST->Diagnostics = Diags; 441 AST->FileMgr.reset(new FileManager); 442 AST->SourceMgr.reset(new SourceManager(AST->getDiagnostics())); 443 AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager())); 444 445 // If requested, capture diagnostics in the ASTUnit. 446 CaptureDroppedDiagnostics Capture(CaptureDiagnostics, AST->getDiagnostics(), 447 AST->StoredDiagnostics); 448 449 for (unsigned I = 0; I != NumRemappedFiles; ++I) { 450 // Create the file entry for the file that we're mapping from. 451 const FileEntry *FromFile 452 = AST->getFileManager().getVirtualFile(RemappedFiles[I].first, 453 RemappedFiles[I].second->getBufferSize(), 454 0); 455 if (!FromFile) { 456 AST->getDiagnostics().Report(diag::err_fe_remap_missing_from_file) 457 << RemappedFiles[I].first; 458 delete RemappedFiles[I].second; 459 continue; 460 } 461 462 // Override the contents of the "from" file with the contents of 463 // the "to" file. 464 AST->getSourceManager().overrideFileContents(FromFile, 465 RemappedFiles[I].second); 466 } 467 468 // Gather Info for preprocessor construction later on. 469 470 LangOptions LangInfo; 471 HeaderSearch &HeaderInfo = *AST->HeaderInfo.get(); 472 std::string TargetTriple; 473 std::string Predefines; 474 unsigned Counter; 475 476 llvm::OwningPtr<ASTReader> Reader; 477 478 Reader.reset(new ASTReader(AST->getSourceManager(), AST->getFileManager(), 479 AST->getDiagnostics())); 480 Reader->setListener(new ASTInfoCollector(LangInfo, HeaderInfo, TargetTriple, 481 Predefines, Counter)); 482 483 switch (Reader->ReadAST(Filename, ASTReader::MainFile)) { 484 case ASTReader::Success: 485 break; 486 487 case ASTReader::Failure: 488 case ASTReader::IgnorePCH: 489 AST->getDiagnostics().Report(diag::err_fe_unable_to_load_pch); 490 return NULL; 491 } 492 493 AST->OriginalSourceFile = Reader->getOriginalSourceFile(); 494 495 // AST file loaded successfully. Now create the preprocessor. 496 497 // Get information about the target being compiled for. 498 // 499 // FIXME: This is broken, we should store the TargetOptions in the AST file. 500 TargetOptions TargetOpts; 501 TargetOpts.ABI = ""; 502 TargetOpts.CXXABI = ""; 503 TargetOpts.CPU = ""; 504 TargetOpts.Features.clear(); 505 TargetOpts.Triple = TargetTriple; 506 AST->Target.reset(TargetInfo::CreateTargetInfo(AST->getDiagnostics(), 507 TargetOpts)); 508 AST->PP.reset(new Preprocessor(AST->getDiagnostics(), LangInfo, 509 *AST->Target.get(), 510 AST->getSourceManager(), HeaderInfo)); 511 Preprocessor &PP = *AST->PP.get(); 512 513 PP.setPredefines(Reader->getSuggestedPredefines()); 514 PP.setCounterValue(Counter); 515 Reader->setPreprocessor(PP); 516 517 // Create and initialize the ASTContext. 518 519 AST->Ctx.reset(new ASTContext(LangInfo, 520 AST->getSourceManager(), 521 *AST->Target.get(), 522 PP.getIdentifierTable(), 523 PP.getSelectorTable(), 524 PP.getBuiltinInfo(), 525 /* size_reserve = */0)); 526 ASTContext &Context = *AST->Ctx.get(); 527 528 Reader->InitializeContext(Context); 529 530 // Attach the AST reader to the AST context as an external AST 531 // source, so that declarations will be deserialized from the 532 // AST file as needed. 533 ASTReader *ReaderPtr = Reader.get(); 534 llvm::OwningPtr<ExternalASTSource> Source(Reader.take()); 535 Context.setExternalSource(Source); 536 537 // Create an AST consumer, even though it isn't used. 538 AST->Consumer.reset(new ASTConsumer); 539 540 // Create a semantic analysis object and tell the AST reader about it. 541 AST->TheSema.reset(new Sema(PP, Context, *AST->Consumer)); 542 AST->TheSema->Initialize(); 543 ReaderPtr->InitializeSema(*AST->TheSema); 544 545 return AST.take(); 546} 547 548namespace { 549 550class TopLevelDeclTrackerConsumer : public ASTConsumer { 551 ASTUnit &Unit; 552 553public: 554 TopLevelDeclTrackerConsumer(ASTUnit &_Unit) : Unit(_Unit) {} 555 556 void HandleTopLevelDecl(DeclGroupRef D) { 557 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) { 558 Decl *D = *it; 559 // FIXME: Currently ObjC method declarations are incorrectly being 560 // reported as top-level declarations, even though their DeclContext 561 // is the containing ObjC @interface/@implementation. This is a 562 // fundamental problem in the parser right now. 563 if (isa<ObjCMethodDecl>(D)) 564 continue; 565 Unit.addTopLevelDecl(D); 566 } 567 } 568 569 // We're not interested in "interesting" decls. 570 void HandleInterestingDecl(DeclGroupRef) {} 571}; 572 573class TopLevelDeclTrackerAction : public ASTFrontendAction { 574public: 575 ASTUnit &Unit; 576 577 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, 578 llvm::StringRef InFile) { 579 return new TopLevelDeclTrackerConsumer(Unit); 580 } 581 582public: 583 TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {} 584 585 virtual bool hasCodeCompletionSupport() const { return false; } 586 virtual bool usesCompleteTranslationUnit() { 587 return Unit.isCompleteTranslationUnit(); 588 } 589}; 590 591class PrecompilePreambleConsumer : public PCHGenerator { 592 ASTUnit &Unit; 593 std::vector<Decl *> TopLevelDecls; 594 595public: 596 PrecompilePreambleConsumer(ASTUnit &Unit, 597 const Preprocessor &PP, bool Chaining, 598 const char *isysroot, llvm::raw_ostream *Out) 599 : PCHGenerator(PP, Chaining, isysroot, Out), Unit(Unit) { } 600 601 virtual void HandleTopLevelDecl(DeclGroupRef D) { 602 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) { 603 Decl *D = *it; 604 // FIXME: Currently ObjC method declarations are incorrectly being 605 // reported as top-level declarations, even though their DeclContext 606 // is the containing ObjC @interface/@implementation. This is a 607 // fundamental problem in the parser right now. 608 if (isa<ObjCMethodDecl>(D)) 609 continue; 610 TopLevelDecls.push_back(D); 611 } 612 } 613 614 virtual void HandleTranslationUnit(ASTContext &Ctx) { 615 PCHGenerator::HandleTranslationUnit(Ctx); 616 if (!Unit.getDiagnostics().hasErrorOccurred()) { 617 // Translate the top-level declarations we captured during 618 // parsing into declaration IDs in the precompiled 619 // preamble. This will allow us to deserialize those top-level 620 // declarations when requested. 621 for (unsigned I = 0, N = TopLevelDecls.size(); I != N; ++I) 622 Unit.addTopLevelDeclFromPreamble( 623 getWriter().getDeclID(TopLevelDecls[I])); 624 } 625 } 626}; 627 628class PrecompilePreambleAction : public ASTFrontendAction { 629 ASTUnit &Unit; 630 631public: 632 explicit PrecompilePreambleAction(ASTUnit &Unit) : Unit(Unit) {} 633 634 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, 635 llvm::StringRef InFile) { 636 std::string Sysroot; 637 llvm::raw_ostream *OS = 0; 638 bool Chaining; 639 if (GeneratePCHAction::ComputeASTConsumerArguments(CI, InFile, Sysroot, 640 OS, Chaining)) 641 return 0; 642 643 const char *isysroot = CI.getFrontendOpts().RelocatablePCH ? 644 Sysroot.c_str() : 0; 645 return new PrecompilePreambleConsumer(Unit, CI.getPreprocessor(), Chaining, 646 isysroot, OS); 647 } 648 649 virtual bool hasCodeCompletionSupport() const { return false; } 650 virtual bool hasASTFileSupport() const { return false; } 651 virtual bool usesCompleteTranslationUnit() { return false; } 652}; 653 654} 655 656/// Parse the source file into a translation unit using the given compiler 657/// invocation, replacing the current translation unit. 658/// 659/// \returns True if a failure occurred that causes the ASTUnit not to 660/// contain any translation-unit information, false otherwise. 661bool ASTUnit::Parse(llvm::MemoryBuffer *OverrideMainBuffer) { 662 delete SavedMainFileBuffer; 663 SavedMainFileBuffer = 0; 664 665 if (!Invocation.get()) { 666 delete OverrideMainBuffer; 667 return true; 668 } 669 670 // Create the compiler instance to use for building the AST. 671 CompilerInstance Clang; 672 Clang.setInvocation(Invocation.take()); 673 OriginalSourceFile = Clang.getFrontendOpts().Inputs[0].second; 674 675 // Set up diagnostics, capturing any diagnostics that would 676 // otherwise be dropped. 677 Clang.setDiagnostics(&getDiagnostics()); 678 CaptureDroppedDiagnostics Capture(CaptureDiagnostics, 679 getDiagnostics(), 680 StoredDiagnostics); 681 682 // Create the target instance. 683 Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(), 684 Clang.getTargetOpts())); 685 if (!Clang.hasTarget()) { 686 delete OverrideMainBuffer; 687 return true; 688 } 689 690 // Inform the target of the language options. 691 // 692 // FIXME: We shouldn't need to do this, the target should be immutable once 693 // created. This complexity should be lifted elsewhere. 694 Clang.getTarget().setForcedLangOptions(Clang.getLangOpts()); 695 696 assert(Clang.getFrontendOpts().Inputs.size() == 1 && 697 "Invocation must have exactly one source file!"); 698 assert(Clang.getFrontendOpts().Inputs[0].first != IK_AST && 699 "FIXME: AST inputs not yet supported here!"); 700 assert(Clang.getFrontendOpts().Inputs[0].first != IK_LLVM_IR && 701 "IR inputs not support here!"); 702 703 // Configure the various subsystems. 704 // FIXME: Should we retain the previous file manager? 705 FileMgr.reset(new FileManager); 706 SourceMgr.reset(new SourceManager(getDiagnostics())); 707 TheSema.reset(); 708 Ctx.reset(); 709 PP.reset(); 710 711 // Clear out old caches and data. 712 TopLevelDecls.clear(); 713 CleanTemporaryFiles(); 714 PreprocessedEntitiesByFile.clear(); 715 716 if (!OverrideMainBuffer) { 717 StoredDiagnostics.clear(); 718 TopLevelDeclsInPreamble.clear(); 719 } 720 721 // Create a file manager object to provide access to and cache the filesystem. 722 Clang.setFileManager(&getFileManager()); 723 724 // Create the source manager. 725 Clang.setSourceManager(&getSourceManager()); 726 727 // If the main file has been overridden due to the use of a preamble, 728 // make that override happen and introduce the preamble. 729 PreprocessorOptions &PreprocessorOpts = Clang.getPreprocessorOpts(); 730 std::string PriorImplicitPCHInclude; 731 if (OverrideMainBuffer) { 732 PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer); 733 PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size(); 734 PreprocessorOpts.PrecompiledPreambleBytes.second 735 = PreambleEndsAtStartOfLine; 736 PriorImplicitPCHInclude = PreprocessorOpts.ImplicitPCHInclude; 737 PreprocessorOpts.ImplicitPCHInclude = PreambleFile; 738 PreprocessorOpts.DisablePCHValidation = true; 739 740 // Keep track of the override buffer; 741 SavedMainFileBuffer = OverrideMainBuffer; 742 743 // The stored diagnostic has the old source manager in it; update 744 // the locations to refer into the new source manager. Since we've 745 // been careful to make sure that the source manager's state 746 // before and after are identical, so that we can reuse the source 747 // location itself. 748 for (unsigned I = 0, N = StoredDiagnostics.size(); I != N; ++I) { 749 FullSourceLoc Loc(StoredDiagnostics[I].getLocation(), 750 getSourceManager()); 751 StoredDiagnostics[I].setLocation(Loc); 752 } 753 } else { 754 PreprocessorOpts.PrecompiledPreambleBytes.first = 0; 755 PreprocessorOpts.PrecompiledPreambleBytes.second = false; 756 } 757 758 llvm::OwningPtr<TopLevelDeclTrackerAction> Act; 759 Act.reset(new TopLevelDeclTrackerAction(*this)); 760 if (!Act->BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second, 761 Clang.getFrontendOpts().Inputs[0].first)) 762 goto error; 763 764 Act->Execute(); 765 766 // Steal the created target, context, and preprocessor, and take back the 767 // source and file managers. 768 TheSema.reset(Clang.takeSema()); 769 Consumer.reset(Clang.takeASTConsumer()); 770 Ctx.reset(Clang.takeASTContext()); 771 PP.reset(Clang.takePreprocessor()); 772 Clang.takeSourceManager(); 773 Clang.takeFileManager(); 774 Target.reset(Clang.takeTarget()); 775 776 Act->EndSourceFile(); 777 778 // Remove the overridden buffer we used for the preamble. 779 if (OverrideMainBuffer) { 780 PreprocessorOpts.eraseRemappedFile( 781 PreprocessorOpts.remapped_file_buffer_end() - 1); 782 PreprocessorOpts.ImplicitPCHInclude = PriorImplicitPCHInclude; 783 } 784 785 Invocation.reset(Clang.takeInvocation()); 786 787 // If we were asked to cache code-completion results and don't have any 788 // results yet, do so now. 789 if (ShouldCacheCodeCompletionResults && CachedCompletionResults.empty()) 790 CacheCodeCompletionResults(); 791 792 return false; 793 794error: 795 // Remove the overridden buffer we used for the preamble. 796 if (OverrideMainBuffer) { 797 PreprocessorOpts.eraseRemappedFile( 798 PreprocessorOpts.remapped_file_buffer_end() - 1); 799 PreprocessorOpts.DisablePCHValidation = true; 800 PreprocessorOpts.ImplicitPCHInclude = PriorImplicitPCHInclude; 801 delete OverrideMainBuffer; 802 SavedMainFileBuffer = 0; 803 } 804 805 Clang.takeSourceManager(); 806 Clang.takeFileManager(); 807 Invocation.reset(Clang.takeInvocation()); 808 return true; 809} 810 811/// \brief Simple function to retrieve a path for a preamble precompiled header. 812static std::string GetPreamblePCHPath() { 813 // FIXME: This is lame; sys::Path should provide this function (in particular, 814 // it should know how to find the temporary files dir). 815 // FIXME: This is really lame. I copied this code from the Driver! 816 // FIXME: This is a hack so that we can override the preamble file during 817 // crash-recovery testing, which is the only case where the preamble files 818 // are not necessarily cleaned up. 819 const char *TmpFile = ::getenv("CINDEXTEST_PREAMBLE_FILE"); 820 if (TmpFile) 821 return TmpFile; 822 823 std::string Error; 824 const char *TmpDir = ::getenv("TMPDIR"); 825 if (!TmpDir) 826 TmpDir = ::getenv("TEMP"); 827 if (!TmpDir) 828 TmpDir = ::getenv("TMP"); 829#ifdef LLVM_ON_WIN32 830 if (!TmpDir) 831 TmpDir = ::getenv("USERPROFILE"); 832#endif 833 if (!TmpDir) 834 TmpDir = "/tmp"; 835 llvm::sys::Path P(TmpDir); 836 P.createDirectoryOnDisk(true); 837 P.appendComponent("preamble"); 838 P.appendSuffix("pch"); 839 if (P.createTemporaryFileOnDisk()) 840 return std::string(); 841 842 return P.str(); 843} 844 845/// \brief Compute the preamble for the main file, providing the source buffer 846/// that corresponds to the main file along with a pair (bytes, start-of-line) 847/// that describes the preamble. 848std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> > 849ASTUnit::ComputePreamble(CompilerInvocation &Invocation, 850 unsigned MaxLines, bool &CreatedBuffer) { 851 FrontendOptions &FrontendOpts = Invocation.getFrontendOpts(); 852 PreprocessorOptions &PreprocessorOpts 853 = Invocation.getPreprocessorOpts(); 854 CreatedBuffer = false; 855 856 // Try to determine if the main file has been remapped, either from the 857 // command line (to another file) or directly through the compiler invocation 858 // (to a memory buffer). 859 llvm::MemoryBuffer *Buffer = 0; 860 llvm::sys::PathWithStatus MainFilePath(FrontendOpts.Inputs[0].second); 861 if (const llvm::sys::FileStatus *MainFileStatus = MainFilePath.getFileStatus()) { 862 // Check whether there is a file-file remapping of the main file 863 for (PreprocessorOptions::remapped_file_iterator 864 M = PreprocessorOpts.remapped_file_begin(), 865 E = PreprocessorOpts.remapped_file_end(); 866 M != E; 867 ++M) { 868 llvm::sys::PathWithStatus MPath(M->first); 869 if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) { 870 if (MainFileStatus->uniqueID == MStatus->uniqueID) { 871 // We found a remapping. Try to load the resulting, remapped source. 872 if (CreatedBuffer) { 873 delete Buffer; 874 CreatedBuffer = false; 875 } 876 877 Buffer = llvm::MemoryBuffer::getFile(M->second); 878 if (!Buffer) 879 return std::make_pair((llvm::MemoryBuffer*)0, 880 std::make_pair(0, true)); 881 CreatedBuffer = true; 882 } 883 } 884 } 885 886 // Check whether there is a file-buffer remapping. It supercedes the 887 // file-file remapping. 888 for (PreprocessorOptions::remapped_file_buffer_iterator 889 M = PreprocessorOpts.remapped_file_buffer_begin(), 890 E = PreprocessorOpts.remapped_file_buffer_end(); 891 M != E; 892 ++M) { 893 llvm::sys::PathWithStatus MPath(M->first); 894 if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) { 895 if (MainFileStatus->uniqueID == MStatus->uniqueID) { 896 // We found a remapping. 897 if (CreatedBuffer) { 898 delete Buffer; 899 CreatedBuffer = false; 900 } 901 902 Buffer = const_cast<llvm::MemoryBuffer *>(M->second); 903 } 904 } 905 } 906 } 907 908 // If the main source file was not remapped, load it now. 909 if (!Buffer) { 910 Buffer = llvm::MemoryBuffer::getFile(FrontendOpts.Inputs[0].second); 911 if (!Buffer) 912 return std::make_pair((llvm::MemoryBuffer*)0, std::make_pair(0, true)); 913 914 CreatedBuffer = true; 915 } 916 917 return std::make_pair(Buffer, Lexer::ComputePreamble(Buffer, MaxLines)); 918} 919 920static llvm::MemoryBuffer *CreatePaddedMainFileBuffer(llvm::MemoryBuffer *Old, 921 bool DeleteOld, 922 unsigned NewSize, 923 llvm::StringRef NewName) { 924 llvm::MemoryBuffer *Result 925 = llvm::MemoryBuffer::getNewUninitMemBuffer(NewSize, NewName); 926 memcpy(const_cast<char*>(Result->getBufferStart()), 927 Old->getBufferStart(), Old->getBufferSize()); 928 memset(const_cast<char*>(Result->getBufferStart()) + Old->getBufferSize(), 929 ' ', NewSize - Old->getBufferSize() - 1); 930 const_cast<char*>(Result->getBufferEnd())[-1] = '\n'; 931 932 if (DeleteOld) 933 delete Old; 934 935 return Result; 936} 937 938/// \brief Attempt to build or re-use a precompiled preamble when (re-)parsing 939/// the source file. 940/// 941/// This routine will compute the preamble of the main source file. If a 942/// non-trivial preamble is found, it will precompile that preamble into a 943/// precompiled header so that the precompiled preamble can be used to reduce 944/// reparsing time. If a precompiled preamble has already been constructed, 945/// this routine will determine if it is still valid and, if so, avoid 946/// rebuilding the precompiled preamble. 947/// 948/// \param AllowRebuild When true (the default), this routine is 949/// allowed to rebuild the precompiled preamble if it is found to be 950/// out-of-date. 951/// 952/// \param MaxLines When non-zero, the maximum number of lines that 953/// can occur within the preamble. 954/// 955/// \returns If the precompiled preamble can be used, returns a newly-allocated 956/// buffer that should be used in place of the main file when doing so. 957/// Otherwise, returns a NULL pointer. 958llvm::MemoryBuffer *ASTUnit::getMainBufferWithPrecompiledPreamble( 959 CompilerInvocation PreambleInvocation, 960 bool AllowRebuild, 961 unsigned MaxLines) { 962 FrontendOptions &FrontendOpts = PreambleInvocation.getFrontendOpts(); 963 PreprocessorOptions &PreprocessorOpts 964 = PreambleInvocation.getPreprocessorOpts(); 965 966 bool CreatedPreambleBuffer = false; 967 std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> > NewPreamble 968 = ComputePreamble(PreambleInvocation, MaxLines, CreatedPreambleBuffer); 969 970 if (!NewPreamble.second.first) { 971 // We couldn't find a preamble in the main source. Clear out the current 972 // preamble, if we have one. It's obviously no good any more. 973 Preamble.clear(); 974 if (!PreambleFile.empty()) { 975 llvm::sys::Path(PreambleFile).eraseFromDisk(); 976 PreambleFile.clear(); 977 } 978 if (CreatedPreambleBuffer) 979 delete NewPreamble.first; 980 981 // The next time we actually see a preamble, precompile it. 982 PreambleRebuildCounter = 1; 983 return 0; 984 } 985 986 if (!Preamble.empty()) { 987 // We've previously computed a preamble. Check whether we have the same 988 // preamble now that we did before, and that there's enough space in 989 // the main-file buffer within the precompiled preamble to fit the 990 // new main file. 991 if (Preamble.size() == NewPreamble.second.first && 992 PreambleEndsAtStartOfLine == NewPreamble.second.second && 993 NewPreamble.first->getBufferSize() < PreambleReservedSize-2 && 994 memcmp(&Preamble[0], NewPreamble.first->getBufferStart(), 995 NewPreamble.second.first) == 0) { 996 // The preamble has not changed. We may be able to re-use the precompiled 997 // preamble. 998 999 // Check that none of the files used by the preamble have changed. 1000 bool AnyFileChanged = false; 1001 1002 // First, make a record of those files that have been overridden via 1003 // remapping or unsaved_files. 1004 llvm::StringMap<std::pair<off_t, time_t> > OverriddenFiles; 1005 for (PreprocessorOptions::remapped_file_iterator 1006 R = PreprocessorOpts.remapped_file_begin(), 1007 REnd = PreprocessorOpts.remapped_file_end(); 1008 !AnyFileChanged && R != REnd; 1009 ++R) { 1010 struct stat StatBuf; 1011 if (stat(R->second.c_str(), &StatBuf)) { 1012 // If we can't stat the file we're remapping to, assume that something 1013 // horrible happened. 1014 AnyFileChanged = true; 1015 break; 1016 } 1017 1018 OverriddenFiles[R->first] = std::make_pair(StatBuf.st_size, 1019 StatBuf.st_mtime); 1020 } 1021 for (PreprocessorOptions::remapped_file_buffer_iterator 1022 R = PreprocessorOpts.remapped_file_buffer_begin(), 1023 REnd = PreprocessorOpts.remapped_file_buffer_end(); 1024 !AnyFileChanged && R != REnd; 1025 ++R) { 1026 // FIXME: Should we actually compare the contents of file->buffer 1027 // remappings? 1028 OverriddenFiles[R->first] = std::make_pair(R->second->getBufferSize(), 1029 0); 1030 } 1031 1032 // Check whether anything has changed. 1033 for (llvm::StringMap<std::pair<off_t, time_t> >::iterator 1034 F = FilesInPreamble.begin(), FEnd = FilesInPreamble.end(); 1035 !AnyFileChanged && F != FEnd; 1036 ++F) { 1037 llvm::StringMap<std::pair<off_t, time_t> >::iterator Overridden 1038 = OverriddenFiles.find(F->first()); 1039 if (Overridden != OverriddenFiles.end()) { 1040 // This file was remapped; check whether the newly-mapped file 1041 // matches up with the previous mapping. 1042 if (Overridden->second != F->second) 1043 AnyFileChanged = true; 1044 continue; 1045 } 1046 1047 // The file was not remapped; check whether it has changed on disk. 1048 struct stat StatBuf; 1049 if (stat(F->first(), &StatBuf)) { 1050 // If we can't stat the file, assume that something horrible happened. 1051 AnyFileChanged = true; 1052 } else if (StatBuf.st_size != F->second.first || 1053 StatBuf.st_mtime != F->second.second) 1054 AnyFileChanged = true; 1055 } 1056 1057 if (!AnyFileChanged) { 1058 // Okay! We can re-use the precompiled preamble. 1059 1060 // Set the state of the diagnostic object to mimic its state 1061 // after parsing the preamble. 1062 // FIXME: This won't catch any #pragma push warning changes that 1063 // have occurred in the preamble. 1064 getDiagnostics().Reset(); 1065 ProcessWarningOptions(getDiagnostics(), 1066 PreambleInvocation.getDiagnosticOpts()); 1067 getDiagnostics().setNumWarnings(NumWarningsInPreamble); 1068 if (StoredDiagnostics.size() > NumStoredDiagnosticsInPreamble) 1069 StoredDiagnostics.erase( 1070 StoredDiagnostics.begin() + NumStoredDiagnosticsInPreamble, 1071 StoredDiagnostics.end()); 1072 1073 // Create a version of the main file buffer that is padded to 1074 // buffer size we reserved when creating the preamble. 1075 return CreatePaddedMainFileBuffer(NewPreamble.first, 1076 CreatedPreambleBuffer, 1077 PreambleReservedSize, 1078 FrontendOpts.Inputs[0].second); 1079 } 1080 } 1081 1082 // If we aren't allowed to rebuild the precompiled preamble, just 1083 // return now. 1084 if (!AllowRebuild) 1085 return 0; 1086 1087 // We can't reuse the previously-computed preamble. Build a new one. 1088 Preamble.clear(); 1089 llvm::sys::Path(PreambleFile).eraseFromDisk(); 1090 PreambleRebuildCounter = 1; 1091 } else if (!AllowRebuild) { 1092 // We aren't allowed to rebuild the precompiled preamble; just 1093 // return now. 1094 return 0; 1095 } 1096 1097 // If the preamble rebuild counter > 1, it's because we previously 1098 // failed to build a preamble and we're not yet ready to try 1099 // again. Decrement the counter and return a failure. 1100 if (PreambleRebuildCounter > 1) { 1101 --PreambleRebuildCounter; 1102 return 0; 1103 } 1104 1105 // Create a temporary file for the precompiled preamble. In rare 1106 // circumstances, this can fail. 1107 std::string PreamblePCHPath = GetPreamblePCHPath(); 1108 if (PreamblePCHPath.empty()) { 1109 // Try again next time. 1110 PreambleRebuildCounter = 1; 1111 return 0; 1112 } 1113 1114 // We did not previously compute a preamble, or it can't be reused anyway. 1115 llvm::Timer *PreambleTimer = 0; 1116 if (TimerGroup.get()) { 1117 PreambleTimer = new llvm::Timer("Precompiling preamble", *TimerGroup); 1118 PreambleTimer->startTimer(); 1119 Timers.push_back(PreambleTimer); 1120 } 1121 1122 // Create a new buffer that stores the preamble. The buffer also contains 1123 // extra space for the original contents of the file (which will be present 1124 // when we actually parse the file) along with more room in case the file 1125 // grows. 1126 PreambleReservedSize = NewPreamble.first->getBufferSize(); 1127 if (PreambleReservedSize < 4096) 1128 PreambleReservedSize = 8191; 1129 else 1130 PreambleReservedSize *= 2; 1131 1132 // Save the preamble text for later; we'll need to compare against it for 1133 // subsequent reparses. 1134 Preamble.assign(NewPreamble.first->getBufferStart(), 1135 NewPreamble.first->getBufferStart() 1136 + NewPreamble.second.first); 1137 PreambleEndsAtStartOfLine = NewPreamble.second.second; 1138 1139 delete PreambleBuffer; 1140 PreambleBuffer 1141 = llvm::MemoryBuffer::getNewUninitMemBuffer(PreambleReservedSize, 1142 FrontendOpts.Inputs[0].second); 1143 memcpy(const_cast<char*>(PreambleBuffer->getBufferStart()), 1144 NewPreamble.first->getBufferStart(), Preamble.size()); 1145 memset(const_cast<char*>(PreambleBuffer->getBufferStart()) + Preamble.size(), 1146 ' ', PreambleReservedSize - Preamble.size() - 1); 1147 const_cast<char*>(PreambleBuffer->getBufferEnd())[-1] = '\n'; 1148 1149 // Remap the main source file to the preamble buffer. 1150 llvm::sys::PathWithStatus MainFilePath(FrontendOpts.Inputs[0].second); 1151 PreprocessorOpts.addRemappedFile(MainFilePath.str(), PreambleBuffer); 1152 1153 // Tell the compiler invocation to generate a temporary precompiled header. 1154 FrontendOpts.ProgramAction = frontend::GeneratePCH; 1155 FrontendOpts.ChainedPCH = true; 1156 // FIXME: Generate the precompiled header into memory? 1157 FrontendOpts.OutputFile = PreamblePCHPath; 1158 PreprocessorOpts.PrecompiledPreambleBytes.first = 0; 1159 PreprocessorOpts.PrecompiledPreambleBytes.second = false; 1160 1161 // Create the compiler instance to use for building the precompiled preamble. 1162 CompilerInstance Clang; 1163 Clang.setInvocation(&PreambleInvocation); 1164 OriginalSourceFile = Clang.getFrontendOpts().Inputs[0].second; 1165 1166 // Set up diagnostics, capturing all of the diagnostics produced. 1167 Clang.setDiagnostics(&getDiagnostics()); 1168 CaptureDroppedDiagnostics Capture(CaptureDiagnostics, 1169 getDiagnostics(), 1170 StoredDiagnostics); 1171 1172 // Create the target instance. 1173 Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(), 1174 Clang.getTargetOpts())); 1175 if (!Clang.hasTarget()) { 1176 llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk(); 1177 Preamble.clear(); 1178 if (CreatedPreambleBuffer) 1179 delete NewPreamble.first; 1180 if (PreambleTimer) 1181 PreambleTimer->stopTimer(); 1182 PreambleRebuildCounter = DefaultPreambleRebuildInterval; 1183 PreprocessorOpts.eraseRemappedFile( 1184 PreprocessorOpts.remapped_file_buffer_end() - 1); 1185 return 0; 1186 } 1187 1188 // Inform the target of the language options. 1189 // 1190 // FIXME: We shouldn't need to do this, the target should be immutable once 1191 // created. This complexity should be lifted elsewhere. 1192 Clang.getTarget().setForcedLangOptions(Clang.getLangOpts()); 1193 1194 assert(Clang.getFrontendOpts().Inputs.size() == 1 && 1195 "Invocation must have exactly one source file!"); 1196 assert(Clang.getFrontendOpts().Inputs[0].first != IK_AST && 1197 "FIXME: AST inputs not yet supported here!"); 1198 assert(Clang.getFrontendOpts().Inputs[0].first != IK_LLVM_IR && 1199 "IR inputs not support here!"); 1200 1201 // Clear out old caches and data. 1202 getDiagnostics().Reset(); 1203 ProcessWarningOptions(getDiagnostics(), Clang.getDiagnosticOpts()); 1204 StoredDiagnostics.clear(); 1205 TopLevelDecls.clear(); 1206 TopLevelDeclsInPreamble.clear(); 1207 1208 // Create a file manager object to provide access to and cache the filesystem. 1209 Clang.setFileManager(new FileManager); 1210 1211 // Create the source manager. 1212 Clang.setSourceManager(new SourceManager(getDiagnostics())); 1213 1214 llvm::OwningPtr<PrecompilePreambleAction> Act; 1215 Act.reset(new PrecompilePreambleAction(*this)); 1216 if (!Act->BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second, 1217 Clang.getFrontendOpts().Inputs[0].first)) { 1218 Clang.takeInvocation(); 1219 llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk(); 1220 Preamble.clear(); 1221 if (CreatedPreambleBuffer) 1222 delete NewPreamble.first; 1223 if (PreambleTimer) 1224 PreambleTimer->stopTimer(); 1225 PreambleRebuildCounter = DefaultPreambleRebuildInterval; 1226 PreprocessorOpts.eraseRemappedFile( 1227 PreprocessorOpts.remapped_file_buffer_end() - 1); 1228 return 0; 1229 } 1230 1231 Act->Execute(); 1232 Act->EndSourceFile(); 1233 Clang.takeInvocation(); 1234 1235 if (Diagnostics->hasErrorOccurred()) { 1236 // There were errors parsing the preamble, so no precompiled header was 1237 // generated. Forget that we even tried. 1238 // FIXME: Should we leave a note for ourselves to try again? 1239 llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk(); 1240 Preamble.clear(); 1241 if (CreatedPreambleBuffer) 1242 delete NewPreamble.first; 1243 if (PreambleTimer) 1244 PreambleTimer->stopTimer(); 1245 TopLevelDeclsInPreamble.clear(); 1246 PreambleRebuildCounter = DefaultPreambleRebuildInterval; 1247 PreprocessorOpts.eraseRemappedFile( 1248 PreprocessorOpts.remapped_file_buffer_end() - 1); 1249 return 0; 1250 } 1251 1252 // Keep track of the preamble we precompiled. 1253 PreambleFile = FrontendOpts.OutputFile; 1254 NumStoredDiagnosticsInPreamble = StoredDiagnostics.size(); 1255 NumWarningsInPreamble = getDiagnostics().getNumWarnings(); 1256 1257 // Keep track of all of the files that the source manager knows about, 1258 // so we can verify whether they have changed or not. 1259 FilesInPreamble.clear(); 1260 SourceManager &SourceMgr = Clang.getSourceManager(); 1261 const llvm::MemoryBuffer *MainFileBuffer 1262 = SourceMgr.getBuffer(SourceMgr.getMainFileID()); 1263 for (SourceManager::fileinfo_iterator F = SourceMgr.fileinfo_begin(), 1264 FEnd = SourceMgr.fileinfo_end(); 1265 F != FEnd; 1266 ++F) { 1267 const FileEntry *File = F->second->Entry; 1268 if (!File || F->second->getRawBuffer() == MainFileBuffer) 1269 continue; 1270 1271 FilesInPreamble[File->getName()] 1272 = std::make_pair(F->second->getSize(), File->getModificationTime()); 1273 } 1274 1275 if (PreambleTimer) 1276 PreambleTimer->stopTimer(); 1277 1278 PreambleRebuildCounter = 1; 1279 PreprocessorOpts.eraseRemappedFile( 1280 PreprocessorOpts.remapped_file_buffer_end() - 1); 1281 return CreatePaddedMainFileBuffer(NewPreamble.first, 1282 CreatedPreambleBuffer, 1283 PreambleReservedSize, 1284 FrontendOpts.Inputs[0].second); 1285} 1286 1287void ASTUnit::RealizeTopLevelDeclsFromPreamble() { 1288 std::vector<Decl *> Resolved; 1289 Resolved.reserve(TopLevelDeclsInPreamble.size()); 1290 ExternalASTSource &Source = *getASTContext().getExternalSource(); 1291 for (unsigned I = 0, N = TopLevelDeclsInPreamble.size(); I != N; ++I) { 1292 // Resolve the declaration ID to an actual declaration, possibly 1293 // deserializing the declaration in the process. 1294 Decl *D = Source.GetExternalDecl(TopLevelDeclsInPreamble[I]); 1295 if (D) 1296 Resolved.push_back(D); 1297 } 1298 TopLevelDeclsInPreamble.clear(); 1299 TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end()); 1300} 1301 1302unsigned ASTUnit::getMaxPCHLevel() const { 1303 if (!getOnlyLocalDecls()) 1304 return Decl::MaxPCHLevel; 1305 1306 return 0; 1307} 1308 1309ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI, 1310 llvm::IntrusiveRefCntPtr<Diagnostic> Diags, 1311 bool OnlyLocalDecls, 1312 bool CaptureDiagnostics, 1313 bool PrecompilePreamble, 1314 bool CompleteTranslationUnit, 1315 bool CacheCodeCompletionResults) { 1316 if (!Diags.getPtr()) { 1317 // No diagnostics engine was provided, so create our own diagnostics object 1318 // with the default options. 1319 DiagnosticOptions DiagOpts; 1320 Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0); 1321 } 1322 1323 // Create the AST unit. 1324 llvm::OwningPtr<ASTUnit> AST; 1325 AST.reset(new ASTUnit(false)); 1326 AST->Diagnostics = Diags; 1327 AST->CaptureDiagnostics = CaptureDiagnostics; 1328 AST->OnlyLocalDecls = OnlyLocalDecls; 1329 AST->CompleteTranslationUnit = CompleteTranslationUnit; 1330 AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults; 1331 AST->Invocation.reset(CI); 1332 CI->getPreprocessorOpts().RetainRemappedFileBuffers = true; 1333 1334 if (getenv("LIBCLANG_TIMING")) 1335 AST->TimerGroup.reset( 1336 new llvm::TimerGroup(CI->getFrontendOpts().Inputs[0].second)); 1337 1338 1339 llvm::MemoryBuffer *OverrideMainBuffer = 0; 1340 // FIXME: When C++ PCH is ready, allow use of it for a precompiled preamble. 1341 if (PrecompilePreamble && !CI->getLangOpts().CPlusPlus) { 1342 AST->PreambleRebuildCounter = 1; 1343 OverrideMainBuffer 1344 = AST->getMainBufferWithPrecompiledPreamble(*AST->Invocation); 1345 } 1346 1347 llvm::Timer *ParsingTimer = 0; 1348 if (AST->TimerGroup.get()) { 1349 ParsingTimer = new llvm::Timer("Initial parse", *AST->TimerGroup); 1350 ParsingTimer->startTimer(); 1351 AST->Timers.push_back(ParsingTimer); 1352 } 1353 1354 bool Failed = AST->Parse(OverrideMainBuffer); 1355 if (ParsingTimer) 1356 ParsingTimer->stopTimer(); 1357 1358 return Failed? 0 : AST.take(); 1359} 1360 1361ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin, 1362 const char **ArgEnd, 1363 llvm::IntrusiveRefCntPtr<Diagnostic> Diags, 1364 llvm::StringRef ResourceFilesPath, 1365 bool OnlyLocalDecls, 1366 RemappedFile *RemappedFiles, 1367 unsigned NumRemappedFiles, 1368 bool CaptureDiagnostics, 1369 bool PrecompilePreamble, 1370 bool CompleteTranslationUnit, 1371 bool CacheCodeCompletionResults) { 1372 bool CreatedDiagnosticsObject = false; 1373 1374 if (!Diags.getPtr()) { 1375 // No diagnostics engine was provided, so create our own diagnostics object 1376 // with the default options. 1377 DiagnosticOptions DiagOpts; 1378 Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0); 1379 CreatedDiagnosticsObject = true; 1380 } 1381 1382 llvm::SmallVector<const char *, 16> Args; 1383 Args.push_back("<clang>"); // FIXME: Remove dummy argument. 1384 Args.insert(Args.end(), ArgBegin, ArgEnd); 1385 1386 // FIXME: Find a cleaner way to force the driver into restricted modes. We 1387 // also want to force it to use clang. 1388 Args.push_back("-fsyntax-only"); 1389 1390 // FIXME: We shouldn't have to pass in the path info. 1391 driver::Driver TheDriver("clang", llvm::sys::getHostTriple(), 1392 "a.out", false, false, *Diags); 1393 1394 // Don't check that inputs exist, they have been remapped. 1395 TheDriver.setCheckInputsExist(false); 1396 1397 llvm::OwningPtr<driver::Compilation> C( 1398 TheDriver.BuildCompilation(Args.size(), Args.data())); 1399 1400 // We expect to get back exactly one command job, if we didn't something 1401 // failed. 1402 const driver::JobList &Jobs = C->getJobs(); 1403 if (Jobs.size() != 1 || !isa<driver::Command>(Jobs.begin())) { 1404 llvm::SmallString<256> Msg; 1405 llvm::raw_svector_ostream OS(Msg); 1406 C->PrintJob(OS, C->getJobs(), "; ", true); 1407 Diags->Report(diag::err_fe_expected_compiler_job) << OS.str(); 1408 return 0; 1409 } 1410 1411 const driver::Command *Cmd = cast<driver::Command>(*Jobs.begin()); 1412 if (llvm::StringRef(Cmd->getCreator().getName()) != "clang") { 1413 Diags->Report(diag::err_fe_expected_clang_command); 1414 return 0; 1415 } 1416 1417 const driver::ArgStringList &CCArgs = Cmd->getArguments(); 1418 llvm::OwningPtr<CompilerInvocation> CI(new CompilerInvocation); 1419 CompilerInvocation::CreateFromArgs(*CI, 1420 const_cast<const char **>(CCArgs.data()), 1421 const_cast<const char **>(CCArgs.data()) + 1422 CCArgs.size(), 1423 *Diags); 1424 1425 // Override any files that need remapping 1426 for (unsigned I = 0; I != NumRemappedFiles; ++I) 1427 CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, 1428 RemappedFiles[I].second); 1429 1430 // Override the resources path. 1431 CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath; 1432 1433 CI->getFrontendOpts().DisableFree = false; 1434 return LoadFromCompilerInvocation(CI.take(), Diags, OnlyLocalDecls, 1435 CaptureDiagnostics, PrecompilePreamble, 1436 CompleteTranslationUnit, 1437 CacheCodeCompletionResults); 1438} 1439 1440bool ASTUnit::Reparse(RemappedFile *RemappedFiles, unsigned NumRemappedFiles) { 1441 if (!Invocation.get()) 1442 return true; 1443 1444 llvm::Timer *ReparsingTimer = 0; 1445 if (TimerGroup.get()) { 1446 ReparsingTimer = new llvm::Timer("Reparse", *TimerGroup); 1447 ReparsingTimer->startTimer(); 1448 Timers.push_back(ReparsingTimer); 1449 } 1450 1451 // Remap files. 1452 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts(); 1453 for (PreprocessorOptions::remapped_file_buffer_iterator 1454 R = PPOpts.remapped_file_buffer_begin(), 1455 REnd = PPOpts.remapped_file_buffer_end(); 1456 R != REnd; 1457 ++R) { 1458 delete R->second; 1459 } 1460 Invocation->getPreprocessorOpts().clearRemappedFiles(); 1461 for (unsigned I = 0; I != NumRemappedFiles; ++I) 1462 Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, 1463 RemappedFiles[I].second); 1464 1465 // If we have a preamble file lying around, or if we might try to 1466 // build a precompiled preamble, do so now. 1467 llvm::MemoryBuffer *OverrideMainBuffer = 0; 1468 if (!PreambleFile.empty() || PreambleRebuildCounter > 0) 1469 OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(*Invocation); 1470 1471 // Clear out the diagnostics state. 1472 if (!OverrideMainBuffer) { 1473 getDiagnostics().Reset(); 1474 ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts()); 1475 } 1476 1477 // Parse the sources 1478 bool Result = Parse(OverrideMainBuffer); 1479 if (ReparsingTimer) 1480 ReparsingTimer->stopTimer(); 1481 1482 if (ShouldCacheCodeCompletionResults) { 1483 if (CacheCodeCompletionCoolDown > 0) 1484 --CacheCodeCompletionCoolDown; 1485 else if (top_level_size() != NumTopLevelDeclsAtLastCompletionCache) 1486 CacheCodeCompletionResults(); 1487 } 1488 1489 return Result; 1490} 1491 1492//----------------------------------------------------------------------------// 1493// Code completion 1494//----------------------------------------------------------------------------// 1495 1496namespace { 1497 /// \brief Code completion consumer that combines the cached code-completion 1498 /// results from an ASTUnit with the code-completion results provided to it, 1499 /// then passes the result on to 1500 class AugmentedCodeCompleteConsumer : public CodeCompleteConsumer { 1501 unsigned NormalContexts; 1502 ASTUnit &AST; 1503 CodeCompleteConsumer &Next; 1504 1505 public: 1506 AugmentedCodeCompleteConsumer(ASTUnit &AST, CodeCompleteConsumer &Next, 1507 bool IncludeMacros, bool IncludeCodePatterns, 1508 bool IncludeGlobals) 1509 : CodeCompleteConsumer(IncludeMacros, IncludeCodePatterns, IncludeGlobals, 1510 Next.isOutputBinary()), AST(AST), Next(Next) 1511 { 1512 // Compute the set of contexts in which we will look when we don't have 1513 // any information about the specific context. 1514 NormalContexts 1515 = (1 << (CodeCompletionContext::CCC_TopLevel - 1)) 1516 | (1 << (CodeCompletionContext::CCC_ObjCInterface - 1)) 1517 | (1 << (CodeCompletionContext::CCC_ObjCImplementation - 1)) 1518 | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1)) 1519 | (1 << (CodeCompletionContext::CCC_Statement - 1)) 1520 | (1 << (CodeCompletionContext::CCC_Expression - 1)) 1521 | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1)) 1522 | (1 << (CodeCompletionContext::CCC_MemberAccess - 1)) 1523 | (1 << (CodeCompletionContext::CCC_ObjCProtocolName - 1)) 1524 | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1)) 1525 | (1 << (CodeCompletionContext::CCC_Recovery - 1)); 1526 1527 if (AST.getASTContext().getLangOptions().CPlusPlus) 1528 NormalContexts |= (1 << (CodeCompletionContext::CCC_EnumTag - 1)) 1529 | (1 << (CodeCompletionContext::CCC_UnionTag - 1)) 1530 | (1 << (CodeCompletionContext::CCC_ClassOrStructTag - 1)); 1531 } 1532 1533 virtual void ProcessCodeCompleteResults(Sema &S, 1534 CodeCompletionContext Context, 1535 CodeCompletionResult *Results, 1536 unsigned NumResults); 1537 1538 virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, 1539 OverloadCandidate *Candidates, 1540 unsigned NumCandidates) { 1541 Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates); 1542 } 1543 }; 1544} 1545 1546/// \brief Helper function that computes which global names are hidden by the 1547/// local code-completion results. 1548void CalculateHiddenNames(const CodeCompletionContext &Context, 1549 CodeCompletionResult *Results, 1550 unsigned NumResults, 1551 ASTContext &Ctx, 1552 llvm::StringSet<> &HiddenNames) { 1553 bool OnlyTagNames = false; 1554 switch (Context.getKind()) { 1555 case CodeCompletionContext::CCC_Recovery: 1556 case CodeCompletionContext::CCC_TopLevel: 1557 case CodeCompletionContext::CCC_ObjCInterface: 1558 case CodeCompletionContext::CCC_ObjCImplementation: 1559 case CodeCompletionContext::CCC_ObjCIvarList: 1560 case CodeCompletionContext::CCC_ClassStructUnion: 1561 case CodeCompletionContext::CCC_Statement: 1562 case CodeCompletionContext::CCC_Expression: 1563 case CodeCompletionContext::CCC_ObjCMessageReceiver: 1564 case CodeCompletionContext::CCC_MemberAccess: 1565 case CodeCompletionContext::CCC_Namespace: 1566 case CodeCompletionContext::CCC_Type: 1567 case CodeCompletionContext::CCC_Name: 1568 case CodeCompletionContext::CCC_PotentiallyQualifiedName: 1569 case CodeCompletionContext::CCC_ParenthesizedExpression: 1570 break; 1571 1572 case CodeCompletionContext::CCC_EnumTag: 1573 case CodeCompletionContext::CCC_UnionTag: 1574 case CodeCompletionContext::CCC_ClassOrStructTag: 1575 OnlyTagNames = true; 1576 break; 1577 1578 case CodeCompletionContext::CCC_ObjCProtocolName: 1579 case CodeCompletionContext::CCC_MacroName: 1580 case CodeCompletionContext::CCC_MacroNameUse: 1581 case CodeCompletionContext::CCC_PreprocessorExpression: 1582 case CodeCompletionContext::CCC_PreprocessorDirective: 1583 case CodeCompletionContext::CCC_NaturalLanguage: 1584 case CodeCompletionContext::CCC_SelectorName: 1585 case CodeCompletionContext::CCC_TypeQualifiers: 1586 case CodeCompletionContext::CCC_Other: 1587 // We're looking for nothing, or we're looking for names that cannot 1588 // be hidden. 1589 return; 1590 } 1591 1592 typedef CodeCompletionResult Result; 1593 for (unsigned I = 0; I != NumResults; ++I) { 1594 if (Results[I].Kind != Result::RK_Declaration) 1595 continue; 1596 1597 unsigned IDNS 1598 = Results[I].Declaration->getUnderlyingDecl()->getIdentifierNamespace(); 1599 1600 bool Hiding = false; 1601 if (OnlyTagNames) 1602 Hiding = (IDNS & Decl::IDNS_Tag); 1603 else { 1604 unsigned HiddenIDNS = (Decl::IDNS_Type | Decl::IDNS_Member | 1605 Decl::IDNS_Namespace | Decl::IDNS_Ordinary | 1606 Decl::IDNS_NonMemberOperator); 1607 if (Ctx.getLangOptions().CPlusPlus) 1608 HiddenIDNS |= Decl::IDNS_Tag; 1609 Hiding = (IDNS & HiddenIDNS); 1610 } 1611 1612 if (!Hiding) 1613 continue; 1614 1615 DeclarationName Name = Results[I].Declaration->getDeclName(); 1616 if (IdentifierInfo *Identifier = Name.getAsIdentifierInfo()) 1617 HiddenNames.insert(Identifier->getName()); 1618 else 1619 HiddenNames.insert(Name.getAsString()); 1620 } 1621} 1622 1623 1624void AugmentedCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &S, 1625 CodeCompletionContext Context, 1626 CodeCompletionResult *Results, 1627 unsigned NumResults) { 1628 // Merge the results we were given with the results we cached. 1629 bool AddedResult = false; 1630 unsigned InContexts 1631 = (Context.getKind() == CodeCompletionContext::CCC_Recovery? NormalContexts 1632 : (1 << (Context.getKind() - 1))); 1633 1634 // Contains the set of names that are hidden by "local" completion results. 1635 llvm::StringSet<> HiddenNames; 1636 llvm::SmallVector<CodeCompletionString *, 4> StringsToDestroy; 1637 typedef CodeCompletionResult Result; 1638 llvm::SmallVector<Result, 8> AllResults; 1639 for (ASTUnit::cached_completion_iterator 1640 C = AST.cached_completion_begin(), 1641 CEnd = AST.cached_completion_end(); 1642 C != CEnd; ++C) { 1643 // If the context we are in matches any of the contexts we are 1644 // interested in, we'll add this result. 1645 if ((C->ShowInContexts & InContexts) == 0) 1646 continue; 1647 1648 // If we haven't added any results previously, do so now. 1649 if (!AddedResult) { 1650 CalculateHiddenNames(Context, Results, NumResults, S.Context, 1651 HiddenNames); 1652 AllResults.insert(AllResults.end(), Results, Results + NumResults); 1653 AddedResult = true; 1654 } 1655 1656 // Determine whether this global completion result is hidden by a local 1657 // completion result. If so, skip it. 1658 if (C->Kind != CXCursor_MacroDefinition && 1659 HiddenNames.count(C->Completion->getTypedText())) 1660 continue; 1661 1662 // Adjust priority based on similar type classes. 1663 unsigned Priority = C->Priority; 1664 CXCursorKind CursorKind = C->Kind; 1665 CodeCompletionString *Completion = C->Completion; 1666 if (!Context.getPreferredType().isNull()) { 1667 if (C->Kind == CXCursor_MacroDefinition) { 1668 Priority = getMacroUsagePriority(C->Completion->getTypedText(), 1669 S.getLangOptions(), 1670 Context.getPreferredType()->isAnyPointerType()); 1671 } else if (C->Type) { 1672 CanQualType Expected 1673 = S.Context.getCanonicalType( 1674 Context.getPreferredType().getUnqualifiedType()); 1675 SimplifiedTypeClass ExpectedSTC = getSimplifiedTypeClass(Expected); 1676 if (ExpectedSTC == C->TypeClass) { 1677 // We know this type is similar; check for an exact match. 1678 llvm::StringMap<unsigned> &CachedCompletionTypes 1679 = AST.getCachedCompletionTypes(); 1680 llvm::StringMap<unsigned>::iterator Pos 1681 = CachedCompletionTypes.find(QualType(Expected).getAsString()); 1682 if (Pos != CachedCompletionTypes.end() && Pos->second == C->Type) 1683 Priority /= CCF_ExactTypeMatch; 1684 else 1685 Priority /= CCF_SimilarTypeMatch; 1686 } 1687 } 1688 } 1689 1690 // Adjust the completion string, if required. 1691 if (C->Kind == CXCursor_MacroDefinition && 1692 Context.getKind() == CodeCompletionContext::CCC_MacroNameUse) { 1693 // Create a new code-completion string that just contains the 1694 // macro name, without its arguments. 1695 Completion = new CodeCompletionString; 1696 Completion->AddTypedTextChunk(C->Completion->getTypedText()); 1697 StringsToDestroy.push_back(Completion); 1698 CursorKind = CXCursor_NotImplemented; 1699 Priority = CCP_CodePattern; 1700 } 1701 1702 AllResults.push_back(Result(Completion, Priority, CursorKind, 1703 C->Availability)); 1704 } 1705 1706 // If we did not add any cached completion results, just forward the 1707 // results we were given to the next consumer. 1708 if (!AddedResult) { 1709 Next.ProcessCodeCompleteResults(S, Context, Results, NumResults); 1710 return; 1711 } 1712 1713 Next.ProcessCodeCompleteResults(S, Context, AllResults.data(), 1714 AllResults.size()); 1715 1716 for (unsigned I = 0, N = StringsToDestroy.size(); I != N; ++I) 1717 delete StringsToDestroy[I]; 1718} 1719 1720 1721 1722void ASTUnit::CodeComplete(llvm::StringRef File, unsigned Line, unsigned Column, 1723 RemappedFile *RemappedFiles, 1724 unsigned NumRemappedFiles, 1725 bool IncludeMacros, 1726 bool IncludeCodePatterns, 1727 CodeCompleteConsumer &Consumer, 1728 Diagnostic &Diag, LangOptions &LangOpts, 1729 SourceManager &SourceMgr, FileManager &FileMgr, 1730 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics, 1731 llvm::SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers) { 1732 if (!Invocation.get()) 1733 return; 1734 1735 llvm::Timer *CompletionTimer = 0; 1736 if (TimerGroup.get()) { 1737 llvm::SmallString<128> TimerName; 1738 llvm::raw_svector_ostream TimerNameOut(TimerName); 1739 TimerNameOut << "Code completion @ " << File << ":" << Line << ":" 1740 << Column; 1741 CompletionTimer = new llvm::Timer(TimerNameOut.str(), *TimerGroup); 1742 CompletionTimer->startTimer(); 1743 Timers.push_back(CompletionTimer); 1744 } 1745 1746 CompilerInvocation CCInvocation(*Invocation); 1747 FrontendOptions &FrontendOpts = CCInvocation.getFrontendOpts(); 1748 PreprocessorOptions &PreprocessorOpts = CCInvocation.getPreprocessorOpts(); 1749 1750 FrontendOpts.ShowMacrosInCodeCompletion 1751 = IncludeMacros && CachedCompletionResults.empty(); 1752 FrontendOpts.ShowCodePatternsInCodeCompletion = IncludeCodePatterns; 1753 FrontendOpts.ShowGlobalSymbolsInCodeCompletion 1754 = CachedCompletionResults.empty(); 1755 FrontendOpts.CodeCompletionAt.FileName = File; 1756 FrontendOpts.CodeCompletionAt.Line = Line; 1757 FrontendOpts.CodeCompletionAt.Column = Column; 1758 1759 // Set the language options appropriately. 1760 LangOpts = CCInvocation.getLangOpts(); 1761 1762 CompilerInstance Clang; 1763 Clang.setInvocation(&CCInvocation); 1764 OriginalSourceFile = Clang.getFrontendOpts().Inputs[0].second; 1765 1766 // Set up diagnostics, capturing any diagnostics produced. 1767 Clang.setDiagnostics(&Diag); 1768 ProcessWarningOptions(Diag, CCInvocation.getDiagnosticOpts()); 1769 CaptureDroppedDiagnostics Capture(true, 1770 Clang.getDiagnostics(), 1771 StoredDiagnostics); 1772 1773 // Create the target instance. 1774 Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(), 1775 Clang.getTargetOpts())); 1776 if (!Clang.hasTarget()) { 1777 Clang.takeInvocation(); 1778 return; 1779 } 1780 1781 // Inform the target of the language options. 1782 // 1783 // FIXME: We shouldn't need to do this, the target should be immutable once 1784 // created. This complexity should be lifted elsewhere. 1785 Clang.getTarget().setForcedLangOptions(Clang.getLangOpts()); 1786 1787 assert(Clang.getFrontendOpts().Inputs.size() == 1 && 1788 "Invocation must have exactly one source file!"); 1789 assert(Clang.getFrontendOpts().Inputs[0].first != IK_AST && 1790 "FIXME: AST inputs not yet supported here!"); 1791 assert(Clang.getFrontendOpts().Inputs[0].first != IK_LLVM_IR && 1792 "IR inputs not support here!"); 1793 1794 1795 // Use the source and file managers that we were given. 1796 Clang.setFileManager(&FileMgr); 1797 Clang.setSourceManager(&SourceMgr); 1798 1799 // Remap files. 1800 PreprocessorOpts.clearRemappedFiles(); 1801 PreprocessorOpts.RetainRemappedFileBuffers = true; 1802 for (unsigned I = 0; I != NumRemappedFiles; ++I) { 1803 PreprocessorOpts.addRemappedFile(RemappedFiles[I].first, 1804 RemappedFiles[I].second); 1805 OwnedBuffers.push_back(RemappedFiles[I].second); 1806 } 1807 1808 // Use the code completion consumer we were given, but adding any cached 1809 // code-completion results. 1810 AugmentedCodeCompleteConsumer 1811 AugmentedConsumer(*this, Consumer, FrontendOpts.ShowMacrosInCodeCompletion, 1812 FrontendOpts.ShowCodePatternsInCodeCompletion, 1813 FrontendOpts.ShowGlobalSymbolsInCodeCompletion); 1814 Clang.setCodeCompletionConsumer(&AugmentedConsumer); 1815 1816 // If we have a precompiled preamble, try to use it. We only allow 1817 // the use of the precompiled preamble if we're if the completion 1818 // point is within the main file, after the end of the precompiled 1819 // preamble. 1820 llvm::MemoryBuffer *OverrideMainBuffer = 0; 1821 if (!PreambleFile.empty()) { 1822 using llvm::sys::FileStatus; 1823 llvm::sys::PathWithStatus CompleteFilePath(File); 1824 llvm::sys::PathWithStatus MainPath(OriginalSourceFile); 1825 if (const FileStatus *CompleteFileStatus = CompleteFilePath.getFileStatus()) 1826 if (const FileStatus *MainStatus = MainPath.getFileStatus()) 1827 if (CompleteFileStatus->getUniqueID() == MainStatus->getUniqueID()) 1828 OverrideMainBuffer 1829 = getMainBufferWithPrecompiledPreamble(CCInvocation, false, 1830 Line - 1); 1831 } 1832 1833 // If the main file has been overridden due to the use of a preamble, 1834 // make that override happen and introduce the preamble. 1835 if (OverrideMainBuffer) { 1836 PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer); 1837 PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size(); 1838 PreprocessorOpts.PrecompiledPreambleBytes.second 1839 = PreambleEndsAtStartOfLine; 1840 PreprocessorOpts.ImplicitPCHInclude = PreambleFile; 1841 PreprocessorOpts.DisablePCHValidation = true; 1842 1843 // The stored diagnostics have the old source manager. Copy them 1844 // to our output set of stored diagnostics, updating the source 1845 // manager to the one we were given. 1846 for (unsigned I = 0, N = this->StoredDiagnostics.size(); I != N; ++I) { 1847 StoredDiagnostics.push_back(this->StoredDiagnostics[I]); 1848 FullSourceLoc Loc(StoredDiagnostics[I].getLocation(), SourceMgr); 1849 StoredDiagnostics[I].setLocation(Loc); 1850 } 1851 1852 OwnedBuffers.push_back(OverrideMainBuffer); 1853 } else { 1854 PreprocessorOpts.PrecompiledPreambleBytes.first = 0; 1855 PreprocessorOpts.PrecompiledPreambleBytes.second = false; 1856 } 1857 1858 llvm::OwningPtr<SyntaxOnlyAction> Act; 1859 Act.reset(new SyntaxOnlyAction); 1860 if (Act->BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second, 1861 Clang.getFrontendOpts().Inputs[0].first)) { 1862 Act->Execute(); 1863 Act->EndSourceFile(); 1864 } 1865 1866 if (CompletionTimer) 1867 CompletionTimer->stopTimer(); 1868 1869 // Steal back our resources. 1870 Clang.takeFileManager(); 1871 Clang.takeSourceManager(); 1872 Clang.takeInvocation(); 1873 Clang.takeCodeCompletionConsumer(); 1874} 1875 1876bool ASTUnit::Save(llvm::StringRef File) { 1877 if (getDiagnostics().hasErrorOccurred()) 1878 return true; 1879 1880 // FIXME: Can we somehow regenerate the stat cache here, or do we need to 1881 // unconditionally create a stat cache when we parse the file? 1882 std::string ErrorInfo; 1883 llvm::raw_fd_ostream Out(File.str().c_str(), ErrorInfo, 1884 llvm::raw_fd_ostream::F_Binary); 1885 if (!ErrorInfo.empty() || Out.has_error()) 1886 return true; 1887 1888 std::vector<unsigned char> Buffer; 1889 llvm::BitstreamWriter Stream(Buffer); 1890 ASTWriter Writer(Stream); 1891 Writer.WriteAST(getSema(), 0, 0); 1892 1893 // Write the generated bitstream to "Out". 1894 if (!Buffer.empty()) 1895 Out.write((char *)&Buffer.front(), Buffer.size()); 1896 Out.close(); 1897 return Out.has_error(); 1898} 1899