blob: eeba859b19a0dceaedc8d1a876521644a09ccffc [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
14#include "clang.h"
Chris Lattner97e8b6f2007-10-07 06:04:32 +000015#include "ASTConsumers.h"
Nico Weberfd54ebc2008-08-05 23:33:20 +000016#include "clang/Driver/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.
46
Reid Spencer5f016e22007-07-11 17:01:13 +000047/// FindDiagnostics - Go through the comment and see if it indicates expected
48/// diagnostics. If so, then put them in a diagnostic list.
49///
Chris Lattner0947b4e2008-11-24 01:28:17 +000050static void FindDiagnostics(const char *CommentStart, unsigned CommentLen,
Reid Spencer5f016e22007-07-11 17:01:13 +000051 DiagList &ExpectedDiags,
Chris Lattner0947b4e2008-11-24 01:28:17 +000052 Preprocessor &PP, SourceLocation Pos,
53 const char *ExpectedStr) {
54 const char *CommentEnd = CommentStart+CommentLen;
55 unsigned ExpectedStrLen = strlen(ExpectedStr);
Chris Lattnerb2c8c552008-11-23 23:38:26 +000056
Chris Lattner0947b4e2008-11-24 01:28:17 +000057 // Find all expected-foo diagnostics in the string and add them to
58 // ExpectedDiags.
59 while (CommentStart != CommentEnd) {
60 CommentStart = std::find(CommentStart, CommentEnd, 'e');
61 if (unsigned(CommentEnd-CommentStart) < ExpectedStrLen) return;
62
63 // If this isn't expected-foo, ignore it.
64 if (memcmp(CommentStart, ExpectedStr, ExpectedStrLen)) {
65 ++CommentStart;
66 continue;
67 }
68
69 CommentStart += ExpectedStrLen;
70
71 // Skip whitespace.
72 while (CommentStart != CommentEnd &&
73 isspace(CommentStart[0]))
74 ++CommentStart;
75
76 // We should have a {{ now.
77 if (CommentEnd-CommentStart < 2 ||
78 CommentStart[0] != '{' || CommentStart[1] != '{') {
79 if (std::find(CommentStart, CommentEnd, '{') != CommentEnd)
80 EmitError(PP, Pos, "bogus characters before '{{' in expected string");
81 else
82 EmitError(PP, Pos, "cannot find start ('{{') of expected string");
Chris Lattnerb2c8c552008-11-23 23:38:26 +000083 return;
Chris Lattner0947b4e2008-11-24 01:28:17 +000084 }
85 CommentStart += 2;
86
87 // Find the }}.
88 const char *ExpectedEnd = CommentStart;
89 while (1) {
90 ExpectedEnd = std::find(ExpectedEnd, CommentEnd, '}');
91 if (CommentEnd-ExpectedEnd < 2) {
92 EmitError(PP, Pos, "cannot find end ('}}') of expected string");
93 return;
94 }
95
96 if (ExpectedEnd[1] == '}')
97 break;
98
99 ++ExpectedEnd; // Skip over singular }'s
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 }
101
Chris Lattner0947b4e2008-11-24 01:28:17 +0000102 std::string Msg(CommentStart, ExpectedEnd);
103 std::string::size_type FindPos;
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000104 while ((FindPos = Msg.find("\\n")) != std::string::npos)
Sebastian Redlad3c91c2008-10-26 19:05:16 +0000105 Msg.replace(FindPos, 2, "\n");
Reid Spencer5f016e22007-07-11 17:01:13 +0000106 ExpectedDiags.push_back(std::make_pair(Pos, Msg));
Chris Lattner0947b4e2008-11-24 01:28:17 +0000107
108 CommentStart = ExpectedEnd;
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 }
110}
111
Ted Kremenek95041a22007-12-19 22:51:13 +0000112/// FindExpectedDiags - Lex the main source file to find all of the
113// expected errors and warnings.
114static void FindExpectedDiags(Preprocessor &PP,
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 DiagList &ExpectedErrors,
Douglas Gregor233f74b2008-09-11 02:46:36 +0000116 DiagList &ExpectedWarnings,
117 DiagList &ExpectedNotes) {
Chris Lattnera39f0482008-11-21 01:18:36 +0000118 // Create a raw lexer to pull all the comments out of the main file. We don't
119 // want to look in #include'd headers for expected-error strings.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000120 FileID FID = PP.getSourceManager().getMainFileID();
Chris Lattnera39f0482008-11-21 01:18:36 +0000121
122 // Create a lexer to lex all the tokens of the main file in raw mode.
Chris Lattner4448a012009-01-17 07:41:36 +0000123 Lexer RawLex(FID, PP.getSourceManager(), PP.getLangOptions());
Chris Lattnera39f0482008-11-21 01:18:36 +0000124
125 // Return comments as tokens, this is how we find expected diagnostics.
126 RawLex.SetCommentRetentionState(true);
127
Chris Lattnerd2177732007-07-20 16:59:19 +0000128 Token Tok;
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000129 Tok.setKind(tok::comment);
130 while (Tok.isNot(tok::eof)) {
Chris Lattnera39f0482008-11-21 01:18:36 +0000131 RawLex.Lex(Tok);
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000132 if (!Tok.is(tok::comment)) continue;
133
134 std::string Comment = PP.getSpelling(Tok);
Chris Lattner0947b4e2008-11-24 01:28:17 +0000135 if (Comment.empty()) continue;
Reid Spencer5f016e22007-07-11 17:01:13 +0000136
Chris Lattner0947b4e2008-11-24 01:28:17 +0000137
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000138 // Find all expected errors.
Chris Lattner0947b4e2008-11-24 01:28:17 +0000139 FindDiagnostics(&Comment[0], Comment.size(), ExpectedErrors, PP,
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000140 Tok.getLocation(), "expected-error");
Reid Spencer5f016e22007-07-11 17:01:13 +0000141
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000142 // Find all expected warnings.
Chris Lattner0947b4e2008-11-24 01:28:17 +0000143 FindDiagnostics(&Comment[0], Comment.size(), ExpectedWarnings, PP,
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000144 Tok.getLocation(), "expected-warning");
Reid Spencer5f016e22007-07-11 17:01:13 +0000145
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000146 // Find all expected notes.
Chris Lattner0947b4e2008-11-24 01:28:17 +0000147 FindDiagnostics(&Comment[0], Comment.size(), ExpectedNotes, PP,
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000148 Tok.getLocation(), "expected-note");
149 };
Reid Spencer5f016e22007-07-11 17:01:13 +0000150}
151
152/// PrintProblem - This takes a diagnostic map of the delta between expected and
153/// seen diagnostics. If there's anything in it, then something unexpected
154/// happened. Print the map out in a nice format and return "true". If the map
155/// is empty and we're not going to print things, then return "false".
156///
157static bool PrintProblem(SourceManager &SourceMgr,
158 const_diag_iterator diag_begin,
159 const_diag_iterator diag_end,
160 const char *Msg) {
161 if (diag_begin == diag_end) return false;
162
163 fprintf(stderr, "%s\n", Msg);
164
165 for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I)
166 fprintf(stderr, " Line %d: %s\n",
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000167 SourceMgr.getInstantiationLineNumber(I->first),
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 I->second.c_str());
169
170 return true;
171}
172
173/// CompareDiagLists - Compare two diangnostic lists and return the difference
174/// between them.
175///
176static bool CompareDiagLists(SourceManager &SourceMgr,
177 const_diag_iterator d1_begin,
178 const_diag_iterator d1_end,
179 const_diag_iterator d2_begin,
180 const_diag_iterator d2_end,
181 const char *Msg) {
182 DiagList DiffList;
183
184 for (const_diag_iterator I = d1_begin, E = d1_end; I != E; ++I) {
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000185 unsigned LineNo1 = SourceMgr.getInstantiationLineNumber(I->first);
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 const std::string &Diag1 = I->second;
187 bool Found = false;
188
189 for (const_diag_iterator II = d2_begin, IE = d2_end; II != IE; ++II) {
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000190 unsigned LineNo2 = SourceMgr.getInstantiationLineNumber(II->first);
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 if (LineNo1 != LineNo2) continue;
192
193 const std::string &Diag2 = II->second;
194 if (Diag2.find(Diag1) != std::string::npos ||
195 Diag1.find(Diag2) != std::string::npos) {
196 Found = true;
197 break;
198 }
199 }
200
201 if (!Found)
202 DiffList.push_back(std::make_pair(I->first, Diag1));
203 }
204
205 return PrintProblem(SourceMgr, DiffList.begin(), DiffList.end(), Msg);
206}
207
208/// CheckResults - This compares the expected results to those that
209/// were actually reported. It emits any discrepencies. Return "true" if there
210/// were problems. Return "false" otherwise.
211///
212static bool CheckResults(Preprocessor &PP,
213 const DiagList &ExpectedErrors,
Douglas Gregor233f74b2008-09-11 02:46:36 +0000214 const DiagList &ExpectedWarnings,
215 const DiagList &ExpectedNotes) {
Nico Weber7bfaaae2008-08-10 19:59:06 +0000216 const DiagnosticClient *DiagClient = PP.getDiagnostics().getClient();
217 assert(DiagClient != 0 &&
218 "DiagChecker requires a valid TextDiagnosticBuffer");
Reid Spencer5f016e22007-07-11 17:01:13 +0000219 const TextDiagnosticBuffer &Diags =
Nico Weber7bfaaae2008-08-10 19:59:06 +0000220 static_cast<const TextDiagnosticBuffer&>(*DiagClient);
Reid Spencer5f016e22007-07-11 17:01:13 +0000221 SourceManager &SourceMgr = PP.getSourceManager();
222
223 // We want to capture the delta between what was expected and what was
224 // seen.
225 //
226 // Expected \ Seen - set expected but not seen
227 // Seen \ Expected - set seen but not expected
228 bool HadProblem = false;
229
230 // See if there were errors that were expected but not seen.
231 HadProblem |= CompareDiagLists(SourceMgr,
232 ExpectedErrors.begin(), ExpectedErrors.end(),
233 Diags.err_begin(), Diags.err_end(),
234 "Errors expected but not seen:");
235
236 // See if there were errors that were seen but not expected.
237 HadProblem |= CompareDiagLists(SourceMgr,
238 Diags.err_begin(), Diags.err_end(),
239 ExpectedErrors.begin(), ExpectedErrors.end(),
240 "Errors seen but not expected:");
241
242 // See if there were warnings that were expected but not seen.
243 HadProblem |= CompareDiagLists(SourceMgr,
244 ExpectedWarnings.begin(),
245 ExpectedWarnings.end(),
246 Diags.warn_begin(), Diags.warn_end(),
247 "Warnings expected but not seen:");
248
249 // See if there were warnings that were seen but not expected.
250 HadProblem |= CompareDiagLists(SourceMgr,
251 Diags.warn_begin(), Diags.warn_end(),
252 ExpectedWarnings.begin(),
253 ExpectedWarnings.end(),
254 "Warnings seen but not expected:");
255
Douglas Gregor233f74b2008-09-11 02:46:36 +0000256 // See if there were notes that were expected but not seen.
257 HadProblem |= CompareDiagLists(SourceMgr,
258 ExpectedNotes.begin(),
259 ExpectedNotes.end(),
260 Diags.note_begin(), Diags.note_end(),
261 "Notes expected but not seen:");
262
263 // See if there were notes that were seen but not expected.
264 HadProblem |= CompareDiagLists(SourceMgr,
265 Diags.note_begin(), Diags.note_end(),
266 ExpectedNotes.begin(),
267 ExpectedNotes.end(),
268 "Notes seen but not expected:");
269
Reid Spencer5f016e22007-07-11 17:01:13 +0000270 return HadProblem;
271}
272
Chris Lattner009e9f72007-08-10 18:27:41 +0000273
Argyrios Kyrtzidis14d41402008-06-13 12:15:34 +0000274/// CheckDiagnostics - Gather the expected diagnostics and check them.
275bool clang::CheckDiagnostics(Preprocessor &PP) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000276 // Gather the set of expected diagnostics.
Douglas Gregor233f74b2008-09-11 02:46:36 +0000277 DiagList ExpectedErrors, ExpectedWarnings, ExpectedNotes;
278 FindExpectedDiags(PP, ExpectedErrors, ExpectedWarnings, ExpectedNotes);
Ted Kremenek44579782007-09-25 18:37:20 +0000279
Reid Spencer5f016e22007-07-11 17:01:13 +0000280 // Check that the expected diagnostics occurred.
Douglas Gregor233f74b2008-09-11 02:46:36 +0000281 return CheckResults(PP, ExpectedErrors, ExpectedWarnings, ExpectedNotes);
Reid Spencer5f016e22007-07-11 17:01:13 +0000282}