diff options
Diffstat (limited to 'gnu/llvm/clang/examples/PrintFunctionNames')
4 files changed, 162 insertions, 0 deletions
diff --git a/gnu/llvm/clang/examples/PrintFunctionNames/CMakeLists.txt b/gnu/llvm/clang/examples/PrintFunctionNames/CMakeLists.txt new file mode 100644 index 00000000000..63b0c015732 --- /dev/null +++ b/gnu/llvm/clang/examples/PrintFunctionNames/CMakeLists.txt @@ -0,0 +1,23 @@ +# If we don't need RTTI or EH, there's no reason to export anything +# from the plugin. +if( NOT MSVC ) # MSVC mangles symbols differently, and + # PrintFunctionNames.export contains C++ symbols. + if( NOT LLVM_REQUIRES_RTTI ) + if( NOT LLVM_REQUIRES_EH ) + set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_CURRENT_SOURCE_DIR}/PrintFunctionNames.exports) + endif() + endif() +endif() + +add_llvm_library(PrintFunctionNames MODULE PrintFunctionNames.cpp PLUGIN_TOOL clang) + +if(LLVM_ENABLE_PLUGINS AND (WIN32 OR CYGWIN)) + set(LLVM_LINK_COMPONENTS + Support + ) + clang_target_link_libraries(PrintFunctionNames PRIVATE + clangAST + clangBasic + clangFrontend + ) +endif() diff --git a/gnu/llvm/clang/examples/PrintFunctionNames/PrintFunctionNames.cpp b/gnu/llvm/clang/examples/PrintFunctionNames/PrintFunctionNames.cpp new file mode 100644 index 00000000000..6509a6440e1 --- /dev/null +++ b/gnu/llvm/clang/examples/PrintFunctionNames/PrintFunctionNames.cpp @@ -0,0 +1,123 @@ +//===- PrintFunctionNames.cpp ---------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// 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/AST/RecursiveASTVisitor.h" +#include "clang/Frontend/CompilerInstance.h" +#include "clang/Sema/Sema.h" +#include "llvm/Support/raw_ostream.h" +using namespace clang; + +namespace { + +class PrintFunctionsConsumer : public ASTConsumer { + CompilerInstance &Instance; + std::set<std::string> ParsedTemplates; + +public: + PrintFunctionsConsumer(CompilerInstance &Instance, + std::set<std::string> ParsedTemplates) + : Instance(Instance), ParsedTemplates(ParsedTemplates) {} + + 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; + } + + 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) { + clang::LateParsedTemplate &LPT = + *sema.LateParsedTemplateMap.find(FD)->second; + sema.LateTemplateParser(sema.OpaqueParser, LPT); + llvm::errs() << "late-parsed-decl: \"" << FD->getNameAsString() << "\"\n"; + } + } +}; + +class PrintFunctionNamesAction : public PluginASTAction { + std::set<std::string> ParsedTemplates; +protected: + std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, + llvm::StringRef) override { + return std::make_unique<PrintFunctionsConsumer>(CI, ParsedTemplates); + } + + 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. + DiagnosticsEngine &D = CI.getDiagnostics(); + if (args[i] == "-an-error") { + unsigned DiagID = D.getCustomDiagID(DiagnosticsEngine::Error, + "invalid argument '%0'"); + D.Report(DiagID) << args[i]; + return false; + } 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]); + } + } + 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"); diff --git a/gnu/llvm/clang/examples/PrintFunctionNames/PrintFunctionNames.exports b/gnu/llvm/clang/examples/PrintFunctionNames/PrintFunctionNames.exports new file mode 100644 index 00000000000..e69de29bb2d --- /dev/null +++ b/gnu/llvm/clang/examples/PrintFunctionNames/PrintFunctionNames.exports diff --git a/gnu/llvm/clang/examples/PrintFunctionNames/README.txt b/gnu/llvm/clang/examples/PrintFunctionNames/README.txt new file mode 100644 index 00000000000..23ab5f0b04f --- /dev/null +++ b/gnu/llvm/clang/examples/PrintFunctionNames/README.txt @@ -0,0 +1,16 @@ +This is a simple example demonstrating how to use clang's facility for +providing AST consumers using a plugin. + +Build the plugin by running `make` in this directory. + +Once the plugin is built, you can run it using: +-- +Linux: +$ clang -cc1 -load ../../Debug+Asserts/lib/libPrintFunctionNames.so -plugin print-fns some-input-file.c +$ clang -cc1 -load ../../Debug+Asserts/lib/libPrintFunctionNames.so -plugin print-fns -plugin-arg-print-fns help -plugin-arg-print-fns --example-argument some-input-file.c +$ clang -cc1 -load ../../Debug+Asserts/lib/libPrintFunctionNames.so -plugin print-fns -plugin-arg-print-fns -an-error some-input-file.c + +Mac: +$ clang -cc1 -load ../../Debug+Asserts/lib/libPrintFunctionNames.dylib -plugin print-fns some-input-file.c +$ clang -cc1 -load ../../Debug+Asserts/lib/libPrintFunctionNames.dylib -plugin print-fns -plugin-arg-print-fns help -plugin-arg-print-fns --example-argument some-input-file.c +$ clang -cc1 -load ../../Debug+Asserts/lib/libPrintFunctionNames.dylib -plugin print-fns -plugin-arg-print-fns -an-error some-input-file.c |