PrintFunctionNames.cpp revision 1890eb81a6ad9df6dee4d54d5031e1ea5c6e9ce7
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 "llvm/Support/raw_ostream.h"
19using namespace clang;
20
21namespace {
22
23class PrintFunctionsConsumer : public ASTConsumer {
24public:
25  virtual void HandleTopLevelDecl(DeclGroupRef DG) {
26    for (DeclGroupRef::iterator i = DG.begin(), e = DG.end(); i != e; ++i) {
27      const Decl *D = *i;
28      if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
29        llvm::errs() << "top-level-decl: \"" << ND->getNameAsString() << "\"\n";
30    }
31  }
32};
33
34class PrintFunctionNamesAction : public PluginASTAction {
35protected:
36  ASTConsumer *CreateASTConsumer(CompilerInstance &CI, llvm::StringRef) {
37    return new PrintFunctionsConsumer();
38  }
39
40  bool ParseArgs(const std::vector<std::string>& args) {
41    for (unsigned i=0; i<args.size(); ++i)
42      llvm::errs() << "PrintFunctionNames arg = " << args[i] << "\n";
43    if (args.size() && args[0] == "help")
44      PrintHelp(llvm::errs());
45
46    return true;
47  }
48  void PrintHelp(llvm::raw_ostream& ros) {
49    ros << "Help for PrintFunctionNames plugin goes here\n";
50  }
51
52};
53
54}
55
56static FrontendPluginRegistry::Add<PrintFunctionNamesAction>
57X("print-fns", "print function names");
58