From 778c3c6c61b7080a9537685b38d6b9df282e0d05 Mon Sep 17 00:00:00 2001 From: John Brawn Date: Thu, 28 Jul 2016 12:48:17 +0000 Subject: [PATCH] Reapply r276856 "Adjust Registry interface to not require plugins to export a registry" This version has two fixes compared to the original: * In Registry.h the template static members are instantiated before they are used, as clang gives an error if you do it the other way around. * The use of the Registry template in clang-tidy is updated in the same way as has been done everywhere else. Original commit message: Currently the Registry class contains the vestiges of a previous attempt to allow plugins to be used on Windows without using BUILD_SHARED_LIBS, where a plugin would have its own copy of a registry and export it to be imported by the tool that's loading the plugin. This only works if the plugin is entirely self-contained with the only interface between the plugin and tool being the registry, and in particular this conflicts with how IR pass plugins work. This patch changes things so that instead the add_node function of the registry is exported by the tool and then imported by the plugin, which solves this problem and also means that instead of every plugin having to export every registry they use instead LLVM only has to export the add_node functions. This allows plugins that use a registry to work on Windows if LLVM_EXPORT_SYMBOLS_FOR_PLUGINS is used. llvm-svn: 276973 --- clang-tools-extra/clang-tidy/ClangTidy.cpp | 2 +- .../clang-tidy/ClangTidyModuleRegistry.h | 2 - clang/examples/AnnotateFunctions/CMakeLists.txt | 2 +- clang/examples/PrintFunctionNames/CMakeLists.txt | 2 +- .../clang/Frontend/FrontendPluginRegistry.h | 3 - clang/include/clang/Lex/Preprocessor.h | 2 - clang/lib/Frontend/FrontendAction.cpp | 2 +- clang/lib/Lex/Preprocessor.cpp | 2 +- clang/lib/Tooling/CompilationDatabase.cpp | 2 + llvm/include/llvm/Support/Registry.h | 81 ++++++---------------- llvm/lib/CodeGen/GCMetadataPrinter.cpp | 2 + llvm/lib/CodeGen/GCStrategy.cpp | 2 + 12 files changed, 33 insertions(+), 71 deletions(-) diff --git a/clang-tools-extra/clang-tidy/ClangTidy.cpp b/clang-tools-extra/clang-tidy/ClangTidy.cpp index af1c7f9..562e1e8 100644 --- a/clang-tools-extra/clang-tidy/ClangTidy.cpp +++ b/clang-tools-extra/clang-tidy/ClangTidy.cpp @@ -47,7 +47,7 @@ using namespace clang::driver; using namespace clang::tooling; using namespace llvm; -template class llvm::Registry; +LLVM_INSTANTIATE_REGISTRY(clang::tidy::ClangTidyModuleRegistry) namespace clang { namespace tidy { diff --git a/clang-tools-extra/clang-tidy/ClangTidyModuleRegistry.h b/clang-tools-extra/clang-tidy/ClangTidyModuleRegistry.h index de2a3d7..dc44d14 100644 --- a/clang-tools-extra/clang-tidy/ClangTidyModuleRegistry.h +++ b/clang-tools-extra/clang-tidy/ClangTidyModuleRegistry.h @@ -13,8 +13,6 @@ #include "ClangTidyModule.h" #include "llvm/Support/Registry.h" -extern template class llvm::Registry; - namespace clang { namespace tidy { diff --git a/clang/examples/AnnotateFunctions/CMakeLists.txt b/clang/examples/AnnotateFunctions/CMakeLists.txt index cf564d5..5684abf 100644 --- a/clang/examples/AnnotateFunctions/CMakeLists.txt +++ b/clang/examples/AnnotateFunctions/CMakeLists.txt @@ -1,4 +1,4 @@ -add_llvm_loadable_module(AnnotateFunctions AnnotateFunctions.cpp) +add_llvm_loadable_module(AnnotateFunctions AnnotateFunctions.cpp PLUGIN_TOOL clang) if(LLVM_ENABLE_PLUGINS AND (WIN32 OR CYGWIN)) target_link_libraries(AnnotateFunctions PRIVATE diff --git a/clang/examples/PrintFunctionNames/CMakeLists.txt b/clang/examples/PrintFunctionNames/CMakeLists.txt index 5a00d50..f5f8188 100644 --- a/clang/examples/PrintFunctionNames/CMakeLists.txt +++ b/clang/examples/PrintFunctionNames/CMakeLists.txt @@ -9,7 +9,7 @@ if( NOT MSVC ) # MSVC mangles symbols differently, and endif() endif() -add_llvm_loadable_module(PrintFunctionNames PrintFunctionNames.cpp) +add_llvm_loadable_module(PrintFunctionNames PrintFunctionNames.cpp PLUGIN_TOOL clang) if(LLVM_ENABLE_PLUGINS AND (WIN32 OR CYGWIN)) target_link_libraries(PrintFunctionNames PRIVATE diff --git a/clang/include/clang/Frontend/FrontendPluginRegistry.h b/clang/include/clang/Frontend/FrontendPluginRegistry.h index ecab630c..9d7ee08 100644 --- a/clang/include/clang/Frontend/FrontendPluginRegistry.h +++ b/clang/include/clang/Frontend/FrontendPluginRegistry.h @@ -13,9 +13,6 @@ #include "clang/Frontend/FrontendAction.h" #include "llvm/Support/Registry.h" -// Instantiated in FrontendAction.cpp. -extern template class llvm::Registry; - namespace clang { /// The frontend plugin registry. diff --git a/clang/include/clang/Lex/Preprocessor.h b/clang/include/clang/Lex/Preprocessor.h index c9b7125..000df66 100644 --- a/clang/include/clang/Lex/Preprocessor.h +++ b/clang/include/clang/Lex/Preprocessor.h @@ -1972,6 +1972,4 @@ typedef llvm::Registry PragmaHandlerRegistry; } // end namespace clang -extern template class llvm::Registry; - #endif diff --git a/clang/lib/Frontend/FrontendAction.cpp b/clang/lib/Frontend/FrontendAction.cpp index d2c2a80..2945b89 100644 --- a/clang/lib/Frontend/FrontendAction.cpp +++ b/clang/lib/Frontend/FrontendAction.cpp @@ -33,7 +33,7 @@ #include using namespace clang; -template class llvm::Registry; +LLVM_INSTANTIATE_REGISTRY(FrontendPluginRegistry) namespace { diff --git a/clang/lib/Lex/Preprocessor.cpp b/clang/lib/Lex/Preprocessor.cpp index 8832c7f..f0d6872 100644 --- a/clang/lib/Lex/Preprocessor.cpp +++ b/clang/lib/Lex/Preprocessor.cpp @@ -54,7 +54,7 @@ #include using namespace clang; -template class llvm::Registry; +LLVM_INSTANTIATE_REGISTRY(PragmaHandlerRegistry) //===----------------------------------------------------------------------===// ExternalPreprocessorSource::~ExternalPreprocessorSource() { } diff --git a/clang/lib/Tooling/CompilationDatabase.cpp b/clang/lib/Tooling/CompilationDatabase.cpp index 8fc4a1f..6f95bf01 100644 --- a/clang/lib/Tooling/CompilationDatabase.cpp +++ b/clang/lib/Tooling/CompilationDatabase.cpp @@ -32,6 +32,8 @@ using namespace clang; using namespace tooling; +LLVM_INSTANTIATE_REGISTRY(CompilationDatabasePluginRegistry) + CompilationDatabase::~CompilationDatabase() {} std::unique_ptr diff --git a/llvm/include/llvm/Support/Registry.h b/llvm/include/llvm/Support/Registry.h index 27f025f..89c405e 100644 --- a/llvm/include/llvm/Support/Registry.h +++ b/llvm/include/llvm/Support/Registry.h @@ -69,13 +69,14 @@ namespace llvm { node(const entry &V) : Next(nullptr), Val(V) {} }; - static void add_node(node *N) { - if (Tail) - Tail->Next = N; - else - Head = N; - Tail = N; - } + /// Add a node to the Registry: this is the interface between the plugin and + /// the executable. + /// + /// This function is exported by the executable and called by the plugin to + /// add a node to the executable's registry. Therefore it's not defined here + /// to avoid it being instantiated in the plugin and is instead defined in + /// the executable (see LLVM_INSTANTIATE_REGISTRY below). + static void add_node(node *N); /// Iterators for registry entries. /// @@ -120,61 +121,23 @@ namespace llvm { add_node(&Node); } }; - - /// A dynamic import facility. This is used on Windows to - /// import the entries added in the plugin. - static void import(sys::DynamicLibrary &DL, const char *RegistryName) { - typedef void *(*GetRegistry)(); - std::string Name("LLVMGetRegistry_"); - Name.append(RegistryName); - GetRegistry Getter = - (GetRegistry)(intptr_t)DL.getAddressOfSymbol(Name.c_str()); - if (Getter) { - // Call the getter function in order to get the full copy of the - // registry defined in the plugin DLL, and copy them over to the - // current Registry. - typedef std::pair Info; - Info *I = static_cast(Getter()); - iterator begin(I->first); - iterator end(I->second); - for (++end; begin != end; ++begin) { - // This Node object needs to remain alive for the - // duration of the program. - add_node(new node(*begin)); - } - } - } - - /// Retrieve the data to be passed across DLL boundaries when - /// importing registries from another DLL on Windows. - static void *exportRegistry() { - static std::pair Info(Head, Tail); - return &Info; - } }; - - - // Since these are defined in a header file, plugins must be sure to export - // these symbols. - template - typename Registry::node *Registry::Head; - - template - typename Registry::node *Registry::Tail; } // end namespace llvm -#ifdef LLVM_ON_WIN32 -#define LLVM_EXPORT_REGISTRY(REGISTRY_CLASS) \ - extern "C" { \ - __declspec(dllexport) void *__cdecl LLVMGetRegistry_##REGISTRY_CLASS() { \ - return REGISTRY_CLASS::exportRegistry(); \ - } \ +/// Instantiate a registry class. +/// +/// This instantiates add_node and the Head and Tail pointers. +#define LLVM_INSTANTIATE_REGISTRY(REGISTRY_CLASS) \ + namespace llvm { \ + template<> typename REGISTRY_CLASS::node *REGISTRY_CLASS::Head = nullptr; \ + template<> typename REGISTRY_CLASS::node *REGISTRY_CLASS::Tail = nullptr; \ + template<> void REGISTRY_CLASS::add_node(REGISTRY_CLASS::node *N) { \ + if (Tail) \ + Tail->Next = N; \ + else \ + Head = N; \ + Tail = N; \ + } \ } -#define LLVM_IMPORT_REGISTRY(REGISTRY_CLASS, DL) \ - REGISTRY_CLASS::import(DL, #REGISTRY_CLASS) -#else -#define LLVM_EXPORT_REGISTRY(REGISTRY_CLASS) -#define LLVM_IMPORT_REGISTRY(REGISTRY_CLASS, DL) -#endif #endif // LLVM_SUPPORT_REGISTRY_H diff --git a/llvm/lib/CodeGen/GCMetadataPrinter.cpp b/llvm/lib/CodeGen/GCMetadataPrinter.cpp index bb8cfa1..d183c7f 100644 --- a/llvm/lib/CodeGen/GCMetadataPrinter.cpp +++ b/llvm/lib/CodeGen/GCMetadataPrinter.cpp @@ -14,6 +14,8 @@ #include "llvm/CodeGen/GCMetadataPrinter.h" using namespace llvm; +LLVM_INSTANTIATE_REGISTRY(GCMetadataPrinterRegistry) + GCMetadataPrinter::GCMetadataPrinter() {} GCMetadataPrinter::~GCMetadataPrinter() {} diff --git a/llvm/lib/CodeGen/GCStrategy.cpp b/llvm/lib/CodeGen/GCStrategy.cpp index 554d326..31ab86f 100644 --- a/llvm/lib/CodeGen/GCStrategy.cpp +++ b/llvm/lib/CodeGen/GCStrategy.cpp @@ -16,6 +16,8 @@ using namespace llvm; +LLVM_INSTANTIATE_REGISTRY(GCRegistry) + GCStrategy::GCStrategy() : UseStatepoints(false), NeededSafePoints(0), CustomReadBarriers(false), CustomWriteBarriers(false), CustomRoots(false), InitRoots(true), -- 2.7.4