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/AST.h"
17*8675b4afSChandler Carruth #include "clang/AST/ASTConsumer.h"
182be96746SDaniel Dunbar #include "clang/Frontend/CompilerInstance.h"
196499e9c6SDaniel Dunbar #include "llvm/Support/raw_ostream.h"
206499e9c6SDaniel Dunbar using namespace clang;
216499e9c6SDaniel Dunbar 
226499e9c6SDaniel Dunbar namespace {
236499e9c6SDaniel Dunbar 
246499e9c6SDaniel Dunbar class PrintFunctionsConsumer : public ASTConsumer {
256499e9c6SDaniel Dunbar public:
269f39a765SDouglas Gregor   virtual bool HandleTopLevelDecl(DeclGroupRef DG) {
276499e9c6SDaniel Dunbar     for (DeclGroupRef::iterator i = DG.begin(), e = DG.end(); i != e; ++i) {
286499e9c6SDaniel Dunbar       const Decl *D = *i;
296499e9c6SDaniel Dunbar       if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
306499e9c6SDaniel Dunbar         llvm::errs() << "top-level-decl: \"" << ND->getNameAsString() << "\"\n";
316499e9c6SDaniel Dunbar     }
329f39a765SDouglas Gregor 
339f39a765SDouglas Gregor     return true;
346499e9c6SDaniel Dunbar   }
356499e9c6SDaniel Dunbar };
366499e9c6SDaniel Dunbar 
377c995e8fSDaniel Dunbar class PrintFunctionNamesAction : public PluginASTAction {
386499e9c6SDaniel Dunbar protected:
396499e9c6SDaniel Dunbar   ASTConsumer *CreateASTConsumer(CompilerInstance &CI, llvm::StringRef) {
406499e9c6SDaniel Dunbar     return new PrintFunctionsConsumer();
416499e9c6SDaniel Dunbar   }
427c995e8fSDaniel Dunbar 
432be96746SDaniel Dunbar   bool ParseArgs(const CompilerInstance &CI,
442be96746SDaniel Dunbar                  const std::vector<std::string>& args) {
452be96746SDaniel Dunbar     for (unsigned i = 0, e = args.size(); i != e; ++i) {
467c995e8fSDaniel Dunbar       llvm::errs() << "PrintFunctionNames arg = " << args[i] << "\n";
472be96746SDaniel Dunbar 
482be96746SDaniel Dunbar       // Example error handling.
492be96746SDaniel Dunbar       if (args[i] == "-an-error") {
50f42db7ceSEli Friedman         DiagnosticsEngine &D = CI.getDiagnostics();
512be96746SDaniel Dunbar         unsigned DiagID = D.getCustomDiagID(
52f42db7ceSEli Friedman           DiagnosticsEngine::Error, "invalid argument '" + args[i] + "'");
532be96746SDaniel Dunbar         D.Report(DiagID);
542be96746SDaniel Dunbar         return false;
552be96746SDaniel Dunbar       }
562be96746SDaniel Dunbar     }
577c995e8fSDaniel Dunbar     if (args.size() && args[0] == "help")
587c995e8fSDaniel Dunbar       PrintHelp(llvm::errs());
597c995e8fSDaniel Dunbar 
607c995e8fSDaniel Dunbar     return true;
617c995e8fSDaniel Dunbar   }
627c995e8fSDaniel Dunbar   void PrintHelp(llvm::raw_ostream& ros) {
637c995e8fSDaniel Dunbar     ros << "Help for PrintFunctionNames plugin goes here\n";
647c995e8fSDaniel Dunbar   }
657c995e8fSDaniel Dunbar 
666499e9c6SDaniel Dunbar };
676499e9c6SDaniel Dunbar 
686499e9c6SDaniel Dunbar }
696499e9c6SDaniel Dunbar 
7034e0f6cbSManuel Klimek static FrontendPluginRegistry::Add<PrintFunctionNamesAction>
716499e9c6SDaniel Dunbar X("print-fns", "print function names");
72