xref: /llvm-project/clang/lib/Frontend/CompilerInstance.cpp (revision 514b636adab8c9934f82ba9a4beb600e2b3072e7)
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/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/Lex/HeaderSearch.h"
20 #include "clang/Lex/Preprocessor.h"
21 #include "clang/Lex/PTHManager.h"
22 #include "clang/Frontend/ChainedDiagnosticConsumer.h"
23 #include "clang/Frontend/FrontendAction.h"
24 #include "clang/Frontend/FrontendActions.h"
25 #include "clang/Frontend/FrontendDiagnostic.h"
26 #include "clang/Frontend/LogDiagnosticPrinter.h"
27 #include "clang/Frontend/SerializedDiagnosticPrinter.h"
28 #include "clang/Frontend/TextDiagnosticPrinter.h"
29 #include "clang/Frontend/VerifyDiagnosticConsumer.h"
30 #include "clang/Frontend/Utils.h"
31 #include "clang/Serialization/ASTReader.h"
32 #include "clang/Sema/CodeCompleteConsumer.h"
33 #include "llvm/Support/FileSystem.h"
34 #include "llvm/Support/MemoryBuffer.h"
35 #include "llvm/Support/raw_ostream.h"
36 #include "llvm/ADT/Statistic.h"
37 #include "llvm/Support/Timer.h"
38 #include "llvm/Support/Host.h"
39 #include "llvm/Support/Path.h"
40 #include "llvm/Support/Program.h"
41 #include "llvm/Support/Signals.h"
42 #include "llvm/Support/system_error.h"
43 #include "llvm/Support/CrashRecoveryContext.h"
44 #include "llvm/Config/config.h"
45 
46 // Support for FileLockManager
47 #include <fstream>
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 
51 #if LLVM_ON_WIN32
52 #include <windows.h>
53 #endif
54 #if LLVM_ON_UNIX
55 #include <unistd.h>
56 #endif
57 
58 using namespace clang;
59 
60 CompilerInstance::CompilerInstance()
61   : Invocation(new CompilerInvocation()), ModuleManager(0) {
62 }
63 
64 CompilerInstance::~CompilerInstance() {
65 }
66 
67 void CompilerInstance::setInvocation(CompilerInvocation *Value) {
68   Invocation = Value;
69 }
70 
71 void CompilerInstance::setDiagnostics(DiagnosticsEngine *Value) {
72   Diagnostics = Value;
73 }
74 
75 void CompilerInstance::setTarget(TargetInfo *Value) {
76   Target = Value;
77 }
78 
79 void CompilerInstance::setFileManager(FileManager *Value) {
80   FileMgr = Value;
81 }
82 
83 void CompilerInstance::setSourceManager(SourceManager *Value) {
84   SourceMgr = Value;
85 }
86 
87 void CompilerInstance::setPreprocessor(Preprocessor *Value) { PP = Value; }
88 
89 void CompilerInstance::setASTContext(ASTContext *Value) { Context = Value; }
90 
91 void CompilerInstance::setSema(Sema *S) {
92   TheSema.reset(S);
93 }
94 
95 void CompilerInstance::setASTConsumer(ASTConsumer *Value) {
96   Consumer.reset(Value);
97 }
98 
99 void CompilerInstance::setCodeCompletionConsumer(CodeCompleteConsumer *Value) {
100   CompletionConsumer.reset(Value);
101 }
102 
103 // Diagnostics
104 static void SetUpBuildDumpLog(const DiagnosticOptions &DiagOpts,
105                               unsigned argc, const char* const *argv,
106                               DiagnosticsEngine &Diags) {
107   std::string ErrorInfo;
108   llvm::OwningPtr<raw_ostream> OS(
109     new llvm::raw_fd_ostream(DiagOpts.DumpBuildInformation.c_str(), ErrorInfo));
110   if (!ErrorInfo.empty()) {
111     Diags.Report(diag::err_fe_unable_to_open_logfile)
112                  << DiagOpts.DumpBuildInformation << ErrorInfo;
113     return;
114   }
115 
116   (*OS) << "clang -cc1 command line arguments: ";
117   for (unsigned i = 0; i != argc; ++i)
118     (*OS) << argv[i] << ' ';
119   (*OS) << '\n';
120 
121   // Chain in a diagnostic client which will log the diagnostics.
122   DiagnosticConsumer *Logger =
123     new TextDiagnosticPrinter(*OS.take(), DiagOpts, /*OwnsOutputStream=*/true);
124   Diags.setClient(new ChainedDiagnosticConsumer(Diags.takeClient(), Logger));
125 }
126 
127 static void SetUpDiagnosticLog(const DiagnosticOptions &DiagOpts,
128                                const CodeGenOptions *CodeGenOpts,
129                                DiagnosticsEngine &Diags) {
130   std::string ErrorInfo;
131   bool OwnsStream = false;
132   raw_ostream *OS = &llvm::errs();
133   if (DiagOpts.DiagnosticLogFile != "-") {
134     // Create the output stream.
135     llvm::raw_fd_ostream *FileOS(
136       new llvm::raw_fd_ostream(DiagOpts.DiagnosticLogFile.c_str(),
137                                ErrorInfo, llvm::raw_fd_ostream::F_Append));
138     if (!ErrorInfo.empty()) {
139       Diags.Report(diag::warn_fe_cc_log_diagnostics_failure)
140         << DiagOpts.DumpBuildInformation << ErrorInfo;
141     } else {
142       FileOS->SetUnbuffered();
143       FileOS->SetUseAtomicWrites(true);
144       OS = FileOS;
145       OwnsStream = true;
146     }
147   }
148 
149   // Chain in the diagnostic client which will log the diagnostics.
150   LogDiagnosticPrinter *Logger = new LogDiagnosticPrinter(*OS, DiagOpts,
151                                                           OwnsStream);
152   if (CodeGenOpts)
153     Logger->setDwarfDebugFlags(CodeGenOpts->DwarfDebugFlags);
154   Diags.setClient(new ChainedDiagnosticConsumer(Diags.takeClient(), Logger));
155 }
156 
157 static void SetupSerializedDiagnostics(const DiagnosticOptions &DiagOpts,
158                                        DiagnosticsEngine &Diags,
159                                        StringRef OutputFile) {
160   std::string ErrorInfo;
161   llvm::OwningPtr<llvm::raw_fd_ostream> OS;
162   OS.reset(new llvm::raw_fd_ostream(OutputFile.str().c_str(), ErrorInfo,
163                                     llvm::raw_fd_ostream::F_Binary));
164 
165   if (!ErrorInfo.empty()) {
166     Diags.Report(diag::warn_fe_serialized_diag_failure)
167       << OutputFile << ErrorInfo;
168     return;
169   }
170 
171   DiagnosticConsumer *SerializedConsumer =
172     clang::serialized_diags::create(OS.take(), Diags);
173 
174 
175   Diags.setClient(new ChainedDiagnosticConsumer(Diags.takeClient(),
176                                                 SerializedConsumer));
177 }
178 
179 void CompilerInstance::createDiagnostics(int Argc, const char* const *Argv,
180                                          DiagnosticConsumer *Client,
181                                          bool ShouldOwnClient,
182                                          bool ShouldCloneClient) {
183   Diagnostics = createDiagnostics(getDiagnosticOpts(), Argc, Argv, Client,
184                                   ShouldOwnClient, ShouldCloneClient,
185                                   &getCodeGenOpts());
186 }
187 
188 llvm::IntrusiveRefCntPtr<DiagnosticsEngine>
189 CompilerInstance::createDiagnostics(const DiagnosticOptions &Opts,
190                                     int Argc, const char* const *Argv,
191                                     DiagnosticConsumer *Client,
192                                     bool ShouldOwnClient,
193                                     bool ShouldCloneClient,
194                                     const CodeGenOptions *CodeGenOpts) {
195   llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
196   llvm::IntrusiveRefCntPtr<DiagnosticsEngine>
197       Diags(new DiagnosticsEngine(DiagID));
198 
199   // Create the diagnostic client for reporting errors or for
200   // implementing -verify.
201   if (Client) {
202     if (ShouldCloneClient)
203       Diags->setClient(Client->clone(*Diags), ShouldOwnClient);
204     else
205       Diags->setClient(Client, ShouldOwnClient);
206   } else
207     Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts));
208 
209   // Chain in -verify checker, if requested.
210   if (Opts.VerifyDiagnostics)
211     Diags->setClient(new VerifyDiagnosticConsumer(*Diags));
212 
213   // Chain in -diagnostic-log-file dumper, if requested.
214   if (!Opts.DiagnosticLogFile.empty())
215     SetUpDiagnosticLog(Opts, CodeGenOpts, *Diags);
216 
217   if (!Opts.DumpBuildInformation.empty())
218     SetUpBuildDumpLog(Opts, Argc, Argv, *Diags);
219 
220   if (!Opts.DiagnosticSerializationFile.empty())
221     SetupSerializedDiagnostics(Opts, *Diags,
222                                Opts.DiagnosticSerializationFile);
223 
224   // Configure our handling of diagnostics.
225   ProcessWarningOptions(*Diags, Opts);
226 
227   return Diags;
228 }
229 
230 // File Manager
231 
232 void CompilerInstance::createFileManager() {
233   FileMgr = new FileManager(getFileSystemOpts());
234 }
235 
236 // Source Manager
237 
238 void CompilerInstance::createSourceManager(FileManager &FileMgr) {
239   SourceMgr = new SourceManager(getDiagnostics(), FileMgr);
240 }
241 
242 // Preprocessor
243 
244 void CompilerInstance::createPreprocessor() {
245   const PreprocessorOptions &PPOpts = getPreprocessorOpts();
246 
247   // Create a PTH manager if we are using some form of a token cache.
248   PTHManager *PTHMgr = 0;
249   if (!PPOpts.TokenCache.empty())
250     PTHMgr = PTHManager::Create(PPOpts.TokenCache, getDiagnostics());
251 
252   // Create the Preprocessor.
253   HeaderSearch *HeaderInfo = new HeaderSearch(getFileManager(),
254                                               getDiagnostics());
255   PP = new Preprocessor(getDiagnostics(), getLangOpts(), &getTarget(),
256                         getSourceManager(), *HeaderInfo, *this, PTHMgr,
257                         /*OwnsHeaderSearch=*/true);
258 
259   // Note that this is different then passing PTHMgr to Preprocessor's ctor.
260   // That argument is used as the IdentifierInfoLookup argument to
261   // IdentifierTable's ctor.
262   if (PTHMgr) {
263     PTHMgr->setPreprocessor(&*PP);
264     PP->setPTHManager(PTHMgr);
265   }
266 
267   if (PPOpts.DetailedRecord)
268     PP->createPreprocessingRecord(
269                                   PPOpts.DetailedRecordIncludesNestedMacroExpansions);
270 
271   InitializePreprocessor(*PP, PPOpts, getHeaderSearchOpts(), getFrontendOpts());
272 
273   // Set up the module path, including the hash for the
274   // module-creation options.
275   llvm::SmallString<256> SpecificModuleCache(
276                            getHeaderSearchOpts().ModuleCachePath);
277   if (!getHeaderSearchOpts().DisableModuleHash)
278     llvm::sys::path::append(SpecificModuleCache,
279                             getInvocation().getModuleHash());
280   PP->getHeaderSearchInfo().configureModules(SpecificModuleCache,
281                                              getLangOpts().CurrentModule);
282 
283   // Handle generating dependencies, if requested.
284   const DependencyOutputOptions &DepOpts = getDependencyOutputOpts();
285   if (!DepOpts.OutputFile.empty())
286     AttachDependencyFileGen(*PP, DepOpts);
287 
288   // Handle generating header include information, if requested.
289   if (DepOpts.ShowHeaderIncludes)
290     AttachHeaderIncludeGen(*PP);
291   if (!DepOpts.HeaderIncludeOutputFile.empty()) {
292     StringRef OutputPath = DepOpts.HeaderIncludeOutputFile;
293     if (OutputPath == "-")
294       OutputPath = "";
295     AttachHeaderIncludeGen(*PP, /*ShowAllHeaders=*/true, OutputPath,
296                            /*ShowDepth=*/false);
297   }
298 }
299 
300 // ASTContext
301 
302 void CompilerInstance::createASTContext() {
303   Preprocessor &PP = getPreprocessor();
304   Context = new ASTContext(getLangOpts(), PP.getSourceManager(),
305                            &getTarget(), PP.getIdentifierTable(),
306                            PP.getSelectorTable(), PP.getBuiltinInfo(),
307                            /*size_reserve=*/ 0);
308 }
309 
310 // ExternalASTSource
311 
312 void CompilerInstance::createPCHExternalASTSource(StringRef Path,
313                                                   bool DisablePCHValidation,
314                                                   bool DisableStatCache,
315                                                  void *DeserializationListener){
316   llvm::OwningPtr<ExternalASTSource> Source;
317   bool Preamble = getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
318   Source.reset(createPCHExternalASTSource(Path, getHeaderSearchOpts().Sysroot,
319                                           DisablePCHValidation,
320                                           DisableStatCache,
321                                           getPreprocessor(), getASTContext(),
322                                           DeserializationListener,
323                                           Preamble));
324   ModuleManager = static_cast<ASTReader*>(Source.get());
325   getASTContext().setExternalSource(Source);
326 }
327 
328 ExternalASTSource *
329 CompilerInstance::createPCHExternalASTSource(StringRef Path,
330                                              const std::string &Sysroot,
331                                              bool DisablePCHValidation,
332                                              bool DisableStatCache,
333                                              Preprocessor &PP,
334                                              ASTContext &Context,
335                                              void *DeserializationListener,
336                                              bool Preamble) {
337   llvm::OwningPtr<ASTReader> Reader;
338   Reader.reset(new ASTReader(PP, Context,
339                              Sysroot.empty() ? "" : Sysroot.c_str(),
340                              DisablePCHValidation, DisableStatCache));
341 
342   Reader->setDeserializationListener(
343             static_cast<ASTDeserializationListener *>(DeserializationListener));
344   switch (Reader->ReadAST(Path,
345                           Preamble ? serialization::MK_Preamble
346                                    : serialization::MK_PCH)) {
347   case ASTReader::Success:
348     // Set the predefines buffer as suggested by the PCH reader. Typically, the
349     // predefines buffer will be empty.
350     PP.setPredefines(Reader->getSuggestedPredefines());
351     return Reader.take();
352 
353   case ASTReader::Failure:
354     // Unrecoverable failure: don't even try to process the input file.
355     break;
356 
357   case ASTReader::IgnorePCH:
358     // No suitable PCH file could be found. Return an error.
359     break;
360   }
361 
362   return 0;
363 }
364 
365 // Code Completion
366 
367 static bool EnableCodeCompletion(Preprocessor &PP,
368                                  const std::string &Filename,
369                                  unsigned Line,
370                                  unsigned Column) {
371   // Tell the source manager to chop off the given file at a specific
372   // line and column.
373   const FileEntry *Entry = PP.getFileManager().getFile(Filename);
374   if (!Entry) {
375     PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file)
376       << Filename;
377     return true;
378   }
379 
380   // Truncate the named file at the given line/column.
381   PP.SetCodeCompletionPoint(Entry, Line, Column);
382   return false;
383 }
384 
385 void CompilerInstance::createCodeCompletionConsumer() {
386   const ParsedSourceLocation &Loc = getFrontendOpts().CodeCompletionAt;
387   if (!CompletionConsumer) {
388     CompletionConsumer.reset(
389       createCodeCompletionConsumer(getPreprocessor(),
390                                    Loc.FileName, Loc.Line, Loc.Column,
391                                    getFrontendOpts().ShowMacrosInCodeCompletion,
392                              getFrontendOpts().ShowCodePatternsInCodeCompletion,
393                            getFrontendOpts().ShowGlobalSymbolsInCodeCompletion,
394                                    llvm::outs()));
395     if (!CompletionConsumer)
396       return;
397   } else if (EnableCodeCompletion(getPreprocessor(), Loc.FileName,
398                                   Loc.Line, Loc.Column)) {
399     CompletionConsumer.reset();
400     return;
401   }
402 
403   if (CompletionConsumer->isOutputBinary() &&
404       llvm::sys::Program::ChangeStdoutToBinary()) {
405     getPreprocessor().getDiagnostics().Report(diag::err_fe_stdout_binary);
406     CompletionConsumer.reset();
407   }
408 }
409 
410 void CompilerInstance::createFrontendTimer() {
411   FrontendTimer.reset(new llvm::Timer("Clang front-end timer"));
412 }
413 
414 CodeCompleteConsumer *
415 CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP,
416                                                const std::string &Filename,
417                                                unsigned Line,
418                                                unsigned Column,
419                                                bool ShowMacros,
420                                                bool ShowCodePatterns,
421                                                bool ShowGlobals,
422                                                raw_ostream &OS) {
423   if (EnableCodeCompletion(PP, Filename, Line, Column))
424     return 0;
425 
426   // Set up the creation routine for code-completion.
427   return new PrintingCodeCompleteConsumer(ShowMacros, ShowCodePatterns,
428                                           ShowGlobals, 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         llvm::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_fe_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 }
481 
482 llvm::raw_fd_ostream *
483 CompilerInstance::createOutputFile(StringRef OutputPath,
484                                    bool Binary, bool RemoveFileOnSignal,
485                                    StringRef InFile,
486                                    StringRef Extension,
487                                    bool UseTemporary) {
488   std::string Error, OutputPathName, TempPathName;
489   llvm::raw_fd_ostream *OS = createOutputFile(OutputPath, Error, Binary,
490                                               RemoveFileOnSignal,
491                                               InFile, Extension,
492                                               UseTemporary,
493                                               &OutputPathName,
494                                               &TempPathName);
495   if (!OS) {
496     getDiagnostics().Report(diag::err_fe_unable_to_open_output)
497       << OutputPath << Error;
498     return 0;
499   }
500 
501   // Add the output file -- but don't try to remove "-", since this means we are
502   // using stdin.
503   addOutputFile(OutputFile((OutputPathName != "-") ? OutputPathName : "",
504                 TempPathName, OS));
505 
506   return OS;
507 }
508 
509 llvm::raw_fd_ostream *
510 CompilerInstance::createOutputFile(StringRef OutputPath,
511                                    std::string &Error,
512                                    bool Binary,
513                                    bool RemoveFileOnSignal,
514                                    StringRef InFile,
515                                    StringRef Extension,
516                                    bool UseTemporary,
517                                    std::string *ResultPathName,
518                                    std::string *TempPathName) {
519   std::string OutFile, TempFile;
520   if (!OutputPath.empty()) {
521     OutFile = OutputPath;
522   } else if (InFile == "-") {
523     OutFile = "-";
524   } else if (!Extension.empty()) {
525     llvm::sys::Path Path(InFile);
526     Path.eraseSuffix();
527     Path.appendSuffix(Extension);
528     OutFile = Path.str();
529   } else {
530     OutFile = "-";
531   }
532 
533   llvm::OwningPtr<llvm::raw_fd_ostream> OS;
534   std::string OSFile;
535 
536   if (UseTemporary && OutFile != "-") {
537     llvm::sys::Path OutPath(OutFile);
538     // Only create the temporary if we can actually write to OutPath, otherwise
539     // we want to fail early.
540     bool Exists;
541     if ((llvm::sys::fs::exists(OutPath.str(), Exists) || !Exists) ||
542         (OutPath.isRegularFile() && OutPath.canWrite())) {
543       // Create a temporary file.
544       llvm::SmallString<128> TempPath;
545       TempPath = OutFile;
546       TempPath += "-%%%%%%%%";
547       int fd;
548       if (llvm::sys::fs::unique_file(TempPath.str(), fd, TempPath,
549                                /*makeAbsolute=*/false) == llvm::errc::success) {
550         OS.reset(new llvm::raw_fd_ostream(fd, /*shouldClose=*/true));
551         OSFile = TempFile = TempPath.str();
552       }
553     }
554   }
555 
556   if (!OS) {
557     OSFile = OutFile;
558     OS.reset(
559       new llvm::raw_fd_ostream(OSFile.c_str(), Error,
560                                (Binary ? llvm::raw_fd_ostream::F_Binary : 0)));
561     if (!Error.empty())
562       return 0;
563   }
564 
565   // Make sure the out stream file gets removed if we crash.
566   if (RemoveFileOnSignal)
567     llvm::sys::RemoveFileOnSignal(llvm::sys::Path(OSFile));
568 
569   if (ResultPathName)
570     *ResultPathName = OutFile;
571   if (TempPathName)
572     *TempPathName = TempFile;
573 
574   return OS.take();
575 }
576 
577 // Initialization Utilities
578 
579 bool CompilerInstance::InitializeSourceManager(StringRef InputFile) {
580   return InitializeSourceManager(InputFile, getDiagnostics(), getFileManager(),
581                                  getSourceManager(), getFrontendOpts());
582 }
583 
584 bool CompilerInstance::InitializeSourceManager(StringRef InputFile,
585                                                DiagnosticsEngine &Diags,
586                                                FileManager &FileMgr,
587                                                SourceManager &SourceMgr,
588                                                const FrontendOptions &Opts) {
589   // Figure out where to get and map in the main file.
590   if (InputFile != "-") {
591     const FileEntry *File = FileMgr.getFile(InputFile);
592     if (!File) {
593       Diags.Report(diag::err_fe_error_reading) << InputFile;
594       return false;
595     }
596     SourceMgr.createMainFileID(File);
597   } else {
598     llvm::OwningPtr<llvm::MemoryBuffer> SB;
599     if (llvm::MemoryBuffer::getSTDIN(SB)) {
600       // FIXME: Give ec.message() in this diag.
601       Diags.Report(diag::err_fe_error_reading_stdin);
602       return false;
603     }
604     const FileEntry *File = FileMgr.getVirtualFile(SB->getBufferIdentifier(),
605                                                    SB->getBufferSize(), 0);
606     SourceMgr.createMainFileID(File);
607     SourceMgr.overrideFileContents(File, SB.take());
608   }
609 
610   assert(!SourceMgr.getMainFileID().isInvalid() &&
611          "Couldn't establish MainFileID!");
612   return true;
613 }
614 
615 // High-Level Operations
616 
617 bool CompilerInstance::ExecuteAction(FrontendAction &Act) {
618   assert(hasDiagnostics() && "Diagnostics engine is not initialized!");
619   assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!");
620   assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!");
621 
622   // FIXME: Take this as an argument, once all the APIs we used have moved to
623   // taking it as an input instead of hard-coding llvm::errs.
624   raw_ostream &OS = llvm::errs();
625 
626   // Create the target instance.
627   setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), getTargetOpts()));
628   if (!hasTarget())
629     return false;
630 
631   // Inform the target of the language options.
632   //
633   // FIXME: We shouldn't need to do this, the target should be immutable once
634   // created. This complexity should be lifted elsewhere.
635   getTarget().setForcedLangOptions(getLangOpts());
636 
637   // Validate/process some options.
638   if (getHeaderSearchOpts().Verbose)
639     OS << "clang -cc1 version " CLANG_VERSION_STRING
640        << " based upon " << PACKAGE_STRING
641        << " default target " << llvm::sys::getDefaultTargetTriple() << "\n";
642 
643   if (getFrontendOpts().ShowTimers)
644     createFrontendTimer();
645 
646   if (getFrontendOpts().ShowStats)
647     llvm::EnableStatistics();
648 
649   for (unsigned i = 0, e = getFrontendOpts().Inputs.size(); i != e; ++i) {
650     const std::string &InFile = getFrontendOpts().Inputs[i].second;
651 
652     // Reset the ID tables if we are reusing the SourceManager.
653     if (hasSourceManager())
654       getSourceManager().clearIDTables();
655 
656     if (Act.BeginSourceFile(*this, InFile, getFrontendOpts().Inputs[i].first)) {
657       Act.Execute();
658       Act.EndSourceFile();
659     }
660   }
661 
662   if (getDiagnosticOpts().ShowCarets) {
663     // We can have multiple diagnostics sharing one diagnostic client.
664     // Get the total number of warnings/errors from the client.
665     unsigned NumWarnings = getDiagnostics().getClient()->getNumWarnings();
666     unsigned NumErrors = getDiagnostics().getClient()->getNumErrors();
667 
668     if (NumWarnings)
669       OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s");
670     if (NumWarnings && NumErrors)
671       OS << " and ";
672     if (NumErrors)
673       OS << NumErrors << " error" << (NumErrors == 1 ? "" : "s");
674     if (NumWarnings || NumErrors)
675       OS << " generated.\n";
676   }
677 
678   if (getFrontendOpts().ShowStats && hasFileManager()) {
679     getFileManager().PrintStats();
680     OS << "\n";
681   }
682 
683   return !getDiagnostics().getClient()->getNumErrors();
684 }
685 
686 /// \brief Determine the appropriate source input kind based on language
687 /// options.
688 static InputKind getSourceInputKindFromOptions(const LangOptions &LangOpts) {
689   if (LangOpts.OpenCL)
690     return IK_OpenCL;
691   if (LangOpts.CUDA)
692     return IK_CUDA;
693   if (LangOpts.ObjC1)
694     return LangOpts.CPlusPlus? IK_ObjCXX : IK_ObjC;
695   return LangOpts.CPlusPlus? IK_CXX : IK_C;
696 }
697 
698 namespace {
699   struct CompileModuleData {
700     CompilerInstance &Instance;
701     GeneratePCHAction &CreateModuleAction;
702   };
703 }
704 
705 /// \brief Helper function that executes the module-generating action under
706 /// a crash recovery context.
707 static void doCompileModule(void *UserData) {
708   CompileModuleData &Data = *reinterpret_cast<CompileModuleData *>(UserData);
709   Data.Instance.ExecuteAction(Data.CreateModuleAction);
710 }
711 
712 namespace {
713   struct CompileModuleMapData {
714     CompilerInstance &Instance;
715     GenerateModuleAction &CreateModuleAction;
716   };
717 }
718 
719 /// \brief Helper function that executes the module-generating action under
720 /// a crash recovery context.
721 static void doCompileMapModule(void *UserData) {
722   CompileModuleMapData &Data
723     = *reinterpret_cast<CompileModuleMapData *>(UserData);
724   Data.Instance.ExecuteAction(Data.CreateModuleAction);
725 }
726 
727 namespace {
728   /// \brief Class that manages the creation of a lock file to aid
729   /// implicit coordination between different processes.
730   ///
731   /// The implicit coordination works by creating a ".lock" file alongside
732   /// the file that we're coordinating for, using the atomicity of the file
733   /// system to ensure that only a single process can create that ".lock" file.
734   /// When the lock file is removed, the owning process has finished the
735   /// operation.
736   class LockFileManager {
737   public:
738     /// \brief Describes the state of a lock file.
739     enum LockFileState {
740       /// \brief The lock file has been created and is owned by this instance
741       /// of the object.
742       LFS_Owned,
743       /// \brief The lock file already exists and is owned by some other
744       /// instance.
745       LFS_Shared,
746       /// \brief An error occurred while trying to create or find the lock
747       /// file.
748       LFS_Error
749     };
750 
751   private:
752     llvm::SmallString<128> LockFileName;
753     llvm::SmallString<128> UniqueLockFileName;
754 
755     llvm::Optional<std::pair<std::string, int> > Owner;
756     llvm::Optional<llvm::error_code> Error;
757 
758     LockFileManager(const LockFileManager &);
759     LockFileManager &operator=(const LockFileManager &);
760 
761     static llvm::Optional<std::pair<std::string, int> >
762     readLockFile(StringRef LockFileName);
763 
764     static bool processStillExecuting(StringRef Hostname, int PID);
765 
766   public:
767 
768     LockFileManager(StringRef FileName);
769     ~LockFileManager();
770 
771     /// \brief Determine the state of the lock file.
772     LockFileState getState() const;
773 
774     operator LockFileState() const { return getState(); }
775 
776     /// \brief For a shared lock, wait until the owner releases the lock.
777     void waitForUnlock();
778   };
779 }
780 
781 /// \brief Attempt to read the lock file with the given name, if it exists.
782 ///
783 /// \param LockFileName The name of the lock file to read.
784 ///
785 /// \returns The process ID of the process that owns this lock file
786 llvm::Optional<std::pair<std::string, int> >
787 LockFileManager::readLockFile(StringRef LockFileName) {
788   // Check whether the lock file exists. If not, clearly there's nothing
789   // to read, so we just return.
790   bool Exists = false;
791   if (llvm::sys::fs::exists(LockFileName, Exists) || !Exists)
792     return llvm::Optional<std::pair<std::string, int> >();
793 
794   // Read the owning host and PID out of the lock file. If it appears that the
795   // owning process is dead, the lock file is invalid.
796   int PID = 0;
797   std::string Hostname;
798   std::ifstream Input(LockFileName.str().c_str());
799   if (Input >> Hostname >> PID && PID > 0 &&
800       processStillExecuting(Hostname, PID))
801     return std::make_pair(Hostname, PID);
802 
803   // Delete the lock file. It's invalid anyway.
804   bool Existed;
805   llvm::sys::fs::remove(LockFileName, Existed);
806   return llvm::Optional<std::pair<std::string, int> >();
807 }
808 
809 bool LockFileManager::processStillExecuting(StringRef Hostname, int PID) {
810 #if LLVM_ON_UNIX
811   char MyHostname[256];
812   MyHostname[255] = 0;
813   MyHostname[0] = 0;
814   gethostname(MyHostname, 255);
815   // Check whether the process is dead. If so, we're done.
816   if (MyHostname == Hostname && getsid(PID) == -1 && errno == ESRCH)
817     return false;
818 #endif
819 
820   return true;
821 }
822 
823 LockFileManager::LockFileManager(StringRef FileName)
824 {
825   LockFileName = FileName;
826   LockFileName += ".lock";
827 
828   // If the lock file already exists, don't bother to try to create our own
829   // lock file; it won't work anyway. Just figure out who owns this lock file.
830   if ((Owner = readLockFile(LockFileName)))
831     return;
832 
833   // Create a lock file that is unique to this instance.
834   UniqueLockFileName = LockFileName;
835   UniqueLockFileName += "-%%%%%%%%";
836   int UniqueLockFileID;
837   if (llvm::error_code EC
838         = llvm::sys::fs::unique_file(UniqueLockFileName.str(),
839                                      UniqueLockFileID,
840                                      UniqueLockFileName,
841                                      /*makeAbsolute=*/false)) {
842     Error = EC;
843     return;
844   }
845 
846   // Write our process ID to our unique lock file.
847   {
848     llvm::raw_fd_ostream Out(UniqueLockFileID, /*shouldClose=*/true);
849 
850 #if LLVM_ON_UNIX
851     // FIXME: move getpid() call into LLVM
852     char hostname[256];
853     hostname[255] = 0;
854     hostname[0] = 0;
855     gethostname(hostname, 255);
856     Out << hostname << ' ' << getpid();
857 #else
858     Out << "localhost 1";
859 #endif
860     Out.close();
861 
862     if (Out.has_error()) {
863       // We failed to write out PID, so make up an excuse, remove the
864       // unique lock file, and fail.
865       Error = llvm::make_error_code(llvm::errc::no_space_on_device);
866       bool Existed;
867       llvm::sys::fs::remove(UniqueLockFileName.c_str(), Existed);
868       return;
869     }
870   }
871 
872   // Create a hard link from the lock file name. If this succeeds, we're done.
873   llvm::error_code EC
874     = llvm::sys::fs::create_hard_link(UniqueLockFileName.str(),
875                                       LockFileName.str());
876   if (EC == llvm::errc::success)
877     return;
878 
879   // Creating the hard link failed.
880 
881 #ifdef LLVM_ON_UNIX
882   // The creation of the hard link may appear to fail, but if stat'ing the
883   // unique file returns a link count of 2, then we can still declare success.
884   struct stat StatBuf;
885   if (stat(UniqueLockFileName.c_str(), &StatBuf) == 0 &&
886       StatBuf.st_nlink == 2)
887     return;
888 #endif
889 
890   // Someone else managed to create the lock file first. Wipe out our unique
891   // lock file (it's useless now) and read the process ID from the lock file.
892   bool Existed;
893   llvm::sys::fs::remove(UniqueLockFileName.str(), Existed);
894   if ((Owner = readLockFile(LockFileName)))
895     return;
896 
897   // There is a lock file that nobody owns; try to clean it up and report
898   // an error.
899   llvm::sys::fs::remove(LockFileName.str(), Existed);
900   Error = EC;
901 }
902 
903 LockFileManager::LockFileState LockFileManager::getState() const {
904   if (Owner)
905     return LFS_Shared;
906 
907   if (Error)
908     return LFS_Error;
909 
910   return LFS_Owned;
911 }
912 
913 LockFileManager::~LockFileManager() {
914   if (getState() != LFS_Owned)
915     return;
916 
917   // Since we own the lock, remove the lock file and our own unique lock file.
918   bool Existed;
919   llvm::sys::fs::remove(LockFileName.str(), Existed);
920   llvm::sys::fs::remove(UniqueLockFileName.str(), Existed);
921 }
922 
923 void LockFileManager::waitForUnlock() {
924   if (getState() != LFS_Shared)
925     return;
926 
927 #if LLVM_ON_WIN32
928   unsigned long Interval = 1;
929 #else
930   struct timespec Interval;
931   Interval.tv_sec = 0;
932   Interval.tv_nsec = 1000000;
933 #endif
934   // Don't wait more than an hour for the file to appear.
935   const unsigned MaxSeconds = 3600;
936   do {
937     // Sleep for the designated interval, to allow the owning process time to
938     // finish up and
939     // FIXME: Should we hook in to system APIs to get a notification when the
940     // lock file is deleted?
941 #if LLVM_ON_WIN32
942     Sleep(Interval);
943 #else
944     nanosleep(&Interval, NULL);
945 #endif
946     // If the file no longer exists, we're done.
947     bool Exists = false;
948     if (!llvm::sys::fs::exists(LockFileName.str(), Exists) && !Exists)
949       return;
950 
951     if (!processStillExecuting((*Owner).first, (*Owner).second))
952       return;
953 
954     // Exponentially increase the time we wait for the lock to be removed.
955 #if LLVM_ON_WIN32
956     Interval *= 2;
957 #else
958     Interval.tv_sec *= 2;
959     Interval.tv_nsec *= 2;
960     if (Interval.tv_nsec >= 1000000000) {
961       ++Interval.tv_sec;
962       Interval.tv_nsec -= 1000000000;
963     }
964 #endif
965   } while (
966 #if LLVM_ON_WIN32
967            Interval < MaxSeconds * 1000
968 #else
969            Interval.tv_sec < (time_t)MaxSeconds
970 #endif
971            );
972 
973   // Give up.
974 }
975 
976 /// \brief Compile a module file for the given module, using the options
977 /// provided by the importing compiler instance.
978 static void compileModule(CompilerInstance &ImportingInstance,
979                           ModuleMap::Module *Module,
980                           StringRef ModuleFileName) {
981   LockFileManager Locked(ModuleFileName);
982   switch (Locked) {
983   case LockFileManager::LFS_Error:
984     return;
985 
986   case LockFileManager::LFS_Owned:
987     // We're responsible for building the module ourselves. Do so below.
988     break;
989 
990   case LockFileManager::LFS_Shared:
991     // Someone else is responsible for building the module. Wait for them to
992     // finish.
993     Locked.waitForUnlock();
994     break;
995   }
996 
997   ModuleMap &ModMap
998     = ImportingInstance.getPreprocessor().getHeaderSearchInfo().getModuleMap();
999 
1000   // Construct a compiler invocation for creating this module.
1001   llvm::IntrusiveRefCntPtr<CompilerInvocation> Invocation
1002     (new CompilerInvocation(ImportingInstance.getInvocation()));
1003 
1004   // For any options that aren't intended to affect how a module is built,
1005   // reset them to their default values.
1006   Invocation->getLangOpts()->resetNonModularOptions();
1007   Invocation->getPreprocessorOpts().resetNonModularOptions();
1008 
1009   // Note the name of the module we're building.
1010   Invocation->getLangOpts()->CurrentModule = Module->getTopLevelModuleName();
1011 
1012   // Note that this module is part of the module build path, so that we
1013   // can detect cycles in the module graph.
1014   Invocation->getPreprocessorOpts().ModuleBuildPath
1015     .push_back(Module->getTopLevelModuleName());
1016 
1017   if (const FileEntry *ModuleMapFile
1018                                   = ModMap.getContainingModuleMapFile(Module)) {
1019     // If there is a module map file, build the module using the module map.
1020     // Set up the inputs/outputs so that we build the module from its umbrella
1021     // header.
1022     FrontendOptions &FrontendOpts = Invocation->getFrontendOpts();
1023     FrontendOpts.OutputFile = ModuleFileName.str();
1024     FrontendOpts.DisableFree = false;
1025     FrontendOpts.Inputs.clear();
1026     FrontendOpts.Inputs.push_back(
1027       std::make_pair(getSourceInputKindFromOptions(*Invocation->getLangOpts()),
1028                      ModuleMapFile->getName()));
1029 
1030     Invocation->getDiagnosticOpts().VerifyDiagnostics = 0;
1031 
1032 
1033     assert(ImportingInstance.getInvocation().getModuleHash() ==
1034            Invocation->getModuleHash() && "Module hash mismatch!");
1035 
1036     // Construct a compiler instance that will be used to actually create the
1037     // module.
1038     CompilerInstance Instance;
1039     Instance.setInvocation(&*Invocation);
1040     Instance.createDiagnostics(/*argc=*/0, /*argv=*/0,
1041                                &ImportingInstance.getDiagnosticClient(),
1042                                /*ShouldOwnClient=*/true,
1043                                /*ShouldCloneClient=*/true);
1044 
1045     // Construct a module-generating action.
1046     GenerateModuleAction CreateModuleAction;
1047 
1048     // Execute the action to actually build the module in-place. Use a separate
1049     // thread so that we get a stack large enough.
1050     const unsigned ThreadStackSize = 8 << 20;
1051     llvm::CrashRecoveryContext CRC;
1052     CompileModuleMapData Data = { Instance, CreateModuleAction };
1053     CRC.RunSafelyOnThread(&doCompileMapModule, &Data, ThreadStackSize);
1054     return;
1055   }
1056 
1057   // FIXME: Temporary fallback: generate the module from the umbrella header.
1058   // This is currently used when we infer a module map from a framework.
1059   assert(Module->UmbrellaHeader && "Inferred module map needs umbrella header");
1060 
1061   // Set up the inputs/outputs so that we build the module from its umbrella
1062   // header.
1063   FrontendOptions &FrontendOpts = Invocation->getFrontendOpts();
1064   FrontendOpts.OutputFile = ModuleFileName.str();
1065   FrontendOpts.DisableFree = false;
1066   FrontendOpts.Inputs.clear();
1067   FrontendOpts.Inputs.push_back(
1068     std::make_pair(getSourceInputKindFromOptions(*Invocation->getLangOpts()),
1069                                            Module->UmbrellaHeader->getName()));
1070 
1071   Invocation->getDiagnosticOpts().VerifyDiagnostics = 0;
1072 
1073 
1074   assert(ImportingInstance.getInvocation().getModuleHash() ==
1075            Invocation->getModuleHash() && "Module hash mismatch!");
1076 
1077   // Construct a compiler instance that will be used to actually create the
1078   // module.
1079   CompilerInstance Instance;
1080   Instance.setInvocation(&*Invocation);
1081   Instance.createDiagnostics(/*argc=*/0, /*argv=*/0,
1082                              &ImportingInstance.getDiagnosticClient(),
1083                              /*ShouldOwnClient=*/true,
1084                              /*ShouldCloneClient=*/true);
1085 
1086   // Construct a module-generating action.
1087   GeneratePCHAction CreateModuleAction(true);
1088 
1089   // Execute the action to actually build the module in-place. Use a separate
1090   // thread so that we get a stack large enough.
1091   const unsigned ThreadStackSize = 8 << 20;
1092   llvm::CrashRecoveryContext CRC;
1093   CompileModuleData Data = { Instance, CreateModuleAction };
1094   CRC.RunSafelyOnThread(&doCompileModule, &Data, ThreadStackSize);
1095 }
1096 
1097 ModuleKey CompilerInstance::loadModule(SourceLocation ImportLoc,
1098                                        IdentifierInfo &ModuleName,
1099                                        SourceLocation ModuleNameLoc) {
1100   // Determine what file we're searching from.
1101   SourceManager &SourceMgr = getSourceManager();
1102   SourceLocation ExpandedImportLoc = SourceMgr.getExpansionLoc(ImportLoc);
1103   const FileEntry *CurFile
1104     = SourceMgr.getFileEntryForID(SourceMgr.getFileID(ExpandedImportLoc));
1105   if (!CurFile)
1106     CurFile = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID());
1107 
1108   // Search for a module with the given name.
1109   ModuleMap::Module *Module = 0;
1110   std::string ModuleFileName;
1111   const FileEntry *ModuleFile
1112     = PP->getHeaderSearchInfo().lookupModule(ModuleName.getName(), Module,
1113                                              &ModuleFileName);
1114 
1115   bool BuildingModule = false;
1116   if (!ModuleFile && Module) {
1117     // The module is not cached, but we have a module map from which we can
1118     // build the module.
1119 
1120     // Check whether there is a cycle in the module graph.
1121     SmallVectorImpl<std::string> &ModuleBuildPath
1122       = getPreprocessorOpts().ModuleBuildPath;
1123     SmallVectorImpl<std::string>::iterator Pos
1124       = std::find(ModuleBuildPath.begin(), ModuleBuildPath.end(),
1125                   ModuleName.getName());
1126     if (Pos != ModuleBuildPath.end()) {
1127       llvm::SmallString<256> CyclePath;
1128       for (; Pos != ModuleBuildPath.end(); ++Pos) {
1129         CyclePath += *Pos;
1130         CyclePath += " -> ";
1131       }
1132       CyclePath += ModuleName.getName();
1133 
1134       getDiagnostics().Report(ModuleNameLoc, diag::err_module_cycle)
1135         << ModuleName.getName() << CyclePath;
1136       return 0;
1137     }
1138 
1139     getDiagnostics().Report(ModuleNameLoc, diag::warn_module_build)
1140       << ModuleName.getName();
1141     BuildingModule = true;
1142     compileModule(*this, Module, ModuleFileName);
1143     ModuleFile = FileMgr->getFile(ModuleFileName);
1144   }
1145 
1146   if (!ModuleFile) {
1147     getDiagnostics().Report(ModuleNameLoc,
1148                             BuildingModule? diag::err_module_not_built
1149                                           : diag::err_module_not_found)
1150       << ModuleName.getName()
1151       << SourceRange(ImportLoc, ModuleNameLoc);
1152     return 0;
1153   }
1154 
1155   // If we don't already have an ASTReader, create one now.
1156   if (!ModuleManager) {
1157     if (!hasASTContext())
1158       createASTContext();
1159 
1160     std::string Sysroot = getHeaderSearchOpts().Sysroot;
1161     const PreprocessorOptions &PPOpts = getPreprocessorOpts();
1162     ModuleManager = new ASTReader(getPreprocessor(), *Context,
1163                                   Sysroot.empty() ? "" : Sysroot.c_str(),
1164                                   PPOpts.DisablePCHValidation,
1165                                   PPOpts.DisableStatCache);
1166     if (hasASTConsumer()) {
1167       ModuleManager->setDeserializationListener(
1168         getASTConsumer().GetASTDeserializationListener());
1169       getASTContext().setASTMutationListener(
1170         getASTConsumer().GetASTMutationListener());
1171     }
1172     llvm::OwningPtr<ExternalASTSource> Source;
1173     Source.reset(ModuleManager);
1174     getASTContext().setExternalSource(Source);
1175     if (hasSema())
1176       ModuleManager->InitializeSema(getSema());
1177     if (hasASTConsumer())
1178       ModuleManager->StartTranslationUnit(&getASTConsumer());
1179   }
1180 
1181   // Try to load the module we found.
1182   switch (ModuleManager->ReadAST(ModuleFile->getName(),
1183                                  serialization::MK_Module)) {
1184   case ASTReader::Success:
1185     break;
1186 
1187   case ASTReader::IgnorePCH:
1188     // FIXME: The ASTReader will already have complained, but can we showhorn
1189     // that diagnostic information into a more useful form?
1190     return 0;
1191 
1192   case ASTReader::Failure:
1193     // Already complained.
1194     return 0;
1195   }
1196 
1197   // FIXME: The module file's FileEntry makes a poor key indeed!
1198   return (ModuleKey)ModuleFile;
1199 }
1200 
1201