Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "clang/Driver/Options.h"
- #include "clang/AST/AST.h"
- #include "clang/AST/ASTContext.h"
- #include "clang/AST/ASTConsumer.h"
- #include "clang/AST/RecursiveASTVisitor.h"
- #include "clang/Frontend/ASTConsumers.h"
- #include "clang/Frontend/FrontendActions.h"
- #include "clang/Frontend/CompilerInstance.h"
- #include "clang/Tooling/CommonOptionsParser.h"
- #include "clang/Tooling/Tooling.h"
- #include "clang/Rewrite/Core/Rewriter.h"
- #include "clang/Analysis/Analyses/LiveVariables.h"
- #include "clang/Analysis/CFG.h"
- #include "clang/Analysis/AnalysisContext.h"
- using namespace std;
- using namespace clang;
- using namespace clang::driver;
- using namespace clang::tooling;
- using namespace llvm;
- Rewriter rewriter;
- int numFunctions = 0;
- int numVarDecl = 0;
- class ExampleVisitor : public RecursiveASTVisitor<ExampleVisitor> {
- private:
- ASTContext *astContext; // used for getting additional AST info
- public:
- // initialize private members
- explicit ExampleVisitor(CompilerInstance *CI) : astContext(&(CI->getASTContext())) {
- rewriter.setSourceMgr(astContext->getSourceManager(), astContext->getLangOpts());
- }
- virtual bool VisitDecl(Decl *D) {
- errs() << numVarDecl++ << "\n";
- // if (D->hasBody()) {
- // Stmt *S = D->getBody();
- // errs() << S << "\n";
- // S->getLocStart().print(errs(), astContext->getSourceManager());
- // errs() << "\n";
- // }
- D->getLocStart().print(errs(), astContext->getSourceManager());
- errs() << "\n";
- return true;
- }
- virtual bool VisitFunctionDecl(FunctionDecl *func) {
- errs() << "Inside " << ++numFunctions << " " << func->getNameInfo().getName().getAsString() << "\n";
- // FIXME: This detects CFGBlocks in given source file
- // correctly, but does not give correct liveness values
- clang::AnalysisDeclContextManager *ADCM = new clang::AnalysisDeclContextManager(false, true, true, true, true, true);
- clang::AnalysisDeclContext *func_ADC = ADCM->getContext(func);
- clang::LiveVariables *func_LV = clang::LiveVariables::computeLiveness(*func_ADC, false);
- clang::LiveVariables::Observer *obs = new clang::LiveVariables::Observer();
- func_LV->runOnAllBlocks(*obs);
- func_LV->dumpBlockLiveness((func_ADC->getASTContext()).getSourceManager());
- return true;
- }
- };
- class ExampleASTConsumer : public ASTConsumer {
- private:
- ExampleVisitor *visitor; // doesn't have to be private
- public:
- // override the constructor in order to pass CI
- explicit ExampleASTConsumer(CompilerInstance *CI)
- : visitor(new ExampleVisitor(CI)) // initialize the visitor
- { }
- // override this to call our ExampleVisitor on the entire source file
- virtual void HandleTranslationUnit(ASTContext &Context) {
- /* we can use ASTContext to get the TranslationUnitDecl, which is
- a single Decl that collectively represents the entire source file */
- visitor->TraverseDecl(Context.getTranslationUnitDecl());
- }
- };
- class ExampleFrontendAction : public ASTFrontendAction {
- public:
- virtual std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, StringRef file) {
- return std::unique_ptr<ASTConsumer>(new ExampleASTConsumer(&CI)); // pass CI pointer to ASTConsumer
- }
- };
- int main(int argc, const char **argv) {
- // parse the command-line args passed to your code
- // CommonOptionsParser op(argc, argv);
- cl::OptionCategory my_tool_category("my tool option");
- clang::tooling::CommonOptionsParser op(argc, argv, my_tool_category);
- // create a new Clang Tool instance (a LibTooling environment)
- ClangTool Tool(op.getCompilations(), op.getSourcePathList());
- // run the Clang Tool, creating a new FrontendAction (explained below)
- int result = Tool.run(newFrontendActionFactory<ExampleFrontendAction>().get());
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement