2018-04-05 23:04:13 +08:00
|
|
|
//===- unittests/Passes/Plugins/PluginsTest.cpp ---------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-04-05 23:04:13 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Analysis/CGSCCPassManager.h"
|
2018-05-17 00:29:05 +08:00
|
|
|
#include "llvm/Config/config.h"
|
2018-04-05 23:04:13 +08:00
|
|
|
#include "llvm/IR/PassManager.h"
|
|
|
|
#include "llvm/Passes/PassBuilder.h"
|
|
|
|
#include "llvm/Passes/PassPlugin.h"
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
2018-10-17 18:36:23 +08:00
|
|
|
#include "llvm/Testing/Support/Error.h"
|
2018-04-05 23:04:13 +08:00
|
|
|
#include "llvm/Transforms/Scalar/LoopPassManager.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
#include "TestPlugin.h"
|
|
|
|
|
2018-04-25 20:15:34 +08:00
|
|
|
#include <cstdint>
|
|
|
|
|
2018-04-05 23:04:13 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
void anchor() {}
|
|
|
|
|
|
|
|
static std::string LibPath(const std::string Name = "TestPlugin") {
|
|
|
|
const std::vector<testing::internal::string> &Argvs =
|
|
|
|
testing::internal::GetArgvs();
|
|
|
|
const char *Argv0 = Argvs.size() > 0 ? Argvs[0].c_str() : "PluginsTests";
|
2018-04-25 20:15:34 +08:00
|
|
|
void *Ptr = (void *)(intptr_t)anchor;
|
2018-04-05 23:04:13 +08:00
|
|
|
std::string Path = sys::fs::getMainExecutable(Argv0, Ptr);
|
|
|
|
llvm::SmallString<256> Buf{sys::path::parent_path(Path)};
|
2018-05-17 00:29:05 +08:00
|
|
|
sys::path::append(Buf, (Name + LTDL_SHLIB_EXT).c_str());
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(Buf.str());
|
2018-04-05 23:04:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(PluginsTests, LoadPlugin) {
|
2018-04-26 04:16:24 +08:00
|
|
|
#if !defined(LLVM_ENABLE_PLUGINS)
|
|
|
|
// Disable the test if plugins are disabled.
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2018-04-05 23:04:13 +08:00
|
|
|
auto PluginPath = LibPath();
|
|
|
|
ASSERT_NE("", PluginPath);
|
|
|
|
|
|
|
|
Expected<PassPlugin> Plugin = PassPlugin::Load(PluginPath);
|
|
|
|
ASSERT_TRUE(!!Plugin) << "Plugin path: " << PluginPath;
|
|
|
|
|
|
|
|
ASSERT_EQ(TEST_PLUGIN_NAME, Plugin->getPluginName());
|
|
|
|
ASSERT_EQ(TEST_PLUGIN_VERSION, Plugin->getPluginVersion());
|
|
|
|
|
|
|
|
PassBuilder PB;
|
|
|
|
ModulePassManager PM;
|
2018-10-17 18:36:23 +08:00
|
|
|
ASSERT_THAT_ERROR(PB.parsePassPipeline(PM, "plugin-pass"), Failed());
|
2018-04-05 23:04:13 +08:00
|
|
|
|
|
|
|
Plugin->registerPassBuilderCallbacks(PB);
|
2018-10-17 18:36:23 +08:00
|
|
|
ASSERT_THAT_ERROR(PB.parsePassPipeline(PM, "plugin-pass"), Succeeded());
|
2018-04-05 23:04:13 +08:00
|
|
|
}
|