blob: ef14df10345ecafec0bcc1f59d21931ad29e78ba [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 Gregor313e26c2010-02-18 23:35:40 +000039 : MainFileIsAST(_MainFileIsAST) {
Steve Naroff36c44642009-10-19 14:34:22 +000040}
Daniel Dunbar521bf9c2009-12-01 09:51:01 +000041ASTUnit::~ASTUnit() {
Douglas Gregor313e26c2010-02-18 23:35:40 +000042 for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I)
43 TemporaryFiles[I].eraseFromDisk();
Steve Naroffe19944c2009-10-15 22:23:48 +000044}
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000045
46namespace {
47
48/// \brief Gathers information from PCHReader that will be used to initialize
49/// a Preprocessor.
Benjamin Kramerbd218282009-11-28 10:07:24 +000050class PCHInfoCollector : public PCHReaderListener {
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000051 LangOptions &LangOpt;
52 HeaderSearch &HSI;
53 std::string &TargetTriple;
54 std::string &Predefines;
55 unsigned &Counter;
Mike Stump1eb44332009-09-09 15:08:12 +000056
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000057 unsigned NumHeaderInfos;
Mike Stump1eb44332009-09-09 15:08:12 +000058
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000059public:
60 PCHInfoCollector(LangOptions &LangOpt, HeaderSearch &HSI,
61 std::string &TargetTriple, std::string &Predefines,
62 unsigned &Counter)
63 : LangOpt(LangOpt), HSI(HSI), TargetTriple(TargetTriple),
64 Predefines(Predefines), Counter(Counter), NumHeaderInfos(0) {}
Mike Stump1eb44332009-09-09 15:08:12 +000065
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000066 virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
67 LangOpt = LangOpts;
68 return false;
69 }
Mike Stump1eb44332009-09-09 15:08:12 +000070
Daniel Dunbardc3c0d22009-11-11 00:52:11 +000071 virtual bool ReadTargetTriple(llvm::StringRef Triple) {
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000072 TargetTriple = Triple;
73 return false;
74 }
Mike Stump1eb44332009-09-09 15:08:12 +000075
Daniel Dunbardc3c0d22009-11-11 00:52:11 +000076 virtual bool ReadPredefinesBuffer(llvm::StringRef PCHPredef,
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000077 FileID PCHBufferID,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +000078 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000079 std::string &SuggestedPredefines) {
80 Predefines = PCHPredef;
81 return false;
82 }
Mike Stump1eb44332009-09-09 15:08:12 +000083
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000084 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI) {
85 HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
86 }
Mike Stump1eb44332009-09-09 15:08:12 +000087
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000088 virtual void ReadCounter(unsigned Value) {
89 Counter = Value;
90 }
91};
92
Douglas Gregora88084b2010-02-18 18:08:43 +000093class StoredDiagnosticClient : public DiagnosticClient {
94 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags;
95
96public:
97 explicit StoredDiagnosticClient(
98 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags)
99 : StoredDiags(StoredDiags) { }
100
101 virtual void HandleDiagnostic(Diagnostic::Level Level,
102 const DiagnosticInfo &Info);
103};
104
105/// \brief RAII object that optionally captures diagnostics, if
106/// there is no diagnostic client to capture them already.
107class CaptureDroppedDiagnostics {
108 Diagnostic &Diags;
109 StoredDiagnosticClient Client;
110 DiagnosticClient *PreviousClient;
111
112public:
113 CaptureDroppedDiagnostics(bool RequestCapture, Diagnostic &Diags,
114 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags)
115 : Diags(Diags), Client(StoredDiags), PreviousClient(Diags.getClient())
116 {
117 if (RequestCapture || Diags.getClient() == 0)
118 Diags.setClient(&Client);
119 }
120
121 ~CaptureDroppedDiagnostics() {
122 Diags.setClient(PreviousClient);
123 }
124};
125
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000126} // anonymous namespace
127
Douglas Gregora88084b2010-02-18 18:08:43 +0000128void StoredDiagnosticClient::HandleDiagnostic(Diagnostic::Level Level,
129 const DiagnosticInfo &Info) {
130 StoredDiags.push_back(StoredDiagnostic(Level, Info));
131}
132
Steve Naroff77accc12009-09-03 18:19:54 +0000133const std::string &ASTUnit::getOriginalSourceFileName() {
Daniel Dunbar68d40e22009-12-02 08:44:16 +0000134 return OriginalSourceFile;
Steve Naroff77accc12009-09-03 18:19:54 +0000135}
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000136
Steve Naroffe19944c2009-10-15 22:23:48 +0000137const std::string &ASTUnit::getPCHFileName() {
Daniel Dunbarc7822db2009-12-02 21:47:43 +0000138 assert(isMainFileAST() && "Not an ASTUnit from a PCH file!");
Benjamin Kramer7297c182010-01-30 16:23:25 +0000139 return static_cast<PCHReader *>(Ctx->getExternalSource())->getFileName();
Steve Naroffe19944c2009-10-15 22:23:48 +0000140}
141
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000142ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename,
Daniel Dunbar5262fda2009-12-03 01:45:44 +0000143 Diagnostic &Diags,
Ted Kremenek5cf48762009-10-17 00:34:24 +0000144 bool OnlyLocalDecls,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000145 RemappedFile *RemappedFiles,
Douglas Gregora88084b2010-02-18 18:08:43 +0000146 unsigned NumRemappedFiles,
147 bool CaptureDiagnostics) {
Daniel Dunbar5262fda2009-12-03 01:45:44 +0000148 llvm::OwningPtr<ASTUnit> AST(new ASTUnit(true));
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000149 AST->OnlyLocalDecls = OnlyLocalDecls;
Steve Naroff36c44642009-10-19 14:34:22 +0000150 AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager()));
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000151
Douglas Gregora88084b2010-02-18 18:08:43 +0000152 // If requested, capture diagnostics in the ASTUnit.
153 CaptureDroppedDiagnostics Capture(CaptureDiagnostics, Diags,
154 AST->Diagnostics);
155
Douglas Gregor4db64a42010-01-23 00:14:00 +0000156 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
157 // Create the file entry for the file that we're mapping from.
158 const FileEntry *FromFile
159 = AST->getFileManager().getVirtualFile(RemappedFiles[I].first,
160 RemappedFiles[I].second->getBufferSize(),
161 0);
162 if (!FromFile) {
163 Diags.Report(diag::err_fe_remap_missing_from_file)
164 << RemappedFiles[I].first;
Douglas Gregorc8dfe5e2010-02-27 01:32:48 +0000165 delete RemappedFiles[I].second;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000166 continue;
167 }
168
169 // Override the contents of the "from" file with the contents of
170 // the "to" file.
171 AST->getSourceManager().overrideFileContents(FromFile,
172 RemappedFiles[I].second);
173 }
174
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000175 // Gather Info for preprocessor construction later on.
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000177 LangOptions LangInfo;
178 HeaderSearch &HeaderInfo = *AST->HeaderInfo.get();
179 std::string TargetTriple;
180 std::string Predefines;
181 unsigned Counter;
182
Daniel Dunbarbce6f622009-09-03 05:59:50 +0000183 llvm::OwningPtr<PCHReader> Reader;
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000184 llvm::OwningPtr<ExternalASTSource> Source;
185
Ted Kremenekfc062212009-10-19 21:44:57 +0000186 Reader.reset(new PCHReader(AST->getSourceManager(), AST->getFileManager(),
Daniel Dunbar5262fda2009-12-03 01:45:44 +0000187 Diags));
Daniel Dunbarcc318932009-09-03 05:59:35 +0000188 Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple,
189 Predefines, Counter));
190
191 switch (Reader->ReadPCH(Filename)) {
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000192 case PCHReader::Success:
193 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000195 case PCHReader::Failure:
Argyrios Kyrtzidis106c9982009-06-25 18:22:30 +0000196 case PCHReader::IgnorePCH:
Daniel Dunbar5262fda2009-12-03 01:45:44 +0000197 Diags.Report(diag::err_fe_unable_to_load_pch);
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000198 return NULL;
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000199 }
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Daniel Dunbar68d40e22009-12-02 08:44:16 +0000201 AST->OriginalSourceFile = Reader->getOriginalSourceFile();
202
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000203 // PCH loaded successfully. Now create the preprocessor.
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000205 // Get information about the target being compiled for.
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000206 //
207 // FIXME: This is broken, we should store the TargetOptions in the PCH.
208 TargetOptions TargetOpts;
209 TargetOpts.ABI = "";
210 TargetOpts.CPU = "";
211 TargetOpts.Features.clear();
212 TargetOpts.Triple = TargetTriple;
Daniel Dunbar5262fda2009-12-03 01:45:44 +0000213 AST->Target.reset(TargetInfo::CreateTargetInfo(Diags, TargetOpts));
214 AST->PP.reset(new Preprocessor(Diags, LangInfo, *AST->Target.get(),
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000215 AST->getSourceManager(), HeaderInfo));
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000216 Preprocessor &PP = *AST->PP.get();
217
Daniel Dunbard5b61262009-09-21 03:03:47 +0000218 PP.setPredefines(Reader->getSuggestedPredefines());
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000219 PP.setCounterValue(Counter);
Daniel Dunbarcc318932009-09-03 05:59:35 +0000220 Reader->setPreprocessor(PP);
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000222 // Create and initialize the ASTContext.
223
224 AST->Ctx.reset(new ASTContext(LangInfo,
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000225 AST->getSourceManager(),
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000226 *AST->Target.get(),
227 PP.getIdentifierTable(),
228 PP.getSelectorTable(),
229 PP.getBuiltinInfo(),
Daniel Dunbarb26d4832010-02-16 01:55:04 +0000230 /* FreeMemory = */ false,
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000231 /* size_reserve = */0));
232 ASTContext &Context = *AST->Ctx.get();
Mike Stump1eb44332009-09-09 15:08:12 +0000233
Daniel Dunbarcc318932009-09-03 05:59:35 +0000234 Reader->InitializeContext(Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000236 // Attach the PCH reader to the AST context as an external AST
237 // source, so that declarations will be deserialized from the
238 // PCH file as needed.
Daniel Dunbarcc318932009-09-03 05:59:35 +0000239 Source.reset(Reader.take());
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000240 Context.setExternalSource(Source);
241
Mike Stump1eb44332009-09-09 15:08:12 +0000242 return AST.take();
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000243}
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000244
245namespace {
246
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000247class TopLevelDeclTrackerConsumer : public ASTConsumer {
248 ASTUnit &Unit;
249
250public:
251 TopLevelDeclTrackerConsumer(ASTUnit &_Unit) : Unit(_Unit) {}
252
253 void HandleTopLevelDecl(DeclGroupRef D) {
254 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it)
255 Unit.getTopLevelDecls().push_back(*it);
256 }
257};
258
259class TopLevelDeclTrackerAction : public ASTFrontendAction {
260public:
261 ASTUnit &Unit;
262
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000263 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
264 llvm::StringRef InFile) {
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000265 return new TopLevelDeclTrackerConsumer(Unit);
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000266 }
267
268public:
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000269 TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {}
270
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000271 virtual bool hasCodeCompletionSupport() const { return false; }
272};
273
274}
275
Daniel Dunbarf7acc372010-02-16 01:54:54 +0000276ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI,
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000277 Diagnostic &Diags,
Douglas Gregora88084b2010-02-18 18:08:43 +0000278 bool OnlyLocalDecls,
279 bool CaptureDiagnostics) {
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000280 // Create the compiler instance to use for building the AST.
Daniel Dunbarcb6dda12009-12-02 08:43:56 +0000281 CompilerInstance Clang;
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000282 llvm::OwningPtr<ASTUnit> AST;
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000283 llvm::OwningPtr<TopLevelDeclTrackerAction> Act;
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000284
Daniel Dunbarf7acc372010-02-16 01:54:54 +0000285 Clang.setInvocation(CI);
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000286
287 Clang.setDiagnostics(&Diags);
288 Clang.setDiagnosticClient(Diags.getClient());
289
290 // Create the target instance.
291 Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(),
292 Clang.getTargetOpts()));
Douglas Gregora88084b2010-02-18 18:08:43 +0000293 if (!Clang.hasTarget()) {
294 Clang.takeSourceManager();
295 Clang.takeFileManager();
296 Clang.takeDiagnosticClient();
297 Clang.takeDiagnostics();
298 return 0;
299 }
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000300
301 // Inform the target of the language options.
302 //
303 // FIXME: We shouldn't need to do this, the target should be immutable once
304 // created. This complexity should be lifted elsewhere.
305 Clang.getTarget().setForcedLangOptions(Clang.getLangOpts());
306
307 assert(Clang.getFrontendOpts().Inputs.size() == 1 &&
308 "Invocation must have exactly one source file!");
309 assert(Clang.getFrontendOpts().Inputs[0].first != FrontendOptions::IK_AST &&
310 "FIXME: AST inputs not yet supported here!");
311
312 // Create the AST unit.
Daniel Dunbarc7822db2009-12-02 21:47:43 +0000313 AST.reset(new ASTUnit(false));
Daniel Dunbar68ea2ac2009-12-02 21:47:32 +0000314 AST->OnlyLocalDecls = OnlyLocalDecls;
Daniel Dunbar68d40e22009-12-02 08:44:16 +0000315 AST->OriginalSourceFile = Clang.getFrontendOpts().Inputs[0].second;
316
Douglas Gregora88084b2010-02-18 18:08:43 +0000317 // Capture any diagnostics that would otherwise be dropped.
318 CaptureDroppedDiagnostics Capture(CaptureDiagnostics,
319 Clang.getDiagnostics(),
320 AST->Diagnostics);
321
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000322 // Create a file manager object to provide access to and cache the filesystem.
323 Clang.setFileManager(&AST->getFileManager());
324
325 // Create the source manager.
326 Clang.setSourceManager(&AST->getSourceManager());
327
328 // Create the preprocessor.
329 Clang.createPreprocessor();
330
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000331 Act.reset(new TopLevelDeclTrackerAction(*AST));
332 if (!Act->BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second,
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000333 /*IsAST=*/false))
334 goto error;
335
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000336 Act->Execute();
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000337
Daniel Dunbar64a32ba2009-12-01 21:57:33 +0000338 // Steal the created target, context, and preprocessor, and take back the
339 // source and file managers.
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000340 AST->Ctx.reset(Clang.takeASTContext());
341 AST->PP.reset(Clang.takePreprocessor());
342 Clang.takeSourceManager();
343 Clang.takeFileManager();
Daniel Dunbar64a32ba2009-12-01 21:57:33 +0000344 AST->Target.reset(Clang.takeTarget());
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000345
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000346 Act->EndSourceFile();
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000347
348 Clang.takeDiagnosticClient();
349 Clang.takeDiagnostics();
Daniel Dunbar807b0612010-01-30 21:47:16 +0000350 Clang.takeInvocation();
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000351
Daniel Dunbarf7acc372010-02-16 01:54:54 +0000352 AST->Invocation.reset(Clang.takeInvocation());
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000353 return AST.take();
354
355error:
356 Clang.takeSourceManager();
357 Clang.takeFileManager();
358 Clang.takeDiagnosticClient();
359 Clang.takeDiagnostics();
360 return 0;
361}
Daniel Dunbar7b556682009-12-02 03:23:45 +0000362
363ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin,
364 const char **ArgEnd,
365 Diagnostic &Diags,
Daniel Dunbar869824e2009-12-13 03:46:13 +0000366 llvm::StringRef ResourceFilesPath,
Daniel Dunbar7b556682009-12-02 03:23:45 +0000367 bool OnlyLocalDecls,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000368 RemappedFile *RemappedFiles,
Douglas Gregora88084b2010-02-18 18:08:43 +0000369 unsigned NumRemappedFiles,
370 bool CaptureDiagnostics) {
Daniel Dunbar7b556682009-12-02 03:23:45 +0000371 llvm::SmallVector<const char *, 16> Args;
372 Args.push_back("<clang>"); // FIXME: Remove dummy argument.
373 Args.insert(Args.end(), ArgBegin, ArgEnd);
374
375 // FIXME: Find a cleaner way to force the driver into restricted modes. We
376 // also want to force it to use clang.
377 Args.push_back("-fsyntax-only");
378
Daniel Dunbar869824e2009-12-13 03:46:13 +0000379 // FIXME: We shouldn't have to pass in the path info.
380 driver::Driver TheDriver("clang", "/", llvm::sys::getHostTriple(),
Daniel Dunbar7b556682009-12-02 03:23:45 +0000381 "a.out", false, Diags);
Daniel Dunbar3bd54cc2010-01-25 00:44:02 +0000382
383 // Don't check that inputs exist, they have been remapped.
384 TheDriver.setCheckInputsExist(false);
385
Daniel Dunbar7b556682009-12-02 03:23:45 +0000386 llvm::OwningPtr<driver::Compilation> C(
387 TheDriver.BuildCompilation(Args.size(), Args.data()));
388
389 // We expect to get back exactly one command job, if we didn't something
390 // failed.
391 const driver::JobList &Jobs = C->getJobs();
392 if (Jobs.size() != 1 || !isa<driver::Command>(Jobs.begin())) {
393 llvm::SmallString<256> Msg;
394 llvm::raw_svector_ostream OS(Msg);
395 C->PrintJob(OS, C->getJobs(), "; ", true);
396 Diags.Report(diag::err_fe_expected_compiler_job) << OS.str();
397 return 0;
398 }
399
400 const driver::Command *Cmd = cast<driver::Command>(*Jobs.begin());
401 if (llvm::StringRef(Cmd->getCreator().getName()) != "clang") {
402 Diags.Report(diag::err_fe_expected_clang_command);
403 return 0;
404 }
405
406 const driver::ArgStringList &CCArgs = Cmd->getArguments();
Daniel Dunbar807b0612010-01-30 21:47:16 +0000407 llvm::OwningPtr<CompilerInvocation> CI(new CompilerInvocation);
408 CompilerInvocation::CreateFromArgs(*CI, (const char**) CCArgs.data(),
Daniel Dunbar7b556682009-12-02 03:23:45 +0000409 (const char**) CCArgs.data()+CCArgs.size(),
Daniel Dunbar1e69fe32009-12-13 03:45:58 +0000410 Diags);
411
Douglas Gregor4db64a42010-01-23 00:14:00 +0000412 // Override any files that need remapping
413 for (unsigned I = 0; I != NumRemappedFiles; ++I)
Daniel Dunbar807b0612010-01-30 21:47:16 +0000414 CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
Daniel Dunbarb26d4832010-02-16 01:55:04 +0000415 RemappedFiles[I].second);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000416
Daniel Dunbar8b9adfe2009-12-15 00:06:45 +0000417 // Override the resources path.
Daniel Dunbar807b0612010-01-30 21:47:16 +0000418 CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
Daniel Dunbar7b556682009-12-02 03:23:45 +0000419
Daniel Dunbarb26d4832010-02-16 01:55:04 +0000420 CI->getFrontendOpts().DisableFree = true;
Douglas Gregora88084b2010-02-18 18:08:43 +0000421 return LoadFromCompilerInvocation(CI.take(), Diags, OnlyLocalDecls,
422 CaptureDiagnostics);
Daniel Dunbar7b556682009-12-02 03:23:45 +0000423}