Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 1 | //===--- VerifyDiagnosticsClient.cpp - Verifying Diagnostic Client --------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This is a concrete diagnostic client, which buffers the diagnostic messages. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Frontend/VerifyDiagnosticsClient.h" |
| 15 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 16 | #include "clang/Frontend/TextDiagnosticBuffer.h" |
| 17 | #include "clang/Lex/Preprocessor.h" |
| 18 | #include "llvm/ADT/SmallString.h" |
| 19 | #include "llvm/Support/raw_ostream.h" |
| 20 | using namespace clang; |
| 21 | |
Daniel Dunbar | 221c721 | 2009-11-14 07:53:24 +0000 | [diff] [blame] | 22 | VerifyDiagnosticsClient::VerifyDiagnosticsClient(Diagnostic &_Diags, |
| 23 | DiagnosticClient *_Primary) |
| 24 | : Diags(_Diags), PrimaryClient(_Primary), |
| 25 | Buffer(new TextDiagnosticBuffer()), CurrentPreprocessor(0), NumErrors(0) { |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | VerifyDiagnosticsClient::~VerifyDiagnosticsClient() { |
| 29 | CheckDiagnostics(); |
| 30 | } |
| 31 | |
| 32 | // DiagnosticClient interface. |
| 33 | |
| 34 | void VerifyDiagnosticsClient::BeginSourceFile(const LangOptions &LangOpts, |
| 35 | const Preprocessor *PP) { |
| 36 | // FIXME: Const hack, we screw up the preprocessor but in practice its ok |
| 37 | // because it doesn't get reused. It would be better if we could make a copy |
| 38 | // though. |
| 39 | CurrentPreprocessor = const_cast<Preprocessor*>(PP); |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 40 | |
| 41 | PrimaryClient->BeginSourceFile(LangOpts, PP); |
| 42 | } |
| 43 | |
| 44 | void VerifyDiagnosticsClient::EndSourceFile() { |
| 45 | CheckDiagnostics(); |
| 46 | |
| 47 | PrimaryClient->EndSourceFile(); |
| 48 | |
| 49 | CurrentPreprocessor = 0; |
| 50 | } |
Daniel Dunbar | 221c721 | 2009-11-14 07:53:24 +0000 | [diff] [blame] | 51 | |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 52 | void VerifyDiagnosticsClient::HandleDiagnostic(Diagnostic::Level DiagLevel, |
| 53 | const DiagnosticInfo &Info) { |
| 54 | // Send the diagnostic to the buffer, we will check it once we reach the end |
| 55 | // of the source file (or are destructed). |
| 56 | Buffer->HandleDiagnostic(DiagLevel, Info); |
| 57 | } |
| 58 | |
| 59 | // FIXME: It would be nice to just get this from the primary diagnostic client |
| 60 | // or something. |
| 61 | bool VerifyDiagnosticsClient::HadErrors() { |
| 62 | CheckDiagnostics(); |
| 63 | |
| 64 | return NumErrors != 0; |
| 65 | } |
| 66 | |
| 67 | //===----------------------------------------------------------------------===// |
| 68 | // Checking diagnostics implementation. |
| 69 | //===----------------------------------------------------------------------===// |
| 70 | |
| 71 | typedef TextDiagnosticBuffer::DiagList DiagList; |
| 72 | typedef TextDiagnosticBuffer::const_iterator const_diag_iterator; |
| 73 | |
| 74 | /// FindDiagnostics - Go through the comment and see if it indicates expected |
| 75 | /// diagnostics. If so, then put them in a diagnostic list. |
| 76 | /// |
| 77 | static void FindDiagnostics(const char *CommentStart, unsigned CommentLen, |
| 78 | DiagList &ExpectedDiags, |
| 79 | Preprocessor &PP, SourceLocation Pos, |
| 80 | const char *ExpectedStr) { |
| 81 | const char *CommentEnd = CommentStart+CommentLen; |
| 82 | unsigned ExpectedStrLen = strlen(ExpectedStr); |
| 83 | |
| 84 | // Find all expected-foo diagnostics in the string and add them to |
| 85 | // ExpectedDiags. |
| 86 | while (CommentStart != CommentEnd) { |
| 87 | CommentStart = std::find(CommentStart, CommentEnd, 'e'); |
| 88 | if (unsigned(CommentEnd-CommentStart) < ExpectedStrLen) return; |
| 89 | |
| 90 | // If this isn't expected-foo, ignore it. |
| 91 | if (memcmp(CommentStart, ExpectedStr, ExpectedStrLen)) { |
| 92 | ++CommentStart; |
| 93 | continue; |
| 94 | } |
| 95 | |
| 96 | CommentStart += ExpectedStrLen; |
| 97 | |
| 98 | // Skip whitespace. |
| 99 | while (CommentStart != CommentEnd && |
| 100 | isspace(CommentStart[0])) |
| 101 | ++CommentStart; |
| 102 | |
| 103 | // Default, if we find the '{' now, is 1 time. |
| 104 | int Times = 1; |
| 105 | int Temp = 0; |
| 106 | // In extended syntax, there could be a digit now. |
| 107 | while (CommentStart != CommentEnd && |
| 108 | CommentStart[0] >= '0' && CommentStart[0] <= '9') { |
| 109 | Temp *= 10; |
| 110 | Temp += CommentStart[0] - '0'; |
| 111 | ++CommentStart; |
| 112 | } |
| 113 | if (Temp > 0) |
| 114 | Times = Temp; |
| 115 | |
| 116 | // Skip whitespace again. |
| 117 | while (CommentStart != CommentEnd && |
| 118 | isspace(CommentStart[0])) |
| 119 | ++CommentStart; |
| 120 | |
| 121 | // We should have a {{ now. |
| 122 | if (CommentEnd-CommentStart < 2 || |
| 123 | CommentStart[0] != '{' || CommentStart[1] != '{') { |
| 124 | if (std::find(CommentStart, CommentEnd, '{') != CommentEnd) |
| 125 | PP.Diag(Pos, diag::err_verify_bogus_characters); |
| 126 | else |
| 127 | PP.Diag(Pos, diag::err_verify_missing_start); |
| 128 | return; |
| 129 | } |
| 130 | CommentStart += 2; |
| 131 | |
| 132 | // Find the }}. |
| 133 | const char *ExpectedEnd = CommentStart; |
| 134 | while (1) { |
| 135 | ExpectedEnd = std::find(ExpectedEnd, CommentEnd, '}'); |
| 136 | if (CommentEnd-ExpectedEnd < 2) { |
| 137 | PP.Diag(Pos, diag::err_verify_missing_end); |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | if (ExpectedEnd[1] == '}') |
| 142 | break; |
| 143 | |
| 144 | ++ExpectedEnd; // Skip over singular }'s |
| 145 | } |
| 146 | |
| 147 | std::string Msg(CommentStart, ExpectedEnd); |
| 148 | std::string::size_type FindPos; |
| 149 | while ((FindPos = Msg.find("\\n")) != std::string::npos) |
| 150 | Msg.replace(FindPos, 2, "\n"); |
| 151 | // Add is possibly multiple times. |
| 152 | for (int i = 0; i < Times; ++i) |
| 153 | ExpectedDiags.push_back(std::make_pair(Pos, Msg)); |
| 154 | |
| 155 | CommentStart = ExpectedEnd; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /// FindExpectedDiags - Lex the main source file to find all of the |
| 160 | // expected errors and warnings. |
| 161 | static void FindExpectedDiags(Preprocessor &PP, |
| 162 | DiagList &ExpectedErrors, |
| 163 | DiagList &ExpectedWarnings, |
| 164 | DiagList &ExpectedNotes) { |
| 165 | // Create a raw lexer to pull all the comments out of the main file. We don't |
| 166 | // want to look in #include'd headers for expected-error strings. |
Chris Lattner | 6e29014 | 2009-11-30 04:18:44 +0000 | [diff] [blame] | 167 | SourceManager &SM = PP.getSourceManager(); |
| 168 | FileID FID = SM.getMainFileID(); |
| 169 | if (SM.getMainFileID().isInvalid()) |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 170 | return; |
| 171 | |
| 172 | // Create a lexer to lex all the tokens of the main file in raw mode. |
Chris Lattner | 6e29014 | 2009-11-30 04:18:44 +0000 | [diff] [blame] | 173 | const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID); |
| 174 | Lexer RawLex(FID, FromFile, SM, PP.getLangOptions()); |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 175 | |
| 176 | // Return comments as tokens, this is how we find expected diagnostics. |
| 177 | RawLex.SetCommentRetentionState(true); |
| 178 | |
| 179 | Token Tok; |
| 180 | Tok.setKind(tok::comment); |
| 181 | while (Tok.isNot(tok::eof)) { |
| 182 | RawLex.Lex(Tok); |
| 183 | if (!Tok.is(tok::comment)) continue; |
| 184 | |
| 185 | std::string Comment = PP.getSpelling(Tok); |
| 186 | if (Comment.empty()) continue; |
| 187 | |
| 188 | // Find all expected errors. |
| 189 | FindDiagnostics(&Comment[0], Comment.size(), ExpectedErrors, PP, |
| 190 | Tok.getLocation(), "expected-error"); |
| 191 | |
| 192 | // Find all expected warnings. |
| 193 | FindDiagnostics(&Comment[0], Comment.size(), ExpectedWarnings, PP, |
| 194 | Tok.getLocation(), "expected-warning"); |
| 195 | |
| 196 | // Find all expected notes. |
| 197 | FindDiagnostics(&Comment[0], Comment.size(), ExpectedNotes, PP, |
| 198 | Tok.getLocation(), "expected-note"); |
| 199 | }; |
| 200 | } |
| 201 | |
| 202 | /// PrintProblem - This takes a diagnostic map of the delta between expected and |
| 203 | /// seen diagnostics. If there's anything in it, then something unexpected |
| 204 | /// happened. Print the map out in a nice format and return "true". If the map |
| 205 | /// is empty and we're not going to print things, then return "false". |
| 206 | /// |
Daniel Dunbar | 221c721 | 2009-11-14 07:53:24 +0000 | [diff] [blame] | 207 | static unsigned PrintProblem(Diagnostic &Diags, SourceManager *SourceMgr, |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 208 | const_diag_iterator diag_begin, |
| 209 | const_diag_iterator diag_end, |
| 210 | const char *Kind, bool Expected) { |
| 211 | if (diag_begin == diag_end) return 0; |
| 212 | |
| 213 | llvm::SmallString<256> Fmt; |
| 214 | llvm::raw_svector_ostream OS(Fmt); |
| 215 | for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I) { |
Daniel Dunbar | 221c721 | 2009-11-14 07:53:24 +0000 | [diff] [blame] | 216 | if (I->first.isInvalid() || !SourceMgr) |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 217 | OS << "\n (frontend)"; |
| 218 | else |
Daniel Dunbar | 221c721 | 2009-11-14 07:53:24 +0000 | [diff] [blame] | 219 | OS << "\n Line " << SourceMgr->getInstantiationLineNumber(I->first); |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 220 | OS << ": " << I->second; |
| 221 | } |
| 222 | |
| 223 | Diags.Report(diag::err_verify_inconsistent_diags) |
Daniel Dunbar | 221c721 | 2009-11-14 07:53:24 +0000 | [diff] [blame] | 224 | << Kind << !Expected << OS.str(); |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 225 | return std::distance(diag_begin, diag_end); |
| 226 | } |
| 227 | |
| 228 | /// CompareDiagLists - Compare two diagnostic lists and return the difference |
| 229 | /// between them. |
| 230 | /// |
| 231 | static unsigned CompareDiagLists(Diagnostic &Diags, |
| 232 | SourceManager &SourceMgr, |
| 233 | const_diag_iterator d1_begin, |
| 234 | const_diag_iterator d1_end, |
| 235 | const_diag_iterator d2_begin, |
| 236 | const_diag_iterator d2_end, |
| 237 | const char *Label) { |
| 238 | DiagList LeftOnly; |
| 239 | DiagList Left(d1_begin, d1_end); |
| 240 | DiagList Right(d2_begin, d2_end); |
| 241 | |
| 242 | for (const_diag_iterator I = Left.begin(), E = Left.end(); I != E; ++I) { |
| 243 | unsigned LineNo1 = SourceMgr.getInstantiationLineNumber(I->first); |
| 244 | const std::string &Diag1 = I->second; |
| 245 | |
| 246 | DiagList::iterator II, IE; |
| 247 | for (II = Right.begin(), IE = Right.end(); II != IE; ++II) { |
| 248 | unsigned LineNo2 = SourceMgr.getInstantiationLineNumber(II->first); |
| 249 | if (LineNo1 != LineNo2) continue; |
| 250 | |
| 251 | const std::string &Diag2 = II->second; |
| 252 | if (Diag2.find(Diag1) != std::string::npos || |
| 253 | Diag1.find(Diag2) != std::string::npos) { |
| 254 | break; |
| 255 | } |
| 256 | } |
| 257 | if (II == IE) { |
| 258 | // Not found. |
| 259 | LeftOnly.push_back(*I); |
| 260 | } else { |
| 261 | // Found. The same cannot be found twice. |
| 262 | Right.erase(II); |
| 263 | } |
| 264 | } |
| 265 | // Now all that's left in Right are those that were not matched. |
| 266 | |
Daniel Dunbar | 221c721 | 2009-11-14 07:53:24 +0000 | [diff] [blame] | 267 | return (PrintProblem(Diags, &SourceMgr, |
| 268 | LeftOnly.begin(), LeftOnly.end(), Label, true) + |
| 269 | PrintProblem(Diags, &SourceMgr, |
| 270 | Right.begin(), Right.end(), Label, false)); |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /// CheckResults - This compares the expected results to those that |
| 274 | /// were actually reported. It emits any discrepencies. Return "true" if there |
| 275 | /// were problems. Return "false" otherwise. |
| 276 | /// |
| 277 | static unsigned CheckResults(Diagnostic &Diags, SourceManager &SourceMgr, |
| 278 | const TextDiagnosticBuffer &Buffer, |
| 279 | const DiagList &ExpectedErrors, |
| 280 | const DiagList &ExpectedWarnings, |
| 281 | const DiagList &ExpectedNotes) { |
| 282 | // We want to capture the delta between what was expected and what was |
| 283 | // seen. |
| 284 | // |
| 285 | // Expected \ Seen - set expected but not seen |
| 286 | // Seen \ Expected - set seen but not expected |
| 287 | unsigned NumProblems = 0; |
| 288 | |
| 289 | // See if there are error mismatches. |
| 290 | NumProblems += CompareDiagLists(Diags, SourceMgr, |
| 291 | ExpectedErrors.begin(), ExpectedErrors.end(), |
| 292 | Buffer.err_begin(), Buffer.err_end(), |
| 293 | "error"); |
Daniel Dunbar | 221c721 | 2009-11-14 07:53:24 +0000 | [diff] [blame] | 294 | |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 295 | // See if there are warning mismatches. |
| 296 | NumProblems += CompareDiagLists(Diags, SourceMgr, |
| 297 | ExpectedWarnings.begin(), |
| 298 | ExpectedWarnings.end(), |
| 299 | Buffer.warn_begin(), Buffer.warn_end(), |
| 300 | "warning"); |
| 301 | |
| 302 | // See if there are note mismatches. |
| 303 | NumProblems += CompareDiagLists(Diags, SourceMgr, |
| 304 | ExpectedNotes.begin(), |
| 305 | ExpectedNotes.end(), |
| 306 | Buffer.note_begin(), Buffer.note_end(), |
| 307 | "note"); |
| 308 | |
| 309 | return NumProblems; |
| 310 | } |
| 311 | |
| 312 | |
| 313 | void VerifyDiagnosticsClient::CheckDiagnostics() { |
| 314 | DiagList ExpectedErrors, ExpectedWarnings, ExpectedNotes; |
| 315 | |
| 316 | // Ensure any diagnostics go to the primary client. |
Daniel Dunbar | 221c721 | 2009-11-14 07:53:24 +0000 | [diff] [blame] | 317 | DiagnosticClient *CurClient = Diags.getClient(); |
| 318 | Diags.setClient(PrimaryClient.get()); |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 319 | |
| 320 | // If we have a preprocessor, scan the source for expected diagnostic |
| 321 | // markers. If not then any diagnostics are unexpected. |
| 322 | if (CurrentPreprocessor) { |
| 323 | FindExpectedDiags(*CurrentPreprocessor, ExpectedErrors, ExpectedWarnings, |
| 324 | ExpectedNotes); |
Daniel Dunbar | 221c721 | 2009-11-14 07:53:24 +0000 | [diff] [blame] | 325 | |
| 326 | // Check that the expected diagnostics occurred. |
| 327 | NumErrors += CheckResults(Diags, CurrentPreprocessor->getSourceManager(), |
| 328 | *Buffer, |
| 329 | ExpectedErrors, ExpectedWarnings, ExpectedNotes); |
| 330 | } else { |
| 331 | NumErrors += (PrintProblem(Diags, 0, |
| 332 | Buffer->err_begin(), Buffer->err_end(), |
| 333 | "error", false) + |
| 334 | PrintProblem(Diags, 0, |
| 335 | Buffer->warn_begin(), Buffer->warn_end(), |
| 336 | "warn", false) + |
| 337 | PrintProblem(Diags, 0, |
| 338 | Buffer->note_begin(), Buffer->note_end(), |
| 339 | "note", false)); |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Daniel Dunbar | 221c721 | 2009-11-14 07:53:24 +0000 | [diff] [blame] | 342 | Diags.setClient(CurClient); |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 343 | |
| 344 | // Reset the buffer, we have processed all the diagnostics in it. |
| 345 | Buffer.reset(new TextDiagnosticBuffer()); |
| 346 | } |