[ThinLTO] Add code comment. NFC
[llvm-complete.git] / lib / Support / PluginLoader.cpp
blob6fe195ffda7ac8fff5c97d15c6a8a7c928fca96b
1 //===-- PluginLoader.cpp - Implement -load command line option ------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the -load <plugin> command line option handler.
11 //===----------------------------------------------------------------------===//
13 #define DONT_GET_PLUGIN_LOADER_OPTION
14 #include "llvm/Support/PluginLoader.h"
15 #include "llvm/Support/DynamicLibrary.h"
16 #include "llvm/Support/ManagedStatic.h"
17 #include "llvm/Support/Mutex.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include <vector>
20 using namespace llvm;
22 static ManagedStatic<std::vector<std::string> > Plugins;
23 static ManagedStatic<sys::SmartMutex<true> > PluginsLock;
25 void PluginLoader::operator=(const std::string &Filename) {
26 sys::SmartScopedLock<true> Lock(*PluginsLock);
27 std::string Error;
28 if (sys::DynamicLibrary::LoadLibraryPermanently(Filename.c_str(), &Error)) {
29 errs() << "Error opening '" << Filename << "': " << Error
30 << "\n -load request ignored.\n";
31 } else {
32 Plugins->push_back(Filename);
36 unsigned PluginLoader::getNumPlugins() {
37 sys::SmartScopedLock<true> Lock(*PluginsLock);
38 return Plugins.isConstructed() ? Plugins->size() : 0;
41 std::string &PluginLoader::getPlugin(unsigned num) {
42 sys::SmartScopedLock<true> Lock(*PluginsLock);
43 assert(Plugins.isConstructed() && num < Plugins->size() &&
44 "Asking for an out of bounds plugin");
45 return (*Plugins)[num];