Philip Pfaffe | 5301677 | 2018-04-05 15:04:13 +0000 | [diff] [blame] | 1 | //===- unittests/Passes/Plugins/PluginsTest.cpp ---------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/Analysis/CGSCCPassManager.h" |
Nico Weber | 8e398a4 | 2018-05-16 16:29:05 +0000 | [diff] [blame] | 11 | #include "llvm/Config/config.h" |
Philip Pfaffe | 5301677 | 2018-04-05 15:04:13 +0000 | [diff] [blame] | 12 | #include "llvm/IR/PassManager.h" |
| 13 | #include "llvm/Passes/PassBuilder.h" |
| 14 | #include "llvm/Passes/PassPlugin.h" |
| 15 | #include "llvm/Support/FileSystem.h" |
| 16 | #include "llvm/Support/ManagedStatic.h" |
| 17 | #include "llvm/Support/Path.h" |
Fedor Sergeev | ff88649 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 18 | #include "llvm/Testing/Support/Error.h" |
Philip Pfaffe | 5301677 | 2018-04-05 15:04:13 +0000 | [diff] [blame] | 19 | #include "llvm/Transforms/Scalar/LoopPassManager.h" |
| 20 | #include "gtest/gtest.h" |
| 21 | |
| 22 | #include "TestPlugin.h" |
| 23 | |
Gabor Buella | e914bb3 | 2018-04-25 12:15:34 +0000 | [diff] [blame] | 24 | #include <cstdint> |
| 25 | |
Philip Pfaffe | 5301677 | 2018-04-05 15:04:13 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
| 28 | void anchor() {} |
| 29 | |
| 30 | static std::string LibPath(const std::string Name = "TestPlugin") { |
| 31 | const std::vector<testing::internal::string> &Argvs = |
| 32 | testing::internal::GetArgvs(); |
| 33 | const char *Argv0 = Argvs.size() > 0 ? Argvs[0].c_str() : "PluginsTests"; |
Gabor Buella | e914bb3 | 2018-04-25 12:15:34 +0000 | [diff] [blame] | 34 | void *Ptr = (void *)(intptr_t)anchor; |
Philip Pfaffe | 5301677 | 2018-04-05 15:04:13 +0000 | [diff] [blame] | 35 | std::string Path = sys::fs::getMainExecutable(Argv0, Ptr); |
| 36 | llvm::SmallString<256> Buf{sys::path::parent_path(Path)}; |
Nico Weber | 8e398a4 | 2018-05-16 16:29:05 +0000 | [diff] [blame] | 37 | sys::path::append(Buf, (Name + LTDL_SHLIB_EXT).c_str()); |
Philip Pfaffe | 5301677 | 2018-04-05 15:04:13 +0000 | [diff] [blame] | 38 | return Buf.str(); |
| 39 | } |
| 40 | |
| 41 | TEST(PluginsTests, LoadPlugin) { |
Reid Kleckner | 22fafe6 | 2018-04-25 20:16:24 +0000 | [diff] [blame] | 42 | #if !defined(LLVM_ENABLE_PLUGINS) |
| 43 | // Disable the test if plugins are disabled. |
| 44 | return; |
| 45 | #endif |
| 46 | |
Philip Pfaffe | 5301677 | 2018-04-05 15:04:13 +0000 | [diff] [blame] | 47 | auto PluginPath = LibPath(); |
| 48 | ASSERT_NE("", PluginPath); |
| 49 | |
| 50 | Expected<PassPlugin> Plugin = PassPlugin::Load(PluginPath); |
| 51 | ASSERT_TRUE(!!Plugin) << "Plugin path: " << PluginPath; |
| 52 | |
| 53 | ASSERT_EQ(TEST_PLUGIN_NAME, Plugin->getPluginName()); |
| 54 | ASSERT_EQ(TEST_PLUGIN_VERSION, Plugin->getPluginVersion()); |
| 55 | |
| 56 | PassBuilder PB; |
| 57 | ModulePassManager PM; |
Fedor Sergeev | ff88649 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 58 | ASSERT_THAT_ERROR(PB.parsePassPipeline(PM, "plugin-pass"), Failed()); |
Philip Pfaffe | 5301677 | 2018-04-05 15:04:13 +0000 | [diff] [blame] | 59 | |
| 60 | Plugin->registerPassBuilderCallbacks(PB); |
Fedor Sergeev | ff88649 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 61 | ASSERT_THAT_ERROR(PB.parsePassPipeline(PM, "plugin-pass"), Succeeded()); |
Philip Pfaffe | 5301677 | 2018-04-05 15:04:13 +0000 | [diff] [blame] | 62 | } |