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)); |
| 81 | ExpectedDiags.push_back(std::make_pair(Pos, Msg)); |
| 82 | ColNo = CloseDiag + 2; |
| 83 | } |
| 84 | } |
| 85 | |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 86 | /// FindExpectedDiags - Lex the main source file to find all of the |
| 87 | // expected errors and warnings. |
| 88 | static void FindExpectedDiags(Preprocessor &PP, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 89 | DiagList &ExpectedErrors, |
Douglas Gregor | 233f74b | 2008-09-11 02:46:36 +0000 | [diff] [blame] | 90 | DiagList &ExpectedWarnings, |
| 91 | DiagList &ExpectedNotes) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 92 | // Return comments as tokens, this is how we find expected diagnostics. |
| 93 | PP.SetCommentRetentionState(true, true); |
| 94 | |
| 95 | // Enter the cave. |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 96 | PP.EnterMainSourceFile(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 97 | |
Chris Lattner | f01654f | 2007-08-30 06:34:23 +0000 | [diff] [blame] | 98 | // Turn off all warnings from relexing or preprocessing. |
| 99 | PP.getDiagnostics().setWarnOnExtensions(false); |
| 100 | PP.getDiagnostics().setErrorOnExtensions(false); |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 101 | for (unsigned i = 0; i != diag::NUM_BUILTIN_DIAGNOSTICS; ++i) |
| 102 | if (PP.getDiagnostics().isBuiltinNoteWarningOrExtension((diag::kind)i)) |
Chris Lattner | f01654f | 2007-08-30 06:34:23 +0000 | [diff] [blame] | 103 | PP.getDiagnostics().setDiagnosticMapping((diag::kind)i, diag::MAP_IGNORE); |
| 104 | |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 105 | Token Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 106 | do { |
| 107 | PP.Lex(Tok); |
| 108 | |
Chris Lattner | 057aaf6 | 2007-10-09 18:03:42 +0000 | [diff] [blame] | 109 | if (Tok.is(tok::comment)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 110 | std::string Comment = PP.getSpelling(Tok); |
| 111 | |
| 112 | // Find all expected errors |
| 113 | FindDiagnostics(Comment, ExpectedErrors,PP.getSourceManager(), |
| 114 | Tok.getLocation(), ExpectedErrStr); |
| 115 | |
| 116 | // Find all expected warnings |
| 117 | FindDiagnostics(Comment, ExpectedWarnings, PP.getSourceManager(), |
| 118 | Tok.getLocation(), ExpectedWarnStr); |
Douglas Gregor | 233f74b | 2008-09-11 02:46:36 +0000 | [diff] [blame] | 119 | |
| 120 | // Find all expected notes |
| 121 | FindDiagnostics(Comment, ExpectedNotes, PP.getSourceManager(), |
| 122 | Tok.getLocation(), ExpectedNoteStr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 123 | } |
Chris Lattner | 057aaf6 | 2007-10-09 18:03:42 +0000 | [diff] [blame] | 124 | } while (Tok.isNot(tok::eof)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 125 | |
| 126 | PP.SetCommentRetentionState(false, false); |
| 127 | } |
| 128 | |
| 129 | /// PrintProblem - This takes a diagnostic map of the delta between expected and |
| 130 | /// seen diagnostics. If there's anything in it, then something unexpected |
| 131 | /// happened. Print the map out in a nice format and return "true". If the map |
| 132 | /// is empty and we're not going to print things, then return "false". |
| 133 | /// |
| 134 | static bool PrintProblem(SourceManager &SourceMgr, |
| 135 | const_diag_iterator diag_begin, |
| 136 | const_diag_iterator diag_end, |
| 137 | const char *Msg) { |
| 138 | if (diag_begin == diag_end) return false; |
| 139 | |
| 140 | fprintf(stderr, "%s\n", Msg); |
| 141 | |
| 142 | for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I) |
| 143 | fprintf(stderr, " Line %d: %s\n", |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 144 | SourceMgr.getLogicalLineNumber(I->first), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 145 | I->second.c_str()); |
| 146 | |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | /// CompareDiagLists - Compare two diangnostic lists and return the difference |
| 151 | /// between them. |
| 152 | /// |
| 153 | static bool CompareDiagLists(SourceManager &SourceMgr, |
| 154 | const_diag_iterator d1_begin, |
| 155 | const_diag_iterator d1_end, |
| 156 | const_diag_iterator d2_begin, |
| 157 | const_diag_iterator d2_end, |
| 158 | const char *Msg) { |
| 159 | DiagList DiffList; |
| 160 | |
| 161 | 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] | 162 | unsigned LineNo1 = SourceMgr.getLogicalLineNumber(I->first); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 163 | const std::string &Diag1 = I->second; |
| 164 | bool Found = false; |
| 165 | |
| 166 | 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] | 167 | unsigned LineNo2 = SourceMgr.getLogicalLineNumber(II->first); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 168 | if (LineNo1 != LineNo2) continue; |
| 169 | |
| 170 | const std::string &Diag2 = II->second; |
| 171 | if (Diag2.find(Diag1) != std::string::npos || |
| 172 | Diag1.find(Diag2) != std::string::npos) { |
| 173 | Found = true; |
| 174 | break; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | if (!Found) |
| 179 | DiffList.push_back(std::make_pair(I->first, Diag1)); |
| 180 | } |
| 181 | |
| 182 | return PrintProblem(SourceMgr, DiffList.begin(), DiffList.end(), Msg); |
| 183 | } |
| 184 | |
| 185 | /// CheckResults - This compares the expected results to those that |
| 186 | /// were actually reported. It emits any discrepencies. Return "true" if there |
| 187 | /// were problems. Return "false" otherwise. |
| 188 | /// |
| 189 | static bool CheckResults(Preprocessor &PP, |
| 190 | const DiagList &ExpectedErrors, |
Douglas Gregor | 233f74b | 2008-09-11 02:46:36 +0000 | [diff] [blame] | 191 | const DiagList &ExpectedWarnings, |
| 192 | const DiagList &ExpectedNotes) { |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 193 | const DiagnosticClient *DiagClient = PP.getDiagnostics().getClient(); |
| 194 | assert(DiagClient != 0 && |
| 195 | "DiagChecker requires a valid TextDiagnosticBuffer"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 196 | const TextDiagnosticBuffer &Diags = |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 197 | static_cast<const TextDiagnosticBuffer&>(*DiagClient); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 198 | SourceManager &SourceMgr = PP.getSourceManager(); |
| 199 | |
| 200 | // We want to capture the delta between what was expected and what was |
| 201 | // seen. |
| 202 | // |
| 203 | // Expected \ Seen - set expected but not seen |
| 204 | // Seen \ Expected - set seen but not expected |
| 205 | bool HadProblem = false; |
| 206 | |
| 207 | // See if there were errors that were expected but not seen. |
| 208 | HadProblem |= CompareDiagLists(SourceMgr, |
| 209 | ExpectedErrors.begin(), ExpectedErrors.end(), |
| 210 | Diags.err_begin(), Diags.err_end(), |
| 211 | "Errors expected but not seen:"); |
| 212 | |
| 213 | // See if there were errors that were seen but not expected. |
| 214 | HadProblem |= CompareDiagLists(SourceMgr, |
| 215 | Diags.err_begin(), Diags.err_end(), |
| 216 | ExpectedErrors.begin(), ExpectedErrors.end(), |
| 217 | "Errors seen but not expected:"); |
| 218 | |
| 219 | // See if there were warnings that were expected but not seen. |
| 220 | HadProblem |= CompareDiagLists(SourceMgr, |
| 221 | ExpectedWarnings.begin(), |
| 222 | ExpectedWarnings.end(), |
| 223 | Diags.warn_begin(), Diags.warn_end(), |
| 224 | "Warnings expected but not seen:"); |
| 225 | |
| 226 | // See if there were warnings that were seen but not expected. |
| 227 | HadProblem |= CompareDiagLists(SourceMgr, |
| 228 | Diags.warn_begin(), Diags.warn_end(), |
| 229 | ExpectedWarnings.begin(), |
| 230 | ExpectedWarnings.end(), |
| 231 | "Warnings seen but not expected:"); |
| 232 | |
Douglas Gregor | 233f74b | 2008-09-11 02:46:36 +0000 | [diff] [blame] | 233 | // See if there were notes that were expected but not seen. |
| 234 | HadProblem |= CompareDiagLists(SourceMgr, |
| 235 | ExpectedNotes.begin(), |
| 236 | ExpectedNotes.end(), |
| 237 | Diags.note_begin(), Diags.note_end(), |
| 238 | "Notes expected but not seen:"); |
| 239 | |
| 240 | // See if there were notes that were seen but not expected. |
| 241 | HadProblem |= CompareDiagLists(SourceMgr, |
| 242 | Diags.note_begin(), Diags.note_end(), |
| 243 | ExpectedNotes.begin(), |
| 244 | ExpectedNotes.end(), |
| 245 | "Notes seen but not expected:"); |
| 246 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 247 | return HadProblem; |
| 248 | } |
| 249 | |
Chris Lattner | 009e9f7 | 2007-08-10 18:27:41 +0000 | [diff] [blame] | 250 | |
Ted Kremenek | 4457978 | 2007-09-25 18:37:20 +0000 | [diff] [blame] | 251 | /// CheckASTConsumer - Implement diagnostic checking for AST consumers. |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 252 | bool clang::CheckASTConsumer(Preprocessor &PP, ASTConsumer* C) { |
| 253 | |
Chris Lattner | 31e6c7d | 2007-11-03 06:24:16 +0000 | [diff] [blame] | 254 | // Parse the AST and run the consumer, ultimately deleting C. |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 255 | ParseAST(PP, C); |
Argyrios Kyrtzidis | 14d4140 | 2008-06-13 12:15:34 +0000 | [diff] [blame] | 256 | return CheckDiagnostics(PP); |
| 257 | } |
| 258 | |
| 259 | /// CheckDiagnostics - Gather the expected diagnostics and check them. |
| 260 | bool clang::CheckDiagnostics(Preprocessor &PP) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 261 | // Gather the set of expected diagnostics. |
Douglas Gregor | 233f74b | 2008-09-11 02:46:36 +0000 | [diff] [blame] | 262 | DiagList ExpectedErrors, ExpectedWarnings, ExpectedNotes; |
| 263 | FindExpectedDiags(PP, ExpectedErrors, ExpectedWarnings, ExpectedNotes); |
Ted Kremenek | 4457978 | 2007-09-25 18:37:20 +0000 | [diff] [blame] | 264 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 265 | // Check that the expected diagnostics occurred. |
Douglas Gregor | 233f74b | 2008-09-11 02:46:36 +0000 | [diff] [blame] | 266 | return CheckResults(PP, ExpectedErrors, ExpectedWarnings, ExpectedNotes); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 267 | } |