blob: 4ea1ccc943b6bc4dbee52c6997b6062aafbcd51e [file] [log] [blame]
Argyrios Kyrtzidis3a08ec12009-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 Kyrtzidisce379752009-06-20 08:08:23 +000014#include "clang/Frontend/ASTUnit.h"
15#include "clang/Frontend/PCHReader.h"
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000016#include "clang/AST/ASTContext.h"
Daniel Dunbar764c0822009-12-01 09:51:01 +000017#include "clang/AST/ASTConsumer.h"
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000018#include "clang/AST/DeclVisitor.h"
19#include "clang/AST/StmtVisitor.h"
Daniel Dunbar55a17b62009-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 Dunbar764c0822009-12-01 09:51:01 +000024#include "clang/Frontend/CompilerInstance.h"
25#include "clang/Frontend/FrontendActions.h"
Daniel Dunbar55a17b62009-12-02 03:23:45 +000026#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbar764c0822009-12-01 09:51:01 +000027#include "clang/Frontend/FrontendOptions.h"
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000028#include "clang/Lex/HeaderSearch.h"
29#include "clang/Lex/Preprocessor.h"
Daniel Dunbarb9bbd542009-11-15 06:48:46 +000030#include "clang/Basic/TargetOptions.h"
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000031#include "clang/Basic/TargetInfo.h"
32#include "clang/Basic/Diagnostic.h"
Douglas Gregoraa98ed92010-01-23 00:14:00 +000033#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar55a17b62009-12-02 03:23:45 +000034#include "llvm/System/Host.h"
Benjamin Kramer6c839f82009-10-18 11:34:14 +000035#include "llvm/System/Path.h"
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000036using namespace clang;
37
Daniel Dunbar59203002009-12-03 01:45:44 +000038ASTUnit::ASTUnit(bool _MainFileIsAST)
Douglas Gregor0c7c2f82010-03-05 21:16:25 +000039 : MainFileIsAST(_MainFileIsAST), ConcurrencyCheckValue(CheckUnlocked) {
Steve Naroff505fb842009-10-19 14:34:22 +000040}
Daniel Dunbar764c0822009-12-01 09:51:01 +000041ASTUnit::~ASTUnit() {
Douglas Gregor0c7c2f82010-03-05 21:16:25 +000042#ifndef NDEBUG
43 ConcurrencyCheckValue = CheckLocked;
44#endif
Douglas Gregor6cb5ba42010-02-18 23:35:40 +000045 for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I)
46 TemporaryFiles[I].eraseFromDisk();
Steve Naroff44cd60e2009-10-15 22:23:48 +000047}
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000048
49namespace {
50
51/// \brief Gathers information from PCHReader that will be used to initialize
52/// a Preprocessor.
Benjamin Kramer16634c22009-11-28 10:07:24 +000053class PCHInfoCollector : public PCHReaderListener {
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000054 LangOptions &LangOpt;
55 HeaderSearch &HSI;
56 std::string &TargetTriple;
57 std::string &Predefines;
58 unsigned &Counter;
Mike Stump11289f42009-09-09 15:08:12 +000059
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000060 unsigned NumHeaderInfos;
Mike Stump11289f42009-09-09 15:08:12 +000061
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000062public:
63 PCHInfoCollector(LangOptions &LangOpt, HeaderSearch &HSI,
64 std::string &TargetTriple, std::string &Predefines,
65 unsigned &Counter)
66 : LangOpt(LangOpt), HSI(HSI), TargetTriple(TargetTriple),
67 Predefines(Predefines), Counter(Counter), NumHeaderInfos(0) {}
Mike Stump11289f42009-09-09 15:08:12 +000068
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000069 virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
70 LangOpt = LangOpts;
71 return false;
72 }
Mike Stump11289f42009-09-09 15:08:12 +000073
Daniel Dunbar20a682d2009-11-11 00:52:11 +000074 virtual bool ReadTargetTriple(llvm::StringRef Triple) {
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000075 TargetTriple = Triple;
76 return false;
77 }
Mike Stump11289f42009-09-09 15:08:12 +000078
Daniel Dunbar20a682d2009-11-11 00:52:11 +000079 virtual bool ReadPredefinesBuffer(llvm::StringRef PCHPredef,
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000080 FileID PCHBufferID,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +000081 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000082 std::string &SuggestedPredefines) {
83 Predefines = PCHPredef;
84 return false;
85 }
Mike Stump11289f42009-09-09 15:08:12 +000086
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000087 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI) {
88 HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
89 }
Mike Stump11289f42009-09-09 15:08:12 +000090
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000091 virtual void ReadCounter(unsigned Value) {
92 Counter = Value;
93 }
94};
95
Douglas Gregor33cdd812010-02-18 18:08:43 +000096class StoredDiagnosticClient : public DiagnosticClient {
97 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags;
98
99public:
100 explicit StoredDiagnosticClient(
101 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags)
102 : StoredDiags(StoredDiags) { }
103
104 virtual void HandleDiagnostic(Diagnostic::Level Level,
105 const DiagnosticInfo &Info);
106};
107
108/// \brief RAII object that optionally captures diagnostics, if
109/// there is no diagnostic client to capture them already.
110class CaptureDroppedDiagnostics {
111 Diagnostic &Diags;
112 StoredDiagnosticClient Client;
113 DiagnosticClient *PreviousClient;
114
115public:
116 CaptureDroppedDiagnostics(bool RequestCapture, Diagnostic &Diags,
117 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags)
118 : Diags(Diags), Client(StoredDiags), PreviousClient(Diags.getClient())
119 {
120 if (RequestCapture || Diags.getClient() == 0)
121 Diags.setClient(&Client);
122 }
123
124 ~CaptureDroppedDiagnostics() {
125 Diags.setClient(PreviousClient);
126 }
127};
128
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000129} // anonymous namespace
130
Douglas Gregor33cdd812010-02-18 18:08:43 +0000131void StoredDiagnosticClient::HandleDiagnostic(Diagnostic::Level Level,
132 const DiagnosticInfo &Info) {
133 StoredDiags.push_back(StoredDiagnostic(Level, Info));
134}
135
Steve Naroffc0683b92009-09-03 18:19:54 +0000136const std::string &ASTUnit::getOriginalSourceFileName() {
Daniel Dunbara8a50932009-12-02 08:44:16 +0000137 return OriginalSourceFile;
Steve Naroffc0683b92009-09-03 18:19:54 +0000138}
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000139
Steve Naroff44cd60e2009-10-15 22:23:48 +0000140const std::string &ASTUnit::getPCHFileName() {
Daniel Dunbara18f9582009-12-02 21:47:43 +0000141 assert(isMainFileAST() && "Not an ASTUnit from a PCH file!");
Benjamin Kramer2ecf8eb2010-01-30 16:23:25 +0000142 return static_cast<PCHReader *>(Ctx->getExternalSource())->getFileName();
Steve Naroff44cd60e2009-10-15 22:23:48 +0000143}
144
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000145ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename,
Daniel Dunbar59203002009-12-03 01:45:44 +0000146 Diagnostic &Diags,
Ted Kremenek8bcb1c62009-10-17 00:34:24 +0000147 bool OnlyLocalDecls,
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000148 RemappedFile *RemappedFiles,
Douglas Gregor33cdd812010-02-18 18:08:43 +0000149 unsigned NumRemappedFiles,
150 bool CaptureDiagnostics) {
Daniel Dunbar59203002009-12-03 01:45:44 +0000151 llvm::OwningPtr<ASTUnit> AST(new ASTUnit(true));
Douglas Gregor16bef852009-10-16 20:01:17 +0000152 AST->OnlyLocalDecls = OnlyLocalDecls;
Steve Naroff505fb842009-10-19 14:34:22 +0000153 AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager()));
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000154
Douglas Gregor33cdd812010-02-18 18:08:43 +0000155 // If requested, capture diagnostics in the ASTUnit.
156 CaptureDroppedDiagnostics Capture(CaptureDiagnostics, Diags,
157 AST->Diagnostics);
158
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000159 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
160 // Create the file entry for the file that we're mapping from.
161 const FileEntry *FromFile
162 = AST->getFileManager().getVirtualFile(RemappedFiles[I].first,
163 RemappedFiles[I].second->getBufferSize(),
164 0);
165 if (!FromFile) {
166 Diags.Report(diag::err_fe_remap_missing_from_file)
167 << RemappedFiles[I].first;
Douglas Gregor89a56c52010-02-27 01:32:48 +0000168 delete RemappedFiles[I].second;
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000169 continue;
170 }
171
172 // Override the contents of the "from" file with the contents of
173 // the "to" file.
174 AST->getSourceManager().overrideFileContents(FromFile,
175 RemappedFiles[I].second);
176 }
177
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000178 // Gather Info for preprocessor construction later on.
Mike Stump11289f42009-09-09 15:08:12 +0000179
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000180 LangOptions LangInfo;
181 HeaderSearch &HeaderInfo = *AST->HeaderInfo.get();
182 std::string TargetTriple;
183 std::string Predefines;
184 unsigned Counter;
185
Daniel Dunbar3a0637b2009-09-03 05:59:50 +0000186 llvm::OwningPtr<PCHReader> Reader;
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000187 llvm::OwningPtr<ExternalASTSource> Source;
188
Ted Kremenek428c6372009-10-19 21:44:57 +0000189 Reader.reset(new PCHReader(AST->getSourceManager(), AST->getFileManager(),
Daniel Dunbar59203002009-12-03 01:45:44 +0000190 Diags));
Daniel Dunbar2d9c7402009-09-03 05:59:35 +0000191 Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple,
192 Predefines, Counter));
193
194 switch (Reader->ReadPCH(Filename)) {
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000195 case PCHReader::Success:
196 break;
Mike Stump11289f42009-09-09 15:08:12 +0000197
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000198 case PCHReader::Failure:
Argyrios Kyrtzidis55c34112009-06-25 18:22:30 +0000199 case PCHReader::IgnorePCH:
Daniel Dunbar59203002009-12-03 01:45:44 +0000200 Diags.Report(diag::err_fe_unable_to_load_pch);
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000201 return NULL;
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000202 }
Mike Stump11289f42009-09-09 15:08:12 +0000203
Daniel Dunbara8a50932009-12-02 08:44:16 +0000204 AST->OriginalSourceFile = Reader->getOriginalSourceFile();
205
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000206 // PCH loaded successfully. Now create the preprocessor.
Mike Stump11289f42009-09-09 15:08:12 +0000207
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000208 // Get information about the target being compiled for.
Daniel Dunbarb9bbd542009-11-15 06:48:46 +0000209 //
210 // FIXME: This is broken, we should store the TargetOptions in the PCH.
211 TargetOptions TargetOpts;
212 TargetOpts.ABI = "";
213 TargetOpts.CPU = "";
214 TargetOpts.Features.clear();
215 TargetOpts.Triple = TargetTriple;
Daniel Dunbar59203002009-12-03 01:45:44 +0000216 AST->Target.reset(TargetInfo::CreateTargetInfo(Diags, TargetOpts));
217 AST->PP.reset(new Preprocessor(Diags, LangInfo, *AST->Target.get(),
Daniel Dunbar7cd285f2009-09-21 03:03:39 +0000218 AST->getSourceManager(), HeaderInfo));
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000219 Preprocessor &PP = *AST->PP.get();
220
Daniel Dunbarb7bbfdd2009-09-21 03:03:47 +0000221 PP.setPredefines(Reader->getSuggestedPredefines());
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000222 PP.setCounterValue(Counter);
Daniel Dunbar2d9c7402009-09-03 05:59:35 +0000223 Reader->setPreprocessor(PP);
Mike Stump11289f42009-09-09 15:08:12 +0000224
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000225 // Create and initialize the ASTContext.
226
227 AST->Ctx.reset(new ASTContext(LangInfo,
Daniel Dunbar7cd285f2009-09-21 03:03:39 +0000228 AST->getSourceManager(),
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000229 *AST->Target.get(),
230 PP.getIdentifierTable(),
231 PP.getSelectorTable(),
232 PP.getBuiltinInfo(),
Daniel Dunbar19511922010-02-16 01:55:04 +0000233 /* FreeMemory = */ false,
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000234 /* size_reserve = */0));
235 ASTContext &Context = *AST->Ctx.get();
Mike Stump11289f42009-09-09 15:08:12 +0000236
Daniel Dunbar2d9c7402009-09-03 05:59:35 +0000237 Reader->InitializeContext(Context);
Mike Stump11289f42009-09-09 15:08:12 +0000238
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000239 // Attach the PCH reader to the AST context as an external AST
240 // source, so that declarations will be deserialized from the
241 // PCH file as needed.
Daniel Dunbar2d9c7402009-09-03 05:59:35 +0000242 Source.reset(Reader.take());
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000243 Context.setExternalSource(Source);
244
Mike Stump11289f42009-09-09 15:08:12 +0000245 return AST.take();
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000246}
Daniel Dunbar764c0822009-12-01 09:51:01 +0000247
248namespace {
249
Daniel Dunbar644dca02009-12-04 08:17:33 +0000250class TopLevelDeclTrackerConsumer : public ASTConsumer {
251 ASTUnit &Unit;
252
253public:
254 TopLevelDeclTrackerConsumer(ASTUnit &_Unit) : Unit(_Unit) {}
255
256 void HandleTopLevelDecl(DeclGroupRef D) {
257 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it)
258 Unit.getTopLevelDecls().push_back(*it);
259 }
260};
261
262class TopLevelDeclTrackerAction : public ASTFrontendAction {
263public:
264 ASTUnit &Unit;
265
Daniel Dunbar764c0822009-12-01 09:51:01 +0000266 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
267 llvm::StringRef InFile) {
Daniel Dunbar644dca02009-12-04 08:17:33 +0000268 return new TopLevelDeclTrackerConsumer(Unit);
Daniel Dunbar764c0822009-12-01 09:51:01 +0000269 }
270
271public:
Daniel Dunbar644dca02009-12-04 08:17:33 +0000272 TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {}
273
Daniel Dunbar764c0822009-12-01 09:51:01 +0000274 virtual bool hasCodeCompletionSupport() const { return false; }
275};
276
277}
278
Daniel Dunbar7f21a7f2010-02-16 01:54:54 +0000279ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI,
Daniel Dunbar764c0822009-12-01 09:51:01 +0000280 Diagnostic &Diags,
Douglas Gregor33cdd812010-02-18 18:08:43 +0000281 bool OnlyLocalDecls,
282 bool CaptureDiagnostics) {
Daniel Dunbar764c0822009-12-01 09:51:01 +0000283 // Create the compiler instance to use for building the AST.
Daniel Dunbar7afbb8c2009-12-02 08:43:56 +0000284 CompilerInstance Clang;
Daniel Dunbar764c0822009-12-01 09:51:01 +0000285 llvm::OwningPtr<ASTUnit> AST;
Daniel Dunbar644dca02009-12-04 08:17:33 +0000286 llvm::OwningPtr<TopLevelDeclTrackerAction> Act;
Daniel Dunbar764c0822009-12-01 09:51:01 +0000287
Daniel Dunbar7f21a7f2010-02-16 01:54:54 +0000288 Clang.setInvocation(CI);
Daniel Dunbar764c0822009-12-01 09:51:01 +0000289
290 Clang.setDiagnostics(&Diags);
291 Clang.setDiagnosticClient(Diags.getClient());
292
293 // Create the target instance.
294 Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(),
295 Clang.getTargetOpts()));
Douglas Gregor33cdd812010-02-18 18:08:43 +0000296 if (!Clang.hasTarget()) {
297 Clang.takeSourceManager();
298 Clang.takeFileManager();
299 Clang.takeDiagnosticClient();
300 Clang.takeDiagnostics();
301 return 0;
302 }
Daniel Dunbar764c0822009-12-01 09:51:01 +0000303
304 // Inform the target of the language options.
305 //
306 // FIXME: We shouldn't need to do this, the target should be immutable once
307 // created. This complexity should be lifted elsewhere.
308 Clang.getTarget().setForcedLangOptions(Clang.getLangOpts());
309
310 assert(Clang.getFrontendOpts().Inputs.size() == 1 &&
311 "Invocation must have exactly one source file!");
312 assert(Clang.getFrontendOpts().Inputs[0].first != FrontendOptions::IK_AST &&
313 "FIXME: AST inputs not yet supported here!");
314
315 // Create the AST unit.
Daniel Dunbara18f9582009-12-02 21:47:43 +0000316 AST.reset(new ASTUnit(false));
Daniel Dunbar48973492009-12-02 21:47:32 +0000317 AST->OnlyLocalDecls = OnlyLocalDecls;
Daniel Dunbara8a50932009-12-02 08:44:16 +0000318 AST->OriginalSourceFile = Clang.getFrontendOpts().Inputs[0].second;
319
Douglas Gregor33cdd812010-02-18 18:08:43 +0000320 // Capture any diagnostics that would otherwise be dropped.
321 CaptureDroppedDiagnostics Capture(CaptureDiagnostics,
322 Clang.getDiagnostics(),
323 AST->Diagnostics);
324
Daniel Dunbar764c0822009-12-01 09:51:01 +0000325 // Create a file manager object to provide access to and cache the filesystem.
326 Clang.setFileManager(&AST->getFileManager());
327
328 // Create the source manager.
329 Clang.setSourceManager(&AST->getSourceManager());
330
331 // Create the preprocessor.
332 Clang.createPreprocessor();
333
Daniel Dunbar644dca02009-12-04 08:17:33 +0000334 Act.reset(new TopLevelDeclTrackerAction(*AST));
335 if (!Act->BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second,
Daniel Dunbar764c0822009-12-01 09:51:01 +0000336 /*IsAST=*/false))
337 goto error;
338
Daniel Dunbar644dca02009-12-04 08:17:33 +0000339 Act->Execute();
Daniel Dunbar764c0822009-12-01 09:51:01 +0000340
Daniel Dunbard2f8be32009-12-01 21:57:33 +0000341 // Steal the created target, context, and preprocessor, and take back the
342 // source and file managers.
Daniel Dunbar764c0822009-12-01 09:51:01 +0000343 AST->Ctx.reset(Clang.takeASTContext());
344 AST->PP.reset(Clang.takePreprocessor());
345 Clang.takeSourceManager();
346 Clang.takeFileManager();
Daniel Dunbard2f8be32009-12-01 21:57:33 +0000347 AST->Target.reset(Clang.takeTarget());
Daniel Dunbar764c0822009-12-01 09:51:01 +0000348
Daniel Dunbar644dca02009-12-04 08:17:33 +0000349 Act->EndSourceFile();
Daniel Dunbar764c0822009-12-01 09:51:01 +0000350
351 Clang.takeDiagnosticClient();
352 Clang.takeDiagnostics();
Daniel Dunbar6b03ece2010-01-30 21:47:16 +0000353 Clang.takeInvocation();
Daniel Dunbar764c0822009-12-01 09:51:01 +0000354
Daniel Dunbar7f21a7f2010-02-16 01:54:54 +0000355 AST->Invocation.reset(Clang.takeInvocation());
Daniel Dunbar764c0822009-12-01 09:51:01 +0000356 return AST.take();
357
358error:
359 Clang.takeSourceManager();
360 Clang.takeFileManager();
361 Clang.takeDiagnosticClient();
362 Clang.takeDiagnostics();
363 return 0;
364}
Daniel Dunbar55a17b62009-12-02 03:23:45 +0000365
366ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin,
367 const char **ArgEnd,
368 Diagnostic &Diags,
Daniel Dunbar8d4a2022009-12-13 03:46:13 +0000369 llvm::StringRef ResourceFilesPath,
Daniel Dunbar55a17b62009-12-02 03:23:45 +0000370 bool OnlyLocalDecls,
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000371 RemappedFile *RemappedFiles,
Douglas Gregor33cdd812010-02-18 18:08:43 +0000372 unsigned NumRemappedFiles,
373 bool CaptureDiagnostics) {
Daniel Dunbar55a17b62009-12-02 03:23:45 +0000374 llvm::SmallVector<const char *, 16> Args;
375 Args.push_back("<clang>"); // FIXME: Remove dummy argument.
376 Args.insert(Args.end(), ArgBegin, ArgEnd);
377
378 // FIXME: Find a cleaner way to force the driver into restricted modes. We
379 // also want to force it to use clang.
380 Args.push_back("-fsyntax-only");
381
Daniel Dunbar8d4a2022009-12-13 03:46:13 +0000382 // FIXME: We shouldn't have to pass in the path info.
383 driver::Driver TheDriver("clang", "/", llvm::sys::getHostTriple(),
Daniel Dunbar55a17b62009-12-02 03:23:45 +0000384 "a.out", false, Diags);
Daniel Dunbarfcf2d422010-01-25 00:44:02 +0000385
386 // Don't check that inputs exist, they have been remapped.
387 TheDriver.setCheckInputsExist(false);
388
Daniel Dunbar55a17b62009-12-02 03:23:45 +0000389 llvm::OwningPtr<driver::Compilation> C(
390 TheDriver.BuildCompilation(Args.size(), Args.data()));
391
392 // We expect to get back exactly one command job, if we didn't something
393 // failed.
394 const driver::JobList &Jobs = C->getJobs();
395 if (Jobs.size() != 1 || !isa<driver::Command>(Jobs.begin())) {
396 llvm::SmallString<256> Msg;
397 llvm::raw_svector_ostream OS(Msg);
398 C->PrintJob(OS, C->getJobs(), "; ", true);
399 Diags.Report(diag::err_fe_expected_compiler_job) << OS.str();
400 return 0;
401 }
402
403 const driver::Command *Cmd = cast<driver::Command>(*Jobs.begin());
404 if (llvm::StringRef(Cmd->getCreator().getName()) != "clang") {
405 Diags.Report(diag::err_fe_expected_clang_command);
406 return 0;
407 }
408
409 const driver::ArgStringList &CCArgs = Cmd->getArguments();
Daniel Dunbar6b03ece2010-01-30 21:47:16 +0000410 llvm::OwningPtr<CompilerInvocation> CI(new CompilerInvocation);
411 CompilerInvocation::CreateFromArgs(*CI, (const char**) CCArgs.data(),
Daniel Dunbar55a17b62009-12-02 03:23:45 +0000412 (const char**) CCArgs.data()+CCArgs.size(),
Daniel Dunbard6136772009-12-13 03:45:58 +0000413 Diags);
414
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000415 // Override any files that need remapping
416 for (unsigned I = 0; I != NumRemappedFiles; ++I)
Daniel Dunbar6b03ece2010-01-30 21:47:16 +0000417 CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
Daniel Dunbar19511922010-02-16 01:55:04 +0000418 RemappedFiles[I].second);
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000419
Daniel Dunbara5a166d2009-12-15 00:06:45 +0000420 // Override the resources path.
Daniel Dunbar6b03ece2010-01-30 21:47:16 +0000421 CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
Daniel Dunbar55a17b62009-12-02 03:23:45 +0000422
Daniel Dunbar19511922010-02-16 01:55:04 +0000423 CI->getFrontendOpts().DisableFree = true;
Douglas Gregor33cdd812010-02-18 18:08:43 +0000424 return LoadFromCompilerInvocation(CI.take(), Diags, OnlyLocalDecls,
425 CaptureDiagnostics);
Daniel Dunbar55a17b62009-12-02 03:23:45 +0000426}