blob: d8e1c5152991d850b2d5696e61e779d746f1390a [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
Sebastian Redlcb481aa2010-07-14 23:29:55 +000077 virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +000078 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000079 std::string &SuggestedPredefines) {
Sebastian Redlcb481aa2010-07-14 23:29:55 +000080 Predefines = Buffers[0].Data;
81 for (unsigned I = 1, N = Buffers.size(); I != N; ++I) {
82 Predefines += Buffers[I].Data;
83 }
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000084 return false;
85 }
Mike Stump1eb44332009-09-09 15:08:12 +000086
Douglas Gregorec1afbf2010-03-16 19:09:18 +000087 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID) {
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000088 HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
89 }
Mike Stump1eb44332009-09-09 15:08:12 +000090
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +000091 virtual void ReadCounter(unsigned Value) {
92 Counter = Value;
93 }
94};
95
Douglas Gregora88084b2010-02-18 18:08:43 +000096class StoredDiagnosticClient : public DiagnosticClient {
97 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags;
98
99public:
100 explicit StoredDiagnosticClient(
101 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags)
102 : StoredDiags(StoredDiags) { }
103
104 virtual void HandleDiagnostic(Diagnostic::Level Level,
105 const DiagnosticInfo &Info);
106};
107
108/// \brief RAII object that optionally captures diagnostics, if
109/// there is no diagnostic client to capture them already.
110class CaptureDroppedDiagnostics {
111 Diagnostic &Diags;
112 StoredDiagnosticClient Client;
113 DiagnosticClient *PreviousClient;
114
115public:
116 CaptureDroppedDiagnostics(bool RequestCapture, Diagnostic &Diags,
117 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags)
118 : Diags(Diags), Client(StoredDiags), PreviousClient(Diags.getClient())
119 {
120 if (RequestCapture || Diags.getClient() == 0)
121 Diags.setClient(&Client);
122 }
123
124 ~CaptureDroppedDiagnostics() {
125 Diags.setClient(PreviousClient);
126 }
127};
128
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000129} // anonymous namespace
130
Douglas Gregora88084b2010-02-18 18:08:43 +0000131void StoredDiagnosticClient::HandleDiagnostic(Diagnostic::Level Level,
132 const DiagnosticInfo &Info) {
133 StoredDiags.push_back(StoredDiagnostic(Level, Info));
134}
135
Steve Naroff77accc12009-09-03 18:19:54 +0000136const std::string &ASTUnit::getOriginalSourceFileName() {
Daniel Dunbar68d40e22009-12-02 08:44:16 +0000137 return OriginalSourceFile;
Steve Naroff77accc12009-09-03 18:19:54 +0000138}
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000139
Steve Naroffe19944c2009-10-15 22:23:48 +0000140const std::string &ASTUnit::getPCHFileName() {
Daniel Dunbarc7822db2009-12-02 21:47:43 +0000141 assert(isMainFileAST() && "Not an ASTUnit from a PCH file!");
Benjamin Kramer7297c182010-01-30 16:23:25 +0000142 return static_cast<PCHReader *>(Ctx->getExternalSource())->getFileName();
Steve Naroffe19944c2009-10-15 22:23:48 +0000143}
144
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000145ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename,
Douglas Gregor28019772010-04-05 23:52:57 +0000146 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
Ted Kremenek5cf48762009-10-17 00:34:24 +0000147 bool OnlyLocalDecls,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000148 RemappedFile *RemappedFiles,
Douglas Gregora88084b2010-02-18 18:08:43 +0000149 unsigned NumRemappedFiles,
150 bool CaptureDiagnostics) {
Douglas Gregor3687e9d2010-04-05 21:10:19 +0000151 llvm::OwningPtr<ASTUnit> AST(new ASTUnit(true));
152
Douglas Gregor28019772010-04-05 23:52:57 +0000153 if (!Diags.getPtr()) {
Douglas Gregor3687e9d2010-04-05 21:10:19 +0000154 // No diagnostics engine was provided, so create our own diagnostics object
155 // with the default options.
156 DiagnosticOptions DiagOpts;
Douglas Gregor28019772010-04-05 23:52:57 +0000157 Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0);
Douglas Gregor3687e9d2010-04-05 21:10:19 +0000158 }
159
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000160 AST->OnlyLocalDecls = OnlyLocalDecls;
Douglas Gregor28019772010-04-05 23:52:57 +0000161 AST->Diagnostics = Diags;
Douglas Gregor3687e9d2010-04-05 21:10:19 +0000162 AST->FileMgr.reset(new FileManager);
163 AST->SourceMgr.reset(new SourceManager(AST->getDiagnostics()));
Steve Naroff36c44642009-10-19 14:34:22 +0000164 AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager()));
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000165
Douglas Gregora88084b2010-02-18 18:08:43 +0000166 // If requested, capture diagnostics in the ASTUnit.
Douglas Gregor3687e9d2010-04-05 21:10:19 +0000167 CaptureDroppedDiagnostics Capture(CaptureDiagnostics, AST->getDiagnostics(),
Douglas Gregor405634b2010-04-05 18:10:21 +0000168 AST->StoredDiagnostics);
Douglas Gregora88084b2010-02-18 18:08:43 +0000169
Douglas Gregor4db64a42010-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 Gregor3687e9d2010-04-05 21:10:19 +0000177 AST->getDiagnostics().Report(diag::err_fe_remap_missing_from_file)
Douglas Gregor4db64a42010-01-23 00:14:00 +0000178 << RemappedFiles[I].first;
Douglas Gregorc8dfe5e2010-02-27 01:32:48 +0000179 delete RemappedFiles[I].second;
Douglas Gregor4db64a42010-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 Kyrtzidis0853a022009-06-20 08:08:23 +0000189 // Gather Info for preprocessor construction later on.
Mike Stump1eb44332009-09-09 15:08:12 +0000190
Argyrios Kyrtzidis0853a022009-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 Dunbarbce6f622009-09-03 05:59:50 +0000197 llvm::OwningPtr<PCHReader> Reader;
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000198 llvm::OwningPtr<ExternalASTSource> Source;
199
Ted Kremenekfc062212009-10-19 21:44:57 +0000200 Reader.reset(new PCHReader(AST->getSourceManager(), AST->getFileManager(),
Douglas Gregor3687e9d2010-04-05 21:10:19 +0000201 AST->getDiagnostics()));
Daniel Dunbarcc318932009-09-03 05:59:35 +0000202 Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple,
203 Predefines, Counter));
204
205 switch (Reader->ReadPCH(Filename)) {
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000206 case PCHReader::Success:
207 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000209 case PCHReader::Failure:
Argyrios Kyrtzidis106c9982009-06-25 18:22:30 +0000210 case PCHReader::IgnorePCH:
Douglas Gregor3687e9d2010-04-05 21:10:19 +0000211 AST->getDiagnostics().Report(diag::err_fe_unable_to_load_pch);
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000212 return NULL;
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000213 }
Mike Stump1eb44332009-09-09 15:08:12 +0000214
Daniel Dunbar68d40e22009-12-02 08:44:16 +0000215 AST->OriginalSourceFile = Reader->getOriginalSourceFile();
216
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000217 // PCH loaded successfully. Now create the preprocessor.
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000219 // Get information about the target being compiled for.
Daniel Dunbard58c03f2009-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 = "";
Charles Davis98b7c5c2010-06-11 01:06:47 +0000224 TargetOpts.CXXABI = "itanium";
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000225 TargetOpts.CPU = "";
226 TargetOpts.Features.clear();
227 TargetOpts.Triple = TargetTriple;
Douglas Gregor3687e9d2010-04-05 21:10:19 +0000228 AST->Target.reset(TargetInfo::CreateTargetInfo(AST->getDiagnostics(),
229 TargetOpts));
230 AST->PP.reset(new Preprocessor(AST->getDiagnostics(), LangInfo,
231 *AST->Target.get(),
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000232 AST->getSourceManager(), HeaderInfo));
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000233 Preprocessor &PP = *AST->PP.get();
234
Daniel Dunbard5b61262009-09-21 03:03:47 +0000235 PP.setPredefines(Reader->getSuggestedPredefines());
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000236 PP.setCounterValue(Counter);
Daniel Dunbarcc318932009-09-03 05:59:35 +0000237 Reader->setPreprocessor(PP);
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000239 // Create and initialize the ASTContext.
240
241 AST->Ctx.reset(new ASTContext(LangInfo,
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000242 AST->getSourceManager(),
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000243 *AST->Target.get(),
244 PP.getIdentifierTable(),
245 PP.getSelectorTable(),
246 PP.getBuiltinInfo(),
Daniel Dunbarb26d4832010-02-16 01:55:04 +0000247 /* FreeMemory = */ false,
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000248 /* size_reserve = */0));
249 ASTContext &Context = *AST->Ctx.get();
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Daniel Dunbarcc318932009-09-03 05:59:35 +0000251 Reader->InitializeContext(Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000252
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000253 // Attach the PCH reader to the AST context as an external AST
254 // source, so that declarations will be deserialized from the
255 // PCH file as needed.
Daniel Dunbarcc318932009-09-03 05:59:35 +0000256 Source.reset(Reader.take());
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000257 Context.setExternalSource(Source);
258
Mike Stump1eb44332009-09-09 15:08:12 +0000259 return AST.take();
Argyrios Kyrtzidis0853a022009-06-20 08:08:23 +0000260}
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000261
262namespace {
263
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000264class TopLevelDeclTrackerConsumer : public ASTConsumer {
265 ASTUnit &Unit;
266
267public:
268 TopLevelDeclTrackerConsumer(ASTUnit &_Unit) : Unit(_Unit) {}
269
270 void HandleTopLevelDecl(DeclGroupRef D) {
Ted Kremenekda5a4282010-05-03 20:16:35 +0000271 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) {
272 Decl *D = *it;
273 // FIXME: Currently ObjC method declarations are incorrectly being
274 // reported as top-level declarations, even though their DeclContext
275 // is the containing ObjC @interface/@implementation. This is a
276 // fundamental problem in the parser right now.
277 if (isa<ObjCMethodDecl>(D))
278 continue;
279 Unit.getTopLevelDecls().push_back(D);
280 }
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000281 }
282};
283
284class TopLevelDeclTrackerAction : public ASTFrontendAction {
285public:
286 ASTUnit &Unit;
287
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000288 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
289 llvm::StringRef InFile) {
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000290 return new TopLevelDeclTrackerConsumer(Unit);
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000291 }
292
293public:
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000294 TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {}
295
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000296 virtual bool hasCodeCompletionSupport() const { return false; }
297};
298
299}
300
Daniel Dunbarf7acc372010-02-16 01:54:54 +0000301ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI,
Douglas Gregor28019772010-04-05 23:52:57 +0000302 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
Douglas Gregora88084b2010-02-18 18:08:43 +0000303 bool OnlyLocalDecls,
Douglas Gregor94dc8f62010-03-19 16:15:56 +0000304 bool CaptureDiagnostics) {
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000305 // Create the compiler instance to use for building the AST.
Daniel Dunbarcb6dda12009-12-02 08:43:56 +0000306 CompilerInstance Clang;
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000307 llvm::OwningPtr<ASTUnit> AST;
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000308 llvm::OwningPtr<TopLevelDeclTrackerAction> Act;
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000309
Douglas Gregor28019772010-04-05 23:52:57 +0000310 if (!Diags.getPtr()) {
Douglas Gregor3687e9d2010-04-05 21:10:19 +0000311 // No diagnostics engine was provided, so create our own diagnostics object
312 // with the default options.
313 DiagnosticOptions DiagOpts;
Douglas Gregor28019772010-04-05 23:52:57 +0000314 Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0);
Douglas Gregor3687e9d2010-04-05 21:10:19 +0000315 }
316
Daniel Dunbarf7acc372010-02-16 01:54:54 +0000317 Clang.setInvocation(CI);
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000318
Douglas Gregor28019772010-04-05 23:52:57 +0000319 Clang.setDiagnostics(Diags.getPtr());
Douglas Gregor3687e9d2010-04-05 21:10:19 +0000320 Clang.setDiagnosticClient(Diags->getClient());
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000321
322 // Create the target instance.
323 Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(),
324 Clang.getTargetOpts()));
Douglas Gregora88084b2010-02-18 18:08:43 +0000325 if (!Clang.hasTarget()) {
Douglas Gregora88084b2010-02-18 18:08:43 +0000326 Clang.takeDiagnosticClient();
Douglas Gregora88084b2010-02-18 18:08:43 +0000327 return 0;
328 }
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000329
330 // Inform the target of the language options.
331 //
332 // FIXME: We shouldn't need to do this, the target should be immutable once
333 // created. This complexity should be lifted elsewhere.
334 Clang.getTarget().setForcedLangOptions(Clang.getLangOpts());
335
336 assert(Clang.getFrontendOpts().Inputs.size() == 1 &&
337 "Invocation must have exactly one source file!");
Daniel Dunbarc34ce3f2010-06-07 23:22:09 +0000338 assert(Clang.getFrontendOpts().Inputs[0].first != IK_AST &&
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000339 "FIXME: AST inputs not yet supported here!");
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000340 assert(Clang.getFrontendOpts().Inputs[0].first != IK_LLVM_IR &&
341 "IR inputs not support here!");
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000342
343 // Create the AST unit.
Douglas Gregor3687e9d2010-04-05 21:10:19 +0000344 AST.reset(new ASTUnit(false));
345 AST->Diagnostics = Diags;
346 AST->FileMgr.reset(new FileManager);
347 AST->SourceMgr.reset(new SourceManager(AST->getDiagnostics()));
Daniel Dunbar68ea2ac2009-12-02 21:47:32 +0000348 AST->OnlyLocalDecls = OnlyLocalDecls;
Daniel Dunbar68d40e22009-12-02 08:44:16 +0000349 AST->OriginalSourceFile = Clang.getFrontendOpts().Inputs[0].second;
350
Douglas Gregora88084b2010-02-18 18:08:43 +0000351 // Capture any diagnostics that would otherwise be dropped.
352 CaptureDroppedDiagnostics Capture(CaptureDiagnostics,
353 Clang.getDiagnostics(),
Douglas Gregor405634b2010-04-05 18:10:21 +0000354 AST->StoredDiagnostics);
Douglas Gregora88084b2010-02-18 18:08:43 +0000355
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000356 // Create a file manager object to provide access to and cache the filesystem.
357 Clang.setFileManager(&AST->getFileManager());
358
359 // Create the source manager.
360 Clang.setSourceManager(&AST->getSourceManager());
361
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000362 Act.reset(new TopLevelDeclTrackerAction(*AST));
363 if (!Act->BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second,
Daniel Dunbard3598a62010-06-07 23:23:06 +0000364 Clang.getFrontendOpts().Inputs[0].first))
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000365 goto error;
366
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000367 Act->Execute();
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000368
Daniel Dunbar64a32ba2009-12-01 21:57:33 +0000369 // Steal the created target, context, and preprocessor, and take back the
370 // source and file managers.
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000371 AST->Ctx.reset(Clang.takeASTContext());
372 AST->PP.reset(Clang.takePreprocessor());
373 Clang.takeSourceManager();
374 Clang.takeFileManager();
Daniel Dunbar64a32ba2009-12-01 21:57:33 +0000375 AST->Target.reset(Clang.takeTarget());
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000376
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000377 Act->EndSourceFile();
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000378
379 Clang.takeDiagnosticClient();
Daniel Dunbar807b0612010-01-30 21:47:16 +0000380 Clang.takeInvocation();
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000381
Daniel Dunbarf7acc372010-02-16 01:54:54 +0000382 AST->Invocation.reset(Clang.takeInvocation());
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000383 return AST.take();
384
385error:
386 Clang.takeSourceManager();
387 Clang.takeFileManager();
388 Clang.takeDiagnosticClient();
Daniel Dunbar521bf9c2009-12-01 09:51:01 +0000389 return 0;
390}
Daniel Dunbar7b556682009-12-02 03:23:45 +0000391
392ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin,
393 const char **ArgEnd,
Douglas Gregor28019772010-04-05 23:52:57 +0000394 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
Daniel Dunbar869824e2009-12-13 03:46:13 +0000395 llvm::StringRef ResourceFilesPath,
Daniel Dunbar7b556682009-12-02 03:23:45 +0000396 bool OnlyLocalDecls,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000397 RemappedFile *RemappedFiles,
Douglas Gregora88084b2010-02-18 18:08:43 +0000398 unsigned NumRemappedFiles,
Douglas Gregor94dc8f62010-03-19 16:15:56 +0000399 bool CaptureDiagnostics) {
Douglas Gregor28019772010-04-05 23:52:57 +0000400 if (!Diags.getPtr()) {
Douglas Gregor3687e9d2010-04-05 21:10:19 +0000401 // No diagnostics engine was provided, so create our own diagnostics object
402 // with the default options.
403 DiagnosticOptions DiagOpts;
Douglas Gregor28019772010-04-05 23:52:57 +0000404 Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0);
Douglas Gregor3687e9d2010-04-05 21:10:19 +0000405 }
406
Daniel Dunbar7b556682009-12-02 03:23:45 +0000407 llvm::SmallVector<const char *, 16> Args;
408 Args.push_back("<clang>"); // FIXME: Remove dummy argument.
409 Args.insert(Args.end(), ArgBegin, ArgEnd);
410
411 // FIXME: Find a cleaner way to force the driver into restricted modes. We
412 // also want to force it to use clang.
413 Args.push_back("-fsyntax-only");
414
Daniel Dunbar869824e2009-12-13 03:46:13 +0000415 // FIXME: We shouldn't have to pass in the path info.
Daniel Dunbar0bbad512010-07-19 00:44:04 +0000416 driver::Driver TheDriver("clang", llvm::sys::getHostTriple(),
Douglas Gregor3687e9d2010-04-05 21:10:19 +0000417 "a.out", false, false, *Diags);
Daniel Dunbar3bd54cc2010-01-25 00:44:02 +0000418
419 // Don't check that inputs exist, they have been remapped.
420 TheDriver.setCheckInputsExist(false);
421
Daniel Dunbar7b556682009-12-02 03:23:45 +0000422 llvm::OwningPtr<driver::Compilation> C(
423 TheDriver.BuildCompilation(Args.size(), Args.data()));
424
425 // We expect to get back exactly one command job, if we didn't something
426 // failed.
427 const driver::JobList &Jobs = C->getJobs();
428 if (Jobs.size() != 1 || !isa<driver::Command>(Jobs.begin())) {
429 llvm::SmallString<256> Msg;
430 llvm::raw_svector_ostream OS(Msg);
431 C->PrintJob(OS, C->getJobs(), "; ", true);
Douglas Gregor3687e9d2010-04-05 21:10:19 +0000432 Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
Daniel Dunbar7b556682009-12-02 03:23:45 +0000433 return 0;
434 }
435
436 const driver::Command *Cmd = cast<driver::Command>(*Jobs.begin());
437 if (llvm::StringRef(Cmd->getCreator().getName()) != "clang") {
Douglas Gregor3687e9d2010-04-05 21:10:19 +0000438 Diags->Report(diag::err_fe_expected_clang_command);
Daniel Dunbar7b556682009-12-02 03:23:45 +0000439 return 0;
440 }
441
442 const driver::ArgStringList &CCArgs = Cmd->getArguments();
Daniel Dunbar807b0612010-01-30 21:47:16 +0000443 llvm::OwningPtr<CompilerInvocation> CI(new CompilerInvocation);
Dan Gohmancb421fa2010-04-19 16:39:44 +0000444 CompilerInvocation::CreateFromArgs(*CI,
445 const_cast<const char **>(CCArgs.data()),
446 const_cast<const char **>(CCArgs.data()) +
447 CCArgs.size(),
Douglas Gregor3687e9d2010-04-05 21:10:19 +0000448 *Diags);
Daniel Dunbar1e69fe32009-12-13 03:45:58 +0000449
Douglas Gregor4db64a42010-01-23 00:14:00 +0000450 // Override any files that need remapping
451 for (unsigned I = 0; I != NumRemappedFiles; ++I)
Daniel Dunbar807b0612010-01-30 21:47:16 +0000452 CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
Daniel Dunbarb26d4832010-02-16 01:55:04 +0000453 RemappedFiles[I].second);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000454
Daniel Dunbar8b9adfe2009-12-15 00:06:45 +0000455 // Override the resources path.
Daniel Dunbar807b0612010-01-30 21:47:16 +0000456 CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
Daniel Dunbar7b556682009-12-02 03:23:45 +0000457
Daniel Dunbarb26d4832010-02-16 01:55:04 +0000458 CI->getFrontendOpts().DisableFree = true;
Douglas Gregora88084b2010-02-18 18:08:43 +0000459 return LoadFromCompilerInvocation(CI.take(), Diags, OnlyLocalDecls,
Douglas Gregor94dc8f62010-03-19 16:15:56 +0000460 CaptureDiagnostics);
Daniel Dunbar7b556682009-12-02 03:23:45 +0000461}