[cfe-dev] Parsing C++ code to an AST and processing the result
Russell Wallace via cfe-dev
cfe-dev at lists.llvm.org
Fri Dec 4 04:15:14 PST 2015
I'm trying to parse C++ code to an abstract syntax tree and do something
with the result. (Basically I'm trying to create a DSL that has the syntax
and denotational semantics of a subset of C++, but different operational
semantics.)
To parse C++ with default compiler options and the ability to add more on
the command line and/or use a compilation database, I'm going by
http://clang.llvm.org/docs/LibASTMatchersTutorial.html which gets as far as
int main(int argc, const char **argv) {
CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
ClangTool Tool(OptionsParser.getCompilations(),
OptionsParser.getSourcePathList());
LoopPrinter Printer;
MatchFinder Finder;
Finder.addMatcher(LoopMatcher, &Printer);
return Tool.run(newFrontendActionFactory(&Finder).get());
}
The only thing not applicable here is that the tutorial is about using the
pattern matching facility to match fragments of code wherever they might
occur in the AST, whereas I'm looking to recur over the entire AST. The
ideal would be if I could just say 'parse, give me the AST and I'll take it
from there'. Is there an easy way to do that?
An alternative possibility would be to use an AST visitor to match function
and other declarations at the top level and go from there.
http://clang.llvm.org/docs/RAVFrontendAction.html discusses this, getting
as far as
int main(int argc, char **argv) {
if (argc > 1) {
clang::tooling::runToolOnCode(new FindNamedClassAction, argv[1]);
}
}
However, this only parses code fragments passed as literal text. Is there a
way to combine these and get the features of CommonOptionsParser and
ClangTool but run over the entire AST rather than using local pattern
matching?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20151204/578beb79/attachment.html>
More information about the cfe-dev
mailing list