blob: 8daaa6cce2dc1228bdb9f35c2170c9a2512a1087 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- DiagChecker.cpp - Diagnostic Checking Functions ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Bill Wendling and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "TextDiagnosticBuffer.h"
Chris Lattner556beb72007-09-15 22:56:56 +000017#include "clang/Sema/ASTStreamer.h"
18#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";
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 Lattnerdce7bd62007-09-16 19:27:16 +000054 size_type ColNo = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000055
56 for (;;) {
57 ColNo = Comment.find(ExpectedStr, ColNo);
58 if (ColNo == std::string::npos) break;
59
60 size_type OpenDiag = Comment.find_first_of("{{", ColNo);
61
62 if (OpenDiag == std::string::npos) {
63 fprintf(stderr,
64 "oops:%d: Cannot find beginning of expected error string\n",
Chris Lattner9dc1f532007-07-20 16:37:10 +000065 SourceMgr.getLogicalLineNumber(Pos));
Reid Spencer5f016e22007-07-11 17:01:13 +000066 break;
67 }
68
69 OpenDiag += 2;
70 size_type CloseDiag = Comment.find_first_of("}}", OpenDiag);
71
72 if (CloseDiag == std::string::npos) {
73 fprintf(stderr,
74 "oops:%d: Cannot find end of expected error string\n",
Chris Lattner9dc1f532007-07-20 16:37:10 +000075 SourceMgr.getLogicalLineNumber(Pos));
Reid Spencer5f016e22007-07-11 17:01:13 +000076 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
85/// FindExpectedDiags - Lex the file to finds all of the expected errors and
86/// warnings.
87static void FindExpectedDiags(Preprocessor &PP, unsigned MainFileID,
88 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.
Chris Lattner53b0dab2007-10-09 22:10:18 +000094 PP.EnterMainSourceFile(MainFileID);
Reid Spencer5f016e22007-07-11 17:01:13 +000095
Chris Lattnerf01654f2007-08-30 06:34:23 +000096 // Turn off all warnings from relexing or preprocessing.
97 PP.getDiagnostics().setWarnOnExtensions(false);
98 PP.getDiagnostics().setErrorOnExtensions(false);
99 for (unsigned i = 0; i != diag::NUM_DIAGNOSTICS; ++i)
100 if (PP.getDiagnostics().isNoteWarningOrExtension((diag::kind)i))
101 PP.getDiagnostics().setDiagnosticMapping((diag::kind)i, diag::MAP_IGNORE);
102
Chris Lattnerd2177732007-07-20 16:59:19 +0000103 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000104 do {
105 PP.Lex(Tok);
106
Chris Lattner057aaf62007-10-09 18:03:42 +0000107 if (Tok.is(tok::comment)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner057aaf62007-10-09 18:03:42 +0000118 } while (Tok.isNot(tok::eof));
Reid Spencer5f016e22007-07-11 17:01:13 +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",
Chris Lattner9dc1f532007-07-20 16:37:10 +0000138 SourceMgr.getLogicalLineNumber(I->first),
Reid Spencer5f016e22007-07-11 17:01:13 +0000139 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) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000156 unsigned LineNo1 = SourceMgr.getLogicalLineNumber(I->first);
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 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) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000161 unsigned LineNo2 = SourceMgr.getLogicalLineNumber(II->first);
Reid Spencer5f016e22007-07-11 17:01:13 +0000162 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 Lattner009e9f72007-08-10 18:27:41 +0000226
Ted Kremenek44579782007-09-25 18:37:20 +0000227/// CheckASTConsumer - Implement diagnostic checking for AST consumers.
228bool clang::CheckASTConsumer(Preprocessor &PP, unsigned MainFileID,
Ted Kremenekd39bcd82007-09-26 18:39:29 +0000229 ASTConsumer* C) {
Chris Lattner31e6c7d2007-11-03 06:24:16 +0000230 // Parse the AST and run the consumer, ultimately deleting C.
231 ParseAST(PP, MainFileID, C);
Ted Kremenek44579782007-09-25 18:37:20 +0000232
Reid Spencer5f016e22007-07-11 17:01:13 +0000233 // Gather the set of expected diagnostics.
234 DiagList ExpectedErrors, ExpectedWarnings;
235 FindExpectedDiags(PP, MainFileID, ExpectedErrors, ExpectedWarnings);
Ted Kremenek44579782007-09-25 18:37:20 +0000236
Reid Spencer5f016e22007-07-11 17:01:13 +0000237 // Check that the expected diagnostics occurred.
238 return CheckResults(PP, ExpectedErrors, ExpectedWarnings);
239}