diff options
| author | Dimitry Andric <dim@FreeBSD.org> | 2016-07-23 20:44:14 +0000 |
|---|---|---|
| committer | Dimitry Andric <dim@FreeBSD.org> | 2016-07-23 20:44:14 +0000 |
| commit | 2b6b257f4e5503a7a2675bdb8735693db769f75c (patch) | |
| tree | e85e046ae7003fe3bcc8b5454cd0fa3f7407b470 /examples | |
| parent | b4348ed0b7e90c0831b925fbee00b5f179a99796 (diff) | |
Notes
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/AnnotateFunctions/AnnotateFunctions.cpp | 88 | ||||
| -rw-r--r-- | examples/AnnotateFunctions/CMakeLists.txt | 11 | ||||
| -rw-r--r-- | examples/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | examples/Makefile | 14 | ||||
| -rw-r--r-- | examples/PrintFunctionNames/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | examples/PrintFunctionNames/Makefile | 28 | ||||
| -rw-r--r-- | examples/analyzer-plugin/CMakeLists.txt | 5 | ||||
| -rw-r--r-- | examples/analyzer-plugin/Makefile | 20 | ||||
| -rw-r--r-- | examples/analyzer-plugin/SampleAnalyzerPlugin.exports | 2 | ||||
| -rw-r--r-- | examples/clang-interpreter/Makefile | 28 |
10 files changed, 106 insertions, 93 deletions
diff --git a/examples/AnnotateFunctions/AnnotateFunctions.cpp b/examples/AnnotateFunctions/AnnotateFunctions.cpp new file mode 100644 index 000000000000..375f18f8e09a --- /dev/null +++ b/examples/AnnotateFunctions/AnnotateFunctions.cpp @@ -0,0 +1,88 @@ +//===- 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 in +// translation units that start with #pragma enable_annotate. +// +//===----------------------------------------------------------------------===// + +#include "clang/Frontend/FrontendPluginRegistry.h" +#include "clang/AST/AST.h" +#include "clang/AST/ASTConsumer.h" +#include "clang/Lex/Preprocessor.h" +#include "clang/Lex/LexDiagnostic.h" +using namespace clang; + +namespace { + +static bool EnableAnnotate = false; +static bool HandledDecl = false; + +class AnnotateFunctionsConsumer : public ASTConsumer { +public: + bool HandleTopLevelDecl(DeclGroupRef DG) override { + HandledDecl = true; + if (!EnableAnnotate) + return true; + 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; + } +}; + +class PragmaAnnotateHandler : public PragmaHandler { +public: + PragmaAnnotateHandler() : PragmaHandler("enable_annotate") { } + + void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, + Token &PragmaTok) override { + + Token Tok; + PP.LexUnexpandedToken(Tok); + if (Tok.isNot(tok::eod)) + PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; + + if (HandledDecl) { + DiagnosticsEngine &D = PP.getDiagnostics(); + unsigned ID = D.getCustomDiagID( + DiagnosticsEngine::Error, + "#pragma enable_annotate not allowed after declarations"); + D.Report(PragmaTok.getLocation(), ID); + } + + EnableAnnotate = true; + } +}; + +} + +static FrontendPluginRegistry::Add<AnnotateFunctionsAction> +X("annotate-fns", "annotate functions"); + +static PragmaHandlerRegistry::Add<PragmaAnnotateHandler> +Y("enable_annotate","enable annotation"); diff --git a/examples/AnnotateFunctions/CMakeLists.txt b/examples/AnnotateFunctions/CMakeLists.txt new file mode 100644 index 000000000000..cf564d527d6a --- /dev/null +++ b/examples/AnnotateFunctions/CMakeLists.txt @@ -0,0 +1,11 @@ +add_llvm_loadable_module(AnnotateFunctions AnnotateFunctions.cpp) + +if(LLVM_ENABLE_PLUGINS AND (WIN32 OR CYGWIN)) + target_link_libraries(AnnotateFunctions PRIVATE + clangAST + clangBasic + clangFrontend + clangLex + LLVMSupport + ) +endif() diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 5d4b5fcdb0f6..8c2654840a98 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) diff --git a/examples/Makefile b/examples/Makefile deleted file mode 100644 index d8d902874ae0..000000000000 --- a/examples/Makefile +++ /dev/null @@ -1,14 +0,0 @@ -##===- examples/Makefile -----------------------------------*- Makefile -*-===## -# -# The LLVM Compiler Infrastructure -# -# This file is distributed under the University of Illinois Open Source -# License. See LICENSE.TXT for details. -# -##===----------------------------------------------------------------------===## - -CLANG_LEVEL := .. - -PARALLEL_DIRS := analyzer-plugin clang-interpreter PrintFunctionNames - -include $(CLANG_LEVEL)/Makefile diff --git a/examples/PrintFunctionNames/CMakeLists.txt b/examples/PrintFunctionNames/CMakeLists.txt index e700281ab489..5a00d5036fb1 100644 --- a/examples/PrintFunctionNames/CMakeLists.txt +++ b/examples/PrintFunctionNames/CMakeLists.txt @@ -12,7 +12,7 @@ endif() add_llvm_loadable_module(PrintFunctionNames PrintFunctionNames.cpp) if(LLVM_ENABLE_PLUGINS AND (WIN32 OR CYGWIN)) - target_link_libraries(PrintFunctionNames ${cmake_2_8_12_PRIVATE} + target_link_libraries(PrintFunctionNames PRIVATE clangAST clangBasic clangFrontend diff --git a/examples/PrintFunctionNames/Makefile b/examples/PrintFunctionNames/Makefile deleted file mode 100644 index 5865098bd305..000000000000 --- a/examples/PrintFunctionNames/Makefile +++ /dev/null @@ -1,28 +0,0 @@ -##===- examples/PrintFunctionNames/Makefile ----------------*- Makefile -*-===## -# -# The LLVM Compiler Infrastructure -# -# This file is distributed under the University of Illinois Open Source -# License. See LICENSE.TXT for details. -# -##===----------------------------------------------------------------------===## - -CLANG_LEVEL := ../.. -LIBRARYNAME = PrintFunctionNames - -# If we don't need RTTI or EH, there's no reason to export anything -# from the plugin. -ifneq ($(REQUIRES_RTTI), 1) -ifneq ($(REQUIRES_EH), 1) -EXPORTED_SYMBOL_FILE = $(PROJ_SRC_DIR)/PrintFunctionNames.exports -endif -endif - -LINK_LIBS_IN_SHARED = 0 -LOADABLE_MODULE = 1 - -include $(CLANG_LEVEL)/Makefile - -ifeq ($(OS),Darwin) - LDFLAGS=-Wl,-undefined,dynamic_lookup -endif diff --git a/examples/analyzer-plugin/CMakeLists.txt b/examples/analyzer-plugin/CMakeLists.txt index 1788d6c5cac9..0d5b2754cafe 100644 --- a/examples/analyzer-plugin/CMakeLists.txt +++ b/examples/analyzer-plugin/CMakeLists.txt @@ -1,7 +1,8 @@ -add_llvm_loadable_module(SampleAnalyzerPlugin MainCallChecker.cpp) +set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_CURRENT_SOURCE_DIR}/SampleAnalyzerPlugin.exports) +add_llvm_loadable_module(SampleAnalyzerPlugin MainCallChecker.cpp PLUGIN_TOOL clang) if(LLVM_ENABLE_PLUGINS AND (WIN32 OR CYGWIN)) - target_link_libraries(SampleAnalyzerPlugin ${cmake_2_8_12_PRIVATE} + target_link_libraries(SampleAnalyzerPlugin PRIVATE clangAnalysis clangAST clangStaticAnalyzerCore diff --git a/examples/analyzer-plugin/Makefile b/examples/analyzer-plugin/Makefile deleted file mode 100644 index 8b83bef9245a..000000000000 --- a/examples/analyzer-plugin/Makefile +++ /dev/null @@ -1,20 +0,0 @@ -##===- examples/analyzer-plugin/Makefile -------------------*- Makefile -*-===## -# -# The LLVM Compiler Infrastructure -# -# This file is distributed under the University of Illinois Open Source -# License. See LICENSE.TXT for details. -# -##===----------------------------------------------------------------------===## - -CLANG_LEVEL := ../.. -LIBRARYNAME = SampleAnalyzerPlugin - -LINK_LIBS_IN_SHARED = 0 -LOADABLE_MODULE = 1 - -include $(CLANG_LEVEL)/Makefile - -ifeq ($(OS),Darwin) - LDFLAGS=-Wl,-undefined,dynamic_lookup -endif diff --git a/examples/analyzer-plugin/SampleAnalyzerPlugin.exports b/examples/analyzer-plugin/SampleAnalyzerPlugin.exports new file mode 100644 index 000000000000..8d9ff882cfb1 --- /dev/null +++ b/examples/analyzer-plugin/SampleAnalyzerPlugin.exports @@ -0,0 +1,2 @@ +clang_registerCheckers +clang_analyzerAPIVersionString diff --git a/examples/clang-interpreter/Makefile b/examples/clang-interpreter/Makefile deleted file mode 100644 index 2eff90b32b05..000000000000 --- a/examples/clang-interpreter/Makefile +++ /dev/null @@ -1,28 +0,0 @@ -##===- examples/clang-interpreter/Makefile -----------------*- Makefile -*-===## -# -# The LLVM Compiler Infrastructure -# -# This file is distributed under the University of Illinois Open Source -# License. See LICENSE.TXT for details. -# -##===----------------------------------------------------------------------===## - -CLANG_LEVEL := ../.. - -TOOLNAME = clang-interpreter -NO_INSTALL = 1 - -# No plugins, optimize startup time. -TOOL_NO_EXPORTS = 1 - -LINK_COMPONENTS := mcjit interpreter nativecodegen bitreader bitwriter irreader \ - ipo linker selectiondag asmparser instrumentation objcarcopts option -USEDLIBS = clangFrontend.a clangSerialization.a clangDriver.a clangCodeGen.a \ - clangParse.a clangSema.a clangStaticAnalyzerFrontend.a \ - clangStaticAnalyzerCheckers.a clangStaticAnalyzerCore.a \ - clangAnalysis.a clangRewrite.a clangRewriteFrontend.a \ - clangEdit.a clangAST.a clangLex.a clangBasic.a LLVMCore.a \ - LLVMExecutionEngine.a LLVMMC.a LLVMMCJIT.a LLVMRuntimeDyld.a \ - LLVMObject.a LLVMSupport.a LLVMProfileData.a - -include $(CLANG_LEVEL)/Makefile |
