blob: 66d7ed7cf40a0679f8d43f7852ac83e94058619a [file] [log] [blame]
Chris Lattnera092b142007-06-27 17:24:55 +00001//===--- DiagChecker.cpp - Diagnostic Checking Functions ------------------===//
Bill Wendling469211a2007-06-27 03:19:45 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bill Wendling469211a2007-06-27 03:19:45 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Process the input files and check that the diagnostic messages are expected.
11//
12//===----------------------------------------------------------------------===//
13
Eli Friedman16b7b6f2009-05-19 04:14:29 +000014#include "clang/Frontend/Utils.h"
Daniel Dunbar51adf582009-03-02 06:16:29 +000015#include "clang/Frontend/TextDiagnosticBuffer.h"
John McCall8b0666c2010-08-20 18:27:03 +000016#include "clang/Parse/ParseAST.h"
Chris Lattner75e0c8c2007-09-15 22:56:56 +000017#include "clang/AST/ASTConsumer.h"
Bill Wendling469211a2007-06-27 03:19:45 +000018#include "clang/Basic/SourceManager.h"
19#include "clang/Lex/Preprocessor.h"
Daniel Dunbarf680e7d2009-12-03 09:14:02 +000020#include "llvm/Support/raw_ostream.h"
Bill Wendling469211a2007-06-27 03:19:45 +000021using namespace clang;
22
Bill Wendling52b0a4e2007-06-27 07:24:11 +000023typedef TextDiagnosticBuffer::DiagList DiagList;
Chris Lattner318a9d72007-06-28 05:17:33 +000024typedef TextDiagnosticBuffer::const_iterator const_diag_iterator;
Bill Wendling52b0a4e2007-06-27 07:24:11 +000025
Chris Lattnerf3ab0142008-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
Bill Wendling469211a2007-06-27 03:19:45 +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 Redl2175b6a2009-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//
Bill Wendling469211a2007-06-27 03:19:45 +000055
Bill Wendling469211a2007-06-27 03:19:45 +000056/// FindDiagnostics - Go through the comment and see if it indicates expected
57/// diagnostics. If so, then put them in a diagnostic list.
Mike Stump11289f42009-09-09 15:08:12 +000058///
Chris Lattner4fc69792008-11-24 01:28:17 +000059static void FindDiagnostics(const char *CommentStart, unsigned CommentLen,
Bill Wendling469211a2007-06-27 03:19:45 +000060 DiagList &ExpectedDiags,
Chris Lattner4fc69792008-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 Stump11289f42009-09-09 15:08:12 +000065
Chris Lattner4fc69792008-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 Stump11289f42009-09-09 15:08:12 +000071
Chris Lattner4fc69792008-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 Stump11289f42009-09-09 15:08:12 +000077
Chris Lattner4fc69792008-11-24 01:28:17 +000078 CommentStart += ExpectedStrLen;
Mike Stump11289f42009-09-09 15:08:12 +000079
Chris Lattner4fc69792008-11-24 01:28:17 +000080 // Skip whitespace.
81 while (CommentStart != CommentEnd &&
82 isspace(CommentStart[0]))
83 ++CommentStart;
Mike Stump11289f42009-09-09 15:08:12 +000084
Sebastian Redl2175b6a2009-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 Stump11289f42009-09-09 15:08:12 +000097
Sebastian Redl2175b6a2009-02-07 19:52:04 +000098 // Skip whitespace again.
99 while (CommentStart != CommentEnd &&
100 isspace(CommentStart[0]))
101 ++CommentStart;
Mike Stump11289f42009-09-09 15:08:12 +0000102
Chris Lattner4fc69792008-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 Lattnerf3ab0142008-11-23 23:38:26 +0000110 return;
Sebastian Redl2175b6a2009-02-07 19:52:04 +0000111 }
Chris Lattner4fc69792008-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 Stump11289f42009-09-09 15:08:12 +0000122
Chris Lattner4fc69792008-11-24 01:28:17 +0000123 if (ExpectedEnd[1] == '}')
124 break;
125
126 ++ExpectedEnd; // Skip over singular }'s
Bill Wendling469211a2007-06-27 03:19:45 +0000127 }
128
Chris Lattner4fc69792008-11-24 01:28:17 +0000129 std::string Msg(CommentStart, ExpectedEnd);
130 std::string::size_type FindPos;
Chris Lattnerf3ab0142008-11-23 23:38:26 +0000131 while ((FindPos = Msg.find("\\n")) != std::string::npos)
Sebastian Redl5cba81a2008-10-26 19:05:16 +0000132 Msg.replace(FindPos, 2, "\n");
Sebastian Redl2175b6a2009-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 Lattner4fc69792008-11-24 01:28:17 +0000137 CommentStart = ExpectedEnd;
Bill Wendling469211a2007-06-27 03:19:45 +0000138 }
139}
140
Ted Kremenek230bd912007-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,
Chris Lattner318a9d72007-06-28 05:17:33 +0000144 DiagList &ExpectedErrors,
Douglas Gregore972aa42008-09-11 02:46:36 +0000145 DiagList &ExpectedWarnings,
146 DiagList &ExpectedNotes) {
Chris Lattnerfd384b12008-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 Lattnerd32480d2009-01-17 06:22:33 +0000149 FileID FID = PP.getSourceManager().getMainFileID();
Mike Stump11289f42009-09-09 15:08:12 +0000150
Chris Lattnerfd384b12008-11-21 01:18:36 +0000151 // Create a lexer to lex all the tokens of the main file in raw mode.
Chris Lattner710bb872009-11-30 04:18:44 +0000152 const llvm::MemoryBuffer *FromFile = PP.getSourceManager().getBuffer(FID);
153 Lexer RawLex(FID, FromFile, PP.getSourceManager(), PP.getLangOptions());
Mike Stump11289f42009-09-09 15:08:12 +0000154
Chris Lattnerfd384b12008-11-21 01:18:36 +0000155 // Return comments as tokens, this is how we find expected diagnostics.
156 RawLex.SetCommentRetentionState(true);
157
Chris Lattner146762e2007-07-20 16:59:19 +0000158 Token Tok;
Chris Lattnerf3ab0142008-11-23 23:38:26 +0000159 Tok.setKind(tok::comment);
160 while (Tok.isNot(tok::eof)) {
Chris Lattnerfd384b12008-11-21 01:18:36 +0000161 RawLex.Lex(Tok);
Chris Lattnerf3ab0142008-11-23 23:38:26 +0000162 if (!Tok.is(tok::comment)) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000163
Chris Lattnerf3ab0142008-11-23 23:38:26 +0000164 std::string Comment = PP.getSpelling(Tok);
Chris Lattner4fc69792008-11-24 01:28:17 +0000165 if (Comment.empty()) continue;
Bill Wendling469211a2007-06-27 03:19:45 +0000166
Mike Stump11289f42009-09-09 15:08:12 +0000167
Chris Lattnerf3ab0142008-11-23 23:38:26 +0000168 // Find all expected errors.
Chris Lattner4fc69792008-11-24 01:28:17 +0000169 FindDiagnostics(&Comment[0], Comment.size(), ExpectedErrors, PP,
Chris Lattnerf3ab0142008-11-23 23:38:26 +0000170 Tok.getLocation(), "expected-error");
Bill Wendling469211a2007-06-27 03:19:45 +0000171
Chris Lattnerf3ab0142008-11-23 23:38:26 +0000172 // Find all expected warnings.
Chris Lattner4fc69792008-11-24 01:28:17 +0000173 FindDiagnostics(&Comment[0], Comment.size(), ExpectedWarnings, PP,
Chris Lattnerf3ab0142008-11-23 23:38:26 +0000174 Tok.getLocation(), "expected-warning");
Bill Wendling469211a2007-06-27 03:19:45 +0000175
Chris Lattnerf3ab0142008-11-23 23:38:26 +0000176 // Find all expected notes.
Chris Lattner4fc69792008-11-24 01:28:17 +0000177 FindDiagnostics(&Comment[0], Comment.size(), ExpectedNotes, PP,
Chris Lattnerf3ab0142008-11-23 23:38:26 +0000178 Tok.getLocation(), "expected-note");
179 };
Bill Wendling469211a2007-06-27 03:19:45 +0000180}
181
Bill Wendling469211a2007-06-27 03:19:45 +0000182/// 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 Stump11289f42009-09-09 15:08:12 +0000186///
Bill Wendling469211a2007-06-27 03:19:45 +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 Dunbarf680e7d2009-12-03 09:14:02 +0000193 llvm::errs() << Msg << "\n";
Bill Wendling97d41462007-06-27 04:06:59 +0000194 for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I)
Daniel Dunbarf680e7d2009-12-03 09:14:02 +0000195 llvm::errs() << " Line " << SourceMgr.getInstantiationLineNumber(I->first)
196 << " " << I->second << "\n";
Bill Wendling469211a2007-06-27 03:19:45 +0000197
198 return true;
199}
200
Chris Lattnere508c362009-02-04 02:15:34 +0000201/// CompareDiagLists - Compare two diagnostic lists and return the difference
Bill Wendling469211a2007-06-27 03:19:45 +0000202/// between them.
Mike Stump11289f42009-09-09 15:08:12 +0000203///
Bill Wendling469211a2007-06-27 03:19:45 +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 Redl2175b6a2009-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);
Bill Wendling469211a2007-06-27 03:19:45 +0000214
Sebastian Redl2175b6a2009-02-07 19:52:04 +0000215 for (const_diag_iterator I = Left.begin(), E = Left.end(); I != E; ++I) {
Chris Lattner8a425862009-01-16 07:36:28 +0000216 unsigned LineNo1 = SourceMgr.getInstantiationLineNumber(I->first);
Bill Wendling97d41462007-06-27 04:06:59 +0000217 const std::string &Diag1 = I->second;
Bill Wendling469211a2007-06-27 03:19:45 +0000218
Sebastian Redl2175b6a2009-02-07 19:52:04 +0000219 DiagList::iterator II, IE;
220 for (II = Right.begin(), IE = Right.end(); II != IE; ++II) {
Chris Lattner8a425862009-01-16 07:36:28 +0000221 unsigned LineNo2 = SourceMgr.getInstantiationLineNumber(II->first);
Bill Wendlinga5b3bb12007-06-27 07:43:27 +0000222 if (LineNo1 != LineNo2) continue;
Bill Wendling97d41462007-06-27 04:06:59 +0000223
Bill Wendlinga5b3bb12007-06-27 07:43:27 +0000224 const std::string &Diag2 = II->second;
Bill Wendling97d41462007-06-27 04:06:59 +0000225 if (Diag2.find(Diag1) != std::string::npos ||
226 Diag1.find(Diag2) != std::string::npos) {
Bill Wendling469211a2007-06-27 03:19:45 +0000227 break;
228 }
229 }
Sebastian Redl2175b6a2009-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 }
Bill Wendling469211a2007-06-27 03:19:45 +0000237 }
Sebastian Redl2175b6a2009-02-07 19:52:04 +0000238 // Now all that's left in Right are those that were not matched.
Bill Wendling469211a2007-06-27 03:19:45 +0000239
Sebastian Redl2175b6a2009-02-07 19:52:04 +0000240 return PrintProblem(SourceMgr, LeftOnly.begin(), LeftOnly.end(), MsgLeftOnly)
241 | PrintProblem(SourceMgr, Right.begin(), Right.end(), MsgRightOnly);
Bill Wendling469211a2007-06-27 03:19:45 +0000242}
243
Chris Lattner318a9d72007-06-28 05:17:33 +0000244/// CheckResults - This compares the expected results to those that
Bill Wendling469211a2007-06-27 03:19:45 +0000245/// were actually reported. It emits any discrepencies. Return "true" if there
246/// were problems. Return "false" otherwise.
Mike Stump11289f42009-09-09 15:08:12 +0000247///
Chris Lattner318a9d72007-06-28 05:17:33 +0000248static bool CheckResults(Preprocessor &PP,
249 const DiagList &ExpectedErrors,
Douglas Gregore972aa42008-09-11 02:46:36 +0000250 const DiagList &ExpectedWarnings,
251 const DiagList &ExpectedNotes) {
Nico Weber4c311642008-08-10 19:59:06 +0000252 const DiagnosticClient *DiagClient = PP.getDiagnostics().getClient();
253 assert(DiagClient != 0 &&
254 "DiagChecker requires a valid TextDiagnosticBuffer");
Chris Lattner318a9d72007-06-28 05:17:33 +0000255 const TextDiagnosticBuffer &Diags =
Nico Weber4c311642008-08-10 19:59:06 +0000256 static_cast<const TextDiagnosticBuffer&>(*DiagClient);
Chris Lattner318a9d72007-06-28 05:17:33 +0000257 SourceManager &SourceMgr = PP.getSourceManager();
258
Bill Wendling469211a2007-06-27 03:19:45 +0000259 // 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 Redl2175b6a2009-02-07 19:52:04 +0000266 // See if there are error mismatches.
Bill Wendling469211a2007-06-27 03:19:45 +0000267 HadProblem |= CompareDiagLists(SourceMgr,
268 ExpectedErrors.begin(), ExpectedErrors.end(),
Chris Lattner318a9d72007-06-28 05:17:33 +0000269 Diags.err_begin(), Diags.err_end(),
Sebastian Redl2175b6a2009-02-07 19:52:04 +0000270 "Errors expected but not seen:",
Bill Wendling469211a2007-06-27 03:19:45 +0000271 "Errors seen but not expected:");
272
Sebastian Redl2175b6a2009-02-07 19:52:04 +0000273 // See if there are warning mismatches.
Bill Wendling469211a2007-06-27 03:19:45 +0000274 HadProblem |= CompareDiagLists(SourceMgr,
275 ExpectedWarnings.begin(),
276 ExpectedWarnings.end(),
Chris Lattner318a9d72007-06-28 05:17:33 +0000277 Diags.warn_begin(), Diags.warn_end(),
Sebastian Redl2175b6a2009-02-07 19:52:04 +0000278 "Warnings expected but not seen:",
Bill Wendling469211a2007-06-27 03:19:45 +0000279 "Warnings seen but not expected:");
280
Sebastian Redl2175b6a2009-02-07 19:52:04 +0000281 // See if there are note mismatches.
Douglas Gregore972aa42008-09-11 02:46:36 +0000282 HadProblem |= CompareDiagLists(SourceMgr,
283 ExpectedNotes.begin(),
284 ExpectedNotes.end(),
285 Diags.note_begin(), Diags.note_end(),
Sebastian Redl2175b6a2009-02-07 19:52:04 +0000286 "Notes expected but not seen:",
Douglas Gregore972aa42008-09-11 02:46:36 +0000287 "Notes seen but not expected:");
288
Bill Wendling469211a2007-06-27 03:19:45 +0000289 return HadProblem;
290}
Chris Lattner23e63532007-06-28 04:54:17 +0000291
Chris Lattnerac9823b2007-08-10 18:27:41 +0000292
Argyrios Kyrtzidis351008d2008-06-13 12:15:34 +0000293/// CheckDiagnostics - Gather the expected diagnostics and check them.
294bool clang::CheckDiagnostics(Preprocessor &PP) {
Chris Lattner23e63532007-06-28 04:54:17 +0000295 // Gather the set of expected diagnostics.
Douglas Gregore972aa42008-09-11 02:46:36 +0000296 DiagList ExpectedErrors, ExpectedWarnings, ExpectedNotes;
297 FindExpectedDiags(PP, ExpectedErrors, ExpectedWarnings, ExpectedNotes);
Ted Kremenek558be362007-09-25 18:37:20 +0000298
Chris Lattner318a9d72007-06-28 05:17:33 +0000299 // Check that the expected diagnostics occurred.
Douglas Gregore972aa42008-09-11 02:46:36 +0000300 return CheckResults(PP, ExpectedErrors, ExpectedWarnings, ExpectedNotes);
Chris Lattner23e63532007-06-28 04:54:17 +0000301}