clang 23.0.0git
Utils.h
Go to the documentation of this file.
1//===- Utils.h - Shared utilities for SSAF tools ----------------*- C++ -*-===//
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// Shared error-handling, format-registry cache, and summary-file abstraction
10// used by clang-ssaf tools.
11//
12// All declarations live in the clang::ssaf namespace.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_CLANG_SCALABLESTATICANALYSISFRAMEWORK_TOOL_UTILS_H
17#define LLVM_CLANG_SCALABLESTATICANALYSISFRAMEWORK_TOOL_UTILS_H
18
20#include "llvm/ADT/ArrayRef.h"
21#include "llvm/ADT/StringRef.h"
22#include "llvm/Support/CommandLine.h"
23#include "llvm/Support/Error.h"
24#include "llvm/Support/FormatVariadic.h"
25#include <string>
26
27namespace clang::ssaf {
28
29//===----------------------------------------------------------------------===//
30// Tool Identity
31//===----------------------------------------------------------------------===//
32
33/// Returns the name of the running tool, as set by initTool().
34llvm::StringRef getToolName();
35
36//===----------------------------------------------------------------------===//
37// Diagnostic Utilities
38//===----------------------------------------------------------------------===//
39
40[[noreturn]] void fail(const char *Msg);
41
42template <typename... Ts>
43[[noreturn]] inline void fail(const char *Fmt, Ts &&...Args) {
44 std::string Message = llvm::formatv(Fmt, std::forward<Ts>(Args)...);
45 fail(Message.data());
46}
47
48[[noreturn]] void fail(llvm::Error Err);
49
50//===----------------------------------------------------------------------===//
51// Plugin Loading
52//===----------------------------------------------------------------------===//
53
55
56//===----------------------------------------------------------------------===//
57// Initialization
58//===----------------------------------------------------------------------===//
59
60/// Sets ToolName, ToolVersion, and the version printer, hides unrelated
61/// command-line options, and parses arguments. Must be called after InitLLVM.
62void initTool(int argc, const char **argv, llvm::StringRef Version,
63 llvm::cl::OptionCategory &Category, llvm::StringRef ToolHeading);
64
65//===----------------------------------------------------------------------===//
66// Data Structures
67//===----------------------------------------------------------------------===//
68
69struct FormatFile {
70 std::string Path;
72
73 /// Validates an input path and returns a FormatFile.
74 ///
75 /// Checks that the path exists and is a regular file, then resolves the
76 /// serialization format from the file extension. Read permission is not
77 /// checked here because llvm::sys::fs::AccessMode does not support Read; read
78 /// errors are caught when the file is opened during deserialization.
79 ///
80 /// Calls fail() and exits on any validation error.
81 static FormatFile fromInputPath(llvm::StringRef Path);
82
83 /// Validates an output path and returns a FormatFile.
84 ///
85 /// Checks that the output file does not already exist, that the parent
86 /// directory exists and is writable, then resolves the serialization format
87 /// from the file extension.
88 ///
89 /// Calls fail() and exits on any validation error.
90 static FormatFile fromOutputPath(llvm::StringRef Path);
91};
92
93} // namespace clang::ssaf
94
95#endif // LLVM_CLANG_SCALABLESTATICANALYSISFRAMEWORK_TOOL_UTILS_H
Abstract base class for serialization formats.
llvm::StringRef getToolName()
Returns the name of the running tool, as set by initTool().
Definition Utils.cpp:125
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:147
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:168
static FormatFile fromOutputPath(llvm::StringRef Path)
Validates an output path and returns a FormatFile.
Definition Utils.cpp:183
SerializationFormat * Format
Definition Utils.h:71
std::string Path
Definition Utils.h:70