From 70bb7e0e1d7231928b51db444cef3fbd231ca7f6 Mon Sep 17 00:00:00 2001 From: primoly <168267431+primoly@users.noreply.github.com> Date: Sun, 28 Apr 2024 10:20:10 +0200 Subject: [PATCH] Fix C and C++ keywords with underscores collisions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Convert to snake case *before* checking for keyword collisions, so C and C++ keywords with underscores will be “escaped” as well. --- crates/c/src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/c/src/lib.rs b/crates/c/src/lib.rs index 23f91bc3d..8e68db96c 100644 --- a/crates/c/src/lib.rs +++ b/crates/c/src/lib.rs @@ -3140,7 +3140,8 @@ pub fn is_arg_by_pointer(resolve: &Resolve, ty: &Type) -> bool { } pub fn to_c_ident(name: &str) -> String { - match name { + let ident = name.to_snake_case(); + match ident.as_str() { // Escape C and C++ keywords. // Source: https://en.cppreference.com/w/cpp/keyword "alignas" => "alignas_".into(), @@ -3245,6 +3246,6 @@ pub fn to_c_ident(name: &str) -> String { // variable names for option and result flattening. "ret" => "ret_".into(), "err" => "err_".into(), - s => s.to_snake_case(), + _ => ident, } }