#include "clang/AST/ASTContext.h" using namespace clang; TranslationUnitDecl* getTopLevelDecl(ASTContext& context) { return context.getTranslationUnitDecl(); }
#include "llvm/Support/raw_ostream.h" #include "clang/AST/ASTContext.h" using namespace clang; void printTopLevelDecl(ASTContext& context) { TranslationUnitDecl* tuDecl = context.getTranslationUnitDecl(); llvm::outs() << "Top-level declaration:\n"; for (auto decl : tuDecl->decls()) { decl->print(llvm::outs(), context.getPrintingPolicy()); llvm::outs() << "\n"; } }This example shows a function that prints all the declarations in the top-level declaration of the translation unit. It uses the `llvm::outs()` stream to print the declarations and the `context.getPrintingPolicy()` function to get the printing policy for the AST. Package/Library: Clang (part of LLVM)