2009-06-20 16:27:14 +08:00
|
|
|
//===--- ASTUnit.cpp - ASTUnit utility ------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// ASTUnit Implementation.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-06-20 16:08:23 +08:00
|
|
|
#include "clang/Frontend/ASTUnit.h"
|
|
|
|
#include "clang/Frontend/PCHReader.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
2009-12-01 17:51:01 +08:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2009-06-20 16:08:23 +08:00
|
|
|
#include "clang/AST/DeclVisitor.h"
|
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2009-12-01 17:51:01 +08:00
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Frontend/FrontendActions.h"
|
|
|
|
#include "clang/Frontend/FrontendOptions.h"
|
2009-06-20 16:08:23 +08:00
|
|
|
#include "clang/Lex/HeaderSearch.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2009-11-15 14:48:46 +08:00
|
|
|
#include "clang/Basic/TargetOptions.h"
|
2009-06-20 16:08:23 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2009-12-01 17:51:01 +08:00
|
|
|
#include "llvm/LLVMContext.h"
|
2009-10-18 19:34:14 +08:00
|
|
|
#include "llvm/System/Path.h"
|
2009-06-20 16:08:23 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2009-12-01 17:51:01 +08:00
|
|
|
ASTUnit::ASTUnit(DiagnosticClient *diagClient) : tempFile(false) {
|
2009-10-20 05:44:57 +08:00
|
|
|
Diags.setClient(diagClient ? diagClient : new TextDiagnosticBuffer());
|
2009-10-19 22:34:22 +08:00
|
|
|
}
|
2009-12-01 17:51:01 +08:00
|
|
|
ASTUnit::~ASTUnit() {
|
2009-10-16 06:23:48 +08:00
|
|
|
if (tempFile)
|
2009-10-18 19:34:14 +08:00
|
|
|
llvm::sys::Path(getPCHFileName()).eraseFromDisk();
|
2009-12-01 17:51:01 +08:00
|
|
|
|
2009-10-20 05:44:57 +08:00
|
|
|
// The ASTUnit object owns the DiagnosticClient.
|
|
|
|
delete Diags.getClient();
|
2009-10-16 06:23:48 +08:00
|
|
|
}
|
2009-06-20 16:08:23 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// \brief Gathers information from PCHReader that will be used to initialize
|
|
|
|
/// a Preprocessor.
|
2009-11-28 18:07:24 +08:00
|
|
|
class PCHInfoCollector : public PCHReaderListener {
|
2009-06-20 16:08:23 +08:00
|
|
|
LangOptions &LangOpt;
|
|
|
|
HeaderSearch &HSI;
|
|
|
|
std::string &TargetTriple;
|
|
|
|
std::string &Predefines;
|
|
|
|
unsigned &Counter;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-20 16:08:23 +08:00
|
|
|
unsigned NumHeaderInfos;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-20 16:08:23 +08:00
|
|
|
public:
|
|
|
|
PCHInfoCollector(LangOptions &LangOpt, HeaderSearch &HSI,
|
|
|
|
std::string &TargetTriple, std::string &Predefines,
|
|
|
|
unsigned &Counter)
|
|
|
|
: LangOpt(LangOpt), HSI(HSI), TargetTriple(TargetTriple),
|
|
|
|
Predefines(Predefines), Counter(Counter), NumHeaderInfos(0) {}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-20 16:08:23 +08:00
|
|
|
virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
|
|
|
|
LangOpt = LangOpts;
|
|
|
|
return false;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-11 08:52:11 +08:00
|
|
|
virtual bool ReadTargetTriple(llvm::StringRef Triple) {
|
2009-06-20 16:08:23 +08:00
|
|
|
TargetTriple = Triple;
|
|
|
|
return false;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-11 08:52:11 +08:00
|
|
|
virtual bool ReadPredefinesBuffer(llvm::StringRef PCHPredef,
|
2009-06-20 16:08:23 +08:00
|
|
|
FileID PCHBufferID,
|
2009-11-11 13:29:04 +08:00
|
|
|
llvm::StringRef OriginalFileName,
|
2009-06-20 16:08:23 +08:00
|
|
|
std::string &SuggestedPredefines) {
|
|
|
|
Predefines = PCHPredef;
|
|
|
|
return false;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-20 16:08:23 +08:00
|
|
|
virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI) {
|
|
|
|
HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-20 16:08:23 +08:00
|
|
|
virtual void ReadCounter(unsigned Value) {
|
|
|
|
Counter = Value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2009-09-04 02:19:54 +08:00
|
|
|
const std::string &ASTUnit::getOriginalSourceFileName() {
|
|
|
|
return dyn_cast<PCHReader>(Ctx->getExternalSource())->getOriginalSourceFile();
|
|
|
|
}
|
2009-06-20 16:08:23 +08:00
|
|
|
|
2009-10-16 06:23:48 +08:00
|
|
|
const std::string &ASTUnit::getPCHFileName() {
|
|
|
|
return dyn_cast<PCHReader>(Ctx->getExternalSource())->getFileName();
|
|
|
|
}
|
|
|
|
|
2009-06-20 16:08:23 +08:00
|
|
|
ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename,
|
2009-10-17 04:01:17 +08:00
|
|
|
std::string *ErrMsg,
|
2009-10-20 05:44:57 +08:00
|
|
|
DiagnosticClient *diagClient,
|
2009-10-17 08:34:24 +08:00
|
|
|
bool OnlyLocalDecls,
|
|
|
|
bool UseBumpAllocator) {
|
2009-10-20 05:44:57 +08:00
|
|
|
llvm::OwningPtr<ASTUnit> AST(new ASTUnit(diagClient));
|
2009-10-17 04:01:17 +08:00
|
|
|
AST->OnlyLocalDecls = OnlyLocalDecls;
|
2009-10-19 22:34:22 +08:00
|
|
|
AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager()));
|
2009-06-20 16:08:23 +08:00
|
|
|
|
|
|
|
// Gather Info for preprocessor construction later on.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-20 16:08:23 +08:00
|
|
|
LangOptions LangInfo;
|
|
|
|
HeaderSearch &HeaderInfo = *AST->HeaderInfo.get();
|
|
|
|
std::string TargetTriple;
|
|
|
|
std::string Predefines;
|
|
|
|
unsigned Counter;
|
|
|
|
|
2009-09-03 13:59:50 +08:00
|
|
|
llvm::OwningPtr<PCHReader> Reader;
|
2009-06-20 16:08:23 +08:00
|
|
|
llvm::OwningPtr<ExternalASTSource> Source;
|
|
|
|
|
2009-10-20 05:44:57 +08:00
|
|
|
Reader.reset(new PCHReader(AST->getSourceManager(), AST->getFileManager(),
|
|
|
|
AST->Diags));
|
2009-09-03 13:59:35 +08:00
|
|
|
Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple,
|
|
|
|
Predefines, Counter));
|
|
|
|
|
|
|
|
switch (Reader->ReadPCH(Filename)) {
|
2009-06-20 16:08:23 +08:00
|
|
|
case PCHReader::Success:
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-20 16:08:23 +08:00
|
|
|
case PCHReader::Failure:
|
2009-06-26 02:22:30 +08:00
|
|
|
case PCHReader::IgnorePCH:
|
2009-06-20 16:08:23 +08:00
|
|
|
if (ErrMsg)
|
|
|
|
*ErrMsg = "Could not load PCH file";
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-20 16:08:23 +08:00
|
|
|
// PCH loaded successfully. Now create the preprocessor.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-20 16:08:23 +08:00
|
|
|
// Get information about the target being compiled for.
|
2009-11-15 14:48:46 +08:00
|
|
|
//
|
|
|
|
// FIXME: This is broken, we should store the TargetOptions in the PCH.
|
|
|
|
TargetOptions TargetOpts;
|
|
|
|
TargetOpts.ABI = "";
|
|
|
|
TargetOpts.CPU = "";
|
|
|
|
TargetOpts.Features.clear();
|
|
|
|
TargetOpts.Triple = TargetTriple;
|
|
|
|
AST->Target.reset(TargetInfo::CreateTargetInfo(AST->Diags, TargetOpts));
|
2009-09-21 11:03:39 +08:00
|
|
|
AST->PP.reset(new Preprocessor(AST->Diags, LangInfo, *AST->Target.get(),
|
|
|
|
AST->getSourceManager(), HeaderInfo));
|
2009-06-20 16:08:23 +08:00
|
|
|
Preprocessor &PP = *AST->PP.get();
|
|
|
|
|
2009-09-21 11:03:47 +08:00
|
|
|
PP.setPredefines(Reader->getSuggestedPredefines());
|
2009-06-20 16:08:23 +08:00
|
|
|
PP.setCounterValue(Counter);
|
2009-09-03 13:59:35 +08:00
|
|
|
Reader->setPreprocessor(PP);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-20 16:08:23 +08:00
|
|
|
// Create and initialize the ASTContext.
|
|
|
|
|
|
|
|
AST->Ctx.reset(new ASTContext(LangInfo,
|
2009-09-21 11:03:39 +08:00
|
|
|
AST->getSourceManager(),
|
2009-06-20 16:08:23 +08:00
|
|
|
*AST->Target.get(),
|
|
|
|
PP.getIdentifierTable(),
|
|
|
|
PP.getSelectorTable(),
|
|
|
|
PP.getBuiltinInfo(),
|
2009-10-17 08:34:24 +08:00
|
|
|
/* FreeMemory = */ !UseBumpAllocator,
|
2009-06-20 16:08:23 +08:00
|
|
|
/* size_reserve = */0));
|
|
|
|
ASTContext &Context = *AST->Ctx.get();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-03 13:59:35 +08:00
|
|
|
Reader->InitializeContext(Context);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-20 16:08:23 +08:00
|
|
|
// Attach the PCH reader to the AST context as an external AST
|
|
|
|
// source, so that declarations will be deserialized from the
|
|
|
|
// PCH file as needed.
|
2009-09-03 13:59:35 +08:00
|
|
|
Source.reset(Reader.take());
|
2009-06-20 16:08:23 +08:00
|
|
|
Context.setExternalSource(Source);
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
return AST.take();
|
2009-06-20 16:08:23 +08:00
|
|
|
}
|
2009-12-01 17:51:01 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class NullAction : public ASTFrontendAction {
|
|
|
|
virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
|
|
|
|
llvm::StringRef InFile) {
|
|
|
|
return new ASTConsumer();
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual bool hasCodeCompletionSupport() const { return false; }
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTUnit *ASTUnit::LoadFromCompilerInvocation(const CompilerInvocation &CI,
|
|
|
|
Diagnostic &Diags,
|
|
|
|
bool OnlyLocalDecls,
|
|
|
|
bool UseBumpAllocator) {
|
|
|
|
// Create the compiler instance to use for building the AST.
|
|
|
|
CompilerInstance Clang(&llvm::getGlobalContext(), false);
|
|
|
|
llvm::OwningPtr<ASTUnit> AST;
|
|
|
|
NullAction Act;
|
|
|
|
|
|
|
|
Clang.getInvocation() = CI;
|
|
|
|
|
|
|
|
Clang.setDiagnostics(&Diags);
|
|
|
|
Clang.setDiagnosticClient(Diags.getClient());
|
|
|
|
|
|
|
|
// Create the target instance.
|
|
|
|
Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(),
|
|
|
|
Clang.getTargetOpts()));
|
|
|
|
if (!Clang.hasTarget())
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
// Inform the target of the language options.
|
|
|
|
//
|
|
|
|
// FIXME: We shouldn't need to do this, the target should be immutable once
|
|
|
|
// created. This complexity should be lifted elsewhere.
|
|
|
|
Clang.getTarget().setForcedLangOptions(Clang.getLangOpts());
|
|
|
|
|
|
|
|
assert(Clang.getFrontendOpts().Inputs.size() == 1 &&
|
|
|
|
"Invocation must have exactly one source file!");
|
|
|
|
assert(Clang.getFrontendOpts().Inputs[0].first != FrontendOptions::IK_AST &&
|
|
|
|
"FIXME: AST inputs not yet supported here!");
|
|
|
|
|
|
|
|
// Create the AST unit.
|
|
|
|
//
|
|
|
|
// FIXME: Use the provided diagnostic client.
|
|
|
|
AST.reset(new ASTUnit());
|
|
|
|
|
|
|
|
// Create a file manager object to provide access to and cache the filesystem.
|
|
|
|
Clang.setFileManager(&AST->getFileManager());
|
|
|
|
|
|
|
|
// Create the source manager.
|
|
|
|
Clang.setSourceManager(&AST->getSourceManager());
|
|
|
|
|
|
|
|
// Create the preprocessor.
|
|
|
|
Clang.createPreprocessor();
|
|
|
|
|
|
|
|
if (!Act.BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second,
|
|
|
|
/*IsAST=*/false))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
Act.Execute();
|
|
|
|
|
|
|
|
// Steal the created context and preprocessor, and take back the source and
|
|
|
|
// file managers.
|
|
|
|
AST->Ctx.reset(Clang.takeASTContext());
|
|
|
|
AST->PP.reset(Clang.takePreprocessor());
|
|
|
|
Clang.takeSourceManager();
|
|
|
|
Clang.takeFileManager();
|
|
|
|
|
|
|
|
Act.EndSourceFile();
|
|
|
|
|
|
|
|
Clang.takeDiagnosticClient();
|
|
|
|
Clang.takeDiagnostics();
|
|
|
|
|
|
|
|
return AST.take();
|
|
|
|
|
|
|
|
error:
|
|
|
|
Clang.takeSourceManager();
|
|
|
|
Clang.takeFileManager();
|
|
|
|
Clang.takeDiagnosticClient();
|
|
|
|
Clang.takeDiagnostics();
|
|
|
|
return 0;
|
|
|
|
}
|