OLD | NEW |
---|---|
1 # -*- coding: utf-8 -*- | 1 # -*- coding: utf-8 -*- |
2 # | 2 # |
3 # Copyright (C) 2009, 2010, 2012 Google Inc. All rights reserved. | 3 # Copyright (C) 2009, 2010, 2012 Google Inc. All rights reserved. |
4 # Copyright (C) 2009 Torch Mobile Inc. | 4 # Copyright (C) 2009 Torch Mobile Inc. |
5 # Copyright (C) 2009 Apple Inc. All rights reserved. | 5 # Copyright (C) 2009 Apple Inc. All rights reserved. |
6 # Copyright (C) 2010 Chris Jerdonek (cjerdonek@webkit.org) | 6 # Copyright (C) 2010 Chris Jerdonek (cjerdonek@webkit.org) |
7 # | 7 # |
8 # Redistribution and use in source and binary forms, with or without | 8 # Redistribution and use in source and binary forms, with or without |
9 # modification, are permitted provided that the following conditions are | 9 # modification, are permitted provided that the following conditions are |
10 # met: | 10 # met: |
(...skipping 3266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3277 error(line_number, 'runtime/bitfields', 5, | 3277 error(line_number, 'runtime/bitfields', 5, |
3278 'Please declare integral type bitfields with either signed or unsi gned.') | 3278 'Please declare integral type bitfields with either signed or unsi gned.') |
3279 | 3279 |
3280 check_identifier_name_in_declaration(filename, line_number, line, file_state , error) | 3280 check_identifier_name_in_declaration(filename, line_number, line, file_state , error) |
3281 | 3281 |
3282 # Check for unsigned int (should be just 'unsigned') | 3282 # Check for unsigned int (should be just 'unsigned') |
3283 if search(r'\bunsigned int\b', line): | 3283 if search(r'\bunsigned int\b', line): |
3284 error(line_number, 'runtime/unsigned', 1, | 3284 error(line_number, 'runtime/unsigned', 1, |
3285 'Omit int when using unsigned') | 3285 'Omit int when using unsigned') |
3286 | 3286 |
3287 # Check that we're not using static_cast<Text*>. | 3287 # Check whether using static_cast<Classname*>. If yes, suggest to use toClas sname(). |
3288 if search(r'\bstatic_cast<Text\*>', line): | 3288 check_for_object_static_cast(line_number, line, error) |
3289 error(line_number, 'readability/check', 4, | 3289 |
3290 'Consider using toText helper function in WebCore/dom/Text.h ' | |
3291 'instead of static_cast<Text*>') | |
3292 | 3290 |
3293 def check_identifier_name_in_declaration(filename, line_number, line, file_state , error): | 3291 def check_identifier_name_in_declaration(filename, line_number, line, file_state , error): |
3294 """Checks if identifier names contain any underscores. | 3292 """Checks if identifier names contain any underscores. |
3295 | 3293 |
3296 As identifiers in libraries we are using have a bunch of | 3294 As identifiers in libraries we are using have a bunch of |
3297 underscores, we only warn about the declarations of identifiers | 3295 underscores, we only warn about the declarations of identifiers |
3298 and don't check use of identifiers. | 3296 and don't check use of identifiers. |
3299 | 3297 |
3300 Args: | 3298 Args: |
3301 filename: The name of the current file. | 3299 filename: The name of the current file. |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3414 return | 3412 return |
3415 # We should continue checking if this is a function | 3413 # We should continue checking if this is a function |
3416 # declaration because we need to check its arguments. | 3414 # declaration because we need to check its arguments. |
3417 # Also, we need to check multiple declarations. | 3415 # Also, we need to check multiple declarations. |
3418 if character_after_identifier != '(' and character_after_identifier != ' ,': | 3416 if character_after_identifier != '(' and character_after_identifier != ' ,': |
3419 return | 3417 return |
3420 | 3418 |
3421 number_of_identifiers += 1 | 3419 number_of_identifiers += 1 |
3422 line = line[matched.end():] | 3420 line = line[matched.end():] |
3423 | 3421 |
3422 | |
3423 def check_for_toFoo_definition(filename, pattern, error): | |
3424 """ Reports for using static_cast instead of toFoo convenience function. | |
3425 | |
3426 This function will output warnings to make sure you are actually using | |
3427 the added toFoo conversion functions rather than directly hard coding | |
3428 the static_cast<Classname*> call. For example, you should toHTMLELement(Node *) | |
3429 to convert Node* to HTMLElement*, instead of static_cast<HTMLElement*>(Node* ) | |
3430 | |
3431 Args: | |
3432 filename: The name of the file. | |
3433 pattern: The conversion function pattern to grep for. | |
3434 error: The function to call with any errors found. | |
3435 """ | |
3436 | |
3437 def get_abs_filepath(filename, subdirectory=None): | |
3438 if subdirectory: | |
3439 start_dir = subdirectory | |
3440 else: | |
3441 start_dir = os.path.dirname(os.path.abspath(filename)) | |
3442 | |
3443 for root, dirs, names in os.walk(start_dir): | |
3444 if filename in names: | |
3445 return os.path.join(root, filename) | |
3446 return filename | |
3447 | |
3448 def grep(filename, pattern): | |
3449 # List of all geninuine alternatives to static_cast. | |
Dirk Pranke
2013/09/17 01:54:55
Nit: typo, "genuine".
r.kasibhatla
2013/09/18 09:12:41
Done.
| |
3450 matches = [] | |
3451 for n, line in enumerate(open(filename)): | |
3452 if pattern in line: | |
3453 # We should exclude the match for dummy conversion function. | |
3454 # The dummy conversion function is to catch invalid conversions | |
3455 # and shouldn't be part of listing possible alternatives. | |
3456 #r = re.compile(r'%s(\s+)%s(\(\w+)%s(\*+\))' % ("void", pattern, object_class)) | |
Dirk Pranke
2013/09/17 01:54:55
Delete this commented-out line?
r.kasibhatla
2013/09/18 09:12:41
Done.
| |
3457 result = re.search(r'%s(\s+)%s' % ("void", pattern), line) | |
3458 if not result: | |
3459 matches.append([line, n]) | |
3460 return matches | |
3461 | |
3462 matches = grep(get_abs_filepath(filename), pattern) | |
3463 if matches: | |
3464 return True | |
3465 | |
3466 return False | |
3467 | |
3468 | |
3469 def check_for_object_static_cast(line_number, line, error): | |
3470 """Checks for a C-style cast by looking for the pattern. | |
3471 Args: | |
3472 line_number: The number of the line to check. | |
3473 line: The line of code to check. | |
3474 raw_line: The raw line of code to check, with comments. | |
3475 error: The function to call with any errors found. | |
3476 """ | |
3477 matched = search(r'\bstatic_cast<(\w+\s?\*+\s?)>', line) | |
inferno
2013/09/16 16:56:46
I think you should cover other variants like stati
r.kasibhatla
2013/09/18 09:12:41
I was looking at including static_pointer_cast in
| |
3478 if not matched: | |
3479 return | |
3480 | |
3481 # We have atleast one hit for static_cast<Classname> pattern. | |
3482 class_object = matched.group(1) | |
3483 class_name = re.sub('[\*]', '', class_object) | |
3484 header = ''.join((class_name, '.h')) | |
3485 is_func_defined = check_for_toFoo_definition(header, ''.join(('to', class_na me)), error) | |
3486 | |
3487 if is_func_defined: | |
inferno
2013/09/16 16:56:46
We are showing the error only if toFoo* function i
r.kasibhatla
2013/09/18 09:12:41
Done.
| |
3488 error(line_number, 'readability/check', 4, | |
3489 'Consider using to%s helper function in %s instead of static_cast< %s>' % | |
inferno
2013/09/16 16:56:46
change "Consider using" to a more stronger message
r.kasibhatla
2013/09/18 09:12:41
Done.
| |
3490 (class_name, header, class_object)) | |
3491 | |
3492 | |
3424 def check_c_style_cast(line_number, line, raw_line, cast_type, pattern, | 3493 def check_c_style_cast(line_number, line, raw_line, cast_type, pattern, |
3425 error): | 3494 error): |
3426 """Checks for a C-style cast by looking for the pattern. | 3495 """Checks for a C-style cast by looking for the pattern. |
3427 | 3496 |
3428 This also handles sizeof(type) warnings, due to similarity of content. | 3497 This also handles sizeof(type) warnings, due to similarity of content. |
3429 | 3498 |
3430 Args: | 3499 Args: |
3431 line_number: The number of the line to check. | 3500 line_number: The number of the line to check. |
3432 line: The line of code to check. | 3501 line: The line of code to check. |
3433 raw_line: The raw line of code to check, with comments. | 3502 raw_line: The raw line of code to check, with comments. |
(...skipping 479 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3913 self.handle_style_error, self.min_confidence) | 3982 self.handle_style_error, self.min_confidence) |
3914 | 3983 |
3915 | 3984 |
3916 # FIXME: Remove this function (requires refactoring unit tests). | 3985 # FIXME: Remove this function (requires refactoring unit tests). |
3917 def process_file_data(filename, file_extension, lines, error, min_confidence, un it_test_config): | 3986 def process_file_data(filename, file_extension, lines, error, min_confidence, un it_test_config): |
3918 global _unit_test_config | 3987 global _unit_test_config |
3919 _unit_test_config = unit_test_config | 3988 _unit_test_config = unit_test_config |
3920 checker = CppChecker(filename, file_extension, error, min_confidence) | 3989 checker = CppChecker(filename, file_extension, error, min_confidence) |
3921 checker.check(lines) | 3990 checker.check(lines) |
3922 _unit_test_config = {} | 3991 _unit_test_config = {} |
OLD | NEW |