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
|
//===--- VerifyDiagnosticsClient.cpp - Verifying Diagnostic Client --------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This is a concrete diagnostic client, which buffers the diagnostic messages.
//
//===----------------------------------------------------------------------===//
#include "clang/Frontend/VerifyDiagnosticsClient.h"
#include "clang/Frontend/FrontendDiagnostic.h"
#include "clang/Frontend/TextDiagnosticBuffer.h"
#include "clang/Lex/Preprocessor.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/Regex.h"
#include "llvm/Support/raw_ostream.h"
using namespace clang;
VerifyDiagnosticsClient::VerifyDiagnosticsClient(Diagnostic &_Diags,
DiagnosticClient *_Primary)
: Diags(_Diags), PrimaryClient(_Primary),
Buffer(new TextDiagnosticBuffer()), CurrentPreprocessor(0), NumErrors(0) {
}
VerifyDiagnosticsClient::~VerifyDiagnosticsClient() {
CheckDiagnostics();
}
// DiagnosticClient interface.
void VerifyDiagnosticsClient::BeginSourceFile(const LangOptions &LangOpts,
const Preprocessor *PP) {
// FIXME: Const hack, we screw up the preprocessor but in practice its ok
// because it doesn't get reused. It would be better if we could make a copy
// though.
CurrentPreprocessor = const_cast<Preprocessor*>(PP);
PrimaryClient->BeginSourceFile(LangOpts, PP);
}
void VerifyDiagnosticsClient::EndSourceFile() {
CheckDiagnostics();
PrimaryClient->EndSourceFile();
CurrentPreprocessor = 0;
}
void VerifyDiagnosticsClient::HandleDiagnostic(Diagnostic::Level DiagLevel,
const DiagnosticInfo &Info) {
// Send the diagnostic to the buffer, we will check it once we reach the end
// of the source file (or are destructed).
Buffer->HandleDiagnostic(DiagLevel, Info);
}
// FIXME: It would be nice to just get this from the primary diagnostic client
// or something.
bool VerifyDiagnosticsClient::HadErrors() {
CheckDiagnostics();
return NumErrors != 0;
}
//===----------------------------------------------------------------------===//
// Checking diagnostics implementation.
//===----------------------------------------------------------------------===//
typedef TextDiagnosticBuffer::DiagList DiagList;
typedef TextDiagnosticBuffer::const_iterator const_diag_iterator;
namespace {
/// Directive - Abstract class representing a parsed verify directive.
///
class Directive {
public:
static Directive* Create(bool RegexKind, const SourceLocation &Location,
const std::string &Text, unsigned Count);
public:
SourceLocation Location;
const std::string Text;
unsigned Count;
virtual ~Directive() { }
// Returns true if directive text is valid.
// Otherwise returns false and populates E.
virtual bool isValid(std::string &Error) = 0;
// Returns true on match.
virtual bool Match(const std::string &S) = 0;
protected:
Directive(const SourceLocation &Location, const std::string &Text,
unsigned Count)
: Location(Location), Text(Text), Count(Count) { }
private:
Directive(const Directive&); // DO NOT IMPLEMENT
void operator=(const Directive&); // DO NOT IMPLEMENT
};
/// StandardDirective - Directive with string matching.
///
class StandardDirective : public Directive {
public:
StandardDirective(const SourceLocation &Location, const std::string &Text,
unsigned Count)
: Directive(Location, Text, Count) { }
virtual bool isValid(std::string &Error) {
// all strings are considered valid; even empty ones
return true;
}
virtual bool Match(const std::string &S) {
return S.find(Text) != std::string::npos ||
Text.find(S) != std::string::npos;
}
};
/// RegexDirective - Directive with regular-expression matching.
///
class RegexDirective : public Directive {
public:
RegexDirective(const SourceLocation &Location, const std::string &Text,
unsigned Count)
: Directive(Location, Text, Count), Regex(Text) { }
virtual bool isValid(std::string &Error) {
if (Regex.isValid(Error))
return true;
return false;
}
virtual bool Match(const std::string &S) {
return Regex.match(S);
}
private:
llvm::Regex Regex;
};
typedef std::vector<Directive*> DirectiveList;
/// ExpectedData - owns directive objects and deletes on destructor.
///
struct ExpectedData {
DirectiveList Errors;
DirectiveList Warnings;
DirectiveList Notes;
~ExpectedData() {
DirectiveList* Lists[] = { &Errors, &Warnings, &Notes, 0 };
for (DirectiveList **PL = Lists; *PL; ++PL) {
DirectiveList * const L = *PL;
for (DirectiveList::iterator I = L->begin(), E = L->end(); I != E; ++I)
delete *I;
}
}
};
class ParseHelper
{
public:
ParseHelper(const char *Begin, const char *End)
: Begin(Begin), End(End), C(Begin), P(Begin), PEnd(NULL) { }
// Return true if string literal is next.
bool Next(const std::string &S) {
std::string::size_type LEN = S.length();
P = C;
PEnd = C + LEN;
if (PEnd > End)
return false;
return !memcmp(P, S.c_str(), LEN);
}
// Return true if number is next.
// Output N only if number is next.
bool Next(unsigned &N) {
unsigned TMP = 0;
P = C;
for (; P < End && P[0] >= '0' && P[0] <= '9'; ++P) {
TMP *= 10;
TMP += P[0] - '0';
}
if (P == C)
return false;
PEnd = P;
N = TMP;
return true;
}
// Return true if string literal is found.
// When true, P marks begin-position of S in content.
bool Search(const std::string &S) {
P = std::search(C, End, S.begin(), S.end());
PEnd = P + S.length();
return P != End;
}
// Advance 1-past previous next/search.
// Behavior is undefined if previous next/search failed.
bool Advance() {
C = PEnd;
return C < End;
}
// Skip zero or more whitespace.
void SkipWhitespace() {
for (; C < End && isspace(*C); ++C)
;
}
// Return true if EOF reached.
bool Done() {
return !(C < End);
}
const char * const Begin; // beginning of expected content
const char * const End; // end of expected content (1-past)
const char *C; // position of next char in content
const char *P;
private:
const char *PEnd; // previous next/search subject end (1-past)
};
} // namespace anonymous
/// ParseDirective - Go through the comment and see if it indicates expected
/// diagnostics. If so, then put them in the appropriate directive list.
///
static void ParseDirective(const char *CommentStart, unsigned CommentLen,
ExpectedData &ED, Preprocessor &PP,
SourceLocation Pos) {
// A single comment may contain multiple directives.
for (ParseHelper PH(CommentStart, CommentStart+CommentLen); !PH.Done();) {
// search for token: expected
if (!PH.Search("expected"))
break;
PH.Advance();
// next token: -
if (!PH.Next("-"))
continue;
PH.Advance();
// next token: { error | warning | note }
DirectiveList* DL = NULL;
if (PH.Next("error"))
DL = &ED.Errors;
else if (PH.Next("warning"))
DL = &ED.Warnings;
else if (PH.Next("note"))
DL = &ED.Notes;
else
continue;
PH.Advance();
// default directive kind
bool RegexKind = false;
const char* KindStr = "string";
// next optional token: -
if (PH.Next("-re")) {
PH.Advance();
RegexKind = true;
KindStr = "regex";
}
// skip optional whitespace
PH.SkipWhitespace();
// next optional token: positive integer
unsigned Count = 1;
if (PH.Next(Count))
PH.Advance();
// skip optional whitespace
PH.SkipWhitespace();
// next token: {{
if (!PH.Next("{{")) {
PP.Diag(Pos.getFileLocWithOffset(PH.C-PH.Begin),
diag::err_verify_missing_start) << KindStr;
continue;
}
PH.Advance();
const char* const ContentBegin = PH.C; // mark content begin
// search for token: }}
if (!PH.Search("}}")) {
PP.Diag(Pos.getFileLocWithOffset(PH.C-PH.Begin),
diag::err_verify_missing_end) << KindStr;
continue;
}
const char* const ContentEnd = PH.P; // mark content end
PH.Advance();
// build directive text; convert \n to newlines
std::string Text;
llvm::StringRef NewlineStr = "\\n";
llvm::StringRef Content(ContentBegin, ContentEnd-ContentBegin);
size_t CPos = 0;
size_t FPos;
while ((FPos = Content.find(NewlineStr, CPos)) != llvm::StringRef::npos) {
Text += Content.substr(CPos, FPos-CPos);
Text += '\n';
CPos = FPos + NewlineStr.size();
}
if (Text.empty())
Text.assign(ContentBegin, ContentEnd);
// construct new directive
Directive *D = Directive::Create(RegexKind, Pos, Text, Count);
std::string Error;
if (D->isValid(Error))
DL->push_back(D);
else {
PP.Diag(Pos.getFileLocWithOffset(ContentBegin-PH.Begin),
diag::err_verify_invalid_content)
<< KindStr << Error;
}
}
}
/// FindExpectedDiags - Lex the main source file to find all of the
// expected errors and warnings.
static void FindExpectedDiags(Preprocessor &PP, ExpectedData &ED) {
// Create a raw lexer to pull all the comments out of the main file. We don't
// want to look in #include'd headers for expected-error strings.
SourceManager &SM = PP.getSourceManager();
FileID FID = SM.getMainFileID();
if (SM.getMainFileID().isInvalid())
return;
// Create a lexer to lex all the tokens of the main file in raw mode.
const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
Lexer RawLex(FID, FromFile, SM, PP.getLangOptions());
// Return comments as tokens, this is how we find expected diagnostics.
RawLex.SetCommentRetentionState(true);
Token Tok;
Tok.setKind(tok::comment);
while (Tok.isNot(tok::eof)) {
RawLex.Lex(Tok);
if (!Tok.is(tok::comment)) continue;
std::string Comment = PP.getSpelling(Tok);
if (Comment.empty()) continue;
// Find all expected errors/warnings/notes.
ParseDirective(&Comment[0], Comment.size(), ED, PP, Tok.getLocation());
};
}
/// PrintProblem - This takes a diagnostic map of the delta between expected and
/// seen diagnostics. If there's anything in it, then something unexpected
/// happened. Print the map out in a nice format and return "true". If the map
/// is empty and we're not going to print things, then return "false".
///
static unsigned PrintProblem(Diagnostic &Diags, SourceManager *SourceMgr,
const_diag_iterator diag_begin,
const_diag_iterator diag_end,
const char *Kind, bool Expected) {
if (diag_begin == diag_end) return 0;
llvm::SmallString<256> Fmt;
llvm::raw_svector_ostream OS(Fmt);
for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I) {
if (I->first.isInvalid() || !SourceMgr)
OS << "\n (frontend)";
else
OS << "\n Line " << SourceMgr->getInstantiationLineNumber(I->first);
OS << ": " << I->second;
}
Diags.Report(diag::err_verify_inconsistent_diags)
<< Kind << !Expected << OS.str();
return std::distance(diag_begin, diag_end);
}
static unsigned PrintProblem(Diagnostic &Diags, SourceManager *SourceMgr,
DirectiveList &DL, const char *Kind,
bool Expected) {
if (DL.empty())
return 0;
llvm::SmallString<256> Fmt;
llvm::raw_svector_ostream OS(Fmt);
for (DirectiveList::iterator I = DL.begin(), E = DL.end(); I != E; ++I) {
Directive& D = **I;
if (D.Location.isInvalid() || !SourceMgr)
OS << "\n (frontend)";
else
OS << "\n Line " << SourceMgr->getInstantiationLineNumber(D.Location);
OS << ": " << D.Text;
}
Diags.Report(diag::err_verify_inconsistent_diags)
<< Kind << !Expected << OS.str();
return DL.size();
}
/// CheckLists - Compare expected to seen diagnostic lists and return the
/// the difference between them.
///
static unsigned CheckLists(Diagnostic &Diags, SourceManager &SourceMgr,
const char *Label,
DirectiveList &Left,
const_diag_iterator d2_begin,
const_diag_iterator d2_end) {
DirectiveList LeftOnly;
DiagList Right(d2_begin, d2_end);
for (DirectiveList::iterator I = Left.begin(), E = Left.end(); I != E; ++I) {
Directive& D = **I;
unsigned LineNo1 = SourceMgr.getInstantiationLineNumber(D.Location);
for (unsigned i = 0; i < D.Count; ++i) {
DiagList::iterator II, IE;
for (II = Right.begin(), IE = Right.end(); II != IE; ++II) {
unsigned LineNo2 = SourceMgr.getInstantiationLineNumber(II->first);
if (LineNo1 != LineNo2)
continue;
const std::string &RightText = II->second;
if (D.Match(RightText))
break;
}
if (II == IE) {
// Not found.
LeftOnly.push_back(*I);
} else {
// Found. The same cannot be found twice.
Right.erase(II);
}
}
}
// Now all that's left in Right are those that were not matched.
return (PrintProblem(Diags, &SourceMgr, LeftOnly, Label, true) +
PrintProblem(Diags, &SourceMgr, Right.begin(), Right.end(),
Label, false));
}
/// CheckResults - This compares the expected results to those that
/// were actually reported. It emits any discrepencies. Return "true" if there
/// were problems. Return "false" otherwise.
///
static unsigned CheckResults(Diagnostic &Diags, SourceManager &SourceMgr,
const TextDiagnosticBuffer &Buffer,
ExpectedData &ED) {
// We want to capture the delta between what was expected and what was
// seen.
//
// Expected \ Seen - set expected but not seen
// Seen \ Expected - set seen but not expected
unsigned NumProblems = 0;
// See if there are error mismatches.
NumProblems += CheckLists(Diags, SourceMgr, "error", ED.Errors,
Buffer.err_begin(), Buffer.err_end());
// See if there are warning mismatches.
NumProblems += CheckLists(Diags, SourceMgr, "warning", ED.Warnings,
Buffer.warn_begin(), Buffer.warn_end());
// See if there are note mismatches.
NumProblems += CheckLists(Diags, SourceMgr, "note", ED.Notes,
Buffer.note_begin(), Buffer.note_end());
return NumProblems;
}
void VerifyDiagnosticsClient::CheckDiagnostics() {
ExpectedData ED;
// Ensure any diagnostics go to the primary client.
DiagnosticClient *CurClient = Diags.getClient();
Diags.setClient(PrimaryClient.get());
// If we have a preprocessor, scan the source for expected diagnostic
// markers. If not then any diagnostics are unexpected.
if (CurrentPreprocessor) {
FindExpectedDiags(*CurrentPreprocessor, ED);
// Check that the expected diagnostics occurred.
NumErrors += CheckResults(Diags, CurrentPreprocessor->getSourceManager(),
*Buffer, ED);
} else {
NumErrors += (PrintProblem(Diags, 0,
Buffer->err_begin(), Buffer->err_end(),
"error", false) +
PrintProblem(Diags, 0,
Buffer->warn_begin(), Buffer->warn_end(),
"warn", false) +
PrintProblem(Diags, 0,
Buffer->note_begin(), Buffer->note_end(),
"note", false));
}
Diags.setClient(CurClient);
// Reset the buffer, we have processed all the diagnostics in it.
Buffer.reset(new TextDiagnosticBuffer());
}
Directive* Directive::Create(bool RegexKind, const SourceLocation &Location,
const std::string &Text, unsigned Count) {
if (RegexKind)
return new RegexDirective(Location, Text, Count);
return new StandardDirective(Location, Text, Count);
}
|