xref: /llvm-project/clang/lib/Frontend/CompilerInstance.cpp (revision 8188c8a1f30ade4f0e6220a9809a10ca3b185b00)
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   PP = new Preprocessor(getDiagnostics(), getLangOpts(), &getTarget(),
255                         getSourceManager(), *HeaderInfo, *this, PTHMgr,
256                         /*OwnsHeaderSearch=*/true);
257 
258   // Note that this is different then passing PTHMgr to Preprocessor's ctor.
259   // That argument is used as the IdentifierInfoLookup argument to
260   // IdentifierTable's ctor.
261   if (PTHMgr) {
262     PTHMgr->setPreprocessor(&*PP);
263     PP->setPTHManager(PTHMgr);
264   }
265 
266   if (PPOpts.DetailedRecord)
267     PP->createPreprocessingRecord(
268                                   PPOpts.DetailedRecordIncludesNestedMacroExpansions);
269 
270   InitializePreprocessor(*PP, PPOpts, getHeaderSearchOpts(), getFrontendOpts());
271 
272   // Set up the module path, including the hash for the
273   // module-creation options.
274   llvm::SmallString<256> SpecificModuleCache(
275                            getHeaderSearchOpts().ModuleCachePath);
276   if (!getHeaderSearchOpts().DisableModuleHash)
277     llvm::sys::path::append(SpecificModuleCache,
278                             getInvocation().getModuleHash());
279   PP->getHeaderSearchInfo().configureModules(SpecificModuleCache,
280     getPreprocessorOpts().ModuleBuildPath.empty()
281       ? std::string()
282       : getPreprocessorOpts().ModuleBuildPath.back());
283 
284   // Handle generating dependencies, if requested.
285   const DependencyOutputOptions &DepOpts = getDependencyOutputOpts();
286   if (!DepOpts.OutputFile.empty())
287     AttachDependencyFileGen(*PP, DepOpts);
288 
289   // Handle generating header include information, if requested.
290   if (DepOpts.ShowHeaderIncludes)
291     AttachHeaderIncludeGen(*PP);
292   if (!DepOpts.HeaderIncludeOutputFile.empty()) {
293     StringRef OutputPath = DepOpts.HeaderIncludeOutputFile;
294     if (OutputPath == "-")
295       OutputPath = "";
296     AttachHeaderIncludeGen(*PP, /*ShowAllHeaders=*/true, OutputPath,
297                            /*ShowDepth=*/false);
298   }
299 }
300 
301 // ASTContext
302 
303 void CompilerInstance::createASTContext() {
304   Preprocessor &PP = getPreprocessor();
305   Context = new ASTContext(getLangOpts(), PP.getSourceManager(),
306                            &getTarget(), PP.getIdentifierTable(),
307                            PP.getSelectorTable(), PP.getBuiltinInfo(),
308                            /*size_reserve=*/ 0);
309 }
310 
311 // ExternalASTSource
312 
313 void CompilerInstance::createPCHExternalASTSource(StringRef Path,
314                                                   bool DisablePCHValidation,
315                                                   bool DisableStatCache,
316                                                  void *DeserializationListener){
317   llvm::OwningPtr<ExternalASTSource> Source;
318   bool Preamble = getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
319   Source.reset(createPCHExternalASTSource(Path, getHeaderSearchOpts().Sysroot,
320                                           DisablePCHValidation,
321                                           DisableStatCache,
322                                           getPreprocessor(), getASTContext(),
323                                           DeserializationListener,
324                                           Preamble));
325   ModuleManager = static_cast<ASTReader*>(Source.get());
326   getASTContext().setExternalSource(Source);
327 }
328 
329 ExternalASTSource *
330 CompilerInstance::createPCHExternalASTSource(StringRef Path,
331                                              const std::string &Sysroot,
332                                              bool DisablePCHValidation,
333                                              bool DisableStatCache,
334                                              Preprocessor &PP,
335                                              ASTContext &Context,
336                                              void *DeserializationListener,
337                                              bool Preamble) {
338   llvm::OwningPtr<ASTReader> Reader;
339   Reader.reset(new ASTReader(PP, Context,
340                              Sysroot.empty() ? "" : Sysroot.c_str(),
341                              DisablePCHValidation, DisableStatCache));
342 
343   Reader->setDeserializationListener(
344             static_cast<ASTDeserializationListener *>(DeserializationListener));
345   switch (Reader->ReadAST(Path,
346                           Preamble ? serialization::MK_Preamble
347                                    : serialization::MK_PCH)) {
348   case ASTReader::Success:
349     // Set the predefines buffer as suggested by the PCH reader. Typically, the
350     // predefines buffer will be empty.
351     PP.setPredefines(Reader->getSuggestedPredefines());
352     return Reader.take();
353 
354   case ASTReader::Failure:
355     // Unrecoverable failure: don't even try to process the input file.
356     break;
357 
358   case ASTReader::IgnorePCH:
359     // No suitable PCH file could be found. Return an error.
360     break;
361   }
362 
363   return 0;
364 }
365 
366 // Code Completion
367 
368 static bool EnableCodeCompletion(Preprocessor &PP,
369                                  const std::string &Filename,
370                                  unsigned Line,
371                                  unsigned Column) {
372   // Tell the source manager to chop off the given file at a specific
373   // line and column.
374   const FileEntry *Entry = PP.getFileManager().getFile(Filename);
375   if (!Entry) {
376     PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file)
377       << Filename;
378     return true;
379   }
380 
381   // Truncate the named file at the given line/column.
382   PP.SetCodeCompletionPoint(Entry, Line, Column);
383   return false;
384 }
385 
386 void CompilerInstance::createCodeCompletionConsumer() {
387   const ParsedSourceLocation &Loc = getFrontendOpts().CodeCompletionAt;
388   if (!CompletionConsumer) {
389     CompletionConsumer.reset(
390       createCodeCompletionConsumer(getPreprocessor(),
391                                    Loc.FileName, Loc.Line, Loc.Column,
392                                    getFrontendOpts().ShowMacrosInCodeCompletion,
393                              getFrontendOpts().ShowCodePatternsInCodeCompletion,
394                            getFrontendOpts().ShowGlobalSymbolsInCodeCompletion,
395                                    llvm::outs()));
396     if (!CompletionConsumer)
397       return;
398   } else if (EnableCodeCompletion(getPreprocessor(), Loc.FileName,
399                                   Loc.Line, Loc.Column)) {
400     CompletionConsumer.reset();
401     return;
402   }
403 
404   if (CompletionConsumer->isOutputBinary() &&
405       llvm::sys::Program::ChangeStdoutToBinary()) {
406     getPreprocessor().getDiagnostics().Report(diag::err_fe_stdout_binary);
407     CompletionConsumer.reset();
408   }
409 }
410 
411 void CompilerInstance::createFrontendTimer() {
412   FrontendTimer.reset(new llvm::Timer("Clang front-end timer"));
413 }
414 
415 CodeCompleteConsumer *
416 CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP,
417                                                const std::string &Filename,
418                                                unsigned Line,
419                                                unsigned Column,
420                                                bool ShowMacros,
421                                                bool ShowCodePatterns,
422                                                bool ShowGlobals,
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(ShowMacros, ShowCodePatterns,
429                                           ShowGlobals, OS);
430 }
431 
432 void CompilerInstance::createSema(TranslationUnitKind TUKind,
433                                   CodeCompleteConsumer *CompletionConsumer) {
434   TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(),
435                          TUKind, CompletionConsumer));
436 }
437 
438 // Output Files
439 
440 void CompilerInstance::addOutputFile(const OutputFile &OutFile) {
441   assert(OutFile.OS && "Attempt to add empty stream to output list!");
442   OutputFiles.push_back(OutFile);
443 }
444 
445 void CompilerInstance::clearOutputFiles(bool EraseFiles) {
446   for (std::list<OutputFile>::iterator
447          it = OutputFiles.begin(), ie = OutputFiles.end(); it != ie; ++it) {
448     delete it->OS;
449     if (!it->TempFilename.empty()) {
450       if (EraseFiles) {
451         bool existed;
452         llvm::sys::fs::remove(it->TempFilename, existed);
453       } else {
454         llvm::SmallString<128> NewOutFile(it->Filename);
455 
456         // If '-working-directory' was passed, the output filename should be
457         // relative to that.
458         FileMgr->FixupRelativePath(NewOutFile);
459         if (llvm::error_code ec = llvm::sys::fs::rename(it->TempFilename,
460                                                         NewOutFile.str())) {
461           getDiagnostics().Report(diag::err_fe_unable_to_rename_temp)
462             << it->TempFilename << it->Filename << ec.message();
463 
464           bool existed;
465           llvm::sys::fs::remove(it->TempFilename, existed);
466         }
467       }
468     } else if (!it->Filename.empty() && EraseFiles)
469       llvm::sys::Path(it->Filename).eraseFromDisk();
470 
471   }
472   OutputFiles.clear();
473 }
474 
475 llvm::raw_fd_ostream *
476 CompilerInstance::createDefaultOutputFile(bool Binary,
477                                           StringRef InFile,
478                                           StringRef Extension) {
479   return createOutputFile(getFrontendOpts().OutputFile, Binary,
480                           /*RemoveFileOnSignal=*/true, InFile, Extension);
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   std::string Error, OutputPathName, TempPathName;
490   llvm::raw_fd_ostream *OS = createOutputFile(OutputPath, Error, Binary,
491                                               RemoveFileOnSignal,
492                                               InFile, Extension,
493                                               UseTemporary,
494                                               &OutputPathName,
495                                               &TempPathName);
496   if (!OS) {
497     getDiagnostics().Report(diag::err_fe_unable_to_open_output)
498       << OutputPath << Error;
499     return 0;
500   }
501 
502   // Add the output file -- but don't try to remove "-", since this means we are
503   // using stdin.
504   addOutputFile(OutputFile((OutputPathName != "-") ? OutputPathName : "",
505                 TempPathName, OS));
506 
507   return OS;
508 }
509 
510 llvm::raw_fd_ostream *
511 CompilerInstance::createOutputFile(StringRef OutputPath,
512                                    std::string &Error,
513                                    bool Binary,
514                                    bool RemoveFileOnSignal,
515                                    StringRef InFile,
516                                    StringRef Extension,
517                                    bool UseTemporary,
518                                    std::string *ResultPathName,
519                                    std::string *TempPathName) {
520   std::string OutFile, TempFile;
521   if (!OutputPath.empty()) {
522     OutFile = OutputPath;
523   } else if (InFile == "-") {
524     OutFile = "-";
525   } else if (!Extension.empty()) {
526     llvm::sys::Path Path(InFile);
527     Path.eraseSuffix();
528     Path.appendSuffix(Extension);
529     OutFile = Path.str();
530   } else {
531     OutFile = "-";
532   }
533 
534   llvm::OwningPtr<llvm::raw_fd_ostream> OS;
535   std::string OSFile;
536 
537   if (UseTemporary && OutFile != "-") {
538     llvm::sys::Path OutPath(OutFile);
539     // Only create the temporary if we can actually write to OutPath, otherwise
540     // we want to fail early.
541     bool Exists;
542     if ((llvm::sys::fs::exists(OutPath.str(), Exists) || !Exists) ||
543         (OutPath.isRegularFile() && OutPath.canWrite())) {
544       // Create a temporary file.
545       llvm::SmallString<128> TempPath;
546       TempPath = OutFile;
547       TempPath += "-%%%%%%%%";
548       int fd;
549       if (llvm::sys::fs::unique_file(TempPath.str(), fd, TempPath,
550                                /*makeAbsolute=*/false) == llvm::errc::success) {
551         OS.reset(new llvm::raw_fd_ostream(fd, /*shouldClose=*/true));
552         OSFile = TempFile = TempPath.str();
553       }
554     }
555   }
556 
557   if (!OS) {
558     OSFile = OutFile;
559     OS.reset(
560       new llvm::raw_fd_ostream(OSFile.c_str(), Error,
561                                (Binary ? llvm::raw_fd_ostream::F_Binary : 0)));
562     if (!Error.empty())
563       return 0;
564   }
565 
566   // Make sure the out stream file gets removed if we crash.
567   if (RemoveFileOnSignal)
568     llvm::sys::RemoveFileOnSignal(llvm::sys::Path(OSFile));
569 
570   if (ResultPathName)
571     *ResultPathName = OutFile;
572   if (TempPathName)
573     *TempPathName = TempFile;
574 
575   return OS.take();
576 }
577 
578 // Initialization Utilities
579 
580 bool CompilerInstance::InitializeSourceManager(StringRef InputFile) {
581   return InitializeSourceManager(InputFile, getDiagnostics(), getFileManager(),
582                                  getSourceManager(), getFrontendOpts());
583 }
584 
585 bool CompilerInstance::InitializeSourceManager(StringRef InputFile,
586                                                DiagnosticsEngine &Diags,
587                                                FileManager &FileMgr,
588                                                SourceManager &SourceMgr,
589                                                const FrontendOptions &Opts) {
590   // Figure out where to get and map in the main file.
591   if (InputFile != "-") {
592     const FileEntry *File = FileMgr.getFile(InputFile);
593     if (!File) {
594       Diags.Report(diag::err_fe_error_reading) << InputFile;
595       return false;
596     }
597     SourceMgr.createMainFileID(File);
598   } else {
599     llvm::OwningPtr<llvm::MemoryBuffer> SB;
600     if (llvm::MemoryBuffer::getSTDIN(SB)) {
601       // FIXME: Give ec.message() in this diag.
602       Diags.Report(diag::err_fe_error_reading_stdin);
603       return false;
604     }
605     const FileEntry *File = FileMgr.getVirtualFile(SB->getBufferIdentifier(),
606                                                    SB->getBufferSize(), 0);
607     SourceMgr.createMainFileID(File);
608     SourceMgr.overrideFileContents(File, SB.take());
609   }
610 
611   assert(!SourceMgr.getMainFileID().isInvalid() &&
612          "Couldn't establish MainFileID!");
613   return true;
614 }
615 
616 // High-Level Operations
617 
618 bool CompilerInstance::ExecuteAction(FrontendAction &Act) {
619   assert(hasDiagnostics() && "Diagnostics engine is not initialized!");
620   assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!");
621   assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!");
622 
623   // FIXME: Take this as an argument, once all the APIs we used have moved to
624   // taking it as an input instead of hard-coding llvm::errs.
625   raw_ostream &OS = llvm::errs();
626 
627   // Create the target instance.
628   setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), getTargetOpts()));
629   if (!hasTarget())
630     return false;
631 
632   // Inform the target of the language options.
633   //
634   // FIXME: We shouldn't need to do this, the target should be immutable once
635   // created. This complexity should be lifted elsewhere.
636   getTarget().setForcedLangOptions(getLangOpts());
637 
638   // Validate/process some options.
639   if (getHeaderSearchOpts().Verbose)
640     OS << "clang -cc1 version " CLANG_VERSION_STRING
641        << " based upon " << PACKAGE_STRING
642        << " default target " << llvm::sys::getDefaultTargetTriple() << "\n";
643 
644   if (getFrontendOpts().ShowTimers)
645     createFrontendTimer();
646 
647   if (getFrontendOpts().ShowStats)
648     llvm::EnableStatistics();
649 
650   for (unsigned i = 0, e = getFrontendOpts().Inputs.size(); i != e; ++i) {
651     const std::string &InFile = getFrontendOpts().Inputs[i].second;
652 
653     // Reset the ID tables if we are reusing the SourceManager.
654     if (hasSourceManager())
655       getSourceManager().clearIDTables();
656 
657     if (Act.BeginSourceFile(*this, InFile, getFrontendOpts().Inputs[i].first)) {
658       Act.Execute();
659       Act.EndSourceFile();
660     }
661   }
662 
663   if (getDiagnosticOpts().ShowCarets) {
664     // We can have multiple diagnostics sharing one diagnostic client.
665     // Get the total number of warnings/errors from the client.
666     unsigned NumWarnings = getDiagnostics().getClient()->getNumWarnings();
667     unsigned NumErrors = getDiagnostics().getClient()->getNumErrors();
668 
669     if (NumWarnings)
670       OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s");
671     if (NumWarnings && NumErrors)
672       OS << " and ";
673     if (NumErrors)
674       OS << NumErrors << " error" << (NumErrors == 1 ? "" : "s");
675     if (NumWarnings || NumErrors)
676       OS << " generated.\n";
677   }
678 
679   if (getFrontendOpts().ShowStats && hasFileManager()) {
680     getFileManager().PrintStats();
681     OS << "\n";
682   }
683 
684   return !getDiagnostics().getClient()->getNumErrors();
685 }
686 
687 /// \brief Determine the appropriate source input kind based on language
688 /// options.
689 static InputKind getSourceInputKindFromOptions(const LangOptions &LangOpts) {
690   if (LangOpts.OpenCL)
691     return IK_OpenCL;
692   if (LangOpts.CUDA)
693     return IK_CUDA;
694   if (LangOpts.ObjC1)
695     return LangOpts.CPlusPlus? IK_ObjCXX : IK_ObjC;
696   return LangOpts.CPlusPlus? IK_CXX : IK_C;
697 }
698 
699 namespace {
700   struct CompileModuleData {
701     CompilerInstance &Instance;
702     GeneratePCHAction &CreateModuleAction;
703   };
704 }
705 
706 /// \brief Helper function that executes the module-generating action under
707 /// a crash recovery context.
708 static void doCompileModule(void *UserData) {
709   CompileModuleData &Data = *reinterpret_cast<CompileModuleData *>(UserData);
710   Data.Instance.ExecuteAction(Data.CreateModuleAction);
711 }
712 
713 namespace {
714   /// \brief Class that manages the creation of a lock file to aid
715   /// implicit coordination between different processes.
716   ///
717   /// The implicit coordination works by creating a ".lock" file alongside
718   /// the file that we're coordinating for, using the atomicity of the file
719   /// system to ensure that only a single process can create that ".lock" file.
720   /// When the lock file is removed, the owning process has finished the
721   /// operation.
722   class LockFileManager {
723   public:
724     /// \brief Describes the state of a lock file.
725     enum LockFileState {
726       /// \brief The lock file has been created and is owned by this instance
727       /// of the object.
728       LFS_Owned,
729       /// \brief The lock file already exists and is owned by some other
730       /// instance.
731       LFS_Shared,
732       /// \brief An error occurred while trying to create or find the lock
733       /// file.
734       LFS_Error
735     };
736 
737   private:
738     llvm::SmallString<128> LockFileName;
739     llvm::SmallString<128> UniqueLockFileName;
740 
741     llvm::Optional<std::pair<std::string, int> > Owner;
742     llvm::Optional<llvm::error_code> Error;
743 
744     LockFileManager(const LockFileManager &);
745     LockFileManager &operator=(const LockFileManager &);
746 
747     static llvm::Optional<std::pair<std::string, int> >
748     readLockFile(StringRef LockFileName);
749 
750     static bool processStillExecuting(StringRef Hostname, int PID);
751 
752   public:
753 
754     LockFileManager(StringRef FileName);
755     ~LockFileManager();
756 
757     /// \brief Determine the state of the lock file.
758     LockFileState getState() const;
759 
760     operator LockFileState() const { return getState(); }
761 
762     /// \brief For a shared lock, wait until the owner releases the lock.
763     void waitForUnlock();
764   };
765 }
766 
767 /// \brief Attempt to read the lock file with the given name, if it exists.
768 ///
769 /// \param LockFileName The name of the lock file to read.
770 ///
771 /// \returns The process ID of the process that owns this lock file
772 llvm::Optional<std::pair<std::string, int> >
773 LockFileManager::readLockFile(StringRef LockFileName) {
774   // Check whether the lock file exists. If not, clearly there's nothing
775   // to read, so we just return.
776   bool Exists = false;
777   if (llvm::sys::fs::exists(LockFileName, Exists) || !Exists)
778     return llvm::Optional<std::pair<std::string, int> >();
779 
780   // Read the owning host and PID out of the lock file. If it appears that the
781   // owning process is dead, the lock file is invalid.
782   int PID = 0;
783   std::string Hostname;
784   std::ifstream Input(LockFileName.str().c_str());
785   if (Input >> Hostname >> PID && PID > 0 &&
786       processStillExecuting(Hostname, PID))
787     return std::make_pair(Hostname, PID);
788 
789   // Delete the lock file. It's invalid anyway.
790   bool Existed;
791   llvm::sys::fs::remove(LockFileName, Existed);
792   return llvm::Optional<std::pair<std::string, int> >();
793 }
794 
795 bool LockFileManager::processStillExecuting(StringRef Hostname, int PID) {
796 #if LLVM_ON_UNIX
797   char MyHostname[256];
798   MyHostname[255] = 0;
799   MyHostname[0] = 0;
800   gethostname(MyHostname, 255);
801   // Check whether the process is dead. If so, we're done.
802   if (MyHostname == Hostname && getsid(PID) == -1 && errno == ESRCH)
803     return false;
804 #endif
805 
806   return true;
807 }
808 
809 LockFileManager::LockFileManager(StringRef FileName)
810 {
811   LockFileName = FileName;
812   LockFileName += ".lock";
813 
814   // If the lock file already exists, don't bother to try to create our own
815   // lock file; it won't work anyway. Just figure out who owns this lock file.
816   if ((Owner = readLockFile(LockFileName)))
817     return;
818 
819   // Create a lock file that is unique to this instance.
820   UniqueLockFileName = LockFileName;
821   UniqueLockFileName += "-%%%%%%%%";
822   int UniqueLockFileID;
823   if (llvm::error_code EC
824         = llvm::sys::fs::unique_file(UniqueLockFileName.str(),
825                                      UniqueLockFileID,
826                                      UniqueLockFileName,
827                                      /*makeAbsolute=*/false)) {
828     Error = EC;
829     return;
830   }
831 
832   // Write our process ID to our unique lock file.
833   {
834     llvm::raw_fd_ostream Out(UniqueLockFileID, /*shouldClose=*/true);
835 
836 #if LLVM_ON_UNIX
837     // FIXME: move getpid() call into LLVM
838     char hostname[256];
839     hostname[255] = 0;
840     hostname[0] = 0;
841     gethostname(hostname, 255);
842     Out << hostname << ' ' << getpid();
843 #else
844     Out << "localhost 1";
845 #endif
846     Out.close();
847 
848     if (Out.has_error()) {
849       // We failed to write out PID, so make up an excuse, remove the
850       // unique lock file, and fail.
851       Error = llvm::make_error_code(llvm::errc::no_space_on_device);
852       bool Existed;
853       llvm::sys::fs::remove(UniqueLockFileName.c_str(), Existed);
854       return;
855     }
856   }
857 
858   // Create a hard link from the lock file name. If this succeeds, we're done.
859   llvm::error_code EC
860     = llvm::sys::fs::create_hard_link(UniqueLockFileName.str(),
861                                       LockFileName.str());
862   if (EC == llvm::errc::success)
863     return;
864 
865   // Creating the hard link failed.
866 
867 #ifdef LLVM_ON_UNIX
868   // The creation of the hard link may appear to fail, but if stat'ing the
869   // unique file returns a link count of 2, then we can still declare success.
870   struct stat StatBuf;
871   if (stat(UniqueLockFileName.c_str(), &StatBuf) == 0 &&
872       StatBuf.st_nlink == 2)
873     return;
874 #endif
875 
876   // Someone else managed to create the lock file first. Wipe out our unique
877   // lock file (it's useless now) and read the process ID from the lock file.
878   bool Existed;
879   llvm::sys::fs::remove(UniqueLockFileName.str(), Existed);
880   if ((Owner = readLockFile(LockFileName)))
881     return;
882 
883   // There is a lock file that nobody owns; try to clean it up and report
884   // an error.
885   llvm::sys::fs::remove(LockFileName.str(), Existed);
886   Error = EC;
887 }
888 
889 LockFileManager::LockFileState LockFileManager::getState() const {
890   if (Owner)
891     return LFS_Shared;
892 
893   if (Error)
894     return LFS_Error;
895 
896   return LFS_Owned;
897 }
898 
899 LockFileManager::~LockFileManager() {
900   if (getState() != LFS_Owned)
901     return;
902 
903   // Since we own the lock, remove the lock file and our own unique lock file.
904   bool Existed;
905   llvm::sys::fs::remove(LockFileName.str(), Existed);
906   llvm::sys::fs::remove(UniqueLockFileName.str(), Existed);
907 }
908 
909 void LockFileManager::waitForUnlock() {
910   if (getState() != LFS_Shared)
911     return;
912 
913 #if LLVM_ON_WIN32
914   unsigned long Interval = 1;
915 #else
916   struct timespec Interval;
917   Interval.tv_sec = 0;
918   Interval.tv_nsec = 1000000;
919 #endif
920   // Don't wait more than an hour for the file to appear.
921   const unsigned MaxSeconds = 3600;
922   do {
923     // Sleep for the designated interval, to allow the owning process time to
924     // finish up and
925     // FIXME: Should we hook in to system APIs to get a notification when the
926     // lock file is deleted?
927 #if LLVM_ON_WIN32
928     Sleep(Interval);
929 #else
930     nanosleep(&Interval, NULL);
931 #endif
932     // If the file no longer exists, we're done.
933     bool Exists = false;
934     if (!llvm::sys::fs::exists(LockFileName.str(), Exists) && !Exists)
935       return;
936 
937     if (!processStillExecuting((*Owner).first, (*Owner).second))
938       return;
939 
940     // Exponentially increase the time we wait for the lock to be removed.
941 #if LLVM_ON_WIN32
942     Interval *= 2;
943 #else
944     Interval.tv_sec *= 2;
945     Interval.tv_nsec *= 2;
946     if (Interval.tv_nsec >= 1000000000) {
947       ++Interval.tv_sec;
948       Interval.tv_nsec -= 1000000000;
949     }
950 #endif
951   } while (
952 #if LLVM_ON_WIN32
953            Interval < MaxSeconds * 1000
954 #else
955            Interval.tv_sec < (time_t)MaxSeconds
956 #endif
957            );
958 
959   // Give up.
960 }
961 
962 /// \brief Compile a module file for the given module name with the given
963 /// umbrella header, using the options provided by the importing compiler
964 /// instance.
965 static void compileModule(CompilerInstance &ImportingInstance,
966                           StringRef ModuleName,
967                           StringRef ModuleFileName,
968                           StringRef UmbrellaHeader) {
969   LockFileManager Locked(ModuleFileName);
970   switch (Locked) {
971   case LockFileManager::LFS_Error:
972     return;
973 
974   case LockFileManager::LFS_Owned:
975     // We're responsible for building the module ourselves. Do so below.
976     break;
977 
978   case LockFileManager::LFS_Shared:
979     // Someone else is responsible for building the module. Wait for them to
980     // finish.
981     Locked.waitForUnlock();
982     break;
983   }
984 
985   // Construct a compiler invocation for creating this module.
986   llvm::IntrusiveRefCntPtr<CompilerInvocation> Invocation
987     (new CompilerInvocation(ImportingInstance.getInvocation()));
988 
989   // For any options that aren't intended to affect how a module is built,
990   // reset them to their default values.
991   Invocation->getLangOpts().resetNonModularOptions();
992   Invocation->getPreprocessorOpts().resetNonModularOptions();
993 
994   // Note that this module is part of the module build path, so that we
995   // can detect cycles in the module graph.
996   Invocation->getPreprocessorOpts().ModuleBuildPath.push_back(ModuleName);
997 
998   // Set up the inputs/outputs so that we build the module from its umbrella
999   // header.
1000   FrontendOptions &FrontendOpts = Invocation->getFrontendOpts();
1001   FrontendOpts.OutputFile = ModuleFileName.str();
1002   FrontendOpts.DisableFree = false;
1003   FrontendOpts.Inputs.clear();
1004   FrontendOpts.Inputs.push_back(
1005     std::make_pair(getSourceInputKindFromOptions(Invocation->getLangOpts()),
1006                                                  UmbrellaHeader));
1007 
1008   Invocation->getDiagnosticOpts().VerifyDiagnostics = 0;
1009 
1010 
1011   assert(ImportingInstance.getInvocation().getModuleHash() ==
1012            Invocation->getModuleHash() && "Module hash mismatch!");
1013 
1014   // Construct a compiler instance that will be used to actually create the
1015   // module.
1016   CompilerInstance Instance;
1017   Instance.setInvocation(&*Invocation);
1018   Instance.createDiagnostics(/*argc=*/0, /*argv=*/0,
1019                              &ImportingInstance.getDiagnosticClient(),
1020                              /*ShouldOwnClient=*/true,
1021                              /*ShouldCloneClient=*/true);
1022 
1023   // Construct a module-generating action.
1024   GeneratePCHAction CreateModuleAction(true);
1025 
1026   // Execute the action to actually build the module in-place. Use a separate
1027   // thread so that we get a stack large enough.
1028   const unsigned ThreadStackSize = 8 << 20;
1029   llvm::CrashRecoveryContext CRC;
1030   CompileModuleData Data = { Instance, CreateModuleAction };
1031   CRC.RunSafelyOnThread(&doCompileModule, &Data, ThreadStackSize);
1032 }
1033 
1034 ModuleKey CompilerInstance::loadModule(SourceLocation ImportLoc,
1035                                        IdentifierInfo &ModuleName,
1036                                        SourceLocation ModuleNameLoc) {
1037   // Determine what file we're searching from.
1038   SourceManager &SourceMgr = getSourceManager();
1039   SourceLocation ExpandedImportLoc = SourceMgr.getExpansionLoc(ImportLoc);
1040   const FileEntry *CurFile
1041     = SourceMgr.getFileEntryForID(SourceMgr.getFileID(ExpandedImportLoc));
1042   if (!CurFile)
1043     CurFile = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID());
1044 
1045   // Search for a module with the given name.
1046   std::string UmbrellaHeader;
1047   std::string ModuleFileName;
1048   const FileEntry *ModuleFile
1049     = PP->getHeaderSearchInfo().lookupModule(ModuleName.getName(),
1050                                              &ModuleFileName,
1051                                              &UmbrellaHeader);
1052 
1053   bool BuildingModule = false;
1054   if (!ModuleFile && !UmbrellaHeader.empty()) {
1055     // We didn't find the module, but there is an umbrella header that
1056     // can be used to create the module file. Create a separate compilation
1057     // module to do so.
1058 
1059     // Check whether there is a cycle in the module graph.
1060     SmallVectorImpl<std::string> &ModuleBuildPath
1061       = getPreprocessorOpts().ModuleBuildPath;
1062     SmallVectorImpl<std::string>::iterator Pos
1063       = std::find(ModuleBuildPath.begin(), ModuleBuildPath.end(),
1064                   ModuleName.getName());
1065     if (Pos != ModuleBuildPath.end()) {
1066       llvm::SmallString<256> CyclePath;
1067       for (; Pos != ModuleBuildPath.end(); ++Pos) {
1068         CyclePath += *Pos;
1069         CyclePath += " -> ";
1070       }
1071       CyclePath += ModuleName.getName();
1072 
1073       getDiagnostics().Report(ModuleNameLoc, diag::err_module_cycle)
1074         << ModuleName.getName() << CyclePath;
1075       return 0;
1076     }
1077 
1078     getDiagnostics().Report(ModuleNameLoc, diag::warn_module_build)
1079       << ModuleName.getName();
1080     BuildingModule = true;
1081     compileModule(*this, ModuleName.getName(), ModuleFileName, UmbrellaHeader);
1082     ModuleFile = PP->getHeaderSearchInfo().lookupModule(ModuleName.getName());
1083   }
1084 
1085   if (!ModuleFile) {
1086     getDiagnostics().Report(ModuleNameLoc,
1087                             BuildingModule? diag::err_module_not_built
1088                                           : diag::err_module_not_found)
1089       << ModuleName.getName()
1090       << SourceRange(ImportLoc, ModuleNameLoc);
1091     return 0;
1092   }
1093 
1094   // If we don't already have an ASTReader, create one now.
1095   if (!ModuleManager) {
1096     if (!hasASTContext())
1097       createASTContext();
1098 
1099     std::string Sysroot = getHeaderSearchOpts().Sysroot;
1100     const PreprocessorOptions &PPOpts = getPreprocessorOpts();
1101     ModuleManager = new ASTReader(getPreprocessor(), *Context,
1102                                   Sysroot.empty() ? "" : Sysroot.c_str(),
1103                                   PPOpts.DisablePCHValidation,
1104                                   PPOpts.DisableStatCache);
1105     if (hasASTConsumer()) {
1106       ModuleManager->setDeserializationListener(
1107         getASTConsumer().GetASTDeserializationListener());
1108       getASTContext().setASTMutationListener(
1109         getASTConsumer().GetASTMutationListener());
1110     }
1111     llvm::OwningPtr<ExternalASTSource> Source;
1112     Source.reset(ModuleManager);
1113     getASTContext().setExternalSource(Source);
1114     if (hasSema())
1115       ModuleManager->InitializeSema(getSema());
1116     if (hasASTConsumer())
1117       ModuleManager->StartTranslationUnit(&getASTConsumer());
1118   }
1119 
1120   // Try to load the module we found.
1121   switch (ModuleManager->ReadAST(ModuleFile->getName(),
1122                                  serialization::MK_Module)) {
1123   case ASTReader::Success:
1124     break;
1125 
1126   case ASTReader::IgnorePCH:
1127     // FIXME: The ASTReader will already have complained, but can we showhorn
1128     // that diagnostic information into a more useful form?
1129     return 0;
1130 
1131   case ASTReader::Failure:
1132     // Already complained.
1133     return 0;
1134   }
1135 
1136   // FIXME: The module file's FileEntry makes a poor key indeed!
1137   return (ModuleKey)ModuleFile;
1138 }
1139 
1140