Here is the code snippet I'm using:
#include <iostream>
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/Casting.h"
#include "clang/Frontend/DiagnosticOptions.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Basic/LangOptions.h"
#include "clang/Basic/FileSystemOptions.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Lex/HeaderSearch.h"
#include "clang/Basic/FileManager.h"
#include "clang/Frontend/HeaderSearchOptions.h"
#include "clang/Frontend/Utils.h"
#include "clang/Basic/TargetOptions.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Frontend/PreprocessorOptions.h"
#include "clang/Frontend/FrontendOptions.h"
#include "clang/Basic/IdentifierTable.h"
#include "clang/Basic/Builtins.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/Sema/Sema.h"
#include "clang/AST/DeclBase.h"
#include "clang/AST/Type.h"
#include "clang/AST/Decl.h"
#include "clang/Sema/Lookup.h"
#include "clang/Sema/Ownership.h"
#include "clang/AST/DeclGroup.h"
#include "clang/Parse/Parser.h"
#include "clang/Parse/ParseAST.h"
#include "clang/Frontend/CompilerInstance.h"
class MyASTConsumer : public clang::ASTConsumer
{
public:
MyASTConsumer() : clang::ASTConsumer() { }
virtual ~MyASTConsumer() { }
virtual bool HandleTopLevelDecl( clang::DeclGroupRef d)
{
static int count = 0;
clang::DeclGroupRef::iterator it;
for( it = d.begin(); it != d.end(); it++)
{
count++;
clang::VarDecl *vd = llvm::dyn_cast<clang::VarDecl>(*it);
if(!vd)
{
continue;
}
if( vd->isFileVarDecl() && !vd->hasExternalStorage() )
{
std::cerr << "Read top-level variable decl: '";
std::cerr << vd->getDeclName().getAsString() ;
std::cerr << std::endl;
}
}
return true;
}
};
int main()
{
clang::DiagnosticOptions diagnosticOptions;
diagnosticOptions.ShowColors=1;
diagnosticOptions.ErrorLimit =10;
diagnosticOptions.ShowOptionNames=1;
clang::TextDiagnosticPrinter *pTextDiagnosticPrinter =
new clang::TextDiagnosticPrinter(
llvm::outs(),
diagnosticOptions);
llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> pDiagIDs;
clang::DiagnosticsEngine *pDiagnosticsEngine =
new clang::DiagnosticsEngine(pDiagIDs, pTextDiagnosticPrinter);
clang::FileSystemOptions fileSystemOptions;
clang::FileManager fileManager(fileSystemOptions);
clang::SourceManager sourceManager(
*pDiagnosticsEngine,
fileManager);
clang::HeaderSearchOptions headerSearchOptions;
headerSearchOptions.ResourceDir = "/home/user/klall/tools/clang_install/bin/../lib/clang/3.2";
headerSearchOptions.ModuleCachePath = "/var/tmp/clang-module-cache";
headerSearchOptions.Verbose =true;
clang::TargetOptions targetOptions;
targetOptions.Triple = "x86_64-unknown-linux-gnu";
targetOptions.CPU = "x86-64";
clang::TargetInfo *pTargetInfo =
clang::TargetInfo::CreateTargetInfo(
*pDiagnosticsEngine,
targetOptions);
clang::CompilerInstance compInst;
compInst.getTargetOpts() = targetOptions;
clang::HeaderSearchOptions &headerSearchOpts = compInst.getHeaderSearchOpts();
headerSearchOpts = headerSearchOptions;
clang::CompilerInvocation &compInvocation = compInst.getInvocation();
clang::LangOptions languageOptions;
languageOptions.CPlusPlus = 1;
compInvocation.setLangDefaults(languageOptions, clang::IK_CXX, clang::LangStandard::lang_gnucxx98);
clang::HeaderSearch headerSearch(fileManager,
*pDiagnosticsEngine,
languageOptions,
pTargetInfo);
clang::FrontendOptions & frontendOpts = compInvocation.getFrontendOpts();
frontendOpts.DisableFree=1;
frontendOpts.ShowHelp=1;
frontendOpts.ProgramAction = clang::frontend::ParseSyntaxOnly;
clang::CodeGenOptions & codeGenOpts = compInvocation.getCodeGenOpts ();
codeGenOpts.RelaxAll = 1;
codeGenOpts.RelocationModel = "static";
codeGenOpts.DisableFPElim = 1;
codeGenOpts.AsmVerbose = 1;
codeGenOpts.CXXCtorDtorAliases= 1;
codeGenOpts.UnwindTables = 1;
codeGenOpts.OmitLeafFramePointer = 1;
codeGenOpts.StackRealignment = 1;
std::vector<std::string> res;
compInvocation.toArgs (res);
std::vector<std::string>::iterator it;
clang::Preprocessor preprocessor(
*pDiagnosticsEngine,
languageOptions,
pTargetInfo,
sourceManager,
headerSearch,
compInst);
preprocessor.getBuiltinInfo().InitializeBuiltins
(preprocessor.getIdentifierTable(),
languageOptions);
clang::PreprocessorOptions preprocessorOptions;
clang::InitializePreprocessor(
preprocessor,
preprocessorOptions,
headerSearchOptions,
frontendOpts);
const clang::FileEntry *pFile = fileManager.getFile("test.cpp");
sourceManager.createMainFileID(pFile);
const clang::TargetInfo &targetInfo = *pTargetInfo;
clang::IdentifierTable identifierTable(languageOptions);
clang::SelectorTable selectorTable;
clang::Builtin::Context builtinContext;
builtinContext.InitializeTarget(targetInfo);
builtinContext.InitializeBuiltins(identifierTable, languageOptions);
llvm::SmallVector<const char *, 32> builtinNames;
builtinContext.GetBuiltinNames(builtinNames, languageOptions.NoBuiltin);
clang::ASTContext astContext(
languageOptions,
sourceManager,
pTargetInfo,
identifierTable,
selectorTable,
builtinContext,
0 /* size_reserve*/);
MyASTConsumer astConsumer;
clang::Sema sema(
preprocessor,
astContext,
astConsumer);
pTextDiagnosticPrinter->BeginSourceFile(languageOptions, &preprocessor);
clang::ParseAST (preprocessor, &astConsumer, astContext);
pTextDiagnosticPrinter->EndSourceFile();
return 0;
}