blob: e94b0ed676a16126e98bb74bdd3e22126fb2f82f [file] [log] [blame]
Daniel Dunbar9a693872009-11-15 00:27:43 +00001//===- 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 Dunbardd63b282009-12-11 23:04:35 +000010// Example clang plugin which simply prints the names of all the top-level decls
11// in the input file.
Daniel Dunbar9a693872009-11-15 00:27:43 +000012//
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"
19using namespace clang;
20
21namespace {
22
23class PrintFunctionsConsumer : public ASTConsumer {
24public:
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
Daniel Dunbar3177aae2010-06-16 16:59:23 +000034class PrintFunctionNamesAction : public PluginASTAction {
Daniel Dunbar9a693872009-11-15 00:27:43 +000035protected:
36 ASTConsumer *CreateASTConsumer(CompilerInstance &CI, llvm::StringRef) {
37 return new PrintFunctionsConsumer();
38 }
Daniel Dunbar3177aae2010-06-16 16:59:23 +000039
40 bool ParseArgs(const std::vector<std::string>& args) {
41 for (unsigned i=0; i<args.size(); ++i)
42 llvm::errs() << "PrintFunctionNames arg = " << args[i] << "\n";
43 if (args.size() && args[0] == "help")
44 PrintHelp(llvm::errs());
45
46 return true;
47 }
48 void PrintHelp(llvm::raw_ostream& ros) {
49 ros << "Help for PrintFunctionNames plugin goes here\n";
50 }
51
Daniel Dunbar9a693872009-11-15 00:27:43 +000052};
53
54}
55
Dan Gohman1890eb82010-07-26 21:12:29 +000056static FrontendPluginRegistry::Add<PrintFunctionNamesAction>
Daniel Dunbar9a693872009-11-15 00:27:43 +000057X("print-fns", "print function names");