| Eugene Zelenko | 4f23318 | 2018-03-22 00:53:26 +0000 | [diff] [blame] | 1 | //===- VerifyDiagnosticConsumer.cpp - Verifying Diagnostic Client ---------===// |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 2 | // |
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This is a concrete diagnostic client, which buffers the diagnostic messages. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| David Blaikie | 69609dc | 2011-09-26 00:38:03 +0000 | [diff] [blame] | 13 | #include "clang/Frontend/VerifyDiagnosticConsumer.h" |
| Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 14 | #include "clang/Basic/CharInfo.h" |
| Eugene Zelenko | 4f23318 | 2018-03-22 00:53:26 +0000 | [diff] [blame] | 15 | #include "clang/Basic/Diagnostic.h" |
| 16 | #include "clang/Basic/DiagnosticOptions.h" |
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 17 | #include "clang/Basic/FileManager.h" |
| Eugene Zelenko | 4f23318 | 2018-03-22 00:53:26 +0000 | [diff] [blame] | 18 | #include "clang/Basic/LLVM.h" |
| 19 | #include "clang/Basic/SourceLocation.h" |
| 20 | #include "clang/Basic/SourceManager.h" |
| 21 | #include "clang/Basic/TokenKinds.h" |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 22 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 23 | #include "clang/Frontend/TextDiagnosticBuffer.h" |
| Jordan Rose | b00073d | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 24 | #include "clang/Lex/HeaderSearch.h" |
| Eugene Zelenko | 4f23318 | 2018-03-22 00:53:26 +0000 | [diff] [blame] | 25 | #include "clang/Lex/Lexer.h" |
| 26 | #include "clang/Lex/PPCallbacks.h" |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 27 | #include "clang/Lex/Preprocessor.h" |
| Eugene Zelenko | 4f23318 | 2018-03-22 00:53:26 +0000 | [diff] [blame] | 28 | #include "clang/Lex/Token.h" |
| 29 | #include "llvm/ADT/STLExtras.h" |
| 30 | #include "llvm/ADT/SmallPtrSet.h" |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SmallString.h" |
| Eugene Zelenko | 4f23318 | 2018-03-22 00:53:26 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/StringRef.h" |
| 33 | #include "llvm/ADT/Twine.h" |
| 34 | #include "llvm/Support/ErrorHandling.h" |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Regex.h" |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 36 | #include "llvm/Support/raw_ostream.h" |
| Eugene Zelenko | 4f23318 | 2018-03-22 00:53:26 +0000 | [diff] [blame] | 37 | #include <algorithm> |
| 38 | #include <cassert> |
| 39 | #include <cstddef> |
| 40 | #include <cstring> |
| 41 | #include <iterator> |
| 42 | #include <memory> |
| 43 | #include <string> |
| 44 | #include <utility> |
| 45 | #include <vector> |
| Anna Zaks | 2c74eed | 2011-12-15 02:58:00 +0000 | [diff] [blame] | 46 | |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 47 | using namespace clang; |
| Eugene Zelenko | 4f23318 | 2018-03-22 00:53:26 +0000 | [diff] [blame] | 48 | |
| 49 | using Directive = VerifyDiagnosticConsumer::Directive; |
| 50 | using DirectiveList = VerifyDiagnosticConsumer::DirectiveList; |
| 51 | using ExpectedData = VerifyDiagnosticConsumer::ExpectedData; |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 52 | |
| Jordan Rose | b00073d | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 53 | #ifndef NDEBUG |
| Eugene Zelenko | 4f23318 | 2018-03-22 00:53:26 +0000 | [diff] [blame] | 54 | |
| Jordan Rose | b00073d | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 55 | namespace { |
| Eugene Zelenko | 4f23318 | 2018-03-22 00:53:26 +0000 | [diff] [blame] | 56 | |
| Jordan Rose | b00073d | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 57 | class VerifyFileTracker : public PPCallbacks { |
| Jordan Rose | 8c1ac0c | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 58 | VerifyDiagnosticConsumer &Verify; |
| Jordan Rose | b00073d | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 59 | SourceManager &SM; |
| 60 | |
| 61 | public: |
| Jordan Rose | 8c1ac0c | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 62 | VerifyFileTracker(VerifyDiagnosticConsumer &Verify, SourceManager &SM) |
| Eugene Zelenko | 4f23318 | 2018-03-22 00:53:26 +0000 | [diff] [blame] | 63 | : Verify(Verify), SM(SM) {} |
| Jordan Rose | b00073d | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 64 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 65 | /// Hook into the preprocessor and update the list of parsed |
| Jordan Rose | b00073d | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 66 | /// files when the preprocessor indicates a new file is entered. |
| Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 67 | void FileChanged(SourceLocation Loc, FileChangeReason Reason, |
| 68 | SrcMgr::CharacteristicKind FileType, |
| 69 | FileID PrevFID) override { |
| Jordan Rose | 8c1ac0c | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 70 | Verify.UpdateParsedFileStatus(SM, SM.getFileID(Loc), |
| 71 | VerifyDiagnosticConsumer::IsParsed); |
| Jordan Rose | b00073d | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 72 | } |
| 73 | }; |
| Eugene Zelenko | 4f23318 | 2018-03-22 00:53:26 +0000 | [diff] [blame] | 74 | |
| 75 | } // namespace |
| 76 | |
| Jordan Rose | b00073d | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 77 | #endif |
| 78 | |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 79 | //===----------------------------------------------------------------------===// |
| 80 | // Checking diagnostics implementation. |
| 81 | //===----------------------------------------------------------------------===// |
| 82 | |
| Eugene Zelenko | 4f23318 | 2018-03-22 00:53:26 +0000 | [diff] [blame] | 83 | using DiagList = TextDiagnosticBuffer::DiagList; |
| 84 | using const_diag_iterator = TextDiagnosticBuffer::const_iterator; |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 85 | |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 86 | namespace { |
| 87 | |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 88 | /// StandardDirective - Directive with string matching. |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 89 | class StandardDirective : public Directive { |
| 90 | public: |
| Jordan Rose | e1572eb | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 91 | StandardDirective(SourceLocation DirectiveLoc, SourceLocation DiagnosticLoc, |
| Andy Gibbs | c1f152e | 2014-07-10 16:43:29 +0000 | [diff] [blame] | 92 | bool MatchAnyLine, StringRef Text, unsigned Min, |
| 93 | unsigned Max) |
| Eugene Zelenko | 4f23318 | 2018-03-22 00:53:26 +0000 | [diff] [blame] | 94 | : Directive(DirectiveLoc, DiagnosticLoc, MatchAnyLine, Text, Min, Max) {} |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 95 | |
| Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 96 | bool isValid(std::string &Error) override { |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 97 | // all strings are considered valid; even empty ones |
| 98 | return true; |
| 99 | } |
| 100 | |
| Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 101 | bool match(StringRef S) override { |
| Jordan Rose | 6dae761 | 2012-07-10 02:56:15 +0000 | [diff] [blame] | 102 | return S.find(Text) != StringRef::npos; |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 103 | } |
| 104 | }; |
| 105 | |
| 106 | /// RegexDirective - Directive with regular-expression matching. |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 107 | class RegexDirective : public Directive { |
| 108 | public: |
| Jordan Rose | e1572eb | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 109 | RegexDirective(SourceLocation DirectiveLoc, SourceLocation DiagnosticLoc, |
| Andy Gibbs | c1f152e | 2014-07-10 16:43:29 +0000 | [diff] [blame] | 110 | bool MatchAnyLine, StringRef Text, unsigned Min, unsigned Max, |
| 111 | StringRef RegexStr) |
| Eugene Zelenko | 4f23318 | 2018-03-22 00:53:26 +0000 | [diff] [blame] | 112 | : Directive(DirectiveLoc, DiagnosticLoc, MatchAnyLine, Text, Min, Max), |
| 113 | Regex(RegexStr) {} |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 114 | |
| Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 115 | bool isValid(std::string &Error) override { |
| Alexander Kornienko | 5583e6f | 2015-12-28 15:15:16 +0000 | [diff] [blame] | 116 | return Regex.isValid(Error); |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 119 | bool match(StringRef S) override { |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 120 | return Regex.match(S); |
| 121 | } |
| 122 | |
| 123 | private: |
| 124 | llvm::Regex Regex; |
| 125 | }; |
| 126 | |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 127 | class ParseHelper |
| 128 | { |
| 129 | public: |
| Jordan Rose | b13eb8d | 2012-07-11 19:58:23 +0000 | [diff] [blame] | 130 | ParseHelper(StringRef S) |
| Eugene Zelenko | 4f23318 | 2018-03-22 00:53:26 +0000 | [diff] [blame] | 131 | : Begin(S.begin()), End(S.end()), C(Begin), P(Begin) {} |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 132 | |
| 133 | // Return true if string literal is next. |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 134 | bool Next(StringRef S) { |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 135 | P = C; |
| Benjamin Kramer | 2bd4cee | 2010-09-01 17:28:48 +0000 | [diff] [blame] | 136 | PEnd = C + S.size(); |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 137 | if (PEnd > End) |
| 138 | return false; |
| Eugene Zelenko | 4f23318 | 2018-03-22 00:53:26 +0000 | [diff] [blame] | 139 | return memcmp(P, S.data(), S.size()) == 0; |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | // Return true if number is next. |
| 143 | // Output N only if number is next. |
| 144 | bool Next(unsigned &N) { |
| 145 | unsigned TMP = 0; |
| 146 | P = C; |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 147 | PEnd = P; |
| Richard Smith | 4e8144a | 2019-04-13 04:33:39 +0000 | [diff] [blame] | 148 | for (; PEnd < End && *PEnd >= '0' && *PEnd <= '9'; ++PEnd) { |
| 149 | TMP *= 10; |
| 150 | TMP += *PEnd - '0'; |
| 151 | } |
| 152 | if (PEnd == C) |
| 153 | return false; |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 154 | N = TMP; |
| 155 | return true; |
| 156 | } |
| 157 | |
| Richard Smith | 4e8144a | 2019-04-13 04:33:39 +0000 | [diff] [blame] | 158 | // Return true if a marker is next. |
| 159 | // A marker is the longest match for /#[A-Za-z0-9_-]+/. |
| 160 | bool NextMarker() { |
| 161 | P = C; |
| 162 | if (P == End || *P != '#') |
| 163 | return false; |
| 164 | PEnd = P; |
| 165 | ++PEnd; |
| 166 | while ((isAlphanumeric(*PEnd) || *PEnd == '-' || *PEnd == '_') && |
| 167 | PEnd < End) |
| 168 | ++PEnd; |
| 169 | return PEnd > P + 1; |
| 170 | } |
| 171 | |
| Hal Finkel | 05e4648 | 2017-12-16 02:23:22 +0000 | [diff] [blame] | 172 | // Return true if string literal S is matched in content. |
| 173 | // When true, P marks begin-position of the match, and calling Advance sets C |
| 174 | // to end-position of the match. |
| 175 | // If S is the empty string, then search for any letter instead (makes sense |
| 176 | // with FinishDirectiveToken=true). |
| 177 | // If EnsureStartOfWord, then skip matches that don't start a new word. |
| 178 | // If FinishDirectiveToken, then assume the match is the start of a comment |
| 179 | // directive for -verify, and extend the match to include the entire first |
| 180 | // token of that directive. |
| 181 | bool Search(StringRef S, bool EnsureStartOfWord = false, |
| 182 | bool FinishDirectiveToken = false) { |
| Andy Gibbs | ac51de6 | 2012-10-19 12:36:49 +0000 | [diff] [blame] | 183 | do { |
| Hal Finkel | 05e4648 | 2017-12-16 02:23:22 +0000 | [diff] [blame] | 184 | if (!S.empty()) { |
| 185 | P = std::search(C, End, S.begin(), S.end()); |
| 186 | PEnd = P + S.size(); |
| 187 | } |
| 188 | else { |
| 189 | P = C; |
| 190 | while (P != End && !isLetter(*P)) |
| 191 | ++P; |
| 192 | PEnd = P + 1; |
| 193 | } |
| Andy Gibbs | ac51de6 | 2012-10-19 12:36:49 +0000 | [diff] [blame] | 194 | if (P == End) |
| 195 | break; |
| Hal Finkel | 05e4648 | 2017-12-16 02:23:22 +0000 | [diff] [blame] | 196 | // If not start of word but required, skip and search again. |
| 197 | if (EnsureStartOfWord |
| 198 | // Check if string literal starts a new word. |
| 199 | && !(P == Begin || isWhitespace(P[-1]) |
| 200 | // Or it could be preceded by the start of a comment. |
| 201 | || (P > (Begin + 1) && (P[-1] == '/' || P[-1] == '*') |
| 202 | && P[-2] == '/'))) |
| 203 | continue; |
| 204 | if (FinishDirectiveToken) { |
| 205 | while (PEnd != End && (isAlphanumeric(*PEnd) |
| 206 | || *PEnd == '-' || *PEnd == '_')) |
| 207 | ++PEnd; |
| 208 | // Put back trailing digits and hyphens to be parsed later as a count |
| 209 | // or count range. Because -verify prefixes must start with letters, |
| 210 | // we know the actual directive we found starts with a letter, so |
| 211 | // we won't put back the entire directive word and thus record an empty |
| 212 | // string. |
| 213 | assert(isLetter(*P) && "-verify prefix must start with a letter"); |
| 214 | while (isDigit(PEnd[-1]) || PEnd[-1] == '-') |
| 215 | --PEnd; |
| 216 | } |
| 217 | return true; |
| Andy Gibbs | ac51de6 | 2012-10-19 12:36:49 +0000 | [diff] [blame] | 218 | } while (Advance()); |
| 219 | return false; |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| Hans Wennborg | cda4b6d | 2013-12-11 23:40:50 +0000 | [diff] [blame] | 222 | // Return true if a CloseBrace that closes the OpenBrace at the current nest |
| 223 | // level is found. When true, P marks begin-position of CloseBrace. |
| 224 | bool SearchClosingBrace(StringRef OpenBrace, StringRef CloseBrace) { |
| 225 | unsigned Depth = 1; |
| 226 | P = C; |
| 227 | while (P < End) { |
| 228 | StringRef S(P, End - P); |
| 229 | if (S.startswith(OpenBrace)) { |
| 230 | ++Depth; |
| 231 | P += OpenBrace.size(); |
| 232 | } else if (S.startswith(CloseBrace)) { |
| 233 | --Depth; |
| 234 | if (Depth == 0) { |
| 235 | PEnd = P + CloseBrace.size(); |
| 236 | return true; |
| 237 | } |
| 238 | P += CloseBrace.size(); |
| 239 | } else { |
| 240 | ++P; |
| 241 | } |
| 242 | } |
| 243 | return false; |
| 244 | } |
| 245 | |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 246 | // Advance 1-past previous next/search. |
| 247 | // Behavior is undefined if previous next/search failed. |
| 248 | bool Advance() { |
| 249 | C = PEnd; |
| 250 | return C < End; |
| 251 | } |
| 252 | |
| Richard Smith | 4e8144a | 2019-04-13 04:33:39 +0000 | [diff] [blame] | 253 | // Return the text matched by the previous next/search. |
| 254 | // Behavior is undefined if previous next/search failed. |
| 255 | StringRef Match() { return StringRef(P, PEnd - P); } |
| 256 | |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 257 | // Skip zero or more whitespace. |
| 258 | void SkipWhitespace() { |
| Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 259 | for (; C < End && isWhitespace(*C); ++C) |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 260 | ; |
| 261 | } |
| 262 | |
| 263 | // Return true if EOF reached. |
| 264 | bool Done() { |
| 265 | return !(C < End); |
| 266 | } |
| 267 | |
| Eugene Zelenko | 4f23318 | 2018-03-22 00:53:26 +0000 | [diff] [blame] | 268 | // Beginning of expected content. |
| 269 | const char * const Begin; |
| 270 | |
| 271 | // End of expected content (1-past). |
| 272 | const char * const End; |
| 273 | |
| 274 | // Position of next char in content. |
| 275 | const char *C; |
| 276 | |
| Richard Smith | 4e8144a | 2019-04-13 04:33:39 +0000 | [diff] [blame] | 277 | // Previous next/search subject start. |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 278 | const char *P; |
| 279 | |
| 280 | private: |
| Eugene Zelenko | 4f23318 | 2018-03-22 00:53:26 +0000 | [diff] [blame] | 281 | // Previous next/search subject end (1-past). |
| 282 | const char *PEnd = nullptr; |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 283 | }; |
| 284 | |
| Richard Smith | 4e8144a | 2019-04-13 04:33:39 +0000 | [diff] [blame] | 285 | // The information necessary to create a directive. |
| 286 | struct UnattachedDirective { |
| 287 | DirectiveList *DL = nullptr; |
| 288 | bool RegexKind = false; |
| 289 | SourceLocation DirectivePos, ContentBegin; |
| 290 | std::string Text; |
| 291 | unsigned Min = 1, Max = 1; |
| 292 | }; |
| 293 | |
| 294 | // Attach the specified directive to the line of code indicated by |
| 295 | // \p ExpectedLoc. |
| 296 | void attachDirective(DiagnosticsEngine &Diags, const UnattachedDirective &UD, |
| 297 | SourceLocation ExpectedLoc, bool MatchAnyLine = false) { |
| 298 | // Construct new directive. |
| 299 | std::unique_ptr<Directive> D = |
| 300 | Directive::create(UD.RegexKind, UD.DirectivePos, ExpectedLoc, |
| 301 | MatchAnyLine, UD.Text, UD.Min, UD.Max); |
| 302 | |
| 303 | std::string Error; |
| 304 | if (!D->isValid(Error)) { |
| 305 | Diags.Report(UD.ContentBegin, diag::err_verify_invalid_content) |
| 306 | << (UD.RegexKind ? "regex" : "string") << Error; |
| 307 | } |
| 308 | |
| 309 | UD.DL->push_back(std::move(D)); |
| 310 | } |
| 311 | |
| Eugene Zelenko | 4f23318 | 2018-03-22 00:53:26 +0000 | [diff] [blame] | 312 | } // anonymous |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 313 | |
| Richard Smith | 4e8144a | 2019-04-13 04:33:39 +0000 | [diff] [blame] | 314 | // Tracker for markers in the input files. A marker is a comment of the form |
| 315 | // |
| 316 | // n = 123; // #123 |
| 317 | // |
| 318 | // ... that can be referred to by a later expected-* directive: |
| 319 | // |
| 320 | // // expected-error@#123 {{undeclared identifier 'n'}} |
| 321 | // |
| 322 | // Marker declarations must be at the start of a comment or preceded by |
| 323 | // whitespace to distinguish them from uses of markers in directives. |
| 324 | class VerifyDiagnosticConsumer::MarkerTracker { |
| 325 | DiagnosticsEngine &Diags; |
| 326 | |
| 327 | struct Marker { |
| 328 | SourceLocation DefLoc; |
| 329 | SourceLocation RedefLoc; |
| 330 | SourceLocation UseLoc; |
| 331 | }; |
| 332 | llvm::StringMap<Marker> Markers; |
| 333 | |
| 334 | // Directives that couldn't be created yet because they name an unknown |
| 335 | // marker. |
| 336 | llvm::StringMap<llvm::SmallVector<UnattachedDirective, 2>> DeferredDirectives; |
| 337 | |
| 338 | public: |
| 339 | MarkerTracker(DiagnosticsEngine &Diags) : Diags(Diags) {} |
| 340 | |
| 341 | // Register a marker. |
| 342 | void addMarker(StringRef MarkerName, SourceLocation Pos) { |
| 343 | auto InsertResult = Markers.insert( |
| 344 | {MarkerName, Marker{Pos, SourceLocation(), SourceLocation()}}); |
| 345 | |
| 346 | Marker &M = InsertResult.first->second; |
| 347 | if (!InsertResult.second) { |
| 348 | // Marker was redefined. |
| 349 | M.RedefLoc = Pos; |
| 350 | } else { |
| 351 | // First definition: build any deferred directives. |
| 352 | auto Deferred = DeferredDirectives.find(MarkerName); |
| 353 | if (Deferred != DeferredDirectives.end()) { |
| 354 | for (auto &UD : Deferred->second) { |
| 355 | if (M.UseLoc.isInvalid()) |
| 356 | M.UseLoc = UD.DirectivePos; |
| 357 | attachDirective(Diags, UD, Pos); |
| 358 | } |
| 359 | DeferredDirectives.erase(Deferred); |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | // Register a directive at the specified marker. |
| 365 | void addDirective(StringRef MarkerName, const UnattachedDirective &UD) { |
| 366 | auto MarkerIt = Markers.find(MarkerName); |
| 367 | if (MarkerIt != Markers.end()) { |
| 368 | Marker &M = MarkerIt->second; |
| 369 | if (M.UseLoc.isInvalid()) |
| 370 | M.UseLoc = UD.DirectivePos; |
| 371 | return attachDirective(Diags, UD, M.DefLoc); |
| 372 | } |
| 373 | DeferredDirectives[MarkerName].push_back(UD); |
| 374 | } |
| 375 | |
| 376 | // Ensure we have no remaining deferred directives, and no |
| 377 | // multiply-defined-and-used markers. |
| 378 | void finalize() { |
| 379 | for (auto &MarkerInfo : Markers) { |
| 380 | StringRef Name = MarkerInfo.first(); |
| 381 | Marker &M = MarkerInfo.second; |
| 382 | if (M.RedefLoc.isValid() && M.UseLoc.isValid()) { |
| 383 | Diags.Report(M.UseLoc, diag::err_verify_ambiguous_marker) << Name; |
| 384 | Diags.Report(M.DefLoc, diag::note_verify_ambiguous_marker) << Name; |
| 385 | Diags.Report(M.RedefLoc, diag::note_verify_ambiguous_marker) << Name; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | for (auto &DeferredPair : DeferredDirectives) { |
| 390 | Diags.Report(DeferredPair.second.front().DirectivePos, |
| 391 | diag::err_verify_no_such_marker) |
| 392 | << DeferredPair.first(); |
| 393 | } |
| 394 | } |
| 395 | }; |
| 396 | |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 397 | /// ParseDirective - Go through the comment and see if it indicates expected |
| 398 | /// diagnostics. If so, then put them in the appropriate directive list. |
| 399 | /// |
| Jordan Rose | b13eb8d | 2012-07-11 19:58:23 +0000 | [diff] [blame] | 400 | /// Returns true if any valid directives were found. |
| Jordan Rose | b00073d | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 401 | static bool ParseDirective(StringRef S, ExpectedData *ED, SourceManager &SM, |
| Andy Gibbs | fcc699a | 2013-04-17 08:06:46 +0000 | [diff] [blame] | 402 | Preprocessor *PP, SourceLocation Pos, |
| Richard Smith | 4e8144a | 2019-04-13 04:33:39 +0000 | [diff] [blame] | 403 | VerifyDiagnosticConsumer::DirectiveStatus &Status, |
| 404 | VerifyDiagnosticConsumer::MarkerTracker &Markers) { |
| Andy Gibbs | fcc699a | 2013-04-17 08:06:46 +0000 | [diff] [blame] | 405 | DiagnosticsEngine &Diags = PP ? PP->getDiagnostics() : SM.getDiagnostics(); |
| 406 | |
| Richard Smith | 4e8144a | 2019-04-13 04:33:39 +0000 | [diff] [blame] | 407 | // First, scan the comment looking for markers. |
| 408 | for (ParseHelper PH(S); !PH.Done();) { |
| 409 | if (!PH.Search("#", true)) |
| 410 | break; |
| 411 | PH.C = PH.P; |
| 412 | if (!PH.NextMarker()) { |
| 413 | PH.Next("#"); |
| 414 | PH.Advance(); |
| 415 | continue; |
| 416 | } |
| 417 | PH.Advance(); |
| 418 | Markers.addMarker(PH.Match(), Pos); |
| 419 | } |
| 420 | |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 421 | // A single comment may contain multiple directives. |
| Jordan Rose | b13eb8d | 2012-07-11 19:58:23 +0000 | [diff] [blame] | 422 | bool FoundDirective = false; |
| 423 | for (ParseHelper PH(S); !PH.Done();) { |
| Hal Finkel | 05e4648 | 2017-12-16 02:23:22 +0000 | [diff] [blame] | 424 | // Search for the initial directive token. |
| 425 | // If one prefix, save time by searching only for its directives. |
| 426 | // Otherwise, search for any potential directive token and check it later. |
| 427 | const auto &Prefixes = Diags.getDiagnosticOptions().VerifyPrefixes; |
| 428 | if (!(Prefixes.size() == 1 ? PH.Search(*Prefixes.begin(), true, true) |
| 429 | : PH.Search("", true, true))) |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 430 | break; |
| Richard Smith | 4e8144a | 2019-04-13 04:33:39 +0000 | [diff] [blame] | 431 | |
| 432 | StringRef DToken = PH.Match(); |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 433 | PH.Advance(); |
| 434 | |
| Hal Finkel | 05e4648 | 2017-12-16 02:23:22 +0000 | [diff] [blame] | 435 | // Default directive kind. |
| Richard Smith | 4e8144a | 2019-04-13 04:33:39 +0000 | [diff] [blame] | 436 | UnattachedDirective D; |
| 437 | const char *KindStr = "string"; |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 438 | |
| Hal Finkel | 05e4648 | 2017-12-16 02:23:22 +0000 | [diff] [blame] | 439 | // Parse the initial directive token in reverse so we can easily determine |
| 440 | // its exact actual prefix. If we were to parse it from the front instead, |
| 441 | // it would be harder to determine where the prefix ends because there |
| 442 | // might be multiple matching -verify prefixes because some might prefix |
| 443 | // others. |
| Hal Finkel | 05e4648 | 2017-12-16 02:23:22 +0000 | [diff] [blame] | 444 | |
| 445 | // Regex in initial directive token: -re |
| 446 | if (DToken.endswith("-re")) { |
| Richard Smith | 4e8144a | 2019-04-13 04:33:39 +0000 | [diff] [blame] | 447 | D.RegexKind = true; |
| Hal Finkel | 05e4648 | 2017-12-16 02:23:22 +0000 | [diff] [blame] | 448 | KindStr = "regex"; |
| 449 | DToken = DToken.substr(0, DToken.size()-3); |
| 450 | } |
| 451 | |
| 452 | // Type in initial directive token: -{error|warning|note|no-diagnostics} |
| Hal Finkel | 05e4648 | 2017-12-16 02:23:22 +0000 | [diff] [blame] | 453 | bool NoDiag = false; |
| 454 | StringRef DType; |
| 455 | if (DToken.endswith(DType="-error")) |
| Richard Smith | 4e8144a | 2019-04-13 04:33:39 +0000 | [diff] [blame] | 456 | D.DL = ED ? &ED->Errors : nullptr; |
| Hal Finkel | 05e4648 | 2017-12-16 02:23:22 +0000 | [diff] [blame] | 457 | else if (DToken.endswith(DType="-warning")) |
| Richard Smith | 4e8144a | 2019-04-13 04:33:39 +0000 | [diff] [blame] | 458 | D.DL = ED ? &ED->Warnings : nullptr; |
| Hal Finkel | 05e4648 | 2017-12-16 02:23:22 +0000 | [diff] [blame] | 459 | else if (DToken.endswith(DType="-remark")) |
| Richard Smith | 4e8144a | 2019-04-13 04:33:39 +0000 | [diff] [blame] | 460 | D.DL = ED ? &ED->Remarks : nullptr; |
| Hal Finkel | 05e4648 | 2017-12-16 02:23:22 +0000 | [diff] [blame] | 461 | else if (DToken.endswith(DType="-note")) |
| Richard Smith | 4e8144a | 2019-04-13 04:33:39 +0000 | [diff] [blame] | 462 | D.DL = ED ? &ED->Notes : nullptr; |
| Hal Finkel | 05e4648 | 2017-12-16 02:23:22 +0000 | [diff] [blame] | 463 | else if (DToken.endswith(DType="-no-diagnostics")) { |
| 464 | NoDiag = true; |
| Richard Smith | 4e8144a | 2019-04-13 04:33:39 +0000 | [diff] [blame] | 465 | if (D.RegexKind) |
| Hal Finkel | 05e4648 | 2017-12-16 02:23:22 +0000 | [diff] [blame] | 466 | continue; |
| 467 | } |
| 468 | else |
| 469 | continue; |
| 470 | DToken = DToken.substr(0, DToken.size()-DType.size()); |
| 471 | |
| 472 | // What's left in DToken is the actual prefix. That might not be a -verify |
| 473 | // prefix even if there is only one -verify prefix (for example, the full |
| 474 | // DToken is foo-bar-warning, but foo is the only -verify prefix). |
| 475 | if (!std::binary_search(Prefixes.begin(), Prefixes.end(), DToken)) |
| 476 | continue; |
| 477 | |
| 478 | if (NoDiag) { |
| Andy Gibbs | 0fea045 | 2012-10-19 12:49:32 +0000 | [diff] [blame] | 479 | if (Status == VerifyDiagnosticConsumer::HasOtherExpectedDirectives) |
| 480 | Diags.Report(Pos, diag::err_verify_invalid_no_diags) |
| 481 | << /*IsExpectedNoDiagnostics=*/true; |
| 482 | else |
| 483 | Status = VerifyDiagnosticConsumer::HasExpectedNoDiagnostics; |
| 484 | continue; |
| Hal Finkel | 05e4648 | 2017-12-16 02:23:22 +0000 | [diff] [blame] | 485 | } |
| Andy Gibbs | 0fea045 | 2012-10-19 12:49:32 +0000 | [diff] [blame] | 486 | if (Status == VerifyDiagnosticConsumer::HasExpectedNoDiagnostics) { |
| 487 | Diags.Report(Pos, diag::err_verify_invalid_no_diags) |
| 488 | << /*IsExpectedNoDiagnostics=*/false; |
| 489 | continue; |
| 490 | } |
| 491 | Status = VerifyDiagnosticConsumer::HasOtherExpectedDirectives; |
| 492 | |
| Jordan Rose | b00073d | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 493 | // If a directive has been found but we're not interested |
| 494 | // in storing the directive information, return now. |
| Richard Smith | 4e8144a | 2019-04-13 04:33:39 +0000 | [diff] [blame] | 495 | if (!D.DL) |
| Jordan Rose | b00073d | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 496 | return true; |
| 497 | |
| Jordan Rose | e1572eb | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 498 | // Next optional token: @ |
| 499 | SourceLocation ExpectedLoc; |
| Richard Smith | 4e8144a | 2019-04-13 04:33:39 +0000 | [diff] [blame] | 500 | StringRef Marker; |
| Andy Gibbs | c1f152e | 2014-07-10 16:43:29 +0000 | [diff] [blame] | 501 | bool MatchAnyLine = false; |
| Jordan Rose | e1572eb | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 502 | if (!PH.Next("@")) { |
| 503 | ExpectedLoc = Pos; |
| 504 | } else { |
| 505 | PH.Advance(); |
| 506 | unsigned Line = 0; |
| 507 | bool FoundPlus = PH.Next("+"); |
| 508 | if (FoundPlus || PH.Next("-")) { |
| 509 | // Relative to current line. |
| 510 | PH.Advance(); |
| 511 | bool Invalid = false; |
| 512 | unsigned ExpectedLine = SM.getSpellingLineNumber(Pos, &Invalid); |
| 513 | if (!Invalid && PH.Next(Line) && (FoundPlus || Line < ExpectedLine)) { |
| 514 | if (FoundPlus) ExpectedLine += Line; |
| 515 | else ExpectedLine -= Line; |
| 516 | ExpectedLoc = SM.translateLineCol(SM.getFileID(Pos), ExpectedLine, 1); |
| 517 | } |
| Andy Gibbs | fcc699a | 2013-04-17 08:06:46 +0000 | [diff] [blame] | 518 | } else if (PH.Next(Line)) { |
| Jordan Rose | e1572eb | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 519 | // Absolute line number. |
| Andy Gibbs | fcc699a | 2013-04-17 08:06:46 +0000 | [diff] [blame] | 520 | if (Line > 0) |
| Jordan Rose | e1572eb | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 521 | ExpectedLoc = SM.translateLineCol(SM.getFileID(Pos), Line, 1); |
| Richard Smith | 4e8144a | 2019-04-13 04:33:39 +0000 | [diff] [blame] | 522 | } else if (PH.NextMarker()) { |
| 523 | Marker = PH.Match(); |
| Andy Gibbs | fcc699a | 2013-04-17 08:06:46 +0000 | [diff] [blame] | 524 | } else if (PP && PH.Search(":")) { |
| 525 | // Specific source file. |
| 526 | StringRef Filename(PH.C, PH.P-PH.C); |
| 527 | PH.Advance(); |
| 528 | |
| 529 | // Lookup file via Preprocessor, like a #include. |
| 530 | const DirectoryLookup *CurDir; |
| Richard Smith | 25d5075 | 2014-10-20 00:15:49 +0000 | [diff] [blame] | 531 | const FileEntry *FE = |
| 532 | PP->LookupFile(Pos, Filename, false, nullptr, nullptr, CurDir, |
| Volodymyr Sapsai | 421380a | 2019-02-05 22:34:55 +0000 | [diff] [blame] | 533 | nullptr, nullptr, nullptr, nullptr, nullptr); |
| Andy Gibbs | fcc699a | 2013-04-17 08:06:46 +0000 | [diff] [blame] | 534 | if (!FE) { |
| 535 | Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin), |
| 536 | diag::err_verify_missing_file) << Filename << KindStr; |
| 537 | continue; |
| 538 | } |
| 539 | |
| 540 | if (SM.translateFile(FE).isInvalid()) |
| 541 | SM.createFileID(FE, Pos, SrcMgr::C_User); |
| 542 | |
| 543 | if (PH.Next(Line) && Line > 0) |
| 544 | ExpectedLoc = SM.translateFileLineCol(FE, Line, 1); |
| Andy Gibbs | c1f152e | 2014-07-10 16:43:29 +0000 | [diff] [blame] | 545 | else if (PH.Next("*")) { |
| 546 | MatchAnyLine = true; |
| 547 | ExpectedLoc = SM.translateFileLineCol(FE, 1, 1); |
| 548 | } |
| Richard Smith | 17ad3ac | 2017-10-18 01:41:38 +0000 | [diff] [blame] | 549 | } else if (PH.Next("*")) { |
| 550 | MatchAnyLine = true; |
| 551 | ExpectedLoc = SourceLocation(); |
| Jordan Rose | e1572eb | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| Richard Smith | 4e8144a | 2019-04-13 04:33:39 +0000 | [diff] [blame] | 554 | if (ExpectedLoc.isInvalid() && !MatchAnyLine && Marker.empty()) { |
| Jordan Rose | e1572eb | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 555 | Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin), |
| 556 | diag::err_verify_missing_line) << KindStr; |
| 557 | continue; |
| 558 | } |
| 559 | PH.Advance(); |
| 560 | } |
| 561 | |
| 562 | // Skip optional whitespace. |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 563 | PH.SkipWhitespace(); |
| 564 | |
| Jordan Rose | e1572eb | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 565 | // Next optional token: positive integer or a '+'. |
| Richard Smith | 4e8144a | 2019-04-13 04:33:39 +0000 | [diff] [blame] | 566 | if (PH.Next(D.Min)) { |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 567 | PH.Advance(); |
| Jordan Rose | b8b2ca6 | 2012-07-10 02:57:26 +0000 | [diff] [blame] | 568 | // A positive integer can be followed by a '+' meaning min |
| 569 | // or more, or by a '-' meaning a range from min to max. |
| 570 | if (PH.Next("+")) { |
| Richard Smith | 4e8144a | 2019-04-13 04:33:39 +0000 | [diff] [blame] | 571 | D.Max = Directive::MaxCount; |
| Jordan Rose | b8b2ca6 | 2012-07-10 02:57:26 +0000 | [diff] [blame] | 572 | PH.Advance(); |
| 573 | } else if (PH.Next("-")) { |
| 574 | PH.Advance(); |
| Richard Smith | 4e8144a | 2019-04-13 04:33:39 +0000 | [diff] [blame] | 575 | if (!PH.Next(D.Max) || D.Max < D.Min) { |
| Jordan Rose | b8b2ca6 | 2012-07-10 02:57:26 +0000 | [diff] [blame] | 576 | Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin), |
| 577 | diag::err_verify_invalid_range) << KindStr; |
| 578 | continue; |
| 579 | } |
| 580 | PH.Advance(); |
| 581 | } else { |
| Richard Smith | 4e8144a | 2019-04-13 04:33:39 +0000 | [diff] [blame] | 582 | D.Max = D.Min; |
| Jordan Rose | b8b2ca6 | 2012-07-10 02:57:26 +0000 | [diff] [blame] | 583 | } |
| 584 | } else if (PH.Next("+")) { |
| 585 | // '+' on its own means "1 or more". |
| Richard Smith | 4e8144a | 2019-04-13 04:33:39 +0000 | [diff] [blame] | 586 | D.Max = Directive::MaxCount; |
| Anna Zaks | a251007 | 2011-12-15 02:28:16 +0000 | [diff] [blame] | 587 | PH.Advance(); |
| 588 | } |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 589 | |
| Jordan Rose | e1572eb | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 590 | // Skip optional whitespace. |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 591 | PH.SkipWhitespace(); |
| 592 | |
| Jordan Rose | e1572eb | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 593 | // Next token: {{ |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 594 | if (!PH.Next("{{")) { |
| Jordan Rose | 6dae761 | 2012-07-10 02:56:15 +0000 | [diff] [blame] | 595 | Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin), |
| 596 | diag::err_verify_missing_start) << KindStr; |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 597 | continue; |
| 598 | } |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 599 | PH.Advance(); |
| 600 | const char* const ContentBegin = PH.C; // mark content begin |
| Jordan Rose | e1572eb | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 601 | // Search for token: }} |
| Hans Wennborg | cda4b6d | 2013-12-11 23:40:50 +0000 | [diff] [blame] | 602 | if (!PH.SearchClosingBrace("{{", "}}")) { |
| Jordan Rose | 6dae761 | 2012-07-10 02:56:15 +0000 | [diff] [blame] | 603 | Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin), |
| 604 | diag::err_verify_missing_end) << KindStr; |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 605 | continue; |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 606 | } |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 607 | const char* const ContentEnd = PH.P; // mark content end |
| 608 | PH.Advance(); |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 609 | |
| Richard Smith | 4e8144a | 2019-04-13 04:33:39 +0000 | [diff] [blame] | 610 | D.DirectivePos = Pos; |
| 611 | D.ContentBegin = Pos.getLocWithOffset(ContentBegin - PH.Begin); |
| 612 | |
| Jordan Rose | e1572eb | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 613 | // Build directive text; convert \n to newlines. |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 614 | StringRef NewlineStr = "\\n"; |
| 615 | StringRef Content(ContentBegin, ContentEnd-ContentBegin); |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 616 | size_t CPos = 0; |
| 617 | size_t FPos; |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 618 | while ((FPos = Content.find(NewlineStr, CPos)) != StringRef::npos) { |
| Richard Smith | 4e8144a | 2019-04-13 04:33:39 +0000 | [diff] [blame] | 619 | D.Text += Content.substr(CPos, FPos-CPos); |
| 620 | D.Text += '\n'; |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 621 | CPos = FPos + NewlineStr.size(); |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 622 | } |
| Richard Smith | 4e8144a | 2019-04-13 04:33:39 +0000 | [diff] [blame] | 623 | if (D.Text.empty()) |
| 624 | D.Text.assign(ContentBegin, ContentEnd); |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 625 | |
| Alp Toker | 6ed7251 | 2013-12-14 01:07:05 +0000 | [diff] [blame] | 626 | // Check that regex directives contain at least one regex. |
| Richard Smith | 4e8144a | 2019-04-13 04:33:39 +0000 | [diff] [blame] | 627 | if (D.RegexKind && D.Text.find("{{") == StringRef::npos) { |
| 628 | Diags.Report(D.ContentBegin, diag::err_verify_missing_regex) << D.Text; |
| Alp Toker | 6ed7251 | 2013-12-14 01:07:05 +0000 | [diff] [blame] | 629 | return false; |
| 630 | } |
| 631 | |
| Richard Smith | 4e8144a | 2019-04-13 04:33:39 +0000 | [diff] [blame] | 632 | if (Marker.empty()) |
| 633 | attachDirective(Diags, D, ExpectedLoc, MatchAnyLine); |
| 634 | else |
| 635 | Markers.addDirective(Marker, D); |
| 636 | FoundDirective = true; |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 637 | } |
| Jordan Rose | b13eb8d | 2012-07-11 19:58:23 +0000 | [diff] [blame] | 638 | |
| 639 | return FoundDirective; |
| 640 | } |
| 641 | |
| Richard Smith | 4e8144a | 2019-04-13 04:33:39 +0000 | [diff] [blame] | 642 | VerifyDiagnosticConsumer::VerifyDiagnosticConsumer(DiagnosticsEngine &Diags_) |
| 643 | : Diags(Diags_), PrimaryClient(Diags.getClient()), |
| 644 | PrimaryClientOwner(Diags.takeClient()), |
| 645 | Buffer(new TextDiagnosticBuffer()), Markers(new MarkerTracker(Diags)), |
| 646 | Status(HasNoDirectives) { |
| 647 | if (Diags.hasSourceManager()) |
| 648 | setSourceManager(Diags.getSourceManager()); |
| 649 | } |
| 650 | |
| 651 | VerifyDiagnosticConsumer::~VerifyDiagnosticConsumer() { |
| 652 | assert(!ActiveSourceFiles && "Incomplete parsing of source files!"); |
| 653 | assert(!CurrentPreprocessor && "CurrentPreprocessor should be invalid!"); |
| 654 | SrcManager = nullptr; |
| 655 | CheckDiagnostics(); |
| 656 | assert(!Diags.ownsClient() && |
| 657 | "The VerifyDiagnosticConsumer takes over ownership of the client!"); |
| 658 | } |
| 659 | |
| 660 | // DiagnosticConsumer interface. |
| 661 | |
| 662 | void VerifyDiagnosticConsumer::BeginSourceFile(const LangOptions &LangOpts, |
| 663 | const Preprocessor *PP) { |
| 664 | // Attach comment handler on first invocation. |
| 665 | if (++ActiveSourceFiles == 1) { |
| 666 | if (PP) { |
| 667 | CurrentPreprocessor = PP; |
| 668 | this->LangOpts = &LangOpts; |
| 669 | setSourceManager(PP->getSourceManager()); |
| 670 | const_cast<Preprocessor *>(PP)->addCommentHandler(this); |
| 671 | #ifndef NDEBUG |
| 672 | // Debug build tracks parsed files. |
| 673 | const_cast<Preprocessor *>(PP)->addPPCallbacks( |
| Jonas Devlieghere | 2b3d49b | 2019-08-14 23:04:18 +0000 | [diff] [blame^] | 674 | std::make_unique<VerifyFileTracker>(*this, *SrcManager)); |
| Richard Smith | 4e8144a | 2019-04-13 04:33:39 +0000 | [diff] [blame] | 675 | #endif |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | assert((!PP || CurrentPreprocessor == PP) && "Preprocessor changed!"); |
| 680 | PrimaryClient->BeginSourceFile(LangOpts, PP); |
| 681 | } |
| 682 | |
| 683 | void VerifyDiagnosticConsumer::EndSourceFile() { |
| 684 | assert(ActiveSourceFiles && "No active source files!"); |
| 685 | PrimaryClient->EndSourceFile(); |
| 686 | |
| 687 | // Detach comment handler once last active source file completed. |
| 688 | if (--ActiveSourceFiles == 0) { |
| 689 | if (CurrentPreprocessor) |
| 690 | const_cast<Preprocessor *>(CurrentPreprocessor)-> |
| 691 | removeCommentHandler(this); |
| 692 | |
| 693 | // Diagnose any used-but-not-defined markers. |
| 694 | Markers->finalize(); |
| 695 | |
| 696 | // Check diagnostics once last file completed. |
| 697 | CheckDiagnostics(); |
| 698 | CurrentPreprocessor = nullptr; |
| 699 | LangOpts = nullptr; |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | void VerifyDiagnosticConsumer::HandleDiagnostic( |
| 704 | DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) { |
| 705 | if (Info.hasSourceManager()) { |
| 706 | // If this diagnostic is for a different source manager, ignore it. |
| 707 | if (SrcManager && &Info.getSourceManager() != SrcManager) |
| 708 | return; |
| 709 | |
| 710 | setSourceManager(Info.getSourceManager()); |
| 711 | } |
| 712 | |
| 713 | #ifndef NDEBUG |
| 714 | // Debug build tracks unparsed files for possible |
| 715 | // unparsed expected-* directives. |
| 716 | if (SrcManager) { |
| 717 | SourceLocation Loc = Info.getLocation(); |
| 718 | if (Loc.isValid()) { |
| 719 | ParsedStatus PS = IsUnparsed; |
| 720 | |
| 721 | Loc = SrcManager->getExpansionLoc(Loc); |
| 722 | FileID FID = SrcManager->getFileID(Loc); |
| 723 | |
| 724 | const FileEntry *FE = SrcManager->getFileEntryForID(FID); |
| 725 | if (FE && CurrentPreprocessor && SrcManager->isLoadedFileID(FID)) { |
| 726 | // If the file is a modules header file it shall not be parsed |
| 727 | // for expected-* directives. |
| 728 | HeaderSearch &HS = CurrentPreprocessor->getHeaderSearchInfo(); |
| 729 | if (HS.findModuleForHeader(FE)) |
| 730 | PS = IsUnparsedNoDirectives; |
| 731 | } |
| 732 | |
| 733 | UpdateParsedFileStatus(*SrcManager, FID, PS); |
| 734 | } |
| 735 | } |
| 736 | #endif |
| 737 | |
| 738 | // Send the diagnostic to the buffer, we will check it once we reach the end |
| 739 | // of the source file (or are destructed). |
| 740 | Buffer->HandleDiagnostic(DiagLevel, Info); |
| 741 | } |
| 742 | |
| Jordan Rose | b13eb8d | 2012-07-11 19:58:23 +0000 | [diff] [blame] | 743 | /// HandleComment - Hook into the preprocessor and extract comments containing |
| 744 | /// expected errors and warnings. |
| 745 | bool VerifyDiagnosticConsumer::HandleComment(Preprocessor &PP, |
| 746 | SourceRange Comment) { |
| 747 | SourceManager &SM = PP.getSourceManager(); |
| Douglas Gregor | 6b93096 | 2013-05-03 22:58:43 +0000 | [diff] [blame] | 748 | |
| 749 | // If this comment is for a different source manager, ignore it. |
| 750 | if (SrcManager && &SM != SrcManager) |
| 751 | return false; |
| 752 | |
| Jordan Rose | b13eb8d | 2012-07-11 19:58:23 +0000 | [diff] [blame] | 753 | SourceLocation CommentBegin = Comment.getBegin(); |
| 754 | |
| 755 | const char *CommentRaw = SM.getCharacterData(CommentBegin); |
| 756 | StringRef C(CommentRaw, SM.getCharacterData(Comment.getEnd()) - CommentRaw); |
| 757 | |
| 758 | if (C.empty()) |
| 759 | return false; |
| 760 | |
| 761 | // Fold any "\<EOL>" sequences |
| 762 | size_t loc = C.find('\\'); |
| 763 | if (loc == StringRef::npos) { |
| Richard Smith | 4e8144a | 2019-04-13 04:33:39 +0000 | [diff] [blame] | 764 | ParseDirective(C, &ED, SM, &PP, CommentBegin, Status, *Markers); |
| Jordan Rose | b13eb8d | 2012-07-11 19:58:23 +0000 | [diff] [blame] | 765 | return false; |
| 766 | } |
| 767 | |
| 768 | std::string C2; |
| 769 | C2.reserve(C.size()); |
| 770 | |
| 771 | for (size_t last = 0;; loc = C.find('\\', last)) { |
| 772 | if (loc == StringRef::npos || loc == C.size()) { |
| 773 | C2 += C.substr(last); |
| 774 | break; |
| 775 | } |
| 776 | C2 += C.substr(last, loc-last); |
| 777 | last = loc + 1; |
| 778 | |
| 779 | if (C[last] == '\n' || C[last] == '\r') { |
| 780 | ++last; |
| 781 | |
| 782 | // Escape \r\n or \n\r, but not \n\n. |
| 783 | if (last < C.size()) |
| 784 | if (C[last] == '\n' || C[last] == '\r') |
| 785 | if (C[last] != C[last-1]) |
| 786 | ++last; |
| 787 | } else { |
| 788 | // This was just a normal backslash. |
| 789 | C2 += '\\'; |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | if (!C2.empty()) |
| Richard Smith | 4e8144a | 2019-04-13 04:33:39 +0000 | [diff] [blame] | 794 | ParseDirective(C2, &ED, SM, &PP, CommentBegin, Status, *Markers); |
| Jordan Rose | b13eb8d | 2012-07-11 19:58:23 +0000 | [diff] [blame] | 795 | return false; |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 796 | } |
| 797 | |
| Jordan Rose | b00073d | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 798 | #ifndef NDEBUG |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 799 | /// Lex the specified source file to determine whether it contains |
| Jordan Rose | b00073d | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 800 | /// any expected-* directives. As a Lexer is used rather than a full-blown |
| 801 | /// Preprocessor, directives inside skipped #if blocks will still be found. |
| 802 | /// |
| 803 | /// \return true if any directives were found. |
| Jordan Rose | 8c1ac0c | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 804 | static bool findDirectives(SourceManager &SM, FileID FID, |
| 805 | const LangOptions &LangOpts) { |
| Axel Naumann | ac50dcf | 2011-07-25 19:18:12 +0000 | [diff] [blame] | 806 | // Create a raw lexer to pull all the comments out of FID. |
| 807 | if (FID.isInvalid()) |
| Jordan Rose | b00073d | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 808 | return false; |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 809 | |
| 810 | // Create a lexer to lex all the tokens of the main file in raw mode. |
| Chris Lattner | 710bb87 | 2009-11-30 04:18:44 +0000 | [diff] [blame] | 811 | const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID); |
| Jordan Rose | 8c1ac0c | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 812 | Lexer RawLex(FID, FromFile, SM, LangOpts); |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 813 | |
| 814 | // Return comments as tokens, this is how we find expected diagnostics. |
| 815 | RawLex.SetCommentRetentionState(true); |
| 816 | |
| 817 | Token Tok; |
| 818 | Tok.setKind(tok::comment); |
| Andy Gibbs | 0fea045 | 2012-10-19 12:49:32 +0000 | [diff] [blame] | 819 | VerifyDiagnosticConsumer::DirectiveStatus Status = |
| 820 | VerifyDiagnosticConsumer::HasNoDirectives; |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 821 | while (Tok.isNot(tok::eof)) { |
| Eli Friedman | 0834a4b | 2013-09-19 00:41:32 +0000 | [diff] [blame] | 822 | RawLex.LexFromRawLexer(Tok); |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 823 | if (!Tok.is(tok::comment)) continue; |
| 824 | |
| Jordan Rose | 8c1ac0c | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 825 | std::string Comment = RawLex.getSpelling(Tok, SM, LangOpts); |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 826 | if (Comment.empty()) continue; |
| 827 | |
| Richard Smith | 4e8144a | 2019-04-13 04:33:39 +0000 | [diff] [blame] | 828 | // We don't care about tracking markers for this phase. |
| 829 | VerifyDiagnosticConsumer::MarkerTracker Markers(SM.getDiagnostics()); |
| 830 | |
| Jordan Rose | 8c1ac0c | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 831 | // Find first directive. |
| Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 832 | if (ParseDirective(Comment, nullptr, SM, nullptr, Tok.getLocation(), |
| Richard Smith | 4e8144a | 2019-04-13 04:33:39 +0000 | [diff] [blame] | 833 | Status, Markers)) |
| Jordan Rose | 8c1ac0c | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 834 | return true; |
| Jordan Rose | b00073d | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 835 | } |
| Jordan Rose | 8c1ac0c | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 836 | return false; |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 837 | } |
| Jordan Rose | b00073d | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 838 | #endif // !NDEBUG |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 839 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 840 | /// Takes a list of diagnostics that have been generated but not matched |
| Jordan Rose | e1572eb | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 841 | /// by an expected-* directive and produces a diagnostic to the user from this. |
| 842 | static unsigned PrintUnexpected(DiagnosticsEngine &Diags, SourceManager *SourceMgr, |
| 843 | const_diag_iterator diag_begin, |
| 844 | const_diag_iterator diag_end, |
| 845 | const char *Kind) { |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 846 | if (diag_begin == diag_end) return 0; |
| 847 | |
| Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 848 | SmallString<256> Fmt; |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 849 | llvm::raw_svector_ostream OS(Fmt); |
| 850 | for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I) { |
| Daniel Dunbar | 1b39a2e | 2009-11-14 07:53:24 +0000 | [diff] [blame] | 851 | if (I->first.isInvalid() || !SourceMgr) |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 852 | OS << "\n (frontend)"; |
| Andy Gibbs | fcc699a | 2013-04-17 08:06:46 +0000 | [diff] [blame] | 853 | else { |
| 854 | OS << "\n "; |
| 855 | if (const FileEntry *File = SourceMgr->getFileEntryForID( |
| 856 | SourceMgr->getFileID(I->first))) |
| 857 | OS << " File " << File->getName(); |
| 858 | OS << " Line " << SourceMgr->getPresumedLineNumber(I->first); |
| 859 | } |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 860 | OS << ": " << I->second; |
| 861 | } |
| 862 | |
| Jordan Rose | 6f524ac | 2012-07-11 16:50:36 +0000 | [diff] [blame] | 863 | Diags.Report(diag::err_verify_inconsistent_diags).setForceEmit() |
| Jordan Rose | e1572eb | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 864 | << Kind << /*Unexpected=*/true << OS.str(); |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 865 | return std::distance(diag_begin, diag_end); |
| 866 | } |
| 867 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 868 | /// Takes a list of diagnostics that were expected to have been generated |
| Jordan Rose | e1572eb | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 869 | /// but were not and produces a diagnostic to the user from this. |
| David Blaikie | 9310651 | 2014-08-29 16:30:23 +0000 | [diff] [blame] | 870 | static unsigned PrintExpected(DiagnosticsEngine &Diags, |
| 871 | SourceManager &SourceMgr, |
| 872 | std::vector<Directive *> &DL, const char *Kind) { |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 873 | if (DL.empty()) |
| 874 | return 0; |
| 875 | |
| Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 876 | SmallString<256> Fmt; |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 877 | llvm::raw_svector_ostream OS(Fmt); |
| Eugene Zelenko | 4f23318 | 2018-03-22 00:53:26 +0000 | [diff] [blame] | 878 | for (const auto *D : DL) { |
| 879 | if (D->DiagnosticLoc.isInvalid()) |
| Richard Smith | 17ad3ac | 2017-10-18 01:41:38 +0000 | [diff] [blame] | 880 | OS << "\n File *"; |
| 881 | else |
| Eugene Zelenko | 4f23318 | 2018-03-22 00:53:26 +0000 | [diff] [blame] | 882 | OS << "\n File " << SourceMgr.getFilename(D->DiagnosticLoc); |
| 883 | if (D->MatchAnyLine) |
| Andy Gibbs | c1f152e | 2014-07-10 16:43:29 +0000 | [diff] [blame] | 884 | OS << " Line *"; |
| 885 | else |
| Eugene Zelenko | 4f23318 | 2018-03-22 00:53:26 +0000 | [diff] [blame] | 886 | OS << " Line " << SourceMgr.getPresumedLineNumber(D->DiagnosticLoc); |
| 887 | if (D->DirectiveLoc != D->DiagnosticLoc) |
| Jordan Rose | e1572eb | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 888 | OS << " (directive at " |
| Eugene Zelenko | 4f23318 | 2018-03-22 00:53:26 +0000 | [diff] [blame] | 889 | << SourceMgr.getFilename(D->DirectiveLoc) << ':' |
| 890 | << SourceMgr.getPresumedLineNumber(D->DirectiveLoc) << ')'; |
| 891 | OS << ": " << D->Text; |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 892 | } |
| 893 | |
| Jordan Rose | 6f524ac | 2012-07-11 16:50:36 +0000 | [diff] [blame] | 894 | Diags.Report(diag::err_verify_inconsistent_diags).setForceEmit() |
| Jordan Rose | e1572eb | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 895 | << Kind << /*Unexpected=*/false << OS.str(); |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 896 | return DL.size(); |
| 897 | } |
| 898 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 899 | /// Determine whether two source locations come from the same file. |
| Andy Gibbs | fcc699a | 2013-04-17 08:06:46 +0000 | [diff] [blame] | 900 | static bool IsFromSameFile(SourceManager &SM, SourceLocation DirectiveLoc, |
| 901 | SourceLocation DiagnosticLoc) { |
| 902 | while (DiagnosticLoc.isMacroID()) |
| 903 | DiagnosticLoc = SM.getImmediateMacroCallerLoc(DiagnosticLoc); |
| 904 | |
| Eli Friedman | 5ba37d5 | 2013-08-22 00:27:10 +0000 | [diff] [blame] | 905 | if (SM.isWrittenInSameFile(DirectiveLoc, DiagnosticLoc)) |
| Andy Gibbs | fcc699a | 2013-04-17 08:06:46 +0000 | [diff] [blame] | 906 | return true; |
| 907 | |
| 908 | const FileEntry *DiagFile = SM.getFileEntryForID(SM.getFileID(DiagnosticLoc)); |
| Eli Friedman | 5ba37d5 | 2013-08-22 00:27:10 +0000 | [diff] [blame] | 909 | if (!DiagFile && SM.isWrittenInMainFile(DirectiveLoc)) |
| Andy Gibbs | fcc699a | 2013-04-17 08:06:46 +0000 | [diff] [blame] | 910 | return true; |
| 911 | |
| 912 | return (DiagFile == SM.getFileEntryForID(SM.getFileID(DirectiveLoc))); |
| 913 | } |
| 914 | |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 915 | /// CheckLists - Compare expected to seen diagnostic lists and return the |
| 916 | /// the difference between them. |
| David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 917 | static unsigned CheckLists(DiagnosticsEngine &Diags, SourceManager &SourceMgr, |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 918 | const char *Label, |
| 919 | DirectiveList &Left, |
| 920 | const_diag_iterator d2_begin, |
| Eric Fiselier | 098e6de | 2015-06-13 07:11:40 +0000 | [diff] [blame] | 921 | const_diag_iterator d2_end, |
| 922 | bool IgnoreUnexpected) { |
| David Blaikie | 9310651 | 2014-08-29 16:30:23 +0000 | [diff] [blame] | 923 | std::vector<Directive *> LeftOnly; |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 924 | DiagList Right(d2_begin, d2_end); |
| 925 | |
| David Blaikie | 9310651 | 2014-08-29 16:30:23 +0000 | [diff] [blame] | 926 | for (auto &Owner : Left) { |
| 927 | Directive &D = *Owner; |
| Jordan Rose | e1572eb | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 928 | unsigned LineNo1 = SourceMgr.getPresumedLineNumber(D.DiagnosticLoc); |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 929 | |
| Jordan Rose | b8b2ca6 | 2012-07-10 02:57:26 +0000 | [diff] [blame] | 930 | for (unsigned i = 0; i < D.Max; ++i) { |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 931 | DiagList::iterator II, IE; |
| 932 | for (II = Right.begin(), IE = Right.end(); II != IE; ++II) { |
| Andy Gibbs | c1f152e | 2014-07-10 16:43:29 +0000 | [diff] [blame] | 933 | if (!D.MatchAnyLine) { |
| 934 | unsigned LineNo2 = SourceMgr.getPresumedLineNumber(II->first); |
| 935 | if (LineNo1 != LineNo2) |
| 936 | continue; |
| 937 | } |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 938 | |
| Richard Smith | 17ad3ac | 2017-10-18 01:41:38 +0000 | [diff] [blame] | 939 | if (!D.DiagnosticLoc.isInvalid() && |
| 940 | !IsFromSameFile(SourceMgr, D.DiagnosticLoc, II->first)) |
| Andy Gibbs | fcc699a | 2013-04-17 08:06:46 +0000 | [diff] [blame] | 941 | continue; |
| 942 | |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 943 | const std::string &RightText = II->second; |
| Jordan Rose | 6dae761 | 2012-07-10 02:56:15 +0000 | [diff] [blame] | 944 | if (D.match(RightText)) |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 945 | break; |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 946 | } |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 947 | if (II == IE) { |
| 948 | // Not found. |
| Jordan Rose | b8b2ca6 | 2012-07-10 02:57:26 +0000 | [diff] [blame] | 949 | if (i >= D.Min) break; |
| David Blaikie | 9310651 | 2014-08-29 16:30:23 +0000 | [diff] [blame] | 950 | LeftOnly.push_back(&D); |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 951 | } else { |
| 952 | // Found. The same cannot be found twice. |
| 953 | Right.erase(II); |
| 954 | } |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 955 | } |
| 956 | } |
| 957 | // Now all that's left in Right are those that were not matched. |
| Jordan Rose | e1572eb | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 958 | unsigned num = PrintExpected(Diags, SourceMgr, LeftOnly, Label); |
| Eric Fiselier | 098e6de | 2015-06-13 07:11:40 +0000 | [diff] [blame] | 959 | if (!IgnoreUnexpected) |
| 960 | num += PrintUnexpected(Diags, &SourceMgr, Right.begin(), Right.end(), Label); |
| NAKAMURA Takumi | 7bfba2f | 2011-12-17 13:00:31 +0000 | [diff] [blame] | 961 | return num; |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | /// CheckResults - This compares the expected results to those that |
| 965 | /// were actually reported. It emits any discrepencies. Return "true" if there |
| 966 | /// were problems. Return "false" otherwise. |
| David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 967 | static unsigned CheckResults(DiagnosticsEngine &Diags, SourceManager &SourceMgr, |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 968 | const TextDiagnosticBuffer &Buffer, |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 969 | ExpectedData &ED) { |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 970 | // We want to capture the delta between what was expected and what was |
| 971 | // seen. |
| 972 | // |
| 973 | // Expected \ Seen - set expected but not seen |
| 974 | // Seen \ Expected - set seen but not expected |
| 975 | unsigned NumProblems = 0; |
| 976 | |
| Eric Fiselier | 098e6de | 2015-06-13 07:11:40 +0000 | [diff] [blame] | 977 | const DiagnosticLevelMask DiagMask = |
| 978 | Diags.getDiagnosticOptions().getVerifyIgnoreUnexpected(); |
| 979 | |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 980 | // See if there are error mismatches. |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 981 | NumProblems += CheckLists(Diags, SourceMgr, "error", ED.Errors, |
| Eric Fiselier | 098e6de | 2015-06-13 07:11:40 +0000 | [diff] [blame] | 982 | Buffer.err_begin(), Buffer.err_end(), |
| 983 | bool(DiagnosticLevelMask::Error & DiagMask)); |
| Daniel Dunbar | 1b39a2e | 2009-11-14 07:53:24 +0000 | [diff] [blame] | 984 | |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 985 | // See if there are warning mismatches. |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 986 | NumProblems += CheckLists(Diags, SourceMgr, "warning", ED.Warnings, |
| Eric Fiselier | 098e6de | 2015-06-13 07:11:40 +0000 | [diff] [blame] | 987 | Buffer.warn_begin(), Buffer.warn_end(), |
| 988 | bool(DiagnosticLevelMask::Warning & DiagMask)); |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 989 | |
| Tobias Grosser | 86a8567 | 2014-05-01 14:06:01 +0000 | [diff] [blame] | 990 | // See if there are remark mismatches. |
| 991 | NumProblems += CheckLists(Diags, SourceMgr, "remark", ED.Remarks, |
| Eric Fiselier | 098e6de | 2015-06-13 07:11:40 +0000 | [diff] [blame] | 992 | Buffer.remark_begin(), Buffer.remark_end(), |
| 993 | bool(DiagnosticLevelMask::Remark & DiagMask)); |
| Tobias Grosser | 86a8567 | 2014-05-01 14:06:01 +0000 | [diff] [blame] | 994 | |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 995 | // See if there are note mismatches. |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 996 | NumProblems += CheckLists(Diags, SourceMgr, "note", ED.Notes, |
| Eric Fiselier | 098e6de | 2015-06-13 07:11:40 +0000 | [diff] [blame] | 997 | Buffer.note_begin(), Buffer.note_end(), |
| 998 | bool(DiagnosticLevelMask::Note & DiagMask)); |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 999 | |
| 1000 | return NumProblems; |
| 1001 | } |
| 1002 | |
| Jordan Rose | 8c1ac0c | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 1003 | void VerifyDiagnosticConsumer::UpdateParsedFileStatus(SourceManager &SM, |
| 1004 | FileID FID, |
| 1005 | ParsedStatus PS) { |
| 1006 | // Check SourceManager hasn't changed. |
| 1007 | setSourceManager(SM); |
| 1008 | |
| 1009 | #ifndef NDEBUG |
| 1010 | if (FID.isInvalid()) |
| 1011 | return; |
| 1012 | |
| 1013 | const FileEntry *FE = SM.getFileEntryForID(FID); |
| 1014 | |
| 1015 | if (PS == IsParsed) { |
| 1016 | // Move the FileID from the unparsed set to the parsed set. |
| 1017 | UnparsedFiles.erase(FID); |
| 1018 | ParsedFiles.insert(std::make_pair(FID, FE)); |
| 1019 | } else if (!ParsedFiles.count(FID) && !UnparsedFiles.count(FID)) { |
| 1020 | // Add the FileID to the unparsed set if we haven't seen it before. |
| 1021 | |
| 1022 | // Check for directives. |
| 1023 | bool FoundDirectives; |
| 1024 | if (PS == IsUnparsedNoDirectives) |
| 1025 | FoundDirectives = false; |
| 1026 | else |
| 1027 | FoundDirectives = !LangOpts || findDirectives(SM, FID, *LangOpts); |
| 1028 | |
| 1029 | // Add the FileID to the unparsed set. |
| 1030 | UnparsedFiles.insert(std::make_pair(FID, |
| 1031 | UnparsedFileStatus(FE, FoundDirectives))); |
| 1032 | } |
| 1033 | #endif |
| 1034 | } |
| 1035 | |
| David Blaikie | 69609dc | 2011-09-26 00:38:03 +0000 | [diff] [blame] | 1036 | void VerifyDiagnosticConsumer::CheckDiagnostics() { |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 1037 | // Ensure any diagnostics go to the primary client. |
| Alexander Kornienko | 41c247a | 2014-11-17 23:46:02 +0000 | [diff] [blame] | 1038 | DiagnosticConsumer *CurClient = Diags.getClient(); |
| 1039 | std::unique_ptr<DiagnosticConsumer> Owner = Diags.takeClient(); |
| Douglas Gregor | 2b9b464 | 2011-09-13 01:26:44 +0000 | [diff] [blame] | 1040 | Diags.setClient(PrimaryClient, false); |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 1041 | |
| Jordan Rose | b00073d | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 1042 | #ifndef NDEBUG |
| Jordan Rose | 8c1ac0c | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 1043 | // In a debug build, scan through any files that may have been missed |
| 1044 | // during parsing and issue a fatal error if directives are contained |
| 1045 | // within these files. If a fatal error occurs, this suggests that |
| 1046 | // this file is being parsed separately from the main file, in which |
| 1047 | // case consider moving the directives to the correct place, if this |
| 1048 | // is applicable. |
| Eugene Zelenko | 4f23318 | 2018-03-22 00:53:26 +0000 | [diff] [blame] | 1049 | if (!UnparsedFiles.empty()) { |
| Jordan Rose | 8c1ac0c | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 1050 | // Generate a cache of parsed FileEntry pointers for alias lookups. |
| 1051 | llvm::SmallPtrSet<const FileEntry *, 8> ParsedFileCache; |
| Eugene Zelenko | 4f23318 | 2018-03-22 00:53:26 +0000 | [diff] [blame] | 1052 | for (const auto &I : ParsedFiles) |
| 1053 | if (const FileEntry *FE = I.second) |
| Jordan Rose | 8c1ac0c | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 1054 | ParsedFileCache.insert(FE); |
| Jordan Rose | 8c1ac0c | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 1055 | |
| 1056 | // Iterate through list of unparsed files. |
| Eugene Zelenko | 4f23318 | 2018-03-22 00:53:26 +0000 | [diff] [blame] | 1057 | for (const auto &I : UnparsedFiles) { |
| 1058 | const UnparsedFileStatus &Status = I.second; |
| Jordan Rose | 8c1ac0c | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 1059 | const FileEntry *FE = Status.getFile(); |
| 1060 | |
| 1061 | // Skip files that have been parsed via an alias. |
| 1062 | if (FE && ParsedFileCache.count(FE)) |
| Jordan Rose | b00073d | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 1063 | continue; |
| 1064 | |
| Jordan Rose | 8c1ac0c | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 1065 | // Report a fatal error if this file contained directives. |
| 1066 | if (Status.foundDirectives()) { |
| Jordan Rose | b00073d | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 1067 | llvm::report_fatal_error(Twine("-verify directives found after rather" |
| 1068 | " than during normal parsing of ", |
| Jordan Rose | 8c1ac0c | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 1069 | StringRef(FE ? FE->getName() : "(unknown)"))); |
| 1070 | } |
| Axel Naumann | 744f121 | 2011-08-24 13:36:19 +0000 | [diff] [blame] | 1071 | } |
| Daniel Dunbar | 1b39a2e | 2009-11-14 07:53:24 +0000 | [diff] [blame] | 1072 | |
| Jordan Rose | 8c1ac0c | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 1073 | // UnparsedFiles has been processed now, so clear it. |
| 1074 | UnparsedFiles.clear(); |
| 1075 | } |
| 1076 | #endif // !NDEBUG |
| 1077 | |
| 1078 | if (SrcManager) { |
| Andy Gibbs | 0fea045 | 2012-10-19 12:49:32 +0000 | [diff] [blame] | 1079 | // Produce an error if no expected-* directives could be found in the |
| 1080 | // source file(s) processed. |
| 1081 | if (Status == HasNoDirectives) { |
| 1082 | Diags.Report(diag::err_verify_no_directives).setForceEmit(); |
| 1083 | ++NumErrors; |
| 1084 | Status = HasNoDirectivesReported; |
| 1085 | } |
| 1086 | |
| Daniel Dunbar | 1b39a2e | 2009-11-14 07:53:24 +0000 | [diff] [blame] | 1087 | // Check that the expected diagnostics occurred. |
| Jordan Rose | 8c1ac0c | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 1088 | NumErrors += CheckResults(Diags, *SrcManager, *Buffer, ED); |
| Daniel Dunbar | 1b39a2e | 2009-11-14 07:53:24 +0000 | [diff] [blame] | 1089 | } else { |
| Eric Fiselier | 098e6de | 2015-06-13 07:11:40 +0000 | [diff] [blame] | 1090 | const DiagnosticLevelMask DiagMask = |
| 1091 | ~Diags.getDiagnosticOptions().getVerifyIgnoreUnexpected(); |
| 1092 | if (bool(DiagnosticLevelMask::Error & DiagMask)) |
| 1093 | NumErrors += PrintUnexpected(Diags, nullptr, Buffer->err_begin(), |
| 1094 | Buffer->err_end(), "error"); |
| 1095 | if (bool(DiagnosticLevelMask::Warning & DiagMask)) |
| 1096 | NumErrors += PrintUnexpected(Diags, nullptr, Buffer->warn_begin(), |
| 1097 | Buffer->warn_end(), "warn"); |
| 1098 | if (bool(DiagnosticLevelMask::Remark & DiagMask)) |
| 1099 | NumErrors += PrintUnexpected(Diags, nullptr, Buffer->remark_begin(), |
| 1100 | Buffer->remark_end(), "remark"); |
| 1101 | if (bool(DiagnosticLevelMask::Note & DiagMask)) |
| 1102 | NumErrors += PrintUnexpected(Diags, nullptr, Buffer->note_begin(), |
| 1103 | Buffer->note_end(), "note"); |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
| Alexander Kornienko | 41c247a | 2014-11-17 23:46:02 +0000 | [diff] [blame] | 1106 | Diags.setClient(CurClient, Owner.release() != nullptr); |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 1107 | |
| 1108 | // Reset the buffer, we have processed all the diagnostics in it. |
| 1109 | Buffer.reset(new TextDiagnosticBuffer()); |
| Nico Weber | dc493cf | 2014-04-24 05:39:55 +0000 | [diff] [blame] | 1110 | ED.Reset(); |
| Daniel Dunbar | 3481855 | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 1111 | } |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 1112 | |
| David Blaikie | 9310651 | 2014-08-29 16:30:23 +0000 | [diff] [blame] | 1113 | std::unique_ptr<Directive> Directive::create(bool RegexKind, |
| 1114 | SourceLocation DirectiveLoc, |
| 1115 | SourceLocation DiagnosticLoc, |
| 1116 | bool MatchAnyLine, StringRef Text, |
| 1117 | unsigned Min, unsigned Max) { |
| Alp Toker | 6ed7251 | 2013-12-14 01:07:05 +0000 | [diff] [blame] | 1118 | if (!RegexKind) |
| Jonas Devlieghere | 2b3d49b | 2019-08-14 23:04:18 +0000 | [diff] [blame^] | 1119 | return std::make_unique<StandardDirective>(DirectiveLoc, DiagnosticLoc, |
| David Blaikie | 9310651 | 2014-08-29 16:30:23 +0000 | [diff] [blame] | 1120 | MatchAnyLine, Text, Min, Max); |
| Hans Wennborg | cda4b6d | 2013-12-11 23:40:50 +0000 | [diff] [blame] | 1121 | |
| 1122 | // Parse the directive into a regular expression. |
| 1123 | std::string RegexStr; |
| 1124 | StringRef S = Text; |
| 1125 | while (!S.empty()) { |
| 1126 | if (S.startswith("{{")) { |
| 1127 | S = S.drop_front(2); |
| 1128 | size_t RegexMatchLength = S.find("}}"); |
| 1129 | assert(RegexMatchLength != StringRef::npos); |
| 1130 | // Append the regex, enclosed in parentheses. |
| 1131 | RegexStr += "("; |
| 1132 | RegexStr.append(S.data(), RegexMatchLength); |
| 1133 | RegexStr += ")"; |
| 1134 | S = S.drop_front(RegexMatchLength + 2); |
| 1135 | } else { |
| 1136 | size_t VerbatimMatchLength = S.find("{{"); |
| 1137 | if (VerbatimMatchLength == StringRef::npos) |
| 1138 | VerbatimMatchLength = S.size(); |
| 1139 | // Escape and append the fixed string. |
| Hans Wennborg | e6a8775 | 2013-12-12 00:27:31 +0000 | [diff] [blame] | 1140 | RegexStr += llvm::Regex::escape(S.substr(0, VerbatimMatchLength)); |
| Hans Wennborg | cda4b6d | 2013-12-11 23:40:50 +0000 | [diff] [blame] | 1141 | S = S.drop_front(VerbatimMatchLength); |
| 1142 | } |
| 1143 | } |
| 1144 | |
| Jonas Devlieghere | 2b3d49b | 2019-08-14 23:04:18 +0000 | [diff] [blame^] | 1145 | return std::make_unique<RegexDirective>( |
| David Blaikie | 9310651 | 2014-08-29 16:30:23 +0000 | [diff] [blame] | 1146 | DirectiveLoc, DiagnosticLoc, MatchAnyLine, Text, Min, Max, RegexStr); |
| Chris Lattner | e82411b | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 1147 | } |