-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
format: add checks for type alias #11634
format: add checks for type alias #11634
Conversation
tools/code_format/check_format.py
Outdated
@@ -696,6 +696,15 @@ def checkSourceLine(line, file_path, reportError): | |||
reportError("Don't call grpc_init() or grpc_shutdown() directly, instantiate " + | |||
"Grpc::GoogleGrpcContext. See #8282") | |||
|
|||
if not line.startswith("using"): | |||
smart_ptr_m = re.search("std::(unique_ptr|shared_ptr)<(.*?)>", line) |
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.
Is this a bit too strict? I understand now wanting to have verbose unique_ptr for commonly used types, but the occasional use of unique_ptr, in particular for non-Envoy types, makes sense.
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.
I’m going to add an allowed type list (eg. [int, std::string]) and report an error for the types which are not included in that list. I suppose it handles normal use cases well.
e8eac94
to
0bd3b19
Compare
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.
It would be nice to actually fix these errors if possible. Note that gazillion format-checks being reported in CI on this PR; I bet you don't want to fix them by hand :)
The problem is that some of the violations found may not actually have aliases in scope of their use, and it would probably be beyond the scope of the format-fixer to verify that. Usually that won't be a problem if we always declare FooPtr wherever the class definition for Foo is. But sometimes (not often) we need to pre-declare Foo, so it's not perfect.
And currently, in my experience the format fixer never breaks code, so I run it before committing without bothering to re-compile/re-test locally (CI is of course still run). I don't want to break that pattern.
So my suggestion is to have an "--aggressive" switch on the format fixer, or similar, which does risky changes, including swapping in the assumed type alias. Then you can fix the large number of errors in this PR's CI: https://dev.azure.com/cncf/envoy/_build/results?buildId=42610&view=logs&j=9fd74226-df5b-5f29-39da-76fc341cc763&t=068b98b1-acf0-52d0-fc6e-f6d1a5b1d5ef&l=4223
tools/code_format/check_format.py
Outdated
@@ -352,6 +398,9 @@ def whitelistedForUnpackTo(file_path): | |||
"./source/common/protobuf/utility.cc", "./source/common/protobuf/utility.h" | |||
] | |||
|
|||
def whitelistedForNonTypeAlias(name): | |||
return re.match(r".*\[\]$", name) or re.match("^std::vector<.*", name) or re.match(f"^({'|'.join(NON_TYPE_ALIAS_ALLOWED_TYPES)})$", name) |
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.
suggest precompiling this regex rather than re-compiling the regex for every name checked.
Also why not combine the first two cases with the third in the precompiled regex so we just have to do one pre-compiled regex check?
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.
done
using ConnectionOptRef = absl::optional<std::reference_wrapper<Connection>>; | ||
}; | ||
} // namespace Network | ||
} // namespace Envoy |
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.
add trailing newline
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.
done
Signed-off-by: tomocy <[email protected]>
Signed-off-by: tomocy <[email protected]>
Signed-off-by: tomocy <[email protected]>
Signed-off-by: tomocy <[email protected]>
Signed-off-by: tomocy <[email protected]>
Signed-off-by: tomocy <[email protected]>
0bd3b19
to
658dc57
Compare
Thank you for your review. I considered adding |
I'm having trouble parsing your phrase:
That makes me want to clarify a few things.
Does that makes sense? |
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.
/wait
tools/code_format/check_format.py
Outdated
@@ -696,6 +747,17 @@ def checkSourceLine(line, file_path, reportError): | |||
reportError("Don't call grpc_init() or grpc_shutdown() directly, instantiate " + | |||
"Grpc::GoogleGrpcContext. See #8282") | |||
|
|||
if not re.search("using .* = .*;", line): |
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.
precompile all the regexes.
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.
done
tools/code_format/check_format.py
Outdated
@@ -696,6 +747,17 @@ def checkSourceLine(line, file_path, reportError): | |||
reportError("Don't call grpc_init() or grpc_shutdown() directly, instantiate " + | |||
"Grpc::GoogleGrpcContext. See #8282") | |||
|
|||
if not re.search("using .* = .*;", line): | |||
smart_ptr_m = re.finditer("std::(unique_ptr|shared_ptr)<(.*?)>", line) |
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.
what does the _m
mean? Spell that out?
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.
done
Signed-off-by: tomocy <[email protected]>
Signed-off-by: tomocy <[email protected]>
I mean 1. I think that it needs C++ AST to resolve those errors perfectly and is difficult to do only with Python scripts. I am looking for a way to use the AST and fix it, and if it is not possible, I will replace those lines with scripts and declare type aliases manually. |
Signed-off-by: tomocy <[email protected]>
Signed-off-by: tomocy <[email protected]>
Signed-off-by: tomocy <[email protected]>
Signed-off-by: tomocy <[email protected]>
Signed-off-by: tomocy <[email protected]>
Signed-off-by: tomocy <[email protected]>
I have changed my mind (too many to fix :)) and let it check the format in the added line as pre-push. |
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.
I have to admit I'm not crazy about this approach, because when you add something like this it adds unexpected burden to anyone modifying a file with otherwise unchecked errors. If we can, I'd prefer to just fix everything and keep it fixed.
IMO, as I suggested above, I think you can do a 95% job at fixing everything automatically in Python without the C++ AST. You should just flag-protect it so that by default, the fix-format mode does not make risky changes.
But for this PR you can go ahead and let it make risky easy changes and then just fix whatever breaks when you run tests.
@@ -14,7 +14,7 @@ DUMMY_SHA=0000000000000000000000000000000000000000 | |||
echo "Running pre-push check; to skip this step use 'push --no-verify'" | |||
|
|||
while read LOCAL_REF LOCAL_SHA REMOTE_REF REMOTE_SHA | |||
do | |||
do |
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
tools/code_format/check_format.py
Outdated
@@ -168,6 +168,7 @@ | |||
"extensions/filters/network/common", | |||
"extensions/filters/network/common/redis", | |||
} | |||
|
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.
revert
/wait |
Signed-off-by: tomocy <[email protected]>
…checks-for-type-alias Signed-off-by: tomocy <[email protected]>
Signed-off-by: tomocy <[email protected]>
@jmarantz ping. It is getting ready to be reviewed. |
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.
quick drive-by: is it possible that you have added using
nicknames that are not used?
This is a tough PR to review of course because it's so massive, and will be a difficult slog staying ahead of merge conflicts.
One suggestion:
- break this up into (say) 50-file fragments without the tools. These should be easy to knock off a chunk at a time.
- review the tools separately, where we'll want to talk in detail about python and whitelists and error messages and how to test, etc
You can phase in all the .cc/.h changes while we iterate on the python. The final python checkin should only need cc/h changes for violations that were added after you started this process.
@@ -484,6 +489,8 @@ class BufferFragmentImpl : NonCopyable, public BufferFragment { | |||
const std::function<void(const void*, size_t, const BufferFragmentImpl*)> releasor_; | |||
}; | |||
|
|||
using BufferFragmentImplPtr = std::unique_ptr<BufferFragmentImpl>; |
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.
it's not obvious to me why this was needed. It does not appear to be used.
@@ -597,6 +604,8 @@ class OwnedImpl : public LibEventInstance { | |||
OverflowDetectingUInt64 length_; | |||
}; | |||
|
|||
using OwnedImplPtr = std::unique_ptr<OwnedImpl>; |
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.
is this used?
I added type aliases every the compiler failed to build due to the undefined error. But I did it manually so a part of those may not be used. My understanding is that I will checkout two kinds of branches where I cherry-pick the changes about type aliasing by 50 file fragments while I push the changes about Python tools. Is it correct? |
RE "unused type declarations": I thought we had talked about an automated approach to resolving these (even that automation was not robust enough to never fail). Sounds like you did a lot of manual work. Sorry about that. If we wind up with extra type aliases I won't get too worried. RE "branches and cherry-picks": I am really barely functional at git and do things in kind of a low-tech way. But what I would do is something like:
Even if you wanted to make K be fairly small that would be fine; the one I want to review first is the one with the tooling changes, even though we won't be able to merge it until all the others are done. Does that make sense? |
Thank you, it makes sense. I am trying in the way. |
Commit Message: format: use type alias Additional Description: N/A Risk Level: Low Testing: N/A Docs Changes: N/A Release Notes: N/A Part of #11634 Signed-off-by: tomocy <[email protected]>
Commit Message: format: use type alias Additional Description: N/A Risk Level: Low Testing: N/A Docs Changes: N/A Release Notes: N/A Part of #11634 Signed-off-by: tomocy <[email protected]>
Commit Message: format: use type alias Additional Description: N/A Risk Level: N/A Testing: N/A Docs Changes: N/A Release Notes: N/A Part of #11634 Signed-off-by: tomocy <[email protected]>
This pull request has been automatically marked as stale because it has not had activity in the last 7 days. It will be closed in 7 days if no further activity occurs. Please feel free to give a status update now, ping for review, or re-open when it's ready. Thank you for your contributions! |
Commit Message: format: use type alias Additional Description: N/A Risk Level: Low Testing: N/A Docs Changes: N/A Release Notes: N/A Part of envoyproxy#11634 Signed-off-by: tomocy <[email protected]> Signed-off-by: Kevin Baichoo <[email protected]>
Commit Message: format: use type alias Additional Description: N/A Risk Level: Low Testing: N/A Docs Changes: N/A Release Notes: N/A Part of envoyproxy#11634 Signed-off-by: tomocy <[email protected]> Signed-off-by: Kevin Baichoo <[email protected]>
Commit Message: format: use type alias Additional Description: N/A Risk Level: N/A Testing: N/A Docs Changes: N/A Release Notes: N/A Part of envoyproxy#11634 Signed-off-by: tomocy <[email protected]> Signed-off-by: Kevin Baichoo <[email protected]>
This pull request has been automatically closed because it has not had activity in the last 14 days. Please feel free to give a status update now, ping for review, or re-open when it's ready. Thank you for your contributions! |
Commit Message: format: use type alias Additional Description: N/A Risk Level: Low Testing: N/A Docs Changes: N/A Release Notes: N/A Part of envoyproxy#11634 Signed-off-by: tomocy <[email protected]> Signed-off-by: scheler <[email protected]>
Commit Message: format: use type alias Additional Description: N/A Risk Level: Low Testing: N/A Docs Changes: N/A Release Notes: N/A Part of envoyproxy#11634 Signed-off-by: tomocy <[email protected]> Signed-off-by: scheler <[email protected]>
Commit Message: format: use type alias Additional Description: N/A Risk Level: N/A Testing: N/A Docs Changes: N/A Release Notes: N/A Part of envoyproxy#11634 Signed-off-by: tomocy <[email protected]> Signed-off-by: scheler <[email protected]>
Signed-off-by: tomocy [email protected]
Commit Message: format: add checks for type alias
Additional Description: N/A
Risk Level: Low
Testing: N/A
Docs Changes: N/A
Release Notes: N/A
Fixes #9840