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// Connects the options struct that -gen-opt-parser-defs generates from an
10// OptionsStruct def to cl::parseCommandLineOptions. See "Declaring a Library's
11// Options in TableGen" in llvm/docs/CommandLine.md.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_OPTION_LIBRARYOPTIONS_H
16#define LLVM_OPTION_LIBRARYOPTIONS_H
17
22#include <utility>
23
24namespace llvm {
25namespace opt {
26
27/// Connects options struct T to cl::parseCommandLineOptions and
28/// cl::LibraryOptionsContext.
30public:
31 template <typename T>
32 constexpr explicit LibraryOptionsParser(std::in_place_type_t<T>)
33 : Table(T::optTable), Default(&T::Default),
34 Apply([](void *Opts, const Arg &A, BumpPtrAllocator &Alloc) {
35 return static_cast<T *>(Opts)->apply(A, Alloc);
36 }),
37 Copy([](BumpPtrAllocator &Alloc) -> void * {
38 return new (Alloc) T(T::Default);
39 }) {}
40
41 void forEachOption(
42 function_ref<void(StringRef, StringRef, StringRef)> Fn) const override;
43 Error parse(void *Opts, ArrayRef<const char *> Args, unsigned &Consumed,
44 BumpPtrAllocator &Alloc) const override;
45 const void *getDefault() const override { return Default; }
46 void *copyDefault(BumpPtrAllocator &Alloc) const override {
47 return Copy(Alloc);
48 }
49
50private:
51 const OptionsStructTable &(*Table)();
52 const void *Default;
53 bool (*Apply)(void *Opts, const Arg &A, BumpPtrAllocator &Alloc);
54 void *(*Copy)(BumpPtrAllocator &Alloc);
55};
56
57/// Registers T with cl::parseCommandLineOptions. The library owning T
58/// defines one static instance in the file that includes the struct's
59/// definitions.
60template <typename T> class RegisterLibraryOptions {
61 static_assert(std::is_trivially_destructible_v<T>,
62 "an options struct must not need an exit-time destructor");
63 LibraryOptionsParser Parser{std::in_place_type<T>};
64
65public:
67};
68
69} // namespace opt
70} // namespace llvm
71
72/// Defines the file-scope registration of options struct T for a library that
73/// has no initialization function to register it from, exempting this static
74/// constructor from -Wglobal-constructors.
75// clang-format off
76#ifdef __clang__
77#define LLVM_REGISTER_LIBRARY_OPTIONS(T) \
78 _Pragma("clang diagnostic push") \
79 _Pragma("clang diagnostic ignored \"-Wglobal-constructors\"") \
80 static llvm::opt::RegisterLibraryOptions<T> Registration; \
81 _Pragma("clang diagnostic pop")
82#else
83#define LLVM_REGISTER_LIBRARY_OPTIONS(T) \
84 static llvm::opt::RegisterLibraryOptions<T> Registration;
85#endif
86// clang-format on
87
88#endif // LLVM_OPTION_LIBRARYOPTIONS_H
aarch64 promote const
This file defines the BumpPtrAllocator interface.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
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 T
The options of a library that declares them in TableGen rather than as cl::opt (see llvm/Option/Libra...
A concrete instance of a particular driver option.
Definition Arg.h:35
Connects options struct T to cl::parseCommandLineOptions and cl::LibraryOptionsContext.
const void * getDefault() const override
Returns the default values.
constexpr LibraryOptionsParser(std::in_place_type_t< T >)
void * copyDefault(BumpPtrAllocator &Alloc) const override
Returns a copy of getDefault() allocated from Alloc.
The option table of an options struct.
LLVM_ABI void addLibraryOptions(const LibraryOptions &L)
Makes ParseCommandLineOptions recognize L's options.
This is an optimization pass for GlobalISel generic memory operations.
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:391