summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorJohn Brawn <john.brawn@arm.com>2016-03-15 12:51:40 +0000
committerJohn Brawn <john.brawn@arm.com>2016-03-15 12:51:40 +0000
commitf6603f2aedd2eedd16c1d744e7c4c8caaa2dfde9 (patch)
tree226535973c4d2718f50f6d5f883942368436d22c /examples
parent8518d88c3151548eb6d8a7a0bb28c73f65acd897 (diff)
downloadclang-f6603f2aedd2eedd16c1d744e7c4c8caaa2dfde9.tar.gz
Make it possible for AST plugins to enable themselves by default
Currently when an AST plugin is loaded it must then be enabled by passing -plugin pluginname or -add-plugin pluginname to the -cc1 command line. This patch adds a method to PluginASTAction which allows it to declare that the action happens before, instead of, or after the main AST action, plus the relevant changes to make the plugin action happen at that time automatically. Differential Revision: http://reviews.llvm.org/D17959 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@263546 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'examples')
-rw-r--r--examples/AnnotateFunctions/AnnotateFunctions.cpp52
-rw-r--r--examples/AnnotateFunctions/CMakeLists.txt9
-rw-r--r--examples/CMakeLists.txt1
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)