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"
|
2010-08-02 23:31:28 +08:00
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
2009-11-15 08:27:43 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class PrintFunctionsConsumer : public ASTConsumer {
|
|
|
|
public:
|
2011-11-20 03:22:13 +08:00
|
|
|
virtual bool HandleTopLevelDecl(DeclGroupRef DG) {
|
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
|
|
|
}
|
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 {
|
2009-11-15 08:27:43 +08:00
|
|
|
protected:
|
|
|
|
ASTConsumer *CreateASTConsumer(CompilerInstance &CI, llvm::StringRef) {
|
|
|
|
return new PrintFunctionsConsumer();
|
|
|
|
}
|
2010-06-17 00:59:23 +08:00
|
|
|
|
2010-08-02 23:31:28 +08:00
|
|
|
bool ParseArgs(const CompilerInstance &CI,
|
|
|
|
const std::vector<std::string>& args) {
|
|
|
|
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.
|
|
|
|
if (args[i] == "-an-error") {
|
2011-09-28 02:33:47 +08:00
|
|
|
DiagnosticsEngine &D = CI.getDiagnostics();
|
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;
|
|
|
|
}
|
|
|
|
}
|
2010-06-17 00:59:23 +08:00
|
|
|
if (args.size() && args[0] == "help")
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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");
|