blob: 2eaf2fe62f5c1f8e83950dbd9fff7bf520fb335b [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- DiagChecker.cpp - Diagnostic Checking Functions ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Process the input files and check that the diagnostic messages are expected.
11//
12//===----------------------------------------------------------------------===//
13
Eli Friedmanb09f6e12009-05-19 04:14:29 +000014#include "clang/Frontend/Utils.h"
Daniel Dunbare1bd4e62009-03-02 06:16:29 +000015#include "clang/Frontend/TextDiagnosticBuffer.h"
Chris Lattnere91c1342008-02-06 00:23:21 +000016#include "clang/Sema/ParseAST.h"
Chris Lattner556beb72007-09-15 22:56:56 +000017#include "clang/AST/ASTConsumer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "clang/Basic/SourceManager.h"
19#include "clang/Lex/Preprocessor.h"
20using namespace clang;
21
22typedef TextDiagnosticBuffer::DiagList DiagList;
23typedef TextDiagnosticBuffer::const_iterator const_diag_iterator;
24
Chris Lattnerb2c8c552008-11-23 23:38:26 +000025static void EmitError(Preprocessor &PP, SourceLocation Pos, const char *String){
26 unsigned ID = PP.getDiagnostics().getCustomDiagID(Diagnostic::Error, String);
27 PP.Diag(Pos, ID);
28}
29
30
Reid Spencer5f016e22007-07-11 17:01:13 +000031// USING THE DIAGNOSTIC CHECKER:
32//
33// Indicating that a line expects an error or a warning is simple. Put a comment
34// on the line that has the diagnostic, use "expected-{error,warning}" to tag
35// if it's an expected error or warning, and place the expected text between {{
36// and }} markers. The full text doesn't have to be included, only enough to
37// ensure that the correct diagnostic was emitted.
38//
39// Here's an example:
40//
41// int A = B; // expected-error {{use of undeclared identifier 'B'}}
42//
43// You can place as many diagnostics on one line as you wish. To make the code
44// more readable, you can use slash-newline to separate out the diagnostics.
Sebastian Redl3cb06922009-02-07 19:52:04 +000045//
46// The simple syntax above allows each specification to match exactly one error.
47// You can use the extended syntax to customize this. The extended syntax is
48// "expected-<type> <n> {{diag text}}", where <type> is one of "error",
49// "warning" or "note", and <n> is a positive integer. This allows the
50// diagnostic to appear as many times as specified. Example:
51//
52// void f(); // expected-note 2 {{previous declaration is here}}
53//
Reid Spencer5f016e22007-07-11 17:01:13 +000054
Reid Spencer5f016e22007-07-11 17:01:13 +000055/// FindDiagnostics - Go through the comment and see if it indicates expected
56/// diagnostics. If so, then put them in a diagnostic list.
57///
Chris Lattner0947b4e2008-11-24 01:28:17 +000058static void FindDiagnostics(const char *CommentStart, unsigned CommentLen,
Reid Spencer5f016e22007-07-11 17:01:13 +000059 DiagList &ExpectedDiags,
Chris Lattner0947b4e2008-11-24 01:28:17 +000060 Preprocessor &PP, SourceLocation Pos,
61 const char *ExpectedStr) {
62 const char *CommentEnd = CommentStart+CommentLen;
63 unsigned ExpectedStrLen = strlen(ExpectedStr);
Chris Lattnerb2c8c552008-11-23 23:38:26 +000064
Chris Lattner0947b4e2008-11-24 01:28:17 +000065 // Find all expected-foo diagnostics in the string and add them to
66 // ExpectedDiags.
67 while (CommentStart != CommentEnd) {
68 CommentStart = std::find(CommentStart, CommentEnd, 'e');
69 if (unsigned(CommentEnd-CommentStart) < ExpectedStrLen) return;
70
71 // If this isn't expected-foo, ignore it.
72 if (memcmp(CommentStart, ExpectedStr, ExpectedStrLen)) {
73 ++CommentStart;
74 continue;
75 }
76
77 CommentStart += ExpectedStrLen;
78
79 // Skip whitespace.
80 while (CommentStart != CommentEnd &&
81 isspace(CommentStart[0]))
82 ++CommentStart;
83
Sebastian Redl3cb06922009-02-07 19:52:04 +000084 // Default, if we find the '{' now, is 1 time.
85 int Times = 1;
86 int Temp = 0;
87 // In extended syntax, there could be a digit now.
88 while (CommentStart != CommentEnd &&
89 CommentStart[0] >= '0' && CommentStart[0] <= '9') {
90 Temp *= 10;
91 Temp += CommentStart[0] - '0';
92 ++CommentStart;
93 }
94 if (Temp > 0)
95 Times = Temp;
96
97 // Skip whitespace again.
98 while (CommentStart != CommentEnd &&
99 isspace(CommentStart[0]))
100 ++CommentStart;
101
Chris Lattner0947b4e2008-11-24 01:28:17 +0000102 // We should have a {{ now.
103 if (CommentEnd-CommentStart < 2 ||
104 CommentStart[0] != '{' || CommentStart[1] != '{') {
105 if (std::find(CommentStart, CommentEnd, '{') != CommentEnd)
106 EmitError(PP, Pos, "bogus characters before '{{' in expected string");
107 else
108 EmitError(PP, Pos, "cannot find start ('{{') of expected string");
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000109 return;
Sebastian Redl3cb06922009-02-07 19:52:04 +0000110 }
Chris Lattner0947b4e2008-11-24 01:28:17 +0000111 CommentStart += 2;
112
113 // Find the }}.
114 const char *ExpectedEnd = CommentStart;
115 while (1) {
116 ExpectedEnd = std::find(ExpectedEnd, CommentEnd, '}');
117 if (CommentEnd-ExpectedEnd < 2) {
118 EmitError(PP, Pos, "cannot find end ('}}') of expected string");
119 return;
120 }
121
122 if (ExpectedEnd[1] == '}')
123 break;
124
125 ++ExpectedEnd; // Skip over singular }'s
Reid Spencer5f016e22007-07-11 17:01:13 +0000126 }
127
Chris Lattner0947b4e2008-11-24 01:28:17 +0000128 std::string Msg(CommentStart, ExpectedEnd);
129 std::string::size_type FindPos;
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000130 while ((FindPos = Msg.find("\\n")) != std::string::npos)
Sebastian Redlad3c91c2008-10-26 19:05:16 +0000131 Msg.replace(FindPos, 2, "\n");
Sebastian Redl3cb06922009-02-07 19:52:04 +0000132 // Add is possibly multiple times.
133 for (int i = 0; i < Times; ++i)
134 ExpectedDiags.push_back(std::make_pair(Pos, Msg));
135
Chris Lattner0947b4e2008-11-24 01:28:17 +0000136 CommentStart = ExpectedEnd;
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 }
138}
139
Ted Kremenek95041a22007-12-19 22:51:13 +0000140/// FindExpectedDiags - Lex the main source file to find all of the
141// expected errors and warnings.
142static void FindExpectedDiags(Preprocessor &PP,
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 DiagList &ExpectedErrors,
Douglas Gregor233f74b2008-09-11 02:46:36 +0000144 DiagList &ExpectedWarnings,
145 DiagList &ExpectedNotes) {
Chris Lattnera39f0482008-11-21 01:18:36 +0000146 // Create a raw lexer to pull all the comments out of the main file. We don't
147 // want to look in #include'd headers for expected-error strings.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000148 FileID FID = PP.getSourceManager().getMainFileID();
Chris Lattnera39f0482008-11-21 01:18:36 +0000149
150 // Create a lexer to lex all the tokens of the main file in raw mode.
Chris Lattner4448a012009-01-17 07:41:36 +0000151 Lexer RawLex(FID, PP.getSourceManager(), PP.getLangOptions());
Chris Lattnera39f0482008-11-21 01:18:36 +0000152
153 // Return comments as tokens, this is how we find expected diagnostics.
154 RawLex.SetCommentRetentionState(true);
155
Chris Lattnerd2177732007-07-20 16:59:19 +0000156 Token Tok;
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000157 Tok.setKind(tok::comment);
158 while (Tok.isNot(tok::eof)) {
Chris Lattnera39f0482008-11-21 01:18:36 +0000159 RawLex.Lex(Tok);
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000160 if (!Tok.is(tok::comment)) continue;
161
162 std::string Comment = PP.getSpelling(Tok);
Chris Lattner0947b4e2008-11-24 01:28:17 +0000163 if (Comment.empty()) continue;
Reid Spencer5f016e22007-07-11 17:01:13 +0000164
Chris Lattner0947b4e2008-11-24 01:28:17 +0000165
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000166 // Find all expected errors.
Chris Lattner0947b4e2008-11-24 01:28:17 +0000167 FindDiagnostics(&Comment[0], Comment.size(), ExpectedErrors, PP,
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000168 Tok.getLocation(), "expected-error");
Reid Spencer5f016e22007-07-11 17:01:13 +0000169
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000170 // Find all expected warnings.
Chris Lattner0947b4e2008-11-24 01:28:17 +0000171 FindDiagnostics(&Comment[0], Comment.size(), ExpectedWarnings, PP,
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000172 Tok.getLocation(), "expected-warning");
Reid Spencer5f016e22007-07-11 17:01:13 +0000173
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000174 // Find all expected notes.
Chris Lattner0947b4e2008-11-24 01:28:17 +0000175 FindDiagnostics(&Comment[0], Comment.size(), ExpectedNotes, PP,
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000176 Tok.getLocation(), "expected-note");
177 };
Reid Spencer5f016e22007-07-11 17:01:13 +0000178}
179
180/// PrintProblem - This takes a diagnostic map of the delta between expected and
181/// seen diagnostics. If there's anything in it, then something unexpected
182/// happened. Print the map out in a nice format and return "true". If the map
183/// is empty and we're not going to print things, then return "false".
184///
185static bool PrintProblem(SourceManager &SourceMgr,
186 const_diag_iterator diag_begin,
187 const_diag_iterator diag_end,
188 const char *Msg) {
189 if (diag_begin == diag_end) return false;
190
191 fprintf(stderr, "%s\n", Msg);
192
193 for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I)
194 fprintf(stderr, " Line %d: %s\n",
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000195 SourceMgr.getInstantiationLineNumber(I->first),
Reid Spencer5f016e22007-07-11 17:01:13 +0000196 I->second.c_str());
197
198 return true;
199}
200
Chris Lattner4e69fd52009-02-04 02:15:34 +0000201/// CompareDiagLists - Compare two diagnostic lists and return the difference
Reid Spencer5f016e22007-07-11 17:01:13 +0000202/// between them.
203///
204static bool CompareDiagLists(SourceManager &SourceMgr,
205 const_diag_iterator d1_begin,
206 const_diag_iterator d1_end,
207 const_diag_iterator d2_begin,
208 const_diag_iterator d2_end,
Sebastian Redl3cb06922009-02-07 19:52:04 +0000209 const char *MsgLeftOnly,
210 const char *MsgRightOnly) {
211 DiagList LeftOnly;
212 DiagList Left(d1_begin, d1_end);
213 DiagList Right(d2_begin, d2_end);
Reid Spencer5f016e22007-07-11 17:01:13 +0000214
Sebastian Redl3cb06922009-02-07 19:52:04 +0000215 for (const_diag_iterator I = Left.begin(), E = Left.end(); I != E; ++I) {
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000216 unsigned LineNo1 = SourceMgr.getInstantiationLineNumber(I->first);
Reid Spencer5f016e22007-07-11 17:01:13 +0000217 const std::string &Diag1 = I->second;
Reid Spencer5f016e22007-07-11 17:01:13 +0000218
Sebastian Redl3cb06922009-02-07 19:52:04 +0000219 DiagList::iterator II, IE;
220 for (II = Right.begin(), IE = Right.end(); II != IE; ++II) {
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000221 unsigned LineNo2 = SourceMgr.getInstantiationLineNumber(II->first);
Reid Spencer5f016e22007-07-11 17:01:13 +0000222 if (LineNo1 != LineNo2) continue;
223
224 const std::string &Diag2 = II->second;
225 if (Diag2.find(Diag1) != std::string::npos ||
226 Diag1.find(Diag2) != std::string::npos) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000227 break;
228 }
229 }
Sebastian Redl3cb06922009-02-07 19:52:04 +0000230 if (II == IE) {
231 // Not found.
232 LeftOnly.push_back(*I);
233 } else {
234 // Found. The same cannot be found twice.
235 Right.erase(II);
236 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000237 }
Sebastian Redl3cb06922009-02-07 19:52:04 +0000238 // Now all that's left in Right are those that were not matched.
Reid Spencer5f016e22007-07-11 17:01:13 +0000239
Sebastian Redl3cb06922009-02-07 19:52:04 +0000240 return PrintProblem(SourceMgr, LeftOnly.begin(), LeftOnly.end(), MsgLeftOnly)
241 | PrintProblem(SourceMgr, Right.begin(), Right.end(), MsgRightOnly);
Reid Spencer5f016e22007-07-11 17:01:13 +0000242}
243
244/// CheckResults - This compares the expected results to those that
245/// were actually reported. It emits any discrepencies. Return "true" if there
246/// were problems. Return "false" otherwise.
247///
248static bool CheckResults(Preprocessor &PP,
249 const DiagList &ExpectedErrors,
Douglas Gregor233f74b2008-09-11 02:46:36 +0000250 const DiagList &ExpectedWarnings,
251 const DiagList &ExpectedNotes) {
Nico Weber7bfaaae2008-08-10 19:59:06 +0000252 const DiagnosticClient *DiagClient = PP.getDiagnostics().getClient();
253 assert(DiagClient != 0 &&
254 "DiagChecker requires a valid TextDiagnosticBuffer");
Reid Spencer5f016e22007-07-11 17:01:13 +0000255 const TextDiagnosticBuffer &Diags =
Nico Weber7bfaaae2008-08-10 19:59:06 +0000256 static_cast<const TextDiagnosticBuffer&>(*DiagClient);
Reid Spencer5f016e22007-07-11 17:01:13 +0000257 SourceManager &SourceMgr = PP.getSourceManager();
258
259 // We want to capture the delta between what was expected and what was
260 // seen.
261 //
262 // Expected \ Seen - set expected but not seen
263 // Seen \ Expected - set seen but not expected
264 bool HadProblem = false;
265
Sebastian Redl3cb06922009-02-07 19:52:04 +0000266 // See if there are error mismatches.
Reid Spencer5f016e22007-07-11 17:01:13 +0000267 HadProblem |= CompareDiagLists(SourceMgr,
268 ExpectedErrors.begin(), ExpectedErrors.end(),
269 Diags.err_begin(), Diags.err_end(),
Sebastian Redl3cb06922009-02-07 19:52:04 +0000270 "Errors expected but not seen:",
Reid Spencer5f016e22007-07-11 17:01:13 +0000271 "Errors seen but not expected:");
272
Sebastian Redl3cb06922009-02-07 19:52:04 +0000273 // See if there are warning mismatches.
Reid Spencer5f016e22007-07-11 17:01:13 +0000274 HadProblem |= CompareDiagLists(SourceMgr,
275 ExpectedWarnings.begin(),
276 ExpectedWarnings.end(),
277 Diags.warn_begin(), Diags.warn_end(),
Sebastian Redl3cb06922009-02-07 19:52:04 +0000278 "Warnings expected but not seen:",
Reid Spencer5f016e22007-07-11 17:01:13 +0000279 "Warnings seen but not expected:");
280
Sebastian Redl3cb06922009-02-07 19:52:04 +0000281 // See if there are note mismatches.
Douglas Gregor233f74b2008-09-11 02:46:36 +0000282 HadProblem |= CompareDiagLists(SourceMgr,
283 ExpectedNotes.begin(),
284 ExpectedNotes.end(),
285 Diags.note_begin(), Diags.note_end(),
Sebastian Redl3cb06922009-02-07 19:52:04 +0000286 "Notes expected but not seen:",
Douglas Gregor233f74b2008-09-11 02:46:36 +0000287 "Notes seen but not expected:");
288
Reid Spencer5f016e22007-07-11 17:01:13 +0000289 return HadProblem;
290}
291
Chris Lattner009e9f72007-08-10 18:27:41 +0000292
Argyrios Kyrtzidis14d41402008-06-13 12:15:34 +0000293/// CheckDiagnostics - Gather the expected diagnostics and check them.
294bool clang::CheckDiagnostics(Preprocessor &PP) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 // Gather the set of expected diagnostics.
Douglas Gregor233f74b2008-09-11 02:46:36 +0000296 DiagList ExpectedErrors, ExpectedWarnings, ExpectedNotes;
297 FindExpectedDiags(PP, ExpectedErrors, ExpectedWarnings, ExpectedNotes);
Ted Kremenek44579782007-09-25 18:37:20 +0000298
Reid Spencer5f016e22007-07-11 17:01:13 +0000299 // Check that the expected diagnostics occurred.
Douglas Gregor233f74b2008-09-11 02:46:36 +0000300 return CheckResults(PP, ExpectedErrors, ExpectedWarnings, ExpectedNotes);
Reid Spencer5f016e22007-07-11 17:01:13 +0000301}