blob: 63cf98d3da5d6fdbddd2b2f1bd274cc47fb1ede0 [file] [log] [blame]
Argyrios Kyrtzidis4b562cf2009-06-20 08:27:14 +00001//===--- ASTUnit.cpp - ASTUnit utility ------------------------------------===//
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// ASTUnit Implementation.
11//
12//===----------------------------------------------------------------------===//
13
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000014#include "clang/Frontend/ASTUnit.h"
15#include "clang/Frontend/PCHReader.h"
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000016#include "clang/AST/ASTContext.h"
Daniel Dunbar521bf9c2009-12-01 09:51:01 +000017#include "clang/AST/ASTConsumer.h"
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000018#include "clang/AST/DeclVisitor.h"
19#include "clang/AST/StmtVisitor.h"
Daniel Dunbar7b556682009-12-02 03:23:45 +000020#include "clang/Driver/Compilation.h"
21#include "clang/Driver/Driver.h"
22#include "clang/Driver/Job.h"
23#include "clang/Driver/Tool.h"
Daniel Dunbar521bf9c2009-12-01 09:51:01 +000024#include "clang/Frontend/CompilerInstance.h"
25#include "clang/Frontend/FrontendActions.h"
Daniel Dunbar7b556682009-12-02 03:23:45 +000026#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbar521bf9c2009-12-01 09:51:01 +000027#include "clang/Frontend/FrontendOptions.h"
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000028#include "clang/Lex/HeaderSearch.h"
29#include "clang/Lex/Preprocessor.h"
Daniel Dunbard58c03f2009-11-15 06:48:46 +000030#include "clang/Basic/TargetOptions.h"
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000031#include "clang/Basic/TargetInfo.h"
32#include "clang/Basic/Diagnostic.h"
Douglas Gregor4db64a42010-01-23 00:14:00 +000033#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar7b556682009-12-02 03:23:45 +000034#include "llvm/System/Host.h"
Benjamin Kramer4a630d32009-10-18 11:34:14 +000035#include "llvm/System/Path.h"
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000036using namespace clang;
37
Daniel Dunbar5262fda2009-12-03 01:45:44 +000038ASTUnit::ASTUnit(bool _MainFileIsAST)
Douglas Gregorbdf60622010-03-05 21:16:25 +000039 : MainFileIsAST(_MainFileIsAST), ConcurrencyCheckValue(CheckUnlocked) {
Steve Naroff36c44642009-10-19 14:34:22 +000040}
Daniel Dunbar521bf9c2009-12-01 09:51:01 +000041ASTUnit::~ASTUnit() {
Douglas Gregorbdf60622010-03-05 21:16:25 +000042 ConcurrencyCheckValue = CheckLocked;
Douglas Gregor313e26c2010-02-18 23:35:40 +000043 for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I)
44 TemporaryFiles[I].eraseFromDisk();
Steve Naroffe19944c2009-10-15 22:23:48 +000045}
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000046
47namespace {
48
49/// \brief Gathers information from PCHReader that will be used to initialize
50/// a Preprocessor.
Benjamin Kramerbd218282009-11-28 10:07:24 +000051class PCHInfoCollector : public PCHReaderListener {
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000052 LangOptions &LangOpt;
53 HeaderSearch &HSI;
54 std::string &TargetTriple;
55 std::string &Predefines;
56 unsigned &Counter;
Mike Stump1eb44332009-09-09 15:08:12 +000057
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000058 unsigned NumHeaderInfos;
Mike Stump1eb44332009-09-09 15:08:12 +000059
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000060public:
61 PCHInfoCollector(LangOptions &LangOpt, HeaderSearch &HSI,
62 std::string &TargetTriple, std::string &Predefines,
63 unsigned &Counter)
64 : LangOpt(LangOpt), HSI(HSI), TargetTriple(TargetTriple),
65 Predefines(Predefines), Counter(Counter), NumHeaderInfos(0) {}
Mike Stump1eb44332009-09-09 15:08:12 +000066
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000067 virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
68 LangOpt = LangOpts;
69 return false;
70 }
Mike Stump1eb44332009-09-09 15:08:12 +000071
Daniel Dunbardc3c0d22009-11-11 00:52:11 +000072 virtual bool ReadTargetTriple(llvm::StringRef Triple) {
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000073 TargetTriple = Triple;
74 return false;
75 }
Mike Stump1eb44332009-09-09 15:08:12 +000076
Daniel Dunbardc3c0d22009-11-11 00:52:11 +000077 virtual bool ReadPredefinesBuffer(llvm::StringRef PCHPredef,
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000078 FileID PCHBufferID,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +000079 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000080 std::string &SuggestedPredefines) {
81 Predefines = PCHPredef;
82 return false;
83 }
Mike Stump1eb44332009-09-09 15:08:12 +000084
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000085 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI) {
86 HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
87 }
Mike Stump1eb44332009-09-09 15:08:12 +000088
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000089 virtual void ReadCounter(unsigned Value) {
90 Counter = Value;
91 }
92};
93
Douglas Gregora88084b2010-02-18 18:08:43 +000094class StoredDiagnosticClient : public DiagnosticClient {
95 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags;
96
97public:
98 explicit StoredDiagnosticClient(
99 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags)
100 : StoredDiags(StoredDiags) { }
101
102 virtual void HandleDiagnostic(Diagnostic::Level Level,
103 const DiagnosticInfo &Info);
104};
105
106/// \brief RAII object that optionally captures diagnostics, if
107/// there is no diagnostic client to capture them already.
108class CaptureDroppedDiagnostics {
109 Diagnostic &Diags;
110 StoredDiagnosticClient Client;
111 DiagnosticClient *PreviousClient;
112
113public:
114 CaptureDroppedDiagnostics(bool RequestCapture, Diagnostic &Diags,
115 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags)
116 : Diags(Diags), Client(StoredDiags), PreviousClient(Diags.getClient())
117 {
118 if (RequestCapture || Diags.getClient() == 0)
119 Diags.setClient(&Client);
120 }
121
122 ~CaptureDroppedDiagnostics() {
123 Diags.setClient(PreviousClient);
124 }
125};
126
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000127} // anonymous namespace
128
Douglas Gregora88084b2010-02-18 18:08:43 +0000129void StoredDiagnosticClient::HandleDiagnostic(Diagnostic::Level Level,
130 const DiagnosticInfo &Info) {
131 StoredDiags.push_back(StoredDiagnostic(Level, Info));
132}
133
Steve Naroff77accc12009-09-03 18:19:54 +0000134const std::string &ASTUnit::getOriginalSourceFileName() {
Daniel Dunbar68d40e22009-12-02 08:44:16 +0000135 return OriginalSourceFile;
Steve Naroff77accc12009-09-03 18:19:54 +0000136}
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000137
Steve Naroffe19944c2009-10-15 22:23:48 +0000138const std::string &ASTUnit::getPCHFileName() {
Daniel Dunbarc7822db2009-12-02 21:47:43 +0000139 assert(isMainFileAST() && "Not an ASTUnit from a PCH file!");
Benjamin Kramer7297c182010-01-30 16:23:25 +0000140 return static_cast<PCHReader *>(Ctx->getExternalSource())->getFileName();
Steve Naroffe19944c2009-10-15 22:23:48 +0000141}
142
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000143ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename,
Daniel Dunbar5262fda2009-12-03 01:45:44 +0000144 Diagnostic &Diags,
Ted Kremenek5cf48762009-10-17 00:34:24 +0000145 bool OnlyLocalDecls,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000146 RemappedFile *RemappedFiles,
Douglas Gregora88084b2010-02-18 18:08:43 +0000147 unsigned NumRemappedFiles,
148 bool CaptureDiagnostics) {
Daniel Dunbar5262fda2009-12-03 01:45:44 +0000149 llvm::OwningPtr<ASTUnit> AST(new ASTUnit(true));
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000150 AST->OnlyLocalDecls = OnlyLocalDecls;
Steve Naroff36c44642009-10-19 14:34:22 +0000151 AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager()));
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000152
Douglas Gregora88084b2010-02-18 18:08:43 +0000153 // If requested, capture diagnostics in the ASTUnit.
154 CaptureDroppedDiagnostics Capture(CaptureDiagnostics, Diags,
155 AST->Diagnostics);
156
Douglas Gregor4db64a42010-01-23 00:14:00 +0000157 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
158 // Create the file entry for the file that we're mapping from.
159 const FileEntry *FromFile
160 = AST->getFileManager().getVirtualFile(RemappedFiles[I].first,
161 RemappedFiles[I].second->getBufferSize(),
162 0);
163 if (!FromFile) {
164 Diags.Report(diag::err_fe_remap_missing_from_file)
165 << RemappedFiles[I].first;
Douglas Gregorc8dfe5e2010-02-27 01:32:48 +0000166 delete RemappedFiles[I].second;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000167 continue;
168 }
169
170 // Override the contents of the "from" file with the contents of
171 // the "to" file.
172 AST->getSourceManager().overrideFileContents(FromFile,
173 RemappedFiles[I].second);
174 }
175
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000176 // Gather Info for preprocessor construction later on.
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000178 LangOptions LangInfo;
179 HeaderSearch &HeaderInfo = *AST->HeaderInfo.get();
180 std::string TargetTriple;
181 std::string Predefines;
182 unsigned Counter;
183
Daniel Dunbarbce6f622009-09-03 05:59:50 +0000184 llvm::OwningPtr<PCHReader> Reader;
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000185 llvm::OwningPtr<ExternalASTSource> Source;
186
Ted Kremenekfc062212009-10-19 21:44:57 +0000187 Reader.reset(new PCHReader(AST->getSourceManager(), AST->getFileManager(),
Daniel Dunbar5262fda2009-12-03 01:45:44 +0000188 Diags));
Daniel Dunbarcc318932009-09-03 05:59:35 +0000189 Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple,
190 Predefines, Counter));
191
192 switch (Reader->ReadPCH(Filename)) {
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000193 case PCHReader::Success:
194 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000195
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000196 case PCHReader::Failure:
Argyrios Kyrtzidis106c9982009-06-25 18:22:30 +0000197 case PCHReader::IgnorePCH:
Daniel Dunbar5262fda2009-12-03 01:45:44 +0000198 Diags.Report(diag::err_fe_unable_to_load_pch);
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000199 return NULL;
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000200 }
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Daniel Dunbar68d40e22009-12-02 08:44:16 +0000202 AST->OriginalSourceFile = Reader->getOriginalSourceFile();
203
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000204 // PCH loaded successfully. Now create the preprocessor.
Mike Stump1eb44332009-09-09 15:08:12 +0000205
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000206 // Get information about the target being compiled for.
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000207 //
208 // FIXME: This is broken, we should store the TargetOptions in the PCH.
209 TargetOptions TargetOpts;
210 TargetOpts.ABI = "";
211 TargetOpts.CPU = "";
212 TargetOpts.Features.clear();
213 TargetOpts.Triple = TargetTriple;
Daniel Dunbar5262fda2009-12-03 01:45:44 +0000214 AST->Target.reset(TargetInfo::CreateTargetInfo(Diags, TargetOpts));
215 AST->PP.reset(new Preprocessor(Diags, LangInfo, *AST->Target.get(),
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000216 AST->getSourceManager(), HeaderInfo));
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000217 Preprocessor &PP = *AST->PP.get();
218
Daniel Dunbard5b61262009-09-21 03:03:47 +0000219 PP.setPredefines(Reader->getSuggestedPredefines());
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000220 PP.setCounterValue(Counter);
Daniel Dunbarcc318932009-09-03 05:59:35 +0000221 Reader->setPreprocessor(PP);
Mike Stump1eb44332009-09-09 15:08:12 +0000222
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000223 // Create and initialize the ASTContext.
224
225 AST->Ctx.reset(new ASTContext(LangInfo,
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000226 AST->getSourceManager(),
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000227 *AST->Target.get(),
228 PP.getIdentifierTable(),
229 PP.getSelectorTable(),
230 PP.getBuiltinInfo(),
Daniel Dunbarb26d4832010-02-16 01:55:04 +0000231 /* FreeMemory = */ false,
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000232 /* size_reserve = */0));
233 ASTContext &Context = *AST->Ctx.get();
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Daniel Dunbarcc318932009-09-03 05:59:35 +0000235 Reader->InitializeContext(Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000237 // Attach the PCH reader to the AST context as an external AST
238 // source, so that declarations will be deserialized from the
239 // PCH file as needed.
Daniel Dunbarcc318932009-09-03 05:59:35 +0000240 Source.reset(Reader.take());
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000241 Context.setExternalSource(Source);
242
Mike Stump1eb44332009-09-09 15:08:12 +0000243 return AST.take();
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000244}
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000245
246namespace {
247
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000248class TopLevelDeclTrackerConsumer : public ASTConsumer {
249 ASTUnit &Unit;
250
251public:
252 TopLevelDeclTrackerConsumer(ASTUnit &_Unit) : Unit(_Unit) {}
253
254 void HandleTopLevelDecl(DeclGroupRef D) {
255 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it)
256 Unit.getTopLevelDecls().push_back(*it);
257 }
258};
259
260class TopLevelDeclTrackerAction : public ASTFrontendAction {
261public:
262 ASTUnit &Unit;
263
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000264 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
265 llvm::StringRef InFile) {
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000266 return new TopLevelDeclTrackerConsumer(Unit);
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000267 }
268
269public:
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000270 TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {}
271
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000272 virtual bool hasCodeCompletionSupport() const { return false; }
273};
274
275}
276
Daniel Dunbarf7acc372010-02-16 01:54:54 +0000277ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI,
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000278 Diagnostic &Diags,
Douglas Gregora88084b2010-02-18 18:08:43 +0000279 bool OnlyLocalDecls,
280 bool CaptureDiagnostics) {
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000281 // Create the compiler instance to use for building the AST.
Daniel Dunbarcb6dda12009-12-02 08:43:56 +0000282 CompilerInstance Clang;
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000283 llvm::OwningPtr<ASTUnit> AST;
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000284 llvm::OwningPtr<TopLevelDeclTrackerAction> Act;
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000285
Daniel Dunbarf7acc372010-02-16 01:54:54 +0000286 Clang.setInvocation(CI);
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000287
288 Clang.setDiagnostics(&Diags);
289 Clang.setDiagnosticClient(Diags.getClient());
290
291 // Create the target instance.
292 Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(),
293 Clang.getTargetOpts()));
Douglas Gregora88084b2010-02-18 18:08:43 +0000294 if (!Clang.hasTarget()) {
295 Clang.takeSourceManager();
296 Clang.takeFileManager();
297 Clang.takeDiagnosticClient();
298 Clang.takeDiagnostics();
299 return 0;
300 }
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000301
302 // Inform the target of the language options.
303 //
304 // FIXME: We shouldn't need to do this, the target should be immutable once
305 // created. This complexity should be lifted elsewhere.
306 Clang.getTarget().setForcedLangOptions(Clang.getLangOpts());
307
308 assert(Clang.getFrontendOpts().Inputs.size() == 1 &&
309 "Invocation must have exactly one source file!");
310 assert(Clang.getFrontendOpts().Inputs[0].first != FrontendOptions::IK_AST &&
311 "FIXME: AST inputs not yet supported here!");
312
313 // Create the AST unit.
Daniel Dunbarc7822db2009-12-02 21:47:43 +0000314 AST.reset(new ASTUnit(false));
Daniel Dunbar68ea2ac2009-12-02 21:47:32 +0000315 AST->OnlyLocalDecls = OnlyLocalDecls;
Daniel Dunbar68d40e22009-12-02 08:44:16 +0000316 AST->OriginalSourceFile = Clang.getFrontendOpts().Inputs[0].second;
317
Douglas Gregora88084b2010-02-18 18:08:43 +0000318 // Capture any diagnostics that would otherwise be dropped.
319 CaptureDroppedDiagnostics Capture(CaptureDiagnostics,
320 Clang.getDiagnostics(),
321 AST->Diagnostics);
322
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000323 // Create a file manager object to provide access to and cache the filesystem.
324 Clang.setFileManager(&AST->getFileManager());
325
326 // Create the source manager.
327 Clang.setSourceManager(&AST->getSourceManager());
328
329 // Create the preprocessor.
330 Clang.createPreprocessor();
331
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000332 Act.reset(new TopLevelDeclTrackerAction(*AST));
333 if (!Act->BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second,
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000334 /*IsAST=*/false))
335 goto error;
336
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000337 Act->Execute();
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000338
Daniel Dunbar64a32ba2009-12-01 21:57:33 +0000339 // Steal the created target, context, and preprocessor, and take back the
340 // source and file managers.
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000341 AST->Ctx.reset(Clang.takeASTContext());
342 AST->PP.reset(Clang.takePreprocessor());
343 Clang.takeSourceManager();
344 Clang.takeFileManager();
Daniel Dunbar64a32ba2009-12-01 21:57:33 +0000345 AST->Target.reset(Clang.takeTarget());
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000346
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000347 Act->EndSourceFile();
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000348
349 Clang.takeDiagnosticClient();
350 Clang.takeDiagnostics();
Daniel Dunbar807b0612010-01-30 21:47:16 +0000351 Clang.takeInvocation();
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000352
Daniel Dunbarf7acc372010-02-16 01:54:54 +0000353 AST->Invocation.reset(Clang.takeInvocation());
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000354 return AST.take();
355
356error:
357 Clang.takeSourceManager();
358 Clang.takeFileManager();
359 Clang.takeDiagnosticClient();
360 Clang.takeDiagnostics();
361 return 0;
362}
Daniel Dunbar7b556682009-12-02 03:23:45 +0000363
364ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin,
365 const char **ArgEnd,
366 Diagnostic &Diags,
Daniel Dunbar869824e2009-12-13 03:46:13 +0000367 llvm::StringRef ResourceFilesPath,
Daniel Dunbar7b556682009-12-02 03:23:45 +0000368 bool OnlyLocalDecls,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000369 RemappedFile *RemappedFiles,
Douglas Gregora88084b2010-02-18 18:08:43 +0000370 unsigned NumRemappedFiles,
371 bool CaptureDiagnostics) {
Daniel Dunbar7b556682009-12-02 03:23:45 +0000372 llvm::SmallVector<const char *, 16> Args;
373 Args.push_back("<clang>"); // FIXME: Remove dummy argument.
374 Args.insert(Args.end(), ArgBegin, ArgEnd);
375
376 // FIXME: Find a cleaner way to force the driver into restricted modes. We
377 // also want to force it to use clang.
378 Args.push_back("-fsyntax-only");
379
Daniel Dunbar869824e2009-12-13 03:46:13 +0000380 // FIXME: We shouldn't have to pass in the path info.
381 driver::Driver TheDriver("clang", "/", llvm::sys::getHostTriple(),
Daniel Dunbar7b556682009-12-02 03:23:45 +0000382 "a.out", false, Diags);
Daniel Dunbar3bd54cc2010-01-25 00:44:02 +0000383
384 // Don't check that inputs exist, they have been remapped.
385 TheDriver.setCheckInputsExist(false);
386
Daniel Dunbar7b556682009-12-02 03:23:45 +0000387 llvm::OwningPtr<driver::Compilation> C(
388 TheDriver.BuildCompilation(Args.size(), Args.data()));
389
390 // We expect to get back exactly one command job, if we didn't something
391 // failed.
392 const driver::JobList &Jobs = C->getJobs();
393 if (Jobs.size() != 1 || !isa<driver::Command>(Jobs.begin())) {
394 llvm::SmallString<256> Msg;
395 llvm::raw_svector_ostream OS(Msg);
396 C->PrintJob(OS, C->getJobs(), "; ", true);
397 Diags.Report(diag::err_fe_expected_compiler_job) << OS.str();
398 return 0;
399 }
400
401 const driver::Command *Cmd = cast<driver::Command>(*Jobs.begin());
402 if (llvm::StringRef(Cmd->getCreator().getName()) != "clang") {
403 Diags.Report(diag::err_fe_expected_clang_command);
404 return 0;
405 }
406
407 const driver::ArgStringList &CCArgs = Cmd->getArguments();
Daniel Dunbar807b0612010-01-30 21:47:16 +0000408 llvm::OwningPtr<CompilerInvocation> CI(new CompilerInvocation);
409 CompilerInvocation::CreateFromArgs(*CI, (const char**) CCArgs.data(),
Daniel Dunbar7b556682009-12-02 03:23:45 +0000410 (const char**) CCArgs.data()+CCArgs.size(),
Daniel Dunbar1e69fe32009-12-13 03:45:58 +0000411 Diags);
412
Douglas Gregor4db64a42010-01-23 00:14:00 +0000413 // Override any files that need remapping
414 for (unsigned I = 0; I != NumRemappedFiles; ++I)
Daniel Dunbar807b0612010-01-30 21:47:16 +0000415 CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
Daniel Dunbarb26d4832010-02-16 01:55:04 +0000416 RemappedFiles[I].second);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000417
Daniel Dunbar8b9adfe2009-12-15 00:06:45 +0000418 // Override the resources path.
Daniel Dunbar807b0612010-01-30 21:47:16 +0000419 CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
Daniel Dunbar7b556682009-12-02 03:23:45 +0000420
Daniel Dunbarb26d4832010-02-16 01:55:04 +0000421 CI->getFrontendOpts().DisableFree = true;
Douglas Gregora88084b2010-02-18 18:08:43 +0000422 return LoadFromCompilerInvocation(CI.take(), Diags, OnlyLocalDecls,
423 CaptureDiagnostics);
Daniel Dunbar7b556682009-12-02 03:23:45 +0000424}