blob: 6f4cd7c509d99ae6e8dbb6d61e66941bfa7fe446 [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 Lattnera39f0482008-11-21 01:18:36 +0000120 unsigned FileID = PP.getSourceManager().getMainFileID();
121 std::pair<const char*,const char*> File =
122 PP.getSourceManager().getBufferData(FileID);
123
124 // Create a lexer to lex all the tokens of the main file in raw mode.
125 Lexer RawLex(SourceLocation::getFileLoc(FileID, 0),
126 PP.getLangOptions(), File.first, File.second);
127
128 // Return comments as tokens, this is how we find expected diagnostics.
129 RawLex.SetCommentRetentionState(true);
130
Chris Lattnerd2177732007-07-20 16:59:19 +0000131 Token Tok;
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000132 Tok.setKind(tok::comment);
133 while (Tok.isNot(tok::eof)) {
Chris Lattnera39f0482008-11-21 01:18:36 +0000134 RawLex.Lex(Tok);
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000135 if (!Tok.is(tok::comment)) continue;
136
137 std::string Comment = PP.getSpelling(Tok);
Chris Lattner0947b4e2008-11-24 01:28:17 +0000138 if (Comment.empty()) continue;
Reid Spencer5f016e22007-07-11 17:01:13 +0000139
Chris Lattner0947b4e2008-11-24 01:28:17 +0000140
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000141 // Find all expected errors.
Chris Lattner0947b4e2008-11-24 01:28:17 +0000142 FindDiagnostics(&Comment[0], Comment.size(), ExpectedErrors, PP,
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000143 Tok.getLocation(), "expected-error");
Reid Spencer5f016e22007-07-11 17:01:13 +0000144
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000145 // Find all expected warnings.
Chris Lattner0947b4e2008-11-24 01:28:17 +0000146 FindDiagnostics(&Comment[0], Comment.size(), ExpectedWarnings, PP,
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000147 Tok.getLocation(), "expected-warning");
Reid Spencer5f016e22007-07-11 17:01:13 +0000148
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000149 // Find all expected notes.
Chris Lattner0947b4e2008-11-24 01:28:17 +0000150 FindDiagnostics(&Comment[0], Comment.size(), ExpectedNotes, PP,
Chris Lattnerb2c8c552008-11-23 23:38:26 +0000151 Tok.getLocation(), "expected-note");
152 };
Reid Spencer5f016e22007-07-11 17:01:13 +0000153}
154
155/// PrintProblem - This takes a diagnostic map of the delta between expected and
156/// seen diagnostics. If there's anything in it, then something unexpected
157/// happened. Print the map out in a nice format and return "true". If the map
158/// is empty and we're not going to print things, then return "false".
159///
160static bool PrintProblem(SourceManager &SourceMgr,
161 const_diag_iterator diag_begin,
162 const_diag_iterator diag_end,
163 const char *Msg) {
164 if (diag_begin == diag_end) return false;
165
166 fprintf(stderr, "%s\n", Msg);
167
168 for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I)
169 fprintf(stderr, " Line %d: %s\n",
Chris Lattner9dc1f532007-07-20 16:37:10 +0000170 SourceMgr.getLogicalLineNumber(I->first),
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 I->second.c_str());
172
173 return true;
174}
175
176/// CompareDiagLists - Compare two diangnostic lists and return the difference
177/// between them.
178///
179static bool CompareDiagLists(SourceManager &SourceMgr,
180 const_diag_iterator d1_begin,
181 const_diag_iterator d1_end,
182 const_diag_iterator d2_begin,
183 const_diag_iterator d2_end,
184 const char *Msg) {
185 DiagList DiffList;
186
187 for (const_diag_iterator I = d1_begin, E = d1_end; I != E; ++I) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000188 unsigned LineNo1 = SourceMgr.getLogicalLineNumber(I->first);
Reid Spencer5f016e22007-07-11 17:01:13 +0000189 const std::string &Diag1 = I->second;
190 bool Found = false;
191
192 for (const_diag_iterator II = d2_begin, IE = d2_end; II != IE; ++II) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000193 unsigned LineNo2 = SourceMgr.getLogicalLineNumber(II->first);
Reid Spencer5f016e22007-07-11 17:01:13 +0000194 if (LineNo1 != LineNo2) continue;
195
196 const std::string &Diag2 = II->second;
197 if (Diag2.find(Diag1) != std::string::npos ||
198 Diag1.find(Diag2) != std::string::npos) {
199 Found = true;
200 break;
201 }
202 }
203
204 if (!Found)
205 DiffList.push_back(std::make_pair(I->first, Diag1));
206 }
207
208 return PrintProblem(SourceMgr, DiffList.begin(), DiffList.end(), Msg);
209}
210
211/// CheckResults - This compares the expected results to those that
212/// were actually reported. It emits any discrepencies. Return "true" if there
213/// were problems. Return "false" otherwise.
214///
215static bool CheckResults(Preprocessor &PP,
216 const DiagList &ExpectedErrors,
Douglas Gregor233f74b2008-09-11 02:46:36 +0000217 const DiagList &ExpectedWarnings,
218 const DiagList &ExpectedNotes) {
Nico Weber7bfaaae2008-08-10 19:59:06 +0000219 const DiagnosticClient *DiagClient = PP.getDiagnostics().getClient();
220 assert(DiagClient != 0 &&
221 "DiagChecker requires a valid TextDiagnosticBuffer");
Reid Spencer5f016e22007-07-11 17:01:13 +0000222 const TextDiagnosticBuffer &Diags =
Nico Weber7bfaaae2008-08-10 19:59:06 +0000223 static_cast<const TextDiagnosticBuffer&>(*DiagClient);
Reid Spencer5f016e22007-07-11 17:01:13 +0000224 SourceManager &SourceMgr = PP.getSourceManager();
225
226 // We want to capture the delta between what was expected and what was
227 // seen.
228 //
229 // Expected \ Seen - set expected but not seen
230 // Seen \ Expected - set seen but not expected
231 bool HadProblem = false;
232
233 // See if there were errors that were expected but not seen.
234 HadProblem |= CompareDiagLists(SourceMgr,
235 ExpectedErrors.begin(), ExpectedErrors.end(),
236 Diags.err_begin(), Diags.err_end(),
237 "Errors expected but not seen:");
238
239 // See if there were errors that were seen but not expected.
240 HadProblem |= CompareDiagLists(SourceMgr,
241 Diags.err_begin(), Diags.err_end(),
242 ExpectedErrors.begin(), ExpectedErrors.end(),
243 "Errors seen but not expected:");
244
245 // See if there were warnings that were expected but not seen.
246 HadProblem |= CompareDiagLists(SourceMgr,
247 ExpectedWarnings.begin(),
248 ExpectedWarnings.end(),
249 Diags.warn_begin(), Diags.warn_end(),
250 "Warnings expected but not seen:");
251
252 // See if there were warnings that were seen but not expected.
253 HadProblem |= CompareDiagLists(SourceMgr,
254 Diags.warn_begin(), Diags.warn_end(),
255 ExpectedWarnings.begin(),
256 ExpectedWarnings.end(),
257 "Warnings seen but not expected:");
258
Douglas Gregor233f74b2008-09-11 02:46:36 +0000259 // See if there were notes that were expected but not seen.
260 HadProblem |= CompareDiagLists(SourceMgr,
261 ExpectedNotes.begin(),
262 ExpectedNotes.end(),
263 Diags.note_begin(), Diags.note_end(),
264 "Notes expected but not seen:");
265
266 // See if there were notes that were seen but not expected.
267 HadProblem |= CompareDiagLists(SourceMgr,
268 Diags.note_begin(), Diags.note_end(),
269 ExpectedNotes.begin(),
270 ExpectedNotes.end(),
271 "Notes seen but not expected:");
272
Reid Spencer5f016e22007-07-11 17:01:13 +0000273 return HadProblem;
274}
275
Chris Lattner009e9f72007-08-10 18:27:41 +0000276
Argyrios Kyrtzidis14d41402008-06-13 12:15:34 +0000277/// CheckDiagnostics - Gather the expected diagnostics and check them.
278bool clang::CheckDiagnostics(Preprocessor &PP) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000279 // Gather the set of expected diagnostics.
Douglas Gregor233f74b2008-09-11 02:46:36 +0000280 DiagList ExpectedErrors, ExpectedWarnings, ExpectedNotes;
281 FindExpectedDiags(PP, ExpectedErrors, ExpectedWarnings, ExpectedNotes);
Ted Kremenek44579782007-09-25 18:37:20 +0000282
Reid Spencer5f016e22007-07-11 17:01:13 +0000283 // Check that the expected diagnostics occurred.
Douglas Gregor233f74b2008-09-11 02:46:36 +0000284 return CheckResults(PP, ExpectedErrors, ExpectedWarnings, ExpectedNotes);
Reid Spencer5f016e22007-07-11 17:01:13 +0000285}