clang 23.0.0git
Utils.cpp
Go to the documentation of this file.
1//===- Utils.cpp - Shared utilities for SSAF tools -----------------------===//
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
10#include "llvm/ADT/STLExtras.h"
11#include "llvm/ADT/SmallVector.h"
12#include "llvm/Support/CommandLine.h"
13#include "llvm/Support/DynamicLibrary.h"
14#include "llvm/Support/Error.h"
15#include "llvm/Support/FormatVariadic.h"
16#include "llvm/Support/Path.h"
17#include "llvm/Support/Process.h"
18#include "llvm/Support/WithColor.h"
19#include "llvm/Support/raw_ostream.h"
20#include <cassert>
21#include <memory>
22#include <string>
23
24using namespace clang;
25using namespace ssaf;
26
27namespace path = llvm::sys::path;
28
29namespace {
30
31llvm::StringRef ToolName;
32llvm::StringRef ToolVersion;
33
34void printVersion(llvm::raw_ostream &OS) {
35 OS << ToolName << " " << ToolVersion << "\n";
36}
37
38// Returns the SerializationFormat registered for \p Extension, or nullptr if
39// none is registered. Results are cached for the lifetime of the process.
40// FIXME: This will be revisited after we add support for registering formats
41// with extensions.
42SerializationFormat *getFormatForExtension(llvm::StringRef Extension) {
43 // This cache is not thread-safe. SSAF tools are single-threaded CLIs, so
44 // concurrent calls to this function are not expected.
45
46 // Realistically, we don't expect to encounter more than four registered
47 // formats.
48 static llvm::SmallVector<
49 std::pair<std::string, std::unique_ptr<SerializationFormat>>, 4>
50 ExtensionFormatList;
51
52 // Most recently used format is most likely to be reused again.
53 auto ReversedList = llvm::reverse(ExtensionFormatList);
54 auto It = llvm::find_if(ReversedList, [&](const auto &Entry) {
55 return Entry.first == Extension;
56 });
57 if (It != ReversedList.end()) {
58 return It->second.get();
59 }
60
61 if (!isFormatRegistered(Extension)) {
62 return nullptr;
63 }
64
65 auto Format = makeFormat(Extension);
66 SerializationFormat *Result = Format.get();
67 assert(Result &&
68 "makeFormat must return non-null for a registered extension");
69
70 ExtensionFormatList.emplace_back(Extension, std::move(Format));
71
72 return Result;
73}
74
75} // namespace
76
77llvm::StringRef ssaf::getToolName() { return ToolName; }
78
79[[noreturn]] void ssaf::fail(const char *Msg) {
80 llvm::WithColor::error(llvm::errs(), ToolName) << Msg << "\n";
81 llvm::sys::Process::Exit(1);
82}
83
84[[noreturn]] void ssaf::fail(llvm::Error Err) {
85 std::string Message = llvm::toString(std::move(Err));
86 ssaf::fail(Message.data());
87}
88
90 for (const std::string &PluginPath : Paths) {
91 std::string ErrMsg;
92 if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(PluginPath.c_str(),
93 &ErrMsg)) {
94 fail(ErrorMessages::FailedToLoadPlugin, PluginPath, ErrMsg);
95 }
96 }
97}
98
99void ssaf::initTool(int argc, const char **argv, llvm::StringRef Version,
100 llvm::cl::OptionCategory &Category,
101 llvm::StringRef ToolHeading) {
102 // path::stem strips the .exe extension on Windows so ToolName is consistent.
103 ToolName = path::stem(argv[0]);
104
105 // Set tool version for the version printer.
106 ToolVersion = Version;
107
108 // Hide options unrelated to the tool from --help output.
109 llvm::cl::HideUnrelatedOptions(Category);
110
111 // Register a custom version printer for the --version flag.
112 llvm::cl::SetVersionPrinter(printVersion);
113
114 // Parse command-line arguments and exit with an error if they are invalid.
115 std::string Overview = (ToolHeading + "\n").str();
116 llvm::cl::ParseCommandLineOptions(argc, argv, Overview);
117}
118
120 llvm::StringRef Extension = path::extension(Path);
121 if (Extension.empty()) {
124 }
125
126 Extension = Extension.drop_front();
127 SerializationFormat *Format = getFormatForExtension(Extension);
128 if (!Format) {
129 std::string BadExtension =
130 llvm::formatv(ErrorMessages::NoFormatForExtension, Extension);
132 }
133
134 return {Path.str(), Format};
135}
Abstract base class for serialization formats.
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
constexpr const char * ExtensionNotSupplied
Definition Utils.h:45
constexpr const char * CannotValidateSummary
Definition Utils.h:42
constexpr const char * NoFormatForExtension
Definition Utils.h:47
constexpr const char * FailedToLoadPlugin
Definition Utils.h:53
llvm::StringRef getToolName()
Returns the name of the running tool, as set by initTool().
Definition Utils.cpp:77
std::unique_ptr< SerializationFormat > makeFormat(llvm::StringRef FormatName)
Try to instantiate a SerializationFormat with a given name.
void fail(const char *Msg)
Definition Utils.cpp:79
void initTool(int argc, const char **argv, llvm::StringRef Version, llvm::cl::OptionCategory &Category, llvm::StringRef ToolHeading)
Sets ToolName, ToolVersion, and the version printer, hides unrelated command-line options,...
Definition Utils.cpp:99
bool isFormatRegistered(llvm::StringRef FormatName)
Check if a SerializationFormat was registered with a given name.
void loadPlugins(llvm::ArrayRef< std::string > Paths)
Definition Utils.cpp:89
The JSON file list parser is used to communicate input to InstallAPI.
@ Result
The result type of a method or function.
Definition TypeBase.h:905
#define noreturn
Definition stdnoreturn.h:17
SerializationFormat * Format
Definition Utils.h:92
static SummaryFile fromPath(llvm::StringRef Path)
Constructs a SummaryFile by resolving the serialization format from the file extension.
Definition Utils.cpp:119
std::string Path
Definition Utils.h:91