LLVM 22.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
9#include "llvm/Option/Arg.h"
11#include "llvm/Config/llvm-config.h"
12#include "llvm/Option/ArgList.h"
13#include "llvm/Option/Option.h"
15#include "llvm/Support/Debug.h"
18
19using namespace llvm;
20using namespace llvm::opt;
21
22Arg::Arg(const Option Opt, StringRef S, unsigned Index, const Arg *BaseArg)
23 : Opt(Opt), BaseArg(BaseArg), Spelling(S), Index(Index), Claimed(false),
24 IgnoredTargetSpecific(false), OwnsValues(false) {}
25
26Arg::Arg(const Option Opt, StringRef S, unsigned Index, const char *Value0,
27 const Arg *BaseArg)
28 : Opt(Opt), BaseArg(BaseArg), Spelling(S), Index(Index), Claimed(false),
29 IgnoredTargetSpecific(false), OwnsValues(false) {
30 Values.push_back(Value0);
31}
32
33Arg::Arg(const Option Opt, StringRef S, unsigned Index, const char *Value0,
34 const char *Value1, const Arg *BaseArg)
35 : Opt(Opt), BaseArg(BaseArg), Spelling(S), Index(Index), Claimed(false),
36 IgnoredTargetSpecific(false), OwnsValues(false) {
37 Values.push_back(Value0);
38 Values.push_back(Value1);
39}
40
42 if (OwnsValues) {
43 for (const char *V : Values)
44 delete[] V;
45 }
46}
47
48void Arg::print(raw_ostream& O) const {
49 O << "<Opt:";
50 Opt.print(O, /*AddNewLine=*/false);
51
52 O << " Index:" << Index;
53
54 O << " Values: [";
55 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
56 if (i) O << ", ";
57 O << "'" << Values[i] << "'";
58 }
59
60 O << "]>\n";
61}
62
63#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
65#endif
66
67std::string Arg::getAsString(const ArgList &Args) const {
68 if (Alias)
69 return Alias->getAsString(Args);
70
73
74 ArgStringList ASL;
75 render(Args, ASL);
76 OS << llvm::interleaved(ASL, " ");
77 return std::string(OS.str());
78}
79
80void Arg::renderAsInput(const ArgList &Args, ArgStringList &Output) const {
81 if (!getOption().hasNoOptAsInput()) {
82 render(Args, Output);
83 return;
84 }
85
86 Output.append(Values.begin(), Values.end());
87}
88
89void Arg::render(const ArgList &Args, ArgStringList &Output) const {
90 switch (getOption().getRenderStyle()) {
92 Output.append(Values.begin(), Values.end());
93 break;
94
99 Output.push_back(Args.MakeArgString(OS.str()));
100 break;
101 }
102
104 Output.push_back(Args.GetOrMakeJoinedArgString(
105 getIndex(), getSpelling(), getValue(0)));
106 Output.append(Values.begin() + 1, Values.end());
107 break;
108
110 Output.push_back(Args.MakeArgString(getSpelling()));
111 Output.append(Values.begin(), Values.end());
112 break;
113 }
114}
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:638
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:79
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:684
void push_back(const T &Elt)
Definition: SmallVector.h:414
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
ArgList - Ordered collection of driver arguments.
Definition: ArgList.h:117
A concrete instance of a particular driver option.
Definition: Arg.h:35
LLVM_ABI void render(const ArgList &Args, ArgStringList &Output) const
Append the argument onto the given array as strings.
Definition: Arg.cpp:89
LLVM_ABI void print(raw_ostream &O) const
Definition: Arg.cpp:48
LLVM_ABI void renderAsInput(const ArgList &Args, ArgStringList &Output) const
Append the argument, render as an input, onto the given array as strings.
Definition: Arg.cpp:80
StringRef getSpelling() const
Returns the used prefix and name of the option: For --foo=bar, returns --foo=.
Definition: Arg.h:93
LLVM_ABI Arg(const Option Opt, StringRef Spelling, unsigned Index, const Arg *BaseArg=nullptr)
Definition: Arg.cpp:22
SmallVectorImpl< const char * > & getValues()
Definition: Arg.h:131
LLVM_ABI std::string getAsString(const ArgList &Args) const
Return a formatted version of the argument and its values, for diagnostics.
Definition: Arg.cpp:67
const Option & getOption() const
Definition: Arg.h:85
unsigned getIndex() const
Definition: Arg.h:95
LLVM_ABI void dump() const
Definition: Arg.cpp:64
LLVM_ABI ~Arg()
Definition: Arg.cpp:41
const char * getValue(unsigned N=0) const
Definition: Arg.h:127
Option - Abstract representation for a single form of driver argument.
Definition: Option.h:55
@ RenderSeparateStyle
Definition: Option.h:76
@ RenderCommaJoinedStyle
Definition: Option.h:74
LLVM_ABI void print(raw_ostream &O, bool AddNewLine=true) const
Definition: Option.cpp:40
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:692
Definition: Arg.h:27
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
InterleavedRange< Range > interleaved(const Range &R, StringRef Separator=", ", StringRef Prefix="", StringRef Suffix="")
Output range R as a sequence of interleaved elements.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:207