blob: 3482d12280413c511703a0df8f7fff18a9e5fdf5 [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
Ted Kremenekc2542b62009-03-31 18:58:14 +000014#include "clang-cc.h"
Chris Lattner97e8b6f2007-10-07 06:04:32 +000015#include "ASTConsumers.h"
Daniel Dunbare1bd4e62009-03-02 06:16:29 +000016#include "clang/Frontend/TextDiagnosticBuffer.h"
Chris Lattnere91c1342008-02-06 00:23:21 +000017#include "clang/Sema/ParseAST.h"
Chris Lattner556beb72007-09-15 22:56:56 +000018#include "clang/AST/ASTConsumer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "clang/Basic/SourceManager.h"
20#include "clang/Lex/Preprocessor.h"
21using namespace clang;
22
23typedef TextDiagnosticBuffer::DiagList DiagList;
24typedef TextDiagnosticBuffer::const_iterator const_diag_iterator;
25
Chris Lattnerb2c8c552008-11-23 23:38:26 +000026static void EmitError(Preprocessor &PP, SourceLocation Pos, const char *String){
27 unsigned ID = PP.getDiagnostics().getCustomDiagID(Diagnostic::Error, String);
28 PP.Diag(Pos, ID);
29}
30
31
Reid Spencer5f016e22007-07-11 17:01:13 +000032// USING THE DIAGNOSTIC CHECKER:
33//
34// Indicating that a line expects an error or a warning is simple. Put a comment
35// on the line that has the diagnostic, use "expected-{error,warning}" to tag
36// if it's an expected error or warning, and place the expected text between {{
37// and }} markers. The full text doesn't have to be included, only enough to
38// ensure that the correct diagnostic was emitted.
39//
40// Here's an example:
41//
42// int A = B; // expected-error {{use of undeclared identifier 'B'}}
43//
44// You can place as many diagnostics on one line as you wish. To make the code
45// more readable, you can use slash-newline to separate out the diagnostics.
Sebastian Redl3cb06922009-02-07 19:52:04 +000046//
47// The simple syntax above allows each specification to match exactly one error.
48// You can use the extended syntax to customize this. The extended syntax is
49// "expected-<type> <n> {{diag text}}", where <type> is one of "error",
50// "warning" or "note", and <n> is a positive integer. This allows the
51// diagnostic to appear as many times as specified. Example:
52//
53// void f(); // expected-note 2 {{previous declaration is here}}
54//
Reid Spencer5f016e22007-07-11 17:01:13 +000055
Reid Spencer5f016e22007-07-11 17:01:13 +000056/// FindDiagnostics - Go through the comment and see if it indicates expected
57/// diagnostics. If so, then put them in a diagnostic list.
58///
Chris Lattner0947b4e2008-11-24 01:28:17 +000059static void FindDiagnostics(const char *CommentStart, unsigned CommentLen,
Reid Spencer5f016e22007-07-11 17:01:13 +000060 DiagList &ExpectedDiags,
Chris Lattner0947b4e2008-11-24 01:28:17 +000061 Preprocessor &PP, SourceLocation Pos,
62 const char *ExpectedStr) {
63 const char *CommentEnd = CommentStart+CommentLen;
64 unsigned ExpectedStrLen = strlen(ExpectedStr);
Chris Lattnerb2c8c552008-11-23 23:38:26 +000065
Chris Lattner0947b4e2008-11-24 01:28:17 +000066 // Find all expected-foo diagnostics in the string and add them to
67 // ExpectedDiags.
68 while (CommentStart != CommentEnd) {
69 CommentStart = std::find(CommentStart, CommentEnd, 'e');
70 if (unsigned(CommentEnd-CommentStart) < ExpectedStrLen) return;
71
72 // If this isn't expected-foo, ignore it.
73 if (memcmp(CommentStart, ExpectedStr, ExpectedStrLen)) {
74 ++CommentStart;
75 continue;
76 }
77
78 CommentStart += ExpectedStrLen;
79
80 // Skip whitespace.
81 while (CommentStart != CommentEnd &&
82 isspace(CommentStart[0]))
83 ++CommentStart;
84
Sebastian Redl3cb06922009-02-07 19:52:04 +000085 // Default, if we find the '{' now, is 1 time.
86 int Times = 1;
87 int Temp = 0;
88 // In extended syntax, there could be a digit now.
89 while (CommentStart != CommentEnd &&
90 CommentStart[0] >= '0' && CommentStart[0] <= '9') {
91 Temp *= 10;
92 Temp += CommentStart[0] - '0';
93 ++CommentStart;
94 }
95 if (Temp > 0)
96 Times = Temp;
97
98 // Skip whitespace again.
99 while (CommentStart != CommentEnd &&
100 isspace(CommentStart[0]))
101 ++CommentStart;
102
Chris Lattner0947b4e2008-11-24 01:28:17 +0000103 // We should have a {{ now.
104 if (CommentEnd-CommentStart < 2 ||
105 CommentStart[0] != '{' || CommentStart[1] != '{') {
106 if (std::find(CommentStart, CommentEnd, '{') != CommentEnd)
107 EmitError(PP, Pos, "bogus characters before '{{' in expected string");
108 else
109 EmitError(PP, Pos, "cannot find start ('{{') of expected string");
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000110 return;
Sebastian Redl3cb06922009-02-07 19:52:04 +0000111 }
Chris Lattner0947b4e2008-11-24 01:28:17 +0000112 CommentStart += 2;
113
114 // Find the }}.
115 const char *ExpectedEnd = CommentStart;
116 while (1) {
117 ExpectedEnd = std::find(ExpectedEnd, CommentEnd, '}');
118 if (CommentEnd-ExpectedEnd < 2) {
119 EmitError(PP, Pos, "cannot find end ('}}') of expected string");
120 return;
121 }
122
123 if (ExpectedEnd[1] == '}')
124 break;
125
126 ++ExpectedEnd; // Skip over singular }'s
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 }
128
Chris Lattner0947b4e2008-11-24 01:28:17 +0000129 std::string Msg(CommentStart, ExpectedEnd);
130 std::string::size_type FindPos;
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000131 while ((FindPos = Msg.find("\\n")) != std::string::npos)
Sebastian Redlad3c91c2008-10-26 19:05:16 +0000132 Msg.replace(FindPos, 2, "\n");
Sebastian Redl3cb06922009-02-07 19:52:04 +0000133 // Add is possibly multiple times.
134 for (int i = 0; i < Times; ++i)
135 ExpectedDiags.push_back(std::make_pair(Pos, Msg));
136
Chris Lattner0947b4e2008-11-24 01:28:17 +0000137 CommentStart = ExpectedEnd;
Reid Spencer5f016e22007-07-11 17:01:13 +0000138 }
139}
140
Ted Kremenek95041a22007-12-19 22:51:13 +0000141/// FindExpectedDiags - Lex the main source file to find all of the
142// expected errors and warnings.
143static void FindExpectedDiags(Preprocessor &PP,
Reid Spencer5f016e22007-07-11 17:01:13 +0000144 DiagList &ExpectedErrors,
Douglas Gregor233f74b2008-09-11 02:46:36 +0000145 DiagList &ExpectedWarnings,
146 DiagList &ExpectedNotes) {
Chris Lattnera39f0482008-11-21 01:18:36 +0000147 // Create a raw lexer to pull all the comments out of the main file. We don't
148 // want to look in #include'd headers for expected-error strings.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000149 FileID FID = PP.getSourceManager().getMainFileID();
Chris Lattnera39f0482008-11-21 01:18:36 +0000150
151 // Create a lexer to lex all the tokens of the main file in raw mode.
Chris Lattner4448a012009-01-17 07:41:36 +0000152 Lexer RawLex(FID, PP.getSourceManager(), PP.getLangOptions());
Chris Lattnera39f0482008-11-21 01:18:36 +0000153
154 // Return comments as tokens, this is how we find expected diagnostics.
155 RawLex.SetCommentRetentionState(true);
156
Chris Lattnerd2177732007-07-20 16:59:19 +0000157 Token Tok;
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000158 Tok.setKind(tok::comment);
159 while (Tok.isNot(tok::eof)) {
Chris Lattnera39f0482008-11-21 01:18:36 +0000160 RawLex.Lex(Tok);
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000161 if (!Tok.is(tok::comment)) continue;
162
163 std::string Comment = PP.getSpelling(Tok);
Chris Lattner0947b4e2008-11-24 01:28:17 +0000164 if (Comment.empty()) continue;
Reid Spencer5f016e22007-07-11 17:01:13 +0000165
Chris Lattner0947b4e2008-11-24 01:28:17 +0000166
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000167 // Find all expected errors.
Chris Lattner0947b4e2008-11-24 01:28:17 +0000168 FindDiagnostics(&Comment[0], Comment.size(), ExpectedErrors, PP,
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000169 Tok.getLocation(), "expected-error");
Reid Spencer5f016e22007-07-11 17:01:13 +0000170
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000171 // Find all expected warnings.
Chris Lattner0947b4e2008-11-24 01:28:17 +0000172 FindDiagnostics(&Comment[0], Comment.size(), ExpectedWarnings, PP,
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000173 Tok.getLocation(), "expected-warning");
Reid Spencer5f016e22007-07-11 17:01:13 +0000174
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000175 // Find all expected notes.
Chris Lattner0947b4e2008-11-24 01:28:17 +0000176 FindDiagnostics(&Comment[0], Comment.size(), ExpectedNotes, PP,
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000177 Tok.getLocation(), "expected-note");
178 };
Reid Spencer5f016e22007-07-11 17:01:13 +0000179}
180
181/// PrintProblem - This takes a diagnostic map of the delta between expected and
182/// seen diagnostics. If there's anything in it, then something unexpected
183/// happened. Print the map out in a nice format and return "true". If the map
184/// is empty and we're not going to print things, then return "false".
185///
186static bool PrintProblem(SourceManager &SourceMgr,
187 const_diag_iterator diag_begin,
188 const_diag_iterator diag_end,
189 const char *Msg) {
190 if (diag_begin == diag_end) return false;
191
192 fprintf(stderr, "%s\n", Msg);
193
194 for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I)
195 fprintf(stderr, " Line %d: %s\n",
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000196 SourceMgr.getInstantiationLineNumber(I->first),
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 I->second.c_str());
198
199 return true;
200}
201
Chris Lattner4e69fd52009-02-04 02:15:34 +0000202/// CompareDiagLists - Compare two diagnostic lists and return the difference
Reid Spencer5f016e22007-07-11 17:01:13 +0000203/// between them.
204///
205static bool CompareDiagLists(SourceManager &SourceMgr,
206 const_diag_iterator d1_begin,
207 const_diag_iterator d1_end,
208 const_diag_iterator d2_begin,
209 const_diag_iterator d2_end,
Sebastian Redl3cb06922009-02-07 19:52:04 +0000210 const char *MsgLeftOnly,
211 const char *MsgRightOnly) {
212 DiagList LeftOnly;
213 DiagList Left(d1_begin, d1_end);
214 DiagList Right(d2_begin, d2_end);
Reid Spencer5f016e22007-07-11 17:01:13 +0000215
Sebastian Redl3cb06922009-02-07 19:52:04 +0000216 for (const_diag_iterator I = Left.begin(), E = Left.end(); I != E; ++I) {
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000217 unsigned LineNo1 = SourceMgr.getInstantiationLineNumber(I->first);
Reid Spencer5f016e22007-07-11 17:01:13 +0000218 const std::string &Diag1 = I->second;
Reid Spencer5f016e22007-07-11 17:01:13 +0000219
Sebastian Redl3cb06922009-02-07 19:52:04 +0000220 DiagList::iterator II, IE;
221 for (II = Right.begin(), IE = Right.end(); II != IE; ++II) {
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000222 unsigned LineNo2 = SourceMgr.getInstantiationLineNumber(II->first);
Reid Spencer5f016e22007-07-11 17:01:13 +0000223 if (LineNo1 != LineNo2) continue;
224
225 const std::string &Diag2 = II->second;
226 if (Diag2.find(Diag1) != std::string::npos ||
227 Diag1.find(Diag2) != std::string::npos) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000228 break;
229 }
230 }
Sebastian Redl3cb06922009-02-07 19:52:04 +0000231 if (II == IE) {
232 // Not found.
233 LeftOnly.push_back(*I);
234 } else {
235 // Found. The same cannot be found twice.
236 Right.erase(II);
237 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000238 }
Sebastian Redl3cb06922009-02-07 19:52:04 +0000239 // Now all that's left in Right are those that were not matched.
Reid Spencer5f016e22007-07-11 17:01:13 +0000240
Sebastian Redl3cb06922009-02-07 19:52:04 +0000241 return PrintProblem(SourceMgr, LeftOnly.begin(), LeftOnly.end(), MsgLeftOnly)
242 | PrintProblem(SourceMgr, Right.begin(), Right.end(), MsgRightOnly);
Reid Spencer5f016e22007-07-11 17:01:13 +0000243}
244
245/// CheckResults - This compares the expected results to those that
246/// were actually reported. It emits any discrepencies. Return "true" if there
247/// were problems. Return "false" otherwise.
248///
249static bool CheckResults(Preprocessor &PP,
250 const DiagList &ExpectedErrors,
Douglas Gregor233f74b2008-09-11 02:46:36 +0000251 const DiagList &ExpectedWarnings,
252 const DiagList &ExpectedNotes) {
Nico Weber7bfaaae2008-08-10 19:59:06 +0000253 const DiagnosticClient *DiagClient = PP.getDiagnostics().getClient();
254 assert(DiagClient != 0 &&
255 "DiagChecker requires a valid TextDiagnosticBuffer");
Reid Spencer5f016e22007-07-11 17:01:13 +0000256 const TextDiagnosticBuffer &Diags =
Nico Weber7bfaaae2008-08-10 19:59:06 +0000257 static_cast<const TextDiagnosticBuffer&>(*DiagClient);
Reid Spencer5f016e22007-07-11 17:01:13 +0000258 SourceManager &SourceMgr = PP.getSourceManager();
259
260 // We want to capture the delta between what was expected and what was
261 // seen.
262 //
263 // Expected \ Seen - set expected but not seen
264 // Seen \ Expected - set seen but not expected
265 bool HadProblem = false;
266
Sebastian Redl3cb06922009-02-07 19:52:04 +0000267 // See if there are error mismatches.
Reid Spencer5f016e22007-07-11 17:01:13 +0000268 HadProblem |= CompareDiagLists(SourceMgr,
269 ExpectedErrors.begin(), ExpectedErrors.end(),
270 Diags.err_begin(), Diags.err_end(),
Sebastian Redl3cb06922009-02-07 19:52:04 +0000271 "Errors expected but not seen:",
Reid Spencer5f016e22007-07-11 17:01:13 +0000272 "Errors seen but not expected:");
273
Sebastian Redl3cb06922009-02-07 19:52:04 +0000274 // See if there are warning mismatches.
Reid Spencer5f016e22007-07-11 17:01:13 +0000275 HadProblem |= CompareDiagLists(SourceMgr,
276 ExpectedWarnings.begin(),
277 ExpectedWarnings.end(),
278 Diags.warn_begin(), Diags.warn_end(),
Sebastian Redl3cb06922009-02-07 19:52:04 +0000279 "Warnings expected but not seen:",
Reid Spencer5f016e22007-07-11 17:01:13 +0000280 "Warnings seen but not expected:");
281
Sebastian Redl3cb06922009-02-07 19:52:04 +0000282 // See if there are note mismatches.
Douglas Gregor233f74b2008-09-11 02:46:36 +0000283 HadProblem |= CompareDiagLists(SourceMgr,
284 ExpectedNotes.begin(),
285 ExpectedNotes.end(),
286 Diags.note_begin(), Diags.note_end(),
Sebastian Redl3cb06922009-02-07 19:52:04 +0000287 "Notes expected but not seen:",
Douglas Gregor233f74b2008-09-11 02:46:36 +0000288 "Notes seen but not expected:");
289
Reid Spencer5f016e22007-07-11 17:01:13 +0000290 return HadProblem;
291}
292
Chris Lattner009e9f72007-08-10 18:27:41 +0000293
Argyrios Kyrtzidis14d41402008-06-13 12:15:34 +0000294/// CheckDiagnostics - Gather the expected diagnostics and check them.
295bool clang::CheckDiagnostics(Preprocessor &PP) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000296 // Gather the set of expected diagnostics.
Douglas Gregor233f74b2008-09-11 02:46:36 +0000297 DiagList ExpectedErrors, ExpectedWarnings, ExpectedNotes;
298 FindExpectedDiags(PP, ExpectedErrors, ExpectedWarnings, ExpectedNotes);
Ted Kremenek44579782007-09-25 18:37:20 +0000299
Reid Spencer5f016e22007-07-11 17:01:13 +0000300 // Check that the expected diagnostics occurred.
Douglas Gregor233f74b2008-09-11 02:46:36 +0000301 return CheckResults(PP, ExpectedErrors, ExpectedWarnings, ExpectedNotes);
Reid Spencer5f016e22007-07-11 17:01:13 +0000302}