Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Changelog
TBA
===

* Recognize UTF-8 BOM-prefixed source files when checking their first-line includes. (#158)
* Added the standard short ``-v`` option as an alias for ``--verbose`` and ``--v``. (#331)
* Use path-aware prefix checks instead of deprecated os.path.commonprefix(). (#445)
* Fixed a whitespace/newline false positive for control conditions containing lambdas. (#410)
Expand Down
4 changes: 3 additions & 1 deletion cpplint.py
Original file line number Diff line number Diff line change
Expand Up @@ -7711,7 +7711,9 @@ def ProcessFile(filename, vlevel, extra_check_functions=None):
if filename == "-":
lines = sys.stdin.read().split("\n")
else:
with open(filename, encoding="utf8", errors="replace", newline=None) as target_file:
with open(
filename, encoding="utf-8-sig", errors="replace", newline=None
) as target_file:
lines = target_file.read().split("\n")

except OSError:
Expand Down
34 changes: 34 additions & 0 deletions cpplint_clitest.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,5 +271,39 @@ def test_third_party_headers_config(tmp_path):
assert b"build/include_subdir" in err


@pytest.mark.parametrize("with_bom", [False, True])
def test_utf8_bom_before_first_include(tmp_path, with_bom):
"""A BOM must not hide the first include from the own-header check."""
(tmp_path / "sample.h").write_text("// Copyright 2026\n", encoding="utf-8")
source = tmp_path / "sample.cc"
bom = b"\xef\xbb\xbf" if with_bom else b""
source.write_bytes(bom + b'#include "sample.h"\n')

status, out, err = run_shell_command(
BASE_CMD,
f"--filter=-,+build/include,-build/include_subdir {source.name}",
cwd=str(tmp_path),
)
assert status == 0, (out, err)
assert b"[build/include]" not in err


@pytest.mark.parametrize("with_bom", [False, True])
def test_utf8_bom_still_reports_missing_own_header(tmp_path, with_bom):
"""Stripping the signature must not disable legitimate own-header errors."""
(tmp_path / "sample.h").write_text("// Copyright 2026\n", encoding="utf-8")
source = tmp_path / "sample.cc"
bom = b"\xef\xbb\xbf" if with_bom else b""
source.write_bytes(bom + b'#include "unrelated.h"\n')

status, out, err = run_shell_command(
BASE_CMD,
f"--filter=-,+build/include,-build/include_subdir {source.name}",
cwd=str(tmp_path),
)
assert status == 1, (out, err)
assert b"[build/include]" in err


if __name__ == "__main__":
pytest.main([__file__])
Loading