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