clang 24.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_SCALABLESTATICANALYSIS_TOOL_UTILS_H
17#define LLVM_CLANG_SCALABLESTATICANALYSIS_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 "llvm/Support/WithColor.h"
26#include "llvm/TargetParser/Triple.h"
27#include <string>
28
29namespace clang::ssaf {
30
31//===----------------------------------------------------------------------===//
32// Tool Identity
33//===----------------------------------------------------------------------===//
34
35/// Returns the name of the running tool, as set by initTool().
36llvm::StringRef getToolName();
37
38//===----------------------------------------------------------------------===//
39// Diagnostic Utilities
40//===----------------------------------------------------------------------===//
41
42[[noreturn]] void fail(const char *Msg);
43
44template <typename... Ts>
45[[noreturn]] inline void fail(const char *Fmt, Ts &&...Args) {
46 std::string Message = llvm::formatv(Fmt, std::forward<Ts>(Args)...);
47 fail(Message.data());
48}
49
50[[noreturn]] void fail(llvm::Error Err);
51
52/// Number of spaces per indentation level used by info().
53constexpr unsigned IndentationWidth = 2;
54
55/// Prints an indented note to stderr when Verbose is set.
56template <typename... Ts>
57inline void info(bool Verbose, unsigned Level, const char *Fmt, Ts &&...Args) {
58 if (Verbose) {
59 llvm::WithColor::note()
60 << std::string(Level * IndentationWidth, ' ') << "- "
61 << llvm::formatv(Fmt, std::forward<Ts>(Args)...) << "\n";
62 }
63}
64
65//===----------------------------------------------------------------------===//
66// Plugin Loading
67//===----------------------------------------------------------------------===//
68
70
71//===----------------------------------------------------------------------===//
72// Initialization
73//===----------------------------------------------------------------------===//
74
75/// Sets ToolName, ToolVersion, and the version printer, hides unrelated
76/// command-line options, and parses arguments. Must be called after InitLLVM.
77void initTool(int argc, const char **argv, llvm::StringRef Version,
78 llvm::cl::OptionCategory &Category, llvm::StringRef ToolHeading);
79
80//===----------------------------------------------------------------------===//
81// Target Triples
82//===----------------------------------------------------------------------===//
83
84/// Parses and validates a target triple supplied on the command line.
85///
86/// \param FlagName The option supplying \p Value, named in the diagnostic.
87/// \param Value The triple as spelled by the user. Must not be empty.
88/// \returns The parsed triple. Calls fail() and exits if the architecture is
89/// unrecognized.
90llvm::Triple parseTargetTripleOrFail(llvm::StringRef FlagName,
91 llvm::StringRef Value);
92
93//===----------------------------------------------------------------------===//
94// Data Structures
95//===----------------------------------------------------------------------===//
96
97struct FormatFile {
98 std::string Path;
100
101 /// Validates an input path and returns a FormatFile.
102 ///
103 /// Checks that the path exists and is a regular file, then resolves the
104 /// serialization format from the file extension. Read permission is not
105 /// checked here because llvm::sys::fs::AccessMode does not support Read; read
106 /// errors are caught when the file is opened during deserialization.
107 ///
108 /// Calls fail() and exits on any validation error.
109 static FormatFile fromInputPath(llvm::StringRef Path);
110
111 /// Validates an output path and returns a FormatFile.
112 ///
113 /// Checks that the output file does not already exist, that the parent
114 /// directory exists and is writable, then resolves the serialization format
115 /// from the file extension.
116 ///
117 /// Calls fail() and exits on any validation error.
118 static FormatFile fromOutputPath(llvm::StringRef Path);
119};
120
121} // namespace clang::ssaf
122
123#endif // LLVM_CLANG_SCALABLESTATICANALYSIS_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:128
constexpr unsigned IndentationWidth
Number of spaces per indentation level used by info().
Definition Utils.h:53
llvm::Triple parseTargetTripleOrFail(llvm::StringRef FlagName, llvm::StringRef Value)
Parses and validates a target triple supplied on the command line.
Definition Utils.cpp:154
void fail(const char *Msg)
Definition Utils.cpp:130
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:172
void info(bool Verbose, unsigned Level, const char *Fmt, Ts &&...Args)
Prints an indented note to stderr when Verbose is set.
Definition Utils.h:57
void loadPlugins(llvm::ArrayRef< std::string > Paths)
Definition Utils.cpp:140
#define noreturn
Definition stdnoreturn.h:17
static FormatFile fromInputPath(llvm::StringRef Path)
Validates an input path and returns a FormatFile.
Definition Utils.cpp:193
static FormatFile fromOutputPath(llvm::StringRef Path)
Validates an output path and returns a FormatFile.
Definition Utils.cpp:208
SerializationFormat * Format
Definition Utils.h:99
std::string Path
Definition Utils.h:98