LLVM 24.0.0git
LibraryOptions.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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// Support for the options struct that -gen-opt-parser-defs generates from an
10// OptionsStruct def. See "Declaring a Library's Options in TableGen" in
11// llvm/docs/CommandLine.md.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_OPTION_LIBRARYOPTIONS_H
16#define LLVM_OPTION_LIBRARYOPTIONS_H
17
18#include "llvm/ADT/ArrayRef.h"
22#include "llvm/Option/Arg.h"
24#include "llvm/Option/Option.h"
28#include <optional>
29#include <type_traits>
30#include <utility>
31
32namespace llvm {
33namespace opt {
34
35// Each accepts the spellings cl::opt accepts for the type.
36inline bool parseArgValue(StringRef S, bool &V) {
37 if (S == "true" || S == "1")
38 V = true;
39 else if (S == "false" || S == "0")
40 V = false;
41 else
42 return false;
43 return true;
44}
45
47 bool B;
48 if (!parseArgValue(S, B))
49 return false;
51 return true;
52}
53
55 V = S;
56 return true;
57}
58
59template <typename T>
60std::enable_if_t<std::is_arithmetic_v<T> && !std::is_same_v<T, bool>, bool>
62 if constexpr (std::is_floating_point_v<T>)
63 return to_float(S, V);
64 else
65 return to_integer(S, V);
66}
67
68// A std::optional member is set only when its option is given.
69template <typename T> bool parseArgValue(StringRef S, std::optional<T> &V) {
70 T X{};
71 if (!parseArgValue(S, X))
72 return false;
73 V = X;
74 return true;
75}
76
77// The generated apply() passes Alloc, which only a list member uses.
78template <typename T>
80 return parseArgValue(S, V);
81}
82
83// An enum member is set by Parse, the generated value-to-enumerator mapping.
84template <typename T, typename F>
86 return Parse(S, V);
87}
88
89// A list member appends the comma-separated values of each occurrence. Its
90// storage comes from Alloc, keeping the options struct trivially destructible.
91template <typename T, typename F>
93 F ParseElem) {
94 static_assert(std::is_trivially_copyable_v<T>);
95 SmallVector<T, 4> Elems(List.begin(), List.end());
96 for (StringRef Item : split(S, ',')) {
97 T Elem{};
98 if (!ParseElem(Item, Elem))
99 return false;
100 Elems.push_back(Elem);
101 }
102 T *Storage = Alloc.Allocate<T>(Elems.size());
103 llvm::copy(Elems, Storage);
104 List = ArrayRef(Storage, Elems.size());
105 return true;
106}
107
108template <typename T>
110 return parseArgValue(S, List, Alloc, [](StringRef Item, T &Elem) {
111 return parseArgValue(Item, Elem);
112 });
113}
114
115/// An OptTable with a public constructor, shared by every options struct.
117public:
118 explicit LibraryOptTable(const Tables &T) : OptTable(T) {}
120};
121
122/// Connects options struct T to cl::parseCommandLineOptions and
123/// cl::LibraryOptionsContext.
125public:
126 template <typename T>
127 constexpr explicit LibraryOptionsParser(std::in_place_type_t<T>)
128 : Table(T::optTable), Default(&T::Default),
129 Apply([](void *Opts, const Arg &A, BumpPtrAllocator &Alloc) {
130 return static_cast<T *>(Opts)->apply(A, Alloc);
131 }),
132 Copy([](BumpPtrAllocator &Alloc) -> void * {
133 return new (Alloc) T(T::Default);
134 }) {}
135
136 void forEachOption(
137 function_ref<void(StringRef, StringRef, StringRef)> Fn) const override;
138 Error parse(void *Opts, ArrayRef<const char *> Args, unsigned &Consumed,
139 BumpPtrAllocator &Alloc) const override;
140 const void *getDefault() const override { return Default; }
141 void *copyDefault(BumpPtrAllocator &Alloc) const override {
142 return Copy(Alloc);
143 }
144
145private:
146 const OptTable &(*Table)();
147 const void *Default;
148 bool (*Apply)(void *Opts, const Arg &A, BumpPtrAllocator &Alloc);
149 void *(*Copy)(BumpPtrAllocator &Alloc);
150};
151
152/// Registers T with cl::parseCommandLineOptions. The library owning T
153/// defines one static instance in the file that includes the struct's
154/// definitions.
155template <typename T> class RegisterLibraryOptions {
156 static_assert(std::is_trivially_destructible_v<T>,
157 "an options struct must not need an exit-time destructor");
158 LibraryOptionsParser Parser{std::in_place_type<T>};
159
160public:
162};
163
164} // namespace opt
165} // namespace llvm
166
167/// Defines the file-scope registration of options struct T for a library that
168/// has no initialization function to register it from, exempting this static
169/// constructor from -Wglobal-constructors.
170// clang-format off
171#ifdef __clang__
172#define LLVM_REGISTER_LIBRARY_OPTIONS(T) \
173 _Pragma("clang diagnostic push") \
174 _Pragma("clang diagnostic ignored \"-Wglobal-constructors\"") \
175 static llvm::opt::RegisterLibraryOptions<T> Registration; \
176 _Pragma("clang diagnostic pop")
177#else
178#define LLVM_REGISTER_LIBRARY_OPTIONS(T) \
179 static llvm::opt::RegisterLibraryOptions<T> Registration;
180#endif
181// clang-format on
182
183#endif // LLVM_OPTION_LIBRARYOPTIONS_H
aarch64 promote const
This file defines the BumpPtrAllocator interface.
Defines the llvm::Arg class for parsed arguments.
#define X(NUM, ENUM, NAME)
Definition ELF.h:665
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static COFFOptTable optTable
#define LLVM_ABI
Definition Compiler.h:211
static llvm::Error parse(GsymDataExtractor &Data, uint64_t BaseAddr, LineEntryCallback const &Callback)
Definition LineTable.cpp:54
#define F(x, y, z)
Definition MD5.cpp:54
#define T
This file defines the SmallVector class.
This file contains some functions that are useful when dealing with strings.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
The options of a library that declares them in TableGen rather than as cl::opt (see llvm/Option/Libra...
A concrete instance of a particular driver option.
Definition Arg.h:35
LibraryOptTable(const Tables &T)
Connects options struct T to cl::parseCommandLineOptions and cl::LibraryOptionsContext.
const void * getDefault() const override
Returns the default values.
constexpr LibraryOptionsParser(std::in_place_type_t< T >)
void * copyDefault(BumpPtrAllocator &Alloc) const override
Returns a copy of getDefault() allocated from Alloc.
Provide access to the Option info table.
Definition OptTable.h:55
OptTable(const Tables &Tables, bool IgnoreCase=false)
Definition OptTable.cpp:76
LLVM_ABI void addLibraryOptions(const LibraryOptions &L)
Makes ParseCommandLineOptions recognize L's options.
bool parseArgValue(StringRef S, bool &V)
This is an optimization pass for GlobalISel generic memory operations.
bool to_float(const Twine &T, float &Num)
iterator_range< SplittingIterator > split(StringRef Str, StringRef Separator)
Split the specified string over a separator and return a range-compatible iterable over its partition...
ArrayRef(const T &OneElt) -> ArrayRef< T >
OutputIt copy(R &&Range, OutputIt Out)
Definition STLExtras.h:1901
bool to_integer(StringRef S, N &Num, unsigned Base=0)
Convert the string S to an integer of the specified type using the radix Base. If Base is 0,...
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:391
The tables TableGen emits for an option set under OPTTABLE_CODE.
Definition OptTable.h:151