-
Notifications
You must be signed in to change notification settings - Fork 5.3k
format: add checks for type aliases #12099
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
1
commit into
envoyproxy:master
from
tomocy:format-add-check-tool-for-type-alias
Closed
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -175,6 +175,21 @@ | |
| } | ||
| # yapf: enable | ||
|
|
||
| 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 .* = .*;|typedef .* .*;)") | ||
| 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)})") | ||
|
|
||
|
|
||
| # Map a line transformation function across each line of a file, | ||
| # writing the result lines as requested. | ||
|
|
@@ -348,6 +363,10 @@ def allowlistedForGrpcInit(file_path): | |
| return file_path in GRPC_INIT_ALLOWLIST | ||
|
|
||
|
|
||
| def whitelistedForNonTypeAlias(name): | ||
| return NON_TYPE_ALIAS_ALLOWED_TYPE_REGEX.match(name) | ||
|
|
||
|
|
||
| def allowlistedForUnpackTo(file_path): | ||
| return file_path.startswith("./test") or file_path in [ | ||
| "./source/common/protobuf/utility.cc", "./source/common/protobuf/utility.h" | ||
|
|
@@ -510,6 +529,71 @@ def reportError(message): | |
| return error_messages | ||
|
|
||
|
|
||
| def replaceWithTypeAlias(line): | ||
|
|
||
| def replaceSmartPtr(match): | ||
| kind = match.group(1) | ||
| name = match.group(2) | ||
| const = "const " in name | ||
| if const: | ||
| name = name.replace("const ", "") | ||
| if whitelistedForNonTypeAlias(name): | ||
| return match.group() | ||
|
|
||
| with_type_param = "<" in name | ||
|
|
||
| if kind == "unique_ptr" and not const and not with_type_param: | ||
| return f"{name}Ptr" | ||
| elif kind == "unique_ptr" and const and not with_type_param: | ||
| return f"{name}ConstPtr" | ||
| elif kind == "unique_ptr" and not const and with_type_param: | ||
| splitted = name.split("<") | ||
| return f"{splitted[0]}Ptr<{splitted[1]}" | ||
| elif kind == "unique_ptr" and const and with_type_param: | ||
| splitted = name.split("<") | ||
| return f"{splitted[0]}ConstPtr<{splitted[1]}" | ||
| elif kind == "shared_ptr" and not const and not with_type_param: | ||
| return f"{name}SharedPtr" | ||
| elif kind == "shared_ptr" and const and not with_type_param: | ||
| return f"{name}ConstSharedPtr" | ||
| elif kind == "shared_ptr" and not const and with_type_param: | ||
| splitted = name.split("<") | ||
| return f"{splitted[0]}SharedPtr<{splitted[1]}" | ||
| elif kind == "shared_ptr" and const and with_type_param: | ||
| splitted = name.split("<") | ||
| return f"{splitted[0]}ConstSharedPtr<{splitted[1]}" | ||
| else: | ||
| return match.group() | ||
|
|
||
| def replaceOptionalRef(match): | ||
| name = match.group(1) | ||
| const = "const " in name | ||
| if const: | ||
| name = name.replace("const ", "") | ||
| if whitelistedForNonTypeAlias(name): | ||
| return match.group() | ||
|
|
||
| with_type_param = "<" in name | ||
|
|
||
| if not const and not with_type_param: | ||
| return f"{name}OptRef" | ||
| elif const and not with_type_param: | ||
| return f"{name}OptConstRef" | ||
| elif not const and with_type_param: | ||
| splitted = name.split("<") | ||
| return f"{splitted[0]}OptRef<{splitted[1]}" | ||
| elif const and with_type_param: | ||
| splitted = name.split("<") | ||
| return f"{splitted[0]}OptConstRef<{splitted[1]}" | ||
| else: | ||
| return match.group() | ||
|
|
||
| line = SMART_PTR_REGEX.sub(replaceSmartPtr, line) | ||
| line = OPTIONAL_REF_REGEX.sub(replaceOptionalRef, line) | ||
|
|
||
| return line | ||
|
|
||
|
|
||
| DOT_MULTI_SPACE_REGEX = re.compile("\\. +") | ||
|
|
||
|
|
||
|
|
@@ -529,6 +613,9 @@ def fixSourceLine(line, line_number): | |
| for invalid_construct, valid_construct in LIBCXX_REPLACEMENTS.items(): | ||
| line = line.replace(invalid_construct, valid_construct) | ||
|
|
||
| if aggressive and not USING_TYPE_ALIAS_REGEX.search(line): | ||
| line = replaceWithTypeAlias(line) | ||
|
|
||
| return line | ||
|
|
||
|
|
||
|
|
@@ -715,6 +802,17 @@ def checkSourceLine(line, file_path, reportError): | |
| reportError("Don't call grpc_init() or grpc_shutdown() directly, instantiate " + | ||
| "Grpc::GoogleGrpcContext. See #8282") | ||
|
|
||
| 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 checkBuildLine(line, file_path, reportError): | ||
| if "@bazel_tools" in line and not (isSkylarkFile(file_path) or file_path.startswith("./bazel/") or | ||
|
|
@@ -989,6 +1087,9 @@ def checkErrorMessages(error_messages): | |
| type=str, | ||
| default=",".join(common.includeDirOrder()), | ||
| help="specify the header block include directory order.") | ||
| parser.add_argument("--aggressive", | ||
| action='store_true', | ||
| help="specify if it fixes formats making risky changes.") | ||
|
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. help="makes risky format changes; re-testing needed after making aggressive fixes" |
||
| args = parser.parse_args() | ||
|
|
||
| operation_type = args.operation_type | ||
|
|
@@ -1006,6 +1107,7 @@ def checkErrorMessages(error_messages): | |
| "./tools/clang_tools", | ||
| ] | ||
| include_dir_order = args.include_dir_order | ||
| aggressive = args.aggressive | ||
| if args.add_excluded_prefixes: | ||
| EXCLUDED_PREFIXES += tuple(args.add_excluded_prefixes) | ||
|
|
||
|
|
||
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
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
11 changes: 11 additions & 0 deletions
11
tools/testdata/check_format/non_type_alias_allowed_type.cc
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,11 @@ | ||
| #include <memory> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| namespace Envoy { | ||
|
|
||
| void a(std::unique_ptr<int>, std::shared_ptr<std::string>, | ||
| absl::optional<std::reference_wrapper<char[]>>, | ||
| absl::optional<std::reference_wrapper<std::vector<int>>>) {} | ||
|
|
||
| } // namespace Envoy |
11 changes: 11 additions & 0 deletions
11
tools/testdata/check_format/non_type_alias_optional_ref.cc
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,11 @@ | ||
| #include <memory> | ||
|
|
||
| #include "server/connection_handler_impl.h" | ||
|
|
||
| namespace Envoy { | ||
|
|
||
| absl::optional<std::reference_wrapper<ConnectionHandlerImpl::ActiveTcpListener>> a() { | ||
| return nullptr; | ||
| } | ||
|
|
||
| } // namespace Envoy |
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,9 @@ | ||
| #include <memory> | ||
|
|
||
| #include "envoy/network/connection.h" | ||
|
|
||
| namespace Envoy { | ||
|
|
||
| std::unique_ptr<Network::Connection> a() { return nullptr; } | ||
|
|
||
| } // namespace Envoy |
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,10 @@ | ||
| #include <memory> | ||
|
|
||
| namespace Envoy { | ||
| void a(std::unique_ptr<AAA>, std::unique_ptr<const AAA>, std::shared_ptr<BBB>, | ||
| std::shared_ptr<const BBB>, absl::optional<std::reference_wrapper<CCC>>, | ||
| absl::optional<std::reference_wrapper<const CCC>>, std::unique_ptr<DDD<EEE>>, | ||
| std::unique_ptr<const DDD<EEE>>, std::shared_ptr<FFF<GGG>>, std::shared_ptr<const FFF<GGG>>, | ||
| absl::optional<std::reference_wrapper<HHH<III>>>, | ||
| absl::optional<std::reference_wrapper<const HHH<III>>>) {} | ||
| } // namespace Envoy |
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,7 @@ | ||
| #include <memory> | ||
|
|
||
| namespace Envoy { | ||
| void a(AAAPtr, AAAConstPtr, BBBSharedPtr, BBBConstSharedPtr, CCCOptRef, | ||
| CCCOptConstRef, DDDPtr<EEE>, DDDConstPtr<EEE>, FFFSharedPtr<GGG>, | ||
| FFFConstSharedPtr<GGG>, HHHOptRef<III>, HHHOptConstRef<III>) {} | ||
| } // namespace Envoy |
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,17 @@ | ||
| #include <memory> | ||
|
|
||
| namespace Envoy { | ||
| namespace Network { | ||
| class Connection; | ||
|
|
||
| using ConnectionPtr = std::unique_ptr<Connection>; | ||
| typedef std::unique_ptr<Connection> ConnectionPtr; | ||
|
|
||
| template <class C> using EdfSchedulerPtr = std::unique_ptr<EdfScheduler<C>>; | ||
|
|
||
| class A { | ||
| using ConnectionSharedPtr = std::shared_ptr<Connection>; | ||
| using ConnectionOptRef = absl::optional<std::reference_wrapper<Connection>>; | ||
| }; | ||
| } // namespace Network | ||
| } // namespace Envoy |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Should we introduce type alias for
weak_ptr? This kind of smart pointer is partly used in envoyproxy/envoy and envoy/envoy-wasm. WDYT? cc. @jmarantzThere 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.
Sure, but I'd recommend not expanding the scope of this series of PRs. Would be an easier follow-up, as there are relatively fewer weak_ptr uses I think.