[cfe-dev] VisitUnaryOperator called twice in RecursiveASTVisitor when post-order traversal
Gennadiy Vikentiy via cfe-dev
cfe-dev at lists.llvm.org
Tue Nov 15 05:29:56 PST 2016
Hi,
I'm new to Clang LibTooling. I am using `RecursiveASTVisitor` to
traverse the AST in post-order (i.e. with `shouldTraversePostOrder()`
returning true). I am finding that one particular `Visit` function,
`VisitUnaryOperator` is called twice (other `Visit` functions seem to
be fine). In pre-order traversal, it is called once as expected. I'm
using the latest svn Clang. Am I missing something?
Here is minimal example which illustrates the problem (based on
[this]( http://eli.thegreenplace.net/2014/05/01/modern-source-to-source-transformation-with-clang-and-libtooling)
example)
#include <iostream>
#include "clang/AST/AST.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Frontend/ASTConsumers.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
using namespace clang;
using namespace clang::driver;
using namespace clang::tooling;
static llvm::cl::OptionCategory ToolingSampleCategory("Tooling Sample");
class MyASTVisitor : public RecursiveASTVisitor<MyASTVisitor> {
public:
MyASTVisitor() {
}
bool shouldTraversePostOrder() const {
// if returns false then "Inside VisitUnaryOperator" printed
once (expected)
// if returns true then "Inside VisitUnaryOperator" printed
twice (why?)
return true;
}
bool VisitUnaryOperator(UnaryOperator * s){
std::cout << "Inside VisitUnaryOperator" << std::endl;
return true;
}
};
class MyASTConsumer : public ASTConsumer {
public:
MyASTConsumer() : Visitor() {}
bool HandleTopLevelDecl(DeclGroupRef DR) override {
for (DeclGroupRef::iterator b = DR.begin(), e = DR.end(); b != e; ++b) {
Visitor.TraverseDecl(*b);
}
return true;
}
private:
MyASTVisitor Visitor;
};
class MyFrontendAction : public ASTFrontendAction {
public:
MyFrontendAction() {}
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
StringRef file) override {
return llvm::make_unique<MyASTConsumer>();
}
};
int main(int argc, const char **argv) {
CommonOptionsParser op(argc, argv, ToolingSampleCategory);
ClangTool Tool(op.getCompilations(), op.getSourcePathList());
return Tool.run(newFrontendActionFactory<MyFrontendAction>().get());
}
And here is the code snippet which this tool is run on:
int main() {
int x = -1;
return 0;
}
More information about the cfe-dev
mailing list