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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
|
// slang-diagnostic-sink.cpp
#include "slang-diagnostic-sink.h"
#include "slang-name.h"
#include "slang-core-diagnostics.h"
#include "slang-name-convention-util.h"
#include "../core/slang-memory-arena.h"
#include "../core/slang-dictionary.h"
#include "../core/slang-string-util.h"
#include "../core/slang-char-util.h"
namespace Slang {
void printDiagnosticArg(StringBuilder& sb, char const* str)
{
sb << str;
}
void printDiagnosticArg(StringBuilder& sb, int32_t val)
{
sb << val;
}
void printDiagnosticArg(StringBuilder& sb, uint32_t val)
{
sb << val;
}
void printDiagnosticArg(StringBuilder& sb, int64_t val)
{
sb << val;
}
void printDiagnosticArg(StringBuilder& sb, uint64_t val)
{
sb << val;
}
void printDiagnosticArg(StringBuilder& sb, double val)
{
sb << val;
}
void printDiagnosticArg(StringBuilder& sb, Slang::String const& str)
{
sb << str;
}
void printDiagnosticArg(StringBuilder& sb, Slang::UnownedStringSlice const& str)
{
sb.append(str);
}
void printDiagnosticArg(StringBuilder& sb, Name* name)
{
sb << getText(name);
}
void printDiagnosticArg(StringBuilder& sb, TokenType tokenType)
{
sb << TokenTypeToString(tokenType);
}
void printDiagnosticArg(StringBuilder& sb, Token const& token)
{
sb << token.getContent();
}
SourceLoc const& getDiagnosticPos(Token const& token)
{
return token.loc;
}
// Take the format string for a diagnostic message, along with its arguments, and turn it into a
static void formatDiagnosticMessage(StringBuilder& sb, char const* format, int argCount, DiagnosticArg const* const* args)
{
char const* spanBegin = format;
for(;;)
{
char const* spanEnd = spanBegin;
while (int c = *spanEnd)
{
if (c == '$')
break;
spanEnd++;
}
sb.Append(spanBegin, int(spanEnd - spanBegin));
if (!*spanEnd)
return;
SLANG_ASSERT(*spanEnd == '$');
spanEnd++;
int d = *spanEnd++;
switch (d)
{
// A double dollar sign `$$` is used to emit a single `$`
case '$':
sb.Append('$');
break;
// A single digit means to emit the corresponding argument.
// TODO: support more than 10 arguments, and add options
// to control formatting, etc.
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
{
int index = d - '0';
if (index >= argCount)
{
// TODO(tfoley): figure out what a good policy will be for "panic" situations like this
SLANG_INVALID_OPERATION("too few arguments for diagnostic message");
}
else
{
DiagnosticArg const* arg = args[index];
arg->printFunc(sb, arg->data);
}
}
break;
default:
SLANG_INVALID_OPERATION("invalid diagnostic message format");
break;
}
spanBegin = spanEnd;
}
}
static void formatDiagnostic(const HumaneSourceLoc& humaneLoc, Diagnostic const& diagnostic, DiagnosticSink::Flags flags, StringBuilder& outBuilder)
{
if (flags & DiagnosticSink::Flag::HumaneLoc)
{
outBuilder << humaneLoc.pathInfo.foundPath;
outBuilder << "(";
outBuilder << Int32(humaneLoc.line);
outBuilder << "): ";
}
outBuilder << getSeverityName(diagnostic.severity);
if (diagnostic.ErrorID >= 0)
{
outBuilder << " ";
outBuilder << diagnostic.ErrorID;
}
outBuilder << ": ";
outBuilder << diagnostic.Message;
outBuilder << "\n";
}
static void _replaceTabWithSpaces(const UnownedStringSlice& slice, Int tabSize, StringBuilder& out)
{
const char* start = slice.begin();
const char*const end = slice.end();
const Index startLength = out.getLength();
for (const char* cur = start; cur < end; cur++)
{
if (*cur == '\t')
{
if (start < cur)
{
out.append(start, cur);
}
// The amount of spaces we add depends on the current position.
const Index lastPosition = out.getLength() - startLength;
Index tabPosition = lastPosition;
// Strip the tabPosition so it's back to the tab stop
// Special case if tabSize is a power of 2
if ((tabSize & (tabSize - 1)) == 0)
{
tabPosition = tabPosition & ~Index(tabSize - 1);
}
else
{
tabPosition -= tabPosition % tabSize;
}
// Move to next tab
tabPosition += tabSize;
// The amount of spaces to simulate the tab
const Index spacesCount = tabPosition - lastPosition;
// Add the spaces
out.appendRepeatedChar(' ', spacesCount);
// Set the start at the first character past
start = cur + 1;
}
}
if (start < end)
{
out.append(start, end);
}
}
// Given multi-line text, and a position within the text (as a pointer into the memory of text)
// extract the line that contains pos
static UnownedStringSlice _extractLineContainingPosition(const UnownedStringSlice& text, const char* pos)
{
SLANG_ASSERT(text.isMemoryContained(pos));
const char*const contentStart = text.begin();
const char*const contentEnd = text.end();
// We want to determine the start of the line, and the end of the line
const char* start = pos;
for (; start > contentStart; --start)
{
const char c = *start;
if (c == '\n' || c == '\r')
{
// We want the character after, but we can only do this if not already at pos
start += int(start < pos);
break;
}
}
const char* end = pos;
for (; end < contentEnd; ++end)
{
const char c = *end;
if (c == '\n' || c == '\r')
{
break;
}
}
return UnownedStringSlice(start, end);
}
static void _reduceLength(Index startIndex, const UnownedStringSlice& prefix, StringBuilder& ioBuf)
{
StringBuilder buf;
buf << prefix;
buf.append(ioBuf.getUnownedSlice().tail(startIndex));
ioBuf = buf;
}
static void _sourceLocationNoteDiagnostic(DiagnosticSink* sink, SourceView* sourceView, SourceLoc sourceLoc, StringBuilder& sb)
{
SourceFile* sourceFile = sourceView->getSourceFile();
if (!sourceFile)
{
return;
}
UnownedStringSlice content = sourceFile->getContent();
// Make sure the offset is within content.
// This is important because it's possible to have a 'SourceFile' that doesn't contain any content
// (for example when reconstructed via serialization with just line offsets, the actual source text 'content' isn't available).
const int offset = sourceView->getRange().getOffset(sourceLoc);
if (offset < 0 || offset >= content.getLength())
{
return;
}
// Work out the position of the SourceLoc in the source
const char*const pos = content.begin() + offset;
UnownedStringSlice line = _extractLineContainingPosition(content, pos);
// Trim any trailing white space
line = UnownedStringSlice(line.begin(), line.trim().end());
// TODO(JS): The tab size should ideally be configurable from command line.
// For now just go with 4.
const Index tabSize = 4;
StringBuilder sourceLine;
StringBuilder caretLine;
// First work out the sourceLine
_replaceTabWithSpaces(line, tabSize, sourceLine);
// Now the caretLine which appears underneath the sourceLine
{
// Produce the text up to the caret position (at pos), taking into account tabs
_replaceTabWithSpaces(UnownedStringSlice(line.begin(), pos), tabSize, caretLine);
// Now make all spaces
const Index length = caretLine.getLength();
caretLine.Clear();
caretLine.appendRepeatedChar(' ', length);
Index caretIndex = caretLine.getLength();
// Add caret
caretLine << "^";
auto lexer = sink->getSourceLocationLexer();
if (lexer)
{
UnownedStringSlice token = lexer(UnownedStringSlice(pos, line.end()));
if (token.getLength() > 1)
{
caretLine.appendRepeatedChar('~', token.getLength() - 1);
}
}
const Index maxLength = sink->getSourceLineMaxLength();
if (maxLength > 0)
{
const UnownedStringSlice ellipsis = UnownedStringSlice::fromLiteral("...");
const UnownedStringSlice spaces = UnownedStringSlice::fromLiteral(" ");
SLANG_ASSERT(ellipsis.getLength() == spaces.getLength());
// We use the caretLine length if we have a lexer, because it will have underscores such that it's end is the end of
// the item at issue.
// If we don't have the lexer, we guesstimate using 1/4 of the maximum length
const Index endIndex = lexer ? caretLine.getLength() : (caretIndex + (maxLength / 4));
if (endIndex > maxLength)
{
const Index startIndex = endIndex - (maxLength - ellipsis.getLength());
_reduceLength(startIndex, ellipsis, sourceLine);
_reduceLength(startIndex, spaces, caretLine);
}
if (sourceLine.getLength() > maxLength)
{
StringBuilder buf;
buf.append(sourceLine.getUnownedSlice().head(maxLength - ellipsis.getLength()));
buf << ellipsis;
sourceLine = buf;
}
}
}
// We could have handling here for if the line is too long, that we surround the important section
// will ellipsis for example.
// For now we just output.
sb << sourceLine << "\n";
sb << caretLine << "\n";
}
static void formatDiagnostic(
DiagnosticSink* sink,
Diagnostic const& diagnostic,
StringBuilder& sb)
{
auto sourceManager = sink->getSourceManager();
SourceView* sourceView = nullptr;
HumaneSourceLoc humaneLoc;
const auto sourceLoc = diagnostic.loc;
{
sourceView = sourceManager->findSourceViewRecursively(sourceLoc);
if (sourceView)
{
humaneLoc = sourceView->getHumaneLoc(sourceLoc);
}
formatDiagnostic(humaneLoc, diagnostic, sink->getFlags(), sb);
{
SourceView* currentView = sourceView;
while (currentView && currentView->getInitiatingSourceLoc().isValid() && currentView->getSourceFile()->getPathInfo().type == PathInfo::Type::TokenPaste)
{
SourceView* initiatingView = sourceManager->findSourceView(currentView->getInitiatingSourceLoc());
if (initiatingView == nullptr)
{
break;
}
const DiagnosticInfo& diagnosticInfo = MiscDiagnostics::seeTokenPasteLocation;
// Turn the message format into a message. For the moment it assumes no parameters.
StringBuilder msg;
formatDiagnosticMessage(msg, diagnosticInfo.messageFormat, 0, nullptr);
// Set up the diagnostic.
Diagnostic initiationDiagnostic;
initiationDiagnostic.ErrorID = diagnosticInfo.id;
initiationDiagnostic.Message = msg.ProduceString();
initiationDiagnostic.loc = sourceView->getInitiatingSourceLoc();
initiationDiagnostic.severity = diagnosticInfo.severity;
// TODO(JS):
// Not 100% clear what the best sourceLoc type is most useful here - we will go with default for now
HumaneSourceLoc pasteHumaneLoc = initiatingView->getHumaneLoc(sourceView->getInitiatingSourceLoc());
// Okay we should output where the token paste took place
formatDiagnostic(pasteHumaneLoc, initiationDiagnostic, sink->getFlags(), sb);
// Make the initiatingView the current view
currentView = initiatingView;
}
}
}
// We don't don't output source line information if this is a 'note' as a note is extra information for one
// of the other main severity types, and so the information should already be output on the initial line
if (sourceView && sink->isFlagSet(DiagnosticSink::Flag::SourceLocationLine) && diagnostic.severity != Severity::Note)
{
_sourceLocationNoteDiagnostic(sink, sourceView, sourceLoc, sb);
}
if (sourceView && sink->isFlagSet(DiagnosticSink::Flag::VerbosePath))
{
auto actualHumaneLoc = sourceView->getHumaneLoc(diagnostic.loc, SourceLocType::Actual);
// Look up the path verbosely (will get the canonical path if necessary)
actualHumaneLoc.pathInfo.foundPath = sourceView->getSourceFile()->calcVerbosePath();
// Only output if it's actually different
if (actualHumaneLoc.pathInfo.foundPath != humaneLoc.pathInfo.foundPath ||
actualHumaneLoc.line != humaneLoc.line ||
actualHumaneLoc.column != humaneLoc.column)
{
formatDiagnostic(actualHumaneLoc, diagnostic, sink->getFlags(), sb);
}
}
}
void DiagnosticSink::init(SourceManager* sourceManager, SourceLocationLexer sourceLocationLexer)
{
m_errorCount = 0;
m_internalErrorLocsNoted = 0;
m_sourceManager = sourceManager;
m_sourceLocationLexer = sourceLocationLexer;
m_sourceLineMaxLength = 0;
m_flags = Flag::HumaneLoc;
// If we have a source location lexer, we'll by default enable source location output
if (sourceLocationLexer)
{
setFlag(Flag::SourceLocationLine);
}
}
void DiagnosticSink::reset()
{
m_errorCount = 0;
m_internalErrorLocsNoted = 0;
outputBuffer.Clear();
}
void DiagnosticSink::noteInternalErrorLoc(SourceLoc const& loc)
{
// Don't consider invalid source locations.
if (!loc.isValid())
return;
if (m_parentSink)
{
m_parentSink->noteInternalErrorLoc(loc);
}
// If this is the first source location being noted,
// then emit a message to help the user isolate what
// code might have confused the compiler.
if (m_internalErrorLocsNoted == 0)
{
diagnose(loc, MiscDiagnostics::noteLocationOfInternalError);
}
m_internalErrorLocsNoted++;
}
SlangResult DiagnosticSink::getBlobIfNeeded(ISlangBlob** outBlob)
{
// If the client doesn't want an output blob, there is nothing to do.
//
if (!outBlob) return SLANG_OK;
// For outputBuffer to be valid and hold diagnostics, writer must not be set
SLANG_ASSERT(writer == nullptr);
// If there were no errors, and there was no diagnostic output, there is nothing to do.
if (getErrorCount() == 0 && outputBuffer.getLength() == 0)
{
return SLANG_OK;
}
Slang::ComPtr<ISlangBlob> blob = Slang::StringUtil::createStringBlob(outputBuffer);
*outBlob = blob.detach();
return SLANG_OK;
}
void DiagnosticSink::diagnoseImpl(DiagnosticInfo const& info, const UnownedStringSlice& formattedMessage)
{
if (info.severity >= Severity::Error)
{
m_errorCount++;
}
if (writer)
{
writer->write(formattedMessage.begin(), formattedMessage.getLength());
}
else
{
outputBuffer.append(formattedMessage);
}
if (m_parentSink)
{
m_parentSink->diagnoseImpl(info, formattedMessage);
}
if (info.severity >= Severity::Fatal)
{
// TODO: figure out a better policy for aborting compilation
SLANG_ABORT_COMPILATION("");
}
}
void DiagnosticSink::diagnoseImpl(SourceLoc const& pos, DiagnosticInfo const& info, int argCount, DiagnosticArg const* const* args)
{
StringBuilder messageBuilder;
{
StringBuilder sb;
formatDiagnosticMessage(sb, info.messageFormat, argCount, args);
Diagnostic diagnostic;
diagnostic.ErrorID = info.id;
diagnostic.Message = sb.ProduceString();
diagnostic.loc = pos;
diagnostic.severity = info.severity;
// If so, pass the error string along to them
formatDiagnostic(this, diagnostic, messageBuilder);
}
diagnoseImpl(info, messageBuilder.getUnownedSlice());
}
void DiagnosticSink::diagnoseRaw(
Severity severity,
char const* message)
{
return diagnoseRaw(severity, UnownedStringSlice(message));
}
void DiagnosticSink::diagnoseRaw(
Severity severity,
const UnownedStringSlice& message)
{
if (severity >= Severity::Error)
{
m_errorCount++;
}
// Did the client supply a callback for us to use?
if(writer)
{
// If so, pass the error string along to them
writer->write(message.begin(), message.getLength());
}
else
{
// If the user doesn't have a callback, then just
// collect our diagnostic messages into a buffer
outputBuffer.append(message);
}
if (m_parentSink)
{
m_parentSink->diagnoseRaw(severity, message);
}
if (severity >= Severity::Fatal)
{
// TODO: figure out a better policy for aborting compilation
SLANG_ABORT_COMPILATION("");
}
}
/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! DiagnosticLookup !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
void DiagnosticsLookup::_add(const char* name, Index index)
{
UnownedStringSlice nameSlice(name);
m_map.Add(nameSlice, index);
// Add a dashed version (KababCase)
{
m_work.Clear();
NameConventionUtil::convert(NameConvention::Camel, nameSlice, CharCase::Lower, NameConvention::Kabab, m_work);
UnownedStringSlice dashSlice(m_arena.allocateString(m_work.getBuffer(), m_work.getLength()), m_work.getLength());
m_map.AddIfNotExists(dashSlice, index);
}
}
void DiagnosticsLookup::addAlias(const char* name, const char* diagnosticName)
{
const Index index = _findDiagnosticIndex(UnownedStringSlice(diagnosticName));
SLANG_ASSERT(index >= 0);
if (index >= 0)
{
_add(name, index);
}
}
Index DiagnosticsLookup::add(const DiagnosticInfo* info)
{
// Check it's not already added
SLANG_ASSERT(m_diagnostics.indexOf(info) < 0);
const Index index = m_diagnostics.getCount();
_add(info->name, index);
m_diagnostics.add(info);
return index;
}
void DiagnosticsLookup::add(const DiagnosticInfo*const* infos, Index infosCount)
{
for (Index i = 0; i < infosCount; ++i)
{
add(infos[i]);
}
}
DiagnosticsLookup::DiagnosticsLookup():
m_arena(kArenaInitialSize)
{
}
DiagnosticsLookup::DiagnosticsLookup(const DiagnosticInfo*const* diagnostics, Index diagnosticsCount) :
m_arena(kArenaInitialSize)
{
m_diagnostics.addRange(diagnostics, diagnosticsCount);
// TODO: We should eventually have a more formal system for associating individual
// diagnostics, or groups of diagnostics, with user-exposed names for use when
// enabling/disabling warnings (or turning warnings into errors, etc.).
//
// For now we build a map from diagnostic name to it's entry. Two entries are typically
// added - the 'original name' as associated with the diagnostic in lowerCamel, and
// a dashified version.
for (Index i = 0; i < diagnosticsCount; ++i)
{
const DiagnosticInfo* diagnostic = diagnostics[i];
_add(diagnostic->name, i);
}
}
} // namespace Slang
|