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" |
| 20 | using namespace clang; |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | class PrintFunctionsConsumer : public ASTConsumer { |
| 25 | public: |
| 26 | virtual void HandleTopLevelDecl(DeclGroupRef DG) { |
| 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 | } |
| 32 | } |
Daniel Dunbar | f56a488 | 2010-08-02 15:31:28 +0000 | [diff] [blame] | 33 | }; |
Daniel Dunbar | 9a69387 | 2009-11-15 00:27:43 +0000 | [diff] [blame] | 34 | |
Daniel Dunbar | 3177aae | 2010-06-16 16:59:23 +0000 | [diff] [blame] | 35 | class PrintFunctionNamesAction : public PluginASTAction { |
Daniel Dunbar | 9a69387 | 2009-11-15 00:27:43 +0000 | [diff] [blame] | 36 | protected: |
| 37 | ASTConsumer *CreateASTConsumer(CompilerInstance &CI, llvm::StringRef) { |
| 38 | return new PrintFunctionsConsumer(); |
| 39 | } |
Daniel Dunbar | 3177aae | 2010-06-16 16:59:23 +0000 | [diff] [blame] | 40 | |
Daniel Dunbar | f56a488 | 2010-08-02 15:31:28 +0000 | [diff] [blame] | 41 | bool ParseArgs(const CompilerInstance &CI, |
| 42 | const std::vector<std::string>& args) { |
| 43 | for (unsigned i = 0, e = args.size(); i != e; ++i) { |
Daniel Dunbar | 3177aae | 2010-06-16 16:59:23 +0000 | [diff] [blame] | 44 | llvm::errs() << "PrintFunctionNames arg = " << args[i] << "\n"; |
Daniel Dunbar | f56a488 | 2010-08-02 15:31:28 +0000 | [diff] [blame] | 45 | |
| 46 | // Example error handling. |
| 47 | if (args[i] == "-an-error") { |
Eli Friedman | d3c1661 | 2011-09-27 18:33:47 +0000 | [diff] [blame] | 48 | DiagnosticsEngine &D = CI.getDiagnostics(); |
Daniel Dunbar | f56a488 | 2010-08-02 15:31:28 +0000 | [diff] [blame] | 49 | unsigned DiagID = D.getCustomDiagID( |
Eli Friedman | d3c1661 | 2011-09-27 18:33:47 +0000 | [diff] [blame] | 50 | DiagnosticsEngine::Error, "invalid argument '" + args[i] + "'"); |
Daniel Dunbar | f56a488 | 2010-08-02 15:31:28 +0000 | [diff] [blame] | 51 | D.Report(DiagID); |
| 52 | return false; |
| 53 | } |
| 54 | } |
Daniel Dunbar | 3177aae | 2010-06-16 16:59:23 +0000 | [diff] [blame] | 55 | if (args.size() && args[0] == "help") |
| 56 | PrintHelp(llvm::errs()); |
| 57 | |
| 58 | return true; |
| 59 | } |
Daniel Dunbar | f56a488 | 2010-08-02 15:31:28 +0000 | [diff] [blame] | 60 | void PrintHelp(llvm::raw_ostream& ros) { |
Daniel Dunbar | 3177aae | 2010-06-16 16:59:23 +0000 | [diff] [blame] | 61 | ros << "Help for PrintFunctionNames plugin goes here\n"; |
| 62 | } |
| 63 | |
Daniel Dunbar | 9a69387 | 2009-11-15 00:27:43 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | } |
| 67 | |
Dan Gohman | 1890eb8 | 2010-07-26 21:12:29 +0000 | [diff] [blame] | 68 | static FrontendPluginRegistry::Add<PrintFunctionNamesAction> |
Daniel Dunbar | 9a69387 | 2009-11-15 00:27:43 +0000 | [diff] [blame] | 69 | X("print-fns", "print function names"); |