blob: 4c0aa4c6e99daaddbd5a4ed292af3f43c6cd210c [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
Chris Lattnerc0332b92008-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
Chris Lattner4b009652007-07-25 00:24:17 +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
Chris Lattner4b009652007-07-25 00:24:17 +000047/// FindDiagnostics - Go through the comment and see if it indicates expected
48/// diagnostics. If so, then put them in a diagnostic list.
49///
50static void FindDiagnostics(const std::string &Comment,
51 DiagList &ExpectedDiags,
Chris Lattnerc0332b92008-11-23 23:38:26 +000052 Preprocessor &PP,
Chris Lattner4b009652007-07-25 00:24:17 +000053 SourceLocation Pos,
54 const char * const ExpectedStr) {
Chris Lattnerc0332b92008-11-23 23:38:26 +000055 SourceManager &SourceMgr = PP.getSourceManager();
56
57 // Find all expected diagnostics.
Chris Lattner4b009652007-07-25 00:24:17 +000058 typedef std::string::size_type size_type;
Chris Lattner12ee82e2007-09-16 19:27:16 +000059 size_type ColNo = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000060
61 for (;;) {
62 ColNo = Comment.find(ExpectedStr, ColNo);
63 if (ColNo == std::string::npos) break;
64
Bill Wendlingc08e97b2007-11-26 08:26:20 +000065 size_type OpenDiag = Comment.find("{{", ColNo);
Chris Lattner4b009652007-07-25 00:24:17 +000066
67 if (OpenDiag == std::string::npos) {
Chris Lattnerc0332b92008-11-23 23:38:26 +000068 EmitError(PP, Pos,
69 "cannot find start ('{{') of expected diagnostic string");
70 return;
Chris Lattner4b009652007-07-25 00:24:17 +000071 }
72
73 OpenDiag += 2;
Bill Wendlingc08e97b2007-11-26 08:26:20 +000074 size_type CloseDiag = Comment.find("}}", OpenDiag);
Chris Lattner4b009652007-07-25 00:24:17 +000075
76 if (CloseDiag == std::string::npos) {
Chris Lattnerc0332b92008-11-23 23:38:26 +000077 EmitError(PP, Pos,"cannot find end ('}}') of expected diagnostic string");
78 return;
Chris Lattner4b009652007-07-25 00:24:17 +000079 }
80
81 std::string Msg(Comment.substr(OpenDiag, CloseDiag - OpenDiag));
Sebastian Redl73e65a62008-10-26 19:05:16 +000082 size_type FindPos;
Chris Lattnerc0332b92008-11-23 23:38:26 +000083 while ((FindPos = Msg.find("\\n")) != std::string::npos)
Sebastian Redl73e65a62008-10-26 19:05:16 +000084 Msg.replace(FindPos, 2, "\n");
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 Lattner733ea7e2008-11-21 01:18:36 +000096 // Create a raw lexer to pull all the comments out of the main file. We don't
97 // want to look in #include'd headers for expected-error strings.
Chris Lattner733ea7e2008-11-21 01:18:36 +000098 unsigned FileID = PP.getSourceManager().getMainFileID();
99 std::pair<const char*,const char*> File =
100 PP.getSourceManager().getBufferData(FileID);
101
102 // Create a lexer to lex all the tokens of the main file in raw mode.
103 Lexer RawLex(SourceLocation::getFileLoc(FileID, 0),
104 PP.getLangOptions(), File.first, File.second);
105
106 // Return comments as tokens, this is how we find expected diagnostics.
107 RawLex.SetCommentRetentionState(true);
108
Chris Lattner4b009652007-07-25 00:24:17 +0000109 Token Tok;
Chris Lattnerc0332b92008-11-23 23:38:26 +0000110 Tok.setKind(tok::comment);
111 while (Tok.isNot(tok::eof)) {
Chris Lattner733ea7e2008-11-21 01:18:36 +0000112 RawLex.Lex(Tok);
Chris Lattnerc0332b92008-11-23 23:38:26 +0000113 if (!Tok.is(tok::comment)) continue;
114
115 std::string Comment = PP.getSpelling(Tok);
Chris Lattner4b009652007-07-25 00:24:17 +0000116
Chris Lattnerc0332b92008-11-23 23:38:26 +0000117 // Find all expected errors.
118 FindDiagnostics(Comment, ExpectedErrors, PP,
119 Tok.getLocation(), "expected-error");
Chris Lattner4b009652007-07-25 00:24:17 +0000120
Chris Lattnerc0332b92008-11-23 23:38:26 +0000121 // Find all expected warnings.
122 FindDiagnostics(Comment, ExpectedWarnings, PP,
123 Tok.getLocation(), "expected-warning");
Chris Lattner4b009652007-07-25 00:24:17 +0000124
Chris Lattnerc0332b92008-11-23 23:38:26 +0000125 // Find all expected notes.
126 FindDiagnostics(Comment, ExpectedNotes, PP,
127 Tok.getLocation(), "expected-note");
128 };
Chris Lattner4b009652007-07-25 00:24:17 +0000129}
130
131/// PrintProblem - This takes a diagnostic map of the delta between expected and
132/// seen diagnostics. If there's anything in it, then something unexpected
133/// happened. Print the map out in a nice format and return "true". If the map
134/// is empty and we're not going to print things, then return "false".
135///
136static bool PrintProblem(SourceManager &SourceMgr,
137 const_diag_iterator diag_begin,
138 const_diag_iterator diag_end,
139 const char *Msg) {
140 if (diag_begin == diag_end) return false;
141
142 fprintf(stderr, "%s\n", Msg);
143
144 for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I)
145 fprintf(stderr, " Line %d: %s\n",
146 SourceMgr.getLogicalLineNumber(I->first),
147 I->second.c_str());
148
149 return true;
150}
151
152/// CompareDiagLists - Compare two diangnostic lists and return the difference
153/// between them.
154///
155static bool CompareDiagLists(SourceManager &SourceMgr,
156 const_diag_iterator d1_begin,
157 const_diag_iterator d1_end,
158 const_diag_iterator d2_begin,
159 const_diag_iterator d2_end,
160 const char *Msg) {
161 DiagList DiffList;
162
163 for (const_diag_iterator I = d1_begin, E = d1_end; I != E; ++I) {
164 unsigned LineNo1 = SourceMgr.getLogicalLineNumber(I->first);
165 const std::string &Diag1 = I->second;
166 bool Found = false;
167
168 for (const_diag_iterator II = d2_begin, IE = d2_end; II != IE; ++II) {
169 unsigned LineNo2 = SourceMgr.getLogicalLineNumber(II->first);
170 if (LineNo1 != LineNo2) continue;
171
172 const std::string &Diag2 = II->second;
173 if (Diag2.find(Diag1) != std::string::npos ||
174 Diag1.find(Diag2) != std::string::npos) {
175 Found = true;
176 break;
177 }
178 }
179
180 if (!Found)
181 DiffList.push_back(std::make_pair(I->first, Diag1));
182 }
183
184 return PrintProblem(SourceMgr, DiffList.begin(), DiffList.end(), Msg);
185}
186
187/// CheckResults - This compares the expected results to those that
188/// were actually reported. It emits any discrepencies. Return "true" if there
189/// were problems. Return "false" otherwise.
190///
191static bool CheckResults(Preprocessor &PP,
192 const DiagList &ExpectedErrors,
Douglas Gregor6293fd12008-09-11 02:46:36 +0000193 const DiagList &ExpectedWarnings,
194 const DiagList &ExpectedNotes) {
Nico Weberd2a6ac92008-08-10 19:59:06 +0000195 const DiagnosticClient *DiagClient = PP.getDiagnostics().getClient();
196 assert(DiagClient != 0 &&
197 "DiagChecker requires a valid TextDiagnosticBuffer");
Chris Lattner4b009652007-07-25 00:24:17 +0000198 const TextDiagnosticBuffer &Diags =
Nico Weberd2a6ac92008-08-10 19:59:06 +0000199 static_cast<const TextDiagnosticBuffer&>(*DiagClient);
Chris Lattner4b009652007-07-25 00:24:17 +0000200 SourceManager &SourceMgr = PP.getSourceManager();
201
202 // We want to capture the delta between what was expected and what was
203 // seen.
204 //
205 // Expected \ Seen - set expected but not seen
206 // Seen \ Expected - set seen but not expected
207 bool HadProblem = false;
208
209 // See if there were errors that were expected but not seen.
210 HadProblem |= CompareDiagLists(SourceMgr,
211 ExpectedErrors.begin(), ExpectedErrors.end(),
212 Diags.err_begin(), Diags.err_end(),
213 "Errors expected but not seen:");
214
215 // See if there were errors that were seen but not expected.
216 HadProblem |= CompareDiagLists(SourceMgr,
217 Diags.err_begin(), Diags.err_end(),
218 ExpectedErrors.begin(), ExpectedErrors.end(),
219 "Errors seen but not expected:");
220
221 // See if there were warnings that were expected but not seen.
222 HadProblem |= CompareDiagLists(SourceMgr,
223 ExpectedWarnings.begin(),
224 ExpectedWarnings.end(),
225 Diags.warn_begin(), Diags.warn_end(),
226 "Warnings expected but not seen:");
227
228 // See if there were warnings that were seen but not expected.
229 HadProblem |= CompareDiagLists(SourceMgr,
230 Diags.warn_begin(), Diags.warn_end(),
231 ExpectedWarnings.begin(),
232 ExpectedWarnings.end(),
233 "Warnings seen but not expected:");
234
Douglas Gregor6293fd12008-09-11 02:46:36 +0000235 // See if there were notes that were expected but not seen.
236 HadProblem |= CompareDiagLists(SourceMgr,
237 ExpectedNotes.begin(),
238 ExpectedNotes.end(),
239 Diags.note_begin(), Diags.note_end(),
240 "Notes expected but not seen:");
241
242 // See if there were notes that were seen but not expected.
243 HadProblem |= CompareDiagLists(SourceMgr,
244 Diags.note_begin(), Diags.note_end(),
245 ExpectedNotes.begin(),
246 ExpectedNotes.end(),
247 "Notes seen but not expected:");
248
Chris Lattner4b009652007-07-25 00:24:17 +0000249 return HadProblem;
250}
251
Chris Lattner4b6f4752007-08-10 18:27:41 +0000252
Argiris Kirtzidis8d833762008-06-13 12:15:34 +0000253/// CheckDiagnostics - Gather the expected diagnostics and check them.
254bool clang::CheckDiagnostics(Preprocessor &PP) {
Chris Lattner4b009652007-07-25 00:24:17 +0000255 // Gather the set of expected diagnostics.
Douglas Gregor6293fd12008-09-11 02:46:36 +0000256 DiagList ExpectedErrors, ExpectedWarnings, ExpectedNotes;
257 FindExpectedDiags(PP, ExpectedErrors, ExpectedWarnings, ExpectedNotes);
Ted Kremenek0841c702007-09-25 18:37:20 +0000258
Chris Lattner4b009652007-07-25 00:24:17 +0000259 // Check that the expected diagnostics occurred.
Douglas Gregor6293fd12008-09-11 02:46:36 +0000260 return CheckResults(PP, ExpectedErrors, ExpectedWarnings, ExpectedNotes);
Chris Lattner4b009652007-07-25 00:24:17 +0000261}