123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- //===- PrintFunctionNames.cpp ---------------------------------------------===//
- //
- // The LLVM Compiler Infrastructure
- //
- // This file is distributed under the University of Illinois Open Source
- // License. See LICENSE.TXT for details.
- //
- //===----------------------------------------------------------------------===//
- //
- // Example clang plugin which simply prints the names of all the top-level decls
- // in the input file.
- //
- //===----------------------------------------------------------------------===//
- #include "clang/Frontend/FrontendPluginRegistry.h"
- #include "clang/AST/AST.h"
- #include "clang/AST/ASTConsumer.h"
- #include "clang/Frontend/CompilerInstance.h"
- #include "llvm/Support/raw_ostream.h"
- using namespace clang;
- namespace {
- class PrintFunctionsConsumer : public ASTConsumer {
- public:
- bool HandleTopLevelDecl(DeclGroupRef DG) override {
- 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";
- }
- return true;
- }
- };
- class PrintFunctionNamesAction : public PluginASTAction {
- protected:
- std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
- llvm::StringRef) override {
- return llvm::make_unique<PrintFunctionsConsumer>();
- }
- bool ParseArgs(const CompilerInstance &CI,
- const std::vector<std::string> &args) override {
- for (unsigned i = 0, e = args.size(); i != e; ++i) {
- llvm::errs() << "PrintFunctionNames arg = " << args[i] << "\n";
- // Example error handling.
- if (args[i] == "-an-error") {
- DiagnosticsEngine &D = CI.getDiagnostics();
- unsigned DiagID = D.getCustomDiagID(DiagnosticsEngine::Error,
- "invalid argument '%0'");
- D.Report(DiagID) << args[i];
- return false;
- }
- }
- if (!args.empty() && args[0] == "help")
- PrintHelp(llvm::errs());
- return true;
- }
- void PrintHelp(llvm::raw_ostream& ros) {
- ros << "Help for PrintFunctionNames plugin goes here\n";
- }
- };
- }
- static FrontendPluginRegistry::Add<PrintFunctionNamesAction>
- X("print-fns", "print function names");
|