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" |
| 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 ASTFrontendAction { |
| 35 | protected: |
| 36 | ASTConsumer *CreateASTConsumer(CompilerInstance &CI, llvm::StringRef) { |
| 37 | return new PrintFunctionsConsumer(); |
| 38 | } |
| 39 | }; |
| 40 | |
| 41 | } |
| 42 | |
| 43 | FrontendPluginRegistry::Add<PrintFunctionNamesAction> |
| 44 | X("print-fns", "print function names"); |