1 //===--- ASTUnit.cpp - ASTUnit utility ------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // ASTUnit Implementation.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Frontend/ASTUnit.h"
15 #include "clang/Frontend/PCHReader.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/ASTConsumer.h"
18 #include "clang/AST/DeclVisitor.h"
19 #include "clang/AST/StmtVisitor.h"
20 #include "clang/Driver/Compilation.h"
21 #include "clang/Driver/Driver.h"
22 #include "clang/Driver/Job.h"
23 #include "clang/Driver/Tool.h"
24 #include "clang/Frontend/CompilerInstance.h"
25 #include "clang/Frontend/FrontendActions.h"
26 #include "clang/Frontend/FrontendDiagnostic.h"
27 #include "clang/Frontend/FrontendOptions.h"
28 #include "clang/Lex/HeaderSearch.h"
29 #include "clang/Lex/Preprocessor.h"
30 #include "clang/Basic/TargetOptions.h"
31 #include "clang/Basic/TargetInfo.h"
32 #include "clang/Basic/Diagnostic.h"
33 #include "llvm/Support/MemoryBuffer.h"
34 #include "llvm/System/Host.h"
35 #include "llvm/System/Path.h"
36 using namespace clang;
38 ASTUnit::ASTUnit(bool _MainFileIsAST)
39 : MainFileIsAST(_MainFileIsAST), ConcurrencyCheckValue(CheckUnlocked) { }
42 ConcurrencyCheckValue = CheckLocked;
43 for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I)
44 TemporaryFiles[I].eraseFromDisk();
49 /// \brief Gathers information from PCHReader that will be used to initialize
51 class PCHInfoCollector : public PCHReaderListener {
54 std::string &TargetTriple;
55 std::string &Predefines;
58 unsigned NumHeaderInfos;
61 PCHInfoCollector(LangOptions &LangOpt, HeaderSearch &HSI,
62 std::string &TargetTriple, std::string &Predefines,
64 : LangOpt(LangOpt), HSI(HSI), TargetTriple(TargetTriple),
65 Predefines(Predefines), Counter(Counter), NumHeaderInfos(0) {}
67 virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
72 virtual bool ReadTargetTriple(llvm::StringRef Triple) {
73 TargetTriple = Triple;
77 virtual bool ReadPredefinesBuffer(llvm::StringRef PCHPredef,
79 llvm::StringRef OriginalFileName,
80 std::string &SuggestedPredefines) {
81 Predefines = PCHPredef;
85 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID) {
86 HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
89 virtual void ReadCounter(unsigned Value) {
94 class StoredDiagnosticClient : public DiagnosticClient {
95 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags;
98 explicit StoredDiagnosticClient(
99 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags)
100 : StoredDiags(StoredDiags) { }
102 virtual void HandleDiagnostic(Diagnostic::Level Level,
103 const DiagnosticInfo &Info);
106 /// \brief RAII object that optionally captures diagnostics, if
107 /// there is no diagnostic client to capture them already.
108 class CaptureDroppedDiagnostics {
110 StoredDiagnosticClient Client;
111 DiagnosticClient *PreviousClient;
114 CaptureDroppedDiagnostics(bool RequestCapture, Diagnostic &Diags,
115 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags)
116 : Diags(Diags), Client(StoredDiags), PreviousClient(Diags.getClient())
118 if (RequestCapture || Diags.getClient() == 0)
119 Diags.setClient(&Client);
122 ~CaptureDroppedDiagnostics() {
123 Diags.setClient(PreviousClient);
127 } // anonymous namespace
129 void StoredDiagnosticClient::HandleDiagnostic(Diagnostic::Level Level,
130 const DiagnosticInfo &Info) {
131 StoredDiags.push_back(StoredDiagnostic(Level, Info));
134 const std::string &ASTUnit::getOriginalSourceFileName() {
135 return OriginalSourceFile;
138 const std::string &ASTUnit::getPCHFileName() {
139 assert(isMainFileAST() && "Not an ASTUnit from a PCH file!");
140 return static_cast<PCHReader *>(Ctx->getExternalSource())->getFileName();
143 ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename,
144 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
146 RemappedFile *RemappedFiles,
147 unsigned NumRemappedFiles,
148 bool CaptureDiagnostics) {
149 llvm::OwningPtr<ASTUnit> AST(new ASTUnit(true));
151 if (!Diags.getPtr()) {
152 // No diagnostics engine was provided, so create our own diagnostics object
153 // with the default options.
154 DiagnosticOptions DiagOpts;
155 Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0);
158 AST->OnlyLocalDecls = OnlyLocalDecls;
159 AST->Diagnostics = Diags;
160 AST->FileMgr.reset(new FileManager);
161 AST->SourceMgr.reset(new SourceManager(AST->getDiagnostics()));
162 AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager()));
164 // If requested, capture diagnostics in the ASTUnit.
165 CaptureDroppedDiagnostics Capture(CaptureDiagnostics, AST->getDiagnostics(),
166 AST->StoredDiagnostics);
168 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(),
175 AST->getDiagnostics().Report(diag::err_fe_remap_missing_from_file)
176 << RemappedFiles[I].first;
177 delete RemappedFiles[I].second;
181 // Override the contents of the "from" file with the contents of
183 AST->getSourceManager().overrideFileContents(FromFile,
184 RemappedFiles[I].second);
187 // Gather Info for preprocessor construction later on.
189 LangOptions LangInfo;
190 HeaderSearch &HeaderInfo = *AST->HeaderInfo.get();
191 std::string TargetTriple;
192 std::string Predefines;
195 llvm::OwningPtr<PCHReader> Reader;
196 llvm::OwningPtr<ExternalASTSource> Source;
198 Reader.reset(new PCHReader(AST->getSourceManager(), AST->getFileManager(),
199 AST->getDiagnostics()));
200 Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple,
201 Predefines, Counter));
203 switch (Reader->ReadPCH(Filename)) {
204 case PCHReader::Success:
207 case PCHReader::Failure:
208 case PCHReader::IgnorePCH:
209 AST->getDiagnostics().Report(diag::err_fe_unable_to_load_pch);
213 AST->OriginalSourceFile = Reader->getOriginalSourceFile();
215 // PCH loaded successfully. Now create the preprocessor.
217 // Get information about the target being compiled for.
219 // FIXME: This is broken, we should store the TargetOptions in the PCH.
220 TargetOptions TargetOpts;
222 TargetOpts.CXXABI = "itanium";
224 TargetOpts.Features.clear();
225 TargetOpts.Triple = TargetTriple;
226 AST->Target.reset(TargetInfo::CreateTargetInfo(AST->getDiagnostics(),
228 AST->PP.reset(new Preprocessor(AST->getDiagnostics(), LangInfo,
230 AST->getSourceManager(), HeaderInfo));
231 Preprocessor &PP = *AST->PP.get();
233 PP.setPredefines(Reader->getSuggestedPredefines());
234 PP.setCounterValue(Counter);
235 Reader->setPreprocessor(PP);
237 // Create and initialize the ASTContext.
239 AST->Ctx.reset(new ASTContext(LangInfo,
240 AST->getSourceManager(),
242 PP.getIdentifierTable(),
243 PP.getSelectorTable(),
245 /* FreeMemory = */ false,
246 /* size_reserve = */0));
247 ASTContext &Context = *AST->Ctx.get();
249 Reader->InitializeContext(Context);
251 // Attach the PCH reader to the AST context as an external AST
252 // source, so that declarations will be deserialized from the
253 // PCH file as needed.
254 Source.reset(Reader.take());
255 Context.setExternalSource(Source);
262 class TopLevelDeclTrackerConsumer : public ASTConsumer {
266 TopLevelDeclTrackerConsumer(ASTUnit &_Unit) : Unit(_Unit) {}
268 void HandleTopLevelDecl(DeclGroupRef D) {
269 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) {
271 // FIXME: Currently ObjC method declarations are incorrectly being
272 // reported as top-level declarations, even though their DeclContext
273 // is the containing ObjC @interface/@implementation. This is a
274 // fundamental problem in the parser right now.
275 if (isa<ObjCMethodDecl>(D))
277 Unit.getTopLevelDecls().push_back(D);
282 class TopLevelDeclTrackerAction : public ASTFrontendAction {
286 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
287 llvm::StringRef InFile) {
288 return new TopLevelDeclTrackerConsumer(Unit);
292 TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {}
294 virtual bool hasCodeCompletionSupport() const { return false; }
299 ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI,
300 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
302 bool CaptureDiagnostics) {
303 // Create the compiler instance to use for building the AST.
304 CompilerInstance Clang;
305 llvm::OwningPtr<ASTUnit> AST;
306 llvm::OwningPtr<TopLevelDeclTrackerAction> Act;
308 if (!Diags.getPtr()) {
309 // No diagnostics engine was provided, so create our own diagnostics object
310 // with the default options.
311 DiagnosticOptions DiagOpts;
312 Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0);
315 Clang.setInvocation(CI);
317 Clang.setDiagnostics(Diags.getPtr());
318 Clang.setDiagnosticClient(Diags->getClient());
320 // Create the target instance.
321 Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(),
322 Clang.getTargetOpts()));
323 if (!Clang.hasTarget()) {
324 Clang.takeDiagnosticClient();
328 // Inform the target of the language options.
330 // FIXME: We shouldn't need to do this, the target should be immutable once
331 // created. This complexity should be lifted elsewhere.
332 Clang.getTarget().setForcedLangOptions(Clang.getLangOpts());
334 assert(Clang.getFrontendOpts().Inputs.size() == 1 &&
335 "Invocation must have exactly one source file!");
336 assert(Clang.getFrontendOpts().Inputs[0].first != IK_AST &&
337 "FIXME: AST inputs not yet supported here!");
338 assert(Clang.getFrontendOpts().Inputs[0].first != IK_LLVM_IR &&
339 "IR inputs not support here!");
341 // Create the AST unit.
342 AST.reset(new ASTUnit(false));
343 AST->Diagnostics = Diags;
344 AST->FileMgr.reset(new FileManager);
345 AST->SourceMgr.reset(new SourceManager(AST->getDiagnostics()));
346 AST->OnlyLocalDecls = OnlyLocalDecls;
347 AST->OriginalSourceFile = Clang.getFrontendOpts().Inputs[0].second;
349 // Capture any diagnostics that would otherwise be dropped.
350 CaptureDroppedDiagnostics Capture(CaptureDiagnostics,
351 Clang.getDiagnostics(),
352 AST->StoredDiagnostics);
354 // Create a file manager object to provide access to and cache the filesystem.
355 Clang.setFileManager(&AST->getFileManager());
357 // Create the source manager.
358 Clang.setSourceManager(&AST->getSourceManager());
360 Act.reset(new TopLevelDeclTrackerAction(*AST));
361 if (!Act->BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second,
362 Clang.getFrontendOpts().Inputs[0].first))
367 // Steal the created target, context, and preprocessor, and take back the
368 // source and file managers.
369 AST->Ctx.reset(Clang.takeASTContext());
370 AST->PP.reset(Clang.takePreprocessor());
371 Clang.takeSourceManager();
372 Clang.takeFileManager();
373 AST->Target.reset(Clang.takeTarget());
375 Act->EndSourceFile();
377 Clang.takeDiagnosticClient();
378 Clang.takeInvocation();
380 AST->Invocation.reset(Clang.takeInvocation());
384 Clang.takeSourceManager();
385 Clang.takeFileManager();
386 Clang.takeDiagnosticClient();
390 ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin,
392 llvm::IntrusiveRefCntPtr<Diagnostic> Diags,
393 llvm::StringRef ResourceFilesPath,
395 RemappedFile *RemappedFiles,
396 unsigned NumRemappedFiles,
397 bool CaptureDiagnostics) {
398 if (!Diags.getPtr()) {
399 // No diagnostics engine was provided, so create our own diagnostics object
400 // with the default options.
401 DiagnosticOptions DiagOpts;
402 Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0);
405 llvm::SmallVector<const char *, 16> Args;
406 Args.push_back("<clang>"); // FIXME: Remove dummy argument.
407 Args.insert(Args.end(), ArgBegin, ArgEnd);
409 // FIXME: Find a cleaner way to force the driver into restricted modes. We
410 // also want to force it to use clang.
411 Args.push_back("-fsyntax-only");
413 // FIXME: We shouldn't have to pass in the path info.
414 driver::Driver TheDriver("clang", "/", llvm::sys::getHostTriple(),
415 "a.out", false, false, *Diags);
417 // Don't check that inputs exist, they have been remapped.
418 TheDriver.setCheckInputsExist(false);
420 llvm::OwningPtr<driver::Compilation> C(
421 TheDriver.BuildCompilation(Args.size(), Args.data()));
423 // We expect to get back exactly one command job, if we didn't something
425 const driver::JobList &Jobs = C->getJobs();
426 if (Jobs.size() != 1 || !isa<driver::Command>(Jobs.begin())) {
427 llvm::SmallString<256> Msg;
428 llvm::raw_svector_ostream OS(Msg);
429 C->PrintJob(OS, C->getJobs(), "; ", true);
430 Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
434 const driver::Command *Cmd = cast<driver::Command>(*Jobs.begin());
435 if (llvm::StringRef(Cmd->getCreator().getName()) != "clang") {
436 Diags->Report(diag::err_fe_expected_clang_command);
440 const driver::ArgStringList &CCArgs = Cmd->getArguments();
441 llvm::OwningPtr<CompilerInvocation> CI(new CompilerInvocation);
442 CompilerInvocation::CreateFromArgs(*CI,
443 const_cast<const char **>(CCArgs.data()),
444 const_cast<const char **>(CCArgs.data()) +
448 // Override any files that need remapping
449 for (unsigned I = 0; I != NumRemappedFiles; ++I)
450 CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
451 RemappedFiles[I].second);
453 // Override the resources path.
454 CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
456 CI->getFrontendOpts().DisableFree = true;
457 return LoadFromCompilerInvocation(CI.take(), Diags, OnlyLocalDecls,