16499e9c6SDaniel Dunbar //===- PrintFunctionNames.cpp ---------------------------------------------===//
26499e9c6SDaniel Dunbar //
36499e9c6SDaniel Dunbar //                     The LLVM Compiler Infrastructure
46499e9c6SDaniel Dunbar //
56499e9c6SDaniel Dunbar // This file is distributed under the University of Illinois Open Source
66499e9c6SDaniel Dunbar // License. See LICENSE.TXT for details.
76499e9c6SDaniel Dunbar //
86499e9c6SDaniel Dunbar //===----------------------------------------------------------------------===//
96499e9c6SDaniel Dunbar //
10520d1e6cSDaniel Dunbar // Example clang plugin which simply prints the names of all the top-level decls
11520d1e6cSDaniel Dunbar // in the input file.
126499e9c6SDaniel Dunbar //
136499e9c6SDaniel Dunbar //===----------------------------------------------------------------------===//
146499e9c6SDaniel Dunbar 
156499e9c6SDaniel Dunbar #include "clang/Frontend/FrontendPluginRegistry.h"
166499e9c6SDaniel Dunbar #include "clang/AST/ASTConsumer.h"
176499e9c6SDaniel Dunbar #include "clang/AST/AST.h"
186499e9c6SDaniel Dunbar #include "llvm/Support/raw_ostream.h"
196499e9c6SDaniel Dunbar using namespace clang;
206499e9c6SDaniel Dunbar 
216499e9c6SDaniel Dunbar namespace {
226499e9c6SDaniel Dunbar 
236499e9c6SDaniel Dunbar class PrintFunctionsConsumer : public ASTConsumer {
246499e9c6SDaniel Dunbar public:
256499e9c6SDaniel Dunbar   virtual void HandleTopLevelDecl(DeclGroupRef DG) {
266499e9c6SDaniel Dunbar     for (DeclGroupRef::iterator i = DG.begin(), e = DG.end(); i != e; ++i) {
276499e9c6SDaniel Dunbar       const Decl *D = *i;
286499e9c6SDaniel Dunbar       if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
296499e9c6SDaniel Dunbar         llvm::errs() << "top-level-decl: \"" << ND->getNameAsString() << "\"\n";
306499e9c6SDaniel Dunbar     }
316499e9c6SDaniel Dunbar   }
326499e9c6SDaniel Dunbar };
336499e9c6SDaniel Dunbar 
347c995e8fSDaniel Dunbar class PrintFunctionNamesAction : public PluginASTAction {
356499e9c6SDaniel Dunbar protected:
366499e9c6SDaniel Dunbar   ASTConsumer *CreateASTConsumer(CompilerInstance &CI, llvm::StringRef) {
376499e9c6SDaniel Dunbar     return new PrintFunctionsConsumer();
386499e9c6SDaniel Dunbar   }
397c995e8fSDaniel Dunbar 
407c995e8fSDaniel Dunbar   bool ParseArgs(const std::vector<std::string>& args) {
417c995e8fSDaniel Dunbar     for (unsigned i=0; i<args.size(); ++i)
427c995e8fSDaniel Dunbar       llvm::errs() << "PrintFunctionNames arg = " << args[i] << "\n";
437c995e8fSDaniel Dunbar     if (args.size() && args[0] == "help")
447c995e8fSDaniel Dunbar       PrintHelp(llvm::errs());
457c995e8fSDaniel Dunbar 
467c995e8fSDaniel Dunbar     return true;
477c995e8fSDaniel Dunbar   }
487c995e8fSDaniel Dunbar   void PrintHelp(llvm::raw_ostream& ros) {
497c995e8fSDaniel Dunbar     ros << "Help for PrintFunctionNames plugin goes here\n";
507c995e8fSDaniel Dunbar   }
517c995e8fSDaniel Dunbar 
526499e9c6SDaniel Dunbar };
536499e9c6SDaniel Dunbar 
546499e9c6SDaniel Dunbar }
556499e9c6SDaniel Dunbar 
56*a8b94988SDan Gohman static FrontendPluginRegistry::Add<PrintFunctionNamesAction>
576499e9c6SDaniel Dunbar X("print-fns", "print function names");
58