pw_presubmit: Exclude generated proto code
This change updates the presubmit script to not run checks on generated
protobuf files and headers.
Change-Id: I6a7844701bb26a87f5385ab9cd0e26abaadddd03
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/13085
Commit-Queue: Alexei Frolov <frolv@google.com>
Reviewed-by: Wyatt Hepler <hepler@google.com>
diff --git a/pw_presubmit/py/pw_presubmit/format_code.py b/pw_presubmit/py/pw_presubmit/format_code.py
index 4f84408..19d0954 100755
--- a/pw_presubmit/py/pw_presubmit/format_code.py
+++ b/pw_presubmit/py/pw_presubmit/format_code.py
@@ -237,6 +237,7 @@
class CodeFormat(NamedTuple):
language: str
extensions: Collection[str]
+ exclude: Collection[str]
check: Callable[[Iterable], Dict[Path, str]]
fix: Callable[[Iterable], None]
@@ -244,38 +245,39 @@
C_FORMAT: CodeFormat = CodeFormat(
'C and C++',
frozenset(['.h', '.hh', '.hpp', '.c', '.cc', '.cpp', '.inc', '.inl']),
- clang_format_check, clang_format_fix)
+ (r'\.pb\.h$', r'\.pb\.c$'), clang_format_check, clang_format_fix)
-PROTO_FORMAT: CodeFormat = CodeFormat('Protocol buffer', ('.proto', ),
+PROTO_FORMAT: CodeFormat = CodeFormat('Protocol buffer', ('.proto', ), (),
clang_format_check, clang_format_fix)
-JAVA_FORMAT: CodeFormat = CodeFormat('Java', ('.java', ), clang_format_check,
- clang_format_fix)
+JAVA_FORMAT: CodeFormat = CodeFormat('Java', ('.java', ), (),
+ clang_format_check, clang_format_fix)
-JAVASCRIPT_FORMAT: CodeFormat = CodeFormat('JavaScript', ('.js', ),
+JAVASCRIPT_FORMAT: CodeFormat = CodeFormat('JavaScript', ('.js', ), (),
clang_format_check,
clang_format_fix)
-GO_FORMAT: CodeFormat = CodeFormat('Go', ('.go', ), check_go_format,
+GO_FORMAT: CodeFormat = CodeFormat('Go', ('.go', ), (), check_go_format,
fix_go_format)
-PYTHON_FORMAT: CodeFormat = CodeFormat('Python', ('.py', ), check_py_format,
- fix_py_format)
+PYTHON_FORMAT: CodeFormat = CodeFormat('Python', ('.py', ), (),
+ check_py_format, fix_py_format)
-GN_FORMAT: CodeFormat = CodeFormat('GN', ('.gn', '.gni'), check_gn_format,
+GN_FORMAT: CodeFormat = CodeFormat('GN', ('.gn', '.gni'), (), check_gn_format,
fix_gn_format)
# TODO(pwbug/191): Add real code formatting support for Bazel and CMake
-BAZEL_FORMAT: CodeFormat = CodeFormat('Bazel', ('BUILD', ),
+BAZEL_FORMAT: CodeFormat = CodeFormat('Bazel', ('BUILD', ), (),
check_trailing_space, fix_trailing_space)
CMAKE_FORMAT: CodeFormat = CodeFormat('CMake', ('CMakeLists.txt', '.cmake'),
- check_trailing_space, fix_trailing_space)
+ (), check_trailing_space,
+ fix_trailing_space)
-RST_FORMAT: CodeFormat = CodeFormat('reStructuredText', ('.rst', ),
+RST_FORMAT: CodeFormat = CodeFormat('reStructuredText', ('.rst', ), (),
check_trailing_space, fix_trailing_space)
-MARKDOWN_FORMAT: CodeFormat = CodeFormat('Markdown', ('.md', ),
+MARKDOWN_FORMAT: CodeFormat = CodeFormat('Markdown', ('.md', ), (),
check_trailing_space,
fix_trailing_space)
@@ -297,6 +299,7 @@
def presubmit_check(code_format: CodeFormat, **filter_paths_args) -> Callable:
"""Creates a presubmit check function from a CodeFormat object."""
filter_paths_args.setdefault('endswith', code_format.extensions)
+ filter_paths_args.setdefault('exclude', code_format.exclude)
@pw_presubmit.filter_paths(**filter_paths_args)
def check_code_format(ctx: pw_presubmit.PresubmitContext):
diff --git a/pw_presubmit/py/pw_presubmit/pigweed_presubmit.py b/pw_presubmit/py/pw_presubmit/pigweed_presubmit.py
index dbaedf9..eb37b7c 100755
--- a/pw_presubmit/py/pw_presubmit/pigweed_presubmit.py
+++ b/pw_presubmit/py/pw_presubmit/pigweed_presubmit.py
@@ -205,6 +205,8 @@
r'\bpackage.json$',
r'\byarn.lock$',
r'\bpw_web_ui/types/serial.d.ts$',
+ r'\.pb\.h$',
+ r'\.pb\.c$',
)
diff --git a/pw_presubmit/py/pw_presubmit/presubmit.py b/pw_presubmit/py/pw_presubmit/presubmit.py
index de12e80..3f7b532 100644
--- a/pw_presubmit/py/pw_presubmit/presubmit.py
+++ b/pw_presubmit/py/pw_presubmit/presubmit.py
@@ -554,7 +554,7 @@
return filter_paths_for_function
-@filter_paths(endswith='.h')
+@filter_paths(endswith='.h', exclude=(r'\.pb\.h$', ))
def pragma_once(ctx: PresubmitContext) -> None:
"""Presubmit check that ensures all header files contain '#pragma once'."""