blob: 643c17ee4296ed5f0a51539e15afc4846f1f7cf2 [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"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "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";
43
44/// FindDiagnostics - Go through the comment and see if it indicates expected
45/// diagnostics. If so, then put them in a diagnostic list.
46///
47static void FindDiagnostics(const std::string &Comment,
48 DiagList &ExpectedDiags,
49 SourceManager &SourceMgr,
50 SourceLocation Pos,
51 const char * const ExpectedStr) {
52 // Find all expected diagnostics
53 typedef std::string::size_type size_type;
Chris Lattner12ee82e2007-09-16 19:27:16 +000054 size_type ColNo = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000055
56 for (;;) {
57 ColNo = Comment.find(ExpectedStr, ColNo);
58 if (ColNo == std::string::npos) break;
59
Bill Wendlingc08e97b2007-11-26 08:26:20 +000060 size_type OpenDiag = Comment.find("{{", ColNo);
Chris Lattner4b009652007-07-25 00:24:17 +000061
62 if (OpenDiag == std::string::npos) {
63 fprintf(stderr,
64 "oops:%d: Cannot find beginning of expected error string\n",
65 SourceMgr.getLogicalLineNumber(Pos));
66 break;
67 }
68
69 OpenDiag += 2;
Bill Wendlingc08e97b2007-11-26 08:26:20 +000070 size_type CloseDiag = Comment.find("}}", OpenDiag);
Chris Lattner4b009652007-07-25 00:24:17 +000071
72 if (CloseDiag == std::string::npos) {
73 fprintf(stderr,
74 "oops:%d: Cannot find end of expected error string\n",
75 SourceMgr.getLogicalLineNumber(Pos));
76 break;
77 }
78
79 std::string Msg(Comment.substr(OpenDiag, CloseDiag - OpenDiag));
80 ExpectedDiags.push_back(std::make_pair(Pos, Msg));
81 ColNo = CloseDiag + 2;
82 }
83}
84
Ted Kremenek17861c52007-12-19 22:51:13 +000085/// FindExpectedDiags - Lex the main source file to find all of the
86// expected errors and warnings.
87static void FindExpectedDiags(Preprocessor &PP,
Chris Lattner4b009652007-07-25 00:24:17 +000088 DiagList &ExpectedErrors,
89 DiagList &ExpectedWarnings) {
90 // Return comments as tokens, this is how we find expected diagnostics.
91 PP.SetCommentRetentionState(true, true);
92
93 // Enter the cave.
Ted Kremenek17861c52007-12-19 22:51:13 +000094 PP.EnterMainSourceFile();
Chris Lattner4b009652007-07-25 00:24:17 +000095
Chris Lattner2db971b2007-08-30 06:34:23 +000096 // Turn off all warnings from relexing or preprocessing.
97 PP.getDiagnostics().setWarnOnExtensions(false);
98 PP.getDiagnostics().setErrorOnExtensions(false);
Chris Lattner4478db92007-11-30 22:53:43 +000099 for (unsigned i = 0; i != diag::NUM_BUILTIN_DIAGNOSTICS; ++i)
100 if (PP.getDiagnostics().isBuiltinNoteWarningOrExtension((diag::kind)i))
Chris Lattner2db971b2007-08-30 06:34:23 +0000101 PP.getDiagnostics().setDiagnosticMapping((diag::kind)i, diag::MAP_IGNORE);
102
Chris Lattner4b009652007-07-25 00:24:17 +0000103 Token Tok;
104 do {
105 PP.Lex(Tok);
106
Chris Lattner3b494152007-10-09 18:03:42 +0000107 if (Tok.is(tok::comment)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000108 std::string Comment = PP.getSpelling(Tok);
109
110 // Find all expected errors
111 FindDiagnostics(Comment, ExpectedErrors,PP.getSourceManager(),
112 Tok.getLocation(), ExpectedErrStr);
113
114 // Find all expected warnings
115 FindDiagnostics(Comment, ExpectedWarnings, PP.getSourceManager(),
116 Tok.getLocation(), ExpectedWarnStr);
117 }
Chris Lattner3b494152007-10-09 18:03:42 +0000118 } while (Tok.isNot(tok::eof));
Chris Lattner4b009652007-07-25 00:24:17 +0000119
120 PP.SetCommentRetentionState(false, false);
121}
122
123/// PrintProblem - This takes a diagnostic map of the delta between expected and
124/// seen diagnostics. If there's anything in it, then something unexpected
125/// happened. Print the map out in a nice format and return "true". If the map
126/// is empty and we're not going to print things, then return "false".
127///
128static bool PrintProblem(SourceManager &SourceMgr,
129 const_diag_iterator diag_begin,
130 const_diag_iterator diag_end,
131 const char *Msg) {
132 if (diag_begin == diag_end) return false;
133
134 fprintf(stderr, "%s\n", Msg);
135
136 for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I)
137 fprintf(stderr, " Line %d: %s\n",
138 SourceMgr.getLogicalLineNumber(I->first),
139 I->second.c_str());
140
141 return true;
142}
143
144/// CompareDiagLists - Compare two diangnostic lists and return the difference
145/// between them.
146///
147static bool CompareDiagLists(SourceManager &SourceMgr,
148 const_diag_iterator d1_begin,
149 const_diag_iterator d1_end,
150 const_diag_iterator d2_begin,
151 const_diag_iterator d2_end,
152 const char *Msg) {
153 DiagList DiffList;
154
155 for (const_diag_iterator I = d1_begin, E = d1_end; I != E; ++I) {
156 unsigned LineNo1 = SourceMgr.getLogicalLineNumber(I->first);
157 const std::string &Diag1 = I->second;
158 bool Found = false;
159
160 for (const_diag_iterator II = d2_begin, IE = d2_end; II != IE; ++II) {
161 unsigned LineNo2 = SourceMgr.getLogicalLineNumber(II->first);
162 if (LineNo1 != LineNo2) continue;
163
164 const std::string &Diag2 = II->second;
165 if (Diag2.find(Diag1) != std::string::npos ||
166 Diag1.find(Diag2) != std::string::npos) {
167 Found = true;
168 break;
169 }
170 }
171
172 if (!Found)
173 DiffList.push_back(std::make_pair(I->first, Diag1));
174 }
175
176 return PrintProblem(SourceMgr, DiffList.begin(), DiffList.end(), Msg);
177}
178
179/// CheckResults - This compares the expected results to those that
180/// were actually reported. It emits any discrepencies. Return "true" if there
181/// were problems. Return "false" otherwise.
182///
183static bool CheckResults(Preprocessor &PP,
184 const DiagList &ExpectedErrors,
185 const DiagList &ExpectedWarnings) {
186 const TextDiagnosticBuffer &Diags =
187 static_cast<const TextDiagnosticBuffer&>(PP.getDiagnostics().getClient());
188 SourceManager &SourceMgr = PP.getSourceManager();
189
190 // We want to capture the delta between what was expected and what was
191 // seen.
192 //
193 // Expected \ Seen - set expected but not seen
194 // Seen \ Expected - set seen but not expected
195 bool HadProblem = false;
196
197 // See if there were errors that were expected but not seen.
198 HadProblem |= CompareDiagLists(SourceMgr,
199 ExpectedErrors.begin(), ExpectedErrors.end(),
200 Diags.err_begin(), Diags.err_end(),
201 "Errors expected but not seen:");
202
203 // See if there were errors that were seen but not expected.
204 HadProblem |= CompareDiagLists(SourceMgr,
205 Diags.err_begin(), Diags.err_end(),
206 ExpectedErrors.begin(), ExpectedErrors.end(),
207 "Errors seen but not expected:");
208
209 // See if there were warnings that were expected but not seen.
210 HadProblem |= CompareDiagLists(SourceMgr,
211 ExpectedWarnings.begin(),
212 ExpectedWarnings.end(),
213 Diags.warn_begin(), Diags.warn_end(),
214 "Warnings expected but not seen:");
215
216 // See if there were warnings that were seen but not expected.
217 HadProblem |= CompareDiagLists(SourceMgr,
218 Diags.warn_begin(), Diags.warn_end(),
219 ExpectedWarnings.begin(),
220 ExpectedWarnings.end(),
221 "Warnings seen but not expected:");
222
223 return HadProblem;
224}
225
Chris Lattner4b6f4752007-08-10 18:27:41 +0000226
Ted Kremenek0841c702007-09-25 18:37:20 +0000227/// CheckASTConsumer - Implement diagnostic checking for AST consumers.
Ted Kremenek17861c52007-12-19 22:51:13 +0000228bool clang::CheckASTConsumer(Preprocessor &PP, ASTConsumer* C) {
229
Chris Lattner8593cbf2007-11-03 06:24:16 +0000230 // Parse the AST and run the consumer, ultimately deleting C.
Ted Kremenek17861c52007-12-19 22:51:13 +0000231 ParseAST(PP, C);
Argiris Kirtzidis8d833762008-06-13 12:15:34 +0000232 return CheckDiagnostics(PP);
233}
234
235/// CheckDiagnostics - Gather the expected diagnostics and check them.
236bool clang::CheckDiagnostics(Preprocessor &PP) {
Chris Lattner4b009652007-07-25 00:24:17 +0000237 // Gather the set of expected diagnostics.
238 DiagList ExpectedErrors, ExpectedWarnings;
Ted Kremenek17861c52007-12-19 22:51:13 +0000239 FindExpectedDiags(PP, ExpectedErrors, ExpectedWarnings);
Ted Kremenek0841c702007-09-25 18:37:20 +0000240
Chris Lattner4b009652007-07-25 00:24:17 +0000241 // Check that the expected diagnostics occurred.
242 return CheckResults(PP, ExpectedErrors, ExpectedWarnings);
243}