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) {
42 for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I)
43 TemporaryFiles[I].eraseFromDisk();
48 /// \brief Gathers information from PCHReader that will be used to initialize
50 class PCHInfoCollector : public PCHReaderListener {
53 std::string &TargetTriple;
54 std::string &Predefines;
57 unsigned NumHeaderInfos;
60 PCHInfoCollector(LangOptions &LangOpt, HeaderSearch &HSI,
61 std::string &TargetTriple, std::string &Predefines,
63 : LangOpt(LangOpt), HSI(HSI), TargetTriple(TargetTriple),
64 Predefines(Predefines), Counter(Counter), NumHeaderInfos(0) {}
66 virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
71 virtual bool ReadTargetTriple(llvm::StringRef Triple) {
72 TargetTriple = Triple;
76 virtual bool ReadPredefinesBuffer(llvm::StringRef PCHPredef,
78 llvm::StringRef OriginalFileName,
79 std::string &SuggestedPredefines) {
80 Predefines = PCHPredef;
84 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI) {
85 HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
88 virtual void ReadCounter(unsigned Value) {
93 class StoredDiagnosticClient : public DiagnosticClient {
94 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags;
97 explicit StoredDiagnosticClient(
98 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags)
99 : StoredDiags(StoredDiags) { }
101 virtual void HandleDiagnostic(Diagnostic::Level Level,
102 const DiagnosticInfo &Info);
105 /// \brief RAII object that optionally captures diagnostics, if
106 /// there is no diagnostic client to capture them already.
107 class CaptureDroppedDiagnostics {
109 StoredDiagnosticClient Client;
110 DiagnosticClient *PreviousClient;
113 CaptureDroppedDiagnostics(bool RequestCapture, Diagnostic &Diags,
114 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags)
115 : Diags(Diags), Client(StoredDiags), PreviousClient(Diags.getClient())
117 if (RequestCapture || Diags.getClient() == 0)
118 Diags.setClient(&Client);
121 ~CaptureDroppedDiagnostics() {
122 Diags.setClient(PreviousClient);
126 } // anonymous namespace
128 void StoredDiagnosticClient::HandleDiagnostic(Diagnostic::Level Level,
129 const DiagnosticInfo &Info) {
130 StoredDiags.push_back(StoredDiagnostic(Level, Info));
133 const std::string &ASTUnit::getOriginalSourceFileName() {
134 return OriginalSourceFile;
137 const std::string &ASTUnit::getPCHFileName() {
138 assert(isMainFileAST() && "Not an ASTUnit from a PCH file!");
139 return static_cast<PCHReader *>(Ctx->getExternalSource())->getFileName();
142 ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename,
145 RemappedFile *RemappedFiles,
146 unsigned NumRemappedFiles,
147 bool CaptureDiagnostics) {
148 llvm::OwningPtr<ASTUnit> AST(new ASTUnit(true));
149 AST->OnlyLocalDecls = OnlyLocalDecls;
150 AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager()));
152 // If requested, capture diagnostics in the ASTUnit.
153 CaptureDroppedDiagnostics Capture(CaptureDiagnostics, Diags,
156 for (unsigned I = 0; I != NumRemappedFiles; ++I) {
157 // Create the file entry for the file that we're mapping from.
158 const FileEntry *FromFile
159 = AST->getFileManager().getVirtualFile(RemappedFiles[I].first,
160 RemappedFiles[I].second->getBufferSize(),
163 Diags.Report(diag::err_fe_remap_missing_from_file)
164 << RemappedFiles[I].first;
165 delete RemappedFiles[I].second;
169 // Override the contents of the "from" file with the contents of
171 AST->getSourceManager().overrideFileContents(FromFile,
172 RemappedFiles[I].second);
175 // Gather Info for preprocessor construction later on.
177 LangOptions LangInfo;
178 HeaderSearch &HeaderInfo = *AST->HeaderInfo.get();
179 std::string TargetTriple;
180 std::string Predefines;
183 llvm::OwningPtr<PCHReader> Reader;
184 llvm::OwningPtr<ExternalASTSource> Source;
186 Reader.reset(new PCHReader(AST->getSourceManager(), AST->getFileManager(),
188 Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple,
189 Predefines, Counter));
191 switch (Reader->ReadPCH(Filename)) {
192 case PCHReader::Success:
195 case PCHReader::Failure:
196 case PCHReader::IgnorePCH:
197 Diags.Report(diag::err_fe_unable_to_load_pch);
201 AST->OriginalSourceFile = Reader->getOriginalSourceFile();
203 // PCH loaded successfully. Now create the preprocessor.
205 // Get information about the target being compiled for.
207 // FIXME: This is broken, we should store the TargetOptions in the PCH.
208 TargetOptions TargetOpts;
211 TargetOpts.Features.clear();
212 TargetOpts.Triple = TargetTriple;
213 AST->Target.reset(TargetInfo::CreateTargetInfo(Diags, TargetOpts));
214 AST->PP.reset(new Preprocessor(Diags, LangInfo, *AST->Target.get(),
215 AST->getSourceManager(), HeaderInfo));
216 Preprocessor &PP = *AST->PP.get();
218 PP.setPredefines(Reader->getSuggestedPredefines());
219 PP.setCounterValue(Counter);
220 Reader->setPreprocessor(PP);
222 // Create and initialize the ASTContext.
224 AST->Ctx.reset(new ASTContext(LangInfo,
225 AST->getSourceManager(),
227 PP.getIdentifierTable(),
228 PP.getSelectorTable(),
230 /* FreeMemory = */ false,
231 /* size_reserve = */0));
232 ASTContext &Context = *AST->Ctx.get();
234 Reader->InitializeContext(Context);
236 // Attach the PCH reader to the AST context as an external AST
237 // source, so that declarations will be deserialized from the
238 // PCH file as needed.
239 Source.reset(Reader.take());
240 Context.setExternalSource(Source);
247 class TopLevelDeclTrackerConsumer : public ASTConsumer {
251 TopLevelDeclTrackerConsumer(ASTUnit &_Unit) : Unit(_Unit) {}
253 void HandleTopLevelDecl(DeclGroupRef D) {
254 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it)
255 Unit.getTopLevelDecls().push_back(*it);
259 class TopLevelDeclTrackerAction : public ASTFrontendAction {
263 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
264 llvm::StringRef InFile) {
265 return new TopLevelDeclTrackerConsumer(Unit);
269 TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {}
271 virtual bool hasCodeCompletionSupport() const { return false; }
276 ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI,
279 bool CaptureDiagnostics) {
280 // Create the compiler instance to use for building the AST.
281 CompilerInstance Clang;
282 llvm::OwningPtr<ASTUnit> AST;
283 llvm::OwningPtr<TopLevelDeclTrackerAction> Act;
285 Clang.setInvocation(CI);
287 Clang.setDiagnostics(&Diags);
288 Clang.setDiagnosticClient(Diags.getClient());
290 // Create the target instance.
291 Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(),
292 Clang.getTargetOpts()));
293 if (!Clang.hasTarget()) {
294 Clang.takeSourceManager();
295 Clang.takeFileManager();
296 Clang.takeDiagnosticClient();
297 Clang.takeDiagnostics();
301 // Inform the target of the language options.
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());
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!");
312 // Create the AST unit.
313 AST.reset(new ASTUnit(false));
314 AST->OnlyLocalDecls = OnlyLocalDecls;
315 AST->OriginalSourceFile = Clang.getFrontendOpts().Inputs[0].second;
317 // Capture any diagnostics that would otherwise be dropped.
318 CaptureDroppedDiagnostics Capture(CaptureDiagnostics,
319 Clang.getDiagnostics(),
322 // Create a file manager object to provide access to and cache the filesystem.
323 Clang.setFileManager(&AST->getFileManager());
325 // Create the source manager.
326 Clang.setSourceManager(&AST->getSourceManager());
328 // Create the preprocessor.
329 Clang.createPreprocessor();
331 Act.reset(new TopLevelDeclTrackerAction(*AST));
332 if (!Act->BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second,
338 // Steal the created target, context, and preprocessor, and take back the
339 // source and file managers.
340 AST->Ctx.reset(Clang.takeASTContext());
341 AST->PP.reset(Clang.takePreprocessor());
342 Clang.takeSourceManager();
343 Clang.takeFileManager();
344 AST->Target.reset(Clang.takeTarget());
346 Act->EndSourceFile();
348 Clang.takeDiagnosticClient();
349 Clang.takeDiagnostics();
350 Clang.takeInvocation();
352 AST->Invocation.reset(Clang.takeInvocation());
356 Clang.takeSourceManager();
357 Clang.takeFileManager();
358 Clang.takeDiagnosticClient();
359 Clang.takeDiagnostics();
363 ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin,
366 llvm::StringRef ResourceFilesPath,
368 RemappedFile *RemappedFiles,
369 unsigned NumRemappedFiles,
370 bool CaptureDiagnostics) {
371 llvm::SmallVector<const char *, 16> Args;
372 Args.push_back("<clang>"); // FIXME: Remove dummy argument.
373 Args.insert(Args.end(), ArgBegin, ArgEnd);
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");
379 // FIXME: We shouldn't have to pass in the path info.
380 driver::Driver TheDriver("clang", "/", llvm::sys::getHostTriple(),
381 "a.out", false, Diags);
383 // Don't check that inputs exist, they have been remapped.
384 TheDriver.setCheckInputsExist(false);
386 llvm::OwningPtr<driver::Compilation> C(
387 TheDriver.BuildCompilation(Args.size(), Args.data()));
389 // We expect to get back exactly one command job, if we didn't something
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();
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);
406 const driver::ArgStringList &CCArgs = Cmd->getArguments();
407 llvm::OwningPtr<CompilerInvocation> CI(new CompilerInvocation);
408 CompilerInvocation::CreateFromArgs(*CI, (const char**) CCArgs.data(),
409 (const char**) CCArgs.data()+CCArgs.size(),
412 // Override any files that need remapping
413 for (unsigned I = 0; I != NumRemappedFiles; ++I)
414 CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
415 RemappedFiles[I].second);
417 // Override the resources path.
418 CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
420 CI->getFrontendOpts().DisableFree = true;
421 return LoadFromCompilerInvocation(CI.take(), Diags, OnlyLocalDecls,