shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1 | #!/usr/bin/python2.4 |
| 2 | # |
| 3 | # Copyright 2008, Google Inc. |
| 4 | # All rights reserved. |
| 5 | # |
| 6 | # Redistribution and use in source and binary forms, with or without |
| 7 | # modification, are permitted provided that the following conditions are |
| 8 | # met: |
| 9 | # |
| 10 | # * Redistributions of source code must retain the above copyright |
| 11 | # notice, this list of conditions and the following disclaimer. |
| 12 | # * Redistributions in binary form must reproduce the above |
| 13 | # copyright notice, this list of conditions and the following disclaimer |
| 14 | # in the documentation and/or other materials provided with the |
| 15 | # distribution. |
| 16 | # * Neither the name of Google Inc. nor the names of its |
| 17 | # contributors may be used to endorse or promote products derived from |
| 18 | # this software without specific prior written permission. |
| 19 | # |
| 20 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | |
| 32 | """Converts gcc errors in code using Google Mock to plain English.""" |
| 33 | |
| 34 | __author__ = 'wan@google.com (Zhanyong Wan)' |
| 35 | |
| 36 | import re |
| 37 | import sys |
| 38 | |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 39 | _VERSION = '1.0.3' |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 40 | |
| 41 | _COMMON_GMOCK_SYMBOLS = [ |
| 42 | # Matchers |
| 43 | '_', |
| 44 | 'A', |
| 45 | 'AddressSatisfies', |
| 46 | 'AllOf', |
| 47 | 'An', |
| 48 | 'AnyOf', |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 49 | 'ContainerEq', |
| 50 | 'Contains', |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 51 | 'ContainsRegex', |
| 52 | 'DoubleEq', |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 53 | 'ElementsAre', |
| 54 | 'ElementsAreArray', |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 55 | 'EndsWith', |
| 56 | 'Eq', |
| 57 | 'Field', |
| 58 | 'FloatEq', |
| 59 | 'Ge', |
| 60 | 'Gt', |
| 61 | 'HasSubstr', |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 62 | 'IsInitializedProto', |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 63 | 'Le', |
| 64 | 'Lt', |
| 65 | 'MatcherCast', |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 66 | 'Matches', |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 67 | 'MatchesRegex', |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 68 | 'NanSensitiveDoubleEq', |
| 69 | 'NanSensitiveFloatEq', |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 70 | 'Ne', |
| 71 | 'Not', |
| 72 | 'NotNull', |
| 73 | 'Pointee', |
| 74 | 'Property', |
| 75 | 'Ref', |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 76 | 'ResultOf', |
| 77 | 'SafeMatcherCast', |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 78 | 'StartsWith', |
| 79 | 'StrCaseEq', |
| 80 | 'StrCaseNe', |
| 81 | 'StrEq', |
| 82 | 'StrNe', |
| 83 | 'Truly', |
| 84 | 'TypedEq', |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 85 | 'Value', |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 86 | |
| 87 | # Actions |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 88 | 'Assign', |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 89 | 'ByRef', |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 90 | 'DeleteArg', |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 91 | 'DoAll', |
| 92 | 'DoDefault', |
| 93 | 'IgnoreResult', |
| 94 | 'Invoke', |
| 95 | 'InvokeArgument', |
| 96 | 'InvokeWithoutArgs', |
| 97 | 'Return', |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 98 | 'ReturnNew', |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 99 | 'ReturnNull', |
| 100 | 'ReturnRef', |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 101 | 'SaveArg', |
| 102 | 'SetArgReferee', |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 103 | 'SetArgumentPointee', |
| 104 | 'SetArrayArgument', |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 105 | 'SetErrnoAndReturn', |
| 106 | 'Throw', |
| 107 | 'WithArg', |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 108 | 'WithArgs', |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 109 | 'WithoutArgs', |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 110 | |
| 111 | # Cardinalities |
| 112 | 'AnyNumber', |
| 113 | 'AtLeast', |
| 114 | 'AtMost', |
| 115 | 'Between', |
| 116 | 'Exactly', |
| 117 | |
| 118 | # Sequences |
| 119 | 'InSequence', |
| 120 | 'Sequence', |
| 121 | |
| 122 | # Misc |
| 123 | 'DefaultValue', |
| 124 | 'Mock', |
| 125 | ] |
| 126 | |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 127 | # Regex for matching source file path and line number in gcc's errors. |
| 128 | _FILE_LINE_RE = r'(?P<file>.*):(?P<line>\d+):\s+' |
| 129 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 130 | |
| 131 | def _FindAllMatches(regex, s): |
| 132 | """Generates all matches of regex in string s.""" |
| 133 | |
| 134 | r = re.compile(regex) |
| 135 | return r.finditer(s) |
| 136 | |
| 137 | |
| 138 | def _GenericDiagnoser(short_name, long_name, regex, diagnosis, msg): |
| 139 | """Diagnoses the given disease by pattern matching. |
| 140 | |
| 141 | Args: |
| 142 | short_name: Short name of the disease. |
| 143 | long_name: Long name of the disease. |
| 144 | regex: Regex for matching the symptoms. |
| 145 | diagnosis: Pattern for formatting the diagnosis. |
| 146 | msg: Gcc's error messages. |
| 147 | Yields: |
| 148 | Tuples of the form |
| 149 | (short name of disease, long name of disease, diagnosis). |
| 150 | """ |
| 151 | |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 152 | diagnosis = '%(file)s:%(line)s:' + diagnosis |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 153 | for m in _FindAllMatches(regex, msg): |
| 154 | yield (short_name, long_name, diagnosis % m.groupdict()) |
| 155 | |
| 156 | |
| 157 | def _NeedToReturnReferenceDiagnoser(msg): |
| 158 | """Diagnoses the NRR disease, given the error messages by gcc.""" |
| 159 | |
| 160 | regex = (r'In member function \'testing::internal::ReturnAction<R>.*\n' |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 161 | + _FILE_LINE_RE + r'instantiated from here\n' |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 162 | r'.*gmock-actions\.h.*error: creating array with negative size') |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 163 | diagnosis = """ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 164 | You are using an Return() action in a function that returns a reference. |
| 165 | Please use ReturnRef() instead.""" |
| 166 | return _GenericDiagnoser('NRR', 'Need to Return Reference', |
| 167 | regex, diagnosis, msg) |
| 168 | |
| 169 | |
| 170 | def _NeedToReturnSomethingDiagnoser(msg): |
| 171 | """Diagnoses the NRS disease, given the error messages by gcc.""" |
| 172 | |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 173 | regex = (_FILE_LINE_RE + |
zhanyong.wan | 16cf473 | 2009-05-14 20:55:30 +0000 | [diff] [blame] | 174 | r'(instantiated from here\n.' |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 175 | r'*gmock.*actions\.h.*error: void value not ignored)' |
zhanyong.wan | 16cf473 | 2009-05-14 20:55:30 +0000 | [diff] [blame] | 176 | r'|(error: control reaches end of non-void function)') |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 177 | diagnosis = """ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 178 | You are using an action that returns void, but it needs to return |
zhanyong.wan | 16cf473 | 2009-05-14 20:55:30 +0000 | [diff] [blame] | 179 | *something*. Please tell it *what* to return. Perhaps you can use |
| 180 | the pattern DoAll(some_action, Return(some_value))?""" |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 181 | return _GenericDiagnoser('NRS', 'Need to Return Something', |
| 182 | regex, diagnosis, msg) |
| 183 | |
| 184 | |
| 185 | def _NeedToReturnNothingDiagnoser(msg): |
| 186 | """Diagnoses the NRN disease, given the error messages by gcc.""" |
| 187 | |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 188 | regex = (_FILE_LINE_RE + r'instantiated from here\n' |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 189 | r'.*gmock-actions\.h.*error: return-statement with a value, ' |
| 190 | r'in function returning \'void\'') |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 191 | diagnosis = """ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 192 | You are using an action that returns *something*, but it needs to return |
| 193 | void. Please use a void-returning action instead. |
| 194 | |
| 195 | All actions but the last in DoAll(...) must return void. Perhaps you need |
| 196 | to re-arrange the order of actions in a DoAll(), if you are using one?""" |
| 197 | return _GenericDiagnoser('NRN', 'Need to Return Nothing', |
| 198 | regex, diagnosis, msg) |
| 199 | |
| 200 | |
| 201 | def _IncompleteByReferenceArgumentDiagnoser(msg): |
| 202 | """Diagnoses the IBRA disease, given the error messages by gcc.""" |
| 203 | |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 204 | regex = (_FILE_LINE_RE + r'instantiated from here\n' |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 205 | r'.*gmock-printers\.h.*error: invalid application of ' |
| 206 | r'\'sizeof\' to incomplete type \'(?P<type>.*)\'') |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 207 | diagnosis = """ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 208 | In order to mock this function, Google Mock needs to see the definition |
| 209 | of type "%(type)s" - declaration alone is not enough. Either #include |
| 210 | the header that defines it, or change the argument to be passed |
| 211 | by pointer.""" |
| 212 | return _GenericDiagnoser('IBRA', 'Incomplete By-Reference Argument Type', |
| 213 | regex, diagnosis, msg) |
| 214 | |
| 215 | |
| 216 | def _OverloadedFunctionMatcherDiagnoser(msg): |
| 217 | """Diagnoses the OFM disease, given the error messages by gcc.""" |
| 218 | |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 219 | regex = (_FILE_LINE_RE + r'error: no matching function for ' |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 220 | r'call to \'Truly\(<unresolved overloaded function type>\)') |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 221 | diagnosis = """ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 222 | The argument you gave to Truly() is an overloaded function. Please tell |
| 223 | gcc which overloaded version you want to use. |
| 224 | |
| 225 | For example, if you want to use the version whose signature is |
| 226 | bool Foo(int n); |
| 227 | you should write |
| 228 | Truly(static_cast<bool (*)(int n)>(Foo))""" |
| 229 | return _GenericDiagnoser('OFM', 'Overloaded Function Matcher', |
| 230 | regex, diagnosis, msg) |
| 231 | |
| 232 | |
| 233 | def _OverloadedFunctionActionDiagnoser(msg): |
| 234 | """Diagnoses the OFA disease, given the error messages by gcc.""" |
| 235 | |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 236 | regex = (_FILE_LINE_RE + r'error: no matching function for call to \'Invoke\(' |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 237 | r'<unresolved overloaded function type>') |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 238 | diagnosis = """ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 239 | You are passing an overloaded function to Invoke(). Please tell gcc |
| 240 | which overloaded version you want to use. |
| 241 | |
| 242 | For example, if you want to use the version whose signature is |
| 243 | bool MyFunction(int n, double x); |
| 244 | you should write something like |
| 245 | Invoke(static_cast<bool (*)(int n, double x)>(MyFunction))""" |
| 246 | return _GenericDiagnoser('OFA', 'Overloaded Function Action', |
| 247 | regex, diagnosis, msg) |
| 248 | |
| 249 | |
| 250 | def _OverloadedMethodActionDiagnoser1(msg): |
| 251 | """Diagnoses the OMA disease, given the error messages by gcc.""" |
| 252 | |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 253 | regex = (_FILE_LINE_RE + r'error: ' |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 254 | r'.*no matching function for call to \'Invoke\(.*, ' |
| 255 | r'unresolved overloaded function type>') |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 256 | diagnosis = """ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 257 | The second argument you gave to Invoke() is an overloaded method. Please |
| 258 | tell gcc which overloaded version you want to use. |
| 259 | |
| 260 | For example, if you want to use the version whose signature is |
| 261 | class Foo { |
| 262 | ... |
| 263 | bool Bar(int n, double x); |
| 264 | }; |
| 265 | you should write something like |
| 266 | Invoke(foo, static_cast<bool (Foo::*)(int n, double x)>(&Foo::Bar))""" |
| 267 | return _GenericDiagnoser('OMA', 'Overloaded Method Action', |
| 268 | regex, diagnosis, msg) |
| 269 | |
| 270 | |
| 271 | def _MockObjectPointerDiagnoser(msg): |
| 272 | """Diagnoses the MOP disease, given the error messages by gcc.""" |
| 273 | |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 274 | regex = (_FILE_LINE_RE + r'error: request for member ' |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 275 | r'\'gmock_(?P<method>.+)\' in \'(?P<mock_object>.+)\', ' |
| 276 | r'which is of non-class type \'(.*::)*(?P<class_name>.+)\*\'') |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 277 | diagnosis = """ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 278 | The first argument to ON_CALL() and EXPECT_CALL() must be a mock *object*, |
| 279 | not a *pointer* to it. Please write '*(%(mock_object)s)' instead of |
| 280 | '%(mock_object)s' as your first argument. |
| 281 | |
| 282 | For example, given the mock class: |
| 283 | |
| 284 | class %(class_name)s : public ... { |
| 285 | ... |
| 286 | MOCK_METHOD0(%(method)s, ...); |
| 287 | }; |
| 288 | |
| 289 | and the following mock instance: |
| 290 | |
| 291 | %(class_name)s* mock_ptr = ... |
| 292 | |
| 293 | you should use the EXPECT_CALL like this: |
| 294 | |
| 295 | EXPECT_CALL(*mock_ptr, %(method)s(...));""" |
| 296 | return _GenericDiagnoser('MOP', 'Mock Object Pointer', |
| 297 | regex, diagnosis, msg) |
| 298 | |
| 299 | |
| 300 | def _OverloadedMethodActionDiagnoser2(msg): |
| 301 | """Diagnoses the OMA disease, given the error messages by gcc.""" |
| 302 | |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 303 | regex = (_FILE_LINE_RE + r'error: no matching function for ' |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 304 | r'call to \'Invoke\(.+, <unresolved overloaded function type>\)') |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 305 | diagnosis = """ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 306 | The second argument you gave to Invoke() is an overloaded method. Please |
| 307 | tell gcc which overloaded version you want to use. |
| 308 | |
| 309 | For example, if you want to use the version whose signature is |
| 310 | class Foo { |
| 311 | ... |
| 312 | bool Bar(int n, double x); |
| 313 | }; |
| 314 | you should write something like |
| 315 | Invoke(foo, static_cast<bool (Foo::*)(int n, double x)>(&Foo::Bar))""" |
| 316 | return _GenericDiagnoser('OMA', 'Overloaded Method Action', |
| 317 | regex, diagnosis, msg) |
| 318 | |
| 319 | |
| 320 | def _NeedToUseSymbolDiagnoser(msg): |
| 321 | """Diagnoses the NUS disease, given the error messages by gcc.""" |
| 322 | |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 323 | regex = (_FILE_LINE_RE + r'error: \'(?P<symbol>.+)\' ' |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 324 | r'(was not declared in this scope|has not been declared)') |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 325 | diagnosis = """ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 326 | '%(symbol)s' is defined by Google Mock in the testing namespace. |
| 327 | Did you forget to write |
| 328 | using testing::%(symbol)s; |
| 329 | ?""" |
| 330 | for m in _FindAllMatches(regex, msg): |
| 331 | symbol = m.groupdict()['symbol'] |
| 332 | if symbol in _COMMON_GMOCK_SYMBOLS: |
| 333 | yield ('NUS', 'Need to Use Symbol', diagnosis % m.groupdict()) |
| 334 | |
| 335 | |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 336 | def _NeedToUseReturnNullDiagnoser(msg): |
| 337 | """Diagnoses the NRNULL disease, given the error messages by gcc.""" |
| 338 | |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 339 | regex = (_FILE_LINE_RE + r'instantiated from here\n' |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 340 | r'.*gmock-actions\.h.*error: invalid conversion from ' |
| 341 | r'\'long int\' to \'(?P<type>.+\*)') |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 342 | diagnosis = """ |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 343 | You are probably calling Return(NULL) and the compiler isn't sure how to turn |
| 344 | NULL into a %(type)s*. Use ReturnNull() instead. |
| 345 | Note: the line number may be off; please fix all instances of Return(NULL).""" |
| 346 | return _GenericDiagnoser('NRNULL', 'Need to use ReturnNull', |
| 347 | regex, diagnosis, msg) |
| 348 | |
| 349 | |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 350 | _TTB_DIAGNOSIS = """ |
| 351 | In a mock class template, types or typedefs defined in the base class |
| 352 | template are *not* automatically visible. This is how C++ works. Before |
| 353 | you can use a type or typedef named %(type)s defined in base class Base<T>, you |
| 354 | need to make it visible. One way to do it is: |
| 355 | |
| 356 | typedef typename Base<T>::%(type)s %(type)s;""" |
| 357 | |
| 358 | |
| 359 | def _TypeInTemplatedBaseDiagnoser1(msg): |
| 360 | """Diagnoses the TTB disease, given the error messages by gcc. |
| 361 | |
| 362 | This version works when the type is used as the mock function's return |
| 363 | type. |
| 364 | """ |
| 365 | |
| 366 | regex = (r'In member function \'int .*\n' + _FILE_LINE_RE + |
| 367 | r'error: a function call cannot appear in a constant-expression') |
| 368 | diagnosis = _TTB_DIAGNOSIS % {'type': 'Foo'} |
| 369 | return _GenericDiagnoser('TTB', 'Type in Template Base', |
| 370 | regex, diagnosis, msg) |
| 371 | |
| 372 | |
| 373 | def _TypeInTemplatedBaseDiagnoser2(msg): |
| 374 | """Diagnoses the TTB disease, given the error messages by gcc. |
| 375 | |
| 376 | This version works when the type is used as the mock function's sole |
| 377 | parameter type. |
| 378 | """ |
| 379 | |
| 380 | regex = (r'In member function \'int .*\n' |
| 381 | + _FILE_LINE_RE + |
| 382 | r'error: \'(?P<type>.+)\' was not declared in this scope\n' |
| 383 | r'.*error: template argument 1 is invalid\n') |
| 384 | return _GenericDiagnoser('TTB', 'Type in Template Base', |
| 385 | regex, _TTB_DIAGNOSIS, msg) |
| 386 | |
| 387 | |
| 388 | def _TypeInTemplatedBaseDiagnoser3(msg): |
| 389 | """Diagnoses the TTB disease, given the error messages by gcc. |
| 390 | |
| 391 | This version works when the type is used as a parameter of a mock |
| 392 | function that has multiple parameters. |
| 393 | """ |
| 394 | |
| 395 | regex = (r'error: expected `;\' before \'::\' token\n' |
| 396 | + _FILE_LINE_RE + |
| 397 | r'error: \'(?P<type>.+)\' was not declared in this scope\n' |
| 398 | r'.*error: template argument 1 is invalid\n' |
| 399 | r'.*error: \'.+\' was not declared in this scope') |
| 400 | return _GenericDiagnoser('TTB', 'Type in Template Base', |
| 401 | regex, _TTB_DIAGNOSIS, msg) |
| 402 | |
| 403 | |
zhanyong.wan | 16cf473 | 2009-05-14 20:55:30 +0000 | [diff] [blame] | 404 | def _WrongMockMethodMacroDiagnoser(msg): |
| 405 | """Diagnoses the WMM disease, given the error messages by gcc.""" |
| 406 | |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 407 | regex = (_FILE_LINE_RE + |
zhanyong.wan | 16cf473 | 2009-05-14 20:55:30 +0000 | [diff] [blame] | 408 | r'.*this_method_does_not_take_(?P<wrong_args>\d+)_argument.*\n' |
| 409 | r'.*\n' |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 410 | r'.*candidates are.*FunctionMocker<[^>]+A(?P<args>\d+)\)>') |
| 411 | diagnosis = """ |
zhanyong.wan | 16cf473 | 2009-05-14 20:55:30 +0000 | [diff] [blame] | 412 | You are using MOCK_METHOD%(wrong_args)s to define a mock method that has |
| 413 | %(args)s arguments. Use MOCK_METHOD%(args)s (or MOCK_CONST_METHOD%(args)s, |
| 414 | MOCK_METHOD%(args)s_T, MOCK_CONST_METHOD%(args)s_T as appropriate) instead.""" |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 415 | return _GenericDiagnoser('WMM', 'Wrong MOCK_METHODn Macro', |
zhanyong.wan | 16cf473 | 2009-05-14 20:55:30 +0000 | [diff] [blame] | 416 | regex, diagnosis, msg) |
| 417 | |
| 418 | |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 419 | def _WrongParenPositionDiagnoser(msg): |
| 420 | """Diagnoses the WPP disease, given the error messages by gcc.""" |
| 421 | |
| 422 | regex = (_FILE_LINE_RE + |
| 423 | r'error:.*testing::internal::MockSpec<.* has no member named \'' |
| 424 | r'(?P<method>\w+)\'') |
| 425 | diagnosis = """ |
| 426 | The closing parenthesis of ON_CALL or EXPECT_CALL should be *before* |
| 427 | ".%(method)s". For example, you should write: |
| 428 | EXPECT_CALL(my_mock, Foo(_)).%(method)s(...); |
| 429 | instead of: |
| 430 | EXPECT_CALL(my_mock, Foo(_).%(method)s(...));""" |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 431 | return _GenericDiagnoser('WPP', 'Wrong Parenthesis Position', |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 432 | regex, diagnosis, msg) |
| 433 | |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 434 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 435 | _DIAGNOSERS = [ |
| 436 | _IncompleteByReferenceArgumentDiagnoser, |
| 437 | _MockObjectPointerDiagnoser, |
| 438 | _NeedToReturnNothingDiagnoser, |
| 439 | _NeedToReturnReferenceDiagnoser, |
| 440 | _NeedToReturnSomethingDiagnoser, |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 441 | _NeedToUseReturnNullDiagnoser, |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 442 | _NeedToUseSymbolDiagnoser, |
| 443 | _OverloadedFunctionActionDiagnoser, |
| 444 | _OverloadedFunctionMatcherDiagnoser, |
| 445 | _OverloadedMethodActionDiagnoser1, |
| 446 | _OverloadedMethodActionDiagnoser2, |
zhanyong.wan | b824316 | 2009-06-04 05:48:20 +0000 | [diff] [blame] | 447 | _TypeInTemplatedBaseDiagnoser1, |
| 448 | _TypeInTemplatedBaseDiagnoser2, |
| 449 | _TypeInTemplatedBaseDiagnoser3, |
zhanyong.wan | 16cf473 | 2009-05-14 20:55:30 +0000 | [diff] [blame] | 450 | _WrongMockMethodMacroDiagnoser, |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 451 | _WrongParenPositionDiagnoser, |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 452 | ] |
| 453 | |
| 454 | |
| 455 | def Diagnose(msg): |
| 456 | """Generates all possible diagnoses given the gcc error message.""" |
| 457 | |
| 458 | for diagnoser in _DIAGNOSERS: |
| 459 | for diagnosis in diagnoser(msg): |
| 460 | yield '[%s - %s]\n%s' % diagnosis |
| 461 | |
| 462 | |
| 463 | def main(): |
| 464 | print ('Google Mock Doctor v%s - ' |
| 465 | 'diagnoses problems in code using Google Mock.' % _VERSION) |
| 466 | |
| 467 | if sys.stdin.isatty(): |
| 468 | print ('Please copy and paste the compiler errors here. Press c-D when ' |
| 469 | 'you are done:') |
| 470 | else: |
| 471 | print 'Waiting for compiler errors on stdin . . .' |
| 472 | |
| 473 | msg = sys.stdin.read().strip() |
| 474 | diagnoses = list(Diagnose(msg)) |
| 475 | count = len(diagnoses) |
| 476 | if not count: |
| 477 | print '\nGcc complained:' |
| 478 | print '8<------------------------------------------------------------' |
| 479 | print msg |
| 480 | print '------------------------------------------------------------>8' |
| 481 | print """ |
| 482 | Uh-oh, I'm not smart enough to figure out what the problem is. :-( |
| 483 | However... |
| 484 | If you send your source code and gcc's error messages to |
| 485 | googlemock@googlegroups.com, you can be helped and I can get smarter -- |
| 486 | win-win for us!""" |
| 487 | else: |
| 488 | print '------------------------------------------------------------' |
| 489 | print 'Your code appears to have the following', |
| 490 | if count > 1: |
| 491 | print '%s diseases:' % (count,) |
| 492 | else: |
| 493 | print 'disease:' |
| 494 | i = 0 |
| 495 | for d in diagnoses: |
| 496 | i += 1 |
| 497 | if count > 1: |
| 498 | print '\n#%s:' % (i,) |
| 499 | print d |
| 500 | print """ |
| 501 | How did I do? If you think I'm wrong or unhelpful, please send your |
| 502 | source code and gcc's error messages to googlemock@googlegroups.com. Then |
| 503 | you can be helped and I can get smarter -- I promise I won't be upset!""" |
| 504 | |
| 505 | |
| 506 | if __name__ == '__main__': |
| 507 | main() |