LLVM 19.0.0git
Arg.cpp
Go to the documentation of this file.
1//===- Arg.cpp - Argument Implementations ---------------------------------===//
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/Config/llvm-config.h"
11#include "llvm/Option/Arg.h"
12#include "llvm/Option/ArgList.h"
13#include "llvm/Option/Option.h"
15#include "llvm/Support/Debug.h"
17
18using namespace llvm;
19using namespace llvm::opt;
20
21Arg::Arg(const Option Opt, StringRef S, unsigned Index, const Arg *BaseArg)
22 : Opt(Opt), BaseArg(BaseArg), Spelling(S), Index(Index), Claimed(false),
23 IgnoredTargetSpecific(false), OwnsValues(false) {}
24
25Arg::Arg(const Option Opt, StringRef S, unsigned Index, const char *Value0,
26 const Arg *BaseArg)
27 : Opt(Opt), BaseArg(BaseArg), Spelling(S), Index(Index), Claimed(false),
28 IgnoredTargetSpecific(false), OwnsValues(false) {
29 Values.push_back(Value0);
30}
31
32Arg::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),
35 IgnoredTargetSpecific(false), OwnsValues(false) {
36 Values.push_back(Value0);
37 Values.push_back(Value1);
38}
39
41 if (OwnsValues) {
42 for (unsigned i = 0, e = Values.size(); i != e; ++i)
43 delete[] Values[i];
44 }
45}
46
47void Arg::print(raw_ostream& O) const {
48 O << "<Opt:";
49 Opt.print(O, /*AddNewLine=*/false);
50
51 O << " Index:" << Index;
52
53 O << " Values: [";
54 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
55 if (i) O << ", ";
56 O << "'" << Values[i] << "'";
57 }
58
59 O << "]>\n";
60}
61
62#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
64#endif
65
66std::string Arg::getAsString(const ArgList &Args) const {
67 if (Alias)
68 return Alias->getAsString(Args);
69
72
73 ArgStringList ASL;
74 render(Args, ASL);
76 it = ASL.begin(), ie = ASL.end(); it != ie; ++it) {
77 if (it != ASL.begin())
78 OS << ' ';
79 OS << *it;
80 }
81
82 return std::string(OS.str());
83}
84
85void Arg::renderAsInput(const ArgList &Args, ArgStringList &Output) const {
86 if (!getOption().hasNoOptAsInput()) {
87 render(Args, Output);
88 return;
89 }
90
91 Output.append(Values.begin(), Values.end());
92}
93
94void Arg::render(const ArgList &Args, ArgStringList &Output) const {
95 switch (getOption().getRenderStyle()) {
97 Output.append(Values.begin(), Values.end());
98 break;
99
103 OS << getSpelling();
104 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
105 if (i) OS << ',';
106 OS << getValue(i);
107 }
108 Output.push_back(Args.MakeArgString(OS.str()));
109 break;
110 }
111
113 Output.push_back(Args.GetOrMakeJoinedArgString(
114 getIndex(), getSpelling(), getValue(0)));
115 Output.append(Values.begin() + 1, Values.end());
116 break;
117
119 Output.push_back(Args.MakeArgString(getSpelling()));
120 Output.append(Values.begin(), Values.end());
121 break;
122 }
123}
Defines the llvm::Arg class for parsed arguments.
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:529
raw_pwrite_stream & OS
This file defines the SmallString class.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
size_t size() const
Definition: SmallVector.h:91
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:696
void push_back(const T &Elt)
Definition: SmallVector.h:426
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
ArgList - Ordered collection of driver arguments.
Definition: ArgList.h:116
A concrete instance of a particular driver option.
Definition: Arg.h:34
void render(const ArgList &Args, ArgStringList &Output) const
Append the argument onto the given array as strings.
Definition: Arg.cpp:94
void print(raw_ostream &O) const
Definition: Arg.cpp:47
void renderAsInput(const ArgList &Args, ArgStringList &Output) const
Append the argument, render as an input, onto the given array as strings.
Definition: Arg.cpp:85
StringRef getSpelling() const
Returns the used prefix and name of the option: For --foo=bar, returns --foo=.
Definition: Arg.h:91
Arg(const Option Opt, StringRef Spelling, unsigned Index, const Arg *BaseArg=nullptr)
Definition: Arg.cpp:21
std::string getAsString(const ArgList &Args) const
Return a formatted version of the argument and its values, for diagnostics.
Definition: Arg.cpp:66
const Option & getOption() const
Definition: Arg.h:83
unsigned getNumValues() const
Definition: Arg.h:123
unsigned getIndex() const
Definition: Arg.h:93
void dump() const
Definition: Arg.cpp:63
const char * getValue(unsigned N=0) const
Definition: Arg.h:125
Option - Abstract representation for a single form of driver argument.
Definition: Option.h:54
@ RenderSeparateStyle
Definition: Option.h:75
@ RenderCommaJoinedStyle
Definition: Option.h:73
void print(raw_ostream &O, bool AddNewLine=true) const
Definition: Option.cpp:41
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:690
Definition: Arg.h:26
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163