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