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#include "llvm/Option/LibraryOptions.h"
10#include "llvm/Option/ArgList.h"
11#include "llvm/Option/OptTable.h"
12#include "llvm/Option/Option.h"
13#include "llvm/Support/Error.h"
14
15using namespace llvm;
16using namespace llvm::opt;
17
18LibraryOptTable::~LibraryOptTable() = default;
19
20void LibraryOptionsParser::forEachOption(
21 function_ref<void(StringRef, StringRef, StringRef)> Fn) const {
22 const OptTable &T = Table();
23 for (unsigned ID = 1, E = T.getNumOptions(); ID <= E; ++ID) {
24 unsigned Kind = T.getOptionKind(id: ID);
25 if (Kind == Option::GroupClass || Kind == Option::InputClass ||
26 Kind == Option::UnknownClass)
27 continue;
28 StringRef V = T.getOptionMetaVar(id: ID);
29 std::string MetaVar;
30 if (Kind == Option::SeparateOrEqClass)
31 MetaVar = ("=" + (V.empty() ? StringRef("<value>") : V)).str();
32 else if (!V.empty())
33 MetaVar = ("[=" + V + "]").str();
34 Fn(T.getOptionName(id: ID), MetaVar, T.getOptionHelpText(id: ID));
35 }
36}
37
38Error LibraryOptionsParser::parse(void *Opts, ArrayRef<const char *> Args,
39 unsigned &Consumed, BumpPtrAllocator &Alloc) {
40 InputArgList List(Args.begin(), Args.end());
41 Consumed = 0;
42 std::unique_ptr<Arg> A = Table().ParseOneArg(Args: List, Index&: Consumed);
43 Consumed = std::min<unsigned>(a: Consumed, b: Args.size());
44 if (!A)
45 return createStringError(S: "option '" + Twine(Args[0]) +
46 "' requires an argument");
47 if (A->getOption().getKind() == Option::UnknownClass)
48 return createStringError(S: "unknown argument '" + Twine(Args[0]) + "'");
49 if (!Apply(Opts, *A, Alloc))
50 return createStringError(S: "invalid value '" + Twine(A->getValue()) +
51 "' in '" + A->getAsString(Args: List) + "'");
52 return Error::success();
53}
54