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/FileSystem.h"
16#include "llvm/Support/FormatVariadic.h"
17#include "llvm/Support/Path.h"
18#include "llvm/Support/Process.h"
19#include "llvm/Support/WithColor.h"
20#include "llvm/Support/raw_ostream.h"
21#include <cassert>
22#include <memory>
23#include <string>
24
25using namespace clang::ssaf;
26
27namespace fs = llvm::sys::fs;
28namespace path = llvm::sys::path;
29
30namespace {
31
32//===----------------------------------------------------------------------===//
33// Error Messages
34//===----------------------------------------------------------------------===//
35
36namespace ErrorMessages {
37
38constexpr const char *CannotValidatePath = "failed to validate path '{0}': {1}";
39
40constexpr const char *ExtensionNotSupplied = "Extension not supplied";
41
42constexpr const char *NoFormatForExtension =
43 "No format registered for extension '{0}'";
44
45constexpr const char *PathDoesNotExist = "Path does not exist";
46
47constexpr const char *PathIsNotAFile = "Path is not a file";
48
49constexpr const char *OutputDirectoryMissing =
50 "Parent directory does not exist";
51
52constexpr const char *OutputDirectoryNotWritable =
53 "Parent directory is not writable";
54
55constexpr const char *FileAlreadyExists = "File already exists";
56
57constexpr const char *FailedToLoadPlugin = "failed to load plugin '{0}': {1}";
58
59} // namespace ErrorMessages
60
61llvm::StringRef ToolName;
62llvm::StringRef ToolVersion;
63
64void printVersion(llvm::raw_ostream &OS) {
65 OS << ToolName << " " << ToolVersion << "\n";
66}
67
68// Returns the SerializationFormat registered for \p Extension, or nullptr if
69// none is registered. Results are cached for the lifetime of the process.
70// FIXME: This will be revisited after we add support for registering formats
71// with extensions.
72SerializationFormat *getFormatForExtension(llvm::StringRef Extension) {
73 // This cache is not thread-safe. SSAF tools are single-threaded CLIs, so
74 // concurrent calls to this function are not expected.
75
76 // Realistically, we don't expect to encounter more than four registered
77 // formats.
78 static llvm::SmallVector<
79 std::pair<std::string, std::unique_ptr<SerializationFormat>>, 4>
80 ExtensionFormatList;
81
82 // Most recently used format is most likely to be reused again.
83 auto ReversedList = llvm::reverse(ExtensionFormatList);
84 auto It = llvm::find_if(ReversedList, [&](const auto &Entry) {
85 return Entry.first == Extension;
86 });
87 if (It != ReversedList.end()) {
88 return It->second.get();
89 }
90
91 if (!isFormatRegistered(Extension)) {
92 return nullptr;
93 }
94
95 auto Format = makeFormat(Extension);
96 SerializationFormat *Result = Format.get();
97 assert(Result &&
98 "makeFormat must return non-null for a registered extension");
99
100 ExtensionFormatList.emplace_back(Extension, std::move(Format));
101
102 return Result;
103}
104
105FormatFile fromPath(llvm::StringRef Path) {
106 llvm::StringRef Extension = path::extension(Path);
107 if (Extension.empty()) {
108 fail(ErrorMessages::CannotValidatePath, Path,
109 ErrorMessages::ExtensionNotSupplied);
110 }
111
112 Extension = Extension.drop_front();
113 SerializationFormat *Format = getFormatForExtension(Extension);
114 if (!Format) {
115 std::string BadExtension =
116 llvm::formatv(ErrorMessages::NoFormatForExtension, Extension);
117 fail(ErrorMessages::CannotValidatePath, Path, BadExtension);
118 }
119
120 return {Path.str(), Format};
121}
122
123} // namespace
124
125llvm::StringRef clang::ssaf::getToolName() { return ToolName; }
126
127[[noreturn]] void clang::ssaf::fail(const char *Msg) {
128 llvm::WithColor::error(llvm::errs(), ToolName) << Msg << "\n";
129 llvm::sys::Process::Exit(1);
130}
131
132[[noreturn]] void clang::ssaf::fail(llvm::Error Err) {
133 std::string Message = llvm::toString(std::move(Err));
134 clang::ssaf::fail(Message.data());
135}
136
138 for (const std::string &PluginPath : Paths) {
139 if (!fs::exists(PluginPath)) {
140 fail(ErrorMessages::FailedToLoadPlugin, PluginPath,
141 ErrorMessages::PathDoesNotExist);
142 }
143 std::string ErrMsg;
144 if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(PluginPath.c_str(),
145 &ErrMsg)) {
146 fail(ErrorMessages::FailedToLoadPlugin, PluginPath, ErrMsg);
147 }
148 }
149}
150
151void clang::ssaf::initTool(int argc, const char **argv, llvm::StringRef Version,
152 llvm::cl::OptionCategory &Category,
153 llvm::StringRef ToolHeading) {
154 // path::stem strips the .exe extension on Windows so ToolName is consistent.
155 ToolName = path::stem(argv[0]);
156
157 // Set tool version for the version printer.
158 ToolVersion = Version;
159
160 // Hide options unrelated to the tool from --help output.
161 llvm::cl::HideUnrelatedOptions(Category);
162
163 // Register a custom version printer for the --version flag.
164 llvm::cl::SetVersionPrinter(printVersion);
165
166 // Parse command-line arguments and exit with an error if they are invalid.
167 std::string Overview = (ToolHeading + "\n").str();
168 llvm::cl::ParseCommandLineOptions(argc, argv, Overview);
169}
170
173 if (!fs::exists(Path)) {
174 fail(ErrorMessages::CannotValidatePath, Path,
175 ErrorMessages::PathDoesNotExist);
176 }
177
178 if (!fs::is_regular_file(Path)) {
179 fail(ErrorMessages::CannotValidatePath, Path,
180 ErrorMessages::PathIsNotAFile);
181 }
182
183 return fromPath(Path);
184}
185
188 if (fs::exists(Path)) {
189 fail(ErrorMessages::CannotValidatePath, Path,
190 ErrorMessages::FileAlreadyExists);
191 }
192
193 llvm::StringRef ParentDir = path::parent_path(Path);
194 llvm::StringRef DirToCheck = ParentDir.empty() ? "." : ParentDir;
195
196 if (!fs::exists(DirToCheck)) {
197 fail(ErrorMessages::CannotValidatePath, Path,
198 ErrorMessages::OutputDirectoryMissing);
199 }
200
201 if (fs::access(DirToCheck, fs::AccessMode::Write)) {
202 fail(ErrorMessages::CannotValidatePath, Path,
203 ErrorMessages::OutputDirectoryNotWritable);
204 }
205
206 return fromPath(Path);
207}
Result
Implement __builtin_bit_cast and related operations.
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
llvm::StringRef getToolName()
Returns the name of the running tool, as set by initTool().
Definition Utils.cpp:125
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:127
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:151
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:137
#define noreturn
Definition stdnoreturn.h:17
static FormatFile fromInputPath(llvm::StringRef Path)
Validates an input path and returns a FormatFile.
Definition Utils.cpp:172
static FormatFile fromOutputPath(llvm::StringRef Path)
Validates an output path and returns a FormatFile.
Definition Utils.cpp:187
std::string Path
Definition Utils.h:70