blob: 51f3979b92a437387acfc7d2dbf27dbc3e1d4588 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- DiagChecker.cpp - Diagnostic Checking Functions ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Process the input files and check that the diagnostic messages are expected.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang.h"
Chris Lattnereb8c9632007-10-07 06:04:32 +000015#include "ASTConsumers.h"
Nico Weber0e13eaa2008-08-05 23:33:20 +000016#include "clang/Driver/TextDiagnosticBuffer.h"
Chris Lattner0bed6ec2008-02-06 00:23:21 +000017#include "clang/Sema/ParseAST.h"
Chris Lattner1cc01712007-09-15 22:56:56 +000018#include "clang/AST/ASTConsumer.h"
Chris Lattner4b009652007-07-25 00:24:17 +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 Gregor6293fd12008-09-11 02:46:36 +000043static const char * const ExpectedNoteStr = "expected-note";
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner12ee82e2007-09-16 19:27:16 +000055 size_type ColNo = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000056
57 for (;;) {
58 ColNo = Comment.find(ExpectedStr, ColNo);
59 if (ColNo == std::string::npos) break;
60
Bill Wendlingc08e97b2007-11-26 08:26:20 +000061 size_type OpenDiag = Comment.find("{{", ColNo);
Chris Lattner4b009652007-07-25 00:24:17 +000062
63 if (OpenDiag == std::string::npos) {
64 fprintf(stderr,
65 "oops:%d: Cannot find beginning of expected error string\n",
66 SourceMgr.getLogicalLineNumber(Pos));
67 break;
68 }
69
70 OpenDiag += 2;
Bill Wendlingc08e97b2007-11-26 08:26:20 +000071 size_type CloseDiag = Comment.find("}}", OpenDiag);
Chris Lattner4b009652007-07-25 00:24:17 +000072
73 if (CloseDiag == std::string::npos) {
74 fprintf(stderr,
75 "oops:%d: Cannot find end of expected error string\n",
76 SourceMgr.getLogicalLineNumber(Pos));
77 break;
78 }
79
80 std::string Msg(Comment.substr(OpenDiag, CloseDiag - OpenDiag));
Sebastian Redl73e65a62008-10-26 19:05:16 +000081 size_type FindPos;
82 while((FindPos = Msg.find("\\n")) != std::string::npos) {
83 Msg.replace(FindPos, 2, "\n");
84 }
Chris Lattner4b009652007-07-25 00:24:17 +000085 ExpectedDiags.push_back(std::make_pair(Pos, Msg));
86 ColNo = CloseDiag + 2;
87 }
88}
89
Ted Kremenek17861c52007-12-19 22:51:13 +000090/// FindExpectedDiags - Lex the main source file to find all of the
91// expected errors and warnings.
92static void FindExpectedDiags(Preprocessor &PP,
Chris Lattner4b009652007-07-25 00:24:17 +000093 DiagList &ExpectedErrors,
Douglas Gregor6293fd12008-09-11 02:46:36 +000094 DiagList &ExpectedWarnings,
95 DiagList &ExpectedNotes) {
Chris Lattner4b009652007-07-25 00:24:17 +000096 // Return comments as tokens, this is how we find expected diagnostics.
97 PP.SetCommentRetentionState(true, true);
98
99 // Enter the cave.
Ted Kremenek17861c52007-12-19 22:51:13 +0000100 PP.EnterMainSourceFile();
Chris Lattner4b009652007-07-25 00:24:17 +0000101
Chris Lattner2db971b2007-08-30 06:34:23 +0000102 // Turn off all warnings from relexing or preprocessing.
103 PP.getDiagnostics().setWarnOnExtensions(false);
104 PP.getDiagnostics().setErrorOnExtensions(false);
Chris Lattner4478db92007-11-30 22:53:43 +0000105 for (unsigned i = 0; i != diag::NUM_BUILTIN_DIAGNOSTICS; ++i)
106 if (PP.getDiagnostics().isBuiltinNoteWarningOrExtension((diag::kind)i))
Chris Lattner2db971b2007-08-30 06:34:23 +0000107 PP.getDiagnostics().setDiagnosticMapping((diag::kind)i, diag::MAP_IGNORE);
108
Chris Lattner4b009652007-07-25 00:24:17 +0000109 Token Tok;
110 do {
111 PP.Lex(Tok);
112
Chris Lattner3b494152007-10-09 18:03:42 +0000113 if (Tok.is(tok::comment)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000114 std::string Comment = PP.getSpelling(Tok);
115
116 // Find all expected errors
117 FindDiagnostics(Comment, ExpectedErrors,PP.getSourceManager(),
118 Tok.getLocation(), ExpectedErrStr);
119
120 // Find all expected warnings
121 FindDiagnostics(Comment, ExpectedWarnings, PP.getSourceManager(),
122 Tok.getLocation(), ExpectedWarnStr);
Douglas Gregor6293fd12008-09-11 02:46:36 +0000123
124 // Find all expected notes
125 FindDiagnostics(Comment, ExpectedNotes, PP.getSourceManager(),
126 Tok.getLocation(), ExpectedNoteStr);
Chris Lattner4b009652007-07-25 00:24:17 +0000127 }
Chris Lattner3b494152007-10-09 18:03:42 +0000128 } while (Tok.isNot(tok::eof));
Chris Lattner4b009652007-07-25 00:24:17 +0000129
130 PP.SetCommentRetentionState(false, false);
131}
132
133/// PrintProblem - This takes a diagnostic map of the delta between expected and
134/// seen diagnostics. If there's anything in it, then something unexpected
135/// happened. Print the map out in a nice format and return "true". If the map
136/// is empty and we're not going to print things, then return "false".
137///
138static bool PrintProblem(SourceManager &SourceMgr,
139 const_diag_iterator diag_begin,
140 const_diag_iterator diag_end,
141 const char *Msg) {
142 if (diag_begin == diag_end) return false;
143
144 fprintf(stderr, "%s\n", Msg);
145
146 for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I)
147 fprintf(stderr, " Line %d: %s\n",
148 SourceMgr.getLogicalLineNumber(I->first),
149 I->second.c_str());
150
151 return true;
152}
153
154/// CompareDiagLists - Compare two diangnostic lists and return the difference
155/// between them.
156///
157static bool CompareDiagLists(SourceManager &SourceMgr,
158 const_diag_iterator d1_begin,
159 const_diag_iterator d1_end,
160 const_diag_iterator d2_begin,
161 const_diag_iterator d2_end,
162 const char *Msg) {
163 DiagList DiffList;
164
165 for (const_diag_iterator I = d1_begin, E = d1_end; I != E; ++I) {
166 unsigned LineNo1 = SourceMgr.getLogicalLineNumber(I->first);
167 const std::string &Diag1 = I->second;
168 bool Found = false;
169
170 for (const_diag_iterator II = d2_begin, IE = d2_end; II != IE; ++II) {
171 unsigned LineNo2 = SourceMgr.getLogicalLineNumber(II->first);
172 if (LineNo1 != LineNo2) continue;
173
174 const std::string &Diag2 = II->second;
175 if (Diag2.find(Diag1) != std::string::npos ||
176 Diag1.find(Diag2) != std::string::npos) {
177 Found = true;
178 break;
179 }
180 }
181
182 if (!Found)
183 DiffList.push_back(std::make_pair(I->first, Diag1));
184 }
185
186 return PrintProblem(SourceMgr, DiffList.begin(), DiffList.end(), Msg);
187}
188
189/// CheckResults - This compares the expected results to those that
190/// were actually reported. It emits any discrepencies. Return "true" if there
191/// were problems. Return "false" otherwise.
192///
193static bool CheckResults(Preprocessor &PP,
194 const DiagList &ExpectedErrors,
Douglas Gregor6293fd12008-09-11 02:46:36 +0000195 const DiagList &ExpectedWarnings,
196 const DiagList &ExpectedNotes) {
Nico Weberd2a6ac92008-08-10 19:59:06 +0000197 const DiagnosticClient *DiagClient = PP.getDiagnostics().getClient();
198 assert(DiagClient != 0 &&
199 "DiagChecker requires a valid TextDiagnosticBuffer");
Chris Lattner4b009652007-07-25 00:24:17 +0000200 const TextDiagnosticBuffer &Diags =
Nico Weberd2a6ac92008-08-10 19:59:06 +0000201 static_cast<const TextDiagnosticBuffer&>(*DiagClient);
Chris Lattner4b009652007-07-25 00:24:17 +0000202 SourceManager &SourceMgr = PP.getSourceManager();
203
204 // We want to capture the delta between what was expected and what was
205 // seen.
206 //
207 // Expected \ Seen - set expected but not seen
208 // Seen \ Expected - set seen but not expected
209 bool HadProblem = false;
210
211 // See if there were errors that were expected but not seen.
212 HadProblem |= CompareDiagLists(SourceMgr,
213 ExpectedErrors.begin(), ExpectedErrors.end(),
214 Diags.err_begin(), Diags.err_end(),
215 "Errors expected but not seen:");
216
217 // See if there were errors that were seen but not expected.
218 HadProblem |= CompareDiagLists(SourceMgr,
219 Diags.err_begin(), Diags.err_end(),
220 ExpectedErrors.begin(), ExpectedErrors.end(),
221 "Errors seen but not expected:");
222
223 // See if there were warnings that were expected but not seen.
224 HadProblem |= CompareDiagLists(SourceMgr,
225 ExpectedWarnings.begin(),
226 ExpectedWarnings.end(),
227 Diags.warn_begin(), Diags.warn_end(),
228 "Warnings expected but not seen:");
229
230 // See if there were warnings that were seen but not expected.
231 HadProblem |= CompareDiagLists(SourceMgr,
232 Diags.warn_begin(), Diags.warn_end(),
233 ExpectedWarnings.begin(),
234 ExpectedWarnings.end(),
235 "Warnings seen but not expected:");
236
Douglas Gregor6293fd12008-09-11 02:46:36 +0000237 // See if there were notes that were expected but not seen.
238 HadProblem |= CompareDiagLists(SourceMgr,
239 ExpectedNotes.begin(),
240 ExpectedNotes.end(),
241 Diags.note_begin(), Diags.note_end(),
242 "Notes expected but not seen:");
243
244 // See if there were notes that were seen but not expected.
245 HadProblem |= CompareDiagLists(SourceMgr,
246 Diags.note_begin(), Diags.note_end(),
247 ExpectedNotes.begin(),
248 ExpectedNotes.end(),
249 "Notes seen but not expected:");
250
Chris Lattner4b009652007-07-25 00:24:17 +0000251 return HadProblem;
252}
253
Chris Lattner4b6f4752007-08-10 18:27:41 +0000254
Ted Kremenek0841c702007-09-25 18:37:20 +0000255/// CheckASTConsumer - Implement diagnostic checking for AST consumers.
Ted Kremenek17861c52007-12-19 22:51:13 +0000256bool clang::CheckASTConsumer(Preprocessor &PP, ASTConsumer* C) {
257
Chris Lattner8593cbf2007-11-03 06:24:16 +0000258 // Parse the AST and run the consumer, ultimately deleting C.
Ted Kremenek17861c52007-12-19 22:51:13 +0000259 ParseAST(PP, C);
Argiris Kirtzidis8d833762008-06-13 12:15:34 +0000260 return CheckDiagnostics(PP);
261}
262
263/// CheckDiagnostics - Gather the expected diagnostics and check them.
264bool clang::CheckDiagnostics(Preprocessor &PP) {
Chris Lattner4b009652007-07-25 00:24:17 +0000265 // Gather the set of expected diagnostics.
Douglas Gregor6293fd12008-09-11 02:46:36 +0000266 DiagList ExpectedErrors, ExpectedWarnings, ExpectedNotes;
267 FindExpectedDiags(PP, ExpectedErrors, ExpectedWarnings, ExpectedNotes);
Ted Kremenek0841c702007-09-25 18:37:20 +0000268
Chris Lattner4b009652007-07-25 00:24:17 +0000269 // Check that the expected diagnostics occurred.
Douglas Gregor6293fd12008-09-11 02:46:36 +0000270 return CheckResults(PP, ExpectedErrors, ExpectedWarnings, ExpectedNotes);
Chris Lattner4b009652007-07-25 00:24:17 +0000271}