blob: 7243f709cc65ed2b2fc290bef912b3f21590e6bf [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
Douglas Gregore0fbb832010-03-16 00:06:06 +000038ASTUnit::ASTUnit(Diagnostic &Diag, bool _MainFileIsAST)
39 : SourceMgr(Diag), MainFileIsAST(_MainFileIsAST),
40 ConcurrencyCheckValue(CheckUnlocked) {
Steve Naroff505fb842009-10-19 14:34:22 +000041}
Daniel Dunbar764c0822009-12-01 09:51:01 +000042ASTUnit::~ASTUnit() {
Douglas Gregor0c7c2f82010-03-05 21:16:25 +000043 ConcurrencyCheckValue = CheckLocked;
Douglas Gregor6cb5ba42010-02-18 23:35:40 +000044 for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I)
45 TemporaryFiles[I].eraseFromDisk();
Steve Naroff44cd60e2009-10-15 22:23:48 +000046}
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000047
48namespace {
49
50/// \brief Gathers information from PCHReader that will be used to initialize
51/// a Preprocessor.
Benjamin Kramer16634c22009-11-28 10:07:24 +000052class PCHInfoCollector : public PCHReaderListener {
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000053 LangOptions &LangOpt;
54 HeaderSearch &HSI;
55 std::string &TargetTriple;
56 std::string &Predefines;
57 unsigned &Counter;
Mike Stump11289f42009-09-09 15:08:12 +000058
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000059 unsigned NumHeaderInfos;
Mike Stump11289f42009-09-09 15:08:12 +000060
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000061public:
62 PCHInfoCollector(LangOptions &LangOpt, HeaderSearch &HSI,
63 std::string &TargetTriple, std::string &Predefines,
64 unsigned &Counter)
65 : LangOpt(LangOpt), HSI(HSI), TargetTriple(TargetTriple),
66 Predefines(Predefines), Counter(Counter), NumHeaderInfos(0) {}
Mike Stump11289f42009-09-09 15:08:12 +000067
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000068 virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
69 LangOpt = LangOpts;
70 return false;
71 }
Mike Stump11289f42009-09-09 15:08:12 +000072
Daniel Dunbar20a682d2009-11-11 00:52:11 +000073 virtual bool ReadTargetTriple(llvm::StringRef Triple) {
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000074 TargetTriple = Triple;
75 return false;
76 }
Mike Stump11289f42009-09-09 15:08:12 +000077
Daniel Dunbar20a682d2009-11-11 00:52:11 +000078 virtual bool ReadPredefinesBuffer(llvm::StringRef PCHPredef,
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000079 FileID PCHBufferID,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +000080 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000081 std::string &SuggestedPredefines) {
82 Predefines = PCHPredef;
83 return false;
84 }
Mike Stump11289f42009-09-09 15:08:12 +000085
Douglas Gregora2f49452010-03-16 19:09:18 +000086 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID) {
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000087 HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
88 }
Mike Stump11289f42009-09-09 15:08:12 +000089
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000090 virtual void ReadCounter(unsigned Value) {
91 Counter = Value;
92 }
93};
94
Douglas Gregor33cdd812010-02-18 18:08:43 +000095class StoredDiagnosticClient : public DiagnosticClient {
96 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags;
97
98public:
99 explicit StoredDiagnosticClient(
100 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags)
101 : StoredDiags(StoredDiags) { }
102
103 virtual void HandleDiagnostic(Diagnostic::Level Level,
104 const DiagnosticInfo &Info);
105};
106
107/// \brief RAII object that optionally captures diagnostics, if
108/// there is no diagnostic client to capture them already.
109class CaptureDroppedDiagnostics {
110 Diagnostic &Diags;
111 StoredDiagnosticClient Client;
112 DiagnosticClient *PreviousClient;
113
114public:
115 CaptureDroppedDiagnostics(bool RequestCapture, Diagnostic &Diags,
116 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags)
117 : Diags(Diags), Client(StoredDiags), PreviousClient(Diags.getClient())
118 {
119 if (RequestCapture || Diags.getClient() == 0)
120 Diags.setClient(&Client);
121 }
122
123 ~CaptureDroppedDiagnostics() {
124 Diags.setClient(PreviousClient);
125 }
126};
127
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000128} // anonymous namespace
129
Douglas Gregor33cdd812010-02-18 18:08:43 +0000130void StoredDiagnosticClient::HandleDiagnostic(Diagnostic::Level Level,
131 const DiagnosticInfo &Info) {
132 StoredDiags.push_back(StoredDiagnostic(Level, Info));
133}
134
Steve Naroffc0683b92009-09-03 18:19:54 +0000135const std::string &ASTUnit::getOriginalSourceFileName() {
Daniel Dunbara8a50932009-12-02 08:44:16 +0000136 return OriginalSourceFile;
Steve Naroffc0683b92009-09-03 18:19:54 +0000137}
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000138
Steve Naroff44cd60e2009-10-15 22:23:48 +0000139const std::string &ASTUnit::getPCHFileName() {
Daniel Dunbara18f9582009-12-02 21:47:43 +0000140 assert(isMainFileAST() && "Not an ASTUnit from a PCH file!");
Benjamin Kramer2ecf8eb2010-01-30 16:23:25 +0000141 return static_cast<PCHReader *>(Ctx->getExternalSource())->getFileName();
Steve Naroff44cd60e2009-10-15 22:23:48 +0000142}
143
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000144ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename,
Daniel Dunbar59203002009-12-03 01:45:44 +0000145 Diagnostic &Diags,
Ted Kremenek8bcb1c62009-10-17 00:34:24 +0000146 bool OnlyLocalDecls,
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000147 RemappedFile *RemappedFiles,
Douglas Gregor33cdd812010-02-18 18:08:43 +0000148 unsigned NumRemappedFiles,
149 bool CaptureDiagnostics) {
Douglas Gregore0fbb832010-03-16 00:06:06 +0000150 llvm::OwningPtr<ASTUnit> AST(new ASTUnit(Diags, true));
Douglas Gregor16bef852009-10-16 20:01:17 +0000151 AST->OnlyLocalDecls = OnlyLocalDecls;
Steve Naroff505fb842009-10-19 14:34:22 +0000152 AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager()));
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000153
Douglas Gregor33cdd812010-02-18 18:08:43 +0000154 // If requested, capture diagnostics in the ASTUnit.
155 CaptureDroppedDiagnostics Capture(CaptureDiagnostics, Diags,
156 AST->Diagnostics);
157
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000158 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
159 // Create the file entry for the file that we're mapping from.
160 const FileEntry *FromFile
161 = AST->getFileManager().getVirtualFile(RemappedFiles[I].first,
162 RemappedFiles[I].second->getBufferSize(),
163 0);
164 if (!FromFile) {
165 Diags.Report(diag::err_fe_remap_missing_from_file)
166 << RemappedFiles[I].first;
Douglas Gregor89a56c52010-02-27 01:32:48 +0000167 delete RemappedFiles[I].second;
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000168 continue;
169 }
170
171 // Override the contents of the "from" file with the contents of
172 // the "to" file.
173 AST->getSourceManager().overrideFileContents(FromFile,
174 RemappedFiles[I].second);
175 }
176
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000177 // Gather Info for preprocessor construction later on.
Mike Stump11289f42009-09-09 15:08:12 +0000178
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000179 LangOptions LangInfo;
180 HeaderSearch &HeaderInfo = *AST->HeaderInfo.get();
181 std::string TargetTriple;
182 std::string Predefines;
183 unsigned Counter;
184
Daniel Dunbar3a0637b2009-09-03 05:59:50 +0000185 llvm::OwningPtr<PCHReader> Reader;
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000186 llvm::OwningPtr<ExternalASTSource> Source;
187
Ted Kremenek428c6372009-10-19 21:44:57 +0000188 Reader.reset(new PCHReader(AST->getSourceManager(), AST->getFileManager(),
Daniel Dunbar59203002009-12-03 01:45:44 +0000189 Diags));
Daniel Dunbar2d9c7402009-09-03 05:59:35 +0000190 Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple,
191 Predefines, Counter));
192
193 switch (Reader->ReadPCH(Filename)) {
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000194 case PCHReader::Success:
195 break;
Mike Stump11289f42009-09-09 15:08:12 +0000196
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000197 case PCHReader::Failure:
Argyrios Kyrtzidis55c34112009-06-25 18:22:30 +0000198 case PCHReader::IgnorePCH:
Daniel Dunbar59203002009-12-03 01:45:44 +0000199 Diags.Report(diag::err_fe_unable_to_load_pch);
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000200 return NULL;
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000201 }
Mike Stump11289f42009-09-09 15:08:12 +0000202
Daniel Dunbara8a50932009-12-02 08:44:16 +0000203 AST->OriginalSourceFile = Reader->getOriginalSourceFile();
204
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000205 // PCH loaded successfully. Now create the preprocessor.
Mike Stump11289f42009-09-09 15:08:12 +0000206
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000207 // Get information about the target being compiled for.
Daniel Dunbarb9bbd542009-11-15 06:48:46 +0000208 //
209 // FIXME: This is broken, we should store the TargetOptions in the PCH.
210 TargetOptions TargetOpts;
211 TargetOpts.ABI = "";
212 TargetOpts.CPU = "";
213 TargetOpts.Features.clear();
214 TargetOpts.Triple = TargetTriple;
Daniel Dunbar59203002009-12-03 01:45:44 +0000215 AST->Target.reset(TargetInfo::CreateTargetInfo(Diags, TargetOpts));
216 AST->PP.reset(new Preprocessor(Diags, LangInfo, *AST->Target.get(),
Daniel Dunbar7cd285f2009-09-21 03:03:39 +0000217 AST->getSourceManager(), HeaderInfo));
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000218 Preprocessor &PP = *AST->PP.get();
219
Daniel Dunbarb7bbfdd2009-09-21 03:03:47 +0000220 PP.setPredefines(Reader->getSuggestedPredefines());
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000221 PP.setCounterValue(Counter);
Daniel Dunbar2d9c7402009-09-03 05:59:35 +0000222 Reader->setPreprocessor(PP);
Mike Stump11289f42009-09-09 15:08:12 +0000223
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000224 // Create and initialize the ASTContext.
225
226 AST->Ctx.reset(new ASTContext(LangInfo,
Daniel Dunbar7cd285f2009-09-21 03:03:39 +0000227 AST->getSourceManager(),
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000228 *AST->Target.get(),
229 PP.getIdentifierTable(),
230 PP.getSelectorTable(),
231 PP.getBuiltinInfo(),
Daniel Dunbar19511922010-02-16 01:55:04 +0000232 /* FreeMemory = */ false,
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000233 /* size_reserve = */0));
234 ASTContext &Context = *AST->Ctx.get();
Mike Stump11289f42009-09-09 15:08:12 +0000235
Daniel Dunbar2d9c7402009-09-03 05:59:35 +0000236 Reader->InitializeContext(Context);
Mike Stump11289f42009-09-09 15:08:12 +0000237
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000238 // Attach the PCH reader to the AST context as an external AST
239 // source, so that declarations will be deserialized from the
240 // PCH file as needed.
Daniel Dunbar2d9c7402009-09-03 05:59:35 +0000241 Source.reset(Reader.take());
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000242 Context.setExternalSource(Source);
243
Mike Stump11289f42009-09-09 15:08:12 +0000244 return AST.take();
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000245}
Daniel Dunbar764c0822009-12-01 09:51:01 +0000246
247namespace {
248
Daniel Dunbar644dca02009-12-04 08:17:33 +0000249class TopLevelDeclTrackerConsumer : public ASTConsumer {
250 ASTUnit &Unit;
251
252public:
253 TopLevelDeclTrackerConsumer(ASTUnit &_Unit) : Unit(_Unit) {}
254
255 void HandleTopLevelDecl(DeclGroupRef D) {
256 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it)
257 Unit.getTopLevelDecls().push_back(*it);
258 }
259};
260
261class TopLevelDeclTrackerAction : public ASTFrontendAction {
262public:
263 ASTUnit &Unit;
264
Daniel Dunbar764c0822009-12-01 09:51:01 +0000265 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
266 llvm::StringRef InFile) {
Daniel Dunbar644dca02009-12-04 08:17:33 +0000267 return new TopLevelDeclTrackerConsumer(Unit);
Daniel Dunbar764c0822009-12-01 09:51:01 +0000268 }
269
270public:
Daniel Dunbar644dca02009-12-04 08:17:33 +0000271 TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {}
272
Daniel Dunbar764c0822009-12-01 09:51:01 +0000273 virtual bool hasCodeCompletionSupport() const { return false; }
274};
275
276}
277
Daniel Dunbar7f21a7f2010-02-16 01:54:54 +0000278ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI,
Daniel Dunbar764c0822009-12-01 09:51:01 +0000279 Diagnostic &Diags,
Douglas Gregor33cdd812010-02-18 18:08:43 +0000280 bool OnlyLocalDecls,
Douglas Gregor7f6d60d2010-03-19 16:15:56 +0000281 bool CaptureDiagnostics) {
Daniel Dunbar764c0822009-12-01 09:51:01 +0000282 // Create the compiler instance to use for building the AST.
Daniel Dunbar7afbb8c2009-12-02 08:43:56 +0000283 CompilerInstance Clang;
Daniel Dunbar764c0822009-12-01 09:51:01 +0000284 llvm::OwningPtr<ASTUnit> AST;
Daniel Dunbar644dca02009-12-04 08:17:33 +0000285 llvm::OwningPtr<TopLevelDeclTrackerAction> Act;
Daniel Dunbar764c0822009-12-01 09:51:01 +0000286
Daniel Dunbar7f21a7f2010-02-16 01:54:54 +0000287 Clang.setInvocation(CI);
Daniel Dunbar764c0822009-12-01 09:51:01 +0000288
289 Clang.setDiagnostics(&Diags);
290 Clang.setDiagnosticClient(Diags.getClient());
291
292 // Create the target instance.
293 Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(),
294 Clang.getTargetOpts()));
Douglas Gregor33cdd812010-02-18 18:08:43 +0000295 if (!Clang.hasTarget()) {
Douglas Gregor33cdd812010-02-18 18:08:43 +0000296 Clang.takeDiagnosticClient();
297 Clang.takeDiagnostics();
298 return 0;
299 }
Daniel Dunbar764c0822009-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.
Douglas Gregore0fbb832010-03-16 00:06:06 +0000313 AST.reset(new ASTUnit(Diags, false));
Daniel Dunbar48973492009-12-02 21:47:32 +0000314 AST->OnlyLocalDecls = OnlyLocalDecls;
Daniel Dunbara8a50932009-12-02 08:44:16 +0000315 AST->OriginalSourceFile = Clang.getFrontendOpts().Inputs[0].second;
316
Douglas Gregor33cdd812010-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 Dunbar764c0822009-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 Dunbar644dca02009-12-04 08:17:33 +0000331 Act.reset(new TopLevelDeclTrackerAction(*AST));
332 if (!Act->BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second,
Daniel Dunbar764c0822009-12-01 09:51:01 +0000333 /*IsAST=*/false))
334 goto error;
335
Daniel Dunbar644dca02009-12-04 08:17:33 +0000336 Act->Execute();
Daniel Dunbar764c0822009-12-01 09:51:01 +0000337
Daniel Dunbard2f8be32009-12-01 21:57:33 +0000338 // Steal the created target, context, and preprocessor, and take back the
339 // source and file managers.
Daniel Dunbar764c0822009-12-01 09:51:01 +0000340 AST->Ctx.reset(Clang.takeASTContext());
341 AST->PP.reset(Clang.takePreprocessor());
342 Clang.takeSourceManager();
343 Clang.takeFileManager();
Daniel Dunbard2f8be32009-12-01 21:57:33 +0000344 AST->Target.reset(Clang.takeTarget());
Daniel Dunbar764c0822009-12-01 09:51:01 +0000345
Daniel Dunbar644dca02009-12-04 08:17:33 +0000346 Act->EndSourceFile();
Daniel Dunbar764c0822009-12-01 09:51:01 +0000347
348 Clang.takeDiagnosticClient();
349 Clang.takeDiagnostics();
Daniel Dunbar6b03ece2010-01-30 21:47:16 +0000350 Clang.takeInvocation();
Daniel Dunbar764c0822009-12-01 09:51:01 +0000351
Daniel Dunbar7f21a7f2010-02-16 01:54:54 +0000352 AST->Invocation.reset(Clang.takeInvocation());
Daniel Dunbar764c0822009-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 Dunbar55a17b62009-12-02 03:23:45 +0000362
363ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin,
364 const char **ArgEnd,
365 Diagnostic &Diags,
Daniel Dunbar8d4a2022009-12-13 03:46:13 +0000366 llvm::StringRef ResourceFilesPath,
Daniel Dunbar55a17b62009-12-02 03:23:45 +0000367 bool OnlyLocalDecls,
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000368 RemappedFile *RemappedFiles,
Douglas Gregor33cdd812010-02-18 18:08:43 +0000369 unsigned NumRemappedFiles,
Douglas Gregor7f6d60d2010-03-19 16:15:56 +0000370 bool CaptureDiagnostics) {
Daniel Dunbar55a17b62009-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 Dunbar8d4a2022009-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 Dunbare43887b2010-04-01 18:21:41 +0000381 "a.out", false, false, Diags);
Daniel Dunbarfcf2d422010-01-25 00:44:02 +0000382
383 // Don't check that inputs exist, they have been remapped.
384 TheDriver.setCheckInputsExist(false);
385
Daniel Dunbar55a17b62009-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 Dunbar6b03ece2010-01-30 21:47:16 +0000407 llvm::OwningPtr<CompilerInvocation> CI(new CompilerInvocation);
408 CompilerInvocation::CreateFromArgs(*CI, (const char**) CCArgs.data(),
Daniel Dunbar55a17b62009-12-02 03:23:45 +0000409 (const char**) CCArgs.data()+CCArgs.size(),
Daniel Dunbard6136772009-12-13 03:45:58 +0000410 Diags);
411
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000412 // Override any files that need remapping
413 for (unsigned I = 0; I != NumRemappedFiles; ++I)
Daniel Dunbar6b03ece2010-01-30 21:47:16 +0000414 CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
Daniel Dunbar19511922010-02-16 01:55:04 +0000415 RemappedFiles[I].second);
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000416
Daniel Dunbara5a166d2009-12-15 00:06:45 +0000417 // Override the resources path.
Daniel Dunbar6b03ece2010-01-30 21:47:16 +0000418 CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
Daniel Dunbar55a17b62009-12-02 03:23:45 +0000419
Daniel Dunbar19511922010-02-16 01:55:04 +0000420 CI->getFrontendOpts().DisableFree = true;
Douglas Gregor33cdd812010-02-18 18:08:43 +0000421 return LoadFromCompilerInvocation(CI.take(), Diags, OnlyLocalDecls,
Douglas Gregor7f6d60d2010-03-19 16:15:56 +0000422 CaptureDiagnostics);
Daniel Dunbar55a17b62009-12-02 03:23:45 +0000423}