[clang] Add test for CWG190 "Layout-compatible POD-struct types" (#121668)
[llvm-project.git] / llvm / lib / Support / PluginLoader.cpp
blobcbe413ce25562b4ff88a4a1bc16ce7de0188f123
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.
11 //===----------------------------------------------------------------------===//
13 #define DONT_GET_PLUGIN_LOADER_OPTION
14 #include "llvm/Support/PluginLoader.h"
15 #include "llvm/Support/DynamicLibrary.h"
16 #include "llvm/Support/Mutex.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include <vector>
19 using namespace llvm;
21 namespace {
23 struct Plugins {
24 sys::SmartMutex<true> Lock;
25 std::vector<std::string> List;
28 Plugins &getPlugins() {
29 static Plugins P;
30 return P;
33 } // anonymous namespace
35 void PluginLoader::operator=(const std::string &Filename) {
36 auto &P = getPlugins();
37 sys::SmartScopedLock<true> Lock(P.Lock);
38 std::string Error;
39 if (sys::DynamicLibrary::LoadLibraryPermanently(Filename.c_str(), &Error)) {
40 errs() << "Error opening '" << Filename << "': " << Error
41 << "\n -load request ignored.\n";
42 } else {
43 P.List.push_back(Filename);
47 unsigned PluginLoader::getNumPlugins() {
48 auto &P = getPlugins();
49 sys::SmartScopedLock<true> Lock(P.Lock);
50 return P.List.size();
53 std::string &PluginLoader::getPlugin(unsigned num) {
54 auto &P = getPlugins();
55 sys::SmartScopedLock<true> Lock(P.Lock);
56 assert(num < P.List.size() && "Asking for an out of bounds plugin");
57 return P.List[num];