From ee7141ef88317ffe129c2210d40c1530562ef934 Mon Sep 17 00:00:00 2001 From: Demi Marie Obenour Date: Sat, 16 Oct 2021 12:01:03 -0400 Subject: [PATCH] Reject identifiers reserved in C++ C reserves all identifiers with leading underscores in file scope, and reservers identifiers with two leading underscores or an underscore followed by a capital letter in any context. C++ also reserves identifiers with two consecutive underscores in any context. Ensure that code generated by wayland-scanner obeys these restrictions. Signed-off-by: Demi Marie Obenour --- src/scanner.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/scanner.c b/src/scanner.c index 6a956036..b232c3cb 100644 --- a/src/scanner.c +++ b/src/scanner.c @@ -653,6 +653,7 @@ validate_identifier(struct location *loc, enum identifier_role role) { const char *scan; + bool last_was_underscore = true; if (!*str) { fail(loc, "element name is empty"); @@ -665,6 +666,12 @@ validate_identifier(struct location *loc, bool is_alpha = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); bool is_digit = c >= '0' && c <= '9'; bool leading_char = (scan == str) && role == STANDALONE_IDENT; + if (c == '_' && last_was_underscore) + fail(loc, + "'%s' is not a valid identifier: identifiers must " + "not start with an underscore or have consecutive " + "underscores", str); + last_was_underscore = c == '_'; if (is_alpha || c == '_' || (!leading_char && is_digit)) continue;