Skip to main content

Module semantic

Module semantic 

Source
Expand description

Semantic-level normalisation (strip comments, line endings, keyword casing) used by --verify and the idempotency tests. Semantic-level normalisation for parsed CMake.

Helpers that strip away parts of the AST that don’t affect CMake semantics — comments, line endings, keyword casing — so two files can be compared “would they behave identically?” without worrying about cosmetic-only differences.

Used by:

  • cmakefmt --verify and cmakefmt --in-place (in main.rs) to confirm formatted output preserves CMake semantics.
  • tests/idempotency.rs to assert formatter idempotency on the real-world corpus.

Previously these helpers were duplicated between main.rs and the integration test, with a hand-coded “keep in sync” comment that Phase 47g’s deduplication audit flagged. They now live here as the single source of truth.

All helpers walk a fully-parsed CommandInvocation in place. The public surface is normalize_command_literals (strip cosmetic differences from a single command) and normalize_keyword_args (uppercase known keyword tokens for case-insensitive comparison). Internal helpers stay private to the module.

Functions§

normalize_command_literals
Strip comment and line-ending differences from a parsed CommandInvocation so two semantically-equivalent commands compare equal regardless of cosmetic formatting.
normalize_keyword_args
Uppercase any unquoted argument that matches a known keyword for the command’s spec. CMake keywords are case-insensitive at the language level, so two files that differ only in the casing of PUBLIC vs public are semantically equivalent.
normalize_line_endings
Strip Windows-style \r\n line endings to plain \n in place.
normalize_semantics
Reduce a parsed file to its semantic skeleton: drop standalone comments and blank lines, zero out spans, lowercase command names, and normalise each command’s literals and keyword casing. Two files that behave identically in CMake produce equal skeletons.
semantic_equivalent
Return true if left and right are the same CMake program once cosmetic-only differences (comments, blank lines, whitespace, line endings, command-name and keyword casing) are stripped.