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