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