1 //===- Arg.cpp - Argument Implementations ---------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/SmallString.h"
10 #include "llvm/Config/llvm-config.h"
11 #include "llvm/Option/Arg.h"
12 #include "llvm/Option/ArgList.h"
13 #include "llvm/Option/Option.h"
14 #include "llvm/Support/Compiler.h"
15 #include "llvm/Support/Debug.h"
16 #include "llvm/Support/raw_ostream.h"
19 using namespace llvm::opt
;
21 Arg::Arg(const Option Opt
, StringRef S
, unsigned Index
, const Arg
*BaseArg
)
22 : Opt(Opt
), BaseArg(BaseArg
), Spelling(S
), Index(Index
), Claimed(false),
25 Arg::Arg(const Option Opt
, StringRef S
, unsigned Index
, const char *Value0
,
27 : Opt(Opt
), BaseArg(BaseArg
), Spelling(S
), Index(Index
), Claimed(false),
29 Values
.push_back(Value0
);
32 Arg::Arg(const Option Opt
, StringRef S
, unsigned Index
, const char *Value0
,
33 const char *Value1
, const Arg
*BaseArg
)
34 : Opt(Opt
), BaseArg(BaseArg
), Spelling(S
), Index(Index
), Claimed(false),
36 Values
.push_back(Value0
);
37 Values
.push_back(Value1
);
42 for (unsigned i
= 0, e
= Values
.size(); i
!= e
; ++i
)
47 void Arg::print(raw_ostream
& O
) const {
53 O
<< " Index:" << Index
;
56 for (unsigned i
= 0, e
= Values
.size(); i
!= e
; ++i
) {
58 O
<< "'" << Values
[i
] << "'";
64 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
65 LLVM_DUMP_METHOD
void Arg::dump() const { print(dbgs()); }
68 std::string
Arg::getAsString(const ArgList
&Args
) const {
70 return Alias
->getAsString(Args
);
73 raw_svector_ostream
OS(Res
);
77 for (ArgStringList::iterator
78 it
= ASL
.begin(), ie
= ASL
.end(); it
!= ie
; ++it
) {
79 if (it
!= ASL
.begin())
87 void Arg::renderAsInput(const ArgList
&Args
, ArgStringList
&Output
) const {
88 if (!getOption().hasNoOptAsInput()) {
93 Output
.append(Values
.begin(), Values
.end());
96 void Arg::render(const ArgList
&Args
, ArgStringList
&Output
) const {
97 switch (getOption().getRenderStyle()) {
98 case Option::RenderValuesStyle
:
99 Output
.append(Values
.begin(), Values
.end());
102 case Option::RenderCommaJoinedStyle
: {
103 SmallString
<256> Res
;
104 raw_svector_ostream
OS(Res
);
106 for (unsigned i
= 0, e
= getNumValues(); i
!= e
; ++i
) {
110 Output
.push_back(Args
.MakeArgString(OS
.str()));
114 case Option::RenderJoinedStyle
:
115 Output
.push_back(Args
.GetOrMakeJoinedArgString(
116 getIndex(), getSpelling(), getValue(0)));
117 Output
.append(Values
.begin() + 1, Values
.end());
120 case Option::RenderSeparateStyle
:
121 Output
.push_back(Args
.MakeArgString(getSpelling()));
122 Output
.append(Values
.begin(), Values
.end());