PluginLoader.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. #define DONT_GET_PLUGIN_LOADER_OPTION
  13. #include "llvm/Support/PluginLoader.h"
  14. #include "llvm/Support/DynamicLibrary.h"
  15. #include "llvm/Support/ManagedStatic.h"
  16. #include "llvm/Support/Mutex.h"
  17. #include "llvm/Support/raw_ostream.h"
  18. #include <vector>
  19. using namespace llvm;
  20. static ManagedStatic<std::vector<std::string> > Plugins;
  21. static ManagedStatic<sys::SmartMutex<true> > PluginsLock;
  22. void PluginLoader::operator=(const std::string &Filename) {
  23. sys::SmartScopedLock<true> Lock(*PluginsLock);
  24. std::string Error;
  25. if (sys::DynamicLibrary::LoadLibraryPermanently(Filename.c_str(), &Error)) {
  26. errs() << "Error opening '" << Filename << "': " << Error
  27. << "\n -load request ignored.\n";
  28. } else {
  29. Plugins->push_back(Filename);
  30. }
  31. }
  32. unsigned PluginLoader::getNumPlugins() {
  33. sys::SmartScopedLock<true> Lock(*PluginsLock);
  34. return Plugins.isConstructed() ? Plugins->size() : 0;
  35. }
  36. std::string &PluginLoader::getPlugin(unsigned num) {
  37. sys::SmartScopedLock<true> Lock(*PluginsLock);
  38. assert(Plugins.isConstructed() && num < Plugins->size() &&
  39. "Asking for an out of bounds plugin");
  40. return (*Plugins)[num];
  41. }