VerifyDiagnosticConsumer.cpp revision 4bf34d19b80bbcd26519b6ab32d17d876c8b8dcc
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 23using namespace clang; 24 25VerifyDiagnosticConsumer::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 33VerifyDiagnosticConsumer::~VerifyDiagnosticConsumer() { 34 CheckDiagnostics(); 35 Diags.takeClient(); 36 if (OwnsPrimaryClient) 37 delete PrimaryClient; 38} 39 40// DiagnosticConsumer interface. 41 42void 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 52void VerifyDiagnosticConsumer::EndSourceFile() { 53 CheckDiagnostics(); 54 55 PrimaryClient->EndSourceFile(); 56 57 CurrentPreprocessor = 0; 58} 59 60void 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 75typedef TextDiagnosticBuffer::DiagList DiagList; 76typedef TextDiagnosticBuffer::const_iterator const_diag_iterator; 77 78namespace { 79 80/// Directive - Abstract class representing a parsed verify directive. 81/// 82class Directive { 83public: 84 static Directive* Create(bool RegexKind, const SourceLocation &Location, 85 const std::string &Text, unsigned Count); 86public: 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 103protected: 104 Directive(const SourceLocation &Location, const std::string &Text, 105 unsigned Count) 106 : Location(Location), Text(Text), Count(Count) { } 107 108private: 109 Directive(const Directive&); // DO NOT IMPLEMENT 110 void operator=(const Directive&); // DO NOT IMPLEMENT 111}; 112 113/// StandardDirective - Directive with string matching. 114/// 115class StandardDirective : public Directive { 116public: 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/// 133class RegexDirective : public Directive { 134public: 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 149private: 150 llvm::Regex Regex; 151}; 152 153typedef std::vector<Directive*> DirectiveList; 154 155/// ExpectedData - owns directive objects and deletes on destructor. 156/// 157struct 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 172class ParseHelper 173{ 174public: 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 234private: 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/// 243static 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. 343static 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/// 375static 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 396static 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/// 421static 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 && FoundOnce) { 447 // We are only interested in at least one match and we found one. 448 break; 449 } 450 // Not found. 451 LeftOnly.push_back(*I); 452 } else { 453 // Found. The same cannot be found twice. 454 Right.erase(II); 455 FoundOnce = true; 456 } 457 } 458 } 459 // Now all that's left in Right are those that were not matched. 460 461 return (PrintProblem(Diags, &SourceMgr, LeftOnly, Label, true) + 462 PrintProblem(Diags, &SourceMgr, Right.begin(), Right.end(), 463 Label, false)); 464} 465 466/// CheckResults - This compares the expected results to those that 467/// were actually reported. It emits any discrepencies. Return "true" if there 468/// were problems. Return "false" otherwise. 469/// 470static unsigned CheckResults(DiagnosticsEngine &Diags, SourceManager &SourceMgr, 471 const TextDiagnosticBuffer &Buffer, 472 ExpectedData &ED) { 473 // We want to capture the delta between what was expected and what was 474 // seen. 475 // 476 // Expected \ Seen - set expected but not seen 477 // Seen \ Expected - set seen but not expected 478 unsigned NumProblems = 0; 479 480 // See if there are error mismatches. 481 NumProblems += CheckLists(Diags, SourceMgr, "error", ED.Errors, 482 Buffer.err_begin(), Buffer.err_end()); 483 484 // See if there are warning mismatches. 485 NumProblems += CheckLists(Diags, SourceMgr, "warning", ED.Warnings, 486 Buffer.warn_begin(), Buffer.warn_end()); 487 488 // See if there are note mismatches. 489 NumProblems += CheckLists(Diags, SourceMgr, "note", ED.Notes, 490 Buffer.note_begin(), Buffer.note_end()); 491 492 return NumProblems; 493} 494 495void VerifyDiagnosticConsumer::CheckDiagnostics() { 496 ExpectedData ED; 497 498 // Ensure any diagnostics go to the primary client. 499 bool OwnsCurClient = Diags.ownsClient(); 500 DiagnosticConsumer *CurClient = Diags.takeClient(); 501 Diags.setClient(PrimaryClient, false); 502 503 // If we have a preprocessor, scan the source for expected diagnostic 504 // markers. If not then any diagnostics are unexpected. 505 if (CurrentPreprocessor) { 506 SourceManager &SM = CurrentPreprocessor->getSourceManager(); 507 // Extract expected-error strings from main file. 508 FindExpectedDiags(*CurrentPreprocessor, ED, SM.getMainFileID()); 509 // Only check for expectations in other diagnostic locations 510 // if they are not the main file (via ID or FileEntry) - the main 511 // file has already been looked at, and its expectations must not 512 // be added twice. 513 if (!FirstErrorFID.isInvalid() && FirstErrorFID != SM.getMainFileID() 514 && (!SM.getFileEntryForID(FirstErrorFID) 515 || (SM.getFileEntryForID(FirstErrorFID) != 516 SM.getFileEntryForID(SM.getMainFileID())))) { 517 FindExpectedDiags(*CurrentPreprocessor, ED, FirstErrorFID); 518 FirstErrorFID = FileID(); 519 } 520 521 // Check that the expected diagnostics occurred. 522 NumErrors += CheckResults(Diags, SM, *Buffer, ED); 523 } else { 524 NumErrors += (PrintProblem(Diags, 0, 525 Buffer->err_begin(), Buffer->err_end(), 526 "error", false) + 527 PrintProblem(Diags, 0, 528 Buffer->warn_begin(), Buffer->warn_end(), 529 "warn", false) + 530 PrintProblem(Diags, 0, 531 Buffer->note_begin(), Buffer->note_end(), 532 "note", false)); 533 } 534 535 Diags.takeClient(); 536 Diags.setClient(CurClient, OwnsCurClient); 537 538 // Reset the buffer, we have processed all the diagnostics in it. 539 Buffer.reset(new TextDiagnosticBuffer()); 540} 541 542DiagnosticConsumer * 543VerifyDiagnosticConsumer::clone(DiagnosticsEngine &Diags) const { 544 if (!Diags.getClient()) 545 Diags.setClient(PrimaryClient->clone(Diags)); 546 547 return new VerifyDiagnosticConsumer(Diags); 548} 549 550Directive* Directive::Create(bool RegexKind, const SourceLocation &Location, 551 const std::string &Text, unsigned Count) { 552 if (RegexKind) 553 return new RegexDirective(Location, Text, Count); 554 return new StandardDirective(Location, Text, Count); 555} 556