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