-
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
Changes from 6 commits
7ddce42
9159b57
aa2dd3c
c70a52e
bf581d4
658dc57
a49224b
0387590
06fe468
143d0db
cd5235b
09a8d13
7aef23a
04c7681
50b3f10
85d0cec
c10380f
9f9a80d
3cfca33
3818a44
2f6214e
b9d2664
9fc4db1
f40c5c0
344eb82
9b1e477
6ace91c
e45a2a3
5da95c9
7005a0b
d7a323c
934e9d3
5fdc789
91db93f
da9da08
02a46bd
3c94bf2
4eb323d
a66968a
47630b3
1e9fffd
5c20a6f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -168,8 +168,56 @@ | |
"extensions/filters/network/common", | ||
"extensions/filters/network/common/redis", | ||
} | ||
|
||
NON_TYPE_ALIAS_ALLOWED_TYPES = { | ||
"void", | ||
"bool", | ||
"short", | ||
"short int", | ||
"signed short", | ||
"signed short int", | ||
"unsigned short", | ||
"unsigned short int", | ||
"int", | ||
"signed", | ||
"signed int", | ||
"unsigned", | ||
"unsigned int", | ||
"long", | ||
"long int", | ||
"signed long", | ||
"signed long int", | ||
"unsigned long", | ||
"unsigned long int", | ||
"long long", | ||
"long long int", | ||
"signed long long", | ||
"signed long long int", | ||
"unsigned long long", | ||
"unsigned long long int", | ||
"int8_t", | ||
"int16_t", | ||
"int32_t", | ||
"int64_t", | ||
"uint8_t", | ||
"uint16_t", | ||
"uint32_t", | ||
"uint64_t", | ||
"signed char", | ||
"unsigned char", | ||
"char", | ||
"wchar_t", | ||
"char16_t", | ||
"char32_t", | ||
"std::string", | ||
"float", | ||
"double", | ||
"long double", | ||
} | ||
# yapf: enable | ||
|
||
NON_TYPE_ALIAS_ALLOWED_TYPE = re.compile(fr"(.*\[\]$|^std::vector<.*|{'|'.join(NON_TYPE_ALIAS_ALLOWED_TYPES)}$)") | ||
|
||
|
||
# Map a line transformation function across each line of a file, | ||
# writing the result lines as requested. | ||
|
@@ -352,6 +400,9 @@ def whitelistedForUnpackTo(file_path): | |
"./source/common/protobuf/utility.cc", "./source/common/protobuf/utility.h" | ||
] | ||
|
||
def whitelistedForNonTypeAlias(name): | ||
return NON_TYPE_ALIAS_ALLOWED_TYPE.match(name) | ||
|
||
|
||
def findSubstringAndReturnError(pattern, file_path, error_message): | ||
text = readFile(file_path) | ||
|
@@ -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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
smart_ptr_m = re.finditer("std::(unique_ptr|shared_ptr)<(.*?)>", line) | ||
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. what does the 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. done |
||
for m in smart_ptr_m: | ||
if not whitelistedForNonTypeAlias(m.group(2)): | ||
reportError(f"Use type alias for '{m.group(2)}' instead. See STYLE.md") | ||
|
||
optional_m = re.finditer("absl::optional<std::reference_wrapper<(.*?)>>", line) | ||
for m in optional_m: | ||
if not whitelistedForNonTypeAlias(m.group(1)): | ||
reportError(f"Use type alias for '{m.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 | ||
|
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 |
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 |
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#include <memory> | ||
|
||
namespace Envoy { | ||
namespace Network { | ||
class Connection; | ||
|
||
using ConnectionPtr = std::unique_ptr<Connection>; | ||
|
||
class A { | ||
using ConnectionSharedPtr = std::shared_ptr<Connection>; | ||
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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. done |
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