Skip to content

Parse a string literal followed by a format macro in mozcpp - #1269

Draft
marco-c wants to merge 1 commit into
masterfrom
mozcpp-format-macros
Draft

marco-c wants to merge 1 commit into
masterfrom
mozcpp-format-macros

Conversation

@marco-c

@marco-c marco-c commented Nov 21, 2026

Copy link
Copy Markdown
Collaborator

Fixes #1266. Part of #1142.

tree-sitter-cpp only accepts an identifier before a string literal in a concatenated_string (tree-sitter/tree-sitter-cpp#307). So "%" PRIu32, which is all over mozilla-central's logging and formatting code, produced an ERROR node.

This PR overrides concatenated_string in tree-sitter-mozcpp so that a string literal can be followed by one of the <inttypes.h> format macros ((PRI|SCN)[diouxX](8|16|32|64|FAST*|LEAST*|MAX|PTR)). The macro is aliased to identifier, so the tree has the same shape as for PRIu32 "%". Accepting any identifier there, like tree-sitter-c does, would be ambiguous with things like extern "C" MOZ_EXPORT ...; that's presumably why tree-sitter-cpp dropped it.

Checked with tree-sitter parse:

  • now parse: "%" PRIi32, "x=%" PRIu64 "\n", "a" "b" PRIx32, u8"a" PRIX64 "b" PRIuPTR "c", "%" SCNu32
  • unchanged: PRIu32 "%", int PRIu32x = 1;, #define PRIu32 "u", extern "C" int x;, extern "C" { ... }, "abc"sv
  • still an error, as before: "%s" FOO with an arbitrary macro

Also in this PR:

  • tree-sitter-mozcpp bumped to 0.20.7, parser regenerated with tree-sitter-cli 0.27.2 (same tree-sitter-cpp 0.23.4 base; scanner.c unchanged), and language_cpp.rs regenerated with recreate-grammars.sh
  • test_fn_id_strings re-enabled, with two more samples

All workspace tests pass, including the DeepSpeech/pdf.js/serde snapshot tests, with no snapshot changes.

tree-sitter-cpp doesn't accept an identifier right after the first
string literal of a concatenation, so the common `"%" PRIu32` pattern
produced an ERROR node. Accept the <inttypes.h> format macros in that
position, aliased to `identifier` like in `PRIu32 "%"`. Restricting it
to those macros avoids ambiguities like `extern "C" FOO`.

Bump tree-sitter-mozcpp to 0.20.7 and re-enable `test_fn_id_strings`.

Fixes #1266.
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.46%. Comparing base (52ed04e) to head (921a34c).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1269      +/-   ##
==========================================
+ Coverage   90.43%   90.46%   +0.02%     
==========================================
  Files          62       62              
  Lines       16458    16462       +4     
==========================================
+ Hits        14884    14892       +8     
+ Misses       1574     1570       -4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
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.

mozcpp: parse a string literal followed by a PRI*/SCN* format macro

2 participants