@@ -988,16 +988,109 @@ static TSNode resolve_qualified_name(TSNode decl) {
988988 return null_node ;
989989}
990990
991+ bool cbm_c_qualifier_is_recovered (TSNode qid ) {
992+ if (ts_node_is_null (qid )) {
993+ return false;
994+ }
995+ uint32_t nc = ts_node_child_count (qid );
996+ for (uint32_t i = 0 ; i < nc ; i ++ ) {
997+ TSNode c = ts_node_child (qid , i );
998+ if (!ts_node_is_named (c ) && strcmp (ts_node_type (c ), "::" ) == 0 ) {
999+ return ts_node_is_missing (c );
1000+ }
1001+ }
1002+ return false;
1003+ }
1004+
1005+ /* A function DEFINITION's parameter list: empty, `(void)`, or every parameter
1006+ * named (variadic `...` allowed). A function-like macro invocation that only
1007+ * looks like a declarator — `TEST_BEGIN(test_name)`, whose one "parameter" is
1008+ * an untyped identifier read as a type — fails this, so its macro name is never
1009+ * mistaken for the defined function's name. */
1010+ static bool c_params_name_every_parameter (TSNode params ) {
1011+ enum { MAX_PARAMS_CHECKED = 64 }; /* indexed child access: keep it small */
1012+ uint32_t nc = ts_node_named_child_count (params );
1013+ if (nc > MAX_PARAMS_CHECKED ) {
1014+ return false;
1015+ }
1016+ for (uint32_t i = 0 ; i < nc ; i ++ ) {
1017+ TSNode p = ts_node_named_child (params , i );
1018+ const char * pk = ts_node_type (p );
1019+ if (strcmp (pk , "comment" ) == 0 || strcmp (pk , "variadic_parameter" ) == 0 ) {
1020+ continue ;
1021+ }
1022+ if (!ts_node_is_null (ts_node_child_by_field_name (p , TS_FIELD ("declarator" )))) {
1023+ continue ;
1024+ }
1025+ /* `(void)`: the one parameter is an unnamed primitive type. */
1026+ TSNode type = ts_node_child_by_field_name (p , TS_FIELD ("type" ));
1027+ if (nc != 1 || ts_node_is_null (type ) || strcmp (ts_node_type (type ), "primitive_type" ) != 0 ) {
1028+ return false;
1029+ }
1030+ }
1031+ return true;
1032+ }
1033+
1034+ TSNode cbm_c_recovered_func_name (TSNode func_declarator ) {
1035+ TSNode null_node = {0 };
1036+ if (ts_node_is_null (func_declarator ) ||
1037+ strcmp (ts_node_type (func_declarator ), "function_declarator" ) != 0 ) {
1038+ return null_node ;
1039+ }
1040+ TSNode params = ts_node_child_by_field_name (func_declarator , TS_FIELD ("parameters" ));
1041+ if (ts_node_is_null (params ) || !c_params_name_every_parameter (params )) {
1042+ return null_node ;
1043+ }
1044+ TSNode err = ts_node_prev_sibling (params );
1045+ if (ts_node_is_null (err ) || strcmp (ts_node_type (err ), "ERROR" ) != 0 ) {
1046+ return null_node ;
1047+ }
1048+ /* Only the macro-prefix shape: the ERROR holds nothing but bare identifiers
1049+ * (the real name, possibly after further attribute macros). Any other token
1050+ * means the region is not a declaration this rule can vouch for. */
1051+ enum { MAX_PREFIX_TOKENS = 4 };
1052+ uint32_t nc = ts_node_child_count (err );
1053+ if (nc == 0 || nc > MAX_PREFIX_TOKENS ) {
1054+ return null_node ;
1055+ }
1056+ TSNode last = null_node ;
1057+ for (uint32_t i = 0 ; i < nc ; i ++ ) {
1058+ TSNode c = ts_node_child (err , i );
1059+ const char * ck = ts_node_type (c );
1060+ if (strcmp (ck , "identifier" ) != 0 && strcmp (ck , "type_identifier" ) != 0 ) {
1061+ return null_node ;
1062+ }
1063+ last = c ;
1064+ }
1065+ return last ;
1066+ }
1067+
9911068// Resolve function name from C/C++/CUDA/GLSL declarator chain. Shared canonical
9921069// implementation — see the header for the full rationale (#438).
9931070TSNode cbm_resolve_c_declarator_name_node (TSNode func_node ) {
9941071 TSNode decl = ts_node_child_by_field_name (func_node , TS_FIELD ("declarator" ));
1072+ /* Set once a recovered `RetT::name` qualifier was stepped through: on that
1073+ * path the C++ grammar names the function with a type_identifier. */
1074+ bool recovered = false;
9951075 for (int depth = 0 ; depth < CBM_DECLARATOR_DEPTH_LIMIT && !ts_node_is_null (decl ); depth ++ ) {
9961076 const char * dk = ts_node_type (decl );
997- if (is_c_terminal_name (dk )) {
1077+ if (is_c_terminal_name (dk ) || ( recovered && strcmp ( dk , "type_identifier" ) == 0 ) ) {
9981078 return decl ;
9991079 }
1080+ if (strcmp (dk , "function_declarator" ) == 0 ) {
1081+ TSNode real = cbm_c_recovered_func_name (decl );
1082+ if (!ts_node_is_null (real )) {
1083+ return real ;
1084+ }
1085+ }
10001086 if (strcmp (dk , "qualified_identifier" ) == 0 || strcmp (dk , "scoped_identifier" ) == 0 ) {
1087+ if (cbm_c_qualifier_is_recovered (decl )) {
1088+ /* `API RetT name(...)`: the "scope" is the return type, the name
1089+ * side carries the real declarator chain. */
1090+ decl = ts_node_child_by_field_name (decl , TS_FIELD ("name" ));
1091+ recovered = true;
1092+ continue ;
1093+ }
10011094 return resolve_qualified_name (decl );
10021095 }
10031096 TSNode inner = ts_node_child_by_field_name (decl , TS_FIELD ("declarator" ));
@@ -1013,6 +1106,36 @@ TSNode cbm_resolve_c_declarator_name_node(TSNode func_node) {
10131106 return null_node ;
10141107}
10151108
1109+ /* The C-declarator grammars: same set resolve_func_name_c_family routes through
1110+ * cbm_resolve_c_declarator_name_node. */
1111+ static bool c_declarator_lang (CBMLanguage lang ) {
1112+ return lang == CBM_LANG_C || lang == CBM_LANG_CPP || lang == CBM_LANG_CUDA ||
1113+ lang == CBM_LANG_GLSL || lang == CBM_LANG_HLSL || lang == CBM_LANG_ISPC ||
1114+ lang == CBM_LANG_SLANG || lang == CBM_LANG_OBJC ;
1115+ }
1116+
1117+ bool cbm_c_reserved_func_name (const char * name ) {
1118+ /* Statement/operator keywords: error recovery over preprocessor-split code
1119+ * reads `else if (a) (b) {` as a definition `else if(...) {...}`. */
1120+ static const char * const kw [] = {"if" , "else" , "for" , "while" , "do" ,
1121+ "switch" , "case" , "default" , "return" , "break" ,
1122+ "continue" , "goto" , "sizeof" , "typedef" , NULL };
1123+ if (!name ) {
1124+ return false;
1125+ }
1126+ for (const char * const * k = kw ; * k ; k ++ ) {
1127+ if (strcmp (name , * k ) == 0 ) {
1128+ return true;
1129+ }
1130+ }
1131+ return false;
1132+ }
1133+
1134+ bool cbm_is_c_preprocessor_lang (CBMLanguage lang ) {
1135+ return lang == CBM_LANG_C || lang == CBM_LANG_CPP || lang == CBM_LANG_CUDA ||
1136+ lang == CBM_LANG_GLSL || lang == CBM_LANG_OBJC || lang == CBM_LANG_ISPC ;
1137+ }
1138+
10161139// Convert a resolved function/method name node to its name string. Most nodes
10171140// map directly to their text, but a C++ conversion-operator's `operator_cast`
10181141// node spans the full "operator bool() const" — this grammar folds the parameter
@@ -1023,6 +1146,12 @@ TSNode cbm_resolve_c_declarator_name_node(TSNode func_node) {
10231146// `if (obj)`) misses.
10241147char * cbm_func_name_node_text (CBMArena * a , TSNode name_node , const char * source , CBMLanguage lang ) {
10251148 char * text = cbm_node_text (a , name_node , source );
1149+ /* A C keyword is never a function name: the definition is an error-recovery
1150+ * artifact. No name means no def and no call scope (calls inside fall back
1151+ * to the enclosing scope), the same as any unnamed definition. */
1152+ if (text && c_declarator_lang (lang ) && cbm_c_reserved_func_name (text )) {
1153+ return NULL ;
1154+ }
10261155 if (text && strcmp (ts_node_type (name_node ), "operator_cast" ) == 0 ) {
10271156 char * paren = strchr (text , '(' );
10281157 if (paren ) {
0 commit comments