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