-
Notifications
You must be signed in to change notification settings - Fork 5.5k
format: add checks for type alias #11634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
tomocy
wants to merge
42
commits into
envoyproxy:master
from
tomocy:format-add-checks-for-type-alias
Closed
Changes from 14 commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
7ddce42
format: add checks for type alias
tomocy 9159b57
modify using type alias match
tomocy aa2dd3c
modify to find all of non type alias type in line
tomocy c70a52e
add non type alias allowed type list
tomocy bf581d4
add trailing newline
tomocy 658dc57
compile and combine regexs
tomocy a49224b
compile regexs
tomocy 0387590
refactor names
tomocy 06fe468
modify regex
tomocy 143d0db
add check line format hook
tomocy cd5235b
add line format check for type alias
tomocy 09a8d13
modify to check only source lines
tomocy 7aef23a
fix
tomocy 04c7681
format
tomocy 50b3f10
add aggressive option
tomocy 85d0cec
cherry pick check for type alias
tomocy c10380f
fix with type alias aggressively
tomocy 9f9a80d
replace with type alias aggressively
tomocy 3cfca33
declare type aliases
tomocy 3818a44
fix format
tomocy 2f6214e
declare type aliases
tomocy b9d2664
Merge branch 'master' of github.com:envoyproxy/envoy into format-add-…
tomocy 9fc4db1
delete line format check
tomocy f40c5c0
replace with type aliases
tomocy 344eb82
fix not to replace type alias decl with template
tomocy 9b1e477
Merge branch 'master' of github.com:envoyproxy/envoy into format-add-…
tomocy 6ace91c
replace with type aliases
tomocy e45a2a3
fix
tomocy 5da95c9
fix
tomocy 7005a0b
Merge branch 'master' of github.com:envoyproxy/envoy into format-add-…
tomocy d7a323c
fix
tomocy 934e9d3
replace with type aliases
tomocy 5fdc789
Merge branch 'master' of github.com:envoyproxy/envoy into format-add-…
tomocy 91db93f
replace with type aliases
tomocy da9da08
Merge branch 'master' of github.com:envoyproxy/envoy into format-add-…
tomocy 02a46bd
fix
tomocy 3c94bf2
replace with type aliases
tomocy 4eb323d
fix
tomocy a66968a
fix not to fix typedef
tomocy 47630b3
fix
tomocy 1e9fffd
Merge branch 'master' of github.com:envoyproxy/envoy into format-add-…
tomocy 5c20a6f
fix
tomocy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -168,6 +168,7 @@ | |
| "extensions/filters/network/common", | ||
| "extensions/filters/network/common/redis", | ||
| } | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. revert |
||
| # yapf: enable | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| #!/usr/bin/env python3 | ||
|
|
||
| import argparse | ||
| import re | ||
| import sys | ||
|
|
||
| NON_TYPE_ALIAS_ALLOWED_TYPES = { | ||
| "^(?![A-Z].*).*$", | ||
| "^(.*::){,1}(StrictMock<|NiceMock<).*$", | ||
| "^(.*::){,1}(Test|Mock|Fake).*$", | ||
| "^Protobuf.*::.*$", | ||
| "^[A-Z]$", | ||
| "^.*, .*", | ||
| r"^.*\[\]$", | ||
| } | ||
|
|
||
| USING_TYPE_ALIAS_REGEX = re.compile("using .* = .*;") | ||
| SMART_PTR_REGEX = re.compile("std::(unique_ptr|shared_ptr)<(.*?)>(?!;)") | ||
| OPTIONAL_REF_REGEX = re.compile("absl::optional<std::reference_wrapper<(.*?)>>(?!;)") | ||
| NON_TYPE_ALIAS_ALLOWED_TYPE_REGEX = re.compile(f"({'|'.join(NON_TYPE_ALIAS_ALLOWED_TYPES)})") | ||
|
|
||
|
|
||
| def whitelistedForNonTypeAlias(name): | ||
| return NON_TYPE_ALIAS_ALLOWED_TYPE_REGEX.match(name) | ||
|
|
||
|
|
||
| def checkSourceLine(line, reportError): | ||
| if not USING_TYPE_ALIAS_REGEX.search(line): | ||
| smart_ptrs = SMART_PTR_REGEX.finditer(line) | ||
| for smart_ptr in smart_ptrs: | ||
| if not whitelistedForNonTypeAlias(smart_ptr.group(2)): | ||
| reportError(f"Use type alias for '{smart_ptr.group(2)}' instead. See STYLE.md") | ||
|
|
||
| optional_refs = OPTIONAL_REF_REGEX.finditer(line) | ||
| for optional_ref in optional_refs: | ||
| if not whitelistedForNonTypeAlias(optional_ref.group(1)): | ||
| reportError(f"Use type alias for '{optional_ref.group(1)}' instead. See STYLE.md") | ||
|
|
||
|
|
||
| def printError(error): | ||
| print(f"ERROR: {error}") | ||
|
|
||
|
|
||
| def checkFormat(line): | ||
| error_messages = [] | ||
|
|
||
| def reportError(message): | ||
| error_messages.append(f"'{line}': {message}") | ||
|
|
||
| checkSourceLine(line, reportError) | ||
|
|
||
| return error_messages | ||
|
|
||
|
|
||
| def checkErrors(errors): | ||
| if errors: | ||
| for e in errors: | ||
| printError(e) | ||
| return True | ||
|
|
||
| return False | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| parser = argparse.ArgumentParser(description="Check line format.") | ||
| parser.add_argument("target_line", type=str, help="specify the line to check the format of") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| target_line = args.target_line | ||
|
|
||
| errors = checkFormat(target_line) | ||
|
|
||
| if checkErrors(errors): | ||
| printError("check format failed.") | ||
| sys.exit(1) | ||
|
|
||
| print("PASS") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove trailing whitespace