xref: /freebsd/contrib/llvm-project/llvm/lib/Support/PluginLoader.cpp (revision 2f513db72b034fd5ef7f080b11be5c711c15186a)
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.
10 //
11 //===----------------------------------------------------------------------===//
12 
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;
21 
22 static ManagedStatic<std::vector<std::string> > Plugins;
23 static ManagedStatic<sys::SmartMutex<true> > PluginsLock;
24 
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);
33   }
34 }
35 
36 unsigned PluginLoader::getNumPlugins() {
37   sys::SmartScopedLock<true> Lock(*PluginsLock);
38   return Plugins.isConstructed() ? Plugins->size() : 0;
39 }
40 
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];
46 }
47