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