CompilerInstance.cpp revision 263508
1234353Sdim//===--- CompilerInstance.cpp ---------------------------------------------===//
2193323Sed//
3353358Sdim//                     The LLVM Compiler Infrastructure
4353358Sdim//
5353358Sdim// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed
10193323Sed#include "clang/Frontend/CompilerInstance.h"
11193323Sed#include "clang/AST/ASTConsumer.h"
12193323Sed#include "clang/AST/ASTContext.h"
13234353Sdim#include "clang/AST/Decl.h"
14249423Sdim#include "clang/Basic/Diagnostic.h"
15193323Sed#include "clang/Basic/FileManager.h"
16249423Sdim#include "clang/Basic/SourceManager.h"
17276479Sdim#include "clang/Basic/TargetInfo.h"
18249423Sdim#include "clang/Basic/Version.h"
19249423Sdim#include "clang/Frontend/ChainedDiagnosticConsumer.h"
20276479Sdim#include "clang/Frontend/FrontendAction.h"
21276479Sdim#include "clang/Frontend/FrontendActions.h"
22276479Sdim#include "clang/Frontend/FrontendDiagnostic.h"
23206274Srdivacky#include "clang/Frontend/LogDiagnosticPrinter.h"
24193323Sed#include "clang/Frontend/SerializedDiagnosticPrinter.h"
25198090Srdivacky#include "clang/Frontend/TextDiagnosticPrinter.h"
26226633Sdim#include "clang/Frontend/Utils.h"
27193323Sed#include "clang/Frontend/VerifyDiagnosticConsumer.h"
28276479Sdim#include "clang/Lex/HeaderSearch.h"
29276479Sdim#include "clang/Lex/PTHManager.h"
30261991Sdim#include "clang/Lex/Preprocessor.h"
31224145Sdim#include "clang/Sema/CodeCompleteConsumer.h"
32224145Sdim#include "clang/Sema/Sema.h"
33193323Sed#include "clang/Serialization/ASTReader.h"
34193323Sed#include "llvm/ADT/Statistic.h"
35193323Sed#include "llvm/Config/config.h"
36193323Sed#include "llvm/Support/CrashRecoveryContext.h"
37193323Sed#include "llvm/Support/FileSystem.h"
38193323Sed#include "llvm/Support/Host.h"
39193323Sed#include "llvm/Support/LockFileManager.h"
40193323Sed#include "llvm/Support/MemoryBuffer.h"
41193323Sed#include "llvm/Support/Path.h"
42193323Sed#include "llvm/Support/Program.h"
43193323Sed#include "llvm/Support/Signals.h"
44193323Sed#include "llvm/Support/Timer.h"
45261991Sdim#include "llvm/Support/raw_ostream.h"
46261991Sdim#include "llvm/Support/system_error.h"
47261991Sdim#include <sys/stat.h>
48198090Srdivacky#include <time.h>
49224145Sdim
50261991Sdimusing namespace clang;
51193323Sed
52193323SedCompilerInstance::CompilerInstance()
53193323Sed  : Invocation(new CompilerInvocation()), ModuleManager(0),
54193323Sed    BuildGlobalModuleIndex(false), ModuleBuildFailed(false) {
55193323Sed}
56193323Sed
57193323SedCompilerInstance::~CompilerInstance() {
58193323Sed  assert(OutputFiles.empty() && "Still output files in flight?");
59193323Sed}
60193323Sed
61193323Sedvoid CompilerInstance::setInvocation(CompilerInvocation *Value) {
62309124Sdim  Invocation = Value;
63309124Sdim}
64309124Sdim
65341825Sdimbool CompilerInstance::shouldBuildGlobalModuleIndex() const {
66193323Sed  return (BuildGlobalModuleIndex ||
67309124Sdim          (ModuleManager && ModuleManager->isGlobalIndexUnavailable() &&
68309124Sdim           getFrontendOpts().GenerateGlobalModuleIndex)) &&
69309124Sdim         !ModuleBuildFailed;
70309124Sdim}
71309124Sdim
72193323Sedvoid CompilerInstance::setDiagnostics(DiagnosticsEngine *Value) {
73193323Sed  Diagnostics = Value;
74193323Sed}
75193323Sed
76341825Sdimvoid CompilerInstance::setTarget(TargetInfo *Value) {
77193323Sed  Target = Value;
78193323Sed}
79193323Sed
80193323Sedvoid CompilerInstance::setFileManager(FileManager *Value) {
81193323Sed  FileMgr = Value;
82309124Sdim}
83309124Sdim
84309124Sdimvoid CompilerInstance::setSourceManager(SourceManager *Value) {
85193323Sed  SourceMgr = Value;
86193323Sed}
87309124Sdim
88309124Sdimvoid CompilerInstance::setPreprocessor(Preprocessor *Value) { PP = Value; }
89309124Sdim
90309124Sdimvoid CompilerInstance::setASTContext(ASTContext *Value) { Context = Value; }
91309124Sdim
92193323Sedvoid CompilerInstance::setSema(Sema *S) {
93193323Sed  TheSema.reset(S);
94193323Sed}
95193323Sed
96193323Sedvoid CompilerInstance::setASTConsumer(ASTConsumer *Value) {
97193323Sed  Consumer.reset(Value);
98193323Sed}
99193323Sed
100193323Sedvoid CompilerInstance::setCodeCompletionConsumer(CodeCompleteConsumer *Value) {
101193323Sed  CompletionConsumer.reset(Value);
102193323Sed}
103193323Sed
104193323Sed// Diagnostics
105193323Sedstatic void SetUpDiagnosticLog(DiagnosticOptions *DiagOpts,
106193323Sed                               const CodeGenOptions *CodeGenOpts,
107193323Sed                               DiagnosticsEngine &Diags) {
108193323Sed  std::string ErrorInfo;
109193323Sed  bool OwnsStream = false;
110193323Sed  raw_ostream *OS = &llvm::errs();
111193323Sed  if (DiagOpts->DiagnosticLogFile != "-") {
112193323Sed    // Create the output stream.
113193323Sed    llvm::raw_fd_ostream *FileOS(
114193323Sed        new llvm::raw_fd_ostream(DiagOpts->DiagnosticLogFile.c_str(), ErrorInfo,
115193323Sed                                 llvm::sys::fs::F_Append));
116193323Sed    if (!ErrorInfo.empty()) {
117193323Sed      Diags.Report(diag::warn_fe_cc_log_diagnostics_failure)
118193323Sed        << DiagOpts->DiagnosticLogFile << ErrorInfo;
119193323Sed    } else {
120193323Sed      FileOS->SetUnbuffered();
121193323Sed      FileOS->SetUseAtomicWrites(true);
122193323Sed      OS = FileOS;
123193323Sed      OwnsStream = true;
124193323Sed    }
125193323Sed  }
126204642Srdivacky
127204642Srdivacky  // Chain in the diagnostic client which will log the diagnostics.
128204642Srdivacky  LogDiagnosticPrinter *Logger = new LogDiagnosticPrinter(*OS, DiagOpts,
129204642Srdivacky                                                          OwnsStream);
130204642Srdivacky  if (CodeGenOpts)
131341825Sdim    Logger->setDwarfDebugFlags(CodeGenOpts->DwarfDebugFlags);
132193323Sed  Diags.setClient(new ChainedDiagnosticConsumer(Diags.takeClient(), Logger));
133341825Sdim}
134193323Sed
135193323Sedstatic void SetupSerializedDiagnostics(DiagnosticOptions *DiagOpts,
136193323Sed                                       DiagnosticsEngine &Diags,
137193323Sed                                       StringRef OutputFile) {
138193323Sed  std::string ErrorInfo;
139193323Sed  OwningPtr<llvm::raw_fd_ostream> OS;
140193323Sed  OS.reset(new llvm::raw_fd_ostream(OutputFile.str().c_str(), ErrorInfo,
141193323Sed                                    llvm::sys::fs::F_Binary));
142193323Sed
143193323Sed  if (!ErrorInfo.empty()) {
144193323Sed    Diags.Report(diag::warn_fe_serialized_diag_failure)
145193323Sed      << OutputFile << ErrorInfo;
146341825Sdim    return;
147193323Sed  }
148193323Sed
149198090Srdivacky  DiagnosticConsumer *SerializedConsumer =
150193323Sed    clang::serialized_diags::create(OS.take(), DiagOpts);
151193323Sed
152193323Sed
153193323Sed  Diags.setClient(new ChainedDiagnosticConsumer(Diags.takeClient(),
154193323Sed                                                SerializedConsumer));
155341825Sdim}
156193323Sed
157193323Sedvoid CompilerInstance::createDiagnostics(DiagnosticConsumer *Client,
158193323Sed                                         bool ShouldOwnClient) {
159193323Sed  Diagnostics = createDiagnostics(&getDiagnosticOpts(), Client,
160198090Srdivacky                                  ShouldOwnClient, &getCodeGenOpts());
161193323Sed}
162193323Sed
163193323SedIntrusiveRefCntPtr<DiagnosticsEngine>
164193323SedCompilerInstance::createDiagnostics(DiagnosticOptions *Opts,
165193323Sed                                    DiagnosticConsumer *Client,
166193323Sed                                    bool ShouldOwnClient,
167193323Sed                                    const CodeGenOptions *CodeGenOpts) {
168193323Sed  IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
169193323Sed  IntrusiveRefCntPtr<DiagnosticsEngine>
170193323Sed      Diags(new DiagnosticsEngine(DiagID, Opts));
171193323Sed
172193323Sed  // Create the diagnostic client for reporting errors or for
173193323Sed  // implementing -verify.
174193323Sed  if (Client) {
175193323Sed    Diags->setClient(Client, ShouldOwnClient);
176193323Sed  } else
177193323Sed    Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts));
178193323Sed
179193323Sed  // Chain in -verify checker, if requested.
180193323Sed  if (Opts->VerifyDiagnostics)
181193323Sed    Diags->setClient(new VerifyDiagnosticConsumer(*Diags));
182193323Sed
183193323Sed  // Chain in -diagnostic-log-file dumper, if requested.
184193323Sed  if (!Opts->DiagnosticLogFile.empty())
185193323Sed    SetUpDiagnosticLog(Opts, CodeGenOpts, *Diags);
186314564Sdim
187193323Sed  if (!Opts->DiagnosticSerializationFile.empty())
188193323Sed    SetupSerializedDiagnostics(Opts, *Diags,
189309124Sdim                               Opts->DiagnosticSerializationFile);
190309124Sdim
191309124Sdim  // Configure our handling of diagnostics.
192309124Sdim  ProcessWarningOptions(*Diags, *Opts);
193309124Sdim
194193323Sed  return Diags;
195288943Sdim}
196288943Sdim
197193323Sed// File Manager
198288943Sdim
199309124Sdimvoid CompilerInstance::createFileManager() {
200206083Srdivacky  FileMgr = new FileManager(getFileSystemOpts());
201193323Sed}
202193323Sed
203314564Sdim// Source Manager
204314564Sdim
205193323Sedvoid CompilerInstance::createSourceManager(FileManager &FileMgr) {
206309124Sdim  SourceMgr = new SourceManager(getDiagnostics(), FileMgr);
207193323Sed}
208193323Sed
209193323Sed// Preprocessor
210193323Sed
211341825Sdimvoid CompilerInstance::createPreprocessor() {
212193323Sed  const PreprocessorOptions &PPOpts = getPreprocessorOpts();
213193323Sed
214193323Sed  // Create a PTH manager if we are using some form of a token cache.
215341825Sdim  PTHManager *PTHMgr = 0;
216193323Sed  if (!PPOpts.TokenCache.empty())
217193323Sed    PTHMgr = PTHManager::Create(PPOpts.TokenCache, getDiagnostics());
218193323Sed
219193323Sed  // Create the Preprocessor.
220193323Sed  HeaderSearch *HeaderInfo = new HeaderSearch(&getHeaderSearchOpts(),
221193323Sed                                              getSourceManager(),
222193323Sed                                              getDiagnostics(),
223193323Sed                                              getLangOpts(),
224341825Sdim                                              &getTarget());
225193323Sed  PP = new Preprocessor(&getPreprocessorOpts(),
226314564Sdim                        getDiagnostics(), getLangOpts(), &getTarget(),
227193323Sed                        getSourceManager(), *HeaderInfo, *this, PTHMgr,
228193323Sed                        /*OwnsHeaderSearch=*/true);
229309124Sdim
230193323Sed  // Note that this is different then passing PTHMgr to Preprocessor's ctor.
231341825Sdim  // That argument is used as the IdentifierInfoLookup argument to
232193323Sed  // IdentifierTable's ctor.
233193323Sed  if (PTHMgr) {
234341825Sdim    PTHMgr->setPreprocessor(&*PP);
235193323Sed    PP->setPTHManager(PTHMgr);
236193323Sed  }
237193323Sed
238193323Sed  if (PPOpts.DetailedRecord)
239193323Sed    PP->createPreprocessingRecord();
240193323Sed
241193323Sed  InitializePreprocessor(*PP, PPOpts, getHeaderSearchOpts(), getFrontendOpts());
242193323Sed
243193323Sed  PP->setPreprocessedOutput(getPreprocessorOutputOpts().ShowCPP);
244193323Sed
245193323Sed  // Set up the module path, including the hash for the
246193323Sed  // module-creation options.
247341825Sdim  SmallString<256> SpecificModuleCache(
248193323Sed                           getHeaderSearchOpts().ModuleCachePath);
249193323Sed  if (!getHeaderSearchOpts().DisableModuleHash)
250341825Sdim    llvm::sys::path::append(SpecificModuleCache,
251193323Sed                            getInvocation().getModuleHash());
252193323Sed  PP->getHeaderSearchInfo().setModuleCachePath(SpecificModuleCache);
253193323Sed
254193323Sed  // Handle generating dependencies, if requested.
255193323Sed  const DependencyOutputOptions &DepOpts = getDependencyOutputOpts();
256193323Sed  if (!DepOpts.OutputFile.empty())
257193323Sed    AttachDependencyFileGen(*PP, DepOpts);
258193323Sed  if (!DepOpts.DOTOutputFile.empty())
259204642Srdivacky    AttachDependencyGraphGen(*PP, DepOpts.DOTOutputFile,
260204642Srdivacky                             getHeaderSearchOpts().Sysroot);
261204642Srdivacky
262204642Srdivacky
263204642Srdivacky  // Handle generating header include information, if requested.
264204642Srdivacky  if (DepOpts.ShowHeaderIncludes)
265204642Srdivacky    AttachHeaderIncludeGen(*PP);
266204642Srdivacky  if (!DepOpts.HeaderIncludeOutputFile.empty()) {
267193323Sed    StringRef OutputPath = DepOpts.HeaderIncludeOutputFile;
268193323Sed    if (OutputPath == "-")
269193323Sed      OutputPath = "";
270193323Sed    AttachHeaderIncludeGen(*PP, /*ShowAllHeaders=*/true, OutputPath,
271314564Sdim                           /*ShowDepth=*/false);
272309124Sdim  }
273309124Sdim
274309124Sdim  if (DepOpts.PrintShowIncludes) {
275314564Sdim    AttachHeaderIncludeGen(*PP, /*ShowAllHeaders=*/false, /*OutputPath=*/"",
276314564Sdim                           /*ShowDepth=*/true, /*MSStyle=*/true);
277193323Sed  }
278314564Sdim}
279193323Sed
280193323Sed// ASTContext
281314564Sdim
282314564Sdimvoid CompilerInstance::createASTContext() {
283276479Sdim  Preprocessor &PP = getPreprocessor();
284193323Sed  Context = new ASTContext(getLangOpts(), PP.getSourceManager(),
285193323Sed                           &getTarget(), PP.getIdentifierTable(),
286210299Sed                           PP.getSelectorTable(), PP.getBuiltinInfo(),
287193323Sed                           /*size_reserve=*/ 0);
288193323Sed}
289193323Sed
290210299Sed// ExternalASTSource
291193323Sed
292193323Sedvoid CompilerInstance::createPCHExternalASTSource(StringRef Path,
293193323Sed                                                  bool DisablePCHValidation,
294193323Sed                                                bool AllowPCHWithCompilerErrors,
295341825Sdim                                                 void *DeserializationListener){
296193323Sed  OwningPtr<ExternalASTSource> Source;
297193323Sed  bool Preamble = getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
298193323Sed  Source.reset(createPCHExternalASTSource(Path, getHeaderSearchOpts().Sysroot,
299210299Sed                                          DisablePCHValidation,
300193323Sed                                          AllowPCHWithCompilerErrors,
301210299Sed                                          getPreprocessor(), getASTContext(),
302193323Sed                                          DeserializationListener,
303193323Sed                                          Preamble,
304193323Sed                                       getFrontendOpts().UseGlobalModuleIndex));
305193323Sed  ModuleManager = static_cast<ASTReader*>(Source.get());
306314564Sdim  getASTContext().setExternalSource(Source);
307314564Sdim}
308314564Sdim
309288943SdimExternalASTSource *
310288943SdimCompilerInstance::createPCHExternalASTSource(StringRef Path,
311288943Sdim                                             const std::string &Sysroot,
312288943Sdim                                             bool DisablePCHValidation,
313193323Sed                                             bool AllowPCHWithCompilerErrors,
314193323Sed                                             Preprocessor &PP,
315341825Sdim                                             ASTContext &Context,
316193323Sed                                             void *DeserializationListener,
317193323Sed                                             bool Preamble,
318341825Sdim                                             bool UseGlobalModuleIndex) {
319193323Sed  OwningPtr<ASTReader> Reader;
320193323Sed  Reader.reset(new ASTReader(PP, Context,
321193323Sed                             Sysroot.empty() ? "" : Sysroot.c_str(),
322193323Sed                             DisablePCHValidation,
323193323Sed                             AllowPCHWithCompilerErrors,
324193323Sed                             UseGlobalModuleIndex));
325341825Sdim
326193323Sed  Reader->setDeserializationListener(
327193323Sed            static_cast<ASTDeserializationListener *>(DeserializationListener));
328193323Sed  switch (Reader->ReadAST(Path,
329193323Sed                          Preamble ? serialization::MK_Preamble
330193323Sed                                   : serialization::MK_PCH,
331210299Sed                          SourceLocation(),
332309124Sdim                          ASTReader::ARR_None)) {
333360784Sdim  case ASTReader::Success:
334360784Sdim    // Set the predefines buffer as suggested by the PCH reader. Typically, the
335210299Sed    // predefines buffer will be empty.
336210299Sed    PP.setPredefines(Reader->getSuggestedPredefines());
337193323Sed    return Reader.take();
338210299Sed
339210299Sed  case ASTReader::Failure:
340210299Sed    // Unrecoverable failure: don't even try to process the input file.
341210299Sed    break;
342210299Sed
343193323Sed  case ASTReader::Missing:
344341825Sdim  case ASTReader::OutOfDate:
345210299Sed  case ASTReader::VersionMismatch:
346210299Sed  case ASTReader::ConfigurationMismatch:
347210299Sed  case ASTReader::HadErrors:
348193323Sed    // No suitable PCH file could be found. Return an error.
349210299Sed    break;
350210299Sed  }
351193323Sed
352210299Sed  return 0;
353210299Sed}
354193323Sed
355210299Sed// Code Completion
356193323Sed
357193323Sedstatic bool EnableCodeCompletion(Preprocessor &PP,
358193323Sed                                 const std::string &Filename,
359193323Sed                                 unsigned Line,
360193323Sed                                 unsigned Column) {
361193323Sed  // Tell the source manager to chop off the given file at a specific
362208599Srdivacky  // line and column.
363208599Srdivacky  const FileEntry *Entry = PP.getFileManager().getFile(Filename);
364193323Sed  if (!Entry) {
365206124Srdivacky    PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file)
366341825Sdim      << Filename;
367276479Sdim    return true;
368276479Sdim  }
369314564Sdim
370296417Sdim  // Truncate the named file at the given line/column.
371296417Sdim  PP.SetCodeCompletionPoint(Entry, Line, Column);
372296417Sdim  return false;
373296417Sdim}
374193323Sed
375193323Sedvoid CompilerInstance::createCodeCompletionConsumer() {
376193323Sed  const ParsedSourceLocation &Loc = getFrontendOpts().CodeCompletionAt;
377276479Sdim  if (!CompletionConsumer) {
378276479Sdim    setCodeCompletionConsumer(
379193323Sed      createCodeCompletionConsumer(getPreprocessor(),
380193323Sed                                   Loc.FileName, Loc.Line, Loc.Column,
381193323Sed                                   getFrontendOpts().CodeCompleteOpts,
382193323Sed                                   llvm::outs()));
383193323Sed    if (!CompletionConsumer)
384208599Srdivacky      return;
385208599Srdivacky  } else if (EnableCodeCompletion(getPreprocessor(), Loc.FileName,
386193323Sed                                  Loc.Line, Loc.Column)) {
387206124Srdivacky    setCodeCompletionConsumer(0);
388341825Sdim    return;
389276479Sdim  }
390276479Sdim
391314564Sdim  if (CompletionConsumer->isOutputBinary() &&
392296417Sdim      llvm::sys::ChangeStdoutToBinary()) {
393296417Sdim    getPreprocessor().getDiagnostics().Report(diag::err_fe_stdout_binary);
394296417Sdim    setCodeCompletionConsumer(0);
395296417Sdim  }
396193323Sed}
397193323Sed
398276479Sdimvoid CompilerInstance::createFrontendTimer() {
399276479Sdim  FrontendTimer.reset(new llvm::Timer("Clang front-end timer"));
400193323Sed}
401193323Sed
402193323SedCodeCompleteConsumer *
403314564SdimCompilerInstance::createCodeCompletionConsumer(Preprocessor &PP,
404314564Sdim                                               const std::string &Filename,
405193323Sed                                               unsigned Line,
406193323Sed                                               unsigned Column,
407193323Sed                                               const CodeCompleteOptions &Opts,
408193323Sed                                               raw_ostream &OS) {
409276479Sdim  if (EnableCodeCompletion(PP, Filename, Line, Column))
410276479Sdim    return 0;
411276479Sdim
412276479Sdim  // Set up the creation routine for code-completion.
413276479Sdim  return new PrintingCodeCompleteConsumer(Opts, OS);
414276479Sdim}
415276479Sdim
416276479Sdimvoid CompilerInstance::createSema(TranslationUnitKind TUKind,
417276479Sdim                                  CodeCompleteConsumer *CompletionConsumer) {
418276479Sdim  TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(),
419276479Sdim                         TUKind, CompletionConsumer));
420276479Sdim}
421276479Sdim
422276479Sdim// Output Files
423276479Sdim
424276479Sdimvoid CompilerInstance::addOutputFile(const OutputFile &OutFile) {
425276479Sdim  assert(OutFile.OS && "Attempt to add empty stream to output list!");
426276479Sdim  OutputFiles.push_back(OutFile);
427276479Sdim}
428276479Sdim
429276479Sdimvoid CompilerInstance::clearOutputFiles(bool EraseFiles) {
430276479Sdim  for (std::list<OutputFile>::iterator
431341825Sdim         it = OutputFiles.begin(), ie = OutputFiles.end(); it != ie; ++it) {
432276479Sdim    delete it->OS;
433276479Sdim    if (!it->TempFilename.empty()) {
434276479Sdim      if (EraseFiles) {
435280031Sdim        bool existed;
436280031Sdim        llvm::sys::fs::remove(it->TempFilename, existed);
437280031Sdim      } else {
438276479Sdim        SmallString<128> NewOutFile(it->Filename);
439276479Sdim
440276479Sdim        // If '-working-directory' was passed, the output filename should be
441280031Sdim        // relative to that.
442276479Sdim        FileMgr->FixupRelativePath(NewOutFile);
443276479Sdim        if (llvm::error_code ec = llvm::sys::fs::rename(it->TempFilename,
444276479Sdim                                                        NewOutFile.str())) {
445327952Sdim          getDiagnostics().Report(diag::err_unable_to_rename_temp)
446276479Sdim            << it->TempFilename << it->Filename << ec.message();
447276479Sdim
448280031Sdim          bool existed;
449280031Sdim          llvm::sys::fs::remove(it->TempFilename, existed);
450276479Sdim        }
451      }
452    } else if (!it->Filename.empty() && EraseFiles)
453      llvm::sys::fs::remove(it->Filename);
454
455  }
456  OutputFiles.clear();
457}
458
459llvm::raw_fd_ostream *
460CompilerInstance::createDefaultOutputFile(bool Binary,
461                                          StringRef InFile,
462                                          StringRef Extension) {
463  return createOutputFile(getFrontendOpts().OutputFile, Binary,
464                          /*RemoveFileOnSignal=*/true, InFile, Extension,
465                          /*UseTemporary=*/true);
466}
467
468llvm::raw_fd_ostream *
469CompilerInstance::createOutputFile(StringRef OutputPath,
470                                   bool Binary, bool RemoveFileOnSignal,
471                                   StringRef InFile,
472                                   StringRef Extension,
473                                   bool UseTemporary,
474                                   bool CreateMissingDirectories) {
475  std::string Error, OutputPathName, TempPathName;
476  llvm::raw_fd_ostream *OS = createOutputFile(OutputPath, Error, Binary,
477                                              RemoveFileOnSignal,
478                                              InFile, Extension,
479                                              UseTemporary,
480                                              CreateMissingDirectories,
481                                              &OutputPathName,
482                                              &TempPathName);
483  if (!OS) {
484    getDiagnostics().Report(diag::err_fe_unable_to_open_output)
485      << OutputPath << Error;
486    return 0;
487  }
488
489  // Add the output file -- but don't try to remove "-", since this means we are
490  // using stdin.
491  addOutputFile(OutputFile((OutputPathName != "-") ? OutputPathName : "",
492                TempPathName, OS));
493
494  return OS;
495}
496
497llvm::raw_fd_ostream *
498CompilerInstance::createOutputFile(StringRef OutputPath,
499                                   std::string &Error,
500                                   bool Binary,
501                                   bool RemoveFileOnSignal,
502                                   StringRef InFile,
503                                   StringRef Extension,
504                                   bool UseTemporary,
505                                   bool CreateMissingDirectories,
506                                   std::string *ResultPathName,
507                                   std::string *TempPathName) {
508  assert((!CreateMissingDirectories || UseTemporary) &&
509         "CreateMissingDirectories is only allowed when using temporary files");
510
511  std::string OutFile, TempFile;
512  if (!OutputPath.empty()) {
513    OutFile = OutputPath;
514  } else if (InFile == "-") {
515    OutFile = "-";
516  } else if (!Extension.empty()) {
517    SmallString<128> Path(InFile);
518    llvm::sys::path::replace_extension(Path, Extension);
519    OutFile = Path.str();
520  } else {
521    OutFile = "-";
522  }
523
524  OwningPtr<llvm::raw_fd_ostream> OS;
525  std::string OSFile;
526
527  if (UseTemporary) {
528    if (OutFile == "-")
529      UseTemporary = false;
530    else {
531      llvm::sys::fs::file_status Status;
532      llvm::sys::fs::status(OutputPath, Status);
533      if (llvm::sys::fs::exists(Status)) {
534        // Fail early if we can't write to the final destination.
535        if (!llvm::sys::fs::can_write(OutputPath))
536          return 0;
537
538        // Don't use a temporary if the output is a special file. This handles
539        // things like '-o /dev/null'
540        if (!llvm::sys::fs::is_regular_file(Status))
541          UseTemporary = false;
542      }
543    }
544  }
545
546  if (UseTemporary) {
547    // Create a temporary file.
548    SmallString<128> TempPath;
549    TempPath = OutFile;
550    TempPath += "-%%%%%%%%";
551    int fd;
552    llvm::error_code EC =
553        llvm::sys::fs::createUniqueFile(TempPath.str(), fd, TempPath);
554
555    if (CreateMissingDirectories &&
556        EC == llvm::errc::no_such_file_or_directory) {
557      StringRef Parent = llvm::sys::path::parent_path(OutputPath);
558      EC = llvm::sys::fs::create_directories(Parent);
559      if (!EC) {
560        EC = llvm::sys::fs::createUniqueFile(TempPath.str(), fd, TempPath);
561      }
562    }
563
564    if (!EC) {
565      OS.reset(new llvm::raw_fd_ostream(fd, /*shouldClose=*/true));
566      OSFile = TempFile = TempPath.str();
567    }
568    // If we failed to create the temporary, fallback to writing to the file
569    // directly. This handles the corner case where we cannot write to the
570    // directory, but can write to the file.
571  }
572
573  if (!OS) {
574    OSFile = OutFile;
575    OS.reset(new llvm::raw_fd_ostream(
576        OSFile.c_str(), Error,
577        (Binary ? llvm::sys::fs::F_Binary : llvm::sys::fs::F_None)));
578    if (!Error.empty())
579      return 0;
580  }
581
582  // Make sure the out stream file gets removed if we crash.
583  if (RemoveFileOnSignal)
584    llvm::sys::RemoveFileOnSignal(OSFile);
585
586  if (ResultPathName)
587    *ResultPathName = OutFile;
588  if (TempPathName)
589    *TempPathName = TempFile;
590
591  return OS.take();
592}
593
594// Initialization Utilities
595
596bool CompilerInstance::InitializeSourceManager(const FrontendInputFile &Input){
597  return InitializeSourceManager(Input, getDiagnostics(),
598                                 getFileManager(), getSourceManager(),
599                                 getFrontendOpts());
600}
601
602bool CompilerInstance::InitializeSourceManager(const FrontendInputFile &Input,
603                                               DiagnosticsEngine &Diags,
604                                               FileManager &FileMgr,
605                                               SourceManager &SourceMgr,
606                                               const FrontendOptions &Opts) {
607  SrcMgr::CharacteristicKind
608    Kind = Input.isSystem() ? SrcMgr::C_System : SrcMgr::C_User;
609
610  if (Input.isBuffer()) {
611    SourceMgr.createMainFileIDForMemBuffer(Input.getBuffer(), Kind);
612    assert(!SourceMgr.getMainFileID().isInvalid() &&
613           "Couldn't establish MainFileID!");
614    return true;
615  }
616
617  StringRef InputFile = Input.getFile();
618
619  // Figure out where to get and map in the main file.
620  if (InputFile != "-") {
621    const FileEntry *File = FileMgr.getFile(InputFile, /*OpenFile=*/true);
622    if (!File) {
623      Diags.Report(diag::err_fe_error_reading) << InputFile;
624      return false;
625    }
626
627    // The natural SourceManager infrastructure can't currently handle named
628    // pipes, but we would at least like to accept them for the main
629    // file. Detect them here, read them with the volatile flag so FileMgr will
630    // pick up the correct size, and simply override their contents as we do for
631    // STDIN.
632    if (File->isNamedPipe()) {
633      std::string ErrorStr;
634      if (llvm::MemoryBuffer *MB =
635              FileMgr.getBufferForFile(File, &ErrorStr, /*isVolatile=*/true)) {
636        // Create a new virtual file that will have the correct size.
637        File = FileMgr.getVirtualFile(InputFile, MB->getBufferSize(), 0);
638        SourceMgr.overrideFileContents(File, MB);
639      } else {
640        Diags.Report(diag::err_cannot_open_file) << InputFile << ErrorStr;
641        return false;
642      }
643    }
644
645    SourceMgr.createMainFileID(File, Kind);
646  } else {
647    OwningPtr<llvm::MemoryBuffer> SB;
648    if (llvm::error_code ec = llvm::MemoryBuffer::getSTDIN(SB)) {
649      Diags.Report(diag::err_fe_error_reading_stdin) << ec.message();
650      return false;
651    }
652    const FileEntry *File = FileMgr.getVirtualFile(SB->getBufferIdentifier(),
653                                                   SB->getBufferSize(), 0);
654    SourceMgr.createMainFileID(File, Kind);
655    SourceMgr.overrideFileContents(File, SB.take());
656  }
657
658  assert(!SourceMgr.getMainFileID().isInvalid() &&
659         "Couldn't establish MainFileID!");
660  return true;
661}
662
663// High-Level Operations
664
665bool CompilerInstance::ExecuteAction(FrontendAction &Act) {
666  assert(hasDiagnostics() && "Diagnostics engine is not initialized!");
667  assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!");
668  assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!");
669
670  // FIXME: Take this as an argument, once all the APIs we used have moved to
671  // taking it as an input instead of hard-coding llvm::errs.
672  raw_ostream &OS = llvm::errs();
673
674  // Create the target instance.
675  setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), &getTargetOpts()));
676  if (!hasTarget())
677    return false;
678
679  // Inform the target of the language options.
680  //
681  // FIXME: We shouldn't need to do this, the target should be immutable once
682  // created. This complexity should be lifted elsewhere.
683  getTarget().setForcedLangOptions(getLangOpts());
684
685  // rewriter project will change target built-in bool type from its default.
686  if (getFrontendOpts().ProgramAction == frontend::RewriteObjC)
687    getTarget().noSignedCharForObjCBool();
688
689  // Validate/process some options.
690  if (getHeaderSearchOpts().Verbose)
691    OS << "clang -cc1 version " CLANG_VERSION_STRING
692       << " based upon " << PACKAGE_STRING
693       << " default target " << llvm::sys::getDefaultTargetTriple() << "\n";
694
695  if (getFrontendOpts().ShowTimers)
696    createFrontendTimer();
697
698  if (getFrontendOpts().ShowStats)
699    llvm::EnableStatistics();
700
701  for (unsigned i = 0, e = getFrontendOpts().Inputs.size(); i != e; ++i) {
702    // Reset the ID tables if we are reusing the SourceManager.
703    if (hasSourceManager())
704      getSourceManager().clearIDTables();
705
706    if (Act.BeginSourceFile(*this, getFrontendOpts().Inputs[i])) {
707      Act.Execute();
708      Act.EndSourceFile();
709    }
710  }
711
712  // Notify the diagnostic client that all files were processed.
713  getDiagnostics().getClient()->finish();
714
715  if (getDiagnosticOpts().ShowCarets) {
716    // We can have multiple diagnostics sharing one diagnostic client.
717    // Get the total number of warnings/errors from the client.
718    unsigned NumWarnings = getDiagnostics().getClient()->getNumWarnings();
719    unsigned NumErrors = getDiagnostics().getClient()->getNumErrors();
720
721    if (NumWarnings)
722      OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s");
723    if (NumWarnings && NumErrors)
724      OS << " and ";
725    if (NumErrors)
726      OS << NumErrors << " error" << (NumErrors == 1 ? "" : "s");
727    if (NumWarnings || NumErrors)
728      OS << " generated.\n";
729  }
730
731  if (getFrontendOpts().ShowStats && hasFileManager()) {
732    getFileManager().PrintStats();
733    OS << "\n";
734  }
735
736  return !getDiagnostics().getClient()->getNumErrors();
737}
738
739/// \brief Determine the appropriate source input kind based on language
740/// options.
741static InputKind getSourceInputKindFromOptions(const LangOptions &LangOpts) {
742  if (LangOpts.OpenCL)
743    return IK_OpenCL;
744  if (LangOpts.CUDA)
745    return IK_CUDA;
746  if (LangOpts.ObjC1)
747    return LangOpts.CPlusPlus? IK_ObjCXX : IK_ObjC;
748  return LangOpts.CPlusPlus? IK_CXX : IK_C;
749}
750
751namespace {
752  struct CompileModuleMapData {
753    CompilerInstance &Instance;
754    GenerateModuleAction &CreateModuleAction;
755  };
756}
757
758/// \brief Helper function that executes the module-generating action under
759/// a crash recovery context.
760static void doCompileMapModule(void *UserData) {
761  CompileModuleMapData &Data
762    = *reinterpret_cast<CompileModuleMapData *>(UserData);
763  Data.Instance.ExecuteAction(Data.CreateModuleAction);
764}
765
766namespace {
767  /// \brief Function object that checks with the given macro definition should
768  /// be removed, because it is one of the ignored macros.
769  class RemoveIgnoredMacro {
770    const HeaderSearchOptions &HSOpts;
771
772  public:
773    explicit RemoveIgnoredMacro(const HeaderSearchOptions &HSOpts)
774      : HSOpts(HSOpts) { }
775
776    bool operator()(const std::pair<std::string, bool> &def) const {
777      StringRef MacroDef = def.first;
778      return HSOpts.ModulesIgnoreMacros.count(MacroDef.split('=').first) > 0;
779    }
780  };
781}
782
783/// \brief Compile a module file for the given module, using the options
784/// provided by the importing compiler instance.
785static void compileModule(CompilerInstance &ImportingInstance,
786                          SourceLocation ImportLoc,
787                          Module *Module,
788                          StringRef ModuleFileName) {
789  // FIXME: have LockFileManager return an error_code so that we can
790  // avoid the mkdir when the directory already exists.
791  StringRef Dir = llvm::sys::path::parent_path(ModuleFileName);
792  llvm::sys::fs::create_directories(Dir);
793
794  llvm::LockFileManager Locked(ModuleFileName);
795  switch (Locked) {
796  case llvm::LockFileManager::LFS_Error:
797    return;
798
799  case llvm::LockFileManager::LFS_Owned:
800    // We're responsible for building the module ourselves. Do so below.
801    break;
802
803  case llvm::LockFileManager::LFS_Shared:
804    // Someone else is responsible for building the module. Wait for them to
805    // finish.
806    Locked.waitForUnlock();
807    return;
808  }
809
810  ModuleMap &ModMap
811    = ImportingInstance.getPreprocessor().getHeaderSearchInfo().getModuleMap();
812
813  // Construct a compiler invocation for creating this module.
814  IntrusiveRefCntPtr<CompilerInvocation> Invocation
815    (new CompilerInvocation(ImportingInstance.getInvocation()));
816
817  PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts();
818
819  // For any options that aren't intended to affect how a module is built,
820  // reset them to their default values.
821  Invocation->getLangOpts()->resetNonModularOptions();
822  PPOpts.resetNonModularOptions();
823
824  // Remove any macro definitions that are explicitly ignored by the module.
825  // They aren't supposed to affect how the module is built anyway.
826  const HeaderSearchOptions &HSOpts = Invocation->getHeaderSearchOpts();
827  PPOpts.Macros.erase(std::remove_if(PPOpts.Macros.begin(), PPOpts.Macros.end(),
828                                     RemoveIgnoredMacro(HSOpts)),
829                      PPOpts.Macros.end());
830
831
832  // Note the name of the module we're building.
833  Invocation->getLangOpts()->CurrentModule = Module->getTopLevelModuleName();
834
835  // Make sure that the failed-module structure has been allocated in
836  // the importing instance, and propagate the pointer to the newly-created
837  // instance.
838  PreprocessorOptions &ImportingPPOpts
839    = ImportingInstance.getInvocation().getPreprocessorOpts();
840  if (!ImportingPPOpts.FailedModules)
841    ImportingPPOpts.FailedModules = new PreprocessorOptions::FailedModulesSet;
842  PPOpts.FailedModules = ImportingPPOpts.FailedModules;
843
844  // If there is a module map file, build the module using the module map.
845  // Set up the inputs/outputs so that we build the module from its umbrella
846  // header.
847  FrontendOptions &FrontendOpts = Invocation->getFrontendOpts();
848  FrontendOpts.OutputFile = ModuleFileName.str();
849  FrontendOpts.DisableFree = false;
850  FrontendOpts.GenerateGlobalModuleIndex = false;
851  FrontendOpts.Inputs.clear();
852  InputKind IK = getSourceInputKindFromOptions(*Invocation->getLangOpts());
853
854  // Don't free the remapped file buffers; they are owned by our caller.
855  PPOpts.RetainRemappedFileBuffers = true;
856
857  Invocation->getDiagnosticOpts().VerifyDiagnostics = 0;
858  assert(ImportingInstance.getInvocation().getModuleHash() ==
859         Invocation->getModuleHash() && "Module hash mismatch!");
860
861  // Construct a compiler instance that will be used to actually create the
862  // module.
863  CompilerInstance Instance;
864  Instance.setInvocation(&*Invocation);
865
866  Instance.createDiagnostics(new ForwardingDiagnosticConsumer(
867                                   ImportingInstance.getDiagnosticClient()),
868                             /*ShouldOwnClient=*/true);
869
870  // Note that this module is part of the module build stack, so that we
871  // can detect cycles in the module graph.
872  Instance.createFileManager(); // FIXME: Adopt file manager from importer?
873  Instance.createSourceManager(Instance.getFileManager());
874  SourceManager &SourceMgr = Instance.getSourceManager();
875  SourceMgr.setModuleBuildStack(
876    ImportingInstance.getSourceManager().getModuleBuildStack());
877  SourceMgr.pushModuleBuildStack(Module->getTopLevelModuleName(),
878    FullSourceLoc(ImportLoc, ImportingInstance.getSourceManager()));
879
880  // Get or create the module map that we'll use to build this module.
881  std::string InferredModuleMapContent;
882  if (const FileEntry *ModuleMapFile =
883          ModMap.getContainingModuleMapFile(Module)) {
884    // Use the module map where this module resides.
885    FrontendOpts.Inputs.push_back(
886        FrontendInputFile(ModuleMapFile->getName(), IK));
887  } else {
888    llvm::raw_string_ostream OS(InferredModuleMapContent);
889    Module->print(OS);
890    OS.flush();
891    FrontendOpts.Inputs.push_back(
892        FrontendInputFile("__inferred_module.map", IK));
893
894    const llvm::MemoryBuffer *ModuleMapBuffer =
895        llvm::MemoryBuffer::getMemBuffer(InferredModuleMapContent);
896    ModuleMapFile = Instance.getFileManager().getVirtualFile(
897        "__inferred_module.map", InferredModuleMapContent.size(), 0);
898    SourceMgr.overrideFileContents(ModuleMapFile, ModuleMapBuffer);
899  }
900
901  // Construct a module-generating action.
902  GenerateModuleAction CreateModuleAction(Module->IsSystem);
903
904  // Execute the action to actually build the module in-place. Use a separate
905  // thread so that we get a stack large enough.
906  const unsigned ThreadStackSize = 8 << 20;
907  llvm::CrashRecoveryContext CRC;
908  CompileModuleMapData Data = { Instance, CreateModuleAction };
909  CRC.RunSafelyOnThread(&doCompileMapModule, &Data, ThreadStackSize);
910
911
912  // Delete the temporary module map file.
913  // FIXME: Even though we're executing under crash protection, it would still
914  // be nice to do this with RemoveFileOnSignal when we can. However, that
915  // doesn't make sense for all clients, so clean this up manually.
916  Instance.clearOutputFiles(/*EraseFiles=*/true);
917
918  // We've rebuilt a module. If we're allowed to generate or update the global
919  // module index, record that fact in the importing compiler instance.
920  if (ImportingInstance.getFrontendOpts().GenerateGlobalModuleIndex) {
921    ImportingInstance.setBuildGlobalModuleIndex(true);
922  }
923}
924
925/// \brief Diagnose differences between the current definition of the given
926/// configuration macro and the definition provided on the command line.
927static void checkConfigMacro(Preprocessor &PP, StringRef ConfigMacro,
928                             Module *Mod, SourceLocation ImportLoc) {
929  IdentifierInfo *Id = PP.getIdentifierInfo(ConfigMacro);
930  SourceManager &SourceMgr = PP.getSourceManager();
931
932  // If this identifier has never had a macro definition, then it could
933  // not have changed.
934  if (!Id->hadMacroDefinition())
935    return;
936
937  // If this identifier does not currently have a macro definition,
938  // check whether it had one on the command line.
939  if (!Id->hasMacroDefinition()) {
940    MacroDirective::DefInfo LatestDef =
941        PP.getMacroDirectiveHistory(Id)->getDefinition();
942    for (MacroDirective::DefInfo Def = LatestDef; Def;
943           Def = Def.getPreviousDefinition()) {
944      FileID FID = SourceMgr.getFileID(Def.getLocation());
945      if (FID.isInvalid())
946        continue;
947
948      // We only care about the predefines buffer.
949      if (FID != PP.getPredefinesFileID())
950        continue;
951
952      // This macro was defined on the command line, then #undef'd later.
953      // Complain.
954      PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
955        << true << ConfigMacro << Mod->getFullModuleName();
956      if (LatestDef.isUndefined())
957        PP.Diag(LatestDef.getUndefLocation(), diag::note_module_def_undef_here)
958          << true;
959      return;
960    }
961
962    // Okay: no definition in the predefines buffer.
963    return;
964  }
965
966  // This identifier has a macro definition. Check whether we had a definition
967  // on the command line.
968  MacroDirective::DefInfo LatestDef =
969      PP.getMacroDirectiveHistory(Id)->getDefinition();
970  MacroDirective::DefInfo PredefinedDef;
971  for (MacroDirective::DefInfo Def = LatestDef; Def;
972         Def = Def.getPreviousDefinition()) {
973    FileID FID = SourceMgr.getFileID(Def.getLocation());
974    if (FID.isInvalid())
975      continue;
976
977    // We only care about the predefines buffer.
978    if (FID != PP.getPredefinesFileID())
979      continue;
980
981    PredefinedDef = Def;
982    break;
983  }
984
985  // If there was no definition for this macro in the predefines buffer,
986  // complain.
987  if (!PredefinedDef ||
988      (!PredefinedDef.getLocation().isValid() &&
989       PredefinedDef.getUndefLocation().isValid())) {
990    PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
991      << false << ConfigMacro << Mod->getFullModuleName();
992    PP.Diag(LatestDef.getLocation(), diag::note_module_def_undef_here)
993      << false;
994    return;
995  }
996
997  // If the current macro definition is the same as the predefined macro
998  // definition, it's okay.
999  if (LatestDef.getMacroInfo() == PredefinedDef.getMacroInfo() ||
1000      LatestDef.getMacroInfo()->isIdenticalTo(*PredefinedDef.getMacroInfo(),PP,
1001                                              /*Syntactically=*/true))
1002    return;
1003
1004  // The macro definitions differ.
1005  PP.Diag(ImportLoc, diag::warn_module_config_macro_undef)
1006    << false << ConfigMacro << Mod->getFullModuleName();
1007  PP.Diag(LatestDef.getLocation(), diag::note_module_def_undef_here)
1008    << false;
1009}
1010
1011/// \brief Write a new timestamp file with the given path.
1012static void writeTimestampFile(StringRef TimestampFile) {
1013  std::string ErrorInfo;
1014  llvm::raw_fd_ostream Out(TimestampFile.str().c_str(), ErrorInfo,
1015                           llvm::sys::fs::F_Binary);
1016}
1017
1018/// \brief Prune the module cache of modules that haven't been accessed in
1019/// a long time.
1020static void pruneModuleCache(const HeaderSearchOptions &HSOpts) {
1021  struct stat StatBuf;
1022  llvm::SmallString<128> TimestampFile;
1023  TimestampFile = HSOpts.ModuleCachePath;
1024  llvm::sys::path::append(TimestampFile, "modules.timestamp");
1025
1026  // Try to stat() the timestamp file.
1027  if (::stat(TimestampFile.c_str(), &StatBuf)) {
1028    // If the timestamp file wasn't there, create one now.
1029    if (errno == ENOENT) {
1030      writeTimestampFile(TimestampFile);
1031    }
1032    return;
1033  }
1034
1035  // Check whether the time stamp is older than our pruning interval.
1036  // If not, do nothing.
1037  time_t TimeStampModTime = StatBuf.st_mtime;
1038  time_t CurrentTime = time(0);
1039  if (CurrentTime - TimeStampModTime <= time_t(HSOpts.ModuleCachePruneInterval))
1040    return;
1041
1042  // Write a new timestamp file so that nobody else attempts to prune.
1043  // There is a benign race condition here, if two Clang instances happen to
1044  // notice at the same time that the timestamp is out-of-date.
1045  writeTimestampFile(TimestampFile);
1046
1047  // Walk the entire module cache, looking for unused module files and module
1048  // indices.
1049  llvm::error_code EC;
1050  SmallString<128> ModuleCachePathNative;
1051  llvm::sys::path::native(HSOpts.ModuleCachePath, ModuleCachePathNative);
1052  for (llvm::sys::fs::directory_iterator
1053         Dir(ModuleCachePathNative.str(), EC), DirEnd;
1054       Dir != DirEnd && !EC; Dir.increment(EC)) {
1055    // If we don't have a directory, there's nothing to look into.
1056    if (!llvm::sys::fs::is_directory(Dir->path()))
1057      continue;
1058
1059    // Walk all of the files within this directory.
1060    bool RemovedAllFiles = true;
1061    for (llvm::sys::fs::directory_iterator File(Dir->path(), EC), FileEnd;
1062         File != FileEnd && !EC; File.increment(EC)) {
1063      // We only care about module and global module index files.
1064      if (llvm::sys::path::extension(File->path()) != ".pcm" &&
1065          llvm::sys::path::filename(File->path()) != "modules.idx") {
1066        RemovedAllFiles = false;
1067        continue;
1068      }
1069
1070      // Look at this file. If we can't stat it, there's nothing interesting
1071      // there.
1072      if (::stat(File->path().c_str(), &StatBuf)) {
1073        RemovedAllFiles = false;
1074        continue;
1075      }
1076
1077      // If the file has been used recently enough, leave it there.
1078      time_t FileAccessTime = StatBuf.st_atime;
1079      if (CurrentTime - FileAccessTime <=
1080              time_t(HSOpts.ModuleCachePruneAfter)) {
1081        RemovedAllFiles = false;
1082        continue;
1083      }
1084
1085      // Remove the file.
1086      bool Existed;
1087      if (llvm::sys::fs::remove(File->path(), Existed) || !Existed) {
1088        RemovedAllFiles = false;
1089      }
1090    }
1091
1092    // If we removed all of the files in the directory, remove the directory
1093    // itself.
1094    if (RemovedAllFiles) {
1095      bool Existed;
1096      llvm::sys::fs::remove(Dir->path(), Existed);
1097    }
1098  }
1099}
1100
1101ModuleLoadResult
1102CompilerInstance::loadModule(SourceLocation ImportLoc,
1103                             ModuleIdPath Path,
1104                             Module::NameVisibilityKind Visibility,
1105                             bool IsInclusionDirective) {
1106  // Determine what file we're searching from.
1107  StringRef ModuleName = Path[0].first->getName();
1108  SourceLocation ModuleNameLoc = Path[0].second;
1109
1110  // If we've already handled this import, just return the cached result.
1111  // This one-element cache is important to eliminate redundant diagnostics
1112  // when both the preprocessor and parser see the same import declaration.
1113  if (!ImportLoc.isInvalid() && LastModuleImportLoc == ImportLoc) {
1114    // Make the named module visible.
1115    if (LastModuleImportResult && ModuleName != getLangOpts().CurrentModule)
1116      ModuleManager->makeModuleVisible(LastModuleImportResult, Visibility,
1117                                       ImportLoc, /*Complain=*/false);
1118    return LastModuleImportResult;
1119  }
1120
1121  clang::Module *Module = 0;
1122
1123  // If we don't already have information on this module, load the module now.
1124  llvm::DenseMap<const IdentifierInfo *, clang::Module *>::iterator Known
1125    = KnownModules.find(Path[0].first);
1126  if (Known != KnownModules.end()) {
1127    // Retrieve the cached top-level module.
1128    Module = Known->second;
1129  } else if (ModuleName == getLangOpts().CurrentModule) {
1130    // This is the module we're building.
1131    Module = PP->getHeaderSearchInfo().getModuleMap().findModule(ModuleName);
1132    Known = KnownModules.insert(std::make_pair(Path[0].first, Module)).first;
1133  } else {
1134    // Search for a module with the given name.
1135    Module = PP->getHeaderSearchInfo().lookupModule(ModuleName);
1136    std::string ModuleFileName;
1137    if (Module) {
1138      ModuleFileName = PP->getHeaderSearchInfo().getModuleFileName(Module);
1139    } else
1140      ModuleFileName = PP->getHeaderSearchInfo().getModuleFileName(ModuleName);
1141
1142    // If we don't already have an ASTReader, create one now.
1143    if (!ModuleManager) {
1144      if (!hasASTContext())
1145        createASTContext();
1146
1147      // If we're not recursively building a module, check whether we
1148      // need to prune the module cache.
1149      if (getSourceManager().getModuleBuildStack().empty() &&
1150          getHeaderSearchOpts().ModuleCachePruneInterval > 0 &&
1151          getHeaderSearchOpts().ModuleCachePruneAfter > 0) {
1152        pruneModuleCache(getHeaderSearchOpts());
1153      }
1154
1155      std::string Sysroot = getHeaderSearchOpts().Sysroot;
1156      const PreprocessorOptions &PPOpts = getPreprocessorOpts();
1157      ModuleManager = new ASTReader(getPreprocessor(), *Context,
1158                                    Sysroot.empty() ? "" : Sysroot.c_str(),
1159                                    PPOpts.DisablePCHValidation,
1160                                    /*AllowASTWithCompilerErrors=*/false,
1161                                    getFrontendOpts().UseGlobalModuleIndex);
1162      if (hasASTConsumer()) {
1163        ModuleManager->setDeserializationListener(
1164          getASTConsumer().GetASTDeserializationListener());
1165        getASTContext().setASTMutationListener(
1166          getASTConsumer().GetASTMutationListener());
1167      }
1168      OwningPtr<ExternalASTSource> Source;
1169      Source.reset(ModuleManager);
1170      getASTContext().setExternalSource(Source);
1171      if (hasSema())
1172        ModuleManager->InitializeSema(getSema());
1173      if (hasASTConsumer())
1174        ModuleManager->StartTranslationUnit(&getASTConsumer());
1175    }
1176
1177    // Try to load the module file.
1178    unsigned ARRFlags = ASTReader::ARR_OutOfDate | ASTReader::ARR_Missing;
1179    switch (ModuleManager->ReadAST(ModuleFileName, serialization::MK_Module,
1180                                   ImportLoc, ARRFlags)) {
1181    case ASTReader::Success:
1182      break;
1183
1184    case ASTReader::OutOfDate:
1185    case ASTReader::Missing: {
1186      // The module file is missing or out-of-date. Build it.
1187
1188      // If we don't have a module, we don't know how to build the module file.
1189      // Complain and return.
1190      if (!Module) {
1191        getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_found)
1192          << ModuleName
1193          << SourceRange(ImportLoc, ModuleNameLoc);
1194        ModuleBuildFailed = true;
1195        return ModuleLoadResult();
1196      }
1197
1198      // Check whether there is a cycle in the module graph.
1199      ModuleBuildStack ModPath = getSourceManager().getModuleBuildStack();
1200      ModuleBuildStack::iterator Pos = ModPath.begin(), PosEnd = ModPath.end();
1201      for (; Pos != PosEnd; ++Pos) {
1202        if (Pos->first == ModuleName)
1203          break;
1204      }
1205
1206      if (Pos != PosEnd) {
1207        SmallString<256> CyclePath;
1208        for (; Pos != PosEnd; ++Pos) {
1209          CyclePath += Pos->first;
1210          CyclePath += " -> ";
1211        }
1212        CyclePath += ModuleName;
1213
1214        getDiagnostics().Report(ModuleNameLoc, diag::err_module_cycle)
1215          << ModuleName << CyclePath;
1216        return ModuleLoadResult();
1217      }
1218
1219      // Check whether we have already attempted to build this module (but
1220      // failed).
1221      if (getPreprocessorOpts().FailedModules &&
1222          getPreprocessorOpts().FailedModules->hasAlreadyFailed(ModuleName)) {
1223        getDiagnostics().Report(ModuleNameLoc, diag::err_module_not_built)
1224          << ModuleName
1225          << SourceRange(ImportLoc, ModuleNameLoc);
1226        ModuleBuildFailed = true;
1227        return ModuleLoadResult();
1228      }
1229
1230      // Try to compile the module.
1231      compileModule(*this, ModuleNameLoc, Module, ModuleFileName);
1232
1233      // Try to read the module file, now that we've compiled it.
1234      ASTReader::ASTReadResult ReadResult
1235        = ModuleManager->ReadAST(ModuleFileName,
1236                                 serialization::MK_Module, ImportLoc,
1237                                 ASTReader::ARR_Missing);
1238      if (ReadResult != ASTReader::Success) {
1239        if (ReadResult == ASTReader::Missing) {
1240          getDiagnostics().Report(ModuleNameLoc,
1241                                  Module? diag::err_module_not_built
1242                                        : diag::err_module_not_found)
1243            << ModuleName
1244            << SourceRange(ImportLoc, ModuleNameLoc);
1245        }
1246
1247        if (getPreprocessorOpts().FailedModules)
1248          getPreprocessorOpts().FailedModules->addFailed(ModuleName);
1249        KnownModules[Path[0].first] = 0;
1250        ModuleBuildFailed = true;
1251        return ModuleLoadResult();
1252      }
1253
1254      // Okay, we've rebuilt and now loaded the module.
1255      break;
1256    }
1257
1258    case ASTReader::VersionMismatch:
1259    case ASTReader::ConfigurationMismatch:
1260    case ASTReader::HadErrors:
1261      ModuleLoader::HadFatalFailure = true;
1262      // FIXME: The ASTReader will already have complained, but can we showhorn
1263      // that diagnostic information into a more useful form?
1264      KnownModules[Path[0].first] = 0;
1265      return ModuleLoadResult();
1266
1267    case ASTReader::Failure:
1268      ModuleLoader::HadFatalFailure = true;
1269      // Already complained, but note now that we failed.
1270      KnownModules[Path[0].first] = 0;
1271      ModuleBuildFailed = true;
1272      return ModuleLoadResult();
1273    }
1274
1275    if (!Module) {
1276      // If we loaded the module directly, without finding a module map first,
1277      // we'll have loaded the module's information from the module itself.
1278      Module = PP->getHeaderSearchInfo().getModuleMap()
1279                 .findModule((Path[0].first->getName()));
1280    }
1281
1282    // Cache the result of this top-level module lookup for later.
1283    Known = KnownModules.insert(std::make_pair(Path[0].first, Module)).first;
1284  }
1285
1286  // If we never found the module, fail.
1287  if (!Module)
1288    return ModuleLoadResult();
1289
1290  // Verify that the rest of the module path actually corresponds to
1291  // a submodule.
1292  if (Path.size() > 1) {
1293    for (unsigned I = 1, N = Path.size(); I != N; ++I) {
1294      StringRef Name = Path[I].first->getName();
1295      clang::Module *Sub = Module->findSubmodule(Name);
1296
1297      if (!Sub) {
1298        // Attempt to perform typo correction to find a module name that works.
1299        SmallVector<StringRef, 2> Best;
1300        unsigned BestEditDistance = (std::numeric_limits<unsigned>::max)();
1301
1302        for (clang::Module::submodule_iterator J = Module->submodule_begin(),
1303                                            JEnd = Module->submodule_end();
1304             J != JEnd; ++J) {
1305          unsigned ED = Name.edit_distance((*J)->Name,
1306                                           /*AllowReplacements=*/true,
1307                                           BestEditDistance);
1308          if (ED <= BestEditDistance) {
1309            if (ED < BestEditDistance) {
1310              Best.clear();
1311              BestEditDistance = ED;
1312            }
1313
1314            Best.push_back((*J)->Name);
1315          }
1316        }
1317
1318        // If there was a clear winner, user it.
1319        if (Best.size() == 1) {
1320          getDiagnostics().Report(Path[I].second,
1321                                  diag::err_no_submodule_suggest)
1322            << Path[I].first << Module->getFullModuleName() << Best[0]
1323            << SourceRange(Path[0].second, Path[I-1].second)
1324            << FixItHint::CreateReplacement(SourceRange(Path[I].second),
1325                                            Best[0]);
1326
1327          Sub = Module->findSubmodule(Best[0]);
1328        }
1329      }
1330
1331      if (!Sub) {
1332        // No submodule by this name. Complain, and don't look for further
1333        // submodules.
1334        getDiagnostics().Report(Path[I].second, diag::err_no_submodule)
1335          << Path[I].first << Module->getFullModuleName()
1336          << SourceRange(Path[0].second, Path[I-1].second);
1337        break;
1338      }
1339
1340      Module = Sub;
1341    }
1342  }
1343
1344  // Make the named module visible, if it's not already part of the module
1345  // we are parsing.
1346  if (ModuleName != getLangOpts().CurrentModule) {
1347    if (!Module->IsFromModuleFile) {
1348      // We have an umbrella header or directory that doesn't actually include
1349      // all of the headers within the directory it covers. Complain about
1350      // this missing submodule and recover by forgetting that we ever saw
1351      // this submodule.
1352      // FIXME: Should we detect this at module load time? It seems fairly
1353      // expensive (and rare).
1354      getDiagnostics().Report(ImportLoc, diag::warn_missing_submodule)
1355        << Module->getFullModuleName()
1356        << SourceRange(Path.front().second, Path.back().second);
1357
1358      return ModuleLoadResult(0, true);
1359    }
1360
1361    // Check whether this module is available.
1362    clang::Module::Requirement Requirement;
1363    if (!Module->isAvailable(getLangOpts(), getTarget(), Requirement)) {
1364      getDiagnostics().Report(ImportLoc, diag::err_module_unavailable)
1365        << Module->getFullModuleName()
1366        << Requirement.second << Requirement.first
1367        << SourceRange(Path.front().second, Path.back().second);
1368      LastModuleImportLoc = ImportLoc;
1369      LastModuleImportResult = ModuleLoadResult();
1370      return ModuleLoadResult();
1371    }
1372
1373    ModuleManager->makeModuleVisible(Module, Visibility, ImportLoc,
1374                                     /*Complain=*/true);
1375  }
1376
1377  // Check for any configuration macros that have changed.
1378  clang::Module *TopModule = Module->getTopLevelModule();
1379  for (unsigned I = 0, N = TopModule->ConfigMacros.size(); I != N; ++I) {
1380    checkConfigMacro(getPreprocessor(), TopModule->ConfigMacros[I],
1381                     Module, ImportLoc);
1382  }
1383
1384  // If this module import was due to an inclusion directive, create an
1385  // implicit import declaration to capture it in the AST.
1386  if (IsInclusionDirective && hasASTContext()) {
1387    TranslationUnitDecl *TU = getASTContext().getTranslationUnitDecl();
1388    ImportDecl *ImportD = ImportDecl::CreateImplicit(getASTContext(), TU,
1389                                                     ImportLoc, Module,
1390                                                     Path.back().second);
1391    TU->addDecl(ImportD);
1392    if (Consumer)
1393      Consumer->HandleImplicitImportDecl(ImportD);
1394  }
1395
1396  LastModuleImportLoc = ImportLoc;
1397  LastModuleImportResult = ModuleLoadResult(Module, false);
1398  return LastModuleImportResult;
1399}
1400
1401void CompilerInstance::makeModuleVisible(Module *Mod,
1402                                         Module::NameVisibilityKind Visibility,
1403                                         SourceLocation ImportLoc,
1404                                         bool Complain){
1405  ModuleManager->makeModuleVisible(Mod, Visibility, ImportLoc, Complain);
1406}
1407
1408