Get rid of exceptions in llvmc.
[oota-llvm.git] / lib / CompilerDriver / Plugin.cpp
1 //===--- Plugin.cpp - The LLVM Compiler Driver ------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open
6 // Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  Plugin support.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/CompilerDriver/Plugin.h"
15 #include "llvm/Support/ManagedStatic.h"
16 #include "llvm/System/Mutex.h"
17 #include <algorithm>
18 #include <vector>
19
20 namespace {
21
22   // Registry::Add<> does not do lifetime management (probably issues
23   // with static constructor/destructor ordering), so we have to
24   // implement it here.
25   //
26   // All this static registration/life-before-main model seems
27   // unnecessary convoluted to me.
28
29   static bool pluginListInitialized = false;
30   typedef std::vector<const llvmc::BasePlugin*> PluginList;
31   static PluginList Plugins;
32   static llvm::ManagedStatic<llvm::sys::SmartMutex<true> > PluginMutex;
33
34   struct ByPriority {
35     bool operator()(const llvmc::BasePlugin* lhs,
36                     const llvmc::BasePlugin* rhs) {
37       return lhs->Priority() < rhs->Priority();
38     }
39   };
40 }
41
42 namespace llvmc {
43
44   PluginLoader::PluginLoader() {
45     llvm::sys::SmartScopedLock<true> Lock(*PluginMutex);
46     if (!pluginListInitialized) {
47       for (PluginRegistry::iterator B = PluginRegistry::begin(),
48              E = PluginRegistry::end(); B != E; ++B)
49         Plugins.push_back(B->instantiate());
50       std::sort(Plugins.begin(), Plugins.end(), ByPriority());
51     }
52     pluginListInitialized = true;
53   }
54
55   PluginLoader::~PluginLoader() {
56     llvm::sys::SmartScopedLock<true> Lock(*PluginMutex);
57     if (pluginListInitialized) {
58       for (PluginList::iterator B = Plugins.begin(), E = Plugins.end();
59            B != E; ++B)
60         delete (*B);
61     }
62     pluginListInitialized = false;
63   }
64
65   int PluginLoader::RunInitialization(LanguageMap& langMap,
66                                        CompilationGraph& graph) const
67   {
68     llvm::sys::SmartScopedLock<true> Lock(*PluginMutex);
69     for (PluginList::iterator B = Plugins.begin(), E = Plugins.end();
70          B != E; ++B) {
71       const BasePlugin* BP = *B;
72       if (int ret = BP->PreprocessOptions())
73         return ret;
74       if (int ret = BP->PopulateLanguageMap(langMap))
75         return ret;
76       if (int ret = BP->PopulateCompilationGraph(graph))
77         return ret;
78     }
79
80     return 0;
81   }
82
83 }