1*f4a2713aSLionel Sambuc //===- PrintFunctionNames.cpp ---------------------------------------------===// 2*f4a2713aSLionel Sambuc // 3*f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure 4*f4a2713aSLionel Sambuc // 5*f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source 6*f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details. 7*f4a2713aSLionel Sambuc // 8*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 9*f4a2713aSLionel Sambuc // 10*f4a2713aSLionel Sambuc // Example clang plugin which simply prints the names of all the top-level decls 11*f4a2713aSLionel Sambuc // in the input file. 12*f4a2713aSLionel Sambuc // 13*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 14*f4a2713aSLionel Sambuc 15*f4a2713aSLionel Sambuc #include "clang/Frontend/FrontendPluginRegistry.h" 16*f4a2713aSLionel Sambuc #include "clang/AST/AST.h" 17*f4a2713aSLionel Sambuc #include "clang/AST/ASTConsumer.h" 18*f4a2713aSLionel Sambuc #include "clang/Frontend/CompilerInstance.h" 19*f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h" 20*f4a2713aSLionel Sambuc using namespace clang; 21*f4a2713aSLionel Sambuc 22*f4a2713aSLionel Sambuc namespace { 23*f4a2713aSLionel Sambuc 24*f4a2713aSLionel Sambuc class PrintFunctionsConsumer : public ASTConsumer { 25*f4a2713aSLionel Sambuc public: 26*f4a2713aSLionel Sambuc virtual bool HandleTopLevelDecl(DeclGroupRef DG) { 27*f4a2713aSLionel Sambuc for (DeclGroupRef::iterator i = DG.begin(), e = DG.end(); i != e; ++i) { 28*f4a2713aSLionel Sambuc const Decl *D = *i; 29*f4a2713aSLionel Sambuc if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) 30*f4a2713aSLionel Sambuc llvm::errs() << "top-level-decl: \"" << ND->getNameAsString() << "\"\n"; 31*f4a2713aSLionel Sambuc } 32*f4a2713aSLionel Sambuc 33*f4a2713aSLionel Sambuc return true; 34*f4a2713aSLionel Sambuc } 35*f4a2713aSLionel Sambuc }; 36*f4a2713aSLionel Sambuc 37*f4a2713aSLionel Sambuc class PrintFunctionNamesAction : public PluginASTAction { 38*f4a2713aSLionel Sambuc protected: 39*f4a2713aSLionel Sambuc ASTConsumer *CreateASTConsumer(CompilerInstance &CI, llvm::StringRef) { 40*f4a2713aSLionel Sambuc return new PrintFunctionsConsumer(); 41*f4a2713aSLionel Sambuc } 42*f4a2713aSLionel Sambuc 43*f4a2713aSLionel Sambuc bool ParseArgs(const CompilerInstance &CI, 44*f4a2713aSLionel Sambuc const std::vector<std::string>& args) { 45*f4a2713aSLionel Sambuc for (unsigned i = 0, e = args.size(); i != e; ++i) { 46*f4a2713aSLionel Sambuc llvm::errs() << "PrintFunctionNames arg = " << args[i] << "\n"; 47*f4a2713aSLionel Sambuc 48*f4a2713aSLionel Sambuc // Example error handling. 49*f4a2713aSLionel Sambuc if (args[i] == "-an-error") { 50*f4a2713aSLionel Sambuc DiagnosticsEngine &D = CI.getDiagnostics(); 51*f4a2713aSLionel Sambuc unsigned DiagID = D.getCustomDiagID( 52*f4a2713aSLionel Sambuc DiagnosticsEngine::Error, "invalid argument '" + args[i] + "'"); 53*f4a2713aSLionel Sambuc D.Report(DiagID); 54*f4a2713aSLionel Sambuc return false; 55*f4a2713aSLionel Sambuc } 56*f4a2713aSLionel Sambuc } 57*f4a2713aSLionel Sambuc if (args.size() && args[0] == "help") 58*f4a2713aSLionel Sambuc PrintHelp(llvm::errs()); 59*f4a2713aSLionel Sambuc 60*f4a2713aSLionel Sambuc return true; 61*f4a2713aSLionel Sambuc } 62*f4a2713aSLionel Sambuc void PrintHelp(llvm::raw_ostream& ros) { 63*f4a2713aSLionel Sambuc ros << "Help for PrintFunctionNames plugin goes here\n"; 64*f4a2713aSLionel Sambuc } 65*f4a2713aSLionel Sambuc 66*f4a2713aSLionel Sambuc }; 67*f4a2713aSLionel Sambuc 68*f4a2713aSLionel Sambuc } 69*f4a2713aSLionel Sambuc 70*f4a2713aSLionel Sambuc static FrontendPluginRegistry::Add<PrintFunctionNamesAction> 71*f4a2713aSLionel Sambuc X("print-fns", "print function names"); 72