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