1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
#include "slang-command-line-args.h"
#include "../core/slang-process-util.h"
#include "../core/slang-string-escape-util.h"
#include "slang-core-diagnostics.h"
namespace Slang {
void CommandLineArgs::setArgs(const char*const* args, size_t argCount)
{
m_args.clear();
SourceManager* sourceManager = m_context->getSourceManager();
const SourceLoc startLoc = sourceManager->getNextRangeStart();
StringBuilder buf;
auto escapeHandler = Process::getEscapeHandler();
for (size_t i = 0; i < argCount; ++i)
{
const Index offset = buf.getLength();
const char* srcArg = args[i];
Arg dstArg;
dstArg.loc = startLoc + offset;
dstArg.value = srcArg;
m_args.add(dstArg);
// Write the string escaped if necessary
StringEscapeUtil::appendMaybeQuoted(escapeHandler, dstArg.value.getUnownedSlice(), buf);
// Put a space between the args
buf << " ";
}
SourceFile* sourceFile = sourceManager->createSourceFileWithString(PathInfo::makeUnknown(), buf.produceString());
SourceView* sourceView = sourceManager->createSourceView(sourceFile, nullptr, SourceLoc::fromRaw(0));
SLANG_UNUSED(sourceView);
SLANG_ASSERT(sourceView->getRange().begin == startLoc);
}
bool CommandLineArgs::hasArgs(const char*const* args, Index count) const
{
if (m_args.getCount() != count)
{
return false;
}
for (Index i = 0; i < count; ++i)
{
if (m_args[i].value != args[i])
{
return false;
}
}
return true;
}
/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
CommandLineReader
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
String CommandLineReader::getPreviousValue() const
{
SLANG_ASSERT(m_index > 0);
if (m_index > 0)
{
const auto& prevArg = (*m_args)[m_index - 1];
return prevArg.value;
}
else
{
return String();
}
}
SlangResult CommandLineReader::expectArg(String& outArg)
{
if (hasArg())
{
outArg = m_args->m_args[m_index++].value;
return SLANG_OK;
}
else
{
m_sink->diagnose(peekLoc(), MiscDiagnostics::expectedArgumentForOption, getPreviousValue());
return SLANG_FAIL;
}
}
SlangResult CommandLineReader::expectArg(CommandLineArg& outArg)
{
if (hasArg())
{
outArg = peekArg();
advance();
return SLANG_OK;
}
else
{
m_sink->diagnose(peekLoc(), MiscDiagnostics::expectedArgumentForOption, getPreviousValue());
return SLANG_FAIL;
}
}
/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
DownstreamArgs
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
Index DownstreamArgs::addName(const String& name)
{
Index index = findName(name);
if (index < 0)
{
index = m_entries.getCount();
m_entries.add(Entry{name, CommandLineArgs(m_context) });
}
return index;
}
Index DownstreamArgs::_findOrAddName(SourceLoc loc, const UnownedStringSlice& name, Flags flags, DiagnosticSink* sink)
{
if (name.getLength() <= 0)
{
if (sink)
{
sink->diagnose(loc, MiscDiagnostics::downstreamToolNameNotDefined);
}
return -1;
}
if (flags & Flag::AllowNewNames)
{
return addName(name);
}
Index index = findName(name);
if (index >= 0)
{
return index;
}
if (sink)
{
StringBuilder names;
names << "[ ";
for (Index i = 0; i < m_entries.getCount(); ++i)
{
if (i)
{
names << ", ";
}
names << m_entries[i].name;
}
names << " ]";
sink->diagnose(loc, MiscDiagnostics::downstreamNameNotKnown, names);
}
return -1;
}
CommandLineArgs& DownstreamArgs::getArgsByName(const char* name)
{
const Index index = findName(name);
SLANG_ASSERT(index >= 0);
return m_entries[index].args;
}
const CommandLineArgs& DownstreamArgs::getArgsByName(const char* name) const
{
const Index index = findName(name);
SLANG_ASSERT(index >= 0);
return m_entries[index].args;
}
SlangResult DownstreamArgs::stripDownstreamArgs(CommandLineArgs& ioArgs, Flags flags, DiagnosticSink* sink)
{
CommandLineReader reader(&ioArgs, sink);
while (reader.hasArg())
{
const CommandLineArg& arg = reader.peekArg();
if (arg.value.startsWith("-X"))
{
if (arg.value.endsWith("..."))
{
const UnownedStringSlice name = arg.value.getUnownedSlice().subString(2, arg.value.getLength() - 5);
const Index nameIndex = _findOrAddName(arg.loc, name, flags, sink);
if (nameIndex < 0)
{
return SLANG_FAIL;
}
Index depth = 1;
const Index startIndex = reader.getIndex();
Int index = startIndex + 1;
const Int count = ioArgs.m_args.getCount();
for (; index < count; ++index)
{
const auto& curArg = ioArgs.m_args[index];
if (curArg.value == "-X.")
{
depth--;
// If we are at end of scope we are done
if (depth <= 0)
{
break;
}
}
else if (curArg.value.startsWith("-X") && curArg.value.endsWith("..."))
{
depth++;
}
}
// We don't care if its 1, as we allow the main scope to be left open
if (depth > 1)
{
sink->diagnose(arg.loc, MiscDiagnostics::unbalancedDownstreamArguments);
return SLANG_FAIL;
}
// We are either at end of scope or at end of list
SLANG_ASSERT(depth <= 0 || index >= count);
// Add all of these args
CommandLineArgs& args = getArgsAt(nameIndex);
// Copy the values in the range
args.m_args.addRange(ioArgs.m_args.getBuffer() + startIndex + 1, index - (startIndex + 1));
// If we aren't at the end, we must be pointing to -X., so skip that
index += Index(index < count);
// Remove the range. The readers position, needs to be fixed though
ioArgs.m_args.removeRange(startIndex, index - startIndex);
// The reader should be at startIndex, and so doesn't need fixing
SLANG_ASSERT(reader.getIndex() == startIndex);
}
else if (arg.value == "-X.")
{
sink->diagnose(arg.loc, MiscDiagnostics::closeOfUnopenDownstreamArgs);
return SLANG_FAIL;
}
else
{
const Index startIndex = reader.getIndex();
// Extract the name
UnownedStringSlice name = arg.value.getUnownedSlice().tail(2);
const Index nameIndex = _findOrAddName(arg.loc, name, flags, sink);
if (nameIndex < 0)
{
return SLANG_FAIL;
}
reader.advance();
CommandLineArg nextArg;
SLANG_RETURN_ON_FAIL(reader.expectArg(nextArg));
getArgsAt(nameIndex).add(nextArg);
// Rewind to the start index
reader.setIndex(startIndex);
// Remove the args
ioArgs.m_args.removeRange(startIndex, 2);
}
}
else
{
// Advance and leave
reader.advance();
}
}
return SLANG_OK;
}
} // namespace Slang
|