David Blaikie | 621bc69 | 2011-09-26 00:38:03 +0000 | [diff] [blame] | 1 | //===---- VerifyDiagnosticConsumer.cpp - Verifying Diagnostic Client ------===// |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This is a concrete diagnostic client, which buffers the diagnostic messages. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Jordan Rose | 78541c4 | 2012-07-11 19:58:23 +0000 | [diff] [blame] | 14 | #include "clang/Basic/FileManager.h" |
David Blaikie | 621bc69 | 2011-09-26 00:38:03 +0000 | [diff] [blame] | 15 | #include "clang/Frontend/VerifyDiagnosticConsumer.h" |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 16 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 17 | #include "clang/Frontend/TextDiagnosticBuffer.h" |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 18 | #include "clang/Lex/HeaderSearch.h" |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 19 | #include "clang/Lex/Preprocessor.h" |
| 20 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Regex.h" |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Joerg Sonnenberger | 7094dee | 2012-08-10 10:58:18 +0000 | [diff] [blame] | 23 | #include <cctype> |
Anna Zaks | c035e09 | 2011-12-15 02:58:00 +0000 | [diff] [blame] | 24 | |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 25 | using namespace clang; |
Jordan Rose | 4313c01 | 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 | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 29 | |
David Blaikie | 621bc69 | 2011-09-26 00:38:03 +0000 | [diff] [blame] | 30 | VerifyDiagnosticConsumer::VerifyDiagnosticConsumer(DiagnosticsEngine &_Diags) |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 31 | : Diags(_Diags), |
| 32 | PrimaryClient(Diags.getClient()), OwnsPrimaryClient(Diags.ownsClient()), |
| 33 | Buffer(new TextDiagnosticBuffer()), CurrentPreprocessor(0), |
Jordan Rose | 7eaaa18 | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 34 | LangOpts(0), SrcManager(0), ActiveSourceFiles(0) |
Douglas Gregor | 7824365 | 2011-09-13 01:26:44 +0000 | [diff] [blame] | 35 | { |
| 36 | Diags.takeClient(); |
Jordan Rose | 7eaaa18 | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 37 | if (Diags.hasSourceManager()) |
| 38 | setSourceManager(Diags.getSourceManager()); |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 39 | } |
| 40 | |
David Blaikie | 621bc69 | 2011-09-26 00:38:03 +0000 | [diff] [blame] | 41 | VerifyDiagnosticConsumer::~VerifyDiagnosticConsumer() { |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 42 | assert(!ActiveSourceFiles && "Incomplete parsing of source files!"); |
| 43 | assert(!CurrentPreprocessor && "CurrentPreprocessor should be invalid!"); |
Jordan Rose | 7eaaa18 | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 44 | SrcManager = 0; |
Douglas Gregor | 7824365 | 2011-09-13 01:26:44 +0000 | [diff] [blame] | 45 | CheckDiagnostics(); |
| 46 | Diags.takeClient(); |
| 47 | if (OwnsPrimaryClient) |
| 48 | delete PrimaryClient; |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 51 | #ifndef NDEBUG |
| 52 | namespace { |
| 53 | class VerifyFileTracker : public PPCallbacks { |
Jordan Rose | 7eaaa18 | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 54 | VerifyDiagnosticConsumer &Verify; |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 55 | SourceManager &SM; |
| 56 | |
| 57 | public: |
Jordan Rose | 7eaaa18 | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 58 | VerifyFileTracker(VerifyDiagnosticConsumer &Verify, SourceManager &SM) |
| 59 | : Verify(Verify), SM(SM) { } |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 60 | |
| 61 | /// \brief Hook into the preprocessor and update the list of parsed |
| 62 | /// files when the preprocessor indicates a new file is entered. |
| 63 | virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason, |
| 64 | SrcMgr::CharacteristicKind FileType, |
| 65 | FileID PrevFID) { |
Jordan Rose | 7eaaa18 | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 66 | Verify.UpdateParsedFileStatus(SM, SM.getFileID(Loc), |
| 67 | VerifyDiagnosticConsumer::IsParsed); |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 68 | } |
| 69 | }; |
| 70 | } // End anonymous namespace. |
| 71 | #endif |
| 72 | |
David Blaikie | 78ad0b9 | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 73 | // DiagnosticConsumer interface. |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 74 | |
David Blaikie | 621bc69 | 2011-09-26 00:38:03 +0000 | [diff] [blame] | 75 | void VerifyDiagnosticConsumer::BeginSourceFile(const LangOptions &LangOpts, |
Douglas Gregor | 1f6b2b5 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 76 | const Preprocessor *PP) { |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 77 | // Attach comment handler on first invocation. |
| 78 | if (++ActiveSourceFiles == 1) { |
| 79 | if (PP) { |
| 80 | CurrentPreprocessor = PP; |
Jordan Rose | 7eaaa18 | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 81 | this->LangOpts = &LangOpts; |
| 82 | setSourceManager(PP->getSourceManager()); |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 83 | const_cast<Preprocessor*>(PP)->addCommentHandler(this); |
| 84 | #ifndef NDEBUG |
Jordan Rose | 7eaaa18 | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 85 | // Debug build tracks parsed files. |
| 86 | VerifyFileTracker *V = new VerifyFileTracker(*this, *SrcManager); |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 87 | const_cast<Preprocessor*>(PP)->addPPCallbacks(V); |
| 88 | #endif |
| 89 | } |
| 90 | } |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 91 | |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 92 | assert((!PP || CurrentPreprocessor == PP) && "Preprocessor changed!"); |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 93 | PrimaryClient->BeginSourceFile(LangOpts, PP); |
| 94 | } |
| 95 | |
David Blaikie | 621bc69 | 2011-09-26 00:38:03 +0000 | [diff] [blame] | 96 | void VerifyDiagnosticConsumer::EndSourceFile() { |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 97 | assert(ActiveSourceFiles && "No active source files!"); |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 98 | PrimaryClient->EndSourceFile(); |
| 99 | |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 100 | // Detach comment handler once last active source file completed. |
| 101 | if (--ActiveSourceFiles == 0) { |
| 102 | if (CurrentPreprocessor) |
| 103 | const_cast<Preprocessor*>(CurrentPreprocessor)->removeCommentHandler(this); |
| 104 | |
| 105 | // Check diagnostics once last file completed. |
| 106 | CheckDiagnostics(); |
| 107 | CurrentPreprocessor = 0; |
Jordan Rose | 7eaaa18 | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 108 | LangOpts = 0; |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 109 | } |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 110 | } |
Daniel Dunbar | 221c721 | 2009-11-14 07:53:24 +0000 | [diff] [blame] | 111 | |
David Blaikie | 621bc69 | 2011-09-26 00:38:03 +0000 | [diff] [blame] | 112 | void VerifyDiagnosticConsumer::HandleDiagnostic( |
David Blaikie | 40847cf | 2011-09-26 01:18:08 +0000 | [diff] [blame] | 113 | DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) { |
Jordan Rose | 7eaaa18 | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 114 | if (Info.hasSourceManager()) |
| 115 | setSourceManager(Info.getSourceManager()); |
| 116 | |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 117 | #ifndef NDEBUG |
Jordan Rose | 7eaaa18 | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 118 | // Debug build tracks unparsed files for possible |
| 119 | // unparsed expected-* directives. |
| 120 | if (SrcManager) { |
| 121 | SourceLocation Loc = Info.getLocation(); |
| 122 | if (Loc.isValid()) { |
| 123 | ParsedStatus PS = IsUnparsed; |
| 124 | |
| 125 | Loc = SrcManager->getExpansionLoc(Loc); |
| 126 | FileID FID = SrcManager->getFileID(Loc); |
| 127 | |
| 128 | const FileEntry *FE = SrcManager->getFileEntryForID(FID); |
| 129 | if (FE && CurrentPreprocessor && SrcManager->isLoadedFileID(FID)) { |
| 130 | // If the file is a modules header file it shall not be parsed |
| 131 | // for expected-* directives. |
| 132 | HeaderSearch &HS = CurrentPreprocessor->getHeaderSearchInfo(); |
| 133 | if (HS.findModuleForHeader(FE)) |
| 134 | PS = IsUnparsedNoDirectives; |
| 135 | } |
| 136 | |
| 137 | UpdateParsedFileStatus(*SrcManager, FID, PS); |
| 138 | } |
Axel Naumann | 0123161 | 2011-07-25 19:18:12 +0000 | [diff] [blame] | 139 | } |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 140 | #endif |
Jordan Rose | 7eaaa18 | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 141 | |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 142 | // Send the diagnostic to the buffer, we will check it once we reach the end |
| 143 | // of the source file (or are destructed). |
| 144 | Buffer->HandleDiagnostic(DiagLevel, Info); |
| 145 | } |
| 146 | |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 147 | //===----------------------------------------------------------------------===// |
| 148 | // Checking diagnostics implementation. |
| 149 | //===----------------------------------------------------------------------===// |
| 150 | |
| 151 | typedef TextDiagnosticBuffer::DiagList DiagList; |
| 152 | typedef TextDiagnosticBuffer::const_iterator const_diag_iterator; |
| 153 | |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 154 | namespace { |
| 155 | |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 156 | /// StandardDirective - Directive with string matching. |
| 157 | /// |
| 158 | class StandardDirective : public Directive { |
| 159 | public: |
Jordan Rose | aa48fe8 | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 160 | StandardDirective(SourceLocation DirectiveLoc, SourceLocation DiagnosticLoc, |
Jordan Rose | 3b81b7d | 2012-07-10 02:57:26 +0000 | [diff] [blame] | 161 | StringRef Text, unsigned Min, unsigned Max) |
| 162 | : Directive(DirectiveLoc, DiagnosticLoc, Text, Min, Max) { } |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 163 | |
| 164 | virtual bool isValid(std::string &Error) { |
| 165 | // all strings are considered valid; even empty ones |
| 166 | return true; |
| 167 | } |
| 168 | |
Jordan Rose | 4313c01 | 2012-07-10 02:56:15 +0000 | [diff] [blame] | 169 | virtual bool match(StringRef S) { |
| 170 | return S.find(Text) != StringRef::npos; |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 171 | } |
| 172 | }; |
| 173 | |
| 174 | /// RegexDirective - Directive with regular-expression matching. |
| 175 | /// |
| 176 | class RegexDirective : public Directive { |
| 177 | public: |
Jordan Rose | aa48fe8 | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 178 | RegexDirective(SourceLocation DirectiveLoc, SourceLocation DiagnosticLoc, |
Jordan Rose | 3b81b7d | 2012-07-10 02:57:26 +0000 | [diff] [blame] | 179 | StringRef Text, unsigned Min, unsigned Max) |
| 180 | : Directive(DirectiveLoc, DiagnosticLoc, Text, Min, Max), Regex(Text) { } |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 181 | |
| 182 | virtual bool isValid(std::string &Error) { |
| 183 | if (Regex.isValid(Error)) |
| 184 | return true; |
| 185 | return false; |
| 186 | } |
| 187 | |
Jordan Rose | 4313c01 | 2012-07-10 02:56:15 +0000 | [diff] [blame] | 188 | virtual bool match(StringRef S) { |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 189 | return Regex.match(S); |
| 190 | } |
| 191 | |
| 192 | private: |
| 193 | llvm::Regex Regex; |
| 194 | }; |
| 195 | |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 196 | class ParseHelper |
| 197 | { |
| 198 | public: |
Jordan Rose | 78541c4 | 2012-07-11 19:58:23 +0000 | [diff] [blame] | 199 | ParseHelper(StringRef S) |
| 200 | : Begin(S.begin()), End(S.end()), C(Begin), P(Begin), PEnd(NULL) { } |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 201 | |
| 202 | // Return true if string literal is next. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 203 | bool Next(StringRef S) { |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 204 | P = C; |
Benjamin Kramer | 0080f0c | 2010-09-01 17:28:48 +0000 | [diff] [blame] | 205 | PEnd = C + S.size(); |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 206 | if (PEnd > End) |
| 207 | return false; |
Benjamin Kramer | 0080f0c | 2010-09-01 17:28:48 +0000 | [diff] [blame] | 208 | return !memcmp(P, S.data(), S.size()); |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | // Return true if number is next. |
| 212 | // Output N only if number is next. |
| 213 | bool Next(unsigned &N) { |
| 214 | unsigned TMP = 0; |
| 215 | P = C; |
| 216 | for (; P < End && P[0] >= '0' && P[0] <= '9'; ++P) { |
| 217 | TMP *= 10; |
| 218 | TMP += P[0] - '0'; |
| 219 | } |
| 220 | if (P == C) |
| 221 | return false; |
| 222 | PEnd = P; |
| 223 | N = TMP; |
| 224 | return true; |
| 225 | } |
| 226 | |
| 227 | // Return true if string literal is found. |
| 228 | // When true, P marks begin-position of S in content. |
Andy Gibbs | 4a529d2 | 2012-10-19 12:36:49 +0000 | [diff] [blame^] | 229 | bool Search(StringRef S, bool EnsureStartOfWord = false) { |
| 230 | do { |
| 231 | P = std::search(C, End, S.begin(), S.end()); |
| 232 | PEnd = P + S.size(); |
| 233 | if (P == End) |
| 234 | break; |
| 235 | if (!EnsureStartOfWord |
| 236 | // Check if string literal starts a new word. |
| 237 | || P == Begin || isspace(P[-1]) |
| 238 | // Or it could be preceeded by the start of a comment. |
| 239 | || (P > (Begin + 1) && (P[-1] == '/' || P[-1] == '*') |
| 240 | && P[-2] == '/')) |
| 241 | return true; |
| 242 | // Otherwise, skip and search again. |
| 243 | } while (Advance()); |
| 244 | return false; |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | // Advance 1-past previous next/search. |
| 248 | // Behavior is undefined if previous next/search failed. |
| 249 | bool Advance() { |
| 250 | C = PEnd; |
| 251 | return C < End; |
| 252 | } |
| 253 | |
| 254 | // Skip zero or more whitespace. |
| 255 | void SkipWhitespace() { |
| 256 | for (; C < End && isspace(*C); ++C) |
| 257 | ; |
| 258 | } |
| 259 | |
| 260 | // Return true if EOF reached. |
| 261 | bool Done() { |
| 262 | return !(C < End); |
| 263 | } |
| 264 | |
| 265 | const char * const Begin; // beginning of expected content |
| 266 | const char * const End; // end of expected content (1-past) |
| 267 | const char *C; // position of next char in content |
| 268 | const char *P; |
| 269 | |
| 270 | private: |
| 271 | const char *PEnd; // previous next/search subject end (1-past) |
| 272 | }; |
| 273 | |
| 274 | } // namespace anonymous |
| 275 | |
| 276 | /// ParseDirective - Go through the comment and see if it indicates expected |
| 277 | /// diagnostics. If so, then put them in the appropriate directive list. |
| 278 | /// |
Jordan Rose | 78541c4 | 2012-07-11 19:58:23 +0000 | [diff] [blame] | 279 | /// Returns true if any valid directives were found. |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 280 | static bool ParseDirective(StringRef S, ExpectedData *ED, SourceManager &SM, |
Jordan Rose | 4313c01 | 2012-07-10 02:56:15 +0000 | [diff] [blame] | 281 | SourceLocation Pos, DiagnosticsEngine &Diags) { |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 282 | // A single comment may contain multiple directives. |
Jordan Rose | 78541c4 | 2012-07-11 19:58:23 +0000 | [diff] [blame] | 283 | bool FoundDirective = false; |
| 284 | for (ParseHelper PH(S); !PH.Done();) { |
Jordan Rose | aa48fe8 | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 285 | // Search for token: expected |
Andy Gibbs | 4a529d2 | 2012-10-19 12:36:49 +0000 | [diff] [blame^] | 286 | if (!PH.Search("expected", true)) |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 287 | break; |
| 288 | PH.Advance(); |
| 289 | |
Jordan Rose | aa48fe8 | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 290 | // Next token: - |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 291 | if (!PH.Next("-")) |
| 292 | continue; |
| 293 | PH.Advance(); |
| 294 | |
Jordan Rose | aa48fe8 | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 295 | // Next token: { error | warning | note } |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 296 | DirectiveList* DL = NULL; |
| 297 | if (PH.Next("error")) |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 298 | DL = ED ? &ED->Errors : NULL; |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 299 | else if (PH.Next("warning")) |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 300 | DL = ED ? &ED->Warnings : NULL; |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 301 | else if (PH.Next("note")) |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 302 | DL = ED ? &ED->Notes : NULL; |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 303 | else |
| 304 | continue; |
| 305 | PH.Advance(); |
| 306 | |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 307 | // If a directive has been found but we're not interested |
| 308 | // in storing the directive information, return now. |
| 309 | if (!DL) |
| 310 | return true; |
| 311 | |
Jordan Rose | aa48fe8 | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 312 | // Default directive kind. |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 313 | bool RegexKind = false; |
| 314 | const char* KindStr = "string"; |
| 315 | |
Jordan Rose | aa48fe8 | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 316 | // Next optional token: - |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 317 | if (PH.Next("-re")) { |
| 318 | PH.Advance(); |
| 319 | RegexKind = true; |
| 320 | KindStr = "regex"; |
| 321 | } |
| 322 | |
Jordan Rose | aa48fe8 | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 323 | // Next optional token: @ |
| 324 | SourceLocation ExpectedLoc; |
| 325 | if (!PH.Next("@")) { |
| 326 | ExpectedLoc = Pos; |
| 327 | } else { |
| 328 | PH.Advance(); |
| 329 | unsigned Line = 0; |
| 330 | bool FoundPlus = PH.Next("+"); |
| 331 | if (FoundPlus || PH.Next("-")) { |
| 332 | // Relative to current line. |
| 333 | PH.Advance(); |
| 334 | bool Invalid = false; |
| 335 | unsigned ExpectedLine = SM.getSpellingLineNumber(Pos, &Invalid); |
| 336 | if (!Invalid && PH.Next(Line) && (FoundPlus || Line < ExpectedLine)) { |
| 337 | if (FoundPlus) ExpectedLine += Line; |
| 338 | else ExpectedLine -= Line; |
| 339 | ExpectedLoc = SM.translateLineCol(SM.getFileID(Pos), ExpectedLine, 1); |
| 340 | } |
| 341 | } else { |
| 342 | // Absolute line number. |
| 343 | if (PH.Next(Line) && Line > 0) |
| 344 | ExpectedLoc = SM.translateLineCol(SM.getFileID(Pos), Line, 1); |
| 345 | } |
| 346 | |
| 347 | if (ExpectedLoc.isInvalid()) { |
| 348 | Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin), |
| 349 | diag::err_verify_missing_line) << KindStr; |
| 350 | continue; |
| 351 | } |
| 352 | PH.Advance(); |
| 353 | } |
| 354 | |
| 355 | // Skip optional whitespace. |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 356 | PH.SkipWhitespace(); |
| 357 | |
Jordan Rose | aa48fe8 | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 358 | // Next optional token: positive integer or a '+'. |
Jordan Rose | 3b81b7d | 2012-07-10 02:57:26 +0000 | [diff] [blame] | 359 | unsigned Min = 1; |
| 360 | unsigned Max = 1; |
| 361 | if (PH.Next(Min)) { |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 362 | PH.Advance(); |
Jordan Rose | 3b81b7d | 2012-07-10 02:57:26 +0000 | [diff] [blame] | 363 | // A positive integer can be followed by a '+' meaning min |
| 364 | // or more, or by a '-' meaning a range from min to max. |
| 365 | if (PH.Next("+")) { |
| 366 | Max = Directive::MaxCount; |
| 367 | PH.Advance(); |
| 368 | } else if (PH.Next("-")) { |
| 369 | PH.Advance(); |
| 370 | if (!PH.Next(Max) || Max < Min) { |
| 371 | Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin), |
| 372 | diag::err_verify_invalid_range) << KindStr; |
| 373 | continue; |
| 374 | } |
| 375 | PH.Advance(); |
| 376 | } else { |
| 377 | Max = Min; |
| 378 | } |
| 379 | } else if (PH.Next("+")) { |
| 380 | // '+' on its own means "1 or more". |
| 381 | Max = Directive::MaxCount; |
Anna Zaks | 2135ebb | 2011-12-15 02:28:16 +0000 | [diff] [blame] | 382 | PH.Advance(); |
| 383 | } |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 384 | |
Jordan Rose | aa48fe8 | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 385 | // Skip optional whitespace. |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 386 | PH.SkipWhitespace(); |
| 387 | |
Jordan Rose | aa48fe8 | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 388 | // Next token: {{ |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 389 | if (!PH.Next("{{")) { |
Jordan Rose | 4313c01 | 2012-07-10 02:56:15 +0000 | [diff] [blame] | 390 | Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin), |
| 391 | diag::err_verify_missing_start) << KindStr; |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 392 | continue; |
| 393 | } |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 394 | PH.Advance(); |
| 395 | const char* const ContentBegin = PH.C; // mark content begin |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 396 | |
Jordan Rose | aa48fe8 | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 397 | // Search for token: }} |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 398 | if (!PH.Search("}}")) { |
Jordan Rose | 4313c01 | 2012-07-10 02:56:15 +0000 | [diff] [blame] | 399 | Diags.Report(Pos.getLocWithOffset(PH.C-PH.Begin), |
| 400 | diag::err_verify_missing_end) << KindStr; |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 401 | continue; |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 402 | } |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 403 | const char* const ContentEnd = PH.P; // mark content end |
| 404 | PH.Advance(); |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 405 | |
Jordan Rose | aa48fe8 | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 406 | // Build directive text; convert \n to newlines. |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 407 | std::string Text; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 408 | StringRef NewlineStr = "\\n"; |
| 409 | StringRef Content(ContentBegin, ContentEnd-ContentBegin); |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 410 | size_t CPos = 0; |
| 411 | size_t FPos; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 412 | while ((FPos = Content.find(NewlineStr, CPos)) != StringRef::npos) { |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 413 | Text += Content.substr(CPos, FPos-CPos); |
| 414 | Text += '\n'; |
| 415 | CPos = FPos + NewlineStr.size(); |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 416 | } |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 417 | if (Text.empty()) |
| 418 | Text.assign(ContentBegin, ContentEnd); |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 419 | |
Jordan Rose | aa48fe8 | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 420 | // Construct new directive. |
Jordan Rose | 3b81b7d | 2012-07-10 02:57:26 +0000 | [diff] [blame] | 421 | Directive *D = Directive::create(RegexKind, Pos, ExpectedLoc, Text, |
| 422 | Min, Max); |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 423 | std::string Error; |
Jordan Rose | 78541c4 | 2012-07-11 19:58:23 +0000 | [diff] [blame] | 424 | if (D->isValid(Error)) { |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 425 | DL->push_back(D); |
Jordan Rose | 78541c4 | 2012-07-11 19:58:23 +0000 | [diff] [blame] | 426 | FoundDirective = true; |
| 427 | } else { |
Jordan Rose | 4313c01 | 2012-07-10 02:56:15 +0000 | [diff] [blame] | 428 | Diags.Report(Pos.getLocWithOffset(ContentBegin-PH.Begin), |
| 429 | diag::err_verify_invalid_content) |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 430 | << KindStr << Error; |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 431 | } |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 432 | } |
Jordan Rose | 78541c4 | 2012-07-11 19:58:23 +0000 | [diff] [blame] | 433 | |
| 434 | return FoundDirective; |
| 435 | } |
| 436 | |
| 437 | /// HandleComment - Hook into the preprocessor and extract comments containing |
| 438 | /// expected errors and warnings. |
| 439 | bool VerifyDiagnosticConsumer::HandleComment(Preprocessor &PP, |
| 440 | SourceRange Comment) { |
| 441 | SourceManager &SM = PP.getSourceManager(); |
| 442 | SourceLocation CommentBegin = Comment.getBegin(); |
| 443 | |
| 444 | const char *CommentRaw = SM.getCharacterData(CommentBegin); |
| 445 | StringRef C(CommentRaw, SM.getCharacterData(Comment.getEnd()) - CommentRaw); |
| 446 | |
| 447 | if (C.empty()) |
| 448 | return false; |
| 449 | |
| 450 | // Fold any "\<EOL>" sequences |
| 451 | size_t loc = C.find('\\'); |
| 452 | if (loc == StringRef::npos) { |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 453 | ParseDirective(C, &ED, SM, CommentBegin, PP.getDiagnostics()); |
Jordan Rose | 78541c4 | 2012-07-11 19:58:23 +0000 | [diff] [blame] | 454 | return false; |
| 455 | } |
| 456 | |
| 457 | std::string C2; |
| 458 | C2.reserve(C.size()); |
| 459 | |
| 460 | for (size_t last = 0;; loc = C.find('\\', last)) { |
| 461 | if (loc == StringRef::npos || loc == C.size()) { |
| 462 | C2 += C.substr(last); |
| 463 | break; |
| 464 | } |
| 465 | C2 += C.substr(last, loc-last); |
| 466 | last = loc + 1; |
| 467 | |
| 468 | if (C[last] == '\n' || C[last] == '\r') { |
| 469 | ++last; |
| 470 | |
| 471 | // Escape \r\n or \n\r, but not \n\n. |
| 472 | if (last < C.size()) |
| 473 | if (C[last] == '\n' || C[last] == '\r') |
| 474 | if (C[last] != C[last-1]) |
| 475 | ++last; |
| 476 | } else { |
| 477 | // This was just a normal backslash. |
| 478 | C2 += '\\'; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | if (!C2.empty()) |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 483 | ParseDirective(C2, &ED, SM, CommentBegin, PP.getDiagnostics()); |
Jordan Rose | 78541c4 | 2012-07-11 19:58:23 +0000 | [diff] [blame] | 484 | return false; |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 487 | #ifndef NDEBUG |
| 488 | /// \brief Lex the specified source file to determine whether it contains |
| 489 | /// any expected-* directives. As a Lexer is used rather than a full-blown |
| 490 | /// Preprocessor, directives inside skipped #if blocks will still be found. |
| 491 | /// |
| 492 | /// \return true if any directives were found. |
Jordan Rose | 7eaaa18 | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 493 | static bool findDirectives(SourceManager &SM, FileID FID, |
| 494 | const LangOptions &LangOpts) { |
Axel Naumann | 0123161 | 2011-07-25 19:18:12 +0000 | [diff] [blame] | 495 | // Create a raw lexer to pull all the comments out of FID. |
| 496 | if (FID.isInvalid()) |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 497 | return false; |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 498 | |
| 499 | // Create a lexer to lex all the tokens of the main file in raw mode. |
Chris Lattner | 6e29014 | 2009-11-30 04:18:44 +0000 | [diff] [blame] | 500 | const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID); |
Jordan Rose | 7eaaa18 | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 501 | Lexer RawLex(FID, FromFile, SM, LangOpts); |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 502 | |
| 503 | // Return comments as tokens, this is how we find expected diagnostics. |
| 504 | RawLex.SetCommentRetentionState(true); |
| 505 | |
| 506 | Token Tok; |
| 507 | Tok.setKind(tok::comment); |
| 508 | while (Tok.isNot(tok::eof)) { |
| 509 | RawLex.Lex(Tok); |
| 510 | if (!Tok.is(tok::comment)) continue; |
| 511 | |
Jordan Rose | 7eaaa18 | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 512 | std::string Comment = RawLex.getSpelling(Tok, SM, LangOpts); |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 513 | if (Comment.empty()) continue; |
| 514 | |
Jordan Rose | 7eaaa18 | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 515 | // Find first directive. |
| 516 | if (ParseDirective(Comment, 0, SM, Tok.getLocation(), |
| 517 | SM.getDiagnostics())) |
| 518 | return true; |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 519 | } |
Jordan Rose | 7eaaa18 | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 520 | return false; |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 521 | } |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 522 | #endif // !NDEBUG |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 523 | |
Jordan Rose | aa48fe8 | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 524 | /// \brief Takes a list of diagnostics that have been generated but not matched |
| 525 | /// by an expected-* directive and produces a diagnostic to the user from this. |
| 526 | static unsigned PrintUnexpected(DiagnosticsEngine &Diags, SourceManager *SourceMgr, |
| 527 | const_diag_iterator diag_begin, |
| 528 | const_diag_iterator diag_end, |
| 529 | const char *Kind) { |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 530 | if (diag_begin == diag_end) return 0; |
| 531 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 532 | SmallString<256> Fmt; |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 533 | llvm::raw_svector_ostream OS(Fmt); |
| 534 | for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I) { |
Daniel Dunbar | 221c721 | 2009-11-14 07:53:24 +0000 | [diff] [blame] | 535 | if (I->first.isInvalid() || !SourceMgr) |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 536 | OS << "\n (frontend)"; |
| 537 | else |
Chandler Carruth | 5ef04ee | 2011-02-23 00:47:48 +0000 | [diff] [blame] | 538 | OS << "\n Line " << SourceMgr->getPresumedLineNumber(I->first); |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 539 | OS << ": " << I->second; |
| 540 | } |
| 541 | |
Jordan Rose | c6d64a2 | 2012-07-11 16:50:36 +0000 | [diff] [blame] | 542 | Diags.Report(diag::err_verify_inconsistent_diags).setForceEmit() |
Jordan Rose | aa48fe8 | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 543 | << Kind << /*Unexpected=*/true << OS.str(); |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 544 | return std::distance(diag_begin, diag_end); |
| 545 | } |
| 546 | |
Jordan Rose | aa48fe8 | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 547 | /// \brief Takes a list of diagnostics that were expected to have been generated |
| 548 | /// but were not and produces a diagnostic to the user from this. |
| 549 | static unsigned PrintExpected(DiagnosticsEngine &Diags, SourceManager &SourceMgr, |
| 550 | DirectiveList &DL, const char *Kind) { |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 551 | if (DL.empty()) |
| 552 | return 0; |
| 553 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 554 | SmallString<256> Fmt; |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 555 | llvm::raw_svector_ostream OS(Fmt); |
| 556 | for (DirectiveList::iterator I = DL.begin(), E = DL.end(); I != E; ++I) { |
Jordan Rose | aa48fe8 | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 557 | Directive &D = **I; |
| 558 | OS << "\n Line " << SourceMgr.getPresumedLineNumber(D.DiagnosticLoc); |
| 559 | if (D.DirectiveLoc != D.DiagnosticLoc) |
| 560 | OS << " (directive at " |
| 561 | << SourceMgr.getFilename(D.DirectiveLoc) << ":" |
| 562 | << SourceMgr.getPresumedLineNumber(D.DirectiveLoc) << ")"; |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 563 | OS << ": " << D.Text; |
| 564 | } |
| 565 | |
Jordan Rose | c6d64a2 | 2012-07-11 16:50:36 +0000 | [diff] [blame] | 566 | Diags.Report(diag::err_verify_inconsistent_diags).setForceEmit() |
Jordan Rose | aa48fe8 | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 567 | << Kind << /*Unexpected=*/false << OS.str(); |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 568 | return DL.size(); |
| 569 | } |
| 570 | |
| 571 | /// CheckLists - Compare expected to seen diagnostic lists and return the |
| 572 | /// the difference between them. |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 573 | /// |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 574 | static unsigned CheckLists(DiagnosticsEngine &Diags, SourceManager &SourceMgr, |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 575 | const char *Label, |
| 576 | DirectiveList &Left, |
| 577 | const_diag_iterator d2_begin, |
| 578 | const_diag_iterator d2_end) { |
| 579 | DirectiveList LeftOnly; |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 580 | DiagList Right(d2_begin, d2_end); |
| 581 | |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 582 | for (DirectiveList::iterator I = Left.begin(), E = Left.end(); I != E; ++I) { |
| 583 | Directive& D = **I; |
Jordan Rose | aa48fe8 | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 584 | unsigned LineNo1 = SourceMgr.getPresumedLineNumber(D.DiagnosticLoc); |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 585 | |
Jordan Rose | 3b81b7d | 2012-07-10 02:57:26 +0000 | [diff] [blame] | 586 | for (unsigned i = 0; i < D.Max; ++i) { |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 587 | DiagList::iterator II, IE; |
| 588 | for (II = Right.begin(), IE = Right.end(); II != IE; ++II) { |
Chandler Carruth | 5ef04ee | 2011-02-23 00:47:48 +0000 | [diff] [blame] | 589 | unsigned LineNo2 = SourceMgr.getPresumedLineNumber(II->first); |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 590 | if (LineNo1 != LineNo2) |
| 591 | continue; |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 592 | |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 593 | const std::string &RightText = II->second; |
Jordan Rose | 4313c01 | 2012-07-10 02:56:15 +0000 | [diff] [blame] | 594 | if (D.match(RightText)) |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 595 | break; |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 596 | } |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 597 | if (II == IE) { |
| 598 | // Not found. |
Jordan Rose | 3b81b7d | 2012-07-10 02:57:26 +0000 | [diff] [blame] | 599 | if (i >= D.Min) break; |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 600 | LeftOnly.push_back(*I); |
| 601 | } else { |
| 602 | // Found. The same cannot be found twice. |
| 603 | Right.erase(II); |
| 604 | } |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | // Now all that's left in Right are those that were not matched. |
Jordan Rose | aa48fe8 | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 608 | unsigned num = PrintExpected(Diags, SourceMgr, LeftOnly, Label); |
| 609 | num += PrintUnexpected(Diags, &SourceMgr, Right.begin(), Right.end(), Label); |
NAKAMURA Takumi | ad64684 | 2011-12-17 13:00:31 +0000 | [diff] [blame] | 610 | return num; |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | /// CheckResults - This compares the expected results to those that |
| 614 | /// were actually reported. It emits any discrepencies. Return "true" if there |
| 615 | /// were problems. Return "false" otherwise. |
| 616 | /// |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 617 | static unsigned CheckResults(DiagnosticsEngine &Diags, SourceManager &SourceMgr, |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 618 | const TextDiagnosticBuffer &Buffer, |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 619 | ExpectedData &ED) { |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 620 | // We want to capture the delta between what was expected and what was |
| 621 | // seen. |
| 622 | // |
| 623 | // Expected \ Seen - set expected but not seen |
| 624 | // Seen \ Expected - set seen but not expected |
| 625 | unsigned NumProblems = 0; |
| 626 | |
| 627 | // See if there are error mismatches. |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 628 | NumProblems += CheckLists(Diags, SourceMgr, "error", ED.Errors, |
| 629 | Buffer.err_begin(), Buffer.err_end()); |
Daniel Dunbar | 221c721 | 2009-11-14 07:53:24 +0000 | [diff] [blame] | 630 | |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 631 | // See if there are warning mismatches. |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 632 | NumProblems += CheckLists(Diags, SourceMgr, "warning", ED.Warnings, |
| 633 | Buffer.warn_begin(), Buffer.warn_end()); |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 634 | |
| 635 | // See if there are note mismatches. |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 636 | NumProblems += CheckLists(Diags, SourceMgr, "note", ED.Notes, |
| 637 | Buffer.note_begin(), Buffer.note_end()); |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 638 | |
| 639 | return NumProblems; |
| 640 | } |
| 641 | |
Jordan Rose | 7eaaa18 | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 642 | void VerifyDiagnosticConsumer::UpdateParsedFileStatus(SourceManager &SM, |
| 643 | FileID FID, |
| 644 | ParsedStatus PS) { |
| 645 | // Check SourceManager hasn't changed. |
| 646 | setSourceManager(SM); |
| 647 | |
| 648 | #ifndef NDEBUG |
| 649 | if (FID.isInvalid()) |
| 650 | return; |
| 651 | |
| 652 | const FileEntry *FE = SM.getFileEntryForID(FID); |
| 653 | |
| 654 | if (PS == IsParsed) { |
| 655 | // Move the FileID from the unparsed set to the parsed set. |
| 656 | UnparsedFiles.erase(FID); |
| 657 | ParsedFiles.insert(std::make_pair(FID, FE)); |
| 658 | } else if (!ParsedFiles.count(FID) && !UnparsedFiles.count(FID)) { |
| 659 | // Add the FileID to the unparsed set if we haven't seen it before. |
| 660 | |
| 661 | // Check for directives. |
| 662 | bool FoundDirectives; |
| 663 | if (PS == IsUnparsedNoDirectives) |
| 664 | FoundDirectives = false; |
| 665 | else |
| 666 | FoundDirectives = !LangOpts || findDirectives(SM, FID, *LangOpts); |
| 667 | |
| 668 | // Add the FileID to the unparsed set. |
| 669 | UnparsedFiles.insert(std::make_pair(FID, |
| 670 | UnparsedFileStatus(FE, FoundDirectives))); |
| 671 | } |
| 672 | #endif |
| 673 | } |
| 674 | |
David Blaikie | 621bc69 | 2011-09-26 00:38:03 +0000 | [diff] [blame] | 675 | void VerifyDiagnosticConsumer::CheckDiagnostics() { |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 676 | // Ensure any diagnostics go to the primary client. |
Douglas Gregor | 7824365 | 2011-09-13 01:26:44 +0000 | [diff] [blame] | 677 | bool OwnsCurClient = Diags.ownsClient(); |
David Blaikie | 78ad0b9 | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 678 | DiagnosticConsumer *CurClient = Diags.takeClient(); |
Douglas Gregor | 7824365 | 2011-09-13 01:26:44 +0000 | [diff] [blame] | 679 | Diags.setClient(PrimaryClient, false); |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 680 | |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 681 | #ifndef NDEBUG |
Jordan Rose | 7eaaa18 | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 682 | // In a debug build, scan through any files that may have been missed |
| 683 | // during parsing and issue a fatal error if directives are contained |
| 684 | // within these files. If a fatal error occurs, this suggests that |
| 685 | // this file is being parsed separately from the main file, in which |
| 686 | // case consider moving the directives to the correct place, if this |
| 687 | // is applicable. |
| 688 | if (UnparsedFiles.size() > 0) { |
| 689 | // Generate a cache of parsed FileEntry pointers for alias lookups. |
| 690 | llvm::SmallPtrSet<const FileEntry *, 8> ParsedFileCache; |
| 691 | for (ParsedFilesMap::iterator I = ParsedFiles.begin(), |
| 692 | End = ParsedFiles.end(); I != End; ++I) { |
| 693 | if (const FileEntry *FE = I->second) |
| 694 | ParsedFileCache.insert(FE); |
| 695 | } |
| 696 | |
| 697 | // Iterate through list of unparsed files. |
| 698 | for (UnparsedFilesMap::iterator I = UnparsedFiles.begin(), |
| 699 | End = UnparsedFiles.end(); I != End; ++I) { |
| 700 | const UnparsedFileStatus &Status = I->second; |
| 701 | const FileEntry *FE = Status.getFile(); |
| 702 | |
| 703 | // Skip files that have been parsed via an alias. |
| 704 | if (FE && ParsedFileCache.count(FE)) |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 705 | continue; |
| 706 | |
Jordan Rose | 7eaaa18 | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 707 | // Report a fatal error if this file contained directives. |
| 708 | if (Status.foundDirectives()) { |
Jordan Rose | 7c304f5 | 2012-08-10 01:06:16 +0000 | [diff] [blame] | 709 | llvm::report_fatal_error(Twine("-verify directives found after rather" |
| 710 | " than during normal parsing of ", |
Jordan Rose | 7eaaa18 | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 711 | StringRef(FE ? FE->getName() : "(unknown)"))); |
| 712 | } |
Axel Naumann | 84c05e3 | 2011-08-24 13:36:19 +0000 | [diff] [blame] | 713 | } |
Daniel Dunbar | 221c721 | 2009-11-14 07:53:24 +0000 | [diff] [blame] | 714 | |
Jordan Rose | 7eaaa18 | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 715 | // UnparsedFiles has been processed now, so clear it. |
| 716 | UnparsedFiles.clear(); |
| 717 | } |
| 718 | #endif // !NDEBUG |
| 719 | |
| 720 | if (SrcManager) { |
Daniel Dunbar | 221c721 | 2009-11-14 07:53:24 +0000 | [diff] [blame] | 721 | // Check that the expected diagnostics occurred. |
Jordan Rose | 7eaaa18 | 2012-08-18 16:58:52 +0000 | [diff] [blame] | 722 | NumErrors += CheckResults(Diags, *SrcManager, *Buffer, ED); |
Daniel Dunbar | 221c721 | 2009-11-14 07:53:24 +0000 | [diff] [blame] | 723 | } else { |
Jordan Rose | aa48fe8 | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 724 | NumErrors += (PrintUnexpected(Diags, 0, Buffer->err_begin(), |
| 725 | Buffer->err_end(), "error") + |
| 726 | PrintUnexpected(Diags, 0, Buffer->warn_begin(), |
| 727 | Buffer->warn_end(), "warn") + |
| 728 | PrintUnexpected(Diags, 0, Buffer->note_begin(), |
| 729 | Buffer->note_end(), "note")); |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 730 | } |
| 731 | |
Douglas Gregor | bdbb004 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 732 | Diags.takeClient(); |
Douglas Gregor | 7824365 | 2011-09-13 01:26:44 +0000 | [diff] [blame] | 733 | Diags.setClient(CurClient, OwnsCurClient); |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 734 | |
| 735 | // Reset the buffer, we have processed all the diagnostics in it. |
| 736 | Buffer.reset(new TextDiagnosticBuffer()); |
Axel Naumann | e445e5d | 2012-07-10 16:24:07 +0000 | [diff] [blame] | 737 | ED.Errors.clear(); |
| 738 | ED.Warnings.clear(); |
| 739 | ED.Notes.clear(); |
Daniel Dunbar | 81f5a1e | 2009-11-14 03:23:19 +0000 | [diff] [blame] | 740 | } |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 741 | |
Douglas Gregor | aee526e | 2011-09-29 00:38:00 +0000 | [diff] [blame] | 742 | DiagnosticConsumer * |
| 743 | VerifyDiagnosticConsumer::clone(DiagnosticsEngine &Diags) const { |
| 744 | if (!Diags.getClient()) |
| 745 | Diags.setClient(PrimaryClient->clone(Diags)); |
| 746 | |
| 747 | return new VerifyDiagnosticConsumer(Diags); |
| 748 | } |
| 749 | |
Jordan Rose | aa48fe8 | 2012-07-10 02:57:03 +0000 | [diff] [blame] | 750 | Directive *Directive::create(bool RegexKind, SourceLocation DirectiveLoc, |
| 751 | SourceLocation DiagnosticLoc, StringRef Text, |
Jordan Rose | 3b81b7d | 2012-07-10 02:57:26 +0000 | [diff] [blame] | 752 | unsigned Min, unsigned Max) { |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 753 | if (RegexKind) |
Jordan Rose | 3b81b7d | 2012-07-10 02:57:26 +0000 | [diff] [blame] | 754 | return new RegexDirective(DirectiveLoc, DiagnosticLoc, Text, Min, Max); |
| 755 | return new StandardDirective(DirectiveLoc, DiagnosticLoc, Text, Min, Max); |
Chris Lattner | 60909e1 | 2010-04-28 20:02:30 +0000 | [diff] [blame] | 756 | } |