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:
31   PrintFunctionsConsumer(CompilerInstance &Instance,
32                          std::set<std::string> ParsedTemplates)
33       : Instance(Instance), ParsedTemplates(ParsedTemplates) {}
34 
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 
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 =
73           *sema.LateParsedTemplateMap.find(FD)->second;
74       sema.LateTemplateParser(sema.OpaqueParser, LPT);
75       llvm::errs() << "late-parsed-decl: \"" << FD->getNameAsString() << "\"\n";
76     }
77   }
78 };
79 
80 class PrintFunctionNamesAction : public PluginASTAction {
81   std::set<std::string> ParsedTemplates;
82 protected:
83   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
84                                                  llvm::StringRef) override {
85     return llvm::make_unique<PrintFunctionsConsumer>(CI, ParsedTemplates);
86   }
87 
88   bool ParseArgs(const CompilerInstance &CI,
89                  const std::vector<std::string> &args) override {
90     for (unsigned i = 0, e = args.size(); i != e; ++i) {
91       llvm::errs() << "PrintFunctionNames arg = " << args[i] << "\n";
92 
93       // Example error handling.
94       DiagnosticsEngine &D = CI.getDiagnostics();
95       if (args[i] == "-an-error") {
96         unsigned DiagID = D.getCustomDiagID(DiagnosticsEngine::Error,
97                                             "invalid argument '%0'");
98         D.Report(DiagID) << args[i];
99         return false;
100       } else if (args[i] == "-parse-template") {
101         if (i + 1 >= e) {
102           D.Report(D.getCustomDiagID(DiagnosticsEngine::Error,
103                                      "missing -parse-template argument"));
104           return false;
105         }
106         ++i;
107         ParsedTemplates.insert(args[i]);
108       }
109     }
110     if (!args.empty() && args[0] == "help")
111       PrintHelp(llvm::errs());
112 
113     return true;
114   }
115   void PrintHelp(llvm::raw_ostream& ros) {
116     ros << "Help for PrintFunctionNames plugin goes here\n";
117   }
118 
119 };
120 
121 }
122 
123 static FrontendPluginRegistry::Add<PrintFunctionNamesAction>
124 X("print-fns", "print function names");
125