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