2009-11-15 08:27:43 +08:00
|
|
|
//===- PrintFunctionNames.cpp ---------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2009-12-12 07:04:35 +08:00
|
|
|
// Example clang plugin which simply prints the names of all the top-level decls
|
|
|
|
// in the input file.
|
2009-11-15 08:27:43 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Frontend/FrontendPluginRegistry.h"
|
|
|
|
#include "clang/AST/AST.h"
|
2012-12-04 17:37:22 +08:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2015-05-17 09:07:16 +08:00
|
|
|
#include "clang/AST/RecursiveASTVisitor.h"
|
2010-08-02 23:31:28 +08:00
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
2015-05-17 09:07:16 +08:00
|
|
|
#include "clang/Sema/Sema.h"
|
2009-11-15 08:27:43 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class PrintFunctionsConsumer : public ASTConsumer {
|
2015-05-17 09:07:16 +08:00
|
|
|
CompilerInstance &Instance;
|
|
|
|
std::set<std::string> ParsedTemplates;
|
|
|
|
|
2009-11-15 08:27:43 +08:00
|
|
|
public:
|
2015-05-17 09:07:16 +08:00
|
|
|
PrintFunctionsConsumer(CompilerInstance &Instance,
|
|
|
|
std::set<std::string> ParsedTemplates)
|
|
|
|
: Instance(Instance), ParsedTemplates(ParsedTemplates) {}
|
|
|
|
|
2015-04-11 10:00:23 +08:00
|
|
|
bool HandleTopLevelDecl(DeclGroupRef DG) override {
|
2009-11-15 08:27:43 +08:00
|
|
|
for (DeclGroupRef::iterator i = DG.begin(), e = DG.end(); i != e; ++i) {
|
|
|
|
const Decl *D = *i;
|
|
|
|
if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
|
|
|
|
llvm::errs() << "top-level-decl: \"" << ND->getNameAsString() << "\"\n";
|
|
|
|
}
|
2011-11-20 03:22:13 +08:00
|
|
|
|
|
|
|
return true;
|
2009-11-15 08:27:43 +08:00
|
|
|
}
|
2015-05-17 09:07:16 +08:00
|
|
|
|
|
|
|
void HandleTranslationUnit(ASTContext& context) override {
|
|
|
|
if (!Instance.getLangOpts().DelayedTemplateParsing)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// This demonstrates how to force instantiation of some templates in
|
|
|
|
// -fdelayed-template-parsing mode. (Note: Doing this unconditionally for
|
|
|
|
// all templates is similar to not using -fdelayed-template-parsig in the
|
|
|
|
// first place.)
|
|
|
|
// The advantage of doing this in HandleTranslationUnit() is that all
|
|
|
|
// codegen (when using -add-plugin) is completely finished and this can't
|
|
|
|
// affect the compiler output.
|
|
|
|
struct Visitor : public RecursiveASTVisitor<Visitor> {
|
|
|
|
const std::set<std::string> &ParsedTemplates;
|
|
|
|
Visitor(const std::set<std::string> &ParsedTemplates)
|
|
|
|
: ParsedTemplates(ParsedTemplates) {}
|
|
|
|
bool VisitFunctionDecl(FunctionDecl *FD) {
|
|
|
|
if (FD->isLateTemplateParsed() &&
|
|
|
|
ParsedTemplates.count(FD->getNameAsString()))
|
|
|
|
LateParsedDecls.insert(FD);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::set<FunctionDecl*> LateParsedDecls;
|
|
|
|
} v(ParsedTemplates);
|
|
|
|
v.TraverseDecl(context.getTranslationUnitDecl());
|
|
|
|
clang::Sema &sema = Instance.getSema();
|
|
|
|
for (const FunctionDecl *FD : v.LateParsedDecls) {
|
2016-10-11 00:41:00 +08:00
|
|
|
clang::LateParsedTemplate &LPT =
|
|
|
|
*sema.LateParsedTemplateMap.find(FD)->second;
|
|
|
|
sema.LateTemplateParser(sema.OpaqueParser, LPT);
|
2015-05-17 09:07:16 +08:00
|
|
|
llvm::errs() << "late-parsed-decl: \"" << FD->getNameAsString() << "\"\n";
|
|
|
|
}
|
|
|
|
}
|
2010-08-02 23:31:28 +08:00
|
|
|
};
|
2009-11-15 08:27:43 +08:00
|
|
|
|
2010-06-17 00:59:23 +08:00
|
|
|
class PrintFunctionNamesAction : public PluginASTAction {
|
2015-05-17 09:07:16 +08:00
|
|
|
std::set<std::string> ParsedTemplates;
|
2009-11-15 08:27:43 +08:00
|
|
|
protected:
|
2014-08-11 03:56:51 +08:00
|
|
|
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
|
2015-04-11 10:00:23 +08:00
|
|
|
llvm::StringRef) override {
|
2015-05-17 09:07:16 +08:00
|
|
|
return llvm::make_unique<PrintFunctionsConsumer>(CI, ParsedTemplates);
|
2009-11-15 08:27:43 +08:00
|
|
|
}
|
2010-06-17 00:59:23 +08:00
|
|
|
|
2010-08-02 23:31:28 +08:00
|
|
|
bool ParseArgs(const CompilerInstance &CI,
|
2015-04-11 10:00:23 +08:00
|
|
|
const std::vector<std::string> &args) override {
|
2010-08-02 23:31:28 +08:00
|
|
|
for (unsigned i = 0, e = args.size(); i != e; ++i) {
|
2010-06-17 00:59:23 +08:00
|
|
|
llvm::errs() << "PrintFunctionNames arg = " << args[i] << "\n";
|
2010-08-02 23:31:28 +08:00
|
|
|
|
|
|
|
// Example error handling.
|
2015-05-17 09:07:16 +08:00
|
|
|
DiagnosticsEngine &D = CI.getDiagnostics();
|
2010-08-02 23:31:28 +08:00
|
|
|
if (args[i] == "-an-error") {
|
2013-12-21 13:19:58 +08:00
|
|
|
unsigned DiagID = D.getCustomDiagID(DiagnosticsEngine::Error,
|
|
|
|
"invalid argument '%0'");
|
|
|
|
D.Report(DiagID) << args[i];
|
2010-08-02 23:31:28 +08:00
|
|
|
return false;
|
2015-05-17 09:07:16 +08:00
|
|
|
} else if (args[i] == "-parse-template") {
|
|
|
|
if (i + 1 >= e) {
|
|
|
|
D.Report(D.getCustomDiagID(DiagnosticsEngine::Error,
|
|
|
|
"missing -parse-template argument"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
ParsedTemplates.insert(args[i]);
|
2010-08-02 23:31:28 +08:00
|
|
|
}
|
|
|
|
}
|
2015-01-23 23:36:10 +08:00
|
|
|
if (!args.empty() && args[0] == "help")
|
2010-06-17 00:59:23 +08:00
|
|
|
PrintHelp(llvm::errs());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2010-08-02 23:31:28 +08:00
|
|
|
void PrintHelp(llvm::raw_ostream& ros) {
|
2010-06-17 00:59:23 +08:00
|
|
|
ros << "Help for PrintFunctionNames plugin goes here\n";
|
|
|
|
}
|
|
|
|
|
2009-11-15 08:27:43 +08:00
|
|
|
};
|
|
|
|
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2009-11-15 08:27:43 +08:00
|
|
|
|
2012-04-26 16:46:12 +08:00
|
|
|
static FrontendPluginRegistry::Add<PrintFunctionNamesAction>
|
2009-11-15 08:27:43 +08:00
|
|
|
X("print-fns", "print function names");
|