為GCC新增中文關鍵字
阿新 • • 發佈:2019-01-04
知乎原文地址 作者:@狗屎咖啡
GCC不支援UTF-8字元,可以自己新增:
也可以 用指令碼轉換原始碼,再編譯:
https://gcc.gnu.org/wiki/FAQ#utf8_identifiers
但是強烈建議打上支援UTF-8的補丁。
例項 swizl/gcn
新增關鍵字
在 gcc/c-family/c-common.c 中
const struct c_common_resword c_common_reswords[] 下新增
複製一行原關鍵字,再將字串該成中文
例如
{ "asm", RID_ASM, D_ASM }, { "彙編", RID_ASM, D_ASM }, { "auto", RID_AUTO, 0 }, { "自動", RID_AUTO, 0 }, { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN }, { "布林", RID_BOOL, D_CXXONLY | D_CXXWARN }, { "break", RID_BREAK, 0 }, { "斷", RID_BREAK, 0 }, { "case", RID_CASE, 0 }, { "例", RID_CASE, 0 }, { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN }, { "抓", RID_CATCH, D_CXX_OBJC | D_CXXWARN }, { "char", RID_CHAR, 0 }, { "字", RID_CHAR, 0 },
新增中文巨集關鍵字
在 libcpp/directives.c 中
1 新增巨集定義A,仿照D,加一個引數name_cn. 因為原始GCC不支援UTF-8
#name 中 name為中文時無法解析,所以name_cn直接對於字串。
當然已經打了支援UTF-8的補丁,沒有這個問題。相容起見,還是按照這樣改。
#define D(name, t, o, f) static void do_##name (cpp_reader *); DIRECTIVE_TABLE #undef D #define D(n, tag, o, f) tag, enum { DIRECTIVE_TABLE N_DIRECTIVES }; #undef D #define D(name, t, origin, flags) \ { do_##name, (const uchar *) #name, \ sizeof #name - 1, origin, flags }, static const directive dtable[] = { DIRECTIVE_TABLE }; #undef D #define D(name, t, origin, flags) #name, static const char * const directive_names[] = { DIRECTIVE_TABLE NULL }; #undef D
改成
#define A(name_cn, name, t, o, f) #define D(name, t, o, f) static void do_##name (cpp_reader *); DIRECTIVE_TABLE #undef D #undef A #define A(n_cn, n, tag, o, f) tag ## _CN, #define D(n, tag, o, f) tag, enum { DIRECTIVE_TABLE N_DIRECTIVES }; #undef D #undef A #define A(name_cn, name, t, origin, flags) \ { do_##name, (const uchar *) name_cn, \ sizeof (name_cn) - 1, origin, flags }, #define D(name, t, origin, flags) \ { do_##name, (const uchar *) #name, \ sizeof #name - 1, origin, flags }, static const directive dtable[] = { DIRECTIVE_TABLE }; #undef D #undef A #define A(name_cn, name, t, origin, flags) name_cn, #define D(name, t, origin, flags) #name, static const char * const directive_names[] = { DIRECTIVE_TABLE NULL }; #undef D #undef A
2 新增巨集關鍵字
在 #define DIRECTIVE_TABLE 中新增
複製一行原巨集關鍵字,該D為A,新增中文名 字串。
例如
#define DIRECTIVE_TABLE \
D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
A("定義", define, T_DEFINE, KANDR, IN_I) /* 270554 */ \
D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \
A("含", include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \
D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
A("瞭如", endif, T_ENDIF, KANDR, COND) /* 45855 */ \
D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
A("如定義", ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \
A("如", if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \
D(else, T_ELSE, KANDR, COND) /* 9863 */ \
A("另", else, T_ELSE, KANDR, COND) /* 9863 */ \
D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
A("如未定義", ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
A("消定義", undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
A("行", line, T_LINE, KANDR, EXPAND) /* 2465 */ \
D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \
A("另如", elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \
D(error, T_ERROR, STDC89, 0) /* 475 */ \
A("錯誤", error, T_ERROR, STDC89, 0) /* 475 */ \
D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
A("雜注", pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
A("告警", warning, T_WARNING, EXTENSION, 0) /* 22 */ \
D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \
A("含下個", include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \
D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \
A("匯入", import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \
D(assert, T_ASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \
A("斷言", assert, T_ASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \
D(unassert, T_UNASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \
A("消斷言", unassert, T_UNASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \
D(sccs, T_SCCS, EXTENSION, IN_I) /* 0 SVR4? */
make bootstrap && make install 之後,配合支援UTF-8的補丁,就可以愉快地用中文關鍵字了。