xref: /minix3/external/bsd/llvm/dist/clang/examples/PrintFunctionNames/PrintFunctionNames.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===- PrintFunctionNames.cpp ---------------------------------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // Example clang plugin which simply prints the names of all the top-level decls
11f4a2713aSLionel Sambuc // in the input file.
12f4a2713aSLionel Sambuc //
13f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
14f4a2713aSLionel Sambuc 
15f4a2713aSLionel Sambuc #include "clang/Frontend/FrontendPluginRegistry.h"
16f4a2713aSLionel Sambuc #include "clang/AST/AST.h"
17f4a2713aSLionel Sambuc #include "clang/AST/ASTConsumer.h"
18f4a2713aSLionel Sambuc #include "clang/Frontend/CompilerInstance.h"
19f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
20f4a2713aSLionel Sambuc using namespace clang;
21f4a2713aSLionel Sambuc 
22f4a2713aSLionel Sambuc namespace {
23f4a2713aSLionel Sambuc 
24f4a2713aSLionel Sambuc class PrintFunctionsConsumer : public ASTConsumer {
25f4a2713aSLionel Sambuc public:
HandleTopLevelDecl(DeclGroupRef DG)26f4a2713aSLionel Sambuc   virtual bool HandleTopLevelDecl(DeclGroupRef DG) {
27f4a2713aSLionel Sambuc     for (DeclGroupRef::iterator i = DG.begin(), e = DG.end(); i != e; ++i) {
28f4a2713aSLionel Sambuc       const Decl *D = *i;
29f4a2713aSLionel Sambuc       if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
30f4a2713aSLionel Sambuc         llvm::errs() << "top-level-decl: \"" << ND->getNameAsString() << "\"\n";
31f4a2713aSLionel Sambuc     }
32f4a2713aSLionel Sambuc 
33f4a2713aSLionel Sambuc     return true;
34f4a2713aSLionel Sambuc   }
35f4a2713aSLionel Sambuc };
36f4a2713aSLionel Sambuc 
37f4a2713aSLionel Sambuc class PrintFunctionNamesAction : public PluginASTAction {
38f4a2713aSLionel Sambuc protected:
CreateASTConsumer(CompilerInstance & CI,llvm::StringRef)39*0a6a1f1dSLionel Sambuc   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
40*0a6a1f1dSLionel Sambuc                                                  llvm::StringRef) {
41*0a6a1f1dSLionel Sambuc     return llvm::make_unique<PrintFunctionsConsumer>();
42f4a2713aSLionel Sambuc   }
43f4a2713aSLionel Sambuc 
ParseArgs(const CompilerInstance & CI,const std::vector<std::string> & args)44f4a2713aSLionel Sambuc   bool ParseArgs(const CompilerInstance &CI,
45f4a2713aSLionel Sambuc                  const std::vector<std::string>& args) {
46f4a2713aSLionel Sambuc     for (unsigned i = 0, e = args.size(); i != e; ++i) {
47f4a2713aSLionel Sambuc       llvm::errs() << "PrintFunctionNames arg = " << args[i] << "\n";
48f4a2713aSLionel Sambuc 
49f4a2713aSLionel Sambuc       // Example error handling.
50f4a2713aSLionel Sambuc       if (args[i] == "-an-error") {
51f4a2713aSLionel Sambuc         DiagnosticsEngine &D = CI.getDiagnostics();
52*0a6a1f1dSLionel Sambuc         unsigned DiagID = D.getCustomDiagID(DiagnosticsEngine::Error,
53*0a6a1f1dSLionel Sambuc                                             "invalid argument '%0'");
54*0a6a1f1dSLionel Sambuc         D.Report(DiagID) << args[i];
55f4a2713aSLionel Sambuc         return false;
56f4a2713aSLionel Sambuc       }
57f4a2713aSLionel Sambuc     }
58f4a2713aSLionel Sambuc     if (args.size() && args[0] == "help")
59f4a2713aSLionel Sambuc       PrintHelp(llvm::errs());
60f4a2713aSLionel Sambuc 
61f4a2713aSLionel Sambuc     return true;
62f4a2713aSLionel Sambuc   }
PrintHelp(llvm::raw_ostream & ros)63f4a2713aSLionel Sambuc   void PrintHelp(llvm::raw_ostream& ros) {
64f4a2713aSLionel Sambuc     ros << "Help for PrintFunctionNames plugin goes here\n";
65f4a2713aSLionel Sambuc   }
66f4a2713aSLionel Sambuc 
67f4a2713aSLionel Sambuc };
68f4a2713aSLionel Sambuc 
69f4a2713aSLionel Sambuc }
70f4a2713aSLionel Sambuc 
71f4a2713aSLionel Sambuc static FrontendPluginRegistry::Add<PrintFunctionNamesAction>
72f4a2713aSLionel Sambuc X("print-fns", "print function names");
73