LLVM 24.0.0git
LibraryOptions.cpp
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
10#include "llvm/Option/ArgList.h"
12#include "llvm/Option/Option.h"
13#include "llvm/Support/Error.h"
14
15using namespace llvm;
16using namespace llvm::opt;
17
19
21 int Argc, const char *const *Argv, StringSaver &Saver,
22 function_ref<bool(const Arg &)> Apply,
23 SmallVectorImpl<StringRef> &Inputs) const {
24 std::string Err;
25 InputArgList List = parseArgs(Argc, const_cast<char *const *>(Argv),
26 UnknownID, Saver, [&](StringRef Msg) {
27 if (Err.empty())
28 Err = Msg;
29 });
30 if (!Err.empty())
31 return createStringError(Err);
32 for (const Arg *A : List) {
33 if (A->getOption().getKind() == Option::InputClass)
34 Inputs.push_back(A->getValue());
35 else if (!Apply(*A))
36 return createStringError("invalid value '" + Twine(A->getValue()) +
37 "' in '" + A->getAsString(List) + "'");
38 }
39 return Error::success();
40}
41
43 function_ref<void(StringRef, StringRef, StringRef)> Fn) const {
44 const OptTable &T = Table();
45 for (unsigned ID = 1, E = T.getNumOptions(); ID <= E; ++ID) {
46 unsigned Kind = T.getOptionKind(ID);
47 if (Kind == Option::GroupClass || Kind == Option::InputClass ||
49 continue;
50 StringRef V = T.getOptionMetaVar(ID);
51 std::string MetaVar;
52 if (Kind == Option::SeparateOrEqClass)
53 MetaVar = ("=" + (V.empty() ? StringRef("<value>") : V)).str();
54 else if (!V.empty())
55 MetaVar = ("[=" + V + "]").str();
56 Fn(T.getOptionName(ID), MetaVar, T.getOptionHelpText(ID));
57 }
58}
59
61 unsigned &Consumed,
62 BumpPtrAllocator &Alloc) const {
63 InputArgList List(Args.begin(), Args.end());
64 Consumed = 0;
65 std::unique_ptr<Arg> A = Table().ParseOneArg(List, Consumed);
66 Consumed = std::min<unsigned>(Consumed, Args.size());
67 if (!A)
68 return createStringError("option '" + Twine(Args[0]) +
69 "' requires an argument");
70 if (A->getOption().getKind() == Option::UnknownClass)
71 return createStringError("unknown argument '" + Twine(Args[0]) + "'");
72 if (!Apply(Opts, *A, Alloc))
73 return createStringError("invalid value '" + Twine(A->getValue()) +
74 "' in '" + A->getAsString(List) + "'");
75 return Error::success();
76}
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define T
const char * Msg
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
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
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
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
An efficient, type-erasing, non-owning reference to a callable.
A concrete instance of a particular driver option.
Definition Arg.h:35
void forEachOption(function_ref< void(StringRef, StringRef, StringRef)> Fn) const override
Error parse(void *Opts, ArrayRef< const char * > Args, unsigned &Consumed, BumpPtrAllocator &Alloc) const override
Parses the option spelled by Args[0], which may take Args[1] as its value, into Opts and sets Consume...
Provide access to the Option info table.
Definition OptTable.h:54
InputArgList parseArgs(int Argc, char *const *Argv, OptSpecifier Unknown, StringSaver &Saver, std::function< void(StringRef)> ErrorFn) const
A convenience helper which handles optional initial options populated from an environment variable,...
Definition OptTable.cpp:585
Error parseToolArgs(int Argc, const char *const *Argv, StringSaver &Saver, function_ref< bool(const Arg &)> Apply, SmallVectorImpl< StringRef > &Inputs) const
This is an optimization pass for GlobalISel generic memory operations.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1321
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:391