CompilerInstance.cpp revision 708002ede3938256a6223d153a0c040f7a40f5cc
1//===--- CompilerInstance.cpp ---------------------------------------------===// 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#include "clang/Frontend/CompilerInstance.h" 11#include "clang/AST/ASTConsumer.h" 12#include "clang/AST/ASTContext.h" 13#include "clang/AST/Decl.h" 14#include "clang/Basic/Diagnostic.h" 15#include "clang/Basic/FileManager.h" 16#include "clang/Basic/SourceManager.h" 17#include "clang/Basic/TargetInfo.h" 18#include "clang/Basic/Version.h" 19#include "clang/Frontend/ChainedDiagnosticConsumer.h" 20#include "clang/Frontend/FrontendAction.h" 21#include "clang/Frontend/FrontendActions.h" 22#include "clang/Frontend/FrontendDiagnostic.h" 23#include "clang/Frontend/LogDiagnosticPrinter.h" 24#include "clang/Frontend/SerializedDiagnosticPrinter.h" 25#include "clang/Frontend/TextDiagnosticPrinter.h" 26#include "clang/Frontend/Utils.h" 27#include "clang/Frontend/VerifyDiagnosticConsumer.h" 28#include "clang/Lex/HeaderSearch.h" 29#include "clang/Lex/PTHManager.h" 30#include "clang/Lex/Preprocessor.h" 31#include "clang/Sema/CodeCompleteConsumer.h" 32#include "clang/Sema/Sema.h" 33#include "clang/Serialization/ASTReader.h" 34#include "llvm/ADT/Statistic.h" 35#include "llvm/Config/config.h" 36#include "llvm/Support/CrashRecoveryContext.h" 37#include "llvm/Support/FileSystem.h" 38#include "llvm/Support/Host.h" 39#include "llvm/Support/LockFileManager.h" 40#include "llvm/Support/MemoryBuffer.h" 41#include "llvm/Support/Path.h" 42#include "llvm/Support/Program.h" 43#include "llvm/Support/Signals.h" 44#include "llvm/Support/Timer.h" 45#include "llvm/Support/raw_ostream.h" 46#include "llvm/Support/system_error.h" 47#include <sys/stat.h> 48#include <time.h> 49 50using namespace clang; 51 52CompilerInstance::CompilerInstance() 53 : Invocation(new CompilerInvocation()), ModuleManager(0), 54 BuildGlobalModuleIndex(false), ModuleBuildFailed(false) { 55} 56 57CompilerInstance::~CompilerInstance() { 58 assert(OutputFiles.empty() && "Still output files in flight?"); 59} 60 61void CompilerInstance::setInvocation(CompilerInvocation *Value) { 62 Invocation = Value; 63} 64 65bool CompilerInstance::shouldBuildGlobalModuleIndex() const { 66 return (BuildGlobalModuleIndex || 67 (ModuleManager && ModuleManager->isGlobalIndexUnavailable() && 68 getFrontendOpts().GenerateGlobalModuleIndex)) && 69 !ModuleBuildFailed; 70} 71 72void CompilerInstance::setDiagnostics(DiagnosticsEngine *Value) { 73 Diagnostics = Value; 74} 75 76void CompilerInstance::setTarget(TargetInfo *Value) { 77 Target = Value; 78} 79 80void CompilerInstance::setFileManager(FileManager *Value) { 81 FileMgr = Value; 82} 83 84void CompilerInstance::setSourceManager(SourceManager *Value) { 85 SourceMgr = Value; 86} 87 88void CompilerInstance::setPreprocessor(Preprocessor *Value) { PP = Value; } 89 90void CompilerInstance::setASTContext(ASTContext *Value) { Context = Value; } 91 92void CompilerInstance::setSema(Sema *S) { 93 TheSema.reset(S); 94} 95 96void CompilerInstance::setASTConsumer(ASTConsumer *Value) { 97 Consumer.reset(Value); 98} 99 100void CompilerInstance::setCodeCompletionConsumer(CodeCompleteConsumer *Value) { 101 CompletionConsumer.reset(Value); 102} 103 104// Diagnostics 105static void SetUpDiagnosticLog(DiagnosticOptions *DiagOpts, 106 const CodeGenOptions *CodeGenOpts, 107 DiagnosticsEngine &Diags) { 108 std::string ErrorInfo; 109 bool OwnsStream = false; 110 raw_ostream *OS = &llvm::errs(); 111 if (DiagOpts->DiagnosticLogFile != "-") { 112 // Create the output stream. 113 llvm::raw_fd_ostream *FileOS( 114 new llvm::raw_fd_ostream(DiagOpts->DiagnosticLogFile.c_str(), ErrorInfo, 115 llvm::sys::fs::F_Append)); 116 if (!ErrorInfo.empty()) { 117 Diags.Report(diag::warn_fe_cc_log_diagnostics_failure) 118 << DiagOpts->DiagnosticLogFile << ErrorInfo; 119 } else { 120 FileOS->SetUnbuffered(); 121 FileOS->SetUseAtomicWrites(true); 122 OS = FileOS; 123 OwnsStream = true; 124 } 125 } 126 127 // Chain in the diagnostic client which will log the diagnostics. 128 LogDiagnosticPrinter *Logger = new LogDiagnosticPrinter(*OS, DiagOpts, 129 OwnsStream); 130 if (CodeGenOpts) 131 Logger->setDwarfDebugFlags(CodeGenOpts->DwarfDebugFlags); 132 Diags.setClient(new ChainedDiagnosticConsumer(Diags.takeClient(), Logger)); 133} 134 135static void SetupSerializedDiagnostics(DiagnosticOptions *DiagOpts, 136 DiagnosticsEngine &Diags, 137 StringRef OutputFile) { 138 std::string ErrorInfo; 139 OwningPtr<llvm::raw_fd_ostream> OS; 140 OS.reset(new llvm::raw_fd_ostream(OutputFile.str().c_str(), ErrorInfo, 141 llvm::sys::fs::F_Binary)); 142 143 if (!ErrorInfo.empty()) { 144 Diags.Report(diag::warn_fe_serialized_diag_failure) 145 << OutputFile << ErrorInfo; 146 return; 147 } 148 149 DiagnosticConsumer *SerializedConsumer = 150 clang::serialized_diags::create(OS.take(), DiagOpts); 151 152 153 Diags.setClient(new ChainedDiagnosticConsumer(Diags.takeClient(), 154 SerializedConsumer)); 155} 156 157void CompilerInstance::createDiagnostics(DiagnosticConsumer *Client, 158 bool ShouldOwnClient) { 159 Diagnostics = createDiagnostics(&getDiagnosticOpts(), Client, 160 ShouldOwnClient, &getCodeGenOpts()); 161} 162 163IntrusiveRefCntPtr<DiagnosticsEngine> 164CompilerInstance::createDiagnostics(DiagnosticOptions *Opts, 165 DiagnosticConsumer *Client, 166 bool ShouldOwnClient, 167 const CodeGenOptions *CodeGenOpts) { 168 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); 169 IntrusiveRefCntPtr<DiagnosticsEngine> 170 Diags(new DiagnosticsEngine(DiagID, Opts)); 171 172 // Create the diagnostic client for reporting errors or for 173 // implementing -verify. 174 if (Client) { 175 Diags->setClient(Client, ShouldOwnClient); 176 } else 177 Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts)); 178 179 // Chain in -verify checker, if requested. 180 if (Opts->VerifyDiagnostics) 181 Diags->setClient(new VerifyDiagnosticConsumer(*Diags)); 182 183 // Chain in -diagnostic-log-file dumper, if requested. 184 if (!Opts->DiagnosticLogFile.empty()) 185 SetUpDiagnosticLog(Opts, CodeGenOpts, *Diags); 186 187 if (!Opts->DiagnosticSerializationFile.empty()) 188 SetupSerializedDiagnostics(Opts, *Diags, 189 Opts->DiagnosticSerializationFile); 190 191 // Configure our handling of diagnostics. 192 ProcessWarningOptions(*Diags, *Opts); 193 194 return Diags; 195} 196 197// File Manager 198 199void CompilerInstance::createFileManager() { 200 FileMgr = new FileManager(getFileSystemOpts()); 201} 202 203// Source Manager 204 205void CompilerInstance::createSourceManager(FileManager &FileMgr) { 206 SourceMgr = new SourceManager(getDiagnostics(), FileMgr); 207} 208 209// Preprocessor 210 211void CompilerInstance::createPreprocessor() { 212 const PreprocessorOptions &PPOpts = getPreprocessorOpts(); 213 214 // Create a PTH manager if we are using some form of a token cache. 215 PTHManager *PTHMgr = 0; 216 if (!PPOpts.TokenCache.empty()) 217 PTHMgr = PTHManager::Create(PPOpts.TokenCache, getDiagnostics()); 218 219 // Create the Preprocessor. 220 HeaderSearch *HeaderInfo = new HeaderSearch(&getHeaderSearchOpts(), 221 getFileManager(), 222 getDiagnostics(), 223 getLangOpts(), 224 &getTarget()); 225 PP = new Preprocessor(&getPreprocessorOpts(), 226 getDiagnostics(), getLangOpts(), &getTarget(), 227 getSourceManager(), *HeaderInfo, *this, PTHMgr, 228 /*OwnsHeaderSearch=*/true); 229 230 // Note that this is different then passing PTHMgr to Preprocessor's ctor. 231 // That argument is used as the IdentifierInfoLookup argument to 232 // IdentifierTable's ctor. 233 if (PTHMgr) { 234 PTHMgr->setPreprocessor(&*PP); 235 PP->setPTHManager(PTHMgr); 236 } 237 238 if (PPOpts.DetailedRecord) 239 PP->createPreprocessingRecord(); 240 241 InitializePreprocessor(*PP, PPOpts, getHeaderSearchOpts(), getFrontendOpts()); 242 243 PP->setPreprocessedOutput(getPreprocessorOutputOpts().ShowCPP); 244 245 // Set up the module path, including the hash for the 246 // module-creation options. 247 SmallString<256> SpecificModuleCache( 248 getHeaderSearchOpts().ModuleCachePath); 249 if (!getHeaderSearchOpts().DisableModuleHash) 250 llvm::sys::path::append(SpecificModuleCache, 251 getInvocation().getModuleHash()); 252 PP->getHeaderSearchInfo().setModuleCachePath(SpecificModuleCache); 253 254 // Handle generating dependencies, if requested. 255 const DependencyOutputOptions &DepOpts = getDependencyOutputOpts(); 256 if (!DepOpts.OutputFile.empty()) 257 AttachDependencyFileGen(*PP, DepOpts); 258 if (!DepOpts.DOTOutputFile.empty()) 259 AttachDependencyGraphGen(*PP, DepOpts.DOTOutputFile, 260 getHeaderSearchOpts().Sysroot); 261 262 263 // Handle generating header include information, if requested. 264 if (DepOpts.ShowHeaderIncludes) 265 AttachHeaderIncludeGen(*PP); 266 if (!DepOpts.HeaderIncludeOutputFile.empty()) { 267 StringRef OutputPath = DepOpts.HeaderIncludeOutputFile; 268 if (OutputPath == "-") 269 OutputPath = ""; 270 AttachHeaderIncludeGen(*PP, /*ShowAllHeaders=*/true, OutputPath, 271 /*ShowDepth=*/false); 272 } 273 274 if (DepOpts.PrintShowIncludes) { 275 AttachHeaderIncludeGen(*PP, /*ShowAllHeaders=*/false, /*OutputPath=*/"", 276 /*ShowDepth=*/true, /*MSStyle=*/true); 277 } 278} 279 280// ASTContext 281 282void CompilerInstance::createASTContext() { 283 Preprocessor &PP = getPreprocessor(); 284 Context = new ASTContext(getLangOpts(), PP.getSourceManager(), 285 &getTarget(), PP.getIdentifierTable(), 286 PP.getSelectorTable(), PP.getBuiltinInfo(), 287 /*size_reserve=*/ 0); 288} 289 290// ExternalASTSource 291 292void CompilerInstance::createPCHExternalASTSource(StringRef Path, 293 bool DisablePCHValidation, 294 bool AllowPCHWithCompilerErrors, 295 void *DeserializationListener){ 296 OwningPtr<ExternalASTSource> Source; 297 bool Preamble = getPreprocessorOpts().PrecompiledPreambleBytes.first != 0; 298 Source.reset(createPCHExternalASTSource(Path, getHeaderSearchOpts().Sysroot, 299 DisablePCHValidation, 300 AllowPCHWithCompilerErrors, 301 getPreprocessor(), getASTContext(), 302 DeserializationListener, 303 Preamble, 304 getFrontendOpts().UseGlobalModuleIndex)); 305 ModuleManager = static_cast<ASTReader*>(Source.get()); 306 getASTContext().setExternalSource(Source); 307} 308 309ExternalASTSource * 310CompilerInstance::createPCHExternalASTSource(StringRef Path, 311 const std::string &Sysroot, 312 bool DisablePCHValidation, 313 bool AllowPCHWithCompilerErrors, 314 Preprocessor &PP, 315 ASTContext &Context, 316 void *DeserializationListener, 317 bool Preamble, 318 bool UseGlobalModuleIndex) { 319 OwningPtr<ASTReader> Reader; 320 Reader.reset(new ASTReader(PP, Context, 321 Sysroot.empty() ? "" : Sysroot.c_str(), 322 DisablePCHValidation, 323 AllowPCHWithCompilerErrors, 324 UseGlobalModuleIndex)); 325 326 Reader->setDeserializationListener( 327 static_cast<ASTDeserializationListener *>(DeserializationListener)); 328 switch (Reader->ReadAST(Path, 329 Preamble ? serialization::MK_Preamble 330 : serialization::MK_PCH, 331 SourceLocation(), 332 ASTReader::ARR_None)) { 333 case ASTReader::Success: 334 // Set the predefines buffer as suggested by the PCH reader. Typically, the 335 // predefines buffer will be empty. 336 PP.setPredefines(Reader->getSuggestedPredefines()); 337 return Reader.take(); 338 339 case ASTReader::Failure: 340 // Unrecoverable failure: don't even try to process the input file. 341 break; 342 343 case ASTReader::Missing: 344 case ASTReader::OutOfDate: 345 case ASTReader::VersionMismatch: 346 case ASTReader::ConfigurationMismatch: 347 case ASTReader::HadErrors: 348 // No suitable PCH file could be found. Return an error. 349 break; 350 } 351 352 return 0; 353} 354 355// Code Completion 356 357static bool EnableCodeCompletion(Preprocessor &PP, 358 const std::string &Filename, 359 unsigned Line, 360 unsigned Column) { 361 // Tell the source manager to chop off the given file at a specific 362 // line and column. 363 const FileEntry *Entry = PP.getFileManager().getFile(Filename); 364 if (!Entry) { 365 PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file) 366 << Filename; 367 return true; 368 } 369 370 // Truncate the named file at the given line/column. 371 PP.SetCodeCompletionPoint(Entry, Line, Column); 372 return false; 373} 374 375void CompilerInstance::createCodeCompletionConsumer() { 376 const ParsedSourceLocation &Loc = getFrontendOpts().CodeCompletionAt; 377 if (!CompletionConsumer) { 378 setCodeCompletionConsumer( 379 createCodeCompletionConsumer(getPreprocessor(), 380 Loc.FileName, Loc.Line, Loc.Column, 381 getFrontendOpts().CodeCompleteOpts, 382 llvm::outs())); 383 if (!CompletionConsumer) 384 return; 385 } else if (EnableCodeCompletion(getPreprocessor(), Loc.FileName, 386 Loc.Line, Loc.Column)) { 387 setCodeCompletionConsumer(0); 388 return; 389 } 390 391 if (CompletionConsumer->isOutputBinary() && 392 llvm::sys::ChangeStdoutToBinary()) { 393 getPreprocessor().getDiagnostics().Report(diag::err_fe_stdout_binary); 394 setCodeCompletionConsumer(0); 395 } 396} 397 398void CompilerInstance::createFrontendTimer() { 399 FrontendTimer.reset(new llvm::Timer("Clang front-end timer")); 400} 401 402CodeCompleteConsumer * 403CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP, 404 const std::string &Filename, 405 unsigned Line, 406 unsigned Column, 407 const CodeCompleteOptions &Opts, 408 raw_ostream &OS) { 409 if (EnableCodeCompletion(PP, Filename, Line, Column)) 410 return 0; 411 412 // Set up the creation routine for code-completion. 413 return new PrintingCodeCompleteConsumer(Opts, OS); 414} 415 416void CompilerInstance::createSema(TranslationUnitKind TUKind, 417 CodeCompleteConsumer *CompletionConsumer) { 418 TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(), 419 TUKind, CompletionConsumer)); 420} 421 422// Output Files 423 424void CompilerInstance::addOutputFile(const OutputFile &OutFile) { 425 assert(OutFile.OS && "Attempt to add empty stream to output list!"); 426 OutputFiles.push_back(OutFile); 427} 428 429void CompilerInstance::clearOutputFiles(bool EraseFiles) { 430 for (std::list<OutputFile>::iterator 431 it = OutputFiles.begin(), ie = OutputFiles.end(); it != ie; ++it) { 432 delete it->OS; 433 if (!it->TempFilename.empty()) { 434 if (EraseFiles) { 435 bool existed; 436 llvm::sys::fs::remove(it->TempFilename, existed); 437 } else { 438 SmallString<128> NewOutFile(it->Filename); 439 440 // If '-working-directory' was passed, the output filename should be 441 // relative to that. 442 FileMgr->FixupRelativePath(NewOutFile); 443 if (llvm::error_code ec = llvm::sys::fs::rename(it->TempFilename, 444 NewOutFile.str())) { 445 getDiagnostics().Report(diag::err_unable_to_rename_temp) 446 << it->TempFilename << it->Filename << ec.message(); 447 448 bool existed; 449 llvm::sys::fs::remove(it->TempFilename, existed); 450 } 451 } 452 } else if (!it->Filename.empty() && EraseFiles) 453 llvm::sys::fs::remove(it->Filename); 454 455 } 456 OutputFiles.clear(); 457} 458 459llvm::raw_fd_ostream * 460CompilerInstance::createDefaultOutputFile(bool Binary, 461 StringRef InFile, 462 StringRef Extension) { 463 return createOutputFile(getFrontendOpts().OutputFile, Binary, 464 /*RemoveFileOnSignal=*/true, InFile, Extension, 465 /*UseTemporary=*/true); 466} 467 468llvm::raw_fd_ostream * 469CompilerInstance::createOutputFile(StringRef OutputPath, 470 bool Binary, bool RemoveFileOnSignal, 471 StringRef InFile, 472 StringRef Extension, 473 bool UseTemporary, 474 bool CreateMissingDirectories) { 475 std::string Error, OutputPathName, TempPathName; 476 llvm::raw_fd_ostream *OS = createOutputFile(OutputPath, Error, Binary, 477 RemoveFileOnSignal, 478 InFile, Extension, 479 UseTemporary, 480 CreateMissingDirectories, 481 &OutputPathName, 482 &TempPathName); 483 if (!OS) { 484 getDiagnostics().Report(diag::err_fe_unable_to_open_output) 485 << OutputPath << Error; 486 return 0; 487 } 488 489 // Add the output file -- but don't try to remove "-", since this means we are 490 // using stdin. 491 addOutputFile(OutputFile((OutputPathName != "-") ? OutputPathName : "", 492 TempPathName, OS)); 493 494 return OS; 495} 496 497llvm::raw_fd_ostream * 498CompilerInstance::createOutputFile(StringRef OutputPath, 499 std::string &Error, 500 bool Binary, 501 bool RemoveFileOnSignal, 502 StringRef InFile, 503 StringRef Extension, 504 bool UseTemporary, 505 bool CreateMissingDirectories, 506 std::string *ResultPathName, 507 std::string *TempPathName) { 508 assert((!CreateMissingDirectories || UseTemporary) && 509 "CreateMissingDirectories is only allowed when using temporary files"); 510 511 std::string OutFile, TempFile; 512 if (!OutputPath.empty()) { 513 OutFile = OutputPath; 514 } else if (InFile == "-") { 515 OutFile = "-"; 516 } else if (!Extension.empty()) { 517 SmallString<128> Path(InFile); 518 llvm::sys::path::replace_extension(Path, Extension); 519 OutFile = Path.str(); 520 } else { 521 OutFile = "-"; 522 } 523 524 OwningPtr<llvm::raw_fd_ostream> OS; 525 std::string OSFile; 526 527 if (UseTemporary) { 528 if (OutFile == "-") 529 UseTemporary = false; 530 else { 531 llvm::sys::fs::file_status Status; 532 llvm::sys::fs::status(OutputPath, Status); 533 if (llvm::sys::fs::exists(Status)) { 534 // Fail early if we can't write to the final destination. 535 if (!llvm::sys::fs::can_write(OutputPath)) 536 return 0; 537 538 // Don't use a temporary if the output is a special file. This handles 539 // things like '-o /dev/null' 540 if (!llvm::sys::fs::is_regular_file(Status)) 541 UseTemporary = false; 542 } 543 } 544 } 545 546 if (UseTemporary) { 547 // Create a temporary file. 548 SmallString<128> TempPath; 549 TempPath = OutFile; 550 TempPath += "-%%%%%%%%"; 551 int fd; 552 llvm::error_code EC = 553 llvm::sys::fs::createUniqueFile(TempPath.str(), fd, TempPath); 554 555 if (CreateMissingDirectories && 556 EC == llvm::errc::no_such_file_or_directory) { 557 StringRef Parent = llvm::sys::path::parent_path(OutputPath); 558 EC = llvm::sys::fs::create_directories(Parent); 559 if (!EC) { 560 EC = llvm::sys::fs::createUniqueFile(TempPath.str(), fd, TempPath); 561 } 562 } 563 564 if (!EC) { 565 OS.reset(new llvm::raw_fd_ostream(fd, /*shouldClose=*/true)); 566 OSFile = TempFile = TempPath.str(); 567 } 568 // If we failed to create the temporary, fallback to writing to the file 569 // directly. This handles the corner case where we cannot write to the 570 // directory, but can write to the file. 571 } 572 573 if (!OS) { 574 OSFile = OutFile; 575 OS.reset(new llvm::raw_fd_ostream( 576 OSFile.c_str(), Error, 577 (Binary ? llvm::sys::fs::F_Binary : llvm::sys::fs::F_None))); 578 if (!Error.empty()) 579 return 0; 580 } 581 582 // Make sure the out stream file gets removed if we crash. 583 if (RemoveFileOnSignal) 584 llvm::sys::RemoveFileOnSignal(OSFile); 585 586 if (ResultPathName) 587 *ResultPathName = OutFile; 588 if (TempPathName) 589 *TempPathName = TempFile; 590 591 return OS.take(); 592} 593 594// Initialization Utilities 595 596bool CompilerInstance::InitializeSourceManager(const FrontendInputFile &Input){ 597 return InitializeSourceManager(Input, getDiagnostics(), 598 getFileManager(), getSourceManager(), 599 getFrontendOpts()); 600} 601 602bool CompilerInstance::InitializeSourceManager(const FrontendInputFile &Input, 603 DiagnosticsEngine &Diags, 604 FileManager &FileMgr, 605 SourceManager &SourceMgr, 606 const FrontendOptions &Opts) { 607 SrcMgr::CharacteristicKind 608 Kind = Input.isSystem() ? SrcMgr::C_System : SrcMgr::C_User; 609 610 if (Input.isBuffer()) { 611 SourceMgr.createMainFileIDForMemBuffer(Input.getBuffer(), Kind); 612 assert(!SourceMgr.getMainFileID().isInvalid() && 613 "Couldn't establish MainFileID!"); 614 return true; 615 } 616 617 StringRef InputFile = Input.getFile(); 618 619 // Figure out where to get and map in the main file. 620 if (InputFile != "-") { 621 const FileEntry *File = FileMgr.getFile(InputFile); 622 if (!File) { 623 Diags.Report(diag::err_fe_error_reading) << InputFile; 624 return false; 625 } 626 627 // The natural SourceManager infrastructure can't currently handle named 628 // pipes, but we would at least like to accept them for the main 629 // file. Detect them here, read them with the more generic MemoryBuffer 630 // function, and simply override their contents as we do for STDIN. 631 if (File->isNamedPipe()) { 632 OwningPtr<llvm::MemoryBuffer> MB; 633 if (llvm::error_code ec = llvm::MemoryBuffer::getFile(InputFile, MB)) { 634 Diags.Report(diag::err_cannot_open_file) << InputFile << ec.message(); 635 return false; 636 } 637 638 // Create a new virtual file that will have the correct size. 639 File = FileMgr.getVirtualFile(InputFile, MB->getBufferSize(), 0); 640 SourceMgr.overrideFileContents(File, MB.take()); 641 } 642 643 SourceMgr.createMainFileID(File, Kind); 644 } else { 645 OwningPtr<llvm::MemoryBuffer> SB; 646 if (llvm::MemoryBuffer::getSTDIN(SB)) { 647 // FIXME: Give ec.message() in this diag. 648 Diags.Report(diag::err_fe_error_reading_stdin); 649 return false; 650 } 651 const FileEntry *File = FileMgr.getVirtualFile(SB->getBufferIdentifier(), 652 SB->getBufferSize(), 0); 653 SourceMgr.createMainFileID(File, Kind); 654 SourceMgr.overrideFileContents(File, SB.take()); 655 } 656 657 assert(!SourceMgr.getMainFileID().isInvalid() && 658 "Couldn't establish MainFileID!"); 659 return true; 660} 661 662// High-Level Operations 663 664bool CompilerInstance::ExecuteAction(FrontendAction &Act) { 665 assert(hasDiagnostics() && "Diagnostics engine is not initialized!"); 666 assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!"); 667 assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!"); 668 669 // FIXME: Take this as an argument, once all the APIs we used have moved to 670 // taking it as an input instead of hard-coding llvm::errs. 671 raw_ostream &OS = llvm::errs(); 672 673 // Create the target instance. 674 setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), &getTargetOpts())); 675 if (!hasTarget()) 676 return false; 677 678 // Inform the target of the language options. 679 // 680 // FIXME: We shouldn't need to do this, the target should be immutable once 681 // created. This complexity should be lifted elsewhere. 682 getTarget().setForcedLangOptions(getLangOpts()); 683 684 // rewriter project will change target built-in bool type from its default. 685 if (getFrontendOpts().ProgramAction == frontend::RewriteObjC) 686 getTarget().noSignedCharForObjCBool(); 687 688 // Validate/process some options. 689 if (getHeaderSearchOpts().Verbose) 690 OS << "clang -cc1 version " CLANG_VERSION_STRING 691 << " based upon " << PACKAGE_STRING 692 << " default target " << llvm::sys::getDefaultTargetTriple() << "\n"; 693 694 if (getFrontendOpts().ShowTimers) 695 createFrontendTimer(); 696 697 if (getFrontendOpts().ShowStats) 698 llvm::EnableStatistics(); 699 700 for (unsigned i = 0, e = getFrontendOpts().Inputs.size(); i != e; ++i) { 701 // Reset the ID tables if we are reusing the SourceManager. 702 if (hasSourceManager()) 703 getSourceManager().clearIDTables(); 704 705 if (Act.BeginSourceFile(*this, getFrontendOpts().Inputs[i])) { 706 Act.Execute(); 707 Act.EndSourceFile(); 708 } 709 } 710 711 // Notify the diagnostic client that all files were processed. 712 getDiagnostics().getClient()->finish(); 713 714 if (getDiagnosticOpts().ShowCarets) { 715 // We can have multiple diagnostics sharing one diagnostic client. 716 // Get the total number of warnings/errors from the client. 717 unsigned NumWarnings = getDiagnostics().getClient()->getNumWarnings(); 718 unsigned NumErrors = getDiagnostics().getClient()->getNumErrors(); 719 720 if (NumWarnings) 721 OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s"); 722 if (NumWarnings && NumErrors) 723 OS << " and "; 724 if (NumErrors) 725 OS << NumErrors << " error" << (NumErrors == 1 ? "" : "s"); 726 if (NumWarnings || NumErrors) 727 OS << " generated.\n"; 728 } 729 730 if (getFrontendOpts().ShowStats && hasFileManager()) { 731 getFileManager().PrintStats(); 732 OS << "\n"; 733 } 734 735 return !getDiagnostics().getClient()->getNumErrors(); 736} 737 738/// \brief Determine the appropriate source input kind based on language 739/// options. 740static InputKind getSourceInputKindFromOptions(const LangOptions &LangOpts) { 741 if (LangOpts.OpenCL) 742 return IK_OpenCL; 743 if (LangOpts.CUDA) 744 return IK_CUDA; 745 if (LangOpts.ObjC1) 746 return LangOpts.CPlusPlus? IK_ObjCXX : IK_ObjC; 747 return LangOpts.CPlusPlus? IK_CXX : IK_C; 748} 749 750namespace { 751 struct CompileModuleMapData { 752 CompilerInstance &Instance; 753 GenerateModuleAction &CreateModuleAction; 754 }; 755} 756 757/// \brief Helper function that executes the module-generating action under 758/// a crash recovery context. 759static void doCompileMapModule(void *UserData) { 760 CompileModuleMapData &Data 761 = *reinterpret_cast<CompileModuleMapData *>(UserData); 762 Data.Instance.ExecuteAction(Data.CreateModuleAction); 763} 764 765namespace { 766 /// \brief Function object that checks with the given macro definition should 767 /// be removed, because it is one of the ignored macros. 768 class RemoveIgnoredMacro { 769 const HeaderSearchOptions &HSOpts; 770 771 public: 772 explicit RemoveIgnoredMacro(const HeaderSearchOptions &HSOpts) 773 : HSOpts(HSOpts) { } 774 775 bool operator()(const std::pair<std::string, bool> &def) const { 776 StringRef MacroDef = def.first; 777 return HSOpts.ModulesIgnoreMacros.count(MacroDef.split('=').first) > 0; 778 } 779 }; 780} 781 782/// \brief Compile a module file for the given module, using the options 783/// provided by the importing compiler instance. 784static void compileModule(CompilerInstance &ImportingInstance, 785 SourceLocation ImportLoc, 786 Module *Module, 787 StringRef ModuleFileName) { 788 // FIXME: have LockFileManager return an error_code so that we can 789 // avoid the mkdir when the directory already exists. 790 StringRef Dir = llvm::sys::path::parent_path(ModuleFileName); 791 llvm::sys::fs::create_directories(Dir); 792 793 llvm::LockFileManager Locked(ModuleFileName); 794 switch (Locked) { 795 case llvm::LockFileManager::LFS_Error: 796 return; 797 798 case llvm::LockFileManager::LFS_Owned: 799 // We're responsible for building the module ourselves. Do so below. 800 break; 801 802 case llvm::LockFileManager::LFS_Shared: 803 // Someone else is responsible for building the module. Wait for them to 804 // finish. 805 Locked.waitForUnlock(); 806 return; 807 } 808 809 ModuleMap &ModMap 810 = ImportingInstance.getPreprocessor().getHeaderSearchInfo().getModuleMap(); 811 812 // Construct a compiler invocation for creating this module. 813 IntrusiveRefCntPtr<CompilerInvocation> Invocation 814 (new CompilerInvocation(ImportingInstance.getInvocation())); 815 816 PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts(); 817 818 // For any options that aren't intended to affect how a module is built, 819 // reset them to their default values. 820 Invocation->getLangOpts()->resetNonModularOptions(); 821 PPOpts.resetNonModularOptions(); 822 823 // Remove any macro definitions that are explicitly ignored by the module. 824 // They aren't supposed to affect how the module is built anyway. 825 const HeaderSearchOptions &HSOpts = Invocation->getHeaderSearchOpts(); 826 PPOpts.Macros.erase(std::remove_if(PPOpts.Macros.begin(), PPOpts.Macros.end(), 827 RemoveIgnoredMacro(HSOpts)), 828 PPOpts.Macros.end()); 829 830 831 // Note the name of the module we're building. 832 Invocation->getLangOpts()->CurrentModule = Module->getTopLevelModuleName(); 833 834 // Make sure that the failed-module structure has been allocated in 835 // the importing instance, and propagate the pointer to the newly-created 836 // instance. 837 PreprocessorOptions &ImportingPPOpts 838 = ImportingInstance.getInvocation().getPreprocessorOpts(); 839 if (!ImportingPPOpts.FailedModules) 840 ImportingPPOpts.FailedModules = new PreprocessorOptions::FailedModulesSet; 841 PPOpts.FailedModules = ImportingPPOpts.FailedModules; 842 843 // If there is a module map file, build the module using the module map. 844 // Set up the inputs/outputs so that we build the module from its umbrella 845 // header. 846 FrontendOptions &FrontendOpts = Invocation->getFrontendOpts(); 847 FrontendOpts.OutputFile = ModuleFileName.str(); 848 FrontendOpts.DisableFree = false; 849 FrontendOpts.GenerateGlobalModuleIndex = false; 850 FrontendOpts.Inputs.clear(); 851 InputKind IK = getSourceInputKindFromOptions(*Invocation->getLangOpts()); 852 853 // Get or create the module map that we'll use to build this module. 854 SmallString<128> TempModuleMapFileName; 855 if (const FileEntry *ModuleMapFile 856 = ModMap.getContainingModuleMapFile(Module)) { 857 // Use the module map where this module resides. 858 FrontendOpts.Inputs.push_back(FrontendInputFile(ModuleMapFile->getName(), 859 IK)); 860 } else { 861 // Create a temporary module map file. 862 int FD; 863 if (llvm::sys::fs::createTemporaryFile(Module->Name, "map", FD, 864 TempModuleMapFileName)) { 865 ImportingInstance.getDiagnostics().Report(diag::err_module_map_temp_file) 866 << TempModuleMapFileName; 867 return; 868 } 869 // Print the module map to this file. 870 llvm::raw_fd_ostream OS(FD, /*shouldClose=*/true); 871 Module->print(OS); 872 FrontendOpts.Inputs.push_back( 873 FrontendInputFile(TempModuleMapFileName.str().str(), IK)); 874 } 875 876 // Don't free the remapped file buffers; they are owned by our caller. 877 PPOpts.RetainRemappedFileBuffers = true; 878 879 Invocation->getDiagnosticOpts().VerifyDiagnostics = 0; 880 assert(ImportingInstance.getInvocation().getModuleHash() == 881 Invocation->getModuleHash() && "Module hash mismatch!"); 882 883 // Construct a compiler instance that will be used to actually create the 884 // module. 885 CompilerInstance Instance; 886 Instance.setInvocation(&*Invocation); 887 888 Instance.createDiagnostics(new ForwardingDiagnosticConsumer( 889 ImportingInstance.getDiagnosticClient()), 890 /*ShouldOwnClient=*/true); 891 892 // Note that this module is part of the module build stack, so that we 893 // can detect cycles in the module graph. 894 Instance.createFileManager(); // FIXME: Adopt file manager from importer? 895 Instance.createSourceManager(Instance.getFileManager()); 896 SourceManager &SourceMgr = Instance.getSourceManager(); 897 SourceMgr.setModuleBuildStack( 898 ImportingInstance.getSourceManager().getModuleBuildStack()); 899 SourceMgr.pushModuleBuildStack(Module->getTopLevelModuleName(), 900 FullSourceLoc(ImportLoc, ImportingInstance.getSourceManager())); 901 902 903 // Construct a module-generating action. 904 GenerateModuleAction CreateModuleAction(Module->IsSystem); 905 906 // Execute the action to actually build the module in-place. Use a separate 907 // thread so that we get a stack large enough. 908 const unsigned ThreadStackSize = 8 << 20; 909 llvm::CrashRecoveryContext CRC; 910 CompileModuleMapData Data = { Instance, CreateModuleAction }; 911 CRC.RunSafelyOnThread(&doCompileMapModule, &Data, ThreadStackSize); 912 913 914 // Delete the temporary module map file. 915 // FIXME: Even though we're executing under crash protection, it would still 916 // be nice to do this with RemoveFileOnSignal when we can. However, that 917 // doesn't make sense for all clients, so clean this up manually. 918 Instance.clearOutputFiles(/*EraseFiles=*/true); 919 if (!TempModuleMapFileName.empty()) 920 llvm::sys::fs::remove(TempModuleMapFileName.str()); 921 922 // We've rebuilt a module. If we're allowed to generate or update the global 923 // module index, record that fact in the importing compiler instance. 924 if (ImportingInstance.getFrontendOpts().GenerateGlobalModuleIndex) { 925 ImportingInstance.setBuildGlobalModuleIndex(true); 926 } 927} 928 929/// \brief Diagnose differences between the current definition of the given 930/// configuration macro and the definition provided on the command line. 931static void checkConfigMacro(Preprocessor &PP, StringRef ConfigMacro, 932 Module *Mod, SourceLocation ImportLoc) { 933 IdentifierInfo *Id = PP.getIdentifierInfo(ConfigMacro); 934 SourceManager &SourceMgr = PP.getSourceManager(); 935 936 // If this identifier has never had a macro definition, then it could 937 // not have changed. 938 if (!Id->hadMacroDefinition()) 939 return; 940 941 // If this identifier does not currently have a macro definition, 942 // check whether it had one on the command line. 943 if (!Id->hasMacroDefinition()) { 944 MacroDirective::DefInfo LatestDef = 945 PP.getMacroDirectiveHistory(Id)->getDefinition(); 946 for (MacroDirective::DefInfo Def = LatestDef; Def; 947 Def = Def.getPreviousDefinition()) { 948 FileID FID = SourceMgr.getFileID(Def.getLocation()); 949 if (FID.isInvalid()) 950 continue; 951 952 // We only care about the predefines buffer. 953 if (FID != PP.getPredefinesFileID()) 954 continue; 955 956 // This macro was defined on the command line, then #undef'd later. 957 // Complain. 958 PP.Diag(ImportLoc, diag::warn_module_config_macro_undef) 959 << true << ConfigMacro << Mod->getFullModuleName(); 960 if (LatestDef.isUndefined()) 961 PP.Diag(LatestDef.getUndefLocation(), diag::note_module_def_undef_here) 962 << true; 963 return; 964 } 965 966 // Okay: no definition in the predefines buffer. 967 return; 968 } 969 970 // This identifier has a macro definition. Check whether we had a definition 971 // on the command line. 972 MacroDirective::DefInfo LatestDef = 973 PP.getMacroDirectiveHistory(Id)->getDefinition(); 974 MacroDirective::DefInfo PredefinedDef; 975 for (MacroDirective::DefInfo Def = LatestDef; Def; 976 Def = Def.getPreviousDefinition()) { 977 FileID FID = SourceMgr.getFileID(Def.getLocation()); 978 if (FID.isInvalid()) 979 continue; 980 981 // We only care about the predefines buffer. 982 if (FID != PP.getPredefinesFileID()) 983 continue; 984 985 PredefinedDef = Def; 986 break; 987 } 988 989 // If there was no definition for this macro in the predefines buffer, 990 // complain. 991 if (!PredefinedDef || 992 (!PredefinedDef.getLocation().isValid() && 993 PredefinedDef.getUndefLocation().isValid())) { 994 PP.Diag(ImportLoc, diag::warn_module_config_macro_undef) 995 << false << ConfigMacro << Mod->getFullModuleName(); 996 PP.Diag(LatestDef.getLocation(), diag::note_module_def_undef_here) 997 << false; 998 return; 999 } 1000 1001 // If the current macro definition is the same as the predefined macro 1002 // definition, it's okay. 1003 if (LatestDef.getMacroInfo() == PredefinedDef.getMacroInfo() || 1004 LatestDef.getMacroInfo()->isIdenticalTo(*PredefinedDef.getMacroInfo(),PP, 1005 /*Syntactically=*/true)) 1006 return; 1007 1008 // The macro definitions differ. 1009 PP.Diag(ImportLoc, diag::warn_module_config_macro_undef) 1010 << false << ConfigMacro << Mod->getFullModuleName(); 1011 PP.Diag(LatestDef.getLocation(), diag::note_module_def_undef_here) 1012 << false; 1013} 1014 1015/// \brief Write a new timestamp file with the given path. 1016static void writeTimestampFile(StringRef TimestampFile) { 1017 std::string ErrorInfo; 1018 llvm::raw_fd_ostream Out(TimestampFile.str().c_str(), ErrorInfo, 1019 llvm::sys::fs::F_Binary); 1020} 1021 1022/// \brief Prune the module cache of modules that haven't been accessed in 1023/// a long time. 1024static void pruneModuleCache(const HeaderSearchOptions &HSOpts) { 1025 struct stat StatBuf; 1026 llvm::SmallString<128> TimestampFile; 1027 TimestampFile = HSOpts.ModuleCachePath; 1028 llvm::sys::path::append(TimestampFile, "modules.timestamp"); 1029 1030 // Try to stat() the timestamp file. 1031 if (::stat(TimestampFile.c_str(), &StatBuf)) { 1032 // If the timestamp file wasn't there, create one now. 1033 if (errno == ENOENT) { 1034 writeTimestampFile(TimestampFile); 1035 } 1036 return; 1037 } 1038 1039 // Check whether the time stamp is older than our pruning interval. 1040 // If not, do nothing. 1041 time_t TimeStampModTime = StatBuf.st_mtime; 1042 time_t CurrentTime = time(0); 1043 if (CurrentTime - TimeStampModTime <= time_t(HSOpts.ModuleCachePruneInterval)) 1044 return; 1045 1046 // Write a new timestamp file so that nobody else attempts to prune. 1047 // There is a benign race condition here, if two Clang instances happen to 1048 // notice at the same time that the timestamp is out-of-date. 1049 writeTimestampFile(TimestampFile); 1050 1051 // Walk the entire module cache, looking for unused module files and module 1052 // indices. 1053 llvm::error_code EC; 1054 SmallString<128> ModuleCachePathNative; 1055 llvm::sys::path::native(HSOpts.ModuleCachePath, ModuleCachePathNative); 1056 for (llvm::sys::fs::directory_iterator 1057 Dir(ModuleCachePathNative.str(), EC), DirEnd; 1058 Dir != DirEnd && !EC; Dir.increment(EC)) { 1059 // If we don't have a directory, there's nothing to look into. 1060 if (!llvm::sys::fs::is_directory(Dir->path())) 1061 continue; 1062 1063 // Walk all of the files within this directory. 1064 bool RemovedAllFiles = true; 1065 for (llvm::sys::fs::directory_iterator File(Dir->path(), EC), FileEnd; 1066 File != FileEnd && !EC; File.increment(EC)) { 1067 // We only care about module and global module index files. 1068 if (llvm::sys::path::extension(File->path()) != ".pcm" && 1069 llvm::sys::path::filename(File->path()) != "modules.idx") { 1070 RemovedAllFiles = false; 1071 continue; 1072 } 1073 1074 // Look at this file. If we can't stat it, there's nothing interesting 1075 // there. 1076 if (::stat(File->path().c_str(), &StatBuf)) { 1077 RemovedAllFiles = false; 1078 continue; 1079 } 1080 1081 // If the file has been used recently enough, leave it there. 1082 time_t FileAccessTime = StatBuf.st_atime; 1083 if (CurrentTime - FileAccessTime <= 1084 time_t(HSOpts.ModuleCachePruneAfter)) { 1085 RemovedAllFiles = false; 1086 continue; 1087 } 1088 1089 // Remove the file. 1090 bool Existed; 1091 if (llvm::sys::fs::remove(File->path(), Existed) || !Existed) { 1092 RemovedAllFiles = false; 1093 } 1094 } 1095 1096 // If we removed all of the files in the directory, remove the directory 1097 // itself. 1098 if (RemovedAllFiles) { 1099 bool Existed; 1100 llvm::sys::fs::remove(Dir->path(), Existed); 1101 } 1102 } 1103} 1104 1105ModuleLoadResult 1106CompilerInstance::loadModule(SourceLocation ImportLoc, 1107 ModuleIdPath Path, 1108 Module::NameVisibilityKind Visibility, 1109 bool IsInclusionDirective) { 1110 // If we've already handled this import, just return the cached result. 1111 // This one-element cache is important to eliminate redundant diagnostics 1112 // when both the preprocessor and parser see the same import declaration. 1113 if (!ImportLoc.isInvalid() && LastModuleImportLoc == ImportLoc) { 1114 // Make the named module visible. 1115 if (LastModuleImportResult) 1116 ModuleManager->makeModuleVisible(LastModuleImportResult, Visibility, 1117 ImportLoc, /*Complain=*/false); 1118 return LastModuleImportResult; 1119 } 1120 1121 // Determine what file we're searching from. 1122 StringRef ModuleName = Path[0].first->getName(); 1123 SourceLocation ModuleNameLoc = Path[0].second; 1124 1125 clang::Module *Module = 0; 1126 1127 // If we don't already have information on this module, load the module now. 1128 llvm::DenseMap<const IdentifierInfo *, clang::Module *>::iterator Known 1129 = KnownModules.find(Path[0].first); 1130 if (Known != KnownModules.end()) { 1131 // Retrieve the cached top-level module. 1132 Module = Known->second; 1133 } else if (ModuleName == getLangOpts().CurrentModule) { 1134 // This is the module we're building. 1135 Module = PP->getHeaderSearchInfo().getModuleMap().findModule(ModuleName); 1136 Known = KnownModules.insert(std::make_pair(Path[0].first, Module)).first; 1137 } else { 1138 // Search for a module with the given name. 1139 Module = PP->getHeaderSearchInfo().lookupModule(ModuleName); 1140 std::string ModuleFileName; 1141 if (Module) { 1142 ModuleFileName = PP->getHeaderSearchInfo().getModuleFileName(Module); 1143 } else 1144 ModuleFileName = PP->getHeaderSearchInfo().getModuleFileName(ModuleName); 1145 1146 // If we don't already have an ASTReader, create one now. 1147 if (!ModuleManager) { 1148 if (!hasASTContext()) 1149 createASTContext(); 1150 1151 // If we're not recursively building a module, check whether we 1152 // need to prune the module cache. 1153 if (getSourceManager().getModuleBuildStack().empty() && 1154 getHeaderSearchOpts().ModuleCachePruneInterval > 0 && 1155 getHeaderSearchOpts().ModuleCachePruneAfter > 0) { 1156 pruneModuleCache(getHeaderSearchOpts()); 1157 } 1158 1159 std::string Sysroot = getHeaderSearchOpts().Sysroot; 1160 const PreprocessorOptions &PPOpts = getPreprocessorOpts(); 1161 ModuleManager = new ASTReader(getPreprocessor(), *Context, 1162 Sysroot.empty() ? "" : Sysroot.c_str(), 1163 PPOpts.DisablePCHValidation, 1164 /*AllowASTWithCompilerErrors=*/false, 1165 getFrontendOpts().UseGlobalModuleIndex); 1166 if (hasASTConsumer()) { 1167 ModuleManager->setDeserializationListener( 1168 getASTConsumer().GetASTDeserializationListener()); 1169 getASTContext().setASTMutationListener( 1170 getASTConsumer().GetASTMutationListener()); 1171 } 1172 OwningPtr<ExternalASTSource> Source; 1173 Source.reset(ModuleManager); 1174 getASTContext().setExternalSource(Source); 1175 if (hasSema()) 1176 ModuleManager->InitializeSema(getSema()); 1177 if (hasASTConsumer()) 1178 ModuleManager->StartTranslationUnit(&getASTConsumer()); 1179 } 1180 1181 // Try to load the module file. 1182 unsigned ARRFlags = ASTReader::ARR_OutOfDate | ASTReader::ARR_Missing; 1183 switch (ModuleManager->ReadAST(ModuleFileName, serialization::MK_Module, 1184 ImportLoc, ARRFlags)) { 1185 case ASTReader::Success: 1186 break; 1187 1188 case ASTReader::OutOfDate: { 1189 // The module file is out-of-date. Remove it, then rebuild it. 1190 bool Existed; 1191 llvm::sys::fs::remove(ModuleFileName, Existed); 1192 } 1193 // Fall through to build the module again. 1194 1195 case ASTReader::Missing: { 1196 // The module file is (now) missing. Build it. 1197 1198 // If we don't have a module, we don't know how to build the module file. 1199 // Complain and return. 1200 if (!Module) { 1201 getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_found) 1202 << ModuleName 1203 << SourceRange(ImportLoc, ModuleNameLoc); 1204 ModuleBuildFailed = true; 1205 return ModuleLoadResult(); 1206 } 1207 1208 // Check whether there is a cycle in the module graph. 1209 ModuleBuildStack ModPath = getSourceManager().getModuleBuildStack(); 1210 ModuleBuildStack::iterator Pos = ModPath.begin(), PosEnd = ModPath.end(); 1211 for (; Pos != PosEnd; ++Pos) { 1212 if (Pos->first == ModuleName) 1213 break; 1214 } 1215 1216 if (Pos != PosEnd) { 1217 SmallString<256> CyclePath; 1218 for (; Pos != PosEnd; ++Pos) { 1219 CyclePath += Pos->first; 1220 CyclePath += " -> "; 1221 } 1222 CyclePath += ModuleName; 1223 1224 getDiagnostics().Report(ModuleNameLoc, diag::err_module_cycle) 1225 << ModuleName << CyclePath; 1226 return ModuleLoadResult(); 1227 } 1228 1229 // Check whether we have already attempted to build this module (but 1230 // failed). 1231 if (getPreprocessorOpts().FailedModules && 1232 getPreprocessorOpts().FailedModules->hasAlreadyFailed(ModuleName)) { 1233 getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_built) 1234 << ModuleName 1235 << SourceRange(ImportLoc, ModuleNameLoc); 1236 ModuleBuildFailed = true; 1237 return ModuleLoadResult(); 1238 } 1239 1240 // Try to compile the module. 1241 compileModule(*this, ModuleNameLoc, Module, ModuleFileName); 1242 1243 // Try to read the module file, now that we've compiled it. 1244 ASTReader::ASTReadResult ReadResult 1245 = ModuleManager->ReadAST(ModuleFileName, 1246 serialization::MK_Module, ImportLoc, 1247 ASTReader::ARR_Missing); 1248 if (ReadResult != ASTReader::Success) { 1249 if (ReadResult == ASTReader::Missing) { 1250 getDiagnostics().Report(ModuleNameLoc, 1251 Module? diag::err_module_not_built 1252 : diag::err_module_not_found) 1253 << ModuleName 1254 << SourceRange(ImportLoc, ModuleNameLoc); 1255 } 1256 1257 if (getPreprocessorOpts().FailedModules) 1258 getPreprocessorOpts().FailedModules->addFailed(ModuleName); 1259 KnownModules[Path[0].first] = 0; 1260 ModuleBuildFailed = true; 1261 return ModuleLoadResult(); 1262 } 1263 1264 // Okay, we've rebuilt and now loaded the module. 1265 break; 1266 } 1267 1268 case ASTReader::VersionMismatch: 1269 case ASTReader::ConfigurationMismatch: 1270 case ASTReader::HadErrors: 1271 ModuleLoader::HadFatalFailure = true; 1272 // FIXME: The ASTReader will already have complained, but can we showhorn 1273 // that diagnostic information into a more useful form? 1274 KnownModules[Path[0].first] = 0; 1275 return ModuleLoadResult(); 1276 1277 case ASTReader::Failure: 1278 ModuleLoader::HadFatalFailure = true; 1279 // Already complained, but note now that we failed. 1280 KnownModules[Path[0].first] = 0; 1281 ModuleBuildFailed = true; 1282 return ModuleLoadResult(); 1283 } 1284 1285 if (!Module) { 1286 // If we loaded the module directly, without finding a module map first, 1287 // we'll have loaded the module's information from the module itself. 1288 Module = PP->getHeaderSearchInfo().getModuleMap() 1289 .findModule((Path[0].first->getName())); 1290 } 1291 1292 // Cache the result of this top-level module lookup for later. 1293 Known = KnownModules.insert(std::make_pair(Path[0].first, Module)).first; 1294 } 1295 1296 // If we never found the module, fail. 1297 if (!Module) 1298 return ModuleLoadResult(); 1299 1300 // Verify that the rest of the module path actually corresponds to 1301 // a submodule. 1302 if (Path.size() > 1) { 1303 for (unsigned I = 1, N = Path.size(); I != N; ++I) { 1304 StringRef Name = Path[I].first->getName(); 1305 clang::Module *Sub = Module->findSubmodule(Name); 1306 1307 if (!Sub) { 1308 // Attempt to perform typo correction to find a module name that works. 1309 SmallVector<StringRef, 2> Best; 1310 unsigned BestEditDistance = (std::numeric_limits<unsigned>::max)(); 1311 1312 for (clang::Module::submodule_iterator J = Module->submodule_begin(), 1313 JEnd = Module->submodule_end(); 1314 J != JEnd; ++J) { 1315 unsigned ED = Name.edit_distance((*J)->Name, 1316 /*AllowReplacements=*/true, 1317 BestEditDistance); 1318 if (ED <= BestEditDistance) { 1319 if (ED < BestEditDistance) { 1320 Best.clear(); 1321 BestEditDistance = ED; 1322 } 1323 1324 Best.push_back((*J)->Name); 1325 } 1326 } 1327 1328 // If there was a clear winner, user it. 1329 if (Best.size() == 1) { 1330 getDiagnostics().Report(Path[I].second, 1331 diag::err_no_submodule_suggest) 1332 << Path[I].first << Module->getFullModuleName() << Best[0] 1333 << SourceRange(Path[0].second, Path[I-1].second) 1334 << FixItHint::CreateReplacement(SourceRange(Path[I].second), 1335 Best[0]); 1336 1337 Sub = Module->findSubmodule(Best[0]); 1338 } 1339 } 1340 1341 if (!Sub) { 1342 // No submodule by this name. Complain, and don't look for further 1343 // submodules. 1344 getDiagnostics().Report(Path[I].second, diag::err_no_submodule) 1345 << Path[I].first << Module->getFullModuleName() 1346 << SourceRange(Path[0].second, Path[I-1].second); 1347 break; 1348 } 1349 1350 Module = Sub; 1351 } 1352 } 1353 1354 // Make the named module visible, if it's not already part of the module 1355 // we are parsing. 1356 if (ModuleName != getLangOpts().CurrentModule) { 1357 if (!Module->IsFromModuleFile) { 1358 // We have an umbrella header or directory that doesn't actually include 1359 // all of the headers within the directory it covers. Complain about 1360 // this missing submodule and recover by forgetting that we ever saw 1361 // this submodule. 1362 // FIXME: Should we detect this at module load time? It seems fairly 1363 // expensive (and rare). 1364 getDiagnostics().Report(ImportLoc, diag::warn_missing_submodule) 1365 << Module->getFullModuleName() 1366 << SourceRange(Path.front().second, Path.back().second); 1367 1368 return ModuleLoadResult(0, true); 1369 } 1370 1371 // Check whether this module is available. 1372 StringRef Feature; 1373 if (!Module->isAvailable(getLangOpts(), getTarget(), Feature)) { 1374 getDiagnostics().Report(ImportLoc, diag::err_module_unavailable) 1375 << Module->getFullModuleName() 1376 << Feature 1377 << SourceRange(Path.front().second, Path.back().second); 1378 LastModuleImportLoc = ImportLoc; 1379 LastModuleImportResult = ModuleLoadResult(); 1380 return ModuleLoadResult(); 1381 } 1382 1383 ModuleManager->makeModuleVisible(Module, Visibility, ImportLoc, 1384 /*Complain=*/true); 1385 } 1386 1387 // Check for any configuration macros that have changed. 1388 clang::Module *TopModule = Module->getTopLevelModule(); 1389 for (unsigned I = 0, N = TopModule->ConfigMacros.size(); I != N; ++I) { 1390 checkConfigMacro(getPreprocessor(), TopModule->ConfigMacros[I], 1391 Module, ImportLoc); 1392 } 1393 1394 // If this module import was due to an inclusion directive, create an 1395 // implicit import declaration to capture it in the AST. 1396 if (IsInclusionDirective && hasASTContext()) { 1397 TranslationUnitDecl *TU = getASTContext().getTranslationUnitDecl(); 1398 ImportDecl *ImportD = ImportDecl::CreateImplicit(getASTContext(), TU, 1399 ImportLoc, Module, 1400 Path.back().second); 1401 TU->addDecl(ImportD); 1402 if (Consumer) 1403 Consumer->HandleImplicitImportDecl(ImportD); 1404 } 1405 1406 LastModuleImportLoc = ImportLoc; 1407 LastModuleImportResult = ModuleLoadResult(Module, false); 1408 return LastModuleImportResult; 1409} 1410 1411void CompilerInstance::makeModuleVisible(Module *Mod, 1412 Module::NameVisibilityKind Visibility, 1413 SourceLocation ImportLoc, 1414 bool Complain){ 1415 ModuleManager->makeModuleVisible(Mod, Visibility, ImportLoc, Complain); 1416} 1417 1418