blob: 8874622a24daaf36698b29c3c0c7cc1bd1e54674 [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;
165 continue;
166 }
167
168 // Override the contents of the "from" file with the contents of
169 // the "to" file.
170 AST->getSourceManager().overrideFileContents(FromFile,
171 RemappedFiles[I].second);
172 }
173
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000174 // Gather Info for preprocessor construction later on.
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000176 LangOptions LangInfo;
177 HeaderSearch &HeaderInfo = *AST->HeaderInfo.get();
178 std::string TargetTriple;
179 std::string Predefines;
180 unsigned Counter;
181
Daniel Dunbarbce6f622009-09-03 05:59:50 +0000182 llvm::OwningPtr<PCHReader> Reader;
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000183 llvm::OwningPtr<ExternalASTSource> Source;
184
Ted Kremenekfc062212009-10-19 21:44:57 +0000185 Reader.reset(new PCHReader(AST->getSourceManager(), AST->getFileManager(),
Daniel Dunbar5262fda2009-12-03 01:45:44 +0000186 Diags));
Daniel Dunbarcc318932009-09-03 05:59:35 +0000187 Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple,
188 Predefines, Counter));
189
190 switch (Reader->ReadPCH(Filename)) {
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000191 case PCHReader::Success:
192 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000194 case PCHReader::Failure:
Argyrios Kyrtzidis106c9982009-06-25 18:22:30 +0000195 case PCHReader::IgnorePCH:
Daniel Dunbar5262fda2009-12-03 01:45:44 +0000196 Diags.Report(diag::err_fe_unable_to_load_pch);
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000197 return NULL;
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000198 }
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Daniel Dunbar68d40e22009-12-02 08:44:16 +0000200 AST->OriginalSourceFile = Reader->getOriginalSourceFile();
201
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000202 // PCH loaded successfully. Now create the preprocessor.
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000204 // Get information about the target being compiled for.
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000205 //
206 // FIXME: This is broken, we should store the TargetOptions in the PCH.
207 TargetOptions TargetOpts;
208 TargetOpts.ABI = "";
209 TargetOpts.CPU = "";
210 TargetOpts.Features.clear();
211 TargetOpts.Triple = TargetTriple;
Daniel Dunbar5262fda2009-12-03 01:45:44 +0000212 AST->Target.reset(TargetInfo::CreateTargetInfo(Diags, TargetOpts));
213 AST->PP.reset(new Preprocessor(Diags, LangInfo, *AST->Target.get(),
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000214 AST->getSourceManager(), HeaderInfo));
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000215 Preprocessor &PP = *AST->PP.get();
216
Daniel Dunbard5b61262009-09-21 03:03:47 +0000217 PP.setPredefines(Reader->getSuggestedPredefines());
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000218 PP.setCounterValue(Counter);
Daniel Dunbarcc318932009-09-03 05:59:35 +0000219 Reader->setPreprocessor(PP);
Mike Stump1eb44332009-09-09 15:08:12 +0000220
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000221 // Create and initialize the ASTContext.
222
223 AST->Ctx.reset(new ASTContext(LangInfo,
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000224 AST->getSourceManager(),
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000225 *AST->Target.get(),
226 PP.getIdentifierTable(),
227 PP.getSelectorTable(),
228 PP.getBuiltinInfo(),
Daniel Dunbarb26d4832010-02-16 01:55:04 +0000229 /* FreeMemory = */ false,
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000230 /* size_reserve = */0));
231 ASTContext &Context = *AST->Ctx.get();
Mike Stump1eb44332009-09-09 15:08:12 +0000232
Daniel Dunbarcc318932009-09-03 05:59:35 +0000233 Reader->InitializeContext(Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000235 // Attach the PCH reader to the AST context as an external AST
236 // source, so that declarations will be deserialized from the
237 // PCH file as needed.
Daniel Dunbarcc318932009-09-03 05:59:35 +0000238 Source.reset(Reader.take());
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000239 Context.setExternalSource(Source);
240
Mike Stump1eb44332009-09-09 15:08:12 +0000241 return AST.take();
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000242}
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000243
244namespace {
245
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000246class TopLevelDeclTrackerConsumer : public ASTConsumer {
247 ASTUnit &Unit;
248
249public:
250 TopLevelDeclTrackerConsumer(ASTUnit &_Unit) : Unit(_Unit) {}
251
252 void HandleTopLevelDecl(DeclGroupRef D) {
253 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it)
254 Unit.getTopLevelDecls().push_back(*it);
255 }
256};
257
258class TopLevelDeclTrackerAction : public ASTFrontendAction {
259public:
260 ASTUnit &Unit;
261
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000262 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
263 llvm::StringRef InFile) {
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000264 return new TopLevelDeclTrackerConsumer(Unit);
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000265 }
266
267public:
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000268 TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {}
269
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000270 virtual bool hasCodeCompletionSupport() const { return false; }
271};
272
273}
274
Daniel Dunbarf7acc372010-02-16 01:54:54 +0000275ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI,
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000276 Diagnostic &Diags,
Douglas Gregora88084b2010-02-18 18:08:43 +0000277 bool OnlyLocalDecls,
278 bool CaptureDiagnostics) {
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000279 // Create the compiler instance to use for building the AST.
Daniel Dunbarcb6dda12009-12-02 08:43:56 +0000280 CompilerInstance Clang;
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000281 llvm::OwningPtr<ASTUnit> AST;
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000282 llvm::OwningPtr<TopLevelDeclTrackerAction> Act;
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000283
Daniel Dunbarf7acc372010-02-16 01:54:54 +0000284 Clang.setInvocation(CI);
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000285
286 Clang.setDiagnostics(&Diags);
287 Clang.setDiagnosticClient(Diags.getClient());
288
289 // Create the target instance.
290 Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(),
291 Clang.getTargetOpts()));
Douglas Gregora88084b2010-02-18 18:08:43 +0000292 if (!Clang.hasTarget()) {
293 Clang.takeSourceManager();
294 Clang.takeFileManager();
295 Clang.takeDiagnosticClient();
296 Clang.takeDiagnostics();
297 return 0;
298 }
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000299
300 // Inform the target of the language options.
301 //
302 // FIXME: We shouldn't need to do this, the target should be immutable once
303 // created. This complexity should be lifted elsewhere.
304 Clang.getTarget().setForcedLangOptions(Clang.getLangOpts());
305
306 assert(Clang.getFrontendOpts().Inputs.size() == 1 &&
307 "Invocation must have exactly one source file!");
308 assert(Clang.getFrontendOpts().Inputs[0].first != FrontendOptions::IK_AST &&
309 "FIXME: AST inputs not yet supported here!");
310
311 // Create the AST unit.
Daniel Dunbarc7822db2009-12-02 21:47:43 +0000312 AST.reset(new ASTUnit(false));
Daniel Dunbar68ea2ac2009-12-02 21:47:32 +0000313 AST->OnlyLocalDecls = OnlyLocalDecls;
Daniel Dunbar68d40e22009-12-02 08:44:16 +0000314 AST->OriginalSourceFile = Clang.getFrontendOpts().Inputs[0].second;
315
Douglas Gregora88084b2010-02-18 18:08:43 +0000316 // Capture any diagnostics that would otherwise be dropped.
317 CaptureDroppedDiagnostics Capture(CaptureDiagnostics,
318 Clang.getDiagnostics(),
319 AST->Diagnostics);
320
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000321 // Create a file manager object to provide access to and cache the filesystem.
322 Clang.setFileManager(&AST->getFileManager());
323
324 // Create the source manager.
325 Clang.setSourceManager(&AST->getSourceManager());
326
327 // Create the preprocessor.
328 Clang.createPreprocessor();
329
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000330 Act.reset(new TopLevelDeclTrackerAction(*AST));
331 if (!Act->BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second,
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000332 /*IsAST=*/false))
333 goto error;
334
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000335 Act->Execute();
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000336
Daniel Dunbar64a32ba2009-12-01 21:57:33 +0000337 // Steal the created target, context, and preprocessor, and take back the
338 // source and file managers.
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000339 AST->Ctx.reset(Clang.takeASTContext());
340 AST->PP.reset(Clang.takePreprocessor());
341 Clang.takeSourceManager();
342 Clang.takeFileManager();
Daniel Dunbar64a32ba2009-12-01 21:57:33 +0000343 AST->Target.reset(Clang.takeTarget());
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000344
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000345 Act->EndSourceFile();
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000346
347 Clang.takeDiagnosticClient();
348 Clang.takeDiagnostics();
Daniel Dunbar807b0612010-01-30 21:47:16 +0000349 Clang.takeInvocation();
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000350
Daniel Dunbarf7acc372010-02-16 01:54:54 +0000351 AST->Invocation.reset(Clang.takeInvocation());
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000352 return AST.take();
353
354error:
355 Clang.takeSourceManager();
356 Clang.takeFileManager();
357 Clang.takeDiagnosticClient();
358 Clang.takeDiagnostics();
359 return 0;
360}
Daniel Dunbar7b556682009-12-02 03:23:45 +0000361
362ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin,
363 const char **ArgEnd,
364 Diagnostic &Diags,
Daniel Dunbar869824e2009-12-13 03:46:13 +0000365 llvm::StringRef ResourceFilesPath,
Daniel Dunbar7b556682009-12-02 03:23:45 +0000366 bool OnlyLocalDecls,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000367 RemappedFile *RemappedFiles,
Douglas Gregora88084b2010-02-18 18:08:43 +0000368 unsigned NumRemappedFiles,
369 bool CaptureDiagnostics) {
Daniel Dunbar7b556682009-12-02 03:23:45 +0000370 llvm::SmallVector<const char *, 16> Args;
371 Args.push_back("<clang>"); // FIXME: Remove dummy argument.
372 Args.insert(Args.end(), ArgBegin, ArgEnd);
373
374 // FIXME: Find a cleaner way to force the driver into restricted modes. We
375 // also want to force it to use clang.
376 Args.push_back("-fsyntax-only");
377
Daniel Dunbar869824e2009-12-13 03:46:13 +0000378 // FIXME: We shouldn't have to pass in the path info.
379 driver::Driver TheDriver("clang", "/", llvm::sys::getHostTriple(),
Daniel Dunbar7b556682009-12-02 03:23:45 +0000380 "a.out", false, Diags);
Daniel Dunbar3bd54cc2010-01-25 00:44:02 +0000381
382 // Don't check that inputs exist, they have been remapped.
383 TheDriver.setCheckInputsExist(false);
384
Daniel Dunbar7b556682009-12-02 03:23:45 +0000385 llvm::OwningPtr<driver::Compilation> C(
386 TheDriver.BuildCompilation(Args.size(), Args.data()));
387
388 // We expect to get back exactly one command job, if we didn't something
389 // failed.
390 const driver::JobList &Jobs = C->getJobs();
391 if (Jobs.size() != 1 || !isa<driver::Command>(Jobs.begin())) {
392 llvm::SmallString<256> Msg;
393 llvm::raw_svector_ostream OS(Msg);
394 C->PrintJob(OS, C->getJobs(), "; ", true);
395 Diags.Report(diag::err_fe_expected_compiler_job) << OS.str();
396 return 0;
397 }
398
399 const driver::Command *Cmd = cast<driver::Command>(*Jobs.begin());
400 if (llvm::StringRef(Cmd->getCreator().getName()) != "clang") {
401 Diags.Report(diag::err_fe_expected_clang_command);
402 return 0;
403 }
404
405 const driver::ArgStringList &CCArgs = Cmd->getArguments();
Daniel Dunbar807b0612010-01-30 21:47:16 +0000406 llvm::OwningPtr<CompilerInvocation> CI(new CompilerInvocation);
407 CompilerInvocation::CreateFromArgs(*CI, (const char**) CCArgs.data(),
Daniel Dunbar7b556682009-12-02 03:23:45 +0000408 (const char**) CCArgs.data()+CCArgs.size(),
Daniel Dunbar1e69fe32009-12-13 03:45:58 +0000409 Diags);
410
Douglas Gregor4db64a42010-01-23 00:14:00 +0000411 // Override any files that need remapping
412 for (unsigned I = 0; I != NumRemappedFiles; ++I)
Daniel Dunbar807b0612010-01-30 21:47:16 +0000413 CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
Daniel Dunbarb26d4832010-02-16 01:55:04 +0000414 RemappedFiles[I].second);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000415
Daniel Dunbar8b9adfe2009-12-15 00:06:45 +0000416 // Override the resources path.
Daniel Dunbar807b0612010-01-30 21:47:16 +0000417 CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
Daniel Dunbar7b556682009-12-02 03:23:45 +0000418
Daniel Dunbarb26d4832010-02-16 01:55:04 +0000419 CI->getFrontendOpts().DisableFree = true;
Douglas Gregora88084b2010-02-18 18:08:43 +0000420 return LoadFromCompilerInvocation(CI.take(), Diags, OnlyLocalDecls,
421 CaptureDiagnostics);
Daniel Dunbar7b556682009-12-02 03:23:45 +0000422}