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