blob: abb7b57ee0c60fdb972836e497db0d47b513e327 [file] [log] [blame]
Philip Pfaffe53016772018-04-05 15:04:13 +00001//===- 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 Weber8e398a42018-05-16 16:29:05 +000011#include "llvm/Config/config.h"
Philip Pfaffe53016772018-04-05 15:04:13 +000012#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 Sergeevff886492018-10-17 10:36:23 +000018#include "llvm/Testing/Support/Error.h"
Philip Pfaffe53016772018-04-05 15:04:13 +000019#include "llvm/Transforms/Scalar/LoopPassManager.h"
20#include "gtest/gtest.h"
21
22#include "TestPlugin.h"
23
Gabor Buellae914bb32018-04-25 12:15:34 +000024#include <cstdint>
25
Philip Pfaffe53016772018-04-05 15:04:13 +000026using namespace llvm;
27
28void anchor() {}
29
30static 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 Buellae914bb32018-04-25 12:15:34 +000034 void *Ptr = (void *)(intptr_t)anchor;
Philip Pfaffe53016772018-04-05 15:04:13 +000035 std::string Path = sys::fs::getMainExecutable(Argv0, Ptr);
36 llvm::SmallString<256> Buf{sys::path::parent_path(Path)};
Nico Weber8e398a42018-05-16 16:29:05 +000037 sys::path::append(Buf, (Name + LTDL_SHLIB_EXT).c_str());
Philip Pfaffe53016772018-04-05 15:04:13 +000038 return Buf.str();
39}
40
41TEST(PluginsTests, LoadPlugin) {
Reid Kleckner22fafe62018-04-25 20:16:24 +000042#if !defined(LLVM_ENABLE_PLUGINS)
43 // Disable the test if plugins are disabled.
44 return;
45#endif
46
Philip Pfaffe53016772018-04-05 15:04:13 +000047 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 Sergeevff886492018-10-17 10:36:23 +000058 ASSERT_THAT_ERROR(PB.parsePassPipeline(PM, "plugin-pass"), Failed());
Philip Pfaffe53016772018-04-05 15:04:13 +000059
60 Plugin->registerPassBuilderCallbacks(PB);
Fedor Sergeevff886492018-10-17 10:36:23 +000061 ASSERT_THAT_ERROR(PB.parsePassPipeline(PM, "plugin-pass"), Succeeded());
Philip Pfaffe53016772018-04-05 15:04:13 +000062}