Chromium Code Reviews| Index: Tools/Scripts/webkitpy/style/checkers/cpp.py |
| diff --git a/Tools/Scripts/webkitpy/style/checkers/cpp.py b/Tools/Scripts/webkitpy/style/checkers/cpp.py |
| index e1543ce6de265494883bd35190b7eeee93161228..80c4e3490988845a141eaa46264287228a9da08d 100644 |
| --- a/Tools/Scripts/webkitpy/style/checkers/cpp.py |
| +++ b/Tools/Scripts/webkitpy/style/checkers/cpp.py |
| @@ -3284,11 +3284,9 @@ def check_language(filename, clean_lines, line_number, file_extension, include_s |
| error(line_number, 'runtime/unsigned', 1, |
| 'Omit int when using unsigned') |
| - # Check that we're not using static_cast<Text*>. |
| - if search(r'\bstatic_cast<Text\*>', line): |
| - error(line_number, 'readability/check', 4, |
| - 'Consider using toText helper function in WebCore/dom/Text.h ' |
| - 'instead of static_cast<Text*>') |
| + # Check whether using static_cast<Classname*>. If yes, suggest to use toClassname(). |
| + check_for_object_static_cast(line_number, line, error) |
| + |
| def check_identifier_name_in_declaration(filename, line_number, line, file_state, error): |
| """Checks if identifier names contain any underscores. |
| @@ -3421,6 +3419,77 @@ def check_identifier_name_in_declaration(filename, line_number, line, file_state |
| number_of_identifiers += 1 |
| line = line[matched.end():] |
| + |
| +def check_for_toFoo_definition(filename, pattern, error): |
| + """ Reports for using static_cast instead of toFoo convenience function. |
| + |
| + This function will output warnings to make sure you are actually using |
| + the added toFoo conversion functions rather than directly hard coding |
| + the static_cast<Classname*> call. For example, you should toHTMLELement(Node*) |
| + to convert Node* to HTMLElement*, instead of static_cast<HTMLElement*>(Node*) |
| + |
| + Args: |
| + filename: The name of the file. |
| + pattern: The conversion function pattern to grep for. |
| + error: The function to call with any errors found. |
| + """ |
| + |
| + def get_abs_filepath(filename, subdirectory=None): |
| + if subdirectory: |
| + start_dir = subdirectory |
| + else: |
| + start_dir = os.path.dirname(os.path.abspath(filename)) |
| + |
| + for root, dirs, names in os.walk(start_dir): |
| + if filename in names: |
| + return os.path.join(root, filename) |
| + return filename |
| + |
| + def grep(filename, pattern): |
| + # 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.
|
| + matches = [] |
| + for n, line in enumerate(open(filename)): |
| + if pattern in line: |
| + # We should exclude the match for dummy conversion function. |
| + # The dummy conversion function is to catch invalid conversions |
| + # and shouldn't be part of listing possible alternatives. |
| + #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.
|
| + result = re.search(r'%s(\s+)%s' % ("void", pattern), line) |
| + if not result: |
| + matches.append([line, n]) |
| + return matches |
| + |
| + matches = grep(get_abs_filepath(filename), pattern) |
| + if matches: |
| + return True |
| + |
| + return False |
| + |
| + |
| +def check_for_object_static_cast(line_number, line, error): |
| + """Checks for a C-style cast by looking for the pattern. |
| + Args: |
| + line_number: The number of the line to check. |
| + line: The line of code to check. |
| + raw_line: The raw line of code to check, with comments. |
| + error: The function to call with any errors found. |
| + """ |
| + 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
|
| + if not matched: |
| + return |
| + |
| + # We have atleast one hit for static_cast<Classname> pattern. |
| + class_object = matched.group(1) |
| + class_name = re.sub('[\*]', '', class_object) |
| + header = ''.join((class_name, '.h')) |
| + is_func_defined = check_for_toFoo_definition(header, ''.join(('to', class_name)), error) |
| + |
| + 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.
|
| + error(line_number, 'readability/check', 4, |
| + '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.
|
| + (class_name, header, class_object)) |
| + |
| + |
| def check_c_style_cast(line_number, line, raw_line, cast_type, pattern, |
| error): |
| """Checks for a C-style cast by looking for the pattern. |