Created
July 12, 2012 17:50
-
-
Save anonymous/3099625 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifdef _MSC_VER | |
# pragma warning (push) | |
# pragma warning (disable: 4146 4800 4345 4244 4355 4624 4267 4244) | |
#endif | |
#include "llvm/Config/config.h" | |
#include "llvm/ADT/ArrayRef.h" | |
#include "llvm/ADT/DenseSet.h" | |
#include "llvm/ADT/SetOperations.h" | |
#include "llvm/ADT/SmallString.h" | |
#include "llvm/ADT/SmallVector.h" | |
#include "llvm/ADT/OwningPtr.h" | |
#include "llvm/ADT/Statistic.h" | |
#include "llvm/ADT/StringExtras.h" | |
#include "llvm/ADT/StringRef.h" | |
#include "llvm/Support/ErrorHandling.h" | |
#include "llvm/Support/FileSystem.h" | |
#include "llvm/Support/ManagedStatic.h" | |
#include "llvm/Support/MemoryBuffer.h" | |
#include "llvm/Support/PrettyStackTrace.h" | |
#include "llvm/Support/Regex.h" | |
#include "llvm/Support/Timer.h" | |
#include "llvm/Support/raw_ostream.h" | |
#include "llvm/Support/Host.h" | |
#include "llvm/Support/Path.h" | |
#include "llvm/Support/Program.h" | |
#include "llvm/Support/Signals.h" | |
#include "llvm/Support/TargetRegistry.h" | |
#include "llvm/Support/TargetSelect.h" | |
#include "llvm/Support/system_error.h" | |
#include "llvm/Support/TimeValue.h" | |
#include "clang/Basic/FileManager.h" | |
#include "clang/Driver/Arg.h" | |
#include "clang/Driver/ArgList.h" | |
#include "clang/Driver/CC1Options.h" | |
#include "clang/Driver/DriverDiagnostic.h" | |
#include "clang/Driver/OptTable.h" | |
#include "clang/Driver/Compilation.h" | |
#include "clang/Driver/Driver.h" | |
#include "clang/Driver/Option.h" | |
#include "clang/AST/AST.h" | |
#include "clang/AST/ASTConsumer.h" | |
#include "clang/AST/DeclVisitor.h" | |
#include "clang/AST/RecursiveASTVisitor.h" | |
#include "clang/AST/StmtVisitor.h" | |
#include "clang/Frontend/DiagnosticOptions.h" | |
#include "clang/Frontend/TextDiagnosticPrinter.h" | |
#include "clang/Frontend/CompilerInstance.h" | |
#include "clang/Frontend/CompilerInvocation.h" | |
#include "clang/Frontend/FrontendDiagnostic.h" | |
#include "clang/Frontend/TextDiagnosticBuffer.h" | |
#include "clang/Frontend/FrontendActions.h" | |
#include "clang/Frontend/FrontendPluginRegistry.h" | |
#include "clang/Rewrite/Rewriter.h" | |
#include "clang/Basic/SourceManager.h" | |
#include "clang/Lex/Preprocessor.h" | |
#include "clang/Lex/Pragma.h" | |
#include "clang/Parse/ParseDiagnostic.h" | |
#ifdef _MSC_VER | |
# pragma warning (pop) | |
#endif | |
#pragma comment (lib, "lib/clangFrontend-debug.lib") | |
#pragma comment (lib, "lib/clangDriver-debug.lib") | |
#pragma comment (lib, "lib/clangBasic-debug.lib") | |
#pragma comment (lib, "lib/clangEdit-debug.lib") | |
#pragma comment (lib, "lib/clangLex-debug.lib") | |
#pragma comment (lib, "lib/clangParse-debug.lib") | |
#pragma comment (lib, "lib/clangSema-debug.lib") | |
#pragma comment (lib, "lib/clangAST-debug.lib") | |
#pragma comment (lib, "lib/clangRewrite-debug.lib") | |
#pragma comment (lib, "lib/clangSerialization-debug.lib") | |
#pragma comment (lib, "lib/clangAnalysis-debug.lib") | |
#pragma comment (lib, "lib/LLVMARMAsmParser-debug.lib") | |
#pragma comment (lib, "lib/LLVMARMAsmPrinter-debug.lib") | |
#pragma comment (lib, "lib/LLVMARMDesc-debug.lib") | |
#pragma comment (lib, "lib/LLVMARMInfo-debug.lib") | |
#pragma comment (lib, "lib/LLVMMC-debug.lib") | |
#pragma comment (lib, "lib/LLVMMCParser-debug.lib") | |
#pragma comment (lib, "lib/LLVMSupport-debug.lib") | |
#pragma comment (lib, "lib/LLVMX86AsmParser-debug.lib") | |
#pragma comment (lib, "lib/LLVMX86AsmPrinter-debug.lib") | |
#pragma comment (lib, "lib/LLVMX86Desc-debug.lib") | |
#pragma comment (lib, "lib/LLVMX86Info-debug.lib") | |
#pragma comment (lib, "lib/LLVMX86Utils-debug.lib") | |
#pragma comment (lib, "lib/LLVMCore-debug.lib") | |
using namespace clang; | |
class MyVisitor : public RecursiveASTVisitor<MyVisitor> | |
{ | |
SourceManager& SM; | |
public: | |
MyVisitor(SourceManager& SM) : SM(SM) {} | |
bool VisitStmt(clang::Stmt* s) | |
{ | |
s->dump(); | |
s->getLocStart().dump(SM); | |
llvm::errs() << " <=> "; | |
s->getLocEnd().dump(SM); | |
llvm::errs() << "\n"; | |
return true; | |
} | |
}; | |
class MyConsumer : public ASTConsumer | |
{ | |
public: | |
virtual bool HandleTopLevelDecl(DeclGroupRef DG) | |
{ | |
for (DeclGroupRef::iterator i = DG.begin(), e = DG.end(); i != e; ++i) | |
{ | |
const Decl *D = *i; | |
if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) | |
{ | |
llvm::errs() << "top-level-decl: " << ND->getNameAsString() << "\n"; | |
MyVisitor v(ND->getASTContext().getSourceManager()); | |
v.TraverseDecl((Decl*)ND); | |
} | |
} | |
return true; | |
} | |
}; | |
class MyAction : public clang::ASTFrontendAction | |
{ | |
public: | |
virtual clang::ASTConsumer* CreateASTConsumer(clang::CompilerInstance& compiler, llvm::StringRef inputFile) | |
{ | |
return new MyConsumer(); | |
} | |
}; | |
int main() | |
{ | |
const char* args[] = { "a.cpp" }; | |
CompilerInstance compiler; | |
llvm::IntrusiveRefCntPtr<DiagnosticIDs> diagID(new DiagnosticIDs()); | |
TextDiagnosticBuffer* diagsBuffer = new TextDiagnosticBuffer(); | |
DiagnosticsEngine diags(diagID, diagsBuffer); | |
CompilerInvocation::CreateFromArgs(compiler.getInvocation(), args, args + 1, diags); | |
compiler.createDiagnostics(1, args); | |
if (!compiler.hasDiagnostics()) | |
{ | |
return false; | |
} | |
diagsBuffer->FlushDiagnostics(compiler.getDiagnostics()); | |
if (!compiler.getDiagnostics().hasErrorOccurred()) | |
{ | |
MyAction action; | |
compiler.ExecuteAction(action); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment