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