blob: 0fdfef4c6a29b7044b93d01ceb7ebd590cf926f9 [file] [log] [blame]
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001//===--- 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
20namespace {
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
42namespace 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 void 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 BP->PreprocessOptions();
73 BP->PopulateLanguageMap(langMap);
74 BP->PopulateCompilationGraph(graph);
75 }
76 }
77
78}