Skip to content

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

Open

Description

@marco-c

Part of #1142 (upstream tree-sitter/tree-sitter-cpp#307).

The C++ grammar only accepts an identifier before a string literal in a concatenated_string, so PRIi32 "%" parses but "%" PRIi32 produces an ERROR node:

nsPrintfCString("%" PRIi32, lifetime.mTag);

This is one of the most common patterns in mozilla-central: the <inttypes.h> format macros (PRIu32, PRIx64, PRId64, ...) are used after a string literal throughout MOZ_LOG, nsPrintfCString, printf_stderr, SprintfLiteral and similar calls. test_fn_id_strings in src/c_langs_macros/mod.rs covers exactly this case and has been #[ignore]d since #1141.

Replacing predefined macros with $ characters (c_macro::replace) doesn't help: "%" $$$$$$ fails the same way. And it's only applied when preprocessor results are available anyway.

Allowing any identifier after a string literal would be ambiguous with things like extern "C" MOZ_EXPORT void f();. A narrower fix in tree-sitter-mozcpp is to accept the <inttypes.h> format macros (PRI*/SCN*) as members of a concatenated_string, and then un-ignore test_fn_id_strings.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions