Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- DiagChecker.cpp - Diagnostic Checking Functions ------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Process the input files and check that the diagnostic messages are expected. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang.h" |
Chris Lattner | 97e8b6f | 2007-10-07 06:04:32 +0000 | [diff] [blame] | 15 | #include "ASTConsumers.h" |
Nico Weber | fd54ebc | 2008-08-05 23:33:20 +0000 | [diff] [blame] | 16 | #include "clang/Driver/TextDiagnosticBuffer.h" |
Chris Lattner | e91c134 | 2008-02-06 00:23:21 +0000 | [diff] [blame] | 17 | #include "clang/Sema/ParseAST.h" |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTConsumer.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 19 | #include "clang/Basic/SourceManager.h" |
| 20 | #include "clang/Lex/Preprocessor.h" |
| 21 | using namespace clang; |
| 22 | |
| 23 | typedef TextDiagnosticBuffer::DiagList DiagList; |
| 24 | typedef TextDiagnosticBuffer::const_iterator const_diag_iterator; |
| 25 | |
Chris Lattner | b2c8c55 | 2008-11-23 23:38:26 +0000 | [diff] [blame] | 26 | static 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 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 32 | // 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 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 47 | /// FindDiagnostics - Go through the comment and see if it indicates expected |
| 48 | /// diagnostics. If so, then put them in a diagnostic list. |
| 49 | /// |
Chris Lattner | 0947b4e | 2008-11-24 01:28:17 +0000 | [diff] [blame] | 50 | static void FindDiagnostics(const char *CommentStart, unsigned CommentLen, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 51 | DiagList &ExpectedDiags, |
Chris Lattner | 0947b4e | 2008-11-24 01:28:17 +0000 | [diff] [blame] | 52 | Preprocessor &PP, SourceLocation Pos, |
| 53 | const char *ExpectedStr) { |
| 54 | const char *CommentEnd = CommentStart+CommentLen; |
| 55 | unsigned ExpectedStrLen = strlen(ExpectedStr); |
Chris Lattner | b2c8c55 | 2008-11-23 23:38:26 +0000 | [diff] [blame] | 56 | |
Chris Lattner | 0947b4e | 2008-11-24 01:28:17 +0000 | [diff] [blame] | 57 | // Find all expected-foo diagnostics in the string and add them to |
| 58 | // ExpectedDiags. |
| 59 | while (CommentStart != CommentEnd) { |
| 60 | CommentStart = std::find(CommentStart, CommentEnd, 'e'); |
| 61 | if (unsigned(CommentEnd-CommentStart) < ExpectedStrLen) return; |
| 62 | |
| 63 | // If this isn't expected-foo, ignore it. |
| 64 | if (memcmp(CommentStart, ExpectedStr, ExpectedStrLen)) { |
| 65 | ++CommentStart; |
| 66 | continue; |
| 67 | } |
| 68 | |
| 69 | CommentStart += ExpectedStrLen; |
| 70 | |
| 71 | // Skip whitespace. |
| 72 | while (CommentStart != CommentEnd && |
| 73 | isspace(CommentStart[0])) |
| 74 | ++CommentStart; |
| 75 | |
| 76 | // We should have a {{ now. |
| 77 | if (CommentEnd-CommentStart < 2 || |
| 78 | CommentStart[0] != '{' || CommentStart[1] != '{') { |
| 79 | if (std::find(CommentStart, CommentEnd, '{') != CommentEnd) |
| 80 | EmitError(PP, Pos, "bogus characters before '{{' in expected string"); |
| 81 | else |
| 82 | EmitError(PP, Pos, "cannot find start ('{{') of expected string"); |
Chris Lattner | b2c8c55 | 2008-11-23 23:38:26 +0000 | [diff] [blame] | 83 | return; |
Chris Lattner | 0947b4e | 2008-11-24 01:28:17 +0000 | [diff] [blame] | 84 | } |
| 85 | CommentStart += 2; |
| 86 | |
| 87 | // Find the }}. |
| 88 | const char *ExpectedEnd = CommentStart; |
| 89 | while (1) { |
| 90 | ExpectedEnd = std::find(ExpectedEnd, CommentEnd, '}'); |
| 91 | if (CommentEnd-ExpectedEnd < 2) { |
| 92 | EmitError(PP, Pos, "cannot find end ('}}') of expected string"); |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | if (ExpectedEnd[1] == '}') |
| 97 | break; |
| 98 | |
| 99 | ++ExpectedEnd; // Skip over singular }'s |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Chris Lattner | 0947b4e | 2008-11-24 01:28:17 +0000 | [diff] [blame] | 102 | std::string Msg(CommentStart, ExpectedEnd); |
| 103 | std::string::size_type FindPos; |
Chris Lattner | b2c8c55 | 2008-11-23 23:38:26 +0000 | [diff] [blame] | 104 | while ((FindPos = Msg.find("\\n")) != std::string::npos) |
Sebastian Redl | ad3c91c | 2008-10-26 19:05:16 +0000 | [diff] [blame] | 105 | Msg.replace(FindPos, 2, "\n"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 106 | ExpectedDiags.push_back(std::make_pair(Pos, Msg)); |
Chris Lattner | 0947b4e | 2008-11-24 01:28:17 +0000 | [diff] [blame] | 107 | |
| 108 | CommentStart = ExpectedEnd; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 112 | /// FindExpectedDiags - Lex the main source file to find all of the |
| 113 | // expected errors and warnings. |
| 114 | static void FindExpectedDiags(Preprocessor &PP, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 115 | DiagList &ExpectedErrors, |
Douglas Gregor | 233f74b | 2008-09-11 02:46:36 +0000 | [diff] [blame] | 116 | DiagList &ExpectedWarnings, |
| 117 | DiagList &ExpectedNotes) { |
Chris Lattner | a39f048 | 2008-11-21 01:18:36 +0000 | [diff] [blame] | 118 | // Create a raw lexer to pull all the comments out of the main file. We don't |
| 119 | // want to look in #include'd headers for expected-error strings. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 120 | FileID FID = PP.getSourceManager().getMainFileID(); |
Chris Lattner | a39f048 | 2008-11-21 01:18:36 +0000 | [diff] [blame] | 121 | |
| 122 | // Create a lexer to lex all the tokens of the main file in raw mode. |
Chris Lattner | 4448a01 | 2009-01-17 07:41:36 +0000 | [diff] [blame] | 123 | Lexer RawLex(FID, PP.getSourceManager(), PP.getLangOptions()); |
Chris Lattner | a39f048 | 2008-11-21 01:18:36 +0000 | [diff] [blame] | 124 | |
| 125 | // Return comments as tokens, this is how we find expected diagnostics. |
| 126 | RawLex.SetCommentRetentionState(true); |
| 127 | |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 128 | Token Tok; |
Chris Lattner | b2c8c55 | 2008-11-23 23:38:26 +0000 | [diff] [blame] | 129 | Tok.setKind(tok::comment); |
| 130 | while (Tok.isNot(tok::eof)) { |
Chris Lattner | a39f048 | 2008-11-21 01:18:36 +0000 | [diff] [blame] | 131 | RawLex.Lex(Tok); |
Chris Lattner | b2c8c55 | 2008-11-23 23:38:26 +0000 | [diff] [blame] | 132 | if (!Tok.is(tok::comment)) continue; |
| 133 | |
| 134 | std::string Comment = PP.getSpelling(Tok); |
Chris Lattner | 0947b4e | 2008-11-24 01:28:17 +0000 | [diff] [blame] | 135 | if (Comment.empty()) continue; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 136 | |
Chris Lattner | 0947b4e | 2008-11-24 01:28:17 +0000 | [diff] [blame] | 137 | |
Chris Lattner | b2c8c55 | 2008-11-23 23:38:26 +0000 | [diff] [blame] | 138 | // Find all expected errors. |
Chris Lattner | 0947b4e | 2008-11-24 01:28:17 +0000 | [diff] [blame] | 139 | FindDiagnostics(&Comment[0], Comment.size(), ExpectedErrors, PP, |
Chris Lattner | b2c8c55 | 2008-11-23 23:38:26 +0000 | [diff] [blame] | 140 | Tok.getLocation(), "expected-error"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 141 | |
Chris Lattner | b2c8c55 | 2008-11-23 23:38:26 +0000 | [diff] [blame] | 142 | // Find all expected warnings. |
Chris Lattner | 0947b4e | 2008-11-24 01:28:17 +0000 | [diff] [blame] | 143 | FindDiagnostics(&Comment[0], Comment.size(), ExpectedWarnings, PP, |
Chris Lattner | b2c8c55 | 2008-11-23 23:38:26 +0000 | [diff] [blame] | 144 | Tok.getLocation(), "expected-warning"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 145 | |
Chris Lattner | b2c8c55 | 2008-11-23 23:38:26 +0000 | [diff] [blame] | 146 | // Find all expected notes. |
Chris Lattner | 0947b4e | 2008-11-24 01:28:17 +0000 | [diff] [blame] | 147 | FindDiagnostics(&Comment[0], Comment.size(), ExpectedNotes, PP, |
Chris Lattner | b2c8c55 | 2008-11-23 23:38:26 +0000 | [diff] [blame] | 148 | Tok.getLocation(), "expected-note"); |
| 149 | }; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /// PrintProblem - This takes a diagnostic map of the delta between expected and |
| 153 | /// seen diagnostics. If there's anything in it, then something unexpected |
| 154 | /// happened. Print the map out in a nice format and return "true". If the map |
| 155 | /// is empty and we're not going to print things, then return "false". |
| 156 | /// |
| 157 | static bool PrintProblem(SourceManager &SourceMgr, |
| 158 | const_diag_iterator diag_begin, |
| 159 | const_diag_iterator diag_end, |
| 160 | const char *Msg) { |
| 161 | if (diag_begin == diag_end) return false; |
| 162 | |
| 163 | fprintf(stderr, "%s\n", Msg); |
| 164 | |
| 165 | for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I) |
| 166 | fprintf(stderr, " Line %d: %s\n", |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 167 | SourceMgr.getInstantiationLineNumber(I->first), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 168 | I->second.c_str()); |
| 169 | |
| 170 | return true; |
| 171 | } |
| 172 | |
| 173 | /// CompareDiagLists - Compare two diangnostic lists and return the difference |
| 174 | /// between them. |
| 175 | /// |
| 176 | static bool CompareDiagLists(SourceManager &SourceMgr, |
| 177 | const_diag_iterator d1_begin, |
| 178 | const_diag_iterator d1_end, |
| 179 | const_diag_iterator d2_begin, |
| 180 | const_diag_iterator d2_end, |
| 181 | const char *Msg) { |
| 182 | DiagList DiffList; |
| 183 | |
| 184 | for (const_diag_iterator I = d1_begin, E = d1_end; I != E; ++I) { |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 185 | unsigned LineNo1 = SourceMgr.getInstantiationLineNumber(I->first); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 186 | const std::string &Diag1 = I->second; |
| 187 | bool Found = false; |
| 188 | |
| 189 | for (const_diag_iterator II = d2_begin, IE = d2_end; II != IE; ++II) { |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 190 | unsigned LineNo2 = SourceMgr.getInstantiationLineNumber(II->first); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 191 | if (LineNo1 != LineNo2) continue; |
| 192 | |
| 193 | const std::string &Diag2 = II->second; |
| 194 | if (Diag2.find(Diag1) != std::string::npos || |
| 195 | Diag1.find(Diag2) != std::string::npos) { |
| 196 | Found = true; |
| 197 | break; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | if (!Found) |
| 202 | DiffList.push_back(std::make_pair(I->first, Diag1)); |
| 203 | } |
| 204 | |
| 205 | return PrintProblem(SourceMgr, DiffList.begin(), DiffList.end(), Msg); |
| 206 | } |
| 207 | |
| 208 | /// CheckResults - This compares the expected results to those that |
| 209 | /// were actually reported. It emits any discrepencies. Return "true" if there |
| 210 | /// were problems. Return "false" otherwise. |
| 211 | /// |
| 212 | static bool CheckResults(Preprocessor &PP, |
| 213 | const DiagList &ExpectedErrors, |
Douglas Gregor | 233f74b | 2008-09-11 02:46:36 +0000 | [diff] [blame] | 214 | const DiagList &ExpectedWarnings, |
| 215 | const DiagList &ExpectedNotes) { |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 216 | const DiagnosticClient *DiagClient = PP.getDiagnostics().getClient(); |
| 217 | assert(DiagClient != 0 && |
| 218 | "DiagChecker requires a valid TextDiagnosticBuffer"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 219 | const TextDiagnosticBuffer &Diags = |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 220 | static_cast<const TextDiagnosticBuffer&>(*DiagClient); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 221 | SourceManager &SourceMgr = PP.getSourceManager(); |
| 222 | |
| 223 | // We want to capture the delta between what was expected and what was |
| 224 | // seen. |
| 225 | // |
| 226 | // Expected \ Seen - set expected but not seen |
| 227 | // Seen \ Expected - set seen but not expected |
| 228 | bool HadProblem = false; |
| 229 | |
| 230 | // See if there were errors that were expected but not seen. |
| 231 | HadProblem |= CompareDiagLists(SourceMgr, |
| 232 | ExpectedErrors.begin(), ExpectedErrors.end(), |
| 233 | Diags.err_begin(), Diags.err_end(), |
| 234 | "Errors expected but not seen:"); |
| 235 | |
| 236 | // See if there were errors that were seen but not expected. |
| 237 | HadProblem |= CompareDiagLists(SourceMgr, |
| 238 | Diags.err_begin(), Diags.err_end(), |
| 239 | ExpectedErrors.begin(), ExpectedErrors.end(), |
| 240 | "Errors seen but not expected:"); |
| 241 | |
| 242 | // See if there were warnings that were expected but not seen. |
| 243 | HadProblem |= CompareDiagLists(SourceMgr, |
| 244 | ExpectedWarnings.begin(), |
| 245 | ExpectedWarnings.end(), |
| 246 | Diags.warn_begin(), Diags.warn_end(), |
| 247 | "Warnings expected but not seen:"); |
| 248 | |
| 249 | // See if there were warnings that were seen but not expected. |
| 250 | HadProblem |= CompareDiagLists(SourceMgr, |
| 251 | Diags.warn_begin(), Diags.warn_end(), |
| 252 | ExpectedWarnings.begin(), |
| 253 | ExpectedWarnings.end(), |
| 254 | "Warnings seen but not expected:"); |
| 255 | |
Douglas Gregor | 233f74b | 2008-09-11 02:46:36 +0000 | [diff] [blame] | 256 | // See if there were notes that were expected but not seen. |
| 257 | HadProblem |= CompareDiagLists(SourceMgr, |
| 258 | ExpectedNotes.begin(), |
| 259 | ExpectedNotes.end(), |
| 260 | Diags.note_begin(), Diags.note_end(), |
| 261 | "Notes expected but not seen:"); |
| 262 | |
| 263 | // See if there were notes that were seen but not expected. |
| 264 | HadProblem |= CompareDiagLists(SourceMgr, |
| 265 | Diags.note_begin(), Diags.note_end(), |
| 266 | ExpectedNotes.begin(), |
| 267 | ExpectedNotes.end(), |
| 268 | "Notes seen but not expected:"); |
| 269 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 270 | return HadProblem; |
| 271 | } |
| 272 | |
Chris Lattner | 009e9f7 | 2007-08-10 18:27:41 +0000 | [diff] [blame] | 273 | |
Argyrios Kyrtzidis | 14d4140 | 2008-06-13 12:15:34 +0000 | [diff] [blame] | 274 | /// CheckDiagnostics - Gather the expected diagnostics and check them. |
| 275 | bool clang::CheckDiagnostics(Preprocessor &PP) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 276 | // Gather the set of expected diagnostics. |
Douglas Gregor | 233f74b | 2008-09-11 02:46:36 +0000 | [diff] [blame] | 277 | DiagList ExpectedErrors, ExpectedWarnings, ExpectedNotes; |
| 278 | FindExpectedDiags(PP, ExpectedErrors, ExpectedWarnings, ExpectedNotes); |
Ted Kremenek | 4457978 | 2007-09-25 18:37:20 +0000 | [diff] [blame] | 279 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 280 | // Check that the expected diagnostics occurred. |
Douglas Gregor | 233f74b | 2008-09-11 02:46:36 +0000 | [diff] [blame] | 281 | return CheckResults(PP, ExpectedErrors, ExpectedWarnings, ExpectedNotes); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 282 | } |