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