blob: 39443f597fa3034f348f3f030e6ac24dbb15c93d [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"
Daniel Dunbar9a693872009-11-15 00:27:43 +000016#include "clang/AST/AST.h"
Chandler Carruthe0c6e932012-12-04 09:37:22 +000017#include "clang/AST/ASTConsumer.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:
Pirama Arumuga Nainar33337ca2015-05-06 11:48:57 -070026 bool HandleTopLevelDecl(DeclGroupRef DG) override {
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:
Stephen Hines176edba2014-12-01 14:53:08 -080039 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
Pirama Arumuga Nainar33337ca2015-05-06 11:48:57 -070040 llvm::StringRef) override {
Stephen Hines176edba2014-12-01 14:53:08 -080041 return llvm::make_unique<PrintFunctionsConsumer>();
Daniel Dunbar9a693872009-11-15 00:27:43 +000042 }
Daniel Dunbar3177aae2010-06-16 16:59:23 +000043
Daniel Dunbarf56a4882010-08-02 15:31:28 +000044 bool ParseArgs(const CompilerInstance &CI,
Pirama Arumuga Nainar33337ca2015-05-06 11:48:57 -070045 const std::vector<std::string> &args) override {
Daniel Dunbarf56a4882010-08-02 15:31:28 +000046 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();
Stephen Hines651f13c2014-04-23 16:59:28 -070052 unsigned DiagID = D.getCustomDiagID(DiagnosticsEngine::Error,
53 "invalid argument '%0'");
54 D.Report(DiagID) << args[i];
Daniel Dunbarf56a4882010-08-02 15:31:28 +000055 return false;
56 }
57 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -070058 if (!args.empty() && args[0] == "help")
Daniel Dunbar3177aae2010-06-16 16:59:23 +000059 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 Klimek981d1ec2012-04-26 08:46:12 +000071static FrontendPluginRegistry::Add<PrintFunctionNamesAction>
Daniel Dunbar9a693872009-11-15 00:27:43 +000072X("print-fns", "print function names");