Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- DiagChecker.cpp - Diagnostic Checking Functions ------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Process the input files and check that the diagnostic messages are expected. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang.h" |
Chris Lattner | 97e8b6f | 2007-10-07 06:04:32 +0000 | [diff] [blame] | 15 | #include "ASTConsumers.h" |
Nico Weber | fd54ebc | 2008-08-05 23:33:20 +0000 | [diff] [blame] | 16 | #include "clang/Driver/TextDiagnosticBuffer.h" |
Chris Lattner | e91c134 | 2008-02-06 00:23:21 +0000 | [diff] [blame] | 17 | #include "clang/Sema/ParseAST.h" |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTConsumer.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 19 | #include "clang/Basic/SourceManager.h" |
| 20 | #include "clang/Lex/Preprocessor.h" |
| 21 | using namespace clang; |
| 22 | |
| 23 | typedef TextDiagnosticBuffer::DiagList DiagList; |
| 24 | typedef TextDiagnosticBuffer::const_iterator const_diag_iterator; |
| 25 | |
| 26 | // USING THE DIAGNOSTIC CHECKER: |
| 27 | // |
| 28 | // Indicating that a line expects an error or a warning is simple. Put a comment |
| 29 | // on the line that has the diagnostic, use "expected-{error,warning}" to tag |
| 30 | // if it's an expected error or warning, and place the expected text between {{ |
| 31 | // and }} markers. The full text doesn't have to be included, only enough to |
| 32 | // ensure that the correct diagnostic was emitted. |
| 33 | // |
| 34 | // Here's an example: |
| 35 | // |
| 36 | // int A = B; // expected-error {{use of undeclared identifier 'B'}} |
| 37 | // |
| 38 | // You can place as many diagnostics on one line as you wish. To make the code |
| 39 | // more readable, you can use slash-newline to separate out the diagnostics. |
| 40 | |
| 41 | static const char * const ExpectedErrStr = "expected-error"; |
| 42 | static const char * const ExpectedWarnStr = "expected-warning"; |
Douglas Gregor | 233f74b | 2008-09-11 02:46:36 +0000 | [diff] [blame] | 43 | static const char * const ExpectedNoteStr = "expected-note"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 44 | |
| 45 | /// FindDiagnostics - Go through the comment and see if it indicates expected |
| 46 | /// diagnostics. If so, then put them in a diagnostic list. |
| 47 | /// |
| 48 | static void FindDiagnostics(const std::string &Comment, |
| 49 | DiagList &ExpectedDiags, |
| 50 | SourceManager &SourceMgr, |
| 51 | SourceLocation Pos, |
| 52 | const char * const ExpectedStr) { |
| 53 | // Find all expected diagnostics |
| 54 | typedef std::string::size_type size_type; |
Chris Lattner | dce7bd6 | 2007-09-16 19:27:16 +0000 | [diff] [blame] | 55 | size_type ColNo = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 56 | |
| 57 | for (;;) { |
| 58 | ColNo = Comment.find(ExpectedStr, ColNo); |
| 59 | if (ColNo == std::string::npos) break; |
| 60 | |
Bill Wendling | f5f20bd | 2007-11-26 08:26:20 +0000 | [diff] [blame] | 61 | size_type OpenDiag = Comment.find("{{", ColNo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 62 | |
| 63 | if (OpenDiag == std::string::npos) { |
| 64 | fprintf(stderr, |
| 65 | "oops:%d: Cannot find beginning of expected error string\n", |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 66 | SourceMgr.getLogicalLineNumber(Pos)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 67 | break; |
| 68 | } |
| 69 | |
| 70 | OpenDiag += 2; |
Bill Wendling | f5f20bd | 2007-11-26 08:26:20 +0000 | [diff] [blame] | 71 | size_type CloseDiag = Comment.find("}}", OpenDiag); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 72 | |
| 73 | if (CloseDiag == std::string::npos) { |
| 74 | fprintf(stderr, |
| 75 | "oops:%d: Cannot find end of expected error string\n", |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 76 | SourceMgr.getLogicalLineNumber(Pos)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 77 | break; |
| 78 | } |
| 79 | |
| 80 | std::string Msg(Comment.substr(OpenDiag, CloseDiag - OpenDiag)); |
Sebastian Redl | ad3c91c | 2008-10-26 19:05:16 +0000 | [diff] [blame] | 81 | size_type FindPos; |
| 82 | while((FindPos = Msg.find("\\n")) != std::string::npos) { |
| 83 | Msg.replace(FindPos, 2, "\n"); |
| 84 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 85 | ExpectedDiags.push_back(std::make_pair(Pos, Msg)); |
| 86 | ColNo = CloseDiag + 2; |
| 87 | } |
| 88 | } |
| 89 | |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 90 | /// FindExpectedDiags - Lex the main source file to find all of the |
| 91 | // expected errors and warnings. |
| 92 | static void FindExpectedDiags(Preprocessor &PP, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 93 | DiagList &ExpectedErrors, |
Douglas Gregor | 233f74b | 2008-09-11 02:46:36 +0000 | [diff] [blame] | 94 | DiagList &ExpectedWarnings, |
| 95 | DiagList &ExpectedNotes) { |
Chris Lattner | a39f048 | 2008-11-21 01:18:36 +0000 | [diff] [blame] | 96 | // Create a raw lexer to pull all the comments out of the main file. We don't |
| 97 | // want to look in #include'd headers for expected-error strings. |
Chris Lattner | f01654f | 2007-08-30 06:34:23 +0000 | [diff] [blame] | 98 | |
Chris Lattner | a39f048 | 2008-11-21 01:18:36 +0000 | [diff] [blame] | 99 | unsigned FileID = PP.getSourceManager().getMainFileID(); |
| 100 | std::pair<const char*,const char*> File = |
| 101 | PP.getSourceManager().getBufferData(FileID); |
| 102 | |
| 103 | // Create a lexer to lex all the tokens of the main file in raw mode. |
| 104 | Lexer RawLex(SourceLocation::getFileLoc(FileID, 0), |
| 105 | PP.getLangOptions(), File.first, File.second); |
| 106 | |
| 107 | // Return comments as tokens, this is how we find expected diagnostics. |
| 108 | RawLex.SetCommentRetentionState(true); |
| 109 | |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 110 | Token Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 111 | do { |
Chris Lattner | a39f048 | 2008-11-21 01:18:36 +0000 | [diff] [blame] | 112 | RawLex.Lex(Tok); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 113 | |
Chris Lattner | 057aaf6 | 2007-10-09 18:03:42 +0000 | [diff] [blame] | 114 | if (Tok.is(tok::comment)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 115 | std::string Comment = PP.getSpelling(Tok); |
| 116 | |
| 117 | // Find all expected errors |
Chris Lattner | a39f048 | 2008-11-21 01:18:36 +0000 | [diff] [blame] | 118 | FindDiagnostics(Comment, ExpectedErrors, PP.getSourceManager(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 119 | Tok.getLocation(), ExpectedErrStr); |
| 120 | |
| 121 | // Find all expected warnings |
| 122 | FindDiagnostics(Comment, ExpectedWarnings, PP.getSourceManager(), |
| 123 | Tok.getLocation(), ExpectedWarnStr); |
Douglas Gregor | 233f74b | 2008-09-11 02:46:36 +0000 | [diff] [blame] | 124 | |
| 125 | // Find all expected notes |
| 126 | FindDiagnostics(Comment, ExpectedNotes, PP.getSourceManager(), |
| 127 | Tok.getLocation(), ExpectedNoteStr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 128 | } |
Chris Lattner | 057aaf6 | 2007-10-09 18:03:42 +0000 | [diff] [blame] | 129 | } while (Tok.isNot(tok::eof)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /// PrintProblem - This takes a diagnostic map of the delta between expected and |
| 133 | /// seen diagnostics. If there's anything in it, then something unexpected |
| 134 | /// happened. Print the map out in a nice format and return "true". If the map |
| 135 | /// is empty and we're not going to print things, then return "false". |
| 136 | /// |
| 137 | static bool PrintProblem(SourceManager &SourceMgr, |
| 138 | const_diag_iterator diag_begin, |
| 139 | const_diag_iterator diag_end, |
| 140 | const char *Msg) { |
| 141 | if (diag_begin == diag_end) return false; |
| 142 | |
| 143 | fprintf(stderr, "%s\n", Msg); |
| 144 | |
| 145 | for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I) |
| 146 | fprintf(stderr, " Line %d: %s\n", |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 147 | SourceMgr.getLogicalLineNumber(I->first), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 148 | I->second.c_str()); |
| 149 | |
| 150 | return true; |
| 151 | } |
| 152 | |
| 153 | /// CompareDiagLists - Compare two diangnostic lists and return the difference |
| 154 | /// between them. |
| 155 | /// |
| 156 | static bool CompareDiagLists(SourceManager &SourceMgr, |
| 157 | const_diag_iterator d1_begin, |
| 158 | const_diag_iterator d1_end, |
| 159 | const_diag_iterator d2_begin, |
| 160 | const_diag_iterator d2_end, |
| 161 | const char *Msg) { |
| 162 | DiagList DiffList; |
| 163 | |
| 164 | for (const_diag_iterator I = d1_begin, E = d1_end; I != E; ++I) { |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 165 | unsigned LineNo1 = SourceMgr.getLogicalLineNumber(I->first); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 166 | const std::string &Diag1 = I->second; |
| 167 | bool Found = false; |
| 168 | |
| 169 | for (const_diag_iterator II = d2_begin, IE = d2_end; II != IE; ++II) { |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 170 | unsigned LineNo2 = SourceMgr.getLogicalLineNumber(II->first); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 171 | if (LineNo1 != LineNo2) continue; |
| 172 | |
| 173 | const std::string &Diag2 = II->second; |
| 174 | if (Diag2.find(Diag1) != std::string::npos || |
| 175 | Diag1.find(Diag2) != std::string::npos) { |
| 176 | Found = true; |
| 177 | break; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | if (!Found) |
| 182 | DiffList.push_back(std::make_pair(I->first, Diag1)); |
| 183 | } |
| 184 | |
| 185 | return PrintProblem(SourceMgr, DiffList.begin(), DiffList.end(), Msg); |
| 186 | } |
| 187 | |
| 188 | /// CheckResults - This compares the expected results to those that |
| 189 | /// were actually reported. It emits any discrepencies. Return "true" if there |
| 190 | /// were problems. Return "false" otherwise. |
| 191 | /// |
| 192 | static bool CheckResults(Preprocessor &PP, |
| 193 | const DiagList &ExpectedErrors, |
Douglas Gregor | 233f74b | 2008-09-11 02:46:36 +0000 | [diff] [blame] | 194 | const DiagList &ExpectedWarnings, |
| 195 | const DiagList &ExpectedNotes) { |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 196 | const DiagnosticClient *DiagClient = PP.getDiagnostics().getClient(); |
| 197 | assert(DiagClient != 0 && |
| 198 | "DiagChecker requires a valid TextDiagnosticBuffer"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 199 | const TextDiagnosticBuffer &Diags = |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 200 | static_cast<const TextDiagnosticBuffer&>(*DiagClient); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 201 | SourceManager &SourceMgr = PP.getSourceManager(); |
| 202 | |
| 203 | // We want to capture the delta between what was expected and what was |
| 204 | // seen. |
| 205 | // |
| 206 | // Expected \ Seen - set expected but not seen |
| 207 | // Seen \ Expected - set seen but not expected |
| 208 | bool HadProblem = false; |
| 209 | |
| 210 | // See if there were errors that were expected but not seen. |
| 211 | HadProblem |= CompareDiagLists(SourceMgr, |
| 212 | ExpectedErrors.begin(), ExpectedErrors.end(), |
| 213 | Diags.err_begin(), Diags.err_end(), |
| 214 | "Errors expected but not seen:"); |
| 215 | |
| 216 | // See if there were errors that were seen but not expected. |
| 217 | HadProblem |= CompareDiagLists(SourceMgr, |
| 218 | Diags.err_begin(), Diags.err_end(), |
| 219 | ExpectedErrors.begin(), ExpectedErrors.end(), |
| 220 | "Errors seen but not expected:"); |
| 221 | |
| 222 | // See if there were warnings that were expected but not seen. |
| 223 | HadProblem |= CompareDiagLists(SourceMgr, |
| 224 | ExpectedWarnings.begin(), |
| 225 | ExpectedWarnings.end(), |
| 226 | Diags.warn_begin(), Diags.warn_end(), |
| 227 | "Warnings expected but not seen:"); |
| 228 | |
| 229 | // See if there were warnings that were seen but not expected. |
| 230 | HadProblem |= CompareDiagLists(SourceMgr, |
| 231 | Diags.warn_begin(), Diags.warn_end(), |
| 232 | ExpectedWarnings.begin(), |
| 233 | ExpectedWarnings.end(), |
| 234 | "Warnings seen but not expected:"); |
| 235 | |
Douglas Gregor | 233f74b | 2008-09-11 02:46:36 +0000 | [diff] [blame] | 236 | // See if there were notes that were expected but not seen. |
| 237 | HadProblem |= CompareDiagLists(SourceMgr, |
| 238 | ExpectedNotes.begin(), |
| 239 | ExpectedNotes.end(), |
| 240 | Diags.note_begin(), Diags.note_end(), |
| 241 | "Notes expected but not seen:"); |
| 242 | |
| 243 | // See if there were notes that were seen but not expected. |
| 244 | HadProblem |= CompareDiagLists(SourceMgr, |
| 245 | Diags.note_begin(), Diags.note_end(), |
| 246 | ExpectedNotes.begin(), |
| 247 | ExpectedNotes.end(), |
| 248 | "Notes seen but not expected:"); |
| 249 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 250 | return HadProblem; |
| 251 | } |
| 252 | |
Chris Lattner | 009e9f7 | 2007-08-10 18:27:41 +0000 | [diff] [blame] | 253 | |
Argyrios Kyrtzidis | 14d4140 | 2008-06-13 12:15:34 +0000 | [diff] [blame] | 254 | /// CheckDiagnostics - Gather the expected diagnostics and check them. |
| 255 | bool clang::CheckDiagnostics(Preprocessor &PP) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 256 | // Gather the set of expected diagnostics. |
Douglas Gregor | 233f74b | 2008-09-11 02:46:36 +0000 | [diff] [blame] | 257 | DiagList ExpectedErrors, ExpectedWarnings, ExpectedNotes; |
| 258 | FindExpectedDiags(PP, ExpectedErrors, ExpectedWarnings, ExpectedNotes); |
Ted Kremenek | 4457978 | 2007-09-25 18:37:20 +0000 | [diff] [blame] | 259 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 260 | // Check that the expected diagnostics occurred. |
Douglas Gregor | 233f74b | 2008-09-11 02:46:36 +0000 | [diff] [blame] | 261 | return CheckResults(PP, ExpectedErrors, ExpectedWarnings, ExpectedNotes); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 262 | } |