blob: 66d7ed7cf40a0679f8d43f7852ac83e94058619a [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"
John McCall19510852010-08-20 18:27:03 +000016#include "clang/Parse/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"
Daniel Dunbare7cb7e42009-12-03 09:14:02 +000020#include "llvm/Support/raw_ostream.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021using 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.
Mike Stump1eb44332009-09-09 15:08:12 +000058///
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);
Mike Stump1eb44332009-09-09 15:08:12 +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;
Mike Stump1eb44332009-09-09 15:08:12 +000071
Chris Lattner0947b4e2008-11-24 01:28:17 +000072 // If this isn't expected-foo, ignore it.
73 if (memcmp(CommentStart, ExpectedStr, ExpectedStrLen)) {
74 ++CommentStart;
75 continue;
76 }
Mike Stump1eb44332009-09-09 15:08:12 +000077
Chris Lattner0947b4e2008-11-24 01:28:17 +000078 CommentStart += ExpectedStrLen;
Mike Stump1eb44332009-09-09 15:08:12 +000079
Chris Lattner0947b4e2008-11-24 01:28:17 +000080 // Skip whitespace.
81 while (CommentStart != CommentEnd &&
82 isspace(CommentStart[0]))
83 ++CommentStart;
Mike Stump1eb44332009-09-09 15:08:12 +000084
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;
Mike Stump1eb44332009-09-09 15:08:12 +000097
Sebastian Redl3cb06922009-02-07 19:52:04 +000098 // Skip whitespace again.
99 while (CommentStart != CommentEnd &&
100 isspace(CommentStart[0]))
101 ++CommentStart;
Mike Stump1eb44332009-09-09 15:08:12 +0000102
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 }
Mike Stump1eb44332009-09-09 15:08:12 +0000122
Chris Lattner0947b4e2008-11-24 01:28:17 +0000123 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();
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Chris Lattnera39f0482008-11-21 01:18:36 +0000151 // Create a lexer to lex all the tokens of the main file in raw mode.
Chris Lattner6e290142009-11-30 04:18:44 +0000152 const llvm::MemoryBuffer *FromFile = PP.getSourceManager().getBuffer(FID);
153 Lexer RawLex(FID, FromFile, PP.getSourceManager(), PP.getLangOptions());
Mike Stump1eb44332009-09-09 15:08:12 +0000154
Chris Lattnera39f0482008-11-21 01:18:36 +0000155 // Return comments as tokens, this is how we find expected diagnostics.
156 RawLex.SetCommentRetentionState(true);
157
Chris Lattnerd2177732007-07-20 16:59:19 +0000158 Token Tok;
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000159 Tok.setKind(tok::comment);
160 while (Tok.isNot(tok::eof)) {
Chris Lattnera39f0482008-11-21 01:18:36 +0000161 RawLex.Lex(Tok);
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000162 if (!Tok.is(tok::comment)) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000164 std::string Comment = PP.getSpelling(Tok);
Chris Lattner0947b4e2008-11-24 01:28:17 +0000165 if (Comment.empty()) continue;
Reid Spencer5f016e22007-07-11 17:01:13 +0000166
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000168 // Find all expected errors.
Chris Lattner0947b4e2008-11-24 01:28:17 +0000169 FindDiagnostics(&Comment[0], Comment.size(), ExpectedErrors, PP,
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000170 Tok.getLocation(), "expected-error");
Reid Spencer5f016e22007-07-11 17:01:13 +0000171
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000172 // Find all expected warnings.
Chris Lattner0947b4e2008-11-24 01:28:17 +0000173 FindDiagnostics(&Comment[0], Comment.size(), ExpectedWarnings, PP,
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000174 Tok.getLocation(), "expected-warning");
Reid Spencer5f016e22007-07-11 17:01:13 +0000175
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000176 // Find all expected notes.
Chris Lattner0947b4e2008-11-24 01:28:17 +0000177 FindDiagnostics(&Comment[0], Comment.size(), ExpectedNotes, PP,
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000178 Tok.getLocation(), "expected-note");
179 };
Reid Spencer5f016e22007-07-11 17:01:13 +0000180}
181
182/// PrintProblem - This takes a diagnostic map of the delta between expected and
183/// seen diagnostics. If there's anything in it, then something unexpected
184/// happened. Print the map out in a nice format and return "true". If the map
185/// is empty and we're not going to print things, then return "false".
Mike Stump1eb44332009-09-09 15:08:12 +0000186///
Reid Spencer5f016e22007-07-11 17:01:13 +0000187static bool PrintProblem(SourceManager &SourceMgr,
188 const_diag_iterator diag_begin,
189 const_diag_iterator diag_end,
190 const char *Msg) {
191 if (diag_begin == diag_end) return false;
192
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000193 llvm::errs() << Msg << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000194 for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I)
Daniel Dunbare7cb7e42009-12-03 09:14:02 +0000195 llvm::errs() << " Line " << SourceMgr.getInstantiationLineNumber(I->first)
196 << " " << I->second << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000197
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.
Mike Stump1eb44332009-09-09 15:08:12 +0000203///
Reid Spencer5f016e22007-07-11 17:01:13 +0000204static 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.
Mike Stump1eb44332009-09-09 15:08:12 +0000247///
Reid Spencer5f016e22007-07-11 17:01:13 +0000248static 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}