Daniel Dunbar | 6499e9c | 2009-11-15 00:27:43 +0000 | [diff] [blame] | 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 | // |
Daniel Dunbar | 520d1e6 | 2009-12-11 23:04:35 +0000 | [diff] [blame] | 10 | // Example clang plugin which simply prints the names of all the top-level decls |
| 11 | // in the input file. |
Daniel Dunbar | 6499e9c | 2009-11-15 00:27:43 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Frontend/FrontendPluginRegistry.h" |
Daniel Dunbar | 6499e9c | 2009-11-15 00:27:43 +0000 | [diff] [blame] | 16 | #include "clang/AST/AST.h" |
Chandler Carruth | 8675b4a | 2012-12-04 09:37:22 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTConsumer.h" |
Nico Weber | 28e0f24 | 2015-05-17 01:07:16 +0000 | [diff] [blame] | 18 | #include "clang/AST/RecursiveASTVisitor.h" |
Daniel Dunbar | 2be9674 | 2010-08-02 15:31:28 +0000 | [diff] [blame] | 19 | #include "clang/Frontend/CompilerInstance.h" |
Nico Weber | 28e0f24 | 2015-05-17 01:07:16 +0000 | [diff] [blame] | 20 | #include "clang/Sema/Sema.h" |
Daniel Dunbar | 6499e9c | 2009-11-15 00:27:43 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
| 22 | using namespace clang; |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | class PrintFunctionsConsumer : public ASTConsumer { |
Nico Weber | 28e0f24 | 2015-05-17 01:07:16 +0000 | [diff] [blame] | 27 | CompilerInstance &Instance; |
| 28 | std::set<std::string> ParsedTemplates; |
| 29 | |
Daniel Dunbar | 6499e9c | 2009-11-15 00:27:43 +0000 | [diff] [blame] | 30 | public: |
Nico Weber | 28e0f24 | 2015-05-17 01:07:16 +0000 | [diff] [blame] | 31 | PrintFunctionsConsumer(CompilerInstance &Instance, |
| 32 | std::set<std::string> ParsedTemplates) |
| 33 | : Instance(Instance), ParsedTemplates(ParsedTemplates) {} |
| 34 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 35 | bool HandleTopLevelDecl(DeclGroupRef DG) override { |
Daniel Dunbar | 6499e9c | 2009-11-15 00:27:43 +0000 | [diff] [blame] | 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 | } |
Douglas Gregor | 9f39a76 | 2011-11-19 19:22:13 +0000 | [diff] [blame] | 41 | |
| 42 | return true; |
Daniel Dunbar | 6499e9c | 2009-11-15 00:27:43 +0000 | [diff] [blame] | 43 | } |
Nico Weber | 28e0f24 | 2015-05-17 01:07:16 +0000 | [diff] [blame] | 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 = sema.LateParsedTemplateMap.lookup(FD); |
| 73 | sema.LateTemplateParser(sema.OpaqueParser, *LPT); |
| 74 | llvm::errs() << "late-parsed-decl: \"" << FD->getNameAsString() << "\"\n"; |
| 75 | } |
| 76 | } |
Daniel Dunbar | 2be9674 | 2010-08-02 15:31:28 +0000 | [diff] [blame] | 77 | }; |
Daniel Dunbar | 6499e9c | 2009-11-15 00:27:43 +0000 | [diff] [blame] | 78 | |
Daniel Dunbar | 7c995e8 | 2010-06-16 16:59:23 +0000 | [diff] [blame] | 79 | class PrintFunctionNamesAction : public PluginASTAction { |
Nico Weber | 28e0f24 | 2015-05-17 01:07:16 +0000 | [diff] [blame] | 80 | std::set<std::string> ParsedTemplates; |
Daniel Dunbar | 6499e9c | 2009-11-15 00:27:43 +0000 | [diff] [blame] | 81 | protected: |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 82 | std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 83 | llvm::StringRef) override { |
Nico Weber | 28e0f24 | 2015-05-17 01:07:16 +0000 | [diff] [blame] | 84 | return llvm::make_unique<PrintFunctionsConsumer>(CI, ParsedTemplates); |
Daniel Dunbar | 6499e9c | 2009-11-15 00:27:43 +0000 | [diff] [blame] | 85 | } |
Daniel Dunbar | 7c995e8 | 2010-06-16 16:59:23 +0000 | [diff] [blame] | 86 | |
Daniel Dunbar | 2be9674 | 2010-08-02 15:31:28 +0000 | [diff] [blame] | 87 | bool ParseArgs(const CompilerInstance &CI, |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 88 | const std::vector<std::string> &args) override { |
Daniel Dunbar | 2be9674 | 2010-08-02 15:31:28 +0000 | [diff] [blame] | 89 | for (unsigned i = 0, e = args.size(); i != e; ++i) { |
Daniel Dunbar | 7c995e8 | 2010-06-16 16:59:23 +0000 | [diff] [blame] | 90 | llvm::errs() << "PrintFunctionNames arg = " << args[i] << "\n"; |
Daniel Dunbar | 2be9674 | 2010-08-02 15:31:28 +0000 | [diff] [blame] | 91 | |
| 92 | // Example error handling. |
Nico Weber | 28e0f24 | 2015-05-17 01:07:16 +0000 | [diff] [blame] | 93 | DiagnosticsEngine &D = CI.getDiagnostics(); |
Daniel Dunbar | 2be9674 | 2010-08-02 15:31:28 +0000 | [diff] [blame] | 94 | if (args[i] == "-an-error") { |
Alp Toker | 9477f9e5 | 2013-12-21 05:19:58 +0000 | [diff] [blame] | 95 | unsigned DiagID = D.getCustomDiagID(DiagnosticsEngine::Error, |
| 96 | "invalid argument '%0'"); |
| 97 | D.Report(DiagID) << args[i]; |
Daniel Dunbar | 2be9674 | 2010-08-02 15:31:28 +0000 | [diff] [blame] | 98 | return false; |
Nico Weber | 28e0f24 | 2015-05-17 01:07:16 +0000 | [diff] [blame] | 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]); |
Daniel Dunbar | 2be9674 | 2010-08-02 15:31:28 +0000 | [diff] [blame] | 107 | } |
| 108 | } |
Alexander Kornienko | 6ee521c | 2015-01-23 15:36:10 +0000 | [diff] [blame] | 109 | if (!args.empty() && args[0] == "help") |
Daniel Dunbar | 7c995e8 | 2010-06-16 16:59:23 +0000 | [diff] [blame] | 110 | PrintHelp(llvm::errs()); |
| 111 | |
| 112 | return true; |
| 113 | } |
Daniel Dunbar | 2be9674 | 2010-08-02 15:31:28 +0000 | [diff] [blame] | 114 | void PrintHelp(llvm::raw_ostream& ros) { |
Daniel Dunbar | 7c995e8 | 2010-06-16 16:59:23 +0000 | [diff] [blame] | 115 | ros << "Help for PrintFunctionNames plugin goes here\n"; |
| 116 | } |
| 117 | |
Daniel Dunbar | 6499e9c | 2009-11-15 00:27:43 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 120 | } |
Daniel Dunbar | 6499e9c | 2009-11-15 00:27:43 +0000 | [diff] [blame] | 121 | |
Manuel Klimek | 34e0f6c | 2012-04-26 08:46:12 +0000 | [diff] [blame] | 122 | static FrontendPluginRegistry::Add<PrintFunctionNamesAction> |
Daniel Dunbar | 6499e9c | 2009-11-15 00:27:43 +0000 | [diff] [blame] | 123 | X("print-fns", "print function names"); |