Skip to content

fix: recognize includes in UTF-8 BOM-prefixed source files - #484

Merged
cclauss merged 3 commits into
cpplint:developfrom
swiast:fix/utf8-bom-source-includes
Nov 22, 2026
Merged

cclauss merged 3 commits into
cpplint:developfrom
swiast:fix/utf8-bom-source-includes

Conversation

@swiast

@swiast swiast commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #158.

Problem

UTF-8 BOM is retained as U+FEFF when source files are read using encoding="utf8". This can hide a first-line #include directive and cause a false build/include warning.

Changes

  • Use utf-8-sig for source-file decoding.
  • Add four CLI regression cases covering BOM/non-BOM files.
  • Update the changelog.

Verification

  • 4/4 targeted CLI tests passed after the fix.
  • 6/6 isolated regression checks passed.
  • GitHub Actions and pre-commit.ci checks passed.
  • The full test suite was not run locally; the targeted CLI tests used a temporary workaround for a missing unrelated test dependency.

AI assistance was used for analysis, implementation and testing.

@coderabbitai

coderabbitai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Named input files are now decoded with utf-8-sig. CLI tests cover first-line include checks with and without a UTF-8 BOM.

Changes: BOM-aware include checks

Layer / File(s) Summary
Decode BOM-prefixed files and verify include checks
cpplint.py, cpplint_clitest.py, CHANGELOG.rst
ProcessFile uses utf-8-sig to read named files. CLI tests check matching and unrelated includes with and without a BOM. The changelog records the BOM-prefixed case.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Merge Risk: Minimal · up to cb8d9

Source files that start with a UTF-8 BOM will now have their first-line include directive recognized. No actionable merge-blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Title check Passed The title names the main change: first-line include recognition in source files that start with a UTF-8 BOM. The changelog entry, the cpplint.py change from utf8 to utf-8-sig when reading named files,…
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title names the main change: first-line include recognition in source files that start with a UTF-8 BOM. The changelog entry, the cpplint.py change from utf8 to utf-8-sig when reading named files, and the two new BOM-parameterized CLI tests all match this title. The title follows the conventional 'fix:' form and is 58 characters long.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 19b0338c-51ba-4355-b875-3c3181ad95c7
Commits

Reviewing files that changed from the base of the PR and between 1e7f9d4 and cb8d9aa.

Files selected for processing: 3

  • CHANGELOG.rst
  • cpplint.py
  • cpplint_clitest.py

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

@swiast swiast changed the title Fix/utf8 bom source includes fix: recognize includes in UTF-8 BOM-prefixed source files Nov 13, 2026

@buurosaaqi-dev buurosaaqi-dev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

PR Review: #484 — fix: recognize includes in UTF-8 BOM-prefixed source files

Summary: LGTM / Approved.
A concise, elegant fix for issue #158. Switching the file read encoding from utf8 to Python's built-in utf-8-sig transparently strips the leading \xef\xbb\xbf BOM signature if present, while behaving identically to standard utf-8 on files without a BOM.


Verification

  • Test Coverage: Verified the parametrized tests in cpplint_clitest.py (test_utf8_bom_before_first_include and test_utf8_bom_still_reports_missing_own_header) accurately confirm that:
    1. An own-header #include "sample.h" preceded by a BOM is recognized and does not produce a false-positive build/include violation.
    2. Legitimate missing own-header violations continue to be flagged when an unrelated header is included, whether a BOM is present or not.
  • CI Status: Verified all 6 GitHub Actions test jobs (Ubuntu, macOS, Windows across Python 3.10 and 3.x) and pre-commit.ci are green (SUCCESS).
  • Changelog: CHANGELOG.rst correctly records the fix referencing #158.

Note for completeness (Non-blocking)

When filename == "-" (reading from sys.stdin), sys.stdin.read() uses the platform stream encoding rather than utf-8-sig. If piped inputs with a BOM ever become a concern, a minor startswith("\ufeff") strip on the stdin buffer could be added in a follow-up. For file-based usage, this PR completely and cleanly resolves the issue.

Ready to merge.

@cclauss
cclauss merged commit 527578f into cpplint:develop Nov 22, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unicode encoding breaks '[build/include] should include its header file'

3 participants