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
20#include "llvm/Option/Arg.h"
22#include "llvm/Option/Option.h"
25#include <optional>
26#include <type_traits>
27
28namespace llvm {
29namespace opt {
30
31// Each accepts the spellings cl::opt accepts for the type.
32inline bool parseArgValue(StringRef S, bool &V) {
33 if (S == "true" || S == "1")
34 V = true;
35 else if (S == "false" || S == "0")
36 V = false;
37 else
38 return false;
39 return true;
40}
41
43 bool B;
44 if (!parseArgValue(S, B))
45 return false;
47 return true;
48}
49
50inline bool parseArgValue(StringRef S, StringRef &V) {
51 V = S;
52 return true;
53}
54
55template <typename T>
56std::enable_if_t<std::is_arithmetic_v<T> && !std::is_same_v<T, bool>, bool>
58 if constexpr (std::is_floating_point_v<T>)
59 return to_float(S, V);
60 else
61 return to_integer(S, V);
62}
63
64// A std::optional member is set only when its option is given.
65template <typename T> bool parseArgValue(StringRef S, std::optional<T> &V) {
66 T X{};
67 if (!parseArgValue(S, X))
68 return false;
69 V = X;
70 return true;
71}
72
73/// An OptTable with a public constructor, shared by every options struct.
75public:
76 explicit LibraryOptTable(const Tables &T) : OptTable(T) {}
77 ~LibraryOptTable() override;
78};
79
80/// Connects an options struct to cl::ParseCommandLineOptions.
82public:
83 using ApplyFn = bool (*)(const Arg &);
84 using TableFn = const OptTable &(*)();
85 LibraryOptionsParser(TableFn Table, ApplyFn Apply, void (*Reset)())
86 : Table(Table), Apply(Apply), Reset(Reset) {}
87
88 void forEachOption(
89 function_ref<void(StringRef, StringRef, StringRef)> Fn) const override;
90 Error parse(ArrayRef<const char *> Args, unsigned &Consumed) override;
91 void reset() override { Reset(); }
92
93private:
94 TableFn Table;
95 ApplyFn Apply;
96 void (*Reset)();
97};
98
99/// Registers T::Global with cl::ParseCommandLineOptions. The library owning T
100/// defines one static instance in the file that includes the struct's
101/// definitions.
102template <typename T> class RegisterLibraryOptions {
103 LibraryOptionsParser Parser{T::optTable,
104 [](const Arg &A) { return T::Global.apply(A); },
105 [] { T::Global = T(); }};
106
107public:
109};
110
111} // namespace opt
112} // namespace llvm
113
114#endif // LLVM_OPTION_LIBRARYOPTIONS_H
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 T
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
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.
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)
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,...
The tables TableGen emits for an option set under OPTTABLE_CODE.
Definition OptTable.h:151