Daniel Dunbar | 9a69387 | 2009-11-15 00:27:43 +0000 | [diff] [blame] | 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 | // |
Daniel Dunbar | dd63b28 | 2009-12-11 23:04:35 +0000 | [diff] [blame] | 10 | // Example clang plugin which simply prints the names of all the top-level decls |
| 11 | // in the input file. |
Daniel Dunbar | 9a69387 | 2009-11-15 00:27:43 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Frontend/FrontendPluginRegistry.h" |
| 16 | #include "clang/AST/ASTConsumer.h" |
| 17 | #include "clang/AST/AST.h" |
Daniel Dunbar | f56a488 | 2010-08-02 15:31:28 +0000 | [diff] [blame] | 18 | #include "clang/Frontend/CompilerInstance.h" |
Daniel Dunbar | 9a69387 | 2009-11-15 00:27:43 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
Manuel Klimek | d9ed0fd | 2012-04-26 08:35:39 +0000 | [diff] [blame] | 20 | #include "clang/Frontend/FrontendActions.h" |
Daniel Dunbar | 9a69387 | 2009-11-15 00:27:43 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | class PrintFunctionsConsumer : public ASTConsumer { |
| 26 | public: |
Douglas Gregor | 1a02680 | 2011-11-19 19:22:13 +0000 | [diff] [blame] | 27 | virtual bool HandleTopLevelDecl(DeclGroupRef DG) { |
Daniel Dunbar | 9a69387 | 2009-11-15 00:27:43 +0000 | [diff] [blame] | 28 | for (DeclGroupRef::iterator i = DG.begin(), e = DG.end(); i != e; ++i) { |
| 29 | const Decl *D = *i; |
| 30 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
| 31 | llvm::errs() << "top-level-decl: \"" << ND->getNameAsString() << "\"\n"; |
| 32 | } |
Douglas Gregor | 1a02680 | 2011-11-19 19:22:13 +0000 | [diff] [blame] | 33 | |
| 34 | return true; |
Daniel Dunbar | 9a69387 | 2009-11-15 00:27:43 +0000 | [diff] [blame] | 35 | } |
Daniel Dunbar | f56a488 | 2010-08-02 15:31:28 +0000 | [diff] [blame] | 36 | }; |
Daniel Dunbar | 9a69387 | 2009-11-15 00:27:43 +0000 | [diff] [blame] | 37 | |
Daniel Dunbar | 3177aae | 2010-06-16 16:59:23 +0000 | [diff] [blame] | 38 | class PrintFunctionNamesAction : public PluginASTAction { |
Daniel Dunbar | 9a69387 | 2009-11-15 00:27:43 +0000 | [diff] [blame] | 39 | protected: |
| 40 | ASTConsumer *CreateASTConsumer(CompilerInstance &CI, llvm::StringRef) { |
| 41 | return new PrintFunctionsConsumer(); |
| 42 | } |
Daniel Dunbar | 3177aae | 2010-06-16 16:59:23 +0000 | [diff] [blame] | 43 | |
Daniel Dunbar | f56a488 | 2010-08-02 15:31:28 +0000 | [diff] [blame] | 44 | bool ParseArgs(const CompilerInstance &CI, |
| 45 | const std::vector<std::string>& args) { |
| 46 | for (unsigned i = 0, e = args.size(); i != e; ++i) { |
Daniel Dunbar | 3177aae | 2010-06-16 16:59:23 +0000 | [diff] [blame] | 47 | llvm::errs() << "PrintFunctionNames arg = " << args[i] << "\n"; |
Daniel Dunbar | f56a488 | 2010-08-02 15:31:28 +0000 | [diff] [blame] | 48 | |
| 49 | // Example error handling. |
| 50 | if (args[i] == "-an-error") { |
Eli Friedman | d3c1661 | 2011-09-27 18:33:47 +0000 | [diff] [blame] | 51 | DiagnosticsEngine &D = CI.getDiagnostics(); |
Daniel Dunbar | f56a488 | 2010-08-02 15:31:28 +0000 | [diff] [blame] | 52 | unsigned DiagID = D.getCustomDiagID( |
Eli Friedman | d3c1661 | 2011-09-27 18:33:47 +0000 | [diff] [blame] | 53 | DiagnosticsEngine::Error, "invalid argument '" + args[i] + "'"); |
Daniel Dunbar | f56a488 | 2010-08-02 15:31:28 +0000 | [diff] [blame] | 54 | D.Report(DiagID); |
| 55 | return false; |
| 56 | } |
| 57 | } |
Daniel Dunbar | 3177aae | 2010-06-16 16:59:23 +0000 | [diff] [blame] | 58 | if (args.size() && args[0] == "help") |
| 59 | PrintHelp(llvm::errs()); |
| 60 | |
| 61 | return true; |
| 62 | } |
Daniel Dunbar | f56a488 | 2010-08-02 15:31:28 +0000 | [diff] [blame] | 63 | void PrintHelp(llvm::raw_ostream& ros) { |
Daniel Dunbar | 3177aae | 2010-06-16 16:59:23 +0000 | [diff] [blame] | 64 | ros << "Help for PrintFunctionNames plugin goes here\n"; |
| 65 | } |
| 66 | |
Daniel Dunbar | 9a69387 | 2009-11-15 00:27:43 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | } |
| 70 | |
Manuel Klimek | d9ed0fd | 2012-04-26 08:35:39 +0000 | [diff] [blame] | 71 | static FrontendPluginRegistry::Add<SyntaxOnlyAction> |
Daniel Dunbar | 9a69387 | 2009-11-15 00:27:43 +0000 | [diff] [blame] | 72 | X("print-fns", "print function names"); |