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