diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/AnnotateFunctions/AnnotateFunctions.cpp | 52 | ||||
-rw-r--r-- | examples/AnnotateFunctions/CMakeLists.txt | 9 | ||||
-rw-r--r-- | examples/CMakeLists.txt | 1 |
3 files changed, 62 insertions, 0 deletions
diff --git a/examples/AnnotateFunctions/AnnotateFunctions.cpp b/examples/AnnotateFunctions/AnnotateFunctions.cpp new file mode 100644 index 0000000000..f2e7322183 --- /dev/null +++ b/examples/AnnotateFunctions/AnnotateFunctions.cpp @@ -0,0 +1,52 @@ +//===- AnnotateFunctions.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 adds an annotation to every function. +// +//===----------------------------------------------------------------------===// + +#include "clang/Frontend/FrontendPluginRegistry.h" +#include "clang/AST/AST.h" +#include "clang/AST/ASTConsumer.h" +using namespace clang; + +namespace { + +class AnnotateFunctionsConsumer : public ASTConsumer { +public: + bool HandleTopLevelDecl(DeclGroupRef DG) override { + for (auto D : DG) + if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) + FD->addAttr(AnnotateAttr::CreateImplicit(FD->getASTContext(), + "example_annotation")); + return true; + } +}; + +class AnnotateFunctionsAction : public PluginASTAction { +public: + std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, + llvm::StringRef) override { + return llvm::make_unique<AnnotateFunctionsConsumer>(); + } + + bool ParseArgs(const CompilerInstance &CI, + const std::vector<std::string> &args) override { + return true; + } + + PluginASTAction::ActionType getActionType() override { + return AddBeforeMainAction; + } +}; + +} + +static FrontendPluginRegistry::Add<AnnotateFunctionsAction> +X("annotate-fns", "annotate functions"); diff --git a/examples/AnnotateFunctions/CMakeLists.txt b/examples/AnnotateFunctions/CMakeLists.txt new file mode 100644 index 0000000000..5aa6a9064c --- /dev/null +++ b/examples/AnnotateFunctions/CMakeLists.txt @@ -0,0 +1,9 @@ +add_llvm_loadable_module(AnnotateFunctions AnnotateFunctions.cpp) + +if(LLVM_ENABLE_PLUGINS AND (WIN32 OR CYGWIN)) + target_link_libraries(AnnotateFunctions ${cmake_2_8_12_PRIVATE} + clangAST + clangFrontend + LLVMSupport + ) +endif() diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 5d4b5fcdb0..8c2654840a 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -8,3 +8,4 @@ add_subdirectory(analyzer-plugin) endif() add_subdirectory(clang-interpreter) add_subdirectory(PrintFunctionNames) +add_subdirectory(AnnotateFunctions) |