blob: ce8f208e41de812aa68447b941e08ee5c8180948 [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"
Daniel Dunbarf56a4882010-08-02 15:31:28 +000018#include "clang/Frontend/CompilerInstance.h"
Daniel Dunbar9a693872009-11-15 00:27:43 +000019#include "llvm/Support/raw_ostream.h"
20using namespace clang;
21
22namespace {
23
24class PrintFunctionsConsumer : public ASTConsumer {
25public:
Douglas Gregor1a026802011-11-19 19:22:13 +000026 virtual bool HandleTopLevelDecl(DeclGroupRef DG) {
Daniel Dunbar9a693872009-11-15 00:27:43 +000027 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 Gregor1a026802011-11-19 19:22:13 +000032
33 return true;
Daniel Dunbar9a693872009-11-15 00:27:43 +000034 }
Daniel Dunbarf56a4882010-08-02 15:31:28 +000035};
Daniel Dunbar9a693872009-11-15 00:27:43 +000036
Daniel Dunbar3177aae2010-06-16 16:59:23 +000037class PrintFunctionNamesAction : public PluginASTAction {
Daniel Dunbar9a693872009-11-15 00:27:43 +000038protected:
39 ASTConsumer *CreateASTConsumer(CompilerInstance &CI, llvm::StringRef) {
40 return new PrintFunctionsConsumer();
41 }
Daniel Dunbar3177aae2010-06-16 16:59:23 +000042
Daniel Dunbarf56a4882010-08-02 15:31:28 +000043 bool ParseArgs(const CompilerInstance &CI,
44 const std::vector<std::string>& args) {
45 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Daniel Dunbar3177aae2010-06-16 16:59:23 +000046 llvm::errs() << "PrintFunctionNames arg = " << args[i] << "\n";
Daniel Dunbarf56a4882010-08-02 15:31:28 +000047
48 // Example error handling.
49 if (args[i] == "-an-error") {
Eli Friedmand3c16612011-09-27 18:33:47 +000050 DiagnosticsEngine &D = CI.getDiagnostics();
Daniel Dunbarf56a4882010-08-02 15:31:28 +000051 unsigned DiagID = D.getCustomDiagID(
Eli Friedmand3c16612011-09-27 18:33:47 +000052 DiagnosticsEngine::Error, "invalid argument '" + args[i] + "'");
Daniel Dunbarf56a4882010-08-02 15:31:28 +000053 D.Report(DiagID);
54 return false;
55 }
56 }
Daniel Dunbar3177aae2010-06-16 16:59:23 +000057 if (args.size() && args[0] == "help")
58 PrintHelp(llvm::errs());
59
60 return true;
61 }
Daniel Dunbarf56a4882010-08-02 15:31:28 +000062 void PrintHelp(llvm::raw_ostream& ros) {
Daniel Dunbar3177aae2010-06-16 16:59:23 +000063 ros << "Help for PrintFunctionNames plugin goes here\n";
64 }
65
Daniel Dunbar9a693872009-11-15 00:27:43 +000066};
67
68}
69
Dan Gohman1890eb82010-07-26 21:12:29 +000070static FrontendPluginRegistry::Add<PrintFunctionNamesAction>
Daniel Dunbar9a693872009-11-15 00:27:43 +000071X("print-fns", "print function names");