Guard the plugin loader.
[oota-llvm.git] / lib / Support / PluginLoader.cpp
1 //===-- PluginLoader.cpp - Implement -load command line option ------------===//
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 // This file implements the -load <plugin> command line option handler.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DONT_GET_PLUGIN_LOADER_OPTION
15 #include "llvm/Support/ManagedStatic.h"
16 #include "llvm/Support/PluginLoader.h"
17 #include "llvm/Support/Streams.h"
18 #include "llvm/System/DynamicLibrary.h"
19 #include "llvm/System/Mutex.h"
20 #include <ostream>
21 #include <vector>
22 using namespace llvm;
23
24 static ManagedStatic<std::vector<std::string> > Plugins;
25 static ManagedStatic<sys::SmartMutex<true> > PluginsLock;
26
27 void PluginLoader::operator=(const std::string &Filename) {
28   sys::SmartScopedLock<true> Lock(&*PluginsLock);
29   std::string Error;
30   if (sys::DynamicLibrary::LoadLibraryPermanently(Filename.c_str(), &Error)) {
31     cerr << "Error opening '" << Filename << "': " << Error
32          << "\n  -load request ignored.\n";
33   } else {
34     Plugins->push_back(Filename);
35   }
36 }
37
38 unsigned PluginLoader::getNumPlugins() {
39   sys::SmartScopedLock<true> Lock(&*PluginsLock);
40   return Plugins.isConstructed() ? Plugins->size() : 0;
41 }
42
43 std::string &PluginLoader::getPlugin(unsigned num) {
44   sys::SmartScopedLock<true> Lock(&*PluginsLock);
45   assert(Plugins.isConstructed() && num < Plugins->size() &&
46          "Asking for an out of bounds plugin");
47   return (*Plugins)[num];
48 }