xref: /llvm-project/clang/examples/PrintFunctionNames/PrintFunctionNames.cpp (revision a8b949883ed352b9b0851dbb7b39b8be05530ffe)
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"
19 using namespace clang;
20 
21 namespace {
22 
23 class PrintFunctionsConsumer : public ASTConsumer {
24 public:
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 
34 class PrintFunctionNamesAction : public PluginASTAction {
35 protected:
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 
56 static FrontendPluginRegistry::Add<PrintFunctionNamesAction>
57 X("print-fns", "print function names");
58