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" |
Daniel Dunbar | 9a69387 | 2009-11-15 00:27:43 +0000 | [diff] [blame] | 16 | #include "clang/AST/AST.h" |
Chandler Carruth | e0c6e93 | 2012-12-04 09:37:22 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTConsumer.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" |
| 20 | using namespace clang; |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | class PrintFunctionsConsumer : public ASTConsumer { |
| 25 | public: |
Pirama Arumuga Nainar | 33337ca | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 26 | bool HandleTopLevelDecl(DeclGroupRef DG) override { |
Daniel Dunbar | 9a69387 | 2009-11-15 00:27:43 +0000 | [diff] [blame] | 27 | for (DeclGroupRef::iterator i = DG.begin(), e = DG.end(); i != e; ++i) { |
| 28 | const Decl *D = *i; |
| 29 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
| 30 | llvm::errs() << "top-level-decl: \"" << ND->getNameAsString() << "\"\n"; |
| 31 | } |
Douglas Gregor | 1a02680 | 2011-11-19 19:22:13 +0000 | [diff] [blame] | 32 | |
| 33 | return true; |
Daniel Dunbar | 9a69387 | 2009-11-15 00:27:43 +0000 | [diff] [blame] | 34 | } |
Daniel Dunbar | f56a488 | 2010-08-02 15:31:28 +0000 | [diff] [blame] | 35 | }; |
Daniel Dunbar | 9a69387 | 2009-11-15 00:27:43 +0000 | [diff] [blame] | 36 | |
Daniel Dunbar | 3177aae | 2010-06-16 16:59:23 +0000 | [diff] [blame] | 37 | class PrintFunctionNamesAction : public PluginASTAction { |
Daniel Dunbar | 9a69387 | 2009-11-15 00:27:43 +0000 | [diff] [blame] | 38 | protected: |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 39 | std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, |
Pirama Arumuga Nainar | 33337ca | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 40 | llvm::StringRef) override { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 41 | return llvm::make_unique<PrintFunctionsConsumer>(); |
Daniel Dunbar | 9a69387 | 2009-11-15 00:27:43 +0000 | [diff] [blame] | 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, |
Pirama Arumuga Nainar | 33337ca | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 45 | const std::vector<std::string> &args) override { |
Daniel Dunbar | f56a488 | 2010-08-02 15:31:28 +0000 | [diff] [blame] | 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(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 52 | unsigned DiagID = D.getCustomDiagID(DiagnosticsEngine::Error, |
| 53 | "invalid argument '%0'"); |
| 54 | D.Report(DiagID) << args[i]; |
Daniel Dunbar | f56a488 | 2010-08-02 15:31:28 +0000 | [diff] [blame] | 55 | return false; |
| 56 | } |
| 57 | } |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 58 | if (!args.empty() && args[0] == "help") |
Daniel Dunbar | 3177aae | 2010-06-16 16:59:23 +0000 | [diff] [blame] | 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 | 981d1ec | 2012-04-26 08:46:12 +0000 | [diff] [blame] | 71 | static FrontendPluginRegistry::Add<PrintFunctionNamesAction> |
Daniel Dunbar | 9a69387 | 2009-11-15 00:27:43 +0000 | [diff] [blame] | 72 | X("print-fns", "print function names"); |