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"
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:
266499e9c6SDaniel Dunbar   virtual void 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     }
326499e9c6SDaniel Dunbar   }
336499e9c6SDaniel Dunbar };
346499e9c6SDaniel Dunbar 
357c995e8fSDaniel Dunbar class PrintFunctionNamesAction : public PluginASTAction {
366499e9c6SDaniel Dunbar protected:
376499e9c6SDaniel Dunbar   ASTConsumer *CreateASTConsumer(CompilerInstance &CI, llvm::StringRef) {
386499e9c6SDaniel Dunbar     return new PrintFunctionsConsumer();
396499e9c6SDaniel Dunbar   }
407c995e8fSDaniel Dunbar 
412be96746SDaniel Dunbar   bool ParseArgs(const CompilerInstance &CI,
422be96746SDaniel Dunbar                  const std::vector<std::string>& args) {
432be96746SDaniel Dunbar     for (unsigned i = 0, e = args.size(); i != e; ++i) {
447c995e8fSDaniel Dunbar       llvm::errs() << "PrintFunctionNames arg = " << args[i] << "\n";
452be96746SDaniel Dunbar 
462be96746SDaniel Dunbar       // Example error handling.
472be96746SDaniel Dunbar       if (args[i] == "-an-error") {
48*f42db7ceSEli Friedman         DiagnosticsEngine &D = CI.getDiagnostics();
492be96746SDaniel Dunbar         unsigned DiagID = D.getCustomDiagID(
50*f42db7ceSEli Friedman           DiagnosticsEngine::Error, "invalid argument '" + args[i] + "'");
512be96746SDaniel Dunbar         D.Report(DiagID);
522be96746SDaniel Dunbar         return false;
532be96746SDaniel Dunbar       }
542be96746SDaniel Dunbar     }
557c995e8fSDaniel Dunbar     if (args.size() && args[0] == "help")
567c995e8fSDaniel Dunbar       PrintHelp(llvm::errs());
577c995e8fSDaniel Dunbar 
587c995e8fSDaniel Dunbar     return true;
597c995e8fSDaniel Dunbar   }
607c995e8fSDaniel Dunbar   void PrintHelp(llvm::raw_ostream& ros) {
617c995e8fSDaniel Dunbar     ros << "Help for PrintFunctionNames plugin goes here\n";
627c995e8fSDaniel Dunbar   }
637c995e8fSDaniel Dunbar 
646499e9c6SDaniel Dunbar };
656499e9c6SDaniel Dunbar 
666499e9c6SDaniel Dunbar }
676499e9c6SDaniel Dunbar 
68a8b94988SDan Gohman static FrontendPluginRegistry::Add<PrintFunctionNamesAction>
696499e9c6SDaniel Dunbar X("print-fns", "print function names");
70