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