How do walk the AST? (Code within)

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::liststd::string InputFilenames(llvm::cl::Positional, llvm::cl::desc(“”));

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(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 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;
}