blob: d6321d60ade744ab28c4529dcd83079661a47e0e [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));
Sebastian Redlad3c91c2008-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 }
Reid Spencer5f016e22007-07-11 17:01:13 +000085 ExpectedDiags.push_back(std::make_pair(Pos, Msg));
86 ColNo = CloseDiag + 2;
87 }
88}
89
Ted Kremenek95041a22007-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,
Reid Spencer5f016e22007-07-11 17:01:13 +000093 DiagList &ExpectedErrors,
Douglas Gregor233f74b2008-09-11 02:46:36 +000094 DiagList &ExpectedWarnings,
95 DiagList &ExpectedNotes) {
Chris Lattnera39f0482008-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 Lattnerf01654f2007-08-30 06:34:23 +000098
Chris Lattnera39f0482008-11-21 01:18:36 +000099 unsigned FileID = PP.getSourceManager().getMainFileID();
100 std::pair<const char*,const char*> File =
101 PP.getSourceManager().getBufferData(FileID);
102
103 // Create a lexer to lex all the tokens of the main file in raw mode.
104 Lexer RawLex(SourceLocation::getFileLoc(FileID, 0),
105 PP.getLangOptions(), File.first, File.second);
106
107 // Return comments as tokens, this is how we find expected diagnostics.
108 RawLex.SetCommentRetentionState(true);
109
Chris Lattnerd2177732007-07-20 16:59:19 +0000110 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000111 do {
Chris Lattnera39f0482008-11-21 01:18:36 +0000112 RawLex.Lex(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000113
Chris Lattner057aaf62007-10-09 18:03:42 +0000114 if (Tok.is(tok::comment)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 std::string Comment = PP.getSpelling(Tok);
116
117 // Find all expected errors
Chris Lattnera39f0482008-11-21 01:18:36 +0000118 FindDiagnostics(Comment, ExpectedErrors, PP.getSourceManager(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 Tok.getLocation(), ExpectedErrStr);
120
121 // Find all expected warnings
122 FindDiagnostics(Comment, ExpectedWarnings, PP.getSourceManager(),
123 Tok.getLocation(), ExpectedWarnStr);
Douglas Gregor233f74b2008-09-11 02:46:36 +0000124
125 // Find all expected notes
126 FindDiagnostics(Comment, ExpectedNotes, PP.getSourceManager(),
127 Tok.getLocation(), ExpectedNoteStr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 }
Chris Lattner057aaf62007-10-09 18:03:42 +0000129 } while (Tok.isNot(tok::eof));
Reid Spencer5f016e22007-07-11 17:01:13 +0000130}
131
132/// PrintProblem - This takes a diagnostic map of the delta between expected and
133/// seen diagnostics. If there's anything in it, then something unexpected
134/// happened. Print the map out in a nice format and return "true". If the map
135/// is empty and we're not going to print things, then return "false".
136///
137static bool PrintProblem(SourceManager &SourceMgr,
138 const_diag_iterator diag_begin,
139 const_diag_iterator diag_end,
140 const char *Msg) {
141 if (diag_begin == diag_end) return false;
142
143 fprintf(stderr, "%s\n", Msg);
144
145 for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I)
146 fprintf(stderr, " Line %d: %s\n",
Chris Lattner9dc1f532007-07-20 16:37:10 +0000147 SourceMgr.getLogicalLineNumber(I->first),
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 I->second.c_str());
149
150 return true;
151}
152
153/// CompareDiagLists - Compare two diangnostic lists and return the difference
154/// between them.
155///
156static bool CompareDiagLists(SourceManager &SourceMgr,
157 const_diag_iterator d1_begin,
158 const_diag_iterator d1_end,
159 const_diag_iterator d2_begin,
160 const_diag_iterator d2_end,
161 const char *Msg) {
162 DiagList DiffList;
163
164 for (const_diag_iterator I = d1_begin, E = d1_end; I != E; ++I) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000165 unsigned LineNo1 = SourceMgr.getLogicalLineNumber(I->first);
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 const std::string &Diag1 = I->second;
167 bool Found = false;
168
169 for (const_diag_iterator II = d2_begin, IE = d2_end; II != IE; ++II) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000170 unsigned LineNo2 = SourceMgr.getLogicalLineNumber(II->first);
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 if (LineNo1 != LineNo2) continue;
172
173 const std::string &Diag2 = II->second;
174 if (Diag2.find(Diag1) != std::string::npos ||
175 Diag1.find(Diag2) != std::string::npos) {
176 Found = true;
177 break;
178 }
179 }
180
181 if (!Found)
182 DiffList.push_back(std::make_pair(I->first, Diag1));
183 }
184
185 return PrintProblem(SourceMgr, DiffList.begin(), DiffList.end(), Msg);
186}
187
188/// CheckResults - This compares the expected results to those that
189/// were actually reported. It emits any discrepencies. Return "true" if there
190/// were problems. Return "false" otherwise.
191///
192static bool CheckResults(Preprocessor &PP,
193 const DiagList &ExpectedErrors,
Douglas Gregor233f74b2008-09-11 02:46:36 +0000194 const DiagList &ExpectedWarnings,
195 const DiagList &ExpectedNotes) {
Nico Weber7bfaaae2008-08-10 19:59:06 +0000196 const DiagnosticClient *DiagClient = PP.getDiagnostics().getClient();
197 assert(DiagClient != 0 &&
198 "DiagChecker requires a valid TextDiagnosticBuffer");
Reid Spencer5f016e22007-07-11 17:01:13 +0000199 const TextDiagnosticBuffer &Diags =
Nico Weber7bfaaae2008-08-10 19:59:06 +0000200 static_cast<const TextDiagnosticBuffer&>(*DiagClient);
Reid Spencer5f016e22007-07-11 17:01:13 +0000201 SourceManager &SourceMgr = PP.getSourceManager();
202
203 // We want to capture the delta between what was expected and what was
204 // seen.
205 //
206 // Expected \ Seen - set expected but not seen
207 // Seen \ Expected - set seen but not expected
208 bool HadProblem = false;
209
210 // See if there were errors that were expected but not seen.
211 HadProblem |= CompareDiagLists(SourceMgr,
212 ExpectedErrors.begin(), ExpectedErrors.end(),
213 Diags.err_begin(), Diags.err_end(),
214 "Errors expected but not seen:");
215
216 // See if there were errors that were seen but not expected.
217 HadProblem |= CompareDiagLists(SourceMgr,
218 Diags.err_begin(), Diags.err_end(),
219 ExpectedErrors.begin(), ExpectedErrors.end(),
220 "Errors seen but not expected:");
221
222 // See if there were warnings that were expected but not seen.
223 HadProblem |= CompareDiagLists(SourceMgr,
224 ExpectedWarnings.begin(),
225 ExpectedWarnings.end(),
226 Diags.warn_begin(), Diags.warn_end(),
227 "Warnings expected but not seen:");
228
229 // See if there were warnings that were seen but not expected.
230 HadProblem |= CompareDiagLists(SourceMgr,
231 Diags.warn_begin(), Diags.warn_end(),
232 ExpectedWarnings.begin(),
233 ExpectedWarnings.end(),
234 "Warnings seen but not expected:");
235
Douglas Gregor233f74b2008-09-11 02:46:36 +0000236 // See if there were notes that were expected but not seen.
237 HadProblem |= CompareDiagLists(SourceMgr,
238 ExpectedNotes.begin(),
239 ExpectedNotes.end(),
240 Diags.note_begin(), Diags.note_end(),
241 "Notes expected but not seen:");
242
243 // See if there were notes that were seen but not expected.
244 HadProblem |= CompareDiagLists(SourceMgr,
245 Diags.note_begin(), Diags.note_end(),
246 ExpectedNotes.begin(),
247 ExpectedNotes.end(),
248 "Notes seen but not expected:");
249
Reid Spencer5f016e22007-07-11 17:01:13 +0000250 return HadProblem;
251}
252
Chris Lattner009e9f72007-08-10 18:27:41 +0000253
Argyrios Kyrtzidis14d41402008-06-13 12:15:34 +0000254/// CheckDiagnostics - Gather the expected diagnostics and check them.
255bool clang::CheckDiagnostics(Preprocessor &PP) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000256 // Gather the set of expected diagnostics.
Douglas Gregor233f74b2008-09-11 02:46:36 +0000257 DiagList ExpectedErrors, ExpectedWarnings, ExpectedNotes;
258 FindExpectedDiags(PP, ExpectedErrors, ExpectedWarnings, ExpectedNotes);
Ted Kremenek44579782007-09-25 18:37:20 +0000259
Reid Spencer5f016e22007-07-11 17:01:13 +0000260 // Check that the expected diagnostics occurred.
Douglas Gregor233f74b2008-09-11 02:46:36 +0000261 return CheckResults(PP, ExpectedErrors, ExpectedWarnings, ExpectedNotes);
Reid Spencer5f016e22007-07-11 17:01:13 +0000262}