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::FlagOrEqClass && Kind != Option::SeparateOrEqClass)
26 continue;
27 StringRef V = T.getOptionMetaVar(id: ID);
28 std::string MetaVar;
29 if (Kind == Option::SeparateOrEqClass)
30 MetaVar = ("=" + (V.empty() ? StringRef("<value>") : V)).str();
31 else if (!V.empty())
32 MetaVar = ("[=" + V + "]").str();
33 Fn(T.getOptionName(id: ID), MetaVar, T.getOptionHelpText(id: ID));
34 }
35}
36
37Error LibraryOptionsParser::parse(ArrayRef<const char *> Args,
38 unsigned &Consumed, BumpPtrAllocator &Alloc) {
39 InputArgList List(Args.begin(), Args.end());
40 Consumed = 0;
41 std::unique_ptr<Arg> A = Table().ParseOneArg(Args: List, Index&: Consumed);
42 Consumed = std::min<unsigned>(a: Consumed, b: Args.size());
43 if (!A)
44 return createStringError(S: "option '" + Twine(Args[0]) +
45 "' requires an argument");
46 if (A->getOption().getKind() == Option::UnknownClass)
47 return createStringError(S: "unknown argument '" + Twine(Args[0]) + "'");
48 if (!Apply(*A, Alloc))
49 return createStringError(S: "invalid value '" + Twine(A->getValue()) +
50 "' in '" + A->getAsString(Args: List) + "'");
51 return Error::success();
52}
53