blob: 2a76d6f37bc9ef1109121d3ecad8ccc45edfd430 [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 Gregord03e8232010-04-05 21:10:19 +000038ASTUnit::ASTUnit(bool _MainFileIsAST)
39 : MainFileIsAST(_MainFileIsAST), ConcurrencyCheckValue(CheckUnlocked) { }
40
Daniel Dunbar764c0822009-12-01 09:51:01 +000041ASTUnit::~ASTUnit() {
Douglas Gregor0c7c2f82010-03-05 21:16:25 +000042 ConcurrencyCheckValue = CheckLocked;
Douglas Gregor6cb5ba42010-02-18 23:35:40 +000043 for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I)
44 TemporaryFiles[I].eraseFromDisk();
Steve Naroff44cd60e2009-10-15 22:23:48 +000045}
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000046
47namespace {
48
49/// \brief Gathers information from PCHReader that will be used to initialize
50/// a Preprocessor.
Benjamin Kramer16634c22009-11-28 10:07:24 +000051class PCHInfoCollector : public PCHReaderListener {
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000052 LangOptions &LangOpt;
53 HeaderSearch &HSI;
54 std::string &TargetTriple;
55 std::string &Predefines;
56 unsigned &Counter;
Mike Stump11289f42009-09-09 15:08:12 +000057
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000058 unsigned NumHeaderInfos;
Mike Stump11289f42009-09-09 15:08:12 +000059
Argyrios Kyrtzidisce379752009-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 Stump11289f42009-09-09 15:08:12 +000066
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000067 virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
68 LangOpt = LangOpts;
69 return false;
70 }
Mike Stump11289f42009-09-09 15:08:12 +000071
Daniel Dunbar20a682d2009-11-11 00:52:11 +000072 virtual bool ReadTargetTriple(llvm::StringRef Triple) {
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000073 TargetTriple = Triple;
74 return false;
75 }
Mike Stump11289f42009-09-09 15:08:12 +000076
Daniel Dunbar20a682d2009-11-11 00:52:11 +000077 virtual bool ReadPredefinesBuffer(llvm::StringRef PCHPredef,
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000078 FileID PCHBufferID,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +000079 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000080 std::string &SuggestedPredefines) {
81 Predefines = PCHPredef;
82 return false;
83 }
Mike Stump11289f42009-09-09 15:08:12 +000084
Douglas Gregora2f49452010-03-16 19:09:18 +000085 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID) {
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000086 HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
87 }
Mike Stump11289f42009-09-09 15:08:12 +000088
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +000089 virtual void ReadCounter(unsigned Value) {
90 Counter = Value;
91 }
92};
93
Douglas Gregor33cdd812010-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 Kyrtzidisce379752009-06-20 08:08:23 +0000127} // anonymous namespace
128
Douglas Gregor33cdd812010-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 Naroffc0683b92009-09-03 18:19:54 +0000134const std::string &ASTUnit::getOriginalSourceFileName() {
Daniel Dunbara8a50932009-12-02 08:44:16 +0000135 return OriginalSourceFile;
Steve Naroffc0683b92009-09-03 18:19:54 +0000136}
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000137
Steve Naroff44cd60e2009-10-15 22:23:48 +0000138const std::string &ASTUnit::getPCHFileName() {
Daniel Dunbara18f9582009-12-02 21:47:43 +0000139 assert(isMainFileAST() && "Not an ASTUnit from a PCH file!");
Benjamin Kramer2ecf8eb2010-01-30 16:23:25 +0000140 return static_cast<PCHReader *>(Ctx->getExternalSource())->getFileName();
Steve Naroff44cd60e2009-10-15 22:23:48 +0000141}
142
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000143ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename,
Douglas Gregord03e8232010-04-05 21:10:19 +0000144 llvm::MaybeOwningPtr<Diagnostic> Diags,
Ted Kremenek8bcb1c62009-10-17 00:34:24 +0000145 bool OnlyLocalDecls,
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000146 RemappedFile *RemappedFiles,
Douglas Gregor33cdd812010-02-18 18:08:43 +0000147 unsigned NumRemappedFiles,
148 bool CaptureDiagnostics) {
Douglas Gregord03e8232010-04-05 21:10:19 +0000149 llvm::OwningPtr<ASTUnit> AST(new ASTUnit(true));
150
151 if (Diags.get())
152 AST->Diagnostics = Diags;
153 else {
154 // No diagnostics engine was provided, so create our own diagnostics object
155 // with the default options.
156 DiagnosticOptions DiagOpts;
157 AST->Diagnostics.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0),
158 true);
159 }
160
Douglas Gregor16bef852009-10-16 20:01:17 +0000161 AST->OnlyLocalDecls = OnlyLocalDecls;
Douglas Gregord03e8232010-04-05 21:10:19 +0000162 AST->FileMgr.reset(new FileManager);
163 AST->SourceMgr.reset(new SourceManager(AST->getDiagnostics()));
Steve Naroff505fb842009-10-19 14:34:22 +0000164 AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager()));
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000165
Douglas Gregor33cdd812010-02-18 18:08:43 +0000166 // If requested, capture diagnostics in the ASTUnit.
Douglas Gregord03e8232010-04-05 21:10:19 +0000167 CaptureDroppedDiagnostics Capture(CaptureDiagnostics, AST->getDiagnostics(),
Douglas Gregora2433152010-04-05 18:10:21 +0000168 AST->StoredDiagnostics);
Douglas Gregor33cdd812010-02-18 18:08:43 +0000169
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000170 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
171 // Create the file entry for the file that we're mapping from.
172 const FileEntry *FromFile
173 = AST->getFileManager().getVirtualFile(RemappedFiles[I].first,
174 RemappedFiles[I].second->getBufferSize(),
175 0);
176 if (!FromFile) {
Douglas Gregord03e8232010-04-05 21:10:19 +0000177 AST->getDiagnostics().Report(diag::err_fe_remap_missing_from_file)
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000178 << RemappedFiles[I].first;
Douglas Gregor89a56c52010-02-27 01:32:48 +0000179 delete RemappedFiles[I].second;
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000180 continue;
181 }
182
183 // Override the contents of the "from" file with the contents of
184 // the "to" file.
185 AST->getSourceManager().overrideFileContents(FromFile,
186 RemappedFiles[I].second);
187 }
188
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000189 // Gather Info for preprocessor construction later on.
Mike Stump11289f42009-09-09 15:08:12 +0000190
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000191 LangOptions LangInfo;
192 HeaderSearch &HeaderInfo = *AST->HeaderInfo.get();
193 std::string TargetTriple;
194 std::string Predefines;
195 unsigned Counter;
196
Daniel Dunbar3a0637b2009-09-03 05:59:50 +0000197 llvm::OwningPtr<PCHReader> Reader;
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000198 llvm::OwningPtr<ExternalASTSource> Source;
199
Ted Kremenek428c6372009-10-19 21:44:57 +0000200 Reader.reset(new PCHReader(AST->getSourceManager(), AST->getFileManager(),
Douglas Gregord03e8232010-04-05 21:10:19 +0000201 AST->getDiagnostics()));
Daniel Dunbar2d9c7402009-09-03 05:59:35 +0000202 Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple,
203 Predefines, Counter));
204
205 switch (Reader->ReadPCH(Filename)) {
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000206 case PCHReader::Success:
207 break;
Mike Stump11289f42009-09-09 15:08:12 +0000208
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000209 case PCHReader::Failure:
Argyrios Kyrtzidis55c34112009-06-25 18:22:30 +0000210 case PCHReader::IgnorePCH:
Douglas Gregord03e8232010-04-05 21:10:19 +0000211 AST->getDiagnostics().Report(diag::err_fe_unable_to_load_pch);
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000212 return NULL;
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000213 }
Mike Stump11289f42009-09-09 15:08:12 +0000214
Daniel Dunbara8a50932009-12-02 08:44:16 +0000215 AST->OriginalSourceFile = Reader->getOriginalSourceFile();
216
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000217 // PCH loaded successfully. Now create the preprocessor.
Mike Stump11289f42009-09-09 15:08:12 +0000218
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000219 // Get information about the target being compiled for.
Daniel Dunbarb9bbd542009-11-15 06:48:46 +0000220 //
221 // FIXME: This is broken, we should store the TargetOptions in the PCH.
222 TargetOptions TargetOpts;
223 TargetOpts.ABI = "";
224 TargetOpts.CPU = "";
225 TargetOpts.Features.clear();
226 TargetOpts.Triple = TargetTriple;
Douglas Gregord03e8232010-04-05 21:10:19 +0000227 AST->Target.reset(TargetInfo::CreateTargetInfo(AST->getDiagnostics(),
228 TargetOpts));
229 AST->PP.reset(new Preprocessor(AST->getDiagnostics(), LangInfo,
230 *AST->Target.get(),
Daniel Dunbar7cd285f2009-09-21 03:03:39 +0000231 AST->getSourceManager(), HeaderInfo));
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000232 Preprocessor &PP = *AST->PP.get();
233
Daniel Dunbarb7bbfdd2009-09-21 03:03:47 +0000234 PP.setPredefines(Reader->getSuggestedPredefines());
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000235 PP.setCounterValue(Counter);
Daniel Dunbar2d9c7402009-09-03 05:59:35 +0000236 Reader->setPreprocessor(PP);
Mike Stump11289f42009-09-09 15:08:12 +0000237
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000238 // Create and initialize the ASTContext.
239
240 AST->Ctx.reset(new ASTContext(LangInfo,
Daniel Dunbar7cd285f2009-09-21 03:03:39 +0000241 AST->getSourceManager(),
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000242 *AST->Target.get(),
243 PP.getIdentifierTable(),
244 PP.getSelectorTable(),
245 PP.getBuiltinInfo(),
Daniel Dunbar19511922010-02-16 01:55:04 +0000246 /* FreeMemory = */ false,
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000247 /* size_reserve = */0));
248 ASTContext &Context = *AST->Ctx.get();
Mike Stump11289f42009-09-09 15:08:12 +0000249
Daniel Dunbar2d9c7402009-09-03 05:59:35 +0000250 Reader->InitializeContext(Context);
Mike Stump11289f42009-09-09 15:08:12 +0000251
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000252 // Attach the PCH reader to the AST context as an external AST
253 // source, so that declarations will be deserialized from the
254 // PCH file as needed.
Daniel Dunbar2d9c7402009-09-03 05:59:35 +0000255 Source.reset(Reader.take());
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000256 Context.setExternalSource(Source);
257
Mike Stump11289f42009-09-09 15:08:12 +0000258 return AST.take();
Argyrios Kyrtzidisce379752009-06-20 08:08:23 +0000259}
Daniel Dunbar764c0822009-12-01 09:51:01 +0000260
261namespace {
262
Daniel Dunbar644dca02009-12-04 08:17:33 +0000263class TopLevelDeclTrackerConsumer : public ASTConsumer {
264 ASTUnit &Unit;
265
266public:
267 TopLevelDeclTrackerConsumer(ASTUnit &_Unit) : Unit(_Unit) {}
268
269 void HandleTopLevelDecl(DeclGroupRef D) {
270 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it)
271 Unit.getTopLevelDecls().push_back(*it);
272 }
273};
274
275class TopLevelDeclTrackerAction : public ASTFrontendAction {
276public:
277 ASTUnit &Unit;
278
Daniel Dunbar764c0822009-12-01 09:51:01 +0000279 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
280 llvm::StringRef InFile) {
Daniel Dunbar644dca02009-12-04 08:17:33 +0000281 return new TopLevelDeclTrackerConsumer(Unit);
Daniel Dunbar764c0822009-12-01 09:51:01 +0000282 }
283
284public:
Daniel Dunbar644dca02009-12-04 08:17:33 +0000285 TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {}
286
Daniel Dunbar764c0822009-12-01 09:51:01 +0000287 virtual bool hasCodeCompletionSupport() const { return false; }
288};
289
290}
291
Daniel Dunbar7f21a7f2010-02-16 01:54:54 +0000292ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI,
Douglas Gregord03e8232010-04-05 21:10:19 +0000293 llvm::MaybeOwningPtr<Diagnostic> Diags,
Douglas Gregor33cdd812010-02-18 18:08:43 +0000294 bool OnlyLocalDecls,
Douglas Gregor7f6d60d2010-03-19 16:15:56 +0000295 bool CaptureDiagnostics) {
Daniel Dunbar764c0822009-12-01 09:51:01 +0000296 // Create the compiler instance to use for building the AST.
Daniel Dunbar7afbb8c2009-12-02 08:43:56 +0000297 CompilerInstance Clang;
Daniel Dunbar764c0822009-12-01 09:51:01 +0000298 llvm::OwningPtr<ASTUnit> AST;
Daniel Dunbar644dca02009-12-04 08:17:33 +0000299 llvm::OwningPtr<TopLevelDeclTrackerAction> Act;
Daniel Dunbar764c0822009-12-01 09:51:01 +0000300
Douglas Gregord03e8232010-04-05 21:10:19 +0000301 if (!Diags.get()) {
302 // No diagnostics engine was provided, so create our own diagnostics object
303 // with the default options.
304 DiagnosticOptions DiagOpts;
305 Diags.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0), true);
306 }
307
Daniel Dunbar7f21a7f2010-02-16 01:54:54 +0000308 Clang.setInvocation(CI);
Daniel Dunbar764c0822009-12-01 09:51:01 +0000309
Douglas Gregord03e8232010-04-05 21:10:19 +0000310 Clang.setDiagnostics(Diags.get());
311 Clang.setDiagnosticClient(Diags->getClient());
Daniel Dunbar764c0822009-12-01 09:51:01 +0000312
313 // Create the target instance.
314 Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(),
315 Clang.getTargetOpts()));
Douglas Gregor33cdd812010-02-18 18:08:43 +0000316 if (!Clang.hasTarget()) {
Douglas Gregor33cdd812010-02-18 18:08:43 +0000317 Clang.takeDiagnosticClient();
318 Clang.takeDiagnostics();
319 return 0;
320 }
Daniel Dunbar764c0822009-12-01 09:51:01 +0000321
322 // Inform the target of the language options.
323 //
324 // FIXME: We shouldn't need to do this, the target should be immutable once
325 // created. This complexity should be lifted elsewhere.
326 Clang.getTarget().setForcedLangOptions(Clang.getLangOpts());
327
328 assert(Clang.getFrontendOpts().Inputs.size() == 1 &&
329 "Invocation must have exactly one source file!");
330 assert(Clang.getFrontendOpts().Inputs[0].first != FrontendOptions::IK_AST &&
331 "FIXME: AST inputs not yet supported here!");
332
333 // Create the AST unit.
Douglas Gregord03e8232010-04-05 21:10:19 +0000334 AST.reset(new ASTUnit(false));
335 AST->Diagnostics = Diags;
336 AST->FileMgr.reset(new FileManager);
337 AST->SourceMgr.reset(new SourceManager(AST->getDiagnostics()));
Daniel Dunbar48973492009-12-02 21:47:32 +0000338 AST->OnlyLocalDecls = OnlyLocalDecls;
Daniel Dunbara8a50932009-12-02 08:44:16 +0000339 AST->OriginalSourceFile = Clang.getFrontendOpts().Inputs[0].second;
340
Douglas Gregor33cdd812010-02-18 18:08:43 +0000341 // Capture any diagnostics that would otherwise be dropped.
342 CaptureDroppedDiagnostics Capture(CaptureDiagnostics,
343 Clang.getDiagnostics(),
Douglas Gregora2433152010-04-05 18:10:21 +0000344 AST->StoredDiagnostics);
Douglas Gregor33cdd812010-02-18 18:08:43 +0000345
Daniel Dunbar764c0822009-12-01 09:51:01 +0000346 // Create a file manager object to provide access to and cache the filesystem.
347 Clang.setFileManager(&AST->getFileManager());
348
349 // Create the source manager.
350 Clang.setSourceManager(&AST->getSourceManager());
351
352 // Create the preprocessor.
353 Clang.createPreprocessor();
354
Daniel Dunbar644dca02009-12-04 08:17:33 +0000355 Act.reset(new TopLevelDeclTrackerAction(*AST));
356 if (!Act->BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second,
Daniel Dunbar764c0822009-12-01 09:51:01 +0000357 /*IsAST=*/false))
358 goto error;
359
Daniel Dunbar644dca02009-12-04 08:17:33 +0000360 Act->Execute();
Daniel Dunbar764c0822009-12-01 09:51:01 +0000361
Daniel Dunbard2f8be32009-12-01 21:57:33 +0000362 // Steal the created target, context, and preprocessor, and take back the
363 // source and file managers.
Daniel Dunbar764c0822009-12-01 09:51:01 +0000364 AST->Ctx.reset(Clang.takeASTContext());
365 AST->PP.reset(Clang.takePreprocessor());
366 Clang.takeSourceManager();
367 Clang.takeFileManager();
Daniel Dunbard2f8be32009-12-01 21:57:33 +0000368 AST->Target.reset(Clang.takeTarget());
Daniel Dunbar764c0822009-12-01 09:51:01 +0000369
Daniel Dunbar644dca02009-12-04 08:17:33 +0000370 Act->EndSourceFile();
Daniel Dunbar764c0822009-12-01 09:51:01 +0000371
372 Clang.takeDiagnosticClient();
373 Clang.takeDiagnostics();
Daniel Dunbar6b03ece2010-01-30 21:47:16 +0000374 Clang.takeInvocation();
Daniel Dunbar764c0822009-12-01 09:51:01 +0000375
Daniel Dunbar7f21a7f2010-02-16 01:54:54 +0000376 AST->Invocation.reset(Clang.takeInvocation());
Daniel Dunbar764c0822009-12-01 09:51:01 +0000377 return AST.take();
378
379error:
380 Clang.takeSourceManager();
381 Clang.takeFileManager();
382 Clang.takeDiagnosticClient();
383 Clang.takeDiagnostics();
384 return 0;
385}
Daniel Dunbar55a17b62009-12-02 03:23:45 +0000386
387ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin,
388 const char **ArgEnd,
Douglas Gregord03e8232010-04-05 21:10:19 +0000389 llvm::MaybeOwningPtr<Diagnostic> Diags,
Daniel Dunbar8d4a2022009-12-13 03:46:13 +0000390 llvm::StringRef ResourceFilesPath,
Daniel Dunbar55a17b62009-12-02 03:23:45 +0000391 bool OnlyLocalDecls,
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000392 RemappedFile *RemappedFiles,
Douglas Gregor33cdd812010-02-18 18:08:43 +0000393 unsigned NumRemappedFiles,
Douglas Gregor7f6d60d2010-03-19 16:15:56 +0000394 bool CaptureDiagnostics) {
Douglas Gregord03e8232010-04-05 21:10:19 +0000395 if (!Diags.get()) {
396 // No diagnostics engine was provided, so create our own diagnostics object
397 // with the default options.
398 DiagnosticOptions DiagOpts;
399 Diags.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0), true);
400 }
401
Daniel Dunbar55a17b62009-12-02 03:23:45 +0000402 llvm::SmallVector<const char *, 16> Args;
403 Args.push_back("<clang>"); // FIXME: Remove dummy argument.
404 Args.insert(Args.end(), ArgBegin, ArgEnd);
405
406 // FIXME: Find a cleaner way to force the driver into restricted modes. We
407 // also want to force it to use clang.
408 Args.push_back("-fsyntax-only");
409
Daniel Dunbar8d4a2022009-12-13 03:46:13 +0000410 // FIXME: We shouldn't have to pass in the path info.
411 driver::Driver TheDriver("clang", "/", llvm::sys::getHostTriple(),
Douglas Gregord03e8232010-04-05 21:10:19 +0000412 "a.out", false, false, *Diags);
Daniel Dunbarfcf2d422010-01-25 00:44:02 +0000413
414 // Don't check that inputs exist, they have been remapped.
415 TheDriver.setCheckInputsExist(false);
416
Daniel Dunbar55a17b62009-12-02 03:23:45 +0000417 llvm::OwningPtr<driver::Compilation> C(
418 TheDriver.BuildCompilation(Args.size(), Args.data()));
419
420 // We expect to get back exactly one command job, if we didn't something
421 // failed.
422 const driver::JobList &Jobs = C->getJobs();
423 if (Jobs.size() != 1 || !isa<driver::Command>(Jobs.begin())) {
424 llvm::SmallString<256> Msg;
425 llvm::raw_svector_ostream OS(Msg);
426 C->PrintJob(OS, C->getJobs(), "; ", true);
Douglas Gregord03e8232010-04-05 21:10:19 +0000427 Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
Daniel Dunbar55a17b62009-12-02 03:23:45 +0000428 return 0;
429 }
430
431 const driver::Command *Cmd = cast<driver::Command>(*Jobs.begin());
432 if (llvm::StringRef(Cmd->getCreator().getName()) != "clang") {
Douglas Gregord03e8232010-04-05 21:10:19 +0000433 Diags->Report(diag::err_fe_expected_clang_command);
Daniel Dunbar55a17b62009-12-02 03:23:45 +0000434 return 0;
435 }
436
437 const driver::ArgStringList &CCArgs = Cmd->getArguments();
Daniel Dunbar6b03ece2010-01-30 21:47:16 +0000438 llvm::OwningPtr<CompilerInvocation> CI(new CompilerInvocation);
439 CompilerInvocation::CreateFromArgs(*CI, (const char**) CCArgs.data(),
Daniel Dunbar55a17b62009-12-02 03:23:45 +0000440 (const char**) CCArgs.data()+CCArgs.size(),
Douglas Gregord03e8232010-04-05 21:10:19 +0000441 *Diags);
Daniel Dunbard6136772009-12-13 03:45:58 +0000442
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000443 // Override any files that need remapping
444 for (unsigned I = 0; I != NumRemappedFiles; ++I)
Daniel Dunbar6b03ece2010-01-30 21:47:16 +0000445 CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
Daniel Dunbar19511922010-02-16 01:55:04 +0000446 RemappedFiles[I].second);
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000447
Daniel Dunbara5a166d2009-12-15 00:06:45 +0000448 // Override the resources path.
Daniel Dunbar6b03ece2010-01-30 21:47:16 +0000449 CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
Daniel Dunbar55a17b62009-12-02 03:23:45 +0000450
Daniel Dunbar19511922010-02-16 01:55:04 +0000451 CI->getFrontendOpts().DisableFree = true;
Douglas Gregor33cdd812010-02-18 18:08:43 +0000452 return LoadFromCompilerInvocation(CI.take(), Diags, OnlyLocalDecls,
Douglas Gregor7f6d60d2010-03-19 16:15:56 +0000453 CaptureDiagnostics);
Daniel Dunbar55a17b62009-12-02 03:23:45 +0000454}