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