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
54inline bool parseArgValue(StringRef S, StringRef &V) {
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 explicit LibraryOptionsParser(std::in_place_type_t<T>)
128 : LibraryOptions(T::PerContext), Table(T::optTable), Global(&T::Global),
129 Default(&T::Default),
130 Apply([](void *Opts, const Arg &A, BumpPtrAllocator &Alloc) {
131 return static_cast<T *>(Opts)->apply(A, Alloc);
132 }),
133 Copy([](BumpPtrAllocator &Alloc) -> void * {
134 return new (Alloc) T(T::Default);
135 }),
136 Reset([] { T::Global = T(); }) {}
137
138 void forEachOption(
139 function_ref<void(StringRef, StringRef, StringRef)> Fn) const override;
140 Error parse(void *Opts, ArrayRef<const char *> Args, unsigned &Consumed,
141 BumpPtrAllocator &Alloc) override;
142 void *global() override { return Global; }
143 const void *getDefault() const override { return Default; }
144 void *copyDefault(BumpPtrAllocator &Alloc) const override {
145 return Copy(Alloc);
146 }
147 void reset() override { Reset(); }
148
149private:
150 const OptTable &(*Table)();
151 void *Global;
152 const void *Default;
153 bool (*Apply)(void *Opts, const Arg &A, BumpPtrAllocator &Alloc);
154 void *(*Copy)(BumpPtrAllocator &Alloc);
155 void (*Reset)();
156};
157
158/// Registers T::Global with cl::ParseCommandLineOptions. The library owning T
159/// defines one static instance in the file that includes the struct's
160/// definitions.
161template <typename T> class RegisterLibraryOptions {
162 static_assert(std::is_trivially_destructible_v<T>,
163 "an options struct must not need an exit-time destructor");
164 LibraryOptionsParser Parser{std::in_place_type<T>};
165
166public:
168};
169
170} // namespace opt
171} // namespace llvm
172
173#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:857
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...
LibraryOptions(bool PerContext=false)
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.
void * global() override
Returns the values that ParseCommandLineOptions sets.
LibraryOptionsParser(std::in_place_type_t< T >)
const void * getDefault() const override
Returns the default values.
void reset() override
Restores the default values.
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(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...
@ Global
Append to llvm.global_dtors.
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