[cfe-dev] How do walk the AST? (Code within)
Adam Smalin
acidzombie24 at gmail.com
Thu Jun 16 22:10:42 PDT 2011
I couldnt get PrintFunctionNames working using visual studios due to the
static variable not registering the plugin. So i tried my own methods. I
found the LoadFromASTFile inside of wpa and basically scrapped it and copy
pasted some of the PrintFunctionNames example inside of it.
Now dont know if i am heading in the wrong direction. Currently the problem
is i dont know how to either get (or create?) the DeclGroupRef variable that
i must pass in.
I did a search for prettyprint and i also found TypePrinter. That just made
me more confused.
How might i list all structs (or at least top level) from an AST? I have the
code below. I dont know where to go from there.
#include "clang/Basic/FileManager.h"
#include "clang/Basic/SourceManager.h"
#include "clang/StaticAnalyzer/Frontend/CheckerRegistration.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/TransferFuncs.h"
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
#include "clang/StaticAnalyzer/Checkers/LocalCheckers.h"
#include "clang/Frontend/ASTUnit.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Index/CallGraph.h"
#include "clang/Index/Indexer.h"
#include "clang/Index/TranslationUnit.h"
#include "clang/Index/DeclReferenceMap.h"
#include "clang/Index/SelectorMap.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Basic/TargetInfo.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/raw_ostream.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/AST.h"
using namespace clang;
using namespace idx;
static llvm::cl::list<std::string> InputFilenames(llvm::cl::Positional,
llvm::cl::desc("<input AST files>"));
class PrintFunctionsConsumer : public ASTConsumer {
public:
virtual void 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";
}
}
};
int main(int argc, char **argv) {
llvm::cl::ParseCommandLineOptions(argc, argv, "clang-wpa");
std::vector<ASTUnit*> ASTUnits;
Program Prog;
Indexer Idxer(Prog);
if (InputFilenames.empty())
return 0;
DiagnosticOptions DiagOpts;
llvm::IntrusiveRefCntPtr<Diagnostic> Diags =
CompilerInstance::createDiagnostics(DiagOpts, argc, argv);
auto astunit = ASTUnit::LoadFromASTFile(argv[1], Diags,
FileSystemOptions(), false, 0, 0, true);
auto c = new PrintFunctionsConsumer;
c->Initialize(astunit->getASTContext());
//c->HandleTopLevelDecl
c->PrintStats();
return 0;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20110617/e54ce510/attachment.html>
More information about the cfe-dev
mailing list