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
31namespace llvm {
32namespace opt {
33
34// Each accepts the spellings cl::opt accepts for the type.
35inline bool parseArgValue(StringRef S, bool &V) {
36 if (S == "true" || S == "1")
37 V = true;
38 else if (S == "false" || S == "0")
39 V = false;
40 else
41 return false;
42 return true;
43}
44
46 bool B;
47 if (!parseArgValue(S, B))
48 return false;
50 return true;
51}
52
53inline bool parseArgValue(StringRef S, StringRef &V) {
54 V = S;
55 return true;
56}
57
58template <typename T>
59std::enable_if_t<std::is_arithmetic_v<T> && !std::is_same_v<T, bool>, bool>
61 if constexpr (std::is_floating_point_v<T>)
62 return to_float(S, V);
63 else
64 return to_integer(S, V);
65}
66
67// A std::optional member is set only when its option is given.
68template <typename T> bool parseArgValue(StringRef S, std::optional<T> &V) {
69 T X{};
70 if (!parseArgValue(S, X))
71 return false;
72 V = X;
73 return true;
74}
75
76// The generated apply() passes Alloc, which only a list member uses.
77template <typename T>
79 return parseArgValue(S, V);
80}
81
82// An enum member is set by Parse, the generated value-to-enumerator mapping.
83template <typename T, typename F>
85 return Parse(S, V);
86}
87
88// A list member appends the comma-separated values of each occurrence. Its
89// storage comes from Alloc, keeping the options struct trivially destructible.
90template <typename T, typename F>
92 F ParseElem) {
93 static_assert(std::is_trivially_copyable_v<T>);
94 SmallVector<T, 4> Elems(List.begin(), List.end());
95 for (StringRef Item : split(S, ',')) {
96 T Elem{};
97 if (!ParseElem(Item, Elem))
98 return false;
99 Elems.push_back(Elem);
100 }
101 T *Storage = Alloc.Allocate<T>(Elems.size());
102 llvm::copy(Elems, Storage);
103 List = ArrayRef(Storage, Elems.size());
104 return true;
105}
106
107template <typename T>
109 return parseArgValue(S, List, Alloc, [](StringRef Item, T &Elem) {
110 return parseArgValue(Item, Elem);
111 });
112}
113
114/// An OptTable with a public constructor, shared by every options struct.
116public:
117 explicit LibraryOptTable(const Tables &T) : OptTable(T) {}
119};
120
121/// Connects an options struct to cl::ParseCommandLineOptions.
123public:
124 using ApplyFn = bool (*)(const Arg &, BumpPtrAllocator &);
125 using TableFn = const OptTable &(*)();
126 LibraryOptionsParser(TableFn Table, ApplyFn Apply, void (*Reset)())
127 : Table(Table), Apply(Apply), Reset(Reset) {}
128
129 void forEachOption(
130 function_ref<void(StringRef, StringRef, StringRef)> Fn) const override;
131 Error parse(ArrayRef<const char *> Args, unsigned &Consumed,
132 BumpPtrAllocator &Alloc) override;
133 void reset() override { Reset(); }
134
135private:
136 TableFn Table;
137 ApplyFn Apply;
138 void (*Reset)();
139};
140
141/// Registers T::Global with cl::ParseCommandLineOptions. The library owning T
142/// defines one static instance in the file that includes the struct's
143/// definitions.
144template <typename T> class RegisterLibraryOptions {
145 static_assert(std::is_trivially_destructible_v<T>,
146 "an options struct must not need an exit-time destructor");
147 LibraryOptionsParser Parser{T::optTable,
148 [](const Arg &A, BumpPtrAllocator &Alloc) {
149 return T::Global.apply(A, Alloc);
150 },
151 [] { T::Global = T(); }};
152
153public:
155};
156
157} // namespace opt
158} // namespace llvm
159
160#endif // LLVM_OPTION_LIBRARYOPTIONS_H
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")
#define LLVM_ABI
Definition Compiler.h:215
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
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
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...
An efficient, type-erasing, non-owning reference to a callable.
A concrete instance of a particular driver option.
Definition Arg.h:35
LibraryOptTable(const Tables &T)
Connects an options struct to cl::ParseCommandLineOptions.
LibraryOptionsParser(TableFn Table, ApplyFn Apply, void(*Reset)())
const OptTable &(*)() TableFn
void reset() override
Restores the default values.
bool(*)(const Arg &, BumpPtrAllocator &) ApplyFn
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...
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