LLVM 24.0.0git
OptionsStruct.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, and for a tool that parses its command line into one.
11// See "Declaring a Library's Options in TableGen" in llvm/docs/CommandLine.md.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_OPTION_OPTIONSSTRUCT_H
16#define LLVM_OPTION_OPTIONSSTRUCT_H
17
18#include "llvm/ADT/ArrayRef.h"
23#include "llvm/Option/Arg.h"
25#include "llvm/Option/Option.h"
28#include "llvm/Support/Error.h"
30#include <optional>
31#include <type_traits>
32
33namespace llvm {
34namespace opt {
35
36// Each accepts the spellings cl::opt accepts for the type.
37inline bool parseArgValue(StringRef S, bool &V) {
38 if (S == "true" || S == "1")
39 V = true;
40 else if (S == "false" || S == "0")
41 V = false;
42 else
43 return false;
44 return true;
45}
46
48 bool B;
49 if (!parseArgValue(S, B))
50 return false;
52 return true;
53}
54
56 V = S;
57 return true;
58}
59
60template <typename T>
61std::enable_if_t<std::is_arithmetic_v<T> && !std::is_same_v<T, bool>, bool>
63 if constexpr (std::is_floating_point_v<T>)
64 return to_float(S, V);
65 else
66 return to_integer(S, V);
67}
68
69// A std::optional member is set only when its option is given.
70template <typename T> bool parseArgValue(StringRef S, std::optional<T> &V) {
71 T X{};
72 if (!parseArgValue(S, X))
73 return false;
74 V = X;
75 return true;
76}
77
78// The generated apply() passes Alloc, which only a list member uses.
79template <typename T>
81 return parseArgValue(S, V);
82}
83
84// An enum member is set by Parse, the generated value-to-enumerator mapping.
85template <typename T, typename F>
87 return Parse(S, V);
88}
89
90// Appends Elems to a list member, whose storage comes from Alloc, keeping the
91// options struct trivially destructible.
92template <typename T>
94 static_assert(std::is_trivially_copyable_v<T>);
95 T *Storage = Alloc.Allocate<T>(List.size() + Elems.size());
96 llvm::copy(Elems, llvm::copy(List, Storage));
97 List = ArrayRef(Storage, List.size() + Elems.size());
98}
99
100// A ListField member appends the comma-separated values of each occurrence.
101template <typename T, typename F>
103 F ParseElem) {
104 SmallVector<T, 4> Elems;
105 for (StringRef Item : split(S, ',')) {
106 T Elem{};
107 if (!ParseElem(Item, Elem))
108 return false;
109 Elems.push_back(Elem);
110 }
112 return true;
113}
114
115template <typename T>
117 return parseArgValue(S, List, Alloc, [](StringRef Item, T &Elem) {
118 return parseArgValue(Item, Elem);
119 });
120}
121
122// A RepeatedField member appends the whole value of each occurrence.
123template <typename T>
125 T Elem{};
126 if (!parseArgValue(S, Elem))
127 return false;
128 appendList(List, ArrayRef(Elem), Alloc);
129 return true;
130}
131
132/// The option table of an options struct.
134public:
135 OptionsStructTable(const Tables &T, unsigned UnknownID)
136 : OptTable(T), UnknownID(UnknownID) {
137 setDashDashParsing(true);
138 }
140
141 Error parseToolArgs(int Argc, const char *const *Argv, StringSaver &Saver,
142 function_ref<bool(const Arg &)> Apply,
143 SmallVectorImpl<StringRef> &Inputs) const;
144
145private:
146 unsigned UnknownID;
147};
148
149/// Parses a tool's command line, expanding response files, into options struct
150/// \p Opts, whose string members refer to \p Argv or \p Alloc. Positional
151/// arguments, and every argument after "--", are appended to \p Inputs.
152template <typename T>
153Error parseToolArgs(T &Opts, int Argc, const char *const *Argv,
156 StringSaver Saver(Alloc);
157 return T::optTable().parseToolArgs(
158 Argc, Argv, Saver, [&](const Arg &A) { return Opts.apply(A, Alloc); },
159 Inputs);
160}
161
162} // namespace opt
163} // namespace llvm
164
165#endif // LLVM_OPTION_OPTIONSSTRUCT_H
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")
#define LLVM_ABI
Definition Compiler.h:211
#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
size_t size() const
Get the array size.
Definition ArrayRef.h:141
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
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
Saves strings in the provided stable storage and returns a StringRef with a stable character pointer.
Definition StringSaver.h:22
An efficient, type-erasing, non-owning reference to a callable.
A concrete instance of a particular driver option.
Definition Arg.h:35
void setDashDashParsing(bool Value)
Set whether "--" stops option parsing and treats all subsequent arguments as positional.
Definition OptTable.h:352
OptTable(const Tables &Tables, bool IgnoreCase=false)
Definition OptTable.cpp:76
OptionsStructTable(const Tables &T, unsigned UnknownID)
Error parseToolArgs(int Argc, const char *const *Argv, StringSaver &Saver, function_ref< bool(const Arg &)> Apply, SmallVectorImpl< StringRef > &Inputs) const
bool parseArgValue(StringRef S, bool &V)
void appendList(ArrayRef< T > &List, ArrayRef< T > Elems, BumpPtrAllocator &Alloc)
Error parseToolArgs(T &Opts, int Argc, const char *const *Argv, BumpPtrAllocator &Alloc, SmallVectorImpl< StringRef > &Inputs)
Parses a tool's command line, expanding response files, into options struct Opts, whose string member...
bool appendArgValue(StringRef S, ArrayRef< T > &List, BumpPtrAllocator &Alloc)
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:150