blob: 6f7ea0b739b31624251a3b5557af1ebef733da5a [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
26// USING THE DIAGNOSTIC CHECKER:
27//
28// Indicating that a line expects an error or a warning is simple. Put a comment
29// on the line that has the diagnostic, use "expected-{error,warning}" to tag
30// if it's an expected error or warning, and place the expected text between {{
31// and }} markers. The full text doesn't have to be included, only enough to
32// ensure that the correct diagnostic was emitted.
33//
34// Here's an example:
35//
36// int A = B; // expected-error {{use of undeclared identifier 'B'}}
37//
38// You can place as many diagnostics on one line as you wish. To make the code
39// more readable, you can use slash-newline to separate out the diagnostics.
40
41static const char * const ExpectedErrStr = "expected-error";
42static const char * const ExpectedWarnStr = "expected-warning";
Douglas Gregor233f74b2008-09-11 02:46:36 +000043static const char * const ExpectedNoteStr = "expected-note";
Reid Spencer5f016e22007-07-11 17:01:13 +000044
45/// FindDiagnostics - Go through the comment and see if it indicates expected
46/// diagnostics. If so, then put them in a diagnostic list.
47///
48static void FindDiagnostics(const std::string &Comment,
49 DiagList &ExpectedDiags,
50 SourceManager &SourceMgr,
51 SourceLocation Pos,
52 const char * const ExpectedStr) {
53 // Find all expected diagnostics
54 typedef std::string::size_type size_type;
Chris Lattnerdce7bd62007-09-16 19:27:16 +000055 size_type ColNo = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000056
57 for (;;) {
58 ColNo = Comment.find(ExpectedStr, ColNo);
59 if (ColNo == std::string::npos) break;
60
Bill Wendlingf5f20bd2007-11-26 08:26:20 +000061 size_type OpenDiag = Comment.find("{{", ColNo);
Reid Spencer5f016e22007-07-11 17:01:13 +000062
63 if (OpenDiag == std::string::npos) {
64 fprintf(stderr,
65 "oops:%d: Cannot find beginning of expected error string\n",
Chris Lattner9dc1f532007-07-20 16:37:10 +000066 SourceMgr.getLogicalLineNumber(Pos));
Reid Spencer5f016e22007-07-11 17:01:13 +000067 break;
68 }
69
70 OpenDiag += 2;
Bill Wendlingf5f20bd2007-11-26 08:26:20 +000071 size_type CloseDiag = Comment.find("}}", OpenDiag);
Reid Spencer5f016e22007-07-11 17:01:13 +000072
73 if (CloseDiag == std::string::npos) {
74 fprintf(stderr,
75 "oops:%d: Cannot find end of expected error string\n",
Chris Lattner9dc1f532007-07-20 16:37:10 +000076 SourceMgr.getLogicalLineNumber(Pos));
Reid Spencer5f016e22007-07-11 17:01:13 +000077 break;
78 }
79
80 std::string Msg(Comment.substr(OpenDiag, CloseDiag - OpenDiag));
81 ExpectedDiags.push_back(std::make_pair(Pos, Msg));
82 ColNo = CloseDiag + 2;
83 }
84}
85
Ted Kremenek95041a22007-12-19 22:51:13 +000086/// FindExpectedDiags - Lex the main source file to find all of the
87// expected errors and warnings.
88static void FindExpectedDiags(Preprocessor &PP,
Reid Spencer5f016e22007-07-11 17:01:13 +000089 DiagList &ExpectedErrors,
Douglas Gregor233f74b2008-09-11 02:46:36 +000090 DiagList &ExpectedWarnings,
91 DiagList &ExpectedNotes) {
Reid Spencer5f016e22007-07-11 17:01:13 +000092 // Return comments as tokens, this is how we find expected diagnostics.
93 PP.SetCommentRetentionState(true, true);
94
95 // Enter the cave.
Ted Kremenek95041a22007-12-19 22:51:13 +000096 PP.EnterMainSourceFile();
Reid Spencer5f016e22007-07-11 17:01:13 +000097
Chris Lattnerf01654f2007-08-30 06:34:23 +000098 // Turn off all warnings from relexing or preprocessing.
99 PP.getDiagnostics().setWarnOnExtensions(false);
100 PP.getDiagnostics().setErrorOnExtensions(false);
Chris Lattner07506182007-11-30 22:53:43 +0000101 for (unsigned i = 0; i != diag::NUM_BUILTIN_DIAGNOSTICS; ++i)
102 if (PP.getDiagnostics().isBuiltinNoteWarningOrExtension((diag::kind)i))
Chris Lattnerf01654f2007-08-30 06:34:23 +0000103 PP.getDiagnostics().setDiagnosticMapping((diag::kind)i, diag::MAP_IGNORE);
104
Chris Lattnerd2177732007-07-20 16:59:19 +0000105 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000106 do {
107 PP.Lex(Tok);
108
Chris Lattner057aaf62007-10-09 18:03:42 +0000109 if (Tok.is(tok::comment)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 std::string Comment = PP.getSpelling(Tok);
111
112 // Find all expected errors
113 FindDiagnostics(Comment, ExpectedErrors,PP.getSourceManager(),
114 Tok.getLocation(), ExpectedErrStr);
115
116 // Find all expected warnings
117 FindDiagnostics(Comment, ExpectedWarnings, PP.getSourceManager(),
118 Tok.getLocation(), ExpectedWarnStr);
Douglas Gregor233f74b2008-09-11 02:46:36 +0000119
120 // Find all expected notes
121 FindDiagnostics(Comment, ExpectedNotes, PP.getSourceManager(),
122 Tok.getLocation(), ExpectedNoteStr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 }
Chris Lattner057aaf62007-10-09 18:03:42 +0000124 } while (Tok.isNot(tok::eof));
Reid Spencer5f016e22007-07-11 17:01:13 +0000125
126 PP.SetCommentRetentionState(false, false);
127}
128
129/// PrintProblem - This takes a diagnostic map of the delta between expected and
130/// seen diagnostics. If there's anything in it, then something unexpected
131/// happened. Print the map out in a nice format and return "true". If the map
132/// is empty and we're not going to print things, then return "false".
133///
134static bool PrintProblem(SourceManager &SourceMgr,
135 const_diag_iterator diag_begin,
136 const_diag_iterator diag_end,
137 const char *Msg) {
138 if (diag_begin == diag_end) return false;
139
140 fprintf(stderr, "%s\n", Msg);
141
142 for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I)
143 fprintf(stderr, " Line %d: %s\n",
Chris Lattner9dc1f532007-07-20 16:37:10 +0000144 SourceMgr.getLogicalLineNumber(I->first),
Reid Spencer5f016e22007-07-11 17:01:13 +0000145 I->second.c_str());
146
147 return true;
148}
149
150/// CompareDiagLists - Compare two diangnostic lists and return the difference
151/// between them.
152///
153static bool CompareDiagLists(SourceManager &SourceMgr,
154 const_diag_iterator d1_begin,
155 const_diag_iterator d1_end,
156 const_diag_iterator d2_begin,
157 const_diag_iterator d2_end,
158 const char *Msg) {
159 DiagList DiffList;
160
161 for (const_diag_iterator I = d1_begin, E = d1_end; I != E; ++I) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000162 unsigned LineNo1 = SourceMgr.getLogicalLineNumber(I->first);
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 const std::string &Diag1 = I->second;
164 bool Found = false;
165
166 for (const_diag_iterator II = d2_begin, IE = d2_end; II != IE; ++II) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000167 unsigned LineNo2 = SourceMgr.getLogicalLineNumber(II->first);
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 if (LineNo1 != LineNo2) continue;
169
170 const std::string &Diag2 = II->second;
171 if (Diag2.find(Diag1) != std::string::npos ||
172 Diag1.find(Diag2) != std::string::npos) {
173 Found = true;
174 break;
175 }
176 }
177
178 if (!Found)
179 DiffList.push_back(std::make_pair(I->first, Diag1));
180 }
181
182 return PrintProblem(SourceMgr, DiffList.begin(), DiffList.end(), Msg);
183}
184
185/// CheckResults - This compares the expected results to those that
186/// were actually reported. It emits any discrepencies. Return "true" if there
187/// were problems. Return "false" otherwise.
188///
189static bool CheckResults(Preprocessor &PP,
190 const DiagList &ExpectedErrors,
Douglas Gregor233f74b2008-09-11 02:46:36 +0000191 const DiagList &ExpectedWarnings,
192 const DiagList &ExpectedNotes) {
Nico Weber7bfaaae2008-08-10 19:59:06 +0000193 const DiagnosticClient *DiagClient = PP.getDiagnostics().getClient();
194 assert(DiagClient != 0 &&
195 "DiagChecker requires a valid TextDiagnosticBuffer");
Reid Spencer5f016e22007-07-11 17:01:13 +0000196 const TextDiagnosticBuffer &Diags =
Nico Weber7bfaaae2008-08-10 19:59:06 +0000197 static_cast<const TextDiagnosticBuffer&>(*DiagClient);
Reid Spencer5f016e22007-07-11 17:01:13 +0000198 SourceManager &SourceMgr = PP.getSourceManager();
199
200 // We want to capture the delta between what was expected and what was
201 // seen.
202 //
203 // Expected \ Seen - set expected but not seen
204 // Seen \ Expected - set seen but not expected
205 bool HadProblem = false;
206
207 // See if there were errors that were expected but not seen.
208 HadProblem |= CompareDiagLists(SourceMgr,
209 ExpectedErrors.begin(), ExpectedErrors.end(),
210 Diags.err_begin(), Diags.err_end(),
211 "Errors expected but not seen:");
212
213 // See if there were errors that were seen but not expected.
214 HadProblem |= CompareDiagLists(SourceMgr,
215 Diags.err_begin(), Diags.err_end(),
216 ExpectedErrors.begin(), ExpectedErrors.end(),
217 "Errors seen but not expected:");
218
219 // See if there were warnings that were expected but not seen.
220 HadProblem |= CompareDiagLists(SourceMgr,
221 ExpectedWarnings.begin(),
222 ExpectedWarnings.end(),
223 Diags.warn_begin(), Diags.warn_end(),
224 "Warnings expected but not seen:");
225
226 // See if there were warnings that were seen but not expected.
227 HadProblem |= CompareDiagLists(SourceMgr,
228 Diags.warn_begin(), Diags.warn_end(),
229 ExpectedWarnings.begin(),
230 ExpectedWarnings.end(),
231 "Warnings seen but not expected:");
232
Douglas Gregor233f74b2008-09-11 02:46:36 +0000233 // See if there were notes that were expected but not seen.
234 HadProblem |= CompareDiagLists(SourceMgr,
235 ExpectedNotes.begin(),
236 ExpectedNotes.end(),
237 Diags.note_begin(), Diags.note_end(),
238 "Notes expected but not seen:");
239
240 // See if there were notes that were seen but not expected.
241 HadProblem |= CompareDiagLists(SourceMgr,
242 Diags.note_begin(), Diags.note_end(),
243 ExpectedNotes.begin(),
244 ExpectedNotes.end(),
245 "Notes seen but not expected:");
246
Reid Spencer5f016e22007-07-11 17:01:13 +0000247 return HadProblem;
248}
249
Chris Lattner009e9f72007-08-10 18:27:41 +0000250
Ted Kremenek44579782007-09-25 18:37:20 +0000251/// CheckASTConsumer - Implement diagnostic checking for AST consumers.
Ted Kremenek95041a22007-12-19 22:51:13 +0000252bool clang::CheckASTConsumer(Preprocessor &PP, ASTConsumer* C) {
253
Chris Lattner31e6c7d2007-11-03 06:24:16 +0000254 // Parse the AST and run the consumer, ultimately deleting C.
Ted Kremenek95041a22007-12-19 22:51:13 +0000255 ParseAST(PP, C);
Argyrios Kyrtzidis14d41402008-06-13 12:15:34 +0000256 return CheckDiagnostics(PP);
257}
258
259/// CheckDiagnostics - Gather the expected diagnostics and check them.
260bool clang::CheckDiagnostics(Preprocessor &PP) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000261 // Gather the set of expected diagnostics.
Douglas Gregor233f74b2008-09-11 02:46:36 +0000262 DiagList ExpectedErrors, ExpectedWarnings, ExpectedNotes;
263 FindExpectedDiags(PP, ExpectedErrors, ExpectedWarnings, ExpectedNotes);
Ted Kremenek44579782007-09-25 18:37:20 +0000264
Reid Spencer5f016e22007-07-11 17:01:13 +0000265 // Check that the expected diagnostics occurred.
Douglas Gregor233f74b2008-09-11 02:46:36 +0000266 return CheckResults(PP, ExpectedErrors, ExpectedWarnings, ExpectedNotes);
Reid Spencer5f016e22007-07-11 17:01:13 +0000267}