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