• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //
10 // Example clang plugin which simply prints the names of all the top-level decls
11 // in the input file.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "clang/Frontend/FrontendPluginRegistry.h"
16 #include "clang/AST/AST.h"
17 #include "clang/AST/ASTConsumer.h"
18 #include "clang/AST/RecursiveASTVisitor.h"
19 #include "clang/Frontend/CompilerInstance.h"
20 #include "clang/Sema/Sema.h"
21 #include "llvm/Support/raw_ostream.h"
22 using namespace clang;
23 
24 namespace {
25 
26 class PrintFunctionsConsumer : public ASTConsumer {
27   CompilerInstance &Instance;
28   std::set<std::string> ParsedTemplates;
29 
30 public:
PrintFunctionsConsumer(CompilerInstance & Instance,std::set<std::string> ParsedTemplates)31   PrintFunctionsConsumer(CompilerInstance &Instance,
32                          std::set<std::string> ParsedTemplates)
33       : Instance(Instance), ParsedTemplates(ParsedTemplates) {}
34 
HandleTopLevelDecl(DeclGroupRef DG)35   bool HandleTopLevelDecl(DeclGroupRef DG) override {
36     for (DeclGroupRef::iterator i = DG.begin(), e = DG.end(); i != e; ++i) {
37       const Decl *D = *i;
38       if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
39         llvm::errs() << "top-level-decl: \"" << ND->getNameAsString() << "\"\n";
40     }
41 
42     return true;
43   }
44 
HandleTranslationUnit(ASTContext & context)45   void HandleTranslationUnit(ASTContext& context) override {
46     if (!Instance.getLangOpts().DelayedTemplateParsing)
47       return;
48 
49     // This demonstrates how to force instantiation of some templates in
50     // -fdelayed-template-parsing mode. (Note: Doing this unconditionally for
51     // all templates is similar to not using -fdelayed-template-parsig in the
52     // first place.)
53     // The advantage of doing this in HandleTranslationUnit() is that all
54     // codegen (when using -add-plugin) is completely finished and this can't
55     // affect the compiler output.
56     struct Visitor : public RecursiveASTVisitor<Visitor> {
57       const std::set<std::string> &ParsedTemplates;
58       Visitor(const std::set<std::string> &ParsedTemplates)
59           : ParsedTemplates(ParsedTemplates) {}
60       bool VisitFunctionDecl(FunctionDecl *FD) {
61         if (FD->isLateTemplateParsed() &&
62             ParsedTemplates.count(FD->getNameAsString()))
63           LateParsedDecls.insert(FD);
64         return true;
65       }
66 
67       std::set<FunctionDecl*> LateParsedDecls;
68     } v(ParsedTemplates);
69     v.TraverseDecl(context.getTranslationUnitDecl());
70     clang::Sema &sema = Instance.getSema();
71     for (const FunctionDecl *FD : v.LateParsedDecls) {
72       clang::LateParsedTemplate* LPT = sema.LateParsedTemplateMap.lookup(FD);
73       sema.LateTemplateParser(sema.OpaqueParser, *LPT);
74       llvm::errs() << "late-parsed-decl: \"" << FD->getNameAsString() << "\"\n";
75     }
76   }
77 };
78 
79 class PrintFunctionNamesAction : public PluginASTAction {
80   std::set<std::string> ParsedTemplates;
81 protected:
CreateASTConsumer(CompilerInstance & CI,llvm::StringRef)82   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
83                                                  llvm::StringRef) override {
84     return llvm::make_unique<PrintFunctionsConsumer>(CI, ParsedTemplates);
85   }
86 
ParseArgs(const CompilerInstance & CI,const std::vector<std::string> & args)87   bool ParseArgs(const CompilerInstance &CI,
88                  const std::vector<std::string> &args) override {
89     for (unsigned i = 0, e = args.size(); i != e; ++i) {
90       llvm::errs() << "PrintFunctionNames arg = " << args[i] << "\n";
91 
92       // Example error handling.
93       DiagnosticsEngine &D = CI.getDiagnostics();
94       if (args[i] == "-an-error") {
95         unsigned DiagID = D.getCustomDiagID(DiagnosticsEngine::Error,
96                                             "invalid argument '%0'");
97         D.Report(DiagID) << args[i];
98         return false;
99       } else if (args[i] == "-parse-template") {
100         if (i + 1 >= e) {
101           D.Report(D.getCustomDiagID(DiagnosticsEngine::Error,
102                                      "missing -parse-template argument"));
103           return false;
104         }
105         ++i;
106         ParsedTemplates.insert(args[i]);
107       }
108     }
109     if (!args.empty() && args[0] == "help")
110       PrintHelp(llvm::errs());
111 
112     return true;
113   }
PrintHelp(llvm::raw_ostream & ros)114   void PrintHelp(llvm::raw_ostream& ros) {
115     ros << "Help for PrintFunctionNames plugin goes here\n";
116   }
117 
118 };
119 
120 }
121 
122 static FrontendPluginRegistry::Add<PrintFunctionNamesAction>
123 X("print-fns", "print function names");
124