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