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 | // |
| 5 | // This file was developed by Bill Wendling and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 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" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 16 | #include "TextDiagnosticBuffer.h" |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 17 | #include "clang/Sema/ASTStreamer.h" |
| 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"; |
| 43 | |
| 44 | /// FindDiagnostics - Go through the comment and see if it indicates expected |
| 45 | /// diagnostics. If so, then put them in a diagnostic list. |
| 46 | /// |
| 47 | static void FindDiagnostics(const std::string &Comment, |
| 48 | DiagList &ExpectedDiags, |
| 49 | SourceManager &SourceMgr, |
| 50 | SourceLocation Pos, |
| 51 | const char * const ExpectedStr) { |
| 52 | // Find all expected diagnostics |
| 53 | typedef std::string::size_type size_type; |
Chris Lattner | dce7bd6 | 2007-09-16 19:27:16 +0000 | [diff] [blame] | 54 | size_type ColNo = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 55 | |
| 56 | for (;;) { |
| 57 | ColNo = Comment.find(ExpectedStr, ColNo); |
| 58 | if (ColNo == std::string::npos) break; |
| 59 | |
| 60 | size_type OpenDiag = Comment.find_first_of("{{", ColNo); |
| 61 | |
| 62 | if (OpenDiag == std::string::npos) { |
| 63 | fprintf(stderr, |
| 64 | "oops:%d: Cannot find beginning of expected error string\n", |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 65 | SourceMgr.getLogicalLineNumber(Pos)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 66 | break; |
| 67 | } |
| 68 | |
| 69 | OpenDiag += 2; |
| 70 | size_type CloseDiag = Comment.find_first_of("}}", OpenDiag); |
| 71 | |
| 72 | if (CloseDiag == std::string::npos) { |
| 73 | fprintf(stderr, |
| 74 | "oops:%d: Cannot find end of expected error string\n", |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 75 | SourceMgr.getLogicalLineNumber(Pos)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 76 | break; |
| 77 | } |
| 78 | |
| 79 | std::string Msg(Comment.substr(OpenDiag, CloseDiag - OpenDiag)); |
| 80 | ExpectedDiags.push_back(std::make_pair(Pos, Msg)); |
| 81 | ColNo = CloseDiag + 2; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /// FindExpectedDiags - Lex the file to finds all of the expected errors and |
| 86 | /// warnings. |
| 87 | static void FindExpectedDiags(Preprocessor &PP, unsigned MainFileID, |
| 88 | DiagList &ExpectedErrors, |
| 89 | DiagList &ExpectedWarnings) { |
| 90 | // Return comments as tokens, this is how we find expected diagnostics. |
| 91 | PP.SetCommentRetentionState(true, true); |
| 92 | |
| 93 | // Enter the cave. |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 94 | PP.EnterMainSourceFile(MainFileID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 95 | |
Chris Lattner | f01654f | 2007-08-30 06:34:23 +0000 | [diff] [blame] | 96 | // Turn off all warnings from relexing or preprocessing. |
| 97 | PP.getDiagnostics().setWarnOnExtensions(false); |
| 98 | PP.getDiagnostics().setErrorOnExtensions(false); |
| 99 | for (unsigned i = 0; i != diag::NUM_DIAGNOSTICS; ++i) |
| 100 | if (PP.getDiagnostics().isNoteWarningOrExtension((diag::kind)i)) |
| 101 | PP.getDiagnostics().setDiagnosticMapping((diag::kind)i, diag::MAP_IGNORE); |
| 102 | |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 103 | Token Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 104 | do { |
| 105 | PP.Lex(Tok); |
| 106 | |
Chris Lattner | 057aaf6 | 2007-10-09 18:03:42 +0000 | [diff] [blame] | 107 | if (Tok.is(tok::comment)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 108 | std::string Comment = PP.getSpelling(Tok); |
| 109 | |
| 110 | // Find all expected errors |
| 111 | FindDiagnostics(Comment, ExpectedErrors,PP.getSourceManager(), |
| 112 | Tok.getLocation(), ExpectedErrStr); |
| 113 | |
| 114 | // Find all expected warnings |
| 115 | FindDiagnostics(Comment, ExpectedWarnings, PP.getSourceManager(), |
| 116 | Tok.getLocation(), ExpectedWarnStr); |
| 117 | } |
Chris Lattner | 057aaf6 | 2007-10-09 18:03:42 +0000 | [diff] [blame] | 118 | } while (Tok.isNot(tok::eof)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 119 | |
| 120 | PP.SetCommentRetentionState(false, false); |
| 121 | } |
| 122 | |
| 123 | /// PrintProblem - This takes a diagnostic map of the delta between expected and |
| 124 | /// seen diagnostics. If there's anything in it, then something unexpected |
| 125 | /// happened. Print the map out in a nice format and return "true". If the map |
| 126 | /// is empty and we're not going to print things, then return "false". |
| 127 | /// |
| 128 | static bool PrintProblem(SourceManager &SourceMgr, |
| 129 | const_diag_iterator diag_begin, |
| 130 | const_diag_iterator diag_end, |
| 131 | const char *Msg) { |
| 132 | if (diag_begin == diag_end) return false; |
| 133 | |
| 134 | fprintf(stderr, "%s\n", Msg); |
| 135 | |
| 136 | for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I) |
| 137 | fprintf(stderr, " Line %d: %s\n", |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 138 | SourceMgr.getLogicalLineNumber(I->first), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 139 | I->second.c_str()); |
| 140 | |
| 141 | return true; |
| 142 | } |
| 143 | |
| 144 | /// CompareDiagLists - Compare two diangnostic lists and return the difference |
| 145 | /// between them. |
| 146 | /// |
| 147 | static bool CompareDiagLists(SourceManager &SourceMgr, |
| 148 | const_diag_iterator d1_begin, |
| 149 | const_diag_iterator d1_end, |
| 150 | const_diag_iterator d2_begin, |
| 151 | const_diag_iterator d2_end, |
| 152 | const char *Msg) { |
| 153 | DiagList DiffList; |
| 154 | |
| 155 | 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] | 156 | unsigned LineNo1 = SourceMgr.getLogicalLineNumber(I->first); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 157 | const std::string &Diag1 = I->second; |
| 158 | bool Found = false; |
| 159 | |
| 160 | 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] | 161 | unsigned LineNo2 = SourceMgr.getLogicalLineNumber(II->first); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 162 | if (LineNo1 != LineNo2) continue; |
| 163 | |
| 164 | const std::string &Diag2 = II->second; |
| 165 | if (Diag2.find(Diag1) != std::string::npos || |
| 166 | Diag1.find(Diag2) != std::string::npos) { |
| 167 | Found = true; |
| 168 | break; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | if (!Found) |
| 173 | DiffList.push_back(std::make_pair(I->first, Diag1)); |
| 174 | } |
| 175 | |
| 176 | return PrintProblem(SourceMgr, DiffList.begin(), DiffList.end(), Msg); |
| 177 | } |
| 178 | |
| 179 | /// CheckResults - This compares the expected results to those that |
| 180 | /// were actually reported. It emits any discrepencies. Return "true" if there |
| 181 | /// were problems. Return "false" otherwise. |
| 182 | /// |
| 183 | static bool CheckResults(Preprocessor &PP, |
| 184 | const DiagList &ExpectedErrors, |
| 185 | const DiagList &ExpectedWarnings) { |
| 186 | const TextDiagnosticBuffer &Diags = |
| 187 | static_cast<const TextDiagnosticBuffer&>(PP.getDiagnostics().getClient()); |
| 188 | SourceManager &SourceMgr = PP.getSourceManager(); |
| 189 | |
| 190 | // We want to capture the delta between what was expected and what was |
| 191 | // seen. |
| 192 | // |
| 193 | // Expected \ Seen - set expected but not seen |
| 194 | // Seen \ Expected - set seen but not expected |
| 195 | bool HadProblem = false; |
| 196 | |
| 197 | // See if there were errors that were expected but not seen. |
| 198 | HadProblem |= CompareDiagLists(SourceMgr, |
| 199 | ExpectedErrors.begin(), ExpectedErrors.end(), |
| 200 | Diags.err_begin(), Diags.err_end(), |
| 201 | "Errors expected but not seen:"); |
| 202 | |
| 203 | // See if there were errors that were seen but not expected. |
| 204 | HadProblem |= CompareDiagLists(SourceMgr, |
| 205 | Diags.err_begin(), Diags.err_end(), |
| 206 | ExpectedErrors.begin(), ExpectedErrors.end(), |
| 207 | "Errors seen but not expected:"); |
| 208 | |
| 209 | // See if there were warnings that were expected but not seen. |
| 210 | HadProblem |= CompareDiagLists(SourceMgr, |
| 211 | ExpectedWarnings.begin(), |
| 212 | ExpectedWarnings.end(), |
| 213 | Diags.warn_begin(), Diags.warn_end(), |
| 214 | "Warnings expected but not seen:"); |
| 215 | |
| 216 | // See if there were warnings that were seen but not expected. |
| 217 | HadProblem |= CompareDiagLists(SourceMgr, |
| 218 | Diags.warn_begin(), Diags.warn_end(), |
| 219 | ExpectedWarnings.begin(), |
| 220 | ExpectedWarnings.end(), |
| 221 | "Warnings seen but not expected:"); |
| 222 | |
| 223 | return HadProblem; |
| 224 | } |
| 225 | |
Chris Lattner | 009e9f7 | 2007-08-10 18:27:41 +0000 | [diff] [blame] | 226 | |
Ted Kremenek | 4457978 | 2007-09-25 18:37:20 +0000 | [diff] [blame] | 227 | /// CheckASTConsumer - Implement diagnostic checking for AST consumers. |
| 228 | bool clang::CheckASTConsumer(Preprocessor &PP, unsigned MainFileID, |
Ted Kremenek | d39bcd8 | 2007-09-26 18:39:29 +0000 | [diff] [blame] | 229 | ASTConsumer* C) { |
Chris Lattner | 31e6c7d | 2007-11-03 06:24:16 +0000 | [diff] [blame] | 230 | // Parse the AST and run the consumer, ultimately deleting C. |
| 231 | ParseAST(PP, MainFileID, C); |
Ted Kremenek | 4457978 | 2007-09-25 18:37:20 +0000 | [diff] [blame] | 232 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 233 | // Gather the set of expected diagnostics. |
| 234 | DiagList ExpectedErrors, ExpectedWarnings; |
| 235 | FindExpectedDiags(PP, MainFileID, ExpectedErrors, ExpectedWarnings); |
Ted Kremenek | 4457978 | 2007-09-25 18:37:20 +0000 | [diff] [blame] | 236 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 237 | // Check that the expected diagnostics occurred. |
| 238 | return CheckResults(PP, ExpectedErrors, ExpectedWarnings); |
| 239 | } |