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