Skip to content
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

Closed
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
7ddce42
format: add checks for type alias
tomocy Jun 18, 2020
9159b57
modify using type alias match
tomocy Jun 19, 2020
aa2dd3c
modify to find all of non type alias type in line
tomocy Jun 19, 2020
c70a52e
add non type alias allowed type list
tomocy Jun 19, 2020
bf581d4
add trailing newline
tomocy Jun 22, 2020
658dc57
compile and combine regexs
tomocy Jun 22, 2020
a49224b
compile regexs
tomocy Jun 23, 2020
0387590
refactor names
tomocy Jun 23, 2020
06fe468
modify regex
tomocy Jun 23, 2020
143d0db
add check line format hook
tomocy Jun 25, 2020
cd5235b
add line format check for type alias
tomocy Jun 25, 2020
09a8d13
modify to check only source lines
tomocy Jun 25, 2020
7aef23a
fix
tomocy Jun 25, 2020
04c7681
format
tomocy Jun 25, 2020
50b3f10
add aggressive option
tomocy Jun 29, 2020
85d0cec
cherry pick check for type alias
tomocy Jun 29, 2020
c10380f
fix with type alias aggressively
tomocy Jun 29, 2020
9f9a80d
replace with type alias aggressively
tomocy Jun 29, 2020
3cfca33
declare type aliases
tomocy Jul 2, 2020
3818a44
fix format
tomocy Jul 2, 2020
2f6214e
declare type aliases
tomocy Jul 3, 2020
b9d2664
Merge branch 'master' of github.com:envoyproxy/envoy into format-add-…
tomocy Jul 3, 2020
9fc4db1
delete line format check
tomocy Jul 3, 2020
f40c5c0
replace with type aliases
tomocy Jul 3, 2020
344eb82
fix not to replace type alias decl with template
tomocy Jul 3, 2020
9b1e477
Merge branch 'master' of github.com:envoyproxy/envoy into format-add-…
tomocy Jul 7, 2020
6ace91c
replace with type aliases
tomocy Jul 7, 2020
e45a2a3
fix
tomocy Jul 9, 2020
5da95c9
fix
tomocy Jul 9, 2020
7005a0b
Merge branch 'master' of github.com:envoyproxy/envoy into format-add-…
tomocy Jul 9, 2020
d7a323c
fix
tomocy Jul 9, 2020
934e9d3
replace with type aliases
tomocy Jul 10, 2020
5fdc789
Merge branch 'master' of github.com:envoyproxy/envoy into format-add-…
tomocy Jul 11, 2020
91db93f
replace with type aliases
tomocy Jul 12, 2020
da9da08
Merge branch 'master' of github.com:envoyproxy/envoy into format-add-…
tomocy Jul 12, 2020
02a46bd
fix
tomocy Jul 12, 2020
3c94bf2
replace with type aliases
tomocy Jul 12, 2020
4eb323d
fix
tomocy Jul 13, 2020
a66968a
fix not to fix typedef
tomocy Jul 13, 2020
47630b3
fix
tomocy Jul 13, 2020
1e9fffd
Merge branch 'master' of github.com:envoyproxy/envoy into format-add-…
tomocy Jul 14, 2020
5c20a6f
fix
tomocy Jul 14, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions tools/code_format/check_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,56 @@
"extensions/filters/network/common",
"extensions/filters/network/common/redis",
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert

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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

precompile all the regexes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

smart_ptr_m = re.finditer("std::(unique_ptr|shared_ptr)<(.*?)>", line)
Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Expand Down
7 changes: 7 additions & 0 deletions tools/code_format/check_format_test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ def runChecks():
"grpc_shutdown.cc",
"Don't call grpc_init() or grpc_shutdown() directly, instantiate Grpc::GoogleGrpcContext. " +
"See #8282")
errors += checkUnfixableError("non_type_alias_smart_ptr.cc",
"Use type alias for 'Network::Connection' instead. See STYLE.md")
errors += checkUnfixableError(
"non_type_alias_optional.cc",
"Use type alias for 'ConnectionHandlerImpl::ActiveTcpListener' instead. See STYLE.md")
errors += checkUnfixableError("clang_format_double_off.cc", "clang-format nested off")
errors += checkUnfixableError("clang_format_trailing_off.cc", "clang-format remains off")
errors += checkUnfixableError("clang_format_double_on.cc", "clang-format nested on")
Expand Down Expand Up @@ -271,6 +276,8 @@ def runChecks():
errors += checkFileExpectingOK("real_time_source_override.cc")
errors += checkFileExpectingOK("time_system_wait_for.cc")
errors += checkFileExpectingOK("clang_format_off.cc")
errors += checkFileExpectingOK("using_type_alias.cc")
errors += checkFileExpectingOK("non_type_alias_allowed_type.cc")
return errors


Expand Down
2 changes: 1 addition & 1 deletion tools/testdata/check_format/cpp_std.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Envoy {

std::unique_ptr<int> to_be_fix = absl::make_unique<int>(0);
auto to_be_fix = absl::make_unique<int>(0);

// Awesome stuff goes here.

Expand Down
2 changes: 1 addition & 1 deletion tools/testdata/check_format/cpp_std.cc.gold
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Envoy {

std::unique_ptr<int> to_be_fix = std::make_unique<int>(0);
auto to_be_fix = std::make_unique<int>(0);

// Awesome stuff goes here.

Expand Down
11 changes: 11 additions & 0 deletions tools/testdata/check_format/non_type_alias_allowed_type.cc
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 tools/testdata/check_format/non_type_alias_optional.cc
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
9 changes: 9 additions & 0 deletions tools/testdata/check_format/non_type_alias_smart_ptr.cc
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
14 changes: 14 additions & 0 deletions tools/testdata/check_format/using_type_alias.cc
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add trailing newline

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done