blob: 1f1a2f77bb3d023d314e076fe00f76e16ffba4e8 [file] [log] [blame]
Daniel Dunbar636404a2009-11-13 03:51:44 +00001//===--- 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"
Douglas Gregor0e93f012010-08-12 23:31:19 +000011#include "clang/Sema/Sema.h"
Daniel Dunbar56d9c292009-11-14 02:47:17 +000012#include "clang/AST/ASTConsumer.h"
Daniel Dunbardf3e30c2009-11-13 08:20:47 +000013#include "clang/AST/ASTContext.h"
Daniel Dunbar636404a2009-11-13 03:51:44 +000014#include "clang/Basic/Diagnostic.h"
Daniel Dunbar546a6762009-11-13 04:12:06 +000015#include "clang/Basic/FileManager.h"
16#include "clang/Basic/SourceManager.h"
Daniel Dunbar636404a2009-11-13 03:51:44 +000017#include "clang/Basic/TargetInfo.h"
Daniel Dunbar4f2bc552010-01-13 00:48:06 +000018#include "clang/Basic/Version.h"
Daniel Dunbaraaa148f2009-11-13 05:52:11 +000019#include "clang/Lex/HeaderSearch.h"
20#include "clang/Lex/Preprocessor.h"
21#include "clang/Lex/PTHManager.h"
Daniel Dunbar7d75afc2009-11-13 05:52:34 +000022#include "clang/Frontend/ChainedDiagnosticClient.h"
Daniel Dunbar4f2bc552010-01-13 00:48:06 +000023#include "clang/Frontend/FrontendAction.h"
Douglas Gregorfaeb1d42011-09-12 23:31:24 +000024#include "clang/Frontend/FrontendActions.h"
Daniel Dunbarf7093b52009-11-13 09:36:05 +000025#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbar2083c322011-04-07 18:31:10 +000026#include "clang/Frontend/LogDiagnosticPrinter.h"
Daniel Dunbar7d75afc2009-11-13 05:52:34 +000027#include "clang/Frontend/TextDiagnosticPrinter.h"
Daniel Dunbar50ec0da2009-11-14 03:24:39 +000028#include "clang/Frontend/VerifyDiagnosticsClient.h"
Daniel Dunbaraaa148f2009-11-13 05:52:11 +000029#include "clang/Frontend/Utils.h"
Sebastian Redlf5b13462010-08-18 23:57:17 +000030#include "clang/Serialization/ASTReader.h"
Daniel Dunbarf7093b52009-11-13 09:36:05 +000031#include "clang/Sema/CodeCompleteConsumer.h"
Michael J. Spencerf6efe582011-01-10 02:34:13 +000032#include "llvm/Support/FileSystem.h"
Daniel Dunbar409e8902009-11-14 07:53:04 +000033#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar7d75afc2009-11-13 05:52:34 +000034#include "llvm/Support/raw_ostream.h"
Douglas Gregor171b7802010-03-30 17:33:59 +000035#include "llvm/ADT/Statistic.h"
Kovarththanan Rajaratnam5505dff2009-11-29 09:57:35 +000036#include "llvm/Support/Timer.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000037#include "llvm/Support/Host.h"
38#include "llvm/Support/Path.h"
39#include "llvm/Support/Program.h"
40#include "llvm/Support/Signals.h"
Michael J. Spencerf25faaa2010-12-09 17:36:38 +000041#include "llvm/Support/system_error.h"
Dylan Noblesmith321cdb82011-06-23 12:20:57 +000042#include "llvm/Config/config.h"
Daniel Dunbar636404a2009-11-13 03:51:44 +000043using namespace clang;
44
Daniel Dunbare922d9b2010-02-16 01:54:47 +000045CompilerInstance::CompilerInstance()
Douglas Gregor925296b2011-07-19 16:10:42 +000046 : Invocation(new CompilerInvocation()), ModuleManager(0) {
Daniel Dunbar68242252010-01-30 21:47:07 +000047}
Daniel Dunbar636404a2009-11-13 03:51:44 +000048
49CompilerInstance::~CompilerInstance() {
Daniel Dunbare922d9b2010-02-16 01:54:47 +000050}
51
Daniel Dunbar68242252010-01-30 21:47:07 +000052void CompilerInstance::setInvocation(CompilerInvocation *Value) {
Ted Kremenek5e14d392011-03-21 18:40:17 +000053 Invocation = Value;
Daniel Dunbar68242252010-01-30 21:47:07 +000054}
55
Daniel Dunbare01dc862009-11-14 01:20:40 +000056void CompilerInstance::setDiagnostics(Diagnostic *Value) {
Douglas Gregor7f95d262010-04-05 23:52:57 +000057 Diagnostics = Value;
Daniel Dunbare01dc862009-11-14 01:20:40 +000058}
59
Daniel Dunbare01dc862009-11-14 01:20:40 +000060void CompilerInstance::setTarget(TargetInfo *Value) {
Ted Kremenek5e14d392011-03-21 18:40:17 +000061 Target = Value;
Daniel Dunbare01dc862009-11-14 01:20:40 +000062}
63
64void CompilerInstance::setFileManager(FileManager *Value) {
Ted Kremenek5e14d392011-03-21 18:40:17 +000065 FileMgr = Value;
Daniel Dunbare01dc862009-11-14 01:20:40 +000066}
67
Ted Kremenek5e14d392011-03-21 18:40:17 +000068void CompilerInstance::setSourceManager(SourceManager *Value) {
69 SourceMgr = Value;
Daniel Dunbare01dc862009-11-14 01:20:40 +000070}
71
Ted Kremenek5e14d392011-03-21 18:40:17 +000072void CompilerInstance::setPreprocessor(Preprocessor *Value) { PP = Value; }
Daniel Dunbare01dc862009-11-14 01:20:40 +000073
Ted Kremenek5e14d392011-03-21 18:40:17 +000074void CompilerInstance::setASTContext(ASTContext *Value) { Context = Value; }
Daniel Dunbare01dc862009-11-14 01:20:40 +000075
Douglas Gregor0e93f012010-08-12 23:31:19 +000076void CompilerInstance::setSema(Sema *S) {
77 TheSema.reset(S);
78}
79
Daniel Dunbar56d9c292009-11-14 02:47:17 +000080void CompilerInstance::setASTConsumer(ASTConsumer *Value) {
81 Consumer.reset(Value);
82}
83
Daniel Dunbare01dc862009-11-14 01:20:40 +000084void CompilerInstance::setCodeCompletionConsumer(CodeCompleteConsumer *Value) {
85 CompletionConsumer.reset(Value);
86}
87
Daniel Dunbar7d75afc2009-11-13 05:52:34 +000088// Diagnostics
Daniel Dunbar7d75afc2009-11-13 05:52:34 +000089static void SetUpBuildDumpLog(const DiagnosticOptions &DiagOpts,
Axel Naumann89c31492010-10-11 09:13:46 +000090 unsigned argc, const char* const *argv,
Kovarththanan Rajaratnam4a94ba52010-03-17 09:24:48 +000091 Diagnostic &Diags) {
Daniel Dunbar7d75afc2009-11-13 05:52:34 +000092 std::string ErrorInfo;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000093 llvm::OwningPtr<raw_ostream> OS(
Kovarththanan Rajaratnameeed0cc2010-03-17 09:47:30 +000094 new llvm::raw_fd_ostream(DiagOpts.DumpBuildInformation.c_str(), ErrorInfo));
Daniel Dunbar7d75afc2009-11-13 05:52:34 +000095 if (!ErrorInfo.empty()) {
Kovarththanan Rajaratnam4a94ba52010-03-17 09:24:48 +000096 Diags.Report(diag::err_fe_unable_to_open_logfile)
97 << DiagOpts.DumpBuildInformation << ErrorInfo;
Daniel Dunbar7d75afc2009-11-13 05:52:34 +000098 return;
99 }
100
Daniel Dunbar520d1e62009-12-11 23:04:35 +0000101 (*OS) << "clang -cc1 command line arguments: ";
Daniel Dunbar7d75afc2009-11-13 05:52:34 +0000102 for (unsigned i = 0; i != argc; ++i)
103 (*OS) << argv[i] << ' ';
104 (*OS) << '\n';
105
106 // Chain in a diagnostic client which will log the diagnostics.
107 DiagnosticClient *Logger =
Kovarththanan Rajaratnameeed0cc2010-03-17 09:47:30 +0000108 new TextDiagnosticPrinter(*OS.take(), DiagOpts, /*OwnsOutputStream=*/true);
Douglas Gregor2dd19f12010-08-18 22:29:43 +0000109 Diags.setClient(new ChainedDiagnosticClient(Diags.takeClient(), Logger));
Daniel Dunbar7d75afc2009-11-13 05:52:34 +0000110}
111
Daniel Dunbar2083c322011-04-07 18:31:10 +0000112static void SetUpDiagnosticLog(const DiagnosticOptions &DiagOpts,
Daniel Dunbar7b833062011-04-07 18:59:02 +0000113 const CodeGenOptions *CodeGenOpts,
Daniel Dunbar2083c322011-04-07 18:31:10 +0000114 Diagnostic &Diags) {
115 std::string ErrorInfo;
116 bool OwnsStream = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000117 raw_ostream *OS = &llvm::errs();
Daniel Dunbar2083c322011-04-07 18:31:10 +0000118 if (DiagOpts.DiagnosticLogFile != "-") {
119 // Create the output stream.
120 llvm::raw_fd_ostream *FileOS(
121 new llvm::raw_fd_ostream(DiagOpts.DiagnosticLogFile.c_str(),
Daniel Dunbar44d9ef72011-04-07 20:19:21 +0000122 ErrorInfo, llvm::raw_fd_ostream::F_Append));
Daniel Dunbar2083c322011-04-07 18:31:10 +0000123 if (!ErrorInfo.empty()) {
124 Diags.Report(diag::warn_fe_cc_log_diagnostics_failure)
125 << DiagOpts.DumpBuildInformation << ErrorInfo;
126 } else {
127 FileOS->SetUnbuffered();
128 FileOS->SetUseAtomicWrites(true);
129 OS = FileOS;
130 OwnsStream = true;
131 }
132 }
133
134 // Chain in the diagnostic client which will log the diagnostics.
Daniel Dunbar7b833062011-04-07 18:59:02 +0000135 LogDiagnosticPrinter *Logger = new LogDiagnosticPrinter(*OS, DiagOpts,
136 OwnsStream);
137 if (CodeGenOpts)
138 Logger->setDwarfDebugFlags(CodeGenOpts->DwarfDebugFlags);
Daniel Dunbar2083c322011-04-07 18:31:10 +0000139 Diags.setClient(new ChainedDiagnosticClient(Diags.takeClient(), Logger));
140}
141
Douglas Gregor44c6ee72010-11-11 00:39:14 +0000142void CompilerInstance::createDiagnostics(int Argc, const char* const *Argv,
143 DiagnosticClient *Client) {
Daniel Dunbar7b833062011-04-07 18:59:02 +0000144 Diagnostics = createDiagnostics(getDiagnosticOpts(), Argc, Argv, Client,
145 &getCodeGenOpts());
Daniel Dunbar7d75afc2009-11-13 05:52:34 +0000146}
147
Douglas Gregor7f95d262010-04-05 23:52:57 +0000148llvm::IntrusiveRefCntPtr<Diagnostic>
149CompilerInstance::createDiagnostics(const DiagnosticOptions &Opts,
Douglas Gregor44c6ee72010-11-11 00:39:14 +0000150 int Argc, const char* const *Argv,
Daniel Dunbar7b833062011-04-07 18:59:02 +0000151 DiagnosticClient *Client,
152 const CodeGenOptions *CodeGenOpts) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000153 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
154 llvm::IntrusiveRefCntPtr<Diagnostic> Diags(new Diagnostic(DiagID));
Daniel Dunbar1b39a2e2009-11-14 07:53:24 +0000155
Daniel Dunbar7d75afc2009-11-13 05:52:34 +0000156 // Create the diagnostic client for reporting errors or for
157 // implementing -verify.
Douglas Gregor44c6ee72010-11-11 00:39:14 +0000158 if (Client)
159 Diags->setClient(Client);
160 else
161 Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts));
Daniel Dunbar50ec0da2009-11-14 03:24:39 +0000162
163 // Chain in -verify checker, if requested.
164 if (Opts.VerifyDiagnostics)
Douglas Gregor2dd19f12010-08-18 22:29:43 +0000165 Diags->setClient(new VerifyDiagnosticsClient(*Diags, Diags->takeClient()));
Daniel Dunbar7d75afc2009-11-13 05:52:34 +0000166
Daniel Dunbar2083c322011-04-07 18:31:10 +0000167 // Chain in -diagnostic-log-file dumper, if requested.
168 if (!Opts.DiagnosticLogFile.empty())
Daniel Dunbar7b833062011-04-07 18:59:02 +0000169 SetUpDiagnosticLog(Opts, CodeGenOpts, *Diags);
Daniel Dunbar2083c322011-04-07 18:31:10 +0000170
Daniel Dunbar7d75afc2009-11-13 05:52:34 +0000171 if (!Opts.DumpBuildInformation.empty())
Kovarththanan Rajaratnam4a94ba52010-03-17 09:24:48 +0000172 SetUpBuildDumpLog(Opts, Argc, Argv, *Diags);
Daniel Dunbar7d75afc2009-11-13 05:52:34 +0000173
174 // Configure our handling of diagnostics.
Kovarththanan Rajaratnam9ff84d92010-03-17 09:36:02 +0000175 ProcessWarningOptions(*Diags, Opts);
Daniel Dunbar7d75afc2009-11-13 05:52:34 +0000176
Douglas Gregor7f95d262010-04-05 23:52:57 +0000177 return Diags;
Daniel Dunbar7d75afc2009-11-13 05:52:34 +0000178}
179
180// File Manager
181
Daniel Dunbar546a6762009-11-13 04:12:06 +0000182void CompilerInstance::createFileManager() {
Ted Kremenek5e14d392011-03-21 18:40:17 +0000183 FileMgr = new FileManager(getFileSystemOpts());
Daniel Dunbar546a6762009-11-13 04:12:06 +0000184}
185
Daniel Dunbar7d75afc2009-11-13 05:52:34 +0000186// Source Manager
187
Chris Lattner5159f612010-11-23 08:35:12 +0000188void CompilerInstance::createSourceManager(FileManager &FileMgr) {
Ted Kremenek5e14d392011-03-21 18:40:17 +0000189 SourceMgr = new SourceManager(getDiagnostics(), FileMgr);
Daniel Dunbar546a6762009-11-13 04:12:06 +0000190}
Daniel Dunbaraaa148f2009-11-13 05:52:11 +0000191
Daniel Dunbar7d75afc2009-11-13 05:52:34 +0000192// Preprocessor
193
Daniel Dunbaraaa148f2009-11-13 05:52:11 +0000194void CompilerInstance::createPreprocessor() {
Douglas Gregor08142532011-08-26 23:56:07 +0000195 const PreprocessorOptions &PPOpts = getPreprocessorOpts();
196
Daniel Dunbaraaa148f2009-11-13 05:52:11 +0000197 // Create a PTH manager if we are using some form of a token cache.
198 PTHManager *PTHMgr = 0;
Daniel Dunbard6ea9022009-11-17 05:52:41 +0000199 if (!PPOpts.TokenCache.empty())
Douglas Gregor08142532011-08-26 23:56:07 +0000200 PTHMgr = PTHManager::Create(PPOpts.TokenCache, getDiagnostics());
201
Daniel Dunbaraaa148f2009-11-13 05:52:11 +0000202 // Create the Preprocessor.
Douglas Gregor08142532011-08-26 23:56:07 +0000203 HeaderSearch *HeaderInfo = new HeaderSearch(getFileManager());
Douglas Gregor83297df2011-09-01 23:39:15 +0000204 PP = new Preprocessor(getDiagnostics(), getLangOpts(), &getTarget(),
Douglas Gregor08142532011-08-26 23:56:07 +0000205 getSourceManager(), *HeaderInfo, *this, PTHMgr,
206 /*OwnsHeaderSearch=*/true);
207
Daniel Dunbaraaa148f2009-11-13 05:52:11 +0000208 // Note that this is different then passing PTHMgr to Preprocessor's ctor.
209 // That argument is used as the IdentifierInfoLookup argument to
210 // IdentifierTable's ctor.
211 if (PTHMgr) {
Douglas Gregor08142532011-08-26 23:56:07 +0000212 PTHMgr->setPreprocessor(&*PP);
Daniel Dunbaraaa148f2009-11-13 05:52:11 +0000213 PP->setPTHManager(PTHMgr);
214 }
Douglas Gregor08142532011-08-26 23:56:07 +0000215
Douglas Gregor7f6d60d2010-03-19 16:15:56 +0000216 if (PPOpts.DetailedRecord)
Douglas Gregor998caea2011-05-06 16:33:08 +0000217 PP->createPreprocessingRecord(
Douglas Gregor08142532011-08-26 23:56:07 +0000218 PPOpts.DetailedRecordIncludesNestedMacroExpansions);
Douglas Gregor7f6d60d2010-03-19 16:15:56 +0000219
Douglas Gregor08142532011-08-26 23:56:07 +0000220 InitializePreprocessor(*PP, PPOpts, getHeaderSearchOpts(), getFrontendOpts());
221
Daniel Dunbaraaa148f2009-11-13 05:52:11 +0000222 // Handle generating dependencies, if requested.
Douglas Gregor08142532011-08-26 23:56:07 +0000223 const DependencyOutputOptions &DepOpts = getDependencyOutputOpts();
Daniel Dunbaraaa148f2009-11-13 05:52:11 +0000224 if (!DepOpts.OutputFile.empty())
225 AttachDependencyFileGen(*PP, DepOpts);
Douglas Gregor08142532011-08-26 23:56:07 +0000226
Daniel Dunbar27734fd2011-02-02 15:41:17 +0000227 // Handle generating header include information, if requested.
228 if (DepOpts.ShowHeaderIncludes)
229 AttachHeaderIncludeGen(*PP);
Daniel Dunbar1af1d27512011-02-02 21:11:31 +0000230 if (!DepOpts.HeaderIncludeOutputFile.empty()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000231 StringRef OutputPath = DepOpts.HeaderIncludeOutputFile;
Daniel Dunbar1af1d27512011-02-02 21:11:31 +0000232 if (OutputPath == "-")
233 OutputPath = "";
Daniel Dunbarfe908a82011-03-21 19:37:38 +0000234 AttachHeaderIncludeGen(*PP, /*ShowAllHeaders=*/true, OutputPath,
235 /*ShowDepth=*/false);
Daniel Dunbar1af1d27512011-02-02 21:11:31 +0000236 }
Daniel Dunbaraaa148f2009-11-13 05:52:11 +0000237}
Daniel Dunbardf3e30c2009-11-13 08:20:47 +0000238
239// ASTContext
240
241void CompilerInstance::createASTContext() {
242 Preprocessor &PP = getPreprocessor();
Ted Kremenek5e14d392011-03-21 18:40:17 +0000243 Context = new ASTContext(getLangOpts(), PP.getSourceManager(),
Douglas Gregore8bbc122011-09-02 00:18:52 +0000244 &getTarget(), PP.getIdentifierTable(),
Ted Kremenek5e14d392011-03-21 18:40:17 +0000245 PP.getSelectorTable(), PP.getBuiltinInfo(),
246 /*size_reserve=*/ 0);
Daniel Dunbardf3e30c2009-11-13 08:20:47 +0000247}
Daniel Dunbar599313e2009-11-13 08:21:10 +0000248
249// ExternalASTSource
250
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000251void CompilerInstance::createPCHExternalASTSource(StringRef Path,
Sebastian Redl07a89a82010-07-30 00:29:29 +0000252 bool DisablePCHValidation,
Douglas Gregor606c4ac2011-02-05 19:42:43 +0000253 bool DisableStatCache,
Sebastian Redl07a89a82010-07-30 00:29:29 +0000254 void *DeserializationListener){
Daniel Dunbar599313e2009-11-13 08:21:10 +0000255 llvm::OwningPtr<ExternalASTSource> Source;
Sebastian Redl009e7f22010-10-05 16:15:19 +0000256 bool Preamble = getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
Daniel Dunbar599313e2009-11-13 08:21:10 +0000257 Source.reset(createPCHExternalASTSource(Path, getHeaderSearchOpts().Sysroot,
Douglas Gregor606c4ac2011-02-05 19:42:43 +0000258 DisablePCHValidation,
259 DisableStatCache,
Sebastian Redl07a89a82010-07-30 00:29:29 +0000260 getPreprocessor(), getASTContext(),
Sebastian Redl009e7f22010-10-05 16:15:19 +0000261 DeserializationListener,
262 Preamble));
Douglas Gregor925296b2011-07-19 16:10:42 +0000263 ModuleManager = static_cast<ASTReader*>(Source.get());
Daniel Dunbar599313e2009-11-13 08:21:10 +0000264 getASTContext().setExternalSource(Source);
265}
266
267ExternalASTSource *
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000268CompilerInstance::createPCHExternalASTSource(StringRef Path,
Daniel Dunbar599313e2009-11-13 08:21:10 +0000269 const std::string &Sysroot,
Douglas Gregorce3a8292010-07-27 00:27:13 +0000270 bool DisablePCHValidation,
Douglas Gregor606c4ac2011-02-05 19:42:43 +0000271 bool DisableStatCache,
Daniel Dunbar599313e2009-11-13 08:21:10 +0000272 Preprocessor &PP,
Sebastian Redl07a89a82010-07-30 00:29:29 +0000273 ASTContext &Context,
Sebastian Redl009e7f22010-10-05 16:15:19 +0000274 void *DeserializationListener,
275 bool Preamble) {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000276 llvm::OwningPtr<ASTReader> Reader;
Douglas Gregor8835e032011-09-02 00:26:20 +0000277 Reader.reset(new ASTReader(PP, Context,
Douglas Gregorc567ba22011-07-22 16:35:34 +0000278 Sysroot.empty() ? "" : Sysroot.c_str(),
Douglas Gregor606c4ac2011-02-05 19:42:43 +0000279 DisablePCHValidation, DisableStatCache));
Daniel Dunbar599313e2009-11-13 08:21:10 +0000280
Sebastian Redl07a89a82010-07-30 00:29:29 +0000281 Reader->setDeserializationListener(
Sebastian Redl3e31c722010-08-18 23:56:56 +0000282 static_cast<ASTDeserializationListener *>(DeserializationListener));
Douglas Gregora6895d82011-07-22 16:00:58 +0000283 switch (Reader->ReadAST(Path,
284 Preamble ? serialization::MK_Preamble
285 : serialization::MK_PCH)) {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000286 case ASTReader::Success:
Daniel Dunbar599313e2009-11-13 08:21:10 +0000287 // Set the predefines buffer as suggested by the PCH reader. Typically, the
288 // predefines buffer will be empty.
289 PP.setPredefines(Reader->getSuggestedPredefines());
290 return Reader.take();
291
Sebastian Redl2c499f62010-08-18 23:56:43 +0000292 case ASTReader::Failure:
Daniel Dunbar599313e2009-11-13 08:21:10 +0000293 // Unrecoverable failure: don't even try to process the input file.
294 break;
295
Sebastian Redl2c499f62010-08-18 23:56:43 +0000296 case ASTReader::IgnorePCH:
Daniel Dunbar599313e2009-11-13 08:21:10 +0000297 // No suitable PCH file could be found. Return an error.
298 break;
299 }
300
301 return 0;
302}
Daniel Dunbarf7093b52009-11-13 09:36:05 +0000303
304// Code Completion
305
Douglas Gregor8e984da2010-08-04 16:47:14 +0000306static bool EnableCodeCompletion(Preprocessor &PP,
307 const std::string &Filename,
308 unsigned Line,
309 unsigned Column) {
310 // Tell the source manager to chop off the given file at a specific
311 // line and column.
Chris Lattner5159f612010-11-23 08:35:12 +0000312 const FileEntry *Entry = PP.getFileManager().getFile(Filename);
Douglas Gregor8e984da2010-08-04 16:47:14 +0000313 if (!Entry) {
314 PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file)
315 << Filename;
316 return true;
317 }
318
319 // Truncate the named file at the given line/column.
320 PP.SetCodeCompletionPoint(Entry, Line, Column);
321 return false;
322}
323
Daniel Dunbarf7093b52009-11-13 09:36:05 +0000324void CompilerInstance::createCodeCompletionConsumer() {
325 const ParsedSourceLocation &Loc = getFrontendOpts().CodeCompletionAt;
Douglas Gregor8e984da2010-08-04 16:47:14 +0000326 if (!CompletionConsumer) {
327 CompletionConsumer.reset(
328 createCodeCompletionConsumer(getPreprocessor(),
329 Loc.FileName, Loc.Line, Loc.Column,
Douglas Gregor8e984da2010-08-04 16:47:14 +0000330 getFrontendOpts().ShowMacrosInCodeCompletion,
Douglas Gregorf64acca2010-05-25 21:41:55 +0000331 getFrontendOpts().ShowCodePatternsInCodeCompletion,
Douglas Gregor39982192010-08-15 06:18:01 +0000332 getFrontendOpts().ShowGlobalSymbolsInCodeCompletion,
Douglas Gregor8e984da2010-08-04 16:47:14 +0000333 llvm::outs()));
334 if (!CompletionConsumer)
335 return;
336 } else if (EnableCodeCompletion(getPreprocessor(), Loc.FileName,
337 Loc.Line, Loc.Column)) {
338 CompletionConsumer.reset();
Douglas Gregor00a0cf72010-03-16 06:04:47 +0000339 return;
Douglas Gregor8e984da2010-08-04 16:47:14 +0000340 }
Douglas Gregorf09935f2009-12-01 05:55:20 +0000341
342 if (CompletionConsumer->isOutputBinary() &&
343 llvm::sys::Program::ChangeStdoutToBinary()) {
344 getPreprocessor().getDiagnostics().Report(diag::err_fe_stdout_binary);
345 CompletionConsumer.reset();
346 }
Daniel Dunbarf7093b52009-11-13 09:36:05 +0000347}
348
Kovarththanan Rajaratnam5505dff2009-11-29 09:57:35 +0000349void CompilerInstance::createFrontendTimer() {
350 FrontendTimer.reset(new llvm::Timer("Clang front-end timer"));
351}
352
Daniel Dunbarf7093b52009-11-13 09:36:05 +0000353CodeCompleteConsumer *
354CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP,
355 const std::string &Filename,
356 unsigned Line,
357 unsigned Column,
Daniel Dunbarf7093b52009-11-13 09:36:05 +0000358 bool ShowMacros,
Douglas Gregorf64acca2010-05-25 21:41:55 +0000359 bool ShowCodePatterns,
Douglas Gregor39982192010-08-15 06:18:01 +0000360 bool ShowGlobals,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000361 raw_ostream &OS) {
Douglas Gregor8e984da2010-08-04 16:47:14 +0000362 if (EnableCodeCompletion(PP, Filename, Line, Column))
Daniel Dunbarf7093b52009-11-13 09:36:05 +0000363 return 0;
Daniel Dunbarf7093b52009-11-13 09:36:05 +0000364
365 // Set up the creation routine for code-completion.
Douglas Gregorb9ab0ed2010-10-11 22:12:15 +0000366 return new PrintingCodeCompleteConsumer(ShowMacros, ShowCodePatterns,
Douglas Gregor39982192010-08-15 06:18:01 +0000367 ShowGlobals, OS);
Daniel Dunbarf7093b52009-11-13 09:36:05 +0000368}
Daniel Dunbar566eeb22009-11-13 10:37:48 +0000369
Douglas Gregor69f74f82011-08-25 22:30:56 +0000370void CompilerInstance::createSema(TranslationUnitKind TUKind,
Douglas Gregor0e93f012010-08-12 23:31:19 +0000371 CodeCompleteConsumer *CompletionConsumer) {
372 TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(),
Douglas Gregor69f74f82011-08-25 22:30:56 +0000373 TUKind, CompletionConsumer));
Douglas Gregor0e93f012010-08-12 23:31:19 +0000374}
375
Daniel Dunbar566eeb22009-11-13 10:37:48 +0000376// Output Files
377
Argyrios Kyrtzidisd0599972010-09-17 17:38:48 +0000378void CompilerInstance::addOutputFile(const OutputFile &OutFile) {
379 assert(OutFile.OS && "Attempt to add empty stream to output list!");
380 OutputFiles.push_back(OutFile);
Daniel Dunbar566eeb22009-11-13 10:37:48 +0000381}
382
Kovarththanan Rajaratnam1c558cd2010-03-06 12:07:48 +0000383void CompilerInstance::clearOutputFiles(bool EraseFiles) {
Argyrios Kyrtzidisd0599972010-09-17 17:38:48 +0000384 for (std::list<OutputFile>::iterator
Daniel Dunbar566eeb22009-11-13 10:37:48 +0000385 it = OutputFiles.begin(), ie = OutputFiles.end(); it != ie; ++it) {
Argyrios Kyrtzidisd0599972010-09-17 17:38:48 +0000386 delete it->OS;
387 if (!it->TempFilename.empty()) {
Anders Carlssonb5c356a2011-03-06 22:25:35 +0000388 if (EraseFiles) {
389 bool existed;
390 llvm::sys::fs::remove(it->TempFilename, existed);
391 } else {
392 llvm::SmallString<128> NewOutFile(it->Filename);
393
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000394 // If '-working-directory' was passed, the output filename should be
395 // relative to that.
Anders Carlsson9ba8fb12011-03-14 01:13:54 +0000396 FileMgr->FixupRelativePath(NewOutFile);
Anders Carlssonb5c356a2011-03-06 22:25:35 +0000397 if (llvm::error_code ec = llvm::sys::fs::rename(it->TempFilename,
398 NewOutFile.str())) {
Argyrios Kyrtzidisd0599972010-09-17 17:38:48 +0000399 getDiagnostics().Report(diag::err_fe_unable_to_rename_temp)
Anders Carlssonb5c356a2011-03-06 22:25:35 +0000400 << it->TempFilename << it->Filename << ec.message();
401
402 bool existed;
403 llvm::sys::fs::remove(it->TempFilename, existed);
Argyrios Kyrtzidisd0599972010-09-17 17:38:48 +0000404 }
405 }
406 } else if (!it->Filename.empty() && EraseFiles)
407 llvm::sys::Path(it->Filename).eraseFromDisk();
408
Daniel Dunbar566eeb22009-11-13 10:37:48 +0000409 }
410 OutputFiles.clear();
411}
412
Daniel Dunbar420b0f12009-11-13 18:32:08 +0000413llvm::raw_fd_ostream *
414CompilerInstance::createDefaultOutputFile(bool Binary,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000415 StringRef InFile,
416 StringRef Extension) {
Daniel Dunbar420b0f12009-11-13 18:32:08 +0000417 return createOutputFile(getFrontendOpts().OutputFile, Binary,
Daniel Dunbare326f9b2011-01-31 22:00:42 +0000418 /*RemoveFileOnSignal=*/true, InFile, Extension);
Daniel Dunbar420b0f12009-11-13 18:32:08 +0000419}
420
421llvm::raw_fd_ostream *
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000422CompilerInstance::createOutputFile(StringRef OutputPath,
Daniel Dunbare326f9b2011-01-31 22:00:42 +0000423 bool Binary, bool RemoveFileOnSignal,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000424 StringRef InFile,
Argyrios Kyrtzidis08a2bfd2011-07-28 00:45:10 +0000425 StringRef Extension,
426 bool UseTemporary) {
Argyrios Kyrtzidisd0599972010-09-17 17:38:48 +0000427 std::string Error, OutputPathName, TempPathName;
Daniel Dunbar420b0f12009-11-13 18:32:08 +0000428 llvm::raw_fd_ostream *OS = createOutputFile(OutputPath, Error, Binary,
Daniel Dunbare326f9b2011-01-31 22:00:42 +0000429 RemoveFileOnSignal,
Daniel Dunbar420b0f12009-11-13 18:32:08 +0000430 InFile, Extension,
Argyrios Kyrtzidis08a2bfd2011-07-28 00:45:10 +0000431 UseTemporary,
Argyrios Kyrtzidisd0599972010-09-17 17:38:48 +0000432 &OutputPathName,
433 &TempPathName);
Daniel Dunbar420b0f12009-11-13 18:32:08 +0000434 if (!OS) {
Daniel Dunbar75546992009-12-03 09:13:30 +0000435 getDiagnostics().Report(diag::err_fe_unable_to_open_output)
436 << OutputPath << Error;
437 return 0;
Daniel Dunbar420b0f12009-11-13 18:32:08 +0000438 }
439
440 // Add the output file -- but don't try to remove "-", since this means we are
441 // using stdin.
Argyrios Kyrtzidisd0599972010-09-17 17:38:48 +0000442 addOutputFile(OutputFile((OutputPathName != "-") ? OutputPathName : "",
443 TempPathName, OS));
Daniel Dunbar420b0f12009-11-13 18:32:08 +0000444
445 return OS;
446}
447
448llvm::raw_fd_ostream *
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000449CompilerInstance::createOutputFile(StringRef OutputPath,
Daniel Dunbar420b0f12009-11-13 18:32:08 +0000450 std::string &Error,
451 bool Binary,
Daniel Dunbare326f9b2011-01-31 22:00:42 +0000452 bool RemoveFileOnSignal,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000453 StringRef InFile,
454 StringRef Extension,
Argyrios Kyrtzidis08a2bfd2011-07-28 00:45:10 +0000455 bool UseTemporary,
Argyrios Kyrtzidisd0599972010-09-17 17:38:48 +0000456 std::string *ResultPathName,
457 std::string *TempPathName) {
458 std::string OutFile, TempFile;
Daniel Dunbar420b0f12009-11-13 18:32:08 +0000459 if (!OutputPath.empty()) {
460 OutFile = OutputPath;
461 } else if (InFile == "-") {
462 OutFile = "-";
463 } else if (!Extension.empty()) {
464 llvm::sys::Path Path(InFile);
465 Path.eraseSuffix();
466 Path.appendSuffix(Extension);
467 OutFile = Path.str();
468 } else {
469 OutFile = "-";
470 }
Argyrios Kyrtzidis08a2bfd2011-07-28 00:45:10 +0000471
472 llvm::OwningPtr<llvm::raw_fd_ostream> OS;
473 std::string OSFile;
474
475 if (UseTemporary && OutFile != "-") {
Argyrios Kyrtzidisd0599972010-09-17 17:38:48 +0000476 llvm::sys::Path OutPath(OutFile);
477 // Only create the temporary if we can actually write to OutPath, otherwise
478 // we want to fail early.
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000479 bool Exists;
480 if ((llvm::sys::fs::exists(OutPath.str(), Exists) || !Exists) ||
Argyrios Kyrtzidisd0599972010-09-17 17:38:48 +0000481 (OutPath.isRegularFile() && OutPath.canWrite())) {
482 // Create a temporary file.
Argyrios Kyrtzidis08a2bfd2011-07-28 00:45:10 +0000483 llvm::SmallString<128> TempPath;
484 TempPath = OutFile;
485 TempPath += "-%%%%%%%%";
486 int fd;
487 if (llvm::sys::fs::unique_file(TempPath.str(), fd, TempPath,
488 /*makeAbsolute=*/false) == llvm::errc::success) {
489 OS.reset(new llvm::raw_fd_ostream(fd, /*shouldClose=*/true));
490 OSFile = TempFile = TempPath.str();
491 }
Argyrios Kyrtzidisd0599972010-09-17 17:38:48 +0000492 }
493 }
494
Argyrios Kyrtzidis08a2bfd2011-07-28 00:45:10 +0000495 if (!OS) {
496 OSFile = OutFile;
497 OS.reset(
498 new llvm::raw_fd_ostream(OSFile.c_str(), Error,
499 (Binary ? llvm::raw_fd_ostream::F_Binary : 0)));
500 if (!Error.empty())
501 return 0;
502 }
Daniel Dunbar420b0f12009-11-13 18:32:08 +0000503
Argyrios Kyrtzidisd0599972010-09-17 17:38:48 +0000504 // Make sure the out stream file gets removed if we crash.
Daniel Dunbare326f9b2011-01-31 22:00:42 +0000505 if (RemoveFileOnSignal)
506 llvm::sys::RemoveFileOnSignal(llvm::sys::Path(OSFile));
Argyrios Kyrtzidisd0599972010-09-17 17:38:48 +0000507
Daniel Dunbar420b0f12009-11-13 18:32:08 +0000508 if (ResultPathName)
509 *ResultPathName = OutFile;
Argyrios Kyrtzidisd0599972010-09-17 17:38:48 +0000510 if (TempPathName)
511 *TempPathName = TempFile;
Daniel Dunbar420b0f12009-11-13 18:32:08 +0000512
Daniel Dunbar2eaef182009-11-20 22:32:38 +0000513 return OS.take();
Daniel Dunbar420b0f12009-11-13 18:32:08 +0000514}
Daniel Dunbar409e8902009-11-14 07:53:04 +0000515
516// Initialization Utilities
517
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000518bool CompilerInstance::InitializeSourceManager(StringRef InputFile) {
Daniel Dunbar409e8902009-11-14 07:53:04 +0000519 return InitializeSourceManager(InputFile, getDiagnostics(), getFileManager(),
520 getSourceManager(), getFrontendOpts());
521}
522
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000523bool CompilerInstance::InitializeSourceManager(StringRef InputFile,
Daniel Dunbar409e8902009-11-14 07:53:04 +0000524 Diagnostic &Diags,
525 FileManager &FileMgr,
526 SourceManager &SourceMgr,
527 const FrontendOptions &Opts) {
Douglas Gregor936a5b42010-11-30 05:23:00 +0000528 // Figure out where to get and map in the main file, unless it's already
529 // been created (e.g., by a precompiled preamble).
530 if (!SourceMgr.getMainFileID().isInvalid()) {
531 // Do nothing: the main file has already been set.
532 } else if (InputFile != "-") {
Chris Lattner5159f612010-11-23 08:35:12 +0000533 const FileEntry *File = FileMgr.getFile(InputFile);
Dan Gohman52765212010-10-26 21:13:51 +0000534 if (!File) {
Daniel Dunbar409e8902009-11-14 07:53:04 +0000535 Diags.Report(diag::err_fe_error_reading) << InputFile;
536 return false;
537 }
Dan Gohman52765212010-10-26 21:13:51 +0000538 SourceMgr.createMainFileID(File);
Daniel Dunbar409e8902009-11-14 07:53:04 +0000539 } else {
Michael J. Spencerd9da7a12010-12-16 03:28:14 +0000540 llvm::OwningPtr<llvm::MemoryBuffer> SB;
541 if (llvm::MemoryBuffer::getSTDIN(SB)) {
Michael J. Spencerf25faaa2010-12-09 17:36:38 +0000542 // FIXME: Give ec.message() in this diag.
Daniel Dunbar409e8902009-11-14 07:53:04 +0000543 Diags.Report(diag::err_fe_error_reading_stdin);
544 return false;
545 }
Dan Gohman2f76cd72010-10-26 23:21:25 +0000546 const FileEntry *File = FileMgr.getVirtualFile(SB->getBufferIdentifier(),
Chris Lattner5159f612010-11-23 08:35:12 +0000547 SB->getBufferSize(), 0);
Dan Gohman2f76cd72010-10-26 23:21:25 +0000548 SourceMgr.createMainFileID(File);
Michael J. Spencerd9da7a12010-12-16 03:28:14 +0000549 SourceMgr.overrideFileContents(File, SB.take());
Daniel Dunbar409e8902009-11-14 07:53:04 +0000550 }
551
Dan Gohman52765212010-10-26 21:13:51 +0000552 assert(!SourceMgr.getMainFileID().isInvalid() &&
553 "Couldn't establish MainFileID!");
Daniel Dunbar409e8902009-11-14 07:53:04 +0000554 return true;
555}
Daniel Dunbar4f2bc552010-01-13 00:48:06 +0000556
557// High-Level Operations
558
559bool CompilerInstance::ExecuteAction(FrontendAction &Act) {
560 assert(hasDiagnostics() && "Diagnostics engine is not initialized!");
561 assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!");
562 assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!");
563
564 // FIXME: Take this as an argument, once all the APIs we used have moved to
565 // taking it as an input instead of hard-coding llvm::errs.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000566 raw_ostream &OS = llvm::errs();
Daniel Dunbar4f2bc552010-01-13 00:48:06 +0000567
568 // Create the target instance.
569 setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), getTargetOpts()));
570 if (!hasTarget())
571 return false;
572
573 // Inform the target of the language options.
574 //
575 // FIXME: We shouldn't need to do this, the target should be immutable once
576 // created. This complexity should be lifted elsewhere.
577 getTarget().setForcedLangOptions(getLangOpts());
578
579 // Validate/process some options.
580 if (getHeaderSearchOpts().Verbose)
581 OS << "clang -cc1 version " CLANG_VERSION_STRING
582 << " based upon " << PACKAGE_STRING
583 << " hosted on " << llvm::sys::getHostTriple() << "\n";
584
585 if (getFrontendOpts().ShowTimers)
586 createFrontendTimer();
587
Douglas Gregor171b7802010-03-30 17:33:59 +0000588 if (getFrontendOpts().ShowStats)
589 llvm::EnableStatistics();
590
Daniel Dunbar4f2bc552010-01-13 00:48:06 +0000591 for (unsigned i = 0, e = getFrontendOpts().Inputs.size(); i != e; ++i) {
592 const std::string &InFile = getFrontendOpts().Inputs[i].second;
593
Daniel Dunbaraed46fc2010-06-07 23:23:50 +0000594 // Reset the ID tables if we are reusing the SourceManager.
595 if (hasSourceManager())
596 getSourceManager().clearIDTables();
Daniel Dunbar4f2bc552010-01-13 00:48:06 +0000597
Daniel Dunbar86546382010-06-07 23:23:06 +0000598 if (Act.BeginSourceFile(*this, InFile, getFrontendOpts().Inputs[i].first)) {
Daniel Dunbar4f2bc552010-01-13 00:48:06 +0000599 Act.Execute();
600 Act.EndSourceFile();
601 }
602 }
603
Chris Lattner198cb4d2010-04-07 18:47:42 +0000604 if (getDiagnosticOpts().ShowCarets) {
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000605 // We can have multiple diagnostics sharing one diagnostic client.
606 // Get the total number of warnings/errors from the client.
607 unsigned NumWarnings = getDiagnostics().getClient()->getNumWarnings();
608 unsigned NumErrors = getDiagnostics().getClient()->getNumErrors();
Chris Lattner198cb4d2010-04-07 18:47:42 +0000609
610 if (NumWarnings)
611 OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s");
612 if (NumWarnings && NumErrors)
613 OS << " and ";
614 if (NumErrors)
615 OS << NumErrors << " error" << (NumErrors == 1 ? "" : "s");
616 if (NumWarnings || NumErrors)
617 OS << " generated.\n";
618 }
Daniel Dunbar4f2bc552010-01-13 00:48:06 +0000619
Daniel Dunbaraed46fc2010-06-07 23:23:50 +0000620 if (getFrontendOpts().ShowStats && hasFileManager()) {
Daniel Dunbar4f2bc552010-01-13 00:48:06 +0000621 getFileManager().PrintStats();
622 OS << "\n";
623 }
624
Argyrios Kyrtzidisbc467932010-11-18 21:13:57 +0000625 return !getDiagnostics().getClient()->getNumErrors();
Daniel Dunbar4f2bc552010-01-13 00:48:06 +0000626}
627
Douglas Gregorfaeb1d42011-09-12 23:31:24 +0000628/// \brief Determine the appropriate source input kind based on language
629/// options.
630static InputKind getSourceInputKindFromOptions(const LangOptions &LangOpts) {
631 if (LangOpts.OpenCL)
632 return IK_OpenCL;
633 if (LangOpts.CUDA)
634 return IK_CUDA;
635 if (LangOpts.ObjC1)
636 return LangOpts.CPlusPlus? IK_ObjCXX : IK_ObjC;
637 return LangOpts.CPlusPlus? IK_CXX : IK_C;
638}
639
640/// \brief Compile a module file for the given module name with the given
641/// umbrella header, using the options provided by the importing compiler
642/// instance.
643static void compileModule(CompilerInstance &ImportingInstance,
644 StringRef ModuleName,
645 StringRef UmbrellaHeader) {
646 // Determine the file that we'll be writing to.
647 llvm::SmallString<128> ModuleFile;
648 ModuleFile +=
649 ImportingInstance.getInvocation().getHeaderSearchOpts().ModuleCachePath;
650 llvm::sys::path::append(ModuleFile, ModuleName + ".pcm");
651
652 // Construct a compiler invocation for creating this module.
653 llvm::IntrusiveRefCntPtr<CompilerInvocation> Invocation
654 (new CompilerInvocation(ImportingInstance.getInvocation()));
655 FrontendOptions &FrontendOpts = Invocation->getFrontendOpts();
656 FrontendOpts.OutputFile = ModuleFile.str();
657 FrontendOpts.DisableFree = false;
658 FrontendOpts.Inputs.clear();
659 FrontendOpts.Inputs.push_back(
660 std::make_pair(getSourceInputKindFromOptions(Invocation->getLangOpts()),
661 UmbrellaHeader));
662 // FIXME: Strip away all of the compilation options that won't be transferred
663 // down to the module. This presumably includes -D flags, optimization
664 // settings, etc.
665
666 // Construct a compiler instance that will be used to actually create the
667 // module.
668 CompilerInstance Instance;
669 Instance.setInvocation(&*Invocation);
670 // Instance.setDiagnostics(&ImportingInstance.getDiagnostics());
671 // FIXME: Need to route diagnostics over to the same diagnostic client!
672 Instance.createDiagnostics(0, 0, 0);
673
674 // Construct a module-generating action.
675 GeneratePCHAction CreateModuleAction(true);
676
677 // Execute the action to actually build the module in-place.
678 // FIXME: Need to synchronize when multiple processes do this.
679 Instance.ExecuteAction(CreateModuleAction);
680
681 // Tell the importing instance's file manager to forget about the module
682 // file, since we've just created it.
683 ImportingInstance.getFileManager().forgetFile(ModuleFile);
684}
685
Douglas Gregor08142532011-08-26 23:56:07 +0000686ModuleKey CompilerInstance::loadModule(SourceLocation ImportLoc,
687 IdentifierInfo &ModuleName,
688 SourceLocation ModuleNameLoc) {
689 // Determine what file we're searching from.
690 SourceManager &SourceMgr = getSourceManager();
691 SourceLocation ExpandedImportLoc = SourceMgr.getExpansionLoc(ImportLoc);
692 const FileEntry *CurFile
693 = SourceMgr.getFileEntryForID(SourceMgr.getFileID(ExpandedImportLoc));
694 if (!CurFile)
695 CurFile = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID());
696
697 // Search for a module with the given name.
Douglas Gregorfaeb1d42011-09-12 23:31:24 +0000698 std::string UmbrellaHeader;
Douglas Gregor08142532011-08-26 23:56:07 +0000699 const FileEntry *ModuleFile
Douglas Gregorfaeb1d42011-09-12 23:31:24 +0000700 = PP->getHeaderSearchInfo().lookupModule(ModuleName.getName(),
701 &UmbrellaHeader);
702
703 bool BuildingModule = false;
704 if (!ModuleFile && !UmbrellaHeader.empty()) {
705 // We didn't find the module, but there is an umbrella header that
706 // can be used to create the module file. Create a separate compilation
707 // module to do so.
708 BuildingModule = true;
709 compileModule(*this, ModuleName.getName(), UmbrellaHeader);
710 ModuleFile = PP->getHeaderSearchInfo().lookupModule(ModuleName.getName());
711 }
712
Douglas Gregor08142532011-08-26 23:56:07 +0000713 if (!ModuleFile) {
Douglas Gregorfaeb1d42011-09-12 23:31:24 +0000714 getDiagnostics().Report(ModuleNameLoc,
715 BuildingModule? diag::err_module_not_built
716 : diag::err_module_not_found)
Douglas Gregor08142532011-08-26 23:56:07 +0000717 << ModuleName.getName()
718 << SourceRange(ImportLoc, ModuleNameLoc);
719 return 0;
720 }
721
722 // If we don't already have an ASTReader, create one now.
723 if (!ModuleManager) {
724 std::string Sysroot = getHeaderSearchOpts().Sysroot;
725 const PreprocessorOptions &PPOpts = getPreprocessorOpts();
Douglas Gregor8835e032011-09-02 00:26:20 +0000726 ModuleManager = new ASTReader(getPreprocessor(), *Context,
Douglas Gregor08142532011-08-26 23:56:07 +0000727 Sysroot.empty() ? "" : Sysroot.c_str(),
728 PPOpts.DisablePCHValidation,
729 PPOpts.DisableStatCache);
730 ModuleManager->setDeserializationListener(
731 getASTConsumer().GetASTDeserializationListener());
732 getASTContext().setASTMutationListener(
733 getASTConsumer().GetASTMutationListener());
734 llvm::OwningPtr<ExternalASTSource> Source;
735 Source.reset(ModuleManager);
736 getASTContext().setExternalSource(Source);
737 ModuleManager->InitializeSema(getSema());
738 }
739
740 // Try to load the module we found.
741 switch (ModuleManager->ReadAST(ModuleFile->getName(),
742 serialization::MK_Module)) {
743 case ASTReader::Success:
744 break;
745
746 case ASTReader::IgnorePCH:
747 // FIXME: The ASTReader will already have complained, but can we showhorn
748 // that diagnostic information into a more useful form?
749 return 0;
750
751 case ASTReader::Failure:
752 // Already complained.
753 return 0;
754 }
755
756 // FIXME: The module file's FileEntry makes a poor key indeed!
757 return (ModuleKey)ModuleFile;
758}
Daniel Dunbar4f2bc552010-01-13 00:48:06 +0000759