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