Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- DiagChecker.cpp - Diagnostic Checking Functions ------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 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. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | eb8c963 | 2007-10-07 06:04:32 +0000 | [diff] [blame] | 15 | #include "ASTConsumers.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 16 | #include "TextDiagnosticBuffer.h" |
Chris Lattner | 0bed6ec | 2008-02-06 00:23:21 +0000 | [diff] [blame] | 17 | #include "clang/Sema/ParseAST.h" |
Chris Lattner | 1cc0171 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTConsumer.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | 12ee82e | 2007-09-16 19:27:16 +0000 | [diff] [blame] | 54 | size_type ColNo = 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 55 | |
| 56 | for (;;) { |
| 57 | ColNo = Comment.find(ExpectedStr, ColNo); |
| 58 | if (ColNo == std::string::npos) break; |
| 59 | |
Bill Wendling | c08e97b | 2007-11-26 08:26:20 +0000 | [diff] [blame] | 60 | size_type OpenDiag = Comment.find("{{", ColNo); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 61 | |
| 62 | if (OpenDiag == std::string::npos) { |
| 63 | fprintf(stderr, |
| 64 | "oops:%d: Cannot find beginning of expected error string\n", |
| 65 | SourceMgr.getLogicalLineNumber(Pos)); |
| 66 | break; |
| 67 | } |
| 68 | |
| 69 | OpenDiag += 2; |
Bill Wendling | c08e97b | 2007-11-26 08:26:20 +0000 | [diff] [blame] | 70 | size_type CloseDiag = Comment.find("}}", OpenDiag); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 71 | |
| 72 | if (CloseDiag == std::string::npos) { |
| 73 | fprintf(stderr, |
| 74 | "oops:%d: Cannot find end of expected error string\n", |
| 75 | SourceMgr.getLogicalLineNumber(Pos)); |
| 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 | |
Ted Kremenek | 17861c5 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 85 | /// FindExpectedDiags - Lex the main source file to find all of the |
| 86 | // expected errors and warnings. |
| 87 | static void FindExpectedDiags(Preprocessor &PP, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 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. |
Ted Kremenek | 17861c5 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 94 | PP.EnterMainSourceFile(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 95 | |
Chris Lattner | 2db971b | 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); |
Chris Lattner | 4478db9 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 99 | for (unsigned i = 0; i != diag::NUM_BUILTIN_DIAGNOSTICS; ++i) |
| 100 | if (PP.getDiagnostics().isBuiltinNoteWarningOrExtension((diag::kind)i)) |
Chris Lattner | 2db971b | 2007-08-30 06:34:23 +0000 | [diff] [blame] | 101 | PP.getDiagnostics().setDiagnosticMapping((diag::kind)i, diag::MAP_IGNORE); |
| 102 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 103 | Token Tok; |
| 104 | do { |
| 105 | PP.Lex(Tok); |
| 106 | |
Chris Lattner | 3b49415 | 2007-10-09 18:03:42 +0000 | [diff] [blame] | 107 | if (Tok.is(tok::comment)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | 3b49415 | 2007-10-09 18:03:42 +0000 | [diff] [blame] | 118 | } while (Tok.isNot(tok::eof)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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", |
| 138 | SourceMgr.getLogicalLineNumber(I->first), |
| 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) { |
| 156 | unsigned LineNo1 = SourceMgr.getLogicalLineNumber(I->first); |
| 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) { |
| 161 | unsigned LineNo2 = SourceMgr.getLogicalLineNumber(II->first); |
| 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 | 4b6f475 | 2007-08-10 18:27:41 +0000 | [diff] [blame] | 226 | |
Ted Kremenek | 0841c70 | 2007-09-25 18:37:20 +0000 | [diff] [blame] | 227 | /// CheckASTConsumer - Implement diagnostic checking for AST consumers. |
Ted Kremenek | 17861c5 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 228 | bool clang::CheckASTConsumer(Preprocessor &PP, ASTConsumer* C) { |
| 229 | |
Chris Lattner | 8593cbf | 2007-11-03 06:24:16 +0000 | [diff] [blame] | 230 | // Parse the AST and run the consumer, ultimately deleting C. |
Ted Kremenek | 17861c5 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 231 | ParseAST(PP, C); |
Argiris Kirtzidis | 8d83376 | 2008-06-13 12:15:34 +0000 | [diff] [blame] | 232 | return CheckDiagnostics(PP); |
| 233 | } |
| 234 | |
| 235 | /// CheckDiagnostics - Gather the expected diagnostics and check them. |
| 236 | bool clang::CheckDiagnostics(Preprocessor &PP) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 237 | // Gather the set of expected diagnostics. |
| 238 | DiagList ExpectedErrors, ExpectedWarnings; |
Ted Kremenek | 17861c5 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 239 | FindExpectedDiags(PP, ExpectedErrors, ExpectedWarnings); |
Ted Kremenek | 0841c70 | 2007-09-25 18:37:20 +0000 | [diff] [blame] | 240 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 241 | // Check that the expected diagnostics occurred. |
| 242 | return CheckResults(PP, ExpectedErrors, ExpectedWarnings); |
| 243 | } |