blob: b024b968dbce8640ed29c3153acb56ea4209f35e [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"
Manuel Klimekd9ed0fd2012-04-26 08:35:39 +000020#include "clang/Frontend/FrontendActions.h"
Daniel Dunbar9a693872009-11-15 00:27:43 +000021using namespace clang;
22
23namespace {
24
25class PrintFunctionsConsumer : public ASTConsumer {
26public:
Douglas Gregor1a026802011-11-19 19:22:13 +000027 virtual bool HandleTopLevelDecl(DeclGroupRef DG) {
Daniel Dunbar9a693872009-11-15 00:27:43 +000028 for (DeclGroupRef::iterator i = DG.begin(), e = DG.end(); i != e; ++i) {
29 const Decl *D = *i;
30 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
31 llvm::errs() << "top-level-decl: \"" << ND->getNameAsString() << "\"\n";
32 }
Douglas Gregor1a026802011-11-19 19:22:13 +000033
34 return true;
Daniel Dunbar9a693872009-11-15 00:27:43 +000035 }
Daniel Dunbarf56a4882010-08-02 15:31:28 +000036};
Daniel Dunbar9a693872009-11-15 00:27:43 +000037
Daniel Dunbar3177aae2010-06-16 16:59:23 +000038class PrintFunctionNamesAction : public PluginASTAction {
Daniel Dunbar9a693872009-11-15 00:27:43 +000039protected:
40 ASTConsumer *CreateASTConsumer(CompilerInstance &CI, llvm::StringRef) {
41 return new PrintFunctionsConsumer();
42 }
Daniel Dunbar3177aae2010-06-16 16:59:23 +000043
Daniel Dunbarf56a4882010-08-02 15:31:28 +000044 bool ParseArgs(const CompilerInstance &CI,
45 const std::vector<std::string>& args) {
46 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Daniel Dunbar3177aae2010-06-16 16:59:23 +000047 llvm::errs() << "PrintFunctionNames arg = " << args[i] << "\n";
Daniel Dunbarf56a4882010-08-02 15:31:28 +000048
49 // Example error handling.
50 if (args[i] == "-an-error") {
Eli Friedmand3c16612011-09-27 18:33:47 +000051 DiagnosticsEngine &D = CI.getDiagnostics();
Daniel Dunbarf56a4882010-08-02 15:31:28 +000052 unsigned DiagID = D.getCustomDiagID(
Eli Friedmand3c16612011-09-27 18:33:47 +000053 DiagnosticsEngine::Error, "invalid argument '" + args[i] + "'");
Daniel Dunbarf56a4882010-08-02 15:31:28 +000054 D.Report(DiagID);
55 return false;
56 }
57 }
Daniel Dunbar3177aae2010-06-16 16:59:23 +000058 if (args.size() && args[0] == "help")
59 PrintHelp(llvm::errs());
60
61 return true;
62 }
Daniel Dunbarf56a4882010-08-02 15:31:28 +000063 void PrintHelp(llvm::raw_ostream& ros) {
Daniel Dunbar3177aae2010-06-16 16:59:23 +000064 ros << "Help for PrintFunctionNames plugin goes here\n";
65 }
66
Daniel Dunbar9a693872009-11-15 00:27:43 +000067};
68
69}
70
Manuel Klimekd9ed0fd2012-04-26 08:35:39 +000071static FrontendPluginRegistry::Add<SyntaxOnlyAction>
Daniel Dunbar9a693872009-11-15 00:27:43 +000072X("print-fns", "print function names");