blob: 427bd6a9b8035188f5c011cda6c37a83649ca7b5 [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
Douglas Gregor3687e9d2010-04-05 21:10:19 +000038ASTUnit::ASTUnit(bool _MainFileIsAST)
39 : MainFileIsAST(_MainFileIsAST), ConcurrencyCheckValue(CheckUnlocked) { }
40
Daniel Dunbar521bf9c2009-12-01 09:51:01 +000041ASTUnit::~ASTUnit() {
Douglas Gregorbdf60622010-03-05 21:16:25 +000042 ConcurrencyCheckValue = CheckLocked;
Douglas Gregor313e26c2010-02-18 23:35:40 +000043 for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I)
44 TemporaryFiles[I].eraseFromDisk();
Steve Naroffe19944c2009-10-15 22:23:48 +000045}
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000046
47namespace {
48
49/// \brief Gathers information from PCHReader that will be used to initialize
50/// a Preprocessor.
Benjamin Kramerbd218282009-11-28 10:07:24 +000051class PCHInfoCollector : public PCHReaderListener {
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000052 LangOptions &LangOpt;
53 HeaderSearch &HSI;
54 std::string &TargetTriple;
55 std::string &Predefines;
56 unsigned &Counter;
Mike Stump1eb44332009-09-09 15:08:12 +000057
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000058 unsigned NumHeaderInfos;
Mike Stump1eb44332009-09-09 15:08:12 +000059
Argyrios Kyrtzidis0853a022009-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 Stump1eb44332009-09-09 15:08:12 +000066
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000067 virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
68 LangOpt = LangOpts;
69 return false;
70 }
Mike Stump1eb44332009-09-09 15:08:12 +000071
Daniel Dunbardc3c0d22009-11-11 00:52:11 +000072 virtual bool ReadTargetTriple(llvm::StringRef Triple) {
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000073 TargetTriple = Triple;
74 return false;
75 }
Mike Stump1eb44332009-09-09 15:08:12 +000076
Daniel Dunbardc3c0d22009-11-11 00:52:11 +000077 virtual bool ReadPredefinesBuffer(llvm::StringRef PCHPredef,
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000078 FileID PCHBufferID,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +000079 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000080 std::string &SuggestedPredefines) {
81 Predefines = PCHPredef;
82 return false;
83 }
Mike Stump1eb44332009-09-09 15:08:12 +000084
Douglas Gregorec1afbf2010-03-16 19:09:18 +000085 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID) {
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000086 HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
87 }
Mike Stump1eb44332009-09-09 15:08:12 +000088
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000089 virtual void ReadCounter(unsigned Value) {
90 Counter = Value;
91 }
92};
93
Douglas Gregora88084b2010-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 Kyrtzidis0853a022009-06-20 08:08:23 +0000127} // anonymous namespace
128
Douglas Gregora88084b2010-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 Naroff77accc12009-09-03 18:19:54 +0000134const std::string &ASTUnit::getOriginalSourceFileName() {
Daniel Dunbar68d40e22009-12-02 08:44:16 +0000135 return OriginalSourceFile;
Steve Naroff77accc12009-09-03 18:19:54 +0000136}
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000137
Steve Naroffe19944c2009-10-15 22:23:48 +0000138const std::string &ASTUnit::getPCHFileName() {
Daniel Dunbarc7822db2009-12-02 21:47:43 +0000139 assert(isMainFileAST() && "Not an ASTUnit from a PCH file!");
Benjamin Kramer7297c182010-01-30 16:23:25 +0000140 return static_cast<PCHReader *>(Ctx->getExternalSource())->getFileName();
Steve Naroffe19944c2009-10-15 22:23:48 +0000141}
142
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000143ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename,
Douglas Gregor28019772010-04-05 23:52:57 +0000144 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
Ted Kremenek5cf48762009-10-17 00:34:24 +0000145 bool OnlyLocalDecls,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000146 RemappedFile *RemappedFiles,
Douglas Gregora88084b2010-02-18 18:08:43 +0000147 unsigned NumRemappedFiles,
148 bool CaptureDiagnostics) {
Douglas Gregor3687e9d2010-04-05 21:10:19 +0000149 llvm::OwningPtr<ASTUnit> AST(new ASTUnit(true));
150
Douglas Gregor28019772010-04-05 23:52:57 +0000151 if (!Diags.getPtr()) {
Douglas Gregor3687e9d2010-04-05 21:10:19 +0000152 // No diagnostics engine was provided, so create our own diagnostics object
153 // with the default options.
154 DiagnosticOptions DiagOpts;
Douglas Gregor28019772010-04-05 23:52:57 +0000155 Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0);
Douglas Gregor3687e9d2010-04-05 21:10:19 +0000156 }
157
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000158 AST->OnlyLocalDecls = OnlyLocalDecls;
Douglas Gregor28019772010-04-05 23:52:57 +0000159 AST->Diagnostics = Diags;
Douglas Gregor3687e9d2010-04-05 21:10:19 +0000160 AST->FileMgr.reset(new FileManager);
161 AST->SourceMgr.reset(new SourceManager(AST->getDiagnostics()));
Steve Naroff36c44642009-10-19 14:34:22 +0000162 AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager()));
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000163
Douglas Gregora88084b2010-02-18 18:08:43 +0000164 // If requested, capture diagnostics in the ASTUnit.
Douglas Gregor3687e9d2010-04-05 21:10:19 +0000165 CaptureDroppedDiagnostics Capture(CaptureDiagnostics, AST->getDiagnostics(),
Douglas Gregor405634b2010-04-05 18:10:21 +0000166 AST->StoredDiagnostics);
Douglas Gregora88084b2010-02-18 18:08:43 +0000167
Douglas Gregor4db64a42010-01-23 00:14:00 +0000168 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
169 // Create the file entry for the file that we're mapping from.
170 const FileEntry *FromFile
171 = AST->getFileManager().getVirtualFile(RemappedFiles[I].first,
172 RemappedFiles[I].second->getBufferSize(),
173 0);
174 if (!FromFile) {
Douglas Gregor3687e9d2010-04-05 21:10:19 +0000175 AST->getDiagnostics().Report(diag::err_fe_remap_missing_from_file)
Douglas Gregor4db64a42010-01-23 00:14:00 +0000176 << RemappedFiles[I].first;
Douglas Gregorc8dfe5e2010-02-27 01:32:48 +0000177 delete RemappedFiles[I].second;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000178 continue;
179 }
180
181 // Override the contents of the "from" file with the contents of
182 // the "to" file.
183 AST->getSourceManager().overrideFileContents(FromFile,
184 RemappedFiles[I].second);
185 }
186
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000187 // Gather Info for preprocessor construction later on.
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000189 LangOptions LangInfo;
190 HeaderSearch &HeaderInfo = *AST->HeaderInfo.get();
191 std::string TargetTriple;
192 std::string Predefines;
193 unsigned Counter;
194
Daniel Dunbarbce6f622009-09-03 05:59:50 +0000195 llvm::OwningPtr<PCHReader> Reader;
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000196 llvm::OwningPtr<ExternalASTSource> Source;
197
Ted Kremenekfc062212009-10-19 21:44:57 +0000198 Reader.reset(new PCHReader(AST->getSourceManager(), AST->getFileManager(),
Douglas Gregor3687e9d2010-04-05 21:10:19 +0000199 AST->getDiagnostics()));
Daniel Dunbarcc318932009-09-03 05:59:35 +0000200 Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple,
201 Predefines, Counter));
202
203 switch (Reader->ReadPCH(Filename)) {
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000204 case PCHReader::Success:
205 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000207 case PCHReader::Failure:
Argyrios Kyrtzidis106c9982009-06-25 18:22:30 +0000208 case PCHReader::IgnorePCH:
Douglas Gregor3687e9d2010-04-05 21:10:19 +0000209 AST->getDiagnostics().Report(diag::err_fe_unable_to_load_pch);
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000210 return NULL;
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000211 }
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Daniel Dunbar68d40e22009-12-02 08:44:16 +0000213 AST->OriginalSourceFile = Reader->getOriginalSourceFile();
214
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000215 // PCH loaded successfully. Now create the preprocessor.
Mike Stump1eb44332009-09-09 15:08:12 +0000216
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000217 // Get information about the target being compiled for.
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000218 //
219 // FIXME: This is broken, we should store the TargetOptions in the PCH.
220 TargetOptions TargetOpts;
221 TargetOpts.ABI = "";
222 TargetOpts.CPU = "";
223 TargetOpts.Features.clear();
224 TargetOpts.Triple = TargetTriple;
Douglas Gregor3687e9d2010-04-05 21:10:19 +0000225 AST->Target.reset(TargetInfo::CreateTargetInfo(AST->getDiagnostics(),
226 TargetOpts));
227 AST->PP.reset(new Preprocessor(AST->getDiagnostics(), LangInfo,
228 *AST->Target.get(),
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000229 AST->getSourceManager(), HeaderInfo));
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000230 Preprocessor &PP = *AST->PP.get();
231
Daniel Dunbard5b61262009-09-21 03:03:47 +0000232 PP.setPredefines(Reader->getSuggestedPredefines());
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000233 PP.setCounterValue(Counter);
Daniel Dunbarcc318932009-09-03 05:59:35 +0000234 Reader->setPreprocessor(PP);
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000236 // Create and initialize the ASTContext.
237
238 AST->Ctx.reset(new ASTContext(LangInfo,
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000239 AST->getSourceManager(),
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000240 *AST->Target.get(),
241 PP.getIdentifierTable(),
242 PP.getSelectorTable(),
243 PP.getBuiltinInfo(),
Daniel Dunbarb26d4832010-02-16 01:55:04 +0000244 /* FreeMemory = */ false,
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000245 /* size_reserve = */0));
246 ASTContext &Context = *AST->Ctx.get();
Mike Stump1eb44332009-09-09 15:08:12 +0000247
Daniel Dunbarcc318932009-09-03 05:59:35 +0000248 Reader->InitializeContext(Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000249
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000250 // Attach the PCH reader to the AST context as an external AST
251 // source, so that declarations will be deserialized from the
252 // PCH file as needed.
Daniel Dunbarcc318932009-09-03 05:59:35 +0000253 Source.reset(Reader.take());
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000254 Context.setExternalSource(Source);
255
Mike Stump1eb44332009-09-09 15:08:12 +0000256 return AST.take();
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000257}
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000258
259namespace {
260
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000261class TopLevelDeclTrackerConsumer : public ASTConsumer {
262 ASTUnit &Unit;
263
264public:
265 TopLevelDeclTrackerConsumer(ASTUnit &_Unit) : Unit(_Unit) {}
266
267 void HandleTopLevelDecl(DeclGroupRef D) {
268 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it)
269 Unit.getTopLevelDecls().push_back(*it);
270 }
271};
272
273class TopLevelDeclTrackerAction : public ASTFrontendAction {
274public:
275 ASTUnit &Unit;
276
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000277 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
278 llvm::StringRef InFile) {
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000279 return new TopLevelDeclTrackerConsumer(Unit);
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000280 }
281
282public:
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000283 TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {}
284
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000285 virtual bool hasCodeCompletionSupport() const { return false; }
286};
287
288}
289
Daniel Dunbarf7acc372010-02-16 01:54:54 +0000290ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI,
Douglas Gregor28019772010-04-05 23:52:57 +0000291 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
Douglas Gregora88084b2010-02-18 18:08:43 +0000292 bool OnlyLocalDecls,
Douglas Gregor94dc8f62010-03-19 16:15:56 +0000293 bool CaptureDiagnostics) {
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000294 // Create the compiler instance to use for building the AST.
Daniel Dunbarcb6dda12009-12-02 08:43:56 +0000295 CompilerInstance Clang;
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000296 llvm::OwningPtr<ASTUnit> AST;
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000297 llvm::OwningPtr<TopLevelDeclTrackerAction> Act;
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000298
Douglas Gregor28019772010-04-05 23:52:57 +0000299 if (!Diags.getPtr()) {
Douglas Gregor3687e9d2010-04-05 21:10:19 +0000300 // No diagnostics engine was provided, so create our own diagnostics object
301 // with the default options.
302 DiagnosticOptions DiagOpts;
Douglas Gregor28019772010-04-05 23:52:57 +0000303 Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0);
Douglas Gregor3687e9d2010-04-05 21:10:19 +0000304 }
305
Daniel Dunbarf7acc372010-02-16 01:54:54 +0000306 Clang.setInvocation(CI);
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000307
Douglas Gregor28019772010-04-05 23:52:57 +0000308 Clang.setDiagnostics(Diags.getPtr());
Douglas Gregor3687e9d2010-04-05 21:10:19 +0000309 Clang.setDiagnosticClient(Diags->getClient());
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000310
311 // Create the target instance.
312 Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(),
313 Clang.getTargetOpts()));
Douglas Gregora88084b2010-02-18 18:08:43 +0000314 if (!Clang.hasTarget()) {
Douglas Gregora88084b2010-02-18 18:08:43 +0000315 Clang.takeDiagnosticClient();
Douglas Gregora88084b2010-02-18 18:08:43 +0000316 return 0;
317 }
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000318
319 // Inform the target of the language options.
320 //
321 // FIXME: We shouldn't need to do this, the target should be immutable once
322 // created. This complexity should be lifted elsewhere.
323 Clang.getTarget().setForcedLangOptions(Clang.getLangOpts());
324
325 assert(Clang.getFrontendOpts().Inputs.size() == 1 &&
326 "Invocation must have exactly one source file!");
327 assert(Clang.getFrontendOpts().Inputs[0].first != FrontendOptions::IK_AST &&
328 "FIXME: AST inputs not yet supported here!");
329
330 // Create the AST unit.
Douglas Gregor3687e9d2010-04-05 21:10:19 +0000331 AST.reset(new ASTUnit(false));
332 AST->Diagnostics = Diags;
333 AST->FileMgr.reset(new FileManager);
334 AST->SourceMgr.reset(new SourceManager(AST->getDiagnostics()));
Daniel Dunbar68ea2ac2009-12-02 21:47:32 +0000335 AST->OnlyLocalDecls = OnlyLocalDecls;
Daniel Dunbar68d40e22009-12-02 08:44:16 +0000336 AST->OriginalSourceFile = Clang.getFrontendOpts().Inputs[0].second;
337
Douglas Gregora88084b2010-02-18 18:08:43 +0000338 // Capture any diagnostics that would otherwise be dropped.
339 CaptureDroppedDiagnostics Capture(CaptureDiagnostics,
340 Clang.getDiagnostics(),
Douglas Gregor405634b2010-04-05 18:10:21 +0000341 AST->StoredDiagnostics);
Douglas Gregora88084b2010-02-18 18:08:43 +0000342
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000343 // Create a file manager object to provide access to and cache the filesystem.
344 Clang.setFileManager(&AST->getFileManager());
345
346 // Create the source manager.
347 Clang.setSourceManager(&AST->getSourceManager());
348
349 // Create the preprocessor.
350 Clang.createPreprocessor();
351
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000352 Act.reset(new TopLevelDeclTrackerAction(*AST));
353 if (!Act->BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second,
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000354 /*IsAST=*/false))
355 goto error;
356
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000357 Act->Execute();
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000358
Daniel Dunbar64a32ba2009-12-01 21:57:33 +0000359 // Steal the created target, context, and preprocessor, and take back the
360 // source and file managers.
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000361 AST->Ctx.reset(Clang.takeASTContext());
362 AST->PP.reset(Clang.takePreprocessor());
363 Clang.takeSourceManager();
364 Clang.takeFileManager();
Daniel Dunbar64a32ba2009-12-01 21:57:33 +0000365 AST->Target.reset(Clang.takeTarget());
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000366
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000367 Act->EndSourceFile();
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000368
369 Clang.takeDiagnosticClient();
Daniel Dunbar807b0612010-01-30 21:47:16 +0000370 Clang.takeInvocation();
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000371
Daniel Dunbarf7acc372010-02-16 01:54:54 +0000372 AST->Invocation.reset(Clang.takeInvocation());
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000373 return AST.take();
374
375error:
376 Clang.takeSourceManager();
377 Clang.takeFileManager();
378 Clang.takeDiagnosticClient();
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000379 return 0;
380}
Daniel Dunbar7b556682009-12-02 03:23:45 +0000381
382ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin,
383 const char **ArgEnd,
Douglas Gregor28019772010-04-05 23:52:57 +0000384 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
Daniel Dunbar869824e2009-12-13 03:46:13 +0000385 llvm::StringRef ResourceFilesPath,
Daniel Dunbar7b556682009-12-02 03:23:45 +0000386 bool OnlyLocalDecls,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000387 RemappedFile *RemappedFiles,
Douglas Gregora88084b2010-02-18 18:08:43 +0000388 unsigned NumRemappedFiles,
Douglas Gregor94dc8f62010-03-19 16:15:56 +0000389 bool CaptureDiagnostics) {
Douglas Gregor28019772010-04-05 23:52:57 +0000390 if (!Diags.getPtr()) {
Douglas Gregor3687e9d2010-04-05 21:10:19 +0000391 // No diagnostics engine was provided, so create our own diagnostics object
392 // with the default options.
393 DiagnosticOptions DiagOpts;
Douglas Gregor28019772010-04-05 23:52:57 +0000394 Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0);
Douglas Gregor3687e9d2010-04-05 21:10:19 +0000395 }
396
Daniel Dunbar7b556682009-12-02 03:23:45 +0000397 llvm::SmallVector<const char *, 16> Args;
398 Args.push_back("<clang>"); // FIXME: Remove dummy argument.
399 Args.insert(Args.end(), ArgBegin, ArgEnd);
400
401 // FIXME: Find a cleaner way to force the driver into restricted modes. We
402 // also want to force it to use clang.
403 Args.push_back("-fsyntax-only");
404
Daniel Dunbar869824e2009-12-13 03:46:13 +0000405 // FIXME: We shouldn't have to pass in the path info.
406 driver::Driver TheDriver("clang", "/", llvm::sys::getHostTriple(),
Douglas Gregor3687e9d2010-04-05 21:10:19 +0000407 "a.out", false, false, *Diags);
Daniel Dunbar3bd54cc2010-01-25 00:44:02 +0000408
409 // Don't check that inputs exist, they have been remapped.
410 TheDriver.setCheckInputsExist(false);
411
Daniel Dunbar7b556682009-12-02 03:23:45 +0000412 llvm::OwningPtr<driver::Compilation> C(
413 TheDriver.BuildCompilation(Args.size(), Args.data()));
414
415 // We expect to get back exactly one command job, if we didn't something
416 // failed.
417 const driver::JobList &Jobs = C->getJobs();
418 if (Jobs.size() != 1 || !isa<driver::Command>(Jobs.begin())) {
419 llvm::SmallString<256> Msg;
420 llvm::raw_svector_ostream OS(Msg);
421 C->PrintJob(OS, C->getJobs(), "; ", true);
Douglas Gregor3687e9d2010-04-05 21:10:19 +0000422 Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
Daniel Dunbar7b556682009-12-02 03:23:45 +0000423 return 0;
424 }
425
426 const driver::Command *Cmd = cast<driver::Command>(*Jobs.begin());
427 if (llvm::StringRef(Cmd->getCreator().getName()) != "clang") {
Douglas Gregor3687e9d2010-04-05 21:10:19 +0000428 Diags->Report(diag::err_fe_expected_clang_command);
Daniel Dunbar7b556682009-12-02 03:23:45 +0000429 return 0;
430 }
431
432 const driver::ArgStringList &CCArgs = Cmd->getArguments();
Daniel Dunbar807b0612010-01-30 21:47:16 +0000433 llvm::OwningPtr<CompilerInvocation> CI(new CompilerInvocation);
434 CompilerInvocation::CreateFromArgs(*CI, (const char**) CCArgs.data(),
Daniel Dunbar7b556682009-12-02 03:23:45 +0000435 (const char**) CCArgs.data()+CCArgs.size(),
Douglas Gregor3687e9d2010-04-05 21:10:19 +0000436 *Diags);
Daniel Dunbar1e69fe32009-12-13 03:45:58 +0000437
Douglas Gregor4db64a42010-01-23 00:14:00 +0000438 // Override any files that need remapping
439 for (unsigned I = 0; I != NumRemappedFiles; ++I)
Daniel Dunbar807b0612010-01-30 21:47:16 +0000440 CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
Daniel Dunbarb26d4832010-02-16 01:55:04 +0000441 RemappedFiles[I].second);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000442
Daniel Dunbar8b9adfe2009-12-15 00:06:45 +0000443 // Override the resources path.
Daniel Dunbar807b0612010-01-30 21:47:16 +0000444 CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
Daniel Dunbar7b556682009-12-02 03:23:45 +0000445
Daniel Dunbarb26d4832010-02-16 01:55:04 +0000446 CI->getFrontendOpts().DisableFree = true;
Douglas Gregora88084b2010-02-18 18:08:43 +0000447 return LoadFromCompilerInvocation(CI.take(), Diags, OnlyLocalDecls,
Douglas Gregor94dc8f62010-03-19 16:15:56 +0000448 CaptureDiagnostics);
Daniel Dunbar7b556682009-12-02 03:23:45 +0000449}