1 //===---- VerifyDiagnosticConsumer.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/VerifyDiagnosticConsumer.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/Regex.h" 20 #include "llvm/Support/raw_ostream.h" 21 #include <climits> 22 23 using namespace clang; 24 25 VerifyDiagnosticConsumer::VerifyDiagnosticConsumer(DiagnosticsEngine &_Diags) 26 : Diags(_Diags), PrimaryClient(Diags.getClient()), 27 OwnsPrimaryClient(Diags.ownsClient()), 28 Buffer(new TextDiagnosticBuffer()), CurrentPreprocessor(0) 29 { 30 Diags.takeClient(); 31 } 32 33 VerifyDiagnosticConsumer::~VerifyDiagnosticConsumer() { 34 CheckDiagnostics(); 35 Diags.takeClient(); 36 if (OwnsPrimaryClient) 37 delete PrimaryClient; 38 } 39 40 // DiagnosticConsumer interface. 41 42 void VerifyDiagnosticConsumer::BeginSourceFile(const LangOptions &LangOpts, 43 const Preprocessor *PP) { 44 // FIXME: Const hack, we screw up the preprocessor but in practice its ok 45 // because it doesn't get reused. It would be better if we could make a copy 46 // though. 47 CurrentPreprocessor = const_cast<Preprocessor*>(PP); 48 49 PrimaryClient->BeginSourceFile(LangOpts, PP); 50 } 51 52 void VerifyDiagnosticConsumer::EndSourceFile() { 53 CheckDiagnostics(); 54 55 PrimaryClient->EndSourceFile(); 56 57 CurrentPreprocessor = 0; 58 } 59 60 void VerifyDiagnosticConsumer::HandleDiagnostic( 61 DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) { 62 if (FirstErrorFID.isInvalid() && Info.hasSourceManager()) { 63 const SourceManager &SM = Info.getSourceManager(); 64 FirstErrorFID = SM.getFileID(Info.getLocation()); 65 } 66 // Send the diagnostic to the buffer, we will check it once we reach the end 67 // of the source file (or are destructed). 68 Buffer->HandleDiagnostic(DiagLevel, Info); 69 } 70 71 //===----------------------------------------------------------------------===// 72 // Checking diagnostics implementation. 73 //===----------------------------------------------------------------------===// 74 75 typedef TextDiagnosticBuffer::DiagList DiagList; 76 typedef TextDiagnosticBuffer::const_iterator const_diag_iterator; 77 78 namespace { 79 80 /// Directive - Abstract class representing a parsed verify directive. 81 /// 82 class Directive { 83 public: 84 static Directive* Create(bool RegexKind, const SourceLocation &Location, 85 const std::string &Text, unsigned Count); 86 public: 87 /// Constant representing one or more matches aka regex "+". 88 static const unsigned OneOrMoreCount = UINT_MAX; 89 90 SourceLocation Location; 91 const std::string Text; 92 unsigned Count; 93 94 virtual ~Directive() { } 95 96 // Returns true if directive text is valid. 97 // Otherwise returns false and populates E. 98 virtual bool isValid(std::string &Error) = 0; 99 100 // Returns true on match. 101 virtual bool Match(const std::string &S) = 0; 102 103 protected: 104 Directive(const SourceLocation &Location, const std::string &Text, 105 unsigned Count) 106 : Location(Location), Text(Text), Count(Count) { } 107 108 private: 109 Directive(const Directive&); // DO NOT IMPLEMENT 110 void operator=(const Directive&); // DO NOT IMPLEMENT 111 }; 112 113 /// StandardDirective - Directive with string matching. 114 /// 115 class StandardDirective : public Directive { 116 public: 117 StandardDirective(const SourceLocation &Location, const std::string &Text, 118 unsigned Count) 119 : Directive(Location, Text, Count) { } 120 121 virtual bool isValid(std::string &Error) { 122 // all strings are considered valid; even empty ones 123 return true; 124 } 125 126 virtual bool Match(const std::string &S) { 127 return S.find(Text) != std::string::npos; 128 } 129 }; 130 131 /// RegexDirective - Directive with regular-expression matching. 132 /// 133 class RegexDirective : public Directive { 134 public: 135 RegexDirective(const SourceLocation &Location, const std::string &Text, 136 unsigned Count) 137 : Directive(Location, Text, Count), Regex(Text) { } 138 139 virtual bool isValid(std::string &Error) { 140 if (Regex.isValid(Error)) 141 return true; 142 return false; 143 } 144 145 virtual bool Match(const std::string &S) { 146 return Regex.match(S); 147 } 148 149 private: 150 llvm::Regex Regex; 151 }; 152 153 typedef std::vector<Directive*> DirectiveList; 154 155 /// ExpectedData - owns directive objects and deletes on destructor. 156 /// 157 struct ExpectedData { 158 DirectiveList Errors; 159 DirectiveList Warnings; 160 DirectiveList Notes; 161 162 ~ExpectedData() { 163 DirectiveList* Lists[] = { &Errors, &Warnings, &Notes, 0 }; 164 for (DirectiveList **PL = Lists; *PL; ++PL) { 165 DirectiveList * const L = *PL; 166 for (DirectiveList::iterator I = L->begin(), E = L->end(); I != E; ++I) 167 delete *I; 168 } 169 } 170 }; 171 172 class ParseHelper 173 { 174 public: 175 ParseHelper(const char *Begin, const char *End) 176 : Begin(Begin), End(End), C(Begin), P(Begin), PEnd(NULL) { } 177 178 // Return true if string literal is next. 179 bool Next(StringRef S) { 180 P = C; 181 PEnd = C + S.size(); 182 if (PEnd > End) 183 return false; 184 return !memcmp(P, S.data(), S.size()); 185 } 186 187 // Return true if number is next. 188 // Output N only if number is next. 189 bool Next(unsigned &N) { 190 unsigned TMP = 0; 191 P = C; 192 for (; P < End && P[0] >= '0' && P[0] <= '9'; ++P) { 193 TMP *= 10; 194 TMP += P[0] - '0'; 195 } 196 if (P == C) 197 return false; 198 PEnd = P; 199 N = TMP; 200 return true; 201 } 202 203 // Return true if string literal is found. 204 // When true, P marks begin-position of S in content. 205 bool Search(StringRef S) { 206 P = std::search(C, End, S.begin(), S.end()); 207 PEnd = P + S.size(); 208 return P != End; 209 } 210 211 // Advance 1-past previous next/search. 212 // Behavior is undefined if previous next/search failed. 213 bool Advance() { 214 C = PEnd; 215 return C < End; 216 } 217 218 // Skip zero or more whitespace. 219 void SkipWhitespace() { 220 for (; C < End && isspace(*C); ++C) 221 ; 222 } 223 224 // Return true if EOF reached. 225 bool Done() { 226 return !(C < End); 227 } 228 229 const char * const Begin; // beginning of expected content 230 const char * const End; // end of expected content (1-past) 231 const char *C; // position of next char in content 232 const char *P; 233 234 private: 235 const char *PEnd; // previous next/search subject end (1-past) 236 }; 237 238 } // namespace anonymous 239 240 /// ParseDirective - Go through the comment and see if it indicates expected 241 /// diagnostics. If so, then put them in the appropriate directive list. 242 /// 243 static void ParseDirective(const char *CommentStart, unsigned CommentLen, 244 ExpectedData &ED, Preprocessor &PP, 245 SourceLocation Pos) { 246 // A single comment may contain multiple directives. 247 for (ParseHelper PH(CommentStart, CommentStart+CommentLen); !PH.Done();) { 248 // search for token: expected 249 if (!PH.Search("expected")) 250 break; 251 PH.Advance(); 252 253 // next token: - 254 if (!PH.Next("-")) 255 continue; 256 PH.Advance(); 257 258 // next token: { error | warning | note } 259 DirectiveList* DL = NULL; 260 if (PH.Next("error")) 261 DL = &ED.Errors; 262 else if (PH.Next("warning")) 263 DL = &ED.Warnings; 264 else if (PH.Next("note")) 265 DL = &ED.Notes; 266 else 267 continue; 268 PH.Advance(); 269 270 // default directive kind 271 bool RegexKind = false; 272 const char* KindStr = "string"; 273 274 // next optional token: - 275 if (PH.Next("-re")) { 276 PH.Advance(); 277 RegexKind = true; 278 KindStr = "regex"; 279 } 280 281 // skip optional whitespace 282 PH.SkipWhitespace(); 283 284 // next optional token: positive integer or a '+'. 285 unsigned Count = 1; 286 if (PH.Next(Count)) 287 PH.Advance(); 288 else if (PH.Next("+")) { 289 Count = Directive::OneOrMoreCount; 290 PH.Advance(); 291 } 292 293 // skip optional whitespace 294 PH.SkipWhitespace(); 295 296 // next token: {{ 297 if (!PH.Next("{{")) { 298 PP.Diag(Pos.getLocWithOffset(PH.C-PH.Begin), 299 diag::err_verify_missing_start) << KindStr; 300 continue; 301 } 302 PH.Advance(); 303 const char* const ContentBegin = PH.C; // mark content begin 304 305 // search for token: }} 306 if (!PH.Search("}}")) { 307 PP.Diag(Pos.getLocWithOffset(PH.C-PH.Begin), 308 diag::err_verify_missing_end) << KindStr; 309 continue; 310 } 311 const char* const ContentEnd = PH.P; // mark content end 312 PH.Advance(); 313 314 // build directive text; convert \n to newlines 315 std::string Text; 316 StringRef NewlineStr = "\\n"; 317 StringRef Content(ContentBegin, ContentEnd-ContentBegin); 318 size_t CPos = 0; 319 size_t FPos; 320 while ((FPos = Content.find(NewlineStr, CPos)) != StringRef::npos) { 321 Text += Content.substr(CPos, FPos-CPos); 322 Text += '\n'; 323 CPos = FPos + NewlineStr.size(); 324 } 325 if (Text.empty()) 326 Text.assign(ContentBegin, ContentEnd); 327 328 // construct new directive 329 Directive *D = Directive::Create(RegexKind, Pos, Text, Count); 330 std::string Error; 331 if (D->isValid(Error)) 332 DL->push_back(D); 333 else { 334 PP.Diag(Pos.getLocWithOffset(ContentBegin-PH.Begin), 335 diag::err_verify_invalid_content) 336 << KindStr << Error; 337 } 338 } 339 } 340 341 /// FindExpectedDiags - Lex the main source file to find all of the 342 // expected errors and warnings. 343 static void FindExpectedDiags(Preprocessor &PP, ExpectedData &ED, FileID FID) { 344 // Create a raw lexer to pull all the comments out of FID. 345 if (FID.isInvalid()) 346 return; 347 348 SourceManager& SM = PP.getSourceManager(); 349 // Create a lexer to lex all the tokens of the main file in raw mode. 350 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID); 351 Lexer RawLex(FID, FromFile, SM, PP.getLangOptions()); 352 353 // Return comments as tokens, this is how we find expected diagnostics. 354 RawLex.SetCommentRetentionState(true); 355 356 Token Tok; 357 Tok.setKind(tok::comment); 358 while (Tok.isNot(tok::eof)) { 359 RawLex.Lex(Tok); 360 if (!Tok.is(tok::comment)) continue; 361 362 std::string Comment = PP.getSpelling(Tok); 363 if (Comment.empty()) continue; 364 365 // Find all expected errors/warnings/notes. 366 ParseDirective(&Comment[0], Comment.size(), ED, PP, Tok.getLocation()); 367 }; 368 } 369 370 /// PrintProblem - This takes a diagnostic map of the delta between expected and 371 /// seen diagnostics. If there's anything in it, then something unexpected 372 /// happened. Print the map out in a nice format and return "true". If the map 373 /// is empty and we're not going to print things, then return "false". 374 /// 375 static unsigned PrintProblem(DiagnosticsEngine &Diags, SourceManager *SourceMgr, 376 const_diag_iterator diag_begin, 377 const_diag_iterator diag_end, 378 const char *Kind, bool Expected) { 379 if (diag_begin == diag_end) return 0; 380 381 llvm::SmallString<256> Fmt; 382 llvm::raw_svector_ostream OS(Fmt); 383 for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I) { 384 if (I->first.isInvalid() || !SourceMgr) 385 OS << "\n (frontend)"; 386 else 387 OS << "\n Line " << SourceMgr->getPresumedLineNumber(I->first); 388 OS << ": " << I->second; 389 } 390 391 Diags.Report(diag::err_verify_inconsistent_diags) 392 << Kind << !Expected << OS.str(); 393 return std::distance(diag_begin, diag_end); 394 } 395 396 static unsigned PrintProblem(DiagnosticsEngine &Diags, SourceManager *SourceMgr, 397 DirectiveList &DL, const char *Kind, 398 bool Expected) { 399 if (DL.empty()) 400 return 0; 401 402 llvm::SmallString<256> Fmt; 403 llvm::raw_svector_ostream OS(Fmt); 404 for (DirectiveList::iterator I = DL.begin(), E = DL.end(); I != E; ++I) { 405 Directive& D = **I; 406 if (D.Location.isInvalid() || !SourceMgr) 407 OS << "\n (frontend)"; 408 else 409 OS << "\n Line " << SourceMgr->getPresumedLineNumber(D.Location); 410 OS << ": " << D.Text; 411 } 412 413 Diags.Report(diag::err_verify_inconsistent_diags) 414 << Kind << !Expected << OS.str(); 415 return DL.size(); 416 } 417 418 /// CheckLists - Compare expected to seen diagnostic lists and return the 419 /// the difference between them. 420 /// 421 static unsigned CheckLists(DiagnosticsEngine &Diags, SourceManager &SourceMgr, 422 const char *Label, 423 DirectiveList &Left, 424 const_diag_iterator d2_begin, 425 const_diag_iterator d2_end) { 426 DirectiveList LeftOnly; 427 DiagList Right(d2_begin, d2_end); 428 429 for (DirectiveList::iterator I = Left.begin(), E = Left.end(); I != E; ++I) { 430 Directive& D = **I; 431 unsigned LineNo1 = SourceMgr.getPresumedLineNumber(D.Location); 432 bool FoundOnce = false; 433 434 for (unsigned i = 0; i < D.Count; ++i) { 435 DiagList::iterator II, IE; 436 for (II = Right.begin(), IE = Right.end(); II != IE; ++II) { 437 unsigned LineNo2 = SourceMgr.getPresumedLineNumber(II->first); 438 if (LineNo1 != LineNo2) 439 continue; 440 441 const std::string &RightText = II->second; 442 if (D.Match(RightText)) 443 break; 444 } 445 if (II == IE) { 446 if (D.Count == D.OneOrMoreCount) { 447 if (!FoundOnce) 448 LeftOnly.push_back(*I); 449 // We are only interested in at least one match, so exit the loop. 450 break; 451 } 452 // Not found. 453 LeftOnly.push_back(*I); 454 } else { 455 // Found. The same cannot be found twice. 456 Right.erase(II); 457 FoundOnce = true; 458 } 459 } 460 } 461 // Now all that's left in Right are those that were not matched. 462 unsigned num = PrintProblem(Diags, &SourceMgr, LeftOnly, Label, true); 463 num += PrintProblem(Diags, &SourceMgr, Right.begin(), Right.end(), 464 Label, false); 465 return num; 466 } 467 468 /// CheckResults - This compares the expected results to those that 469 /// were actually reported. It emits any discrepencies. Return "true" if there 470 /// were problems. Return "false" otherwise. 471 /// 472 static unsigned CheckResults(DiagnosticsEngine &Diags, SourceManager &SourceMgr, 473 const TextDiagnosticBuffer &Buffer, 474 ExpectedData &ED) { 475 // We want to capture the delta between what was expected and what was 476 // seen. 477 // 478 // Expected \ Seen - set expected but not seen 479 // Seen \ Expected - set seen but not expected 480 unsigned NumProblems = 0; 481 482 // See if there are error mismatches. 483 NumProblems += CheckLists(Diags, SourceMgr, "error", ED.Errors, 484 Buffer.err_begin(), Buffer.err_end()); 485 486 // See if there are warning mismatches. 487 NumProblems += CheckLists(Diags, SourceMgr, "warning", ED.Warnings, 488 Buffer.warn_begin(), Buffer.warn_end()); 489 490 // See if there are note mismatches. 491 NumProblems += CheckLists(Diags, SourceMgr, "note", ED.Notes, 492 Buffer.note_begin(), Buffer.note_end()); 493 494 return NumProblems; 495 } 496 497 void VerifyDiagnosticConsumer::CheckDiagnostics() { 498 ExpectedData ED; 499 500 // Ensure any diagnostics go to the primary client. 501 bool OwnsCurClient = Diags.ownsClient(); 502 DiagnosticConsumer *CurClient = Diags.takeClient(); 503 Diags.setClient(PrimaryClient, false); 504 505 // If we have a preprocessor, scan the source for expected diagnostic 506 // markers. If not then any diagnostics are unexpected. 507 if (CurrentPreprocessor) { 508 SourceManager &SM = CurrentPreprocessor->getSourceManager(); 509 // Extract expected-error strings from main file. 510 FindExpectedDiags(*CurrentPreprocessor, ED, SM.getMainFileID()); 511 // Only check for expectations in other diagnostic locations 512 // if they are not the main file (via ID or FileEntry) - the main 513 // file has already been looked at, and its expectations must not 514 // be added twice. 515 if (!FirstErrorFID.isInvalid() && FirstErrorFID != SM.getMainFileID() 516 && (!SM.getFileEntryForID(FirstErrorFID) 517 || (SM.getFileEntryForID(FirstErrorFID) != 518 SM.getFileEntryForID(SM.getMainFileID())))) { 519 FindExpectedDiags(*CurrentPreprocessor, ED, FirstErrorFID); 520 FirstErrorFID = FileID(); 521 } 522 523 // Check that the expected diagnostics occurred. 524 NumErrors += CheckResults(Diags, SM, *Buffer, ED); 525 } else { 526 NumErrors += (PrintProblem(Diags, 0, 527 Buffer->err_begin(), Buffer->err_end(), 528 "error", false) + 529 PrintProblem(Diags, 0, 530 Buffer->warn_begin(), Buffer->warn_end(), 531 "warn", false) + 532 PrintProblem(Diags, 0, 533 Buffer->note_begin(), Buffer->note_end(), 534 "note", false)); 535 } 536 537 Diags.takeClient(); 538 Diags.setClient(CurClient, OwnsCurClient); 539 540 // Reset the buffer, we have processed all the diagnostics in it. 541 Buffer.reset(new TextDiagnosticBuffer()); 542 } 543 544 DiagnosticConsumer * 545 VerifyDiagnosticConsumer::clone(DiagnosticsEngine &Diags) const { 546 if (!Diags.getClient()) 547 Diags.setClient(PrimaryClient->clone(Diags)); 548 549 return new VerifyDiagnosticConsumer(Diags); 550 } 551 552 Directive* Directive::Create(bool RegexKind, const SourceLocation &Location, 553 const std::string &Text, unsigned Count) { 554 if (RegexKind) 555 return new RegexDirective(Location, Text, Count); 556 return new StandardDirective(Location, Text, Count); 557 } 558