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