[cfe-dev] Access live variable analysis while writing a refactring tool
Pei-Shiang Hung
pshung807 at gmail.com
Thu Apr 23 00:49:42 PDT 2015
Dear all,
I'm writing a code refactoring tool using libtooling.
I would like to access live variable variable once certain AST nodes
are matched.
For instance,
int add (int n)
{
int x = 10;
int y = 5;
if (n > 7)
return x;
return y;
}
I would like to know live variable information.
Please refer to the following code.
class LiveObserver : public LiveVariables::Observer {
public:
void observeStmt(const Stmt *S, const CFGBlock *block,
const LiveVariables::LivenessValues &Live) override {
}
};
In matcher callback,
if (const FunctionDecl *foo =
Result.Nodes.getNodeAs<clang::FunctionDecl>("foo")) {
AnalysisDeclContextManager manager;
AnalysisDeclContext* context = manager.getContext(main);
CFG *cfg = context->getCFG();
Livevariables *LV = LiveVariables::computeLiveness(*context, true);
if (context->isCFGBuilt())
errs() << "CFG is built\n";
else
errs() << "CFG is not built\n";
LiveObserver obs;
LV->runOnAllBlocks(obs);
LV->dumpBlockLiveness((context->getASTContext()).getSourceManager());
}
However, the result tells
CFG is not built
[ B0 (live variables at block exit) ]
[ B1 (live variables at block exit) ]
[ B2 (live variables at block exit) ]
[ B3 (live variables at block exit) ]
[ B4 (live variables at block exit) ]
This result is different from clang -cc1 -analyze
-analyzer-checker=debug.DumpLiveVars
[ B0 (live variables at block exit) ]
[ B1 (live variables at block exit) ]
[ B2 (live variables at block exit) ]
[ B3 (live variables at block exit) ]
x <test/test_wrapper_04.c:4:7>
y <test/test_wrapper_04.c:5:7>
[ B4 (live variables at block exit) ]
n <test/test_wrapper_04.c:2:14>
I have no idea why CFG is not built and no live information available.
Please give a direction. Thanks all.
Best regards.
More information about the cfe-dev
mailing list