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" |
Eli Friedman | 4bf34d1 | 2011-12-15 04:24:37 +0000 | [diff] [blame] | 21 | #include <climits> |
Anna Zaks | c035e09 | 2011-12-15 02:58:00 +0000 | [diff] [blame] | 22 | |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | |
David Blaikie | 621bc69 | 2011-09-26 00:38:03 +0000 | [diff] [blame] | 25 | VerifyDiagnosticConsumer::VerifyDiagnosticConsumer(DiagnosticsEngine &_Diags) |
Douglas Gregor | 7824365 | 2011-09-13 01:26:44 +0000 | [diff] [blame] | 26 | : Diags(_Diags), PrimaryClient(Diags.getClient()), |
| 27 | OwnsPrimaryClient(Diags.ownsClient()), |
| 28 | Buffer(new TextDiagnosticBuffer()), CurrentPreprocessor(0) |
| 29 | { |
| 30 | Diags.takeClient(); |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 31 | } |
| 32 | |
David Blaikie | 621bc69 | 2011-09-26 00:38:03 +0000 | [diff] [blame] | 33 | VerifyDiagnosticConsumer::~VerifyDiagnosticConsumer() { |
Douglas Gregor | 7824365 | 2011-09-13 01:26:44 +0000 | [diff] [blame] | 34 | CheckDiagnostics(); |
| 35 | Diags.takeClient(); |
| 36 | if (OwnsPrimaryClient) |
| 37 | delete PrimaryClient; |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 38 | } |
| 39 | |
David Blaikie | 78ad0b9 | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 40 | // DiagnosticConsumer interface. |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 41 | |
David Blaikie | 621bc69 | 2011-09-26 00:38:03 +0000 | [diff] [blame] | 42 | void VerifyDiagnosticConsumer::BeginSourceFile(const LangOptions &LangOpts, |
Douglas Gregor | 1f6b2b5 | 2012-01-20 16:28:04 +0000 | [diff] [blame^] | 43 | const Preprocessor *PP) { |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 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); |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 48 | |
| 49 | PrimaryClient->BeginSourceFile(LangOpts, PP); |
| 50 | } |
| 51 | |
David Blaikie | 621bc69 | 2011-09-26 00:38:03 +0000 | [diff] [blame] | 52 | void VerifyDiagnosticConsumer::EndSourceFile() { |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 53 | CheckDiagnostics(); |
| 54 | |
| 55 | PrimaryClient->EndSourceFile(); |
| 56 | |
| 57 | CurrentPreprocessor = 0; |
| 58 | } |
Daniel Dunbar | 221c721 | 2009-11-14 07:53:24 +0000 | [diff] [blame] | 59 | |
David Blaikie | 621bc69 | 2011-09-26 00:38:03 +0000 | [diff] [blame] | 60 | void VerifyDiagnosticConsumer::HandleDiagnostic( |
David Blaikie | 40847cf | 2011-09-26 01:18:08 +0000 | [diff] [blame] | 61 | DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) { |
Axel Naumann | 0123161 | 2011-07-25 19:18:12 +0000 | [diff] [blame] | 62 | if (FirstErrorFID.isInvalid() && Info.hasSourceManager()) { |
| 63 | const SourceManager &SM = Info.getSourceManager(); |
| 64 | FirstErrorFID = SM.getFileID(Info.getLocation()); |
| 65 | } |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 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 | |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 71 | //===----------------------------------------------------------------------===// |
| 72 | // Checking diagnostics implementation. |
| 73 | //===----------------------------------------------------------------------===// |
| 74 | |
| 75 | typedef TextDiagnosticBuffer::DiagList DiagList; |
| 76 | typedef TextDiagnosticBuffer::const_iterator const_diag_iterator; |
| 77 | |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 78 | namespace { |
| 79 | |
| 80 | /// Directive - Abstract class representing a parsed verify directive. |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 81 | /// |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 82 | class Directive { |
| 83 | public: |
| 84 | static Directive* Create(bool RegexKind, const SourceLocation &Location, |
| 85 | const std::string &Text, unsigned Count); |
| 86 | public: |
Anna Zaks | 2135ebb | 2011-12-15 02:28:16 +0000 | [diff] [blame] | 87 | /// Constant representing one or more matches aka regex "+". |
| 88 | static const unsigned OneOrMoreCount = UINT_MAX; |
| 89 | |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 90 | SourceLocation Location; |
| 91 | const std::string Text; |
| 92 | unsigned Count; |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 93 | |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 94 | virtual ~Directive() { } |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 95 | |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 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: |
Argyrios Kyrtzidis | c83d2d7 | 2010-08-15 10:17:39 +0000 | [diff] [blame] | 109 | Directive(const Directive&); // DO NOT IMPLEMENT |
| 110 | void operator=(const Directive&); // DO NOT IMPLEMENT |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 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) { |
Richard Trieu | 2fe9b7f | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 127 | return S.find(Text) != std::string::npos; |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 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. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 179 | bool Next(StringRef S) { |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 180 | P = C; |
Benjamin Kramer | 0080f0c | 2010-09-01 17:28:48 +0000 | [diff] [blame] | 181 | PEnd = C + S.size(); |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 182 | if (PEnd > End) |
| 183 | return false; |
Benjamin Kramer | 0080f0c | 2010-09-01 17:28:48 +0000 | [diff] [blame] | 184 | return !memcmp(P, S.data(), S.size()); |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 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. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 205 | bool Search(StringRef S) { |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 206 | P = std::search(C, End, S.begin(), S.end()); |
Benjamin Kramer | 0080f0c | 2010-09-01 17:28:48 +0000 | [diff] [blame] | 207 | PEnd = P + S.size(); |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 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 | |
Anna Zaks | 2135ebb | 2011-12-15 02:28:16 +0000 | [diff] [blame] | 284 | // next optional token: positive integer or a '+'. |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 285 | unsigned Count = 1; |
| 286 | if (PH.Next(Count)) |
| 287 | PH.Advance(); |
Anna Zaks | 2135ebb | 2011-12-15 02:28:16 +0000 | [diff] [blame] | 288 | else if (PH.Next("+")) { |
| 289 | Count = Directive::OneOrMoreCount; |
| 290 | PH.Advance(); |
| 291 | } |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 292 | |
| 293 | // skip optional whitespace |
| 294 | PH.SkipWhitespace(); |
| 295 | |
| 296 | // next token: {{ |
| 297 | if (!PH.Next("{{")) { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 298 | PP.Diag(Pos.getLocWithOffset(PH.C-PH.Begin), |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 305 | // search for token: }} |
| 306 | if (!PH.Search("}}")) { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 307 | PP.Diag(Pos.getLocWithOffset(PH.C-PH.Begin), |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 308 | diag::err_verify_missing_end) << KindStr; |
| 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 | |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 314 | // build directive text; convert \n to newlines |
| 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 | |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 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 { |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 334 | PP.Diag(Pos.getLocWithOffset(ContentBegin-PH.Begin), |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 335 | diag::err_verify_invalid_content) |
| 336 | << KindStr << Error; |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 337 | } |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
| 341 | /// FindExpectedDiags - Lex the main source file to find all of the |
| 342 | // expected errors and warnings. |
Axel Naumann | 0123161 | 2011-07-25 19:18:12 +0000 | [diff] [blame] | 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()) |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 346 | return; |
| 347 | |
Axel Naumann | 0123161 | 2011-07-25 19:18:12 +0000 | [diff] [blame] | 348 | SourceManager& SM = PP.getSourceManager(); |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 349 | // 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] | 350 | const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID); |
| 351 | Lexer RawLex(FID, FromFile, SM, PP.getLangOptions()); |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 365 | // Find all expected errors/warnings/notes. |
| 366 | ParseDirective(&Comment[0], Comment.size(), ED, PP, Tok.getLocation()); |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 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 | /// |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 375 | static unsigned PrintProblem(DiagnosticsEngine &Diags, SourceManager *SourceMgr, |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 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) { |
Daniel Dunbar | 221c721 | 2009-11-14 07:53:24 +0000 | [diff] [blame] | 384 | if (I->first.isInvalid() || !SourceMgr) |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 385 | OS << "\n (frontend)"; |
| 386 | else |
Chandler Carruth | 5ef04ee | 2011-02-23 00:47:48 +0000 | [diff] [blame] | 387 | OS << "\n Line " << SourceMgr->getPresumedLineNumber(I->first); |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 388 | OS << ": " << I->second; |
| 389 | } |
| 390 | |
| 391 | Diags.Report(diag::err_verify_inconsistent_diags) |
Daniel Dunbar | 221c721 | 2009-11-14 07:53:24 +0000 | [diff] [blame] | 392 | << Kind << !Expected << OS.str(); |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 393 | return std::distance(diag_begin, diag_end); |
| 394 | } |
| 395 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 396 | static unsigned PrintProblem(DiagnosticsEngine &Diags, SourceManager *SourceMgr, |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 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 |
Chandler Carruth | 5ef04ee | 2011-02-23 00:47:48 +0000 | [diff] [blame] | 409 | OS << "\n Line " << SourceMgr->getPresumedLineNumber(D.Location); |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 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. |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 420 | /// |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 421 | static unsigned CheckLists(DiagnosticsEngine &Diags, SourceManager &SourceMgr, |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 422 | const char *Label, |
| 423 | DirectiveList &Left, |
| 424 | const_diag_iterator d2_begin, |
| 425 | const_diag_iterator d2_end) { |
| 426 | DirectiveList LeftOnly; |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 427 | DiagList Right(d2_begin, d2_end); |
| 428 | |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 429 | for (DirectiveList::iterator I = Left.begin(), E = Left.end(); I != E; ++I) { |
| 430 | Directive& D = **I; |
Chandler Carruth | 5ef04ee | 2011-02-23 00:47:48 +0000 | [diff] [blame] | 431 | unsigned LineNo1 = SourceMgr.getPresumedLineNumber(D.Location); |
Anna Zaks | 2135ebb | 2011-12-15 02:28:16 +0000 | [diff] [blame] | 432 | bool FoundOnce = false; |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 433 | |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 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) { |
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; |
| 442 | if (D.Match(RightText)) |
| 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) { |
Anna Zaks | 0e818a4 | 2011-12-16 18:28:45 +0000 | [diff] [blame] | 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. |
Anna Zaks | 2135ebb | 2011-12-15 02:28:16 +0000 | [diff] [blame] | 450 | break; |
| 451 | } |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 452 | // Not found. |
| 453 | LeftOnly.push_back(*I); |
| 454 | } else { |
| 455 | // Found. The same cannot be found twice. |
| 456 | Right.erase(II); |
Anna Zaks | 2135ebb | 2011-12-15 02:28:16 +0000 | [diff] [blame] | 457 | FoundOnce = true; |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 458 | } |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | // Now all that's left in Right are those that were not matched. |
NAKAMURA Takumi | ad64684 | 2011-12-17 13:00:31 +0000 | [diff] [blame] | 462 | unsigned num = PrintProblem(Diags, &SourceMgr, LeftOnly, Label, true); |
| 463 | num += PrintProblem(Diags, &SourceMgr, Right.begin(), Right.end(), |
| 464 | Label, false); |
| 465 | return num; |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 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 | /// |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 472 | static unsigned CheckResults(DiagnosticsEngine &Diags, SourceManager &SourceMgr, |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 473 | const TextDiagnosticBuffer &Buffer, |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 474 | ExpectedData &ED) { |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 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. |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 483 | NumProblems += CheckLists(Diags, SourceMgr, "error", ED.Errors, |
| 484 | Buffer.err_begin(), Buffer.err_end()); |
Daniel Dunbar | 221c721 | 2009-11-14 07:53:24 +0000 | [diff] [blame] | 485 | |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 486 | // See if there are warning mismatches. |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 487 | NumProblems += CheckLists(Diags, SourceMgr, "warning", ED.Warnings, |
| 488 | Buffer.warn_begin(), Buffer.warn_end()); |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 489 | |
| 490 | // See if there are note mismatches. |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 491 | NumProblems += CheckLists(Diags, SourceMgr, "note", ED.Notes, |
| 492 | Buffer.note_begin(), Buffer.note_end()); |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 493 | |
| 494 | return NumProblems; |
| 495 | } |
| 496 | |
David Blaikie | 621bc69 | 2011-09-26 00:38:03 +0000 | [diff] [blame] | 497 | void VerifyDiagnosticConsumer::CheckDiagnostics() { |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 498 | ExpectedData ED; |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 499 | |
| 500 | // Ensure any diagnostics go to the primary client. |
Douglas Gregor | 7824365 | 2011-09-13 01:26:44 +0000 | [diff] [blame] | 501 | bool OwnsCurClient = Diags.ownsClient(); |
David Blaikie | 78ad0b9 | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 502 | DiagnosticConsumer *CurClient = Diags.takeClient(); |
Douglas Gregor | 7824365 | 2011-09-13 01:26:44 +0000 | [diff] [blame] | 503 | Diags.setClient(PrimaryClient, false); |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 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) { |
Axel Naumann | 0123161 | 2011-07-25 19:18:12 +0000 | [diff] [blame] | 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) != |
Axel Naumann | 84c05e3 | 2011-08-24 13:36:19 +0000 | [diff] [blame] | 518 | SM.getFileEntryForID(SM.getMainFileID())))) { |
Axel Naumann | 0123161 | 2011-07-25 19:18:12 +0000 | [diff] [blame] | 519 | FindExpectedDiags(*CurrentPreprocessor, ED, FirstErrorFID); |
Axel Naumann | 84c05e3 | 2011-08-24 13:36:19 +0000 | [diff] [blame] | 520 | FirstErrorFID = FileID(); |
| 521 | } |
Daniel Dunbar | 221c721 | 2009-11-14 07:53:24 +0000 | [diff] [blame] | 522 | |
| 523 | // Check that the expected diagnostics occurred. |
Axel Naumann | 0123161 | 2011-07-25 19:18:12 +0000 | [diff] [blame] | 524 | NumErrors += CheckResults(Diags, SM, *Buffer, ED); |
Daniel Dunbar | 221c721 | 2009-11-14 07:53:24 +0000 | [diff] [blame] | 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)); |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Douglas Gregor | bdbb004 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 537 | Diags.takeClient(); |
Douglas Gregor | 7824365 | 2011-09-13 01:26:44 +0000 | [diff] [blame] | 538 | Diags.setClient(CurClient, OwnsCurClient); |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 539 | |
| 540 | // Reset the buffer, we have processed all the diagnostics in it. |
| 541 | Buffer.reset(new TextDiagnosticBuffer()); |
| 542 | } |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 543 | |
Douglas Gregor | aee526e | 2011-09-29 00:38:00 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 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 | } |