PageRenderTime 559ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/external/chromium/testing/gmock/scripts/gmock_doctor.py

https://gitlab.com/brian0218/rk3066_r-box_android4.2.2_sdk
Python | 617 lines | 586 code | 1 blank | 30 comment | 0 complexity | 689a15eadee9c991389f2bd8435a5c80 MD5 | raw file
  1. #!/usr/bin/env python
  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. """Converts compiler's errors in code using Google Mock to plain English."""
  32. __author__ = 'wan@google.com (Zhanyong Wan)'
  33. import re
  34. import sys
  35. _VERSION = '1.0.3'
  36. _EMAIL = 'googlemock@googlegroups.com'
  37. _COMMON_GMOCK_SYMBOLS = [
  38. # Matchers
  39. '_',
  40. 'A',
  41. 'AddressSatisfies',
  42. 'AllOf',
  43. 'An',
  44. 'AnyOf',
  45. 'ContainerEq',
  46. 'Contains',
  47. 'ContainsRegex',
  48. 'DoubleEq',
  49. 'ElementsAre',
  50. 'ElementsAreArray',
  51. 'EndsWith',
  52. 'Eq',
  53. 'Field',
  54. 'FloatEq',
  55. 'Ge',
  56. 'Gt',
  57. 'HasSubstr',
  58. 'IsInitializedProto',
  59. 'Le',
  60. 'Lt',
  61. 'MatcherCast',
  62. 'Matches',
  63. 'MatchesRegex',
  64. 'NanSensitiveDoubleEq',
  65. 'NanSensitiveFloatEq',
  66. 'Ne',
  67. 'Not',
  68. 'NotNull',
  69. 'Pointee',
  70. 'Property',
  71. 'Ref',
  72. 'ResultOf',
  73. 'SafeMatcherCast',
  74. 'StartsWith',
  75. 'StrCaseEq',
  76. 'StrCaseNe',
  77. 'StrEq',
  78. 'StrNe',
  79. 'Truly',
  80. 'TypedEq',
  81. 'Value',
  82. # Actions
  83. 'Assign',
  84. 'ByRef',
  85. 'DeleteArg',
  86. 'DoAll',
  87. 'DoDefault',
  88. 'IgnoreResult',
  89. 'Invoke',
  90. 'InvokeArgument',
  91. 'InvokeWithoutArgs',
  92. 'Return',
  93. 'ReturnNew',
  94. 'ReturnNull',
  95. 'ReturnRef',
  96. 'SaveArg',
  97. 'SetArgReferee',
  98. 'SetArgPointee',
  99. 'SetArgumentPointee',
  100. 'SetArrayArgument',
  101. 'SetErrnoAndReturn',
  102. 'Throw',
  103. 'WithArg',
  104. 'WithArgs',
  105. 'WithoutArgs',
  106. # Cardinalities
  107. 'AnyNumber',
  108. 'AtLeast',
  109. 'AtMost',
  110. 'Between',
  111. 'Exactly',
  112. # Sequences
  113. 'InSequence',
  114. 'Sequence',
  115. # Misc
  116. 'DefaultValue',
  117. 'Mock',
  118. ]
  119. # Regex for matching source file path and line number in the compiler's errors.
  120. _GCC_FILE_LINE_RE = r'(?P<file>.*):(?P<line>\d+):\s+'
  121. _CLANG_FILE_LINE_RE = r'(?P<file>.*):(?P<line>\d+):(?P<column>\d+):\s+'
  122. _CLANG_NON_GMOCK_FILE_LINE_RE = (
  123. r'(?P<file>.*[/\\^](?!gmock-)[^/\\]+):(?P<line>\d+):(?P<column>\d+):\s+')
  124. def _FindAllMatches(regex, s):
  125. """Generates all matches of regex in string s."""
  126. r = re.compile(regex)
  127. return r.finditer(s)
  128. def _GenericDiagnoser(short_name, long_name, diagnoses, msg):
  129. """Diagnoses the given disease by pattern matching.
  130. Can provide different diagnoses for different patterns.
  131. Args:
  132. short_name: Short name of the disease.
  133. long_name: Long name of the disease.
  134. diagnoses: A list of pairs (regex, pattern for formatting the diagnosis
  135. for matching regex).
  136. msg: Compiler's error messages.
  137. Yields:
  138. Tuples of the form
  139. (short name of disease, long name of disease, diagnosis).
  140. """
  141. for regex, diagnosis in diagnoses:
  142. if re.search(regex, msg):
  143. diagnosis = '%(file)s:%(line)s:' + diagnosis
  144. for m in _FindAllMatches(regex, msg):
  145. yield (short_name, long_name, diagnosis % m.groupdict())
  146. def _NeedToReturnReferenceDiagnoser(msg):
  147. """Diagnoses the NRR disease, given the error messages by the compiler."""
  148. gcc_regex = (r'In member function \'testing::internal::ReturnAction<R>.*\n'
  149. + _GCC_FILE_LINE_RE + r'instantiated from here\n'
  150. r'.*gmock-actions\.h.*error: creating array with negative size')
  151. clang_regex = (r'error:.*array.*negative.*\r?\n'
  152. r'(.*\n)*?' +
  153. _CLANG_NON_GMOCK_FILE_LINE_RE +
  154. r'note: in instantiation of function template specialization '
  155. r'\'testing::internal::ReturnAction<(?P<type>).*>'
  156. r'::operator Action<.*>\' requested here')
  157. diagnosis = """
  158. You are using a Return() action in a function that returns a reference to
  159. %(type)s. Please use ReturnRef() instead."""
  160. return _GenericDiagnoser('NRR', 'Need to Return Reference',
  161. [(clang_regex, diagnosis),
  162. (gcc_regex, diagnosis % {'type': 'a type'})],
  163. msg)
  164. def _NeedToReturnSomethingDiagnoser(msg):
  165. """Diagnoses the NRS disease, given the error messages by the compiler."""
  166. gcc_regex = (_GCC_FILE_LINE_RE + r'(instantiated from here\n.'
  167. r'*gmock.*actions\.h.*error: void value not ignored)'
  168. r'|(error: control reaches end of non-void function)')
  169. clang_regex1 = (_CLANG_FILE_LINE_RE +
  170. r'error: cannot initialize return object '
  171. r'of type \'Result\' \(aka \'(?P<return_type>).*\'\) '
  172. r'with an rvalue of type \'void\'')
  173. clang_regex2 = (_CLANG_FILE_LINE_RE +
  174. r'error: cannot initialize return object '
  175. r'of type \'(?P<return_type>).*\' '
  176. r'with an rvalue of type \'void\'')
  177. diagnosis = """
  178. You are using an action that returns void, but it needs to return
  179. %(return_type)s. Please tell it *what* to return. Perhaps you can use
  180. the pattern DoAll(some_action, Return(some_value))?"""
  181. return _GenericDiagnoser(
  182. 'NRS',
  183. 'Need to Return Something',
  184. [(gcc_regex, diagnosis % {'return_type': '*something*'}),
  185. (clang_regex1, diagnosis),
  186. (clang_regex2, diagnosis)],
  187. msg)
  188. def _NeedToReturnNothingDiagnoser(msg):
  189. """Diagnoses the NRN disease, given the error messages by the compiler."""
  190. gcc_regex = (_GCC_FILE_LINE_RE + r'instantiated from here\n'
  191. r'.*gmock-actions\.h.*error: instantiation of '
  192. r'\'testing::internal::ReturnAction<R>::Impl<F>::value_\' '
  193. r'as type \'void\'')
  194. clang_regex1 = (r'error: field has incomplete type '
  195. r'\'Result\' \(aka \'void\'\)(\r)?\n'
  196. r'(.*\n)*?' +
  197. _CLANG_NON_GMOCK_FILE_LINE_RE + r'note: in instantiation '
  198. r'of function template specialization '
  199. r'\'testing::internal::ReturnAction<(?P<return_type>.*)>'
  200. r'::operator Action<void \(.*\)>\' requested here')
  201. clang_regex2 = (r'error: field has incomplete type '
  202. r'\'Result\' \(aka \'void\'\)(\r)?\n'
  203. r'(.*\n)*?' +
  204. _CLANG_NON_GMOCK_FILE_LINE_RE + r'note: in instantiation '
  205. r'of function template specialization '
  206. r'\'testing::internal::DoBothAction<.*>'
  207. r'::operator Action<(?P<return_type>.*) \(.*\)>\' '
  208. r'requested here')
  209. diagnosis = """
  210. You are using an action that returns %(return_type)s, but it needs to return
  211. void. Please use a void-returning action instead.
  212. All actions but the last in DoAll(...) must return void. Perhaps you need
  213. to re-arrange the order of actions in a DoAll(), if you are using one?"""
  214. return _GenericDiagnoser(
  215. 'NRN',
  216. 'Need to Return Nothing',
  217. [(gcc_regex, diagnosis % {'return_type': '*something*'}),
  218. (clang_regex1, diagnosis),
  219. (clang_regex2, diagnosis)],
  220. msg)
  221. def _IncompleteByReferenceArgumentDiagnoser(msg):
  222. """Diagnoses the IBRA disease, given the error messages by the compiler."""
  223. gcc_regex = (_GCC_FILE_LINE_RE + r'instantiated from here\n'
  224. r'.*gtest-printers\.h.*error: invalid application of '
  225. r'\'sizeof\' to incomplete type \'(?P<type>.*)\'')
  226. clang_regex = (r'.*gtest-printers\.h.*error: invalid application of '
  227. r'\'sizeof\' to an incomplete type '
  228. r'\'(?P<type>.*)( const)?\'\r?\n'
  229. r'(.*\n)*?' +
  230. _CLANG_NON_GMOCK_FILE_LINE_RE +
  231. r'note: in instantiation of member function '
  232. r'\'testing::internal2::TypeWithoutFormatter<.*>::'
  233. r'PrintValue\' requested here')
  234. diagnosis = """
  235. In order to mock this function, Google Mock needs to see the definition
  236. of type "%(type)s" - declaration alone is not enough. Either #include
  237. the header that defines it, or change the argument to be passed
  238. by pointer."""
  239. return _GenericDiagnoser('IBRA', 'Incomplete By-Reference Argument Type',
  240. [(gcc_regex, diagnosis),
  241. (clang_regex, diagnosis)],
  242. msg)
  243. def _OverloadedFunctionMatcherDiagnoser(msg):
  244. """Diagnoses the OFM disease, given the error messages by the compiler."""
  245. gcc_regex = (_GCC_FILE_LINE_RE + r'error: no matching function for '
  246. r'call to \'Truly\(<unresolved overloaded function type>\)')
  247. clang_regex = (_CLANG_FILE_LINE_RE + r'error: no matching function for '
  248. r'call to \'Truly')
  249. diagnosis = """
  250. The argument you gave to Truly() is an overloaded function. Please tell
  251. your compiler which overloaded version you want to use.
  252. For example, if you want to use the version whose signature is
  253. bool Foo(int n);
  254. you should write
  255. Truly(static_cast<bool (*)(int n)>(Foo))"""
  256. return _GenericDiagnoser('OFM', 'Overloaded Function Matcher',
  257. [(gcc_regex, diagnosis),
  258. (clang_regex, diagnosis)],
  259. msg)
  260. def _OverloadedFunctionActionDiagnoser(msg):
  261. """Diagnoses the OFA disease, given the error messages by the compiler."""
  262. gcc_regex = (_GCC_FILE_LINE_RE + r'error: no matching function for call to '
  263. r'\'Invoke\(<unresolved overloaded function type>')
  264. clang_regex = (_CLANG_FILE_LINE_RE + r'error: no matching '
  265. r'function for call to \'Invoke\'\r?\n'
  266. r'(.*\n)*?'
  267. r'.*\bgmock-\w+-actions\.h:\d+:\d+:\s+'
  268. r'note: candidate template ignored:\s+'
  269. r'couldn\'t infer template argument \'FunctionImpl\'')
  270. diagnosis = """
  271. Function you are passing to Invoke is overloaded. Please tell your compiler
  272. which overloaded version you want to use.
  273. For example, if you want to use the version whose signature is
  274. bool MyFunction(int n, double x);
  275. you should write something like
  276. Invoke(static_cast<bool (*)(int n, double x)>(MyFunction))"""
  277. return _GenericDiagnoser('OFA', 'Overloaded Function Action',
  278. [(gcc_regex, diagnosis),
  279. (clang_regex, diagnosis)],
  280. msg)
  281. def _OverloadedMethodActionDiagnoser(msg):
  282. """Diagnoses the OMA disease, given the error messages by the compiler."""
  283. gcc_regex = (_GCC_FILE_LINE_RE + r'error: no matching function for '
  284. r'call to \'Invoke\(.+, <unresolved overloaded function '
  285. r'type>\)')
  286. clang_regex = (_CLANG_FILE_LINE_RE + r'error: no matching function '
  287. r'for call to \'Invoke\'\r?\n'
  288. r'(.*\n)*?'
  289. r'.*\bgmock-\w+-actions\.h:\d+:\d+: '
  290. r'note: candidate function template not viable: '
  291. r'requires 1 argument, but 2 were provided')
  292. diagnosis = """
  293. The second argument you gave to Invoke() is an overloaded method. Please
  294. tell your compiler which overloaded version you want to use.
  295. For example, if you want to use the version whose signature is
  296. class Foo {
  297. ...
  298. bool Bar(int n, double x);
  299. };
  300. you should write something like
  301. Invoke(foo, static_cast<bool (Foo::*)(int n, double x)>(&Foo::Bar))"""
  302. return _GenericDiagnoser('OMA', 'Overloaded Method Action',
  303. [(gcc_regex, diagnosis),
  304. (clang_regex, diagnosis)],
  305. msg)
  306. def _MockObjectPointerDiagnoser(msg):
  307. """Diagnoses the MOP disease, given the error messages by the compiler."""
  308. gcc_regex = (_GCC_FILE_LINE_RE + r'error: request for member '
  309. r'\'gmock_(?P<method>.+)\' in \'(?P<mock_object>.+)\', '
  310. r'which is of non-class type \'(.*::)*(?P<class_name>.+)\*\'')
  311. clang_regex = (_CLANG_FILE_LINE_RE + r'error: member reference type '
  312. r'\'(?P<class_name>.*?) *\' is a pointer; '
  313. r'maybe you meant to use \'->\'\?')
  314. diagnosis = """
  315. The first argument to ON_CALL() and EXPECT_CALL() must be a mock *object*,
  316. not a *pointer* to it. Please write '*(%(mock_object)s)' instead of
  317. '%(mock_object)s' as your first argument.
  318. For example, given the mock class:
  319. class %(class_name)s : public ... {
  320. ...
  321. MOCK_METHOD0(%(method)s, ...);
  322. };
  323. and the following mock instance:
  324. %(class_name)s* mock_ptr = ...
  325. you should use the EXPECT_CALL like this:
  326. EXPECT_CALL(*mock_ptr, %(method)s(...));"""
  327. return _GenericDiagnoser(
  328. 'MOP',
  329. 'Mock Object Pointer',
  330. [(gcc_regex, diagnosis),
  331. (clang_regex, diagnosis % {'mock_object': 'mock_object',
  332. 'method': 'method',
  333. 'class_name': '%(class_name)s'})],
  334. msg)
  335. def _NeedToUseSymbolDiagnoser(msg):
  336. """Diagnoses the NUS disease, given the error messages by the compiler."""
  337. gcc_regex = (_GCC_FILE_LINE_RE + r'error: \'(?P<symbol>.+)\' '
  338. r'(was not declared in this scope|has not been declared)')
  339. clang_regex = (_CLANG_FILE_LINE_RE + r'error: use of undeclared identifier '
  340. r'\'(?P<symbol>.+)\'')
  341. diagnosis = """
  342. '%(symbol)s' is defined by Google Mock in the testing namespace.
  343. Did you forget to write
  344. using testing::%(symbol)s;
  345. ?"""
  346. for m in (list(_FindAllMatches(gcc_regex, msg)) +
  347. list(_FindAllMatches(clang_regex, msg))):
  348. symbol = m.groupdict()['symbol']
  349. if symbol in _COMMON_GMOCK_SYMBOLS:
  350. yield ('NUS', 'Need to Use Symbol', diagnosis % m.groupdict())
  351. def _NeedToUseReturnNullDiagnoser(msg):
  352. """Diagnoses the NRNULL disease, given the error messages by the compiler."""
  353. gcc_regex = ('instantiated from \'testing::internal::ReturnAction<R>'
  354. '::operator testing::Action<Func>\(\) const.*\n' +
  355. _GCC_FILE_LINE_RE + r'instantiated from here\n'
  356. r'.*error: no matching function for call to \'ImplicitCast_\('
  357. r'long int&\)')
  358. clang_regex = (r'\bgmock-actions.h:.* error: no matching function for '
  359. r'call to \'ImplicitCast_\'\r?\n'
  360. r'(.*\n)*?' +
  361. _CLANG_NON_GMOCK_FILE_LINE_RE + r'note: in instantiation '
  362. r'of function template specialization '
  363. r'\'testing::internal::ReturnAction<long>::operator '
  364. r'Action<(?P<type>.*)\(\)>\' requested here')
  365. diagnosis = """
  366. You are probably calling Return(NULL) and the compiler isn't sure how to turn
  367. NULL into %(type)s. Use ReturnNull() instead.
  368. Note: the line number may be off; please fix all instances of Return(NULL)."""
  369. return _GenericDiagnoser(
  370. 'NRNULL', 'Need to use ReturnNull',
  371. [(clang_regex, diagnosis),
  372. (gcc_regex, diagnosis % {'type': 'the right type'})],
  373. msg)
  374. def _TypeInTemplatedBaseDiagnoser(msg):
  375. """Diagnoses the TTB disease, given the error messages by the compiler."""
  376. # This version works when the type is used as the mock function's return
  377. # type.
  378. gcc_4_3_1_regex_type_in_retval = (
  379. r'In member function \'int .*\n' + _GCC_FILE_LINE_RE +
  380. r'error: a function call cannot appear in a constant-expression')
  381. gcc_4_4_0_regex_type_in_retval = (
  382. r'error: a function call cannot appear in a constant-expression'
  383. + _GCC_FILE_LINE_RE + r'error: template argument 1 is invalid\n')
  384. # This version works when the type is used as the mock function's sole
  385. # parameter type.
  386. gcc_regex_type_of_sole_param = (
  387. _GCC_FILE_LINE_RE +
  388. r'error: \'(?P<type>.+)\' was not declared in this scope\n'
  389. r'.*error: template argument 1 is invalid\n')
  390. # This version works when the type is used as a parameter of a mock
  391. # function that has multiple parameters.
  392. gcc_regex_type_of_a_param = (
  393. r'error: expected `;\' before \'::\' token\n'
  394. + _GCC_FILE_LINE_RE +
  395. r'error: \'(?P<type>.+)\' was not declared in this scope\n'
  396. r'.*error: template argument 1 is invalid\n'
  397. r'.*error: \'.+\' was not declared in this scope')
  398. clang_regex_type_of_retval_or_sole_param = (
  399. _CLANG_FILE_LINE_RE +
  400. r'error: use of undeclared identifier \'(?P<type>.*)\'\n'
  401. r'(.*\n)*?'
  402. r'(?P=file):(?P=line):\d+: error: '
  403. r'non-friend class member \'Result\' cannot have a qualified name'
  404. )
  405. clang_regex_type_of_a_param = (
  406. _CLANG_FILE_LINE_RE +
  407. r'error: C\+\+ requires a type specifier for all declarations\n'
  408. r'(.*\n)*?'
  409. r'(?P=file):(?P=line):(?P=column): error: '
  410. r'C\+\+ requires a type specifier for all declarations'
  411. )
  412. diagnosis = """
  413. In a mock class template, types or typedefs defined in the base class
  414. template are *not* automatically visible. This is how C++ works. Before
  415. you can use a type or typedef named %(type)s defined in base class Base<T>, you
  416. need to make it visible. One way to do it is:
  417. typedef typename Base<T>::%(type)s %(type)s;"""
  418. return _GenericDiagnoser(
  419. 'TTB', 'Type in Template Base',
  420. [(gcc_4_3_1_regex_type_in_retval, diagnosis % {'type': 'Foo'}),
  421. (gcc_4_4_0_regex_type_in_retval, diagnosis % {'type': 'Foo'}),
  422. (gcc_regex_type_of_sole_param, diagnosis),
  423. (gcc_regex_type_of_a_param, diagnosis),
  424. (clang_regex_type_of_retval_or_sole_param, diagnosis),
  425. (clang_regex_type_of_a_param, diagnosis % {'type': 'Foo'})],
  426. msg)
  427. def _WrongMockMethodMacroDiagnoser(msg):
  428. """Diagnoses the WMM disease, given the error messages by the compiler."""
  429. gcc_regex = (_GCC_FILE_LINE_RE +
  430. r'.*this_method_does_not_take_(?P<wrong_args>\d+)_argument.*\n'
  431. r'.*\n'
  432. r'.*candidates are.*FunctionMocker<[^>]+A(?P<args>\d+)\)>')
  433. clang_regex = (_CLANG_NON_GMOCK_FILE_LINE_RE +
  434. r'error:.*array.*negative.*r?\n'
  435. r'(.*\n)*?'
  436. r'(?P=file):(?P=line):(?P=column): error: too few arguments '
  437. r'to function call, expected (?P<args>\d+), '
  438. r'have (?P<wrong_args>\d+)')
  439. diagnosis = """
  440. You are using MOCK_METHOD%(wrong_args)s to define a mock method that has
  441. %(args)s arguments. Use MOCK_METHOD%(args)s (or MOCK_CONST_METHOD%(args)s,
  442. MOCK_METHOD%(args)s_T, MOCK_CONST_METHOD%(args)s_T as appropriate) instead."""
  443. return _GenericDiagnoser('WMM', 'Wrong MOCK_METHODn Macro',
  444. [(gcc_regex, diagnosis),
  445. (clang_regex, diagnosis)],
  446. msg)
  447. def _WrongParenPositionDiagnoser(msg):
  448. """Diagnoses the WPP disease, given the error messages by the compiler."""
  449. gcc_regex = (_GCC_FILE_LINE_RE +
  450. r'error:.*testing::internal::MockSpec<.* has no member named \''
  451. r'(?P<method>\w+)\'')
  452. clang_regex = (_CLANG_NON_GMOCK_FILE_LINE_RE +
  453. r'error: no member named \'(?P<method>\w+)\' in '
  454. r'\'testing::internal::MockSpec<.*>\'')
  455. diagnosis = """
  456. The closing parenthesis of ON_CALL or EXPECT_CALL should be *before*
  457. ".%(method)s". For example, you should write:
  458. EXPECT_CALL(my_mock, Foo(_)).%(method)s(...);
  459. instead of:
  460. EXPECT_CALL(my_mock, Foo(_).%(method)s(...));"""
  461. return _GenericDiagnoser('WPP', 'Wrong Parenthesis Position',
  462. [(gcc_regex, diagnosis),
  463. (clang_regex, diagnosis)],
  464. msg)
  465. _DIAGNOSERS = [
  466. _IncompleteByReferenceArgumentDiagnoser,
  467. _MockObjectPointerDiagnoser,
  468. _NeedToReturnNothingDiagnoser,
  469. _NeedToReturnReferenceDiagnoser,
  470. _NeedToReturnSomethingDiagnoser,
  471. _NeedToUseReturnNullDiagnoser,
  472. _NeedToUseSymbolDiagnoser,
  473. _OverloadedFunctionActionDiagnoser,
  474. _OverloadedFunctionMatcherDiagnoser,
  475. _OverloadedMethodActionDiagnoser,
  476. _TypeInTemplatedBaseDiagnoser,
  477. _WrongMockMethodMacroDiagnoser,
  478. _WrongParenPositionDiagnoser,
  479. ]
  480. def Diagnose(msg):
  481. """Generates all possible diagnoses given the compiler error message."""
  482. msg = re.sub(r'\x1b\[[^m]*m', '', msg) # Strips all color formatting.
  483. diagnoses = []
  484. for diagnoser in _DIAGNOSERS:
  485. for diag in diagnoser(msg):
  486. diagnosis = '[%s - %s]\n%s' % diag
  487. if not diagnosis in diagnoses:
  488. diagnoses.append(diagnosis)
  489. return diagnoses
  490. def main():
  491. print ('Google Mock Doctor v%s - '
  492. 'diagnoses problems in code using Google Mock.' % _VERSION)
  493. if sys.stdin.isatty():
  494. print ('Please copy and paste the compiler errors here. Press c-D when '
  495. 'you are done:')
  496. else:
  497. print 'Waiting for compiler errors on stdin . . .'
  498. msg = sys.stdin.read().strip()
  499. diagnoses = Diagnose(msg)
  500. count = len(diagnoses)
  501. if not count:
  502. print ("""
  503. Your compiler complained:
  504. 8<------------------------------------------------------------
  505. %s
  506. ------------------------------------------------------------>8
  507. Uh-oh, I'm not smart enough to figure out what the problem is. :-(
  508. However...
  509. If you send your source code and the compiler's error messages to
  510. %s, you can be helped and I can get smarter --
  511. win-win for us!""" % (msg, _EMAIL))
  512. else:
  513. print '------------------------------------------------------------'
  514. print 'Your code appears to have the following',
  515. if count > 1:
  516. print '%s diseases:' % (count,)
  517. else:
  518. print 'disease:'
  519. i = 0
  520. for d in diagnoses:
  521. i += 1
  522. if count > 1:
  523. print '\n#%s:' % (i,)
  524. print d
  525. print ("""
  526. How did I do? If you think I'm wrong or unhelpful, please send your
  527. source code and the compiler's error messages to %s.
  528. Then you can be helped and I can get smarter -- I promise I won't be upset!""" %
  529. _EMAIL)
  530. if __name__ == '__main__':
  531. main()