check idea: include sparse as a library in C++
Hi,
I want to include sparse as a library in C++ and access fields of the structs such as symbol and expression directly, but some of the variables in sparse are keywords in C++ so they cannot be included in C++ directly. I have several fixes below that are not ideal, and any feedback is appreciated.
1. Append an underscore to C++ keywords, for instance change new to new_. The change only involves the code that I included so there might be some C++ keywords in sparse that are unchanged.
2. Replace every keyword with a macro:
#ifdef __cplusplus
#define NEW new_
#else
#define NEW new
#endifSimilar to solution 1, this is not complete, and both 1 and 2 involves about 200 changes already.
3. When including sparse header files, guard them with
#define new new_
#include ...
#define new_ newThis does not involve change to sparse but it imposes weird restriction to the ordering of other C++ libraries that I do not understand at all.
I can send the patches if one of the fixes is acceptable. Alternatively, if there is a better way, please let me know.
Cheers,
-- Ke
Quoted 29 lines · Ke Du
Hi,
I want to include sparse as a library in C++ and access fields of the structs such as symbol and expression directly, but some of the variables in sparse are keywords in C++ so they cannot be included in C++ directly. I have several fixes below that are not ideal, and any feedback is appreciated.
1. Append an underscore to C++ keywords, for instance change new to
new_. The change only involves the code that I included so there might be some C++ keywords in sparse that are unchanged.2. Replace every keyword with a macro:
#ifdef __cplusplus #define NEW new_ #else #define NEW new #endifSimilar to solution 1, this is not complete, and both 1 and 2 involves about 200 changes already.
3. When including sparse header files, guard them with
#define new new_ #include ... #define new_ new
Quoted 5 lines
This does not involve change to sparse but it imposes weird restriction to the ordering of other C++ libraries that I do not understand at all.
I can send the patches if one of the fixes is acceptable. Alternatively, if there is a better way, please let me know.
Hi,
Please forgive me for this late answer.
I don't mind at all to make header files like expression.h, symbol.h more compatible with C++ like replacing or removing the names 'new', 'class', 'typename' & 'namespace' and adding a guarded 'extern "C"'). Maybe adding a few more '#ifdef __cplusplus' to isolate the type definitions from the inline helpers would also help.
Would you need access to anything else than 'struct expression' and 'struct symbol'? If yes, the following patch should be enough.
Cheers,
-- Luc
ast-inspect.c +1 -1
@@ -128,7 +128,7 @@ void inspect_symbol(AstNode *node) builtin_typename(sym) ?: show_ident(sym->ident));ast_append_child (node,"ctype.base_type:" , sym->ctype.base_type,inspect_symbol); -switch (sym->namespace) { +switch (sym->ns) {case NS_PREPROCESSOR :break ;default :
c2xml.c +3 -3
@@ -144,7 +144,7 @@ static void examine_modifiers(struct symbol *sym, xmlNodePtr node)int i; -if (sym->namespace !=NS_SYMBOL ) +if (sym->ns !=NS_SYMBOL )return ;/*iterate over the 32 bit bitfield*/
@@ -253,7 +253,7 @@ static void examine_namespace(struct symbol *sym)if (sym->ident && sym->ident->reserved)return ; -switch (sym->namespace) { +switch (sym->ns) {case NS_MACRO :examine_macro (sym, root_node);break ;
@@ -270,7 +270,7 @@ static void examine_namespace(struct symbol *sym)case NS_KEYWORD :break ;default : -die ("Unrecognised namespace type %d" ,sym->namespace); +die ("Unrecognised namespace type %d" ,sym->ns); } }
ctags.c +3 -3
@@ -161,7 +161,7 @@ static void examine_symbol(struct symbol *sym)default :die ("unknown symbol %s namespace:%d type:%d\n" ,show_ident (sym->ident), - sym->namespace, sym->type); + sym->ns, sym->type); }if (!sym->kind) sym->kind ='v' ;
@@ -175,7 +175,7 @@ static void examine_namespace(struct symbol *sym)if (sym->ident && sym->ident->reserved)return ; -switch (sym->namespace) { +switch (sym->ns) {case NS_KEYWORD :case NS_PREPROCESSOR :return ;
@@ -193,7 +193,7 @@ static void examine_namespace(struct symbol *sym)examine_symbol (sym);break ;default : -die ("unknown namespace %d symbol:%s type:%d\n" , sym->namespace, +die ("unknown namespace %d symbol:%s type:%d\n" , sym->ns,show_ident (sym->ident), sym->type); }add_tag (sym);
evaluate.c +1 -1
@@ -3308,7 +3308,7 @@ static struct symbol *evaluate_offsetof(struct expression *expr)static void check_label_declaration (struct position pos,struct symbol *label) { -switch (label->namespace) { +switch (label->ns) {case NS_LABEL :if (label->stmt)break ;
expression.c +1 -1
@@ -484,7 +484,7 @@ struct token *primary_expression(struct token *token, struct expression **tree)* * if (typeof(a) == int) .. */ -if (sym && sym->namespace ==NS_TYPEDEF ) { +if (sym && sym->ns ==NS_TYPEDEF ) {sparse_error (token->pos,"typename in expression" ); sym =NULL ; }
expression.h +4 -1
@@ -265,6 +265,8 @@ struct expression { }; }; +#ifndef __cplusplus +/// // Constant expression values // --------------------------
@@ -323,7 +325,7 @@ static inline int lookup_type(struct token *token) {if (token->pos.type ==TOKEN_IDENT ) {struct symbol *sym =lookup_symbol (token->ident,NS_SYMBOL |NS_TYPEDEF ); -return sym && (sym->namespace &NS_TYPEDEF ); +return sym && (sym->ns &NS_TYPEDEF ); }return 0 ; }
@@ -340,4 +342,5 @@ struct token *compound_statement(struct token *, struct statement *);void cast_value (struct expression *expr,struct symbol *newtype,struct expression *old,struct symbol *oldtype); +#endif // __cplusplus #endif
graph.c +1 -1
@@ -142,7 +142,7 @@ static void graph_calls(struct entrypoint *ep, int internal)if (insn->func->type ==PSEUDO_SYM ) {for (sym = insn->func->sym->ident->symbols; sym; sym = sym->next_id) { -if (sym->namespace &NS_SYMBOL && sym->ep) +if (sym->ns &NS_SYMBOL && sym->ep)break ; }
lib.h +3 -0
@@ -85,6 +85,8 @@ DECLARE_PTR_LIST(string_list, char);typedef struct pseudo *pseudo_t ; +#ifndef __cplusplus +#ifdef __GNUC__#define FORMAT_ATTR (pos) __attribute__ ((__format__ (__printf__, pos, pos+1)))#define NORETURN_ATTR __attribute__ ((__noreturn__))
@@ -247,4 +249,5 @@ static inline void add_ident(struct ident_list **list, struct ident *ident)#define hashval (x) ((unsigned long)(x)) +#endif // __cplusplus #endif
parse.c +14 -14
@@ -238,7 +238,7 @@ static struct symbol_op char_op = { .type = KW_SPECIFIER, .test = Set_T|Set_Long|Set_Short, .set = Set_T|Set_Char, - .class = CInt, + .type_class = CInt, };static struct symbol_op int_op = {
@@ -251,14 +251,14 @@ static struct symbol_op double_op = { .type = KW_SPECIFIER, .test = Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Vlong, .set = Set_T|Set_Double, - .class = CReal, + .type_class = CReal, };static struct symbol_op float_op = { .type =KW_SPECIFIER , .test = Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Long, .set = Set_T|Set_Float, - .class = CReal, + .type_class = CReal, };static struct symbol_op short_op = {
@@ -271,14 +271,14 @@ static struct symbol_op signed_op = { .type = KW_SPECIFIER, .test = Set_S|Set_Float|Set_Double|Set_Signed|Set_Unsigned, .set = Set_Signed, - .class = CSInt, + .type_class = CSInt, };static struct symbol_op unsigned_op = { .type =KW_SPECIFIER , .test = Set_S|Set_Float|Set_Double|Set_Signed|Set_Unsigned, .set = Set_Unsigned, - .class = CUInt, + .type_class = CUInt, };static struct symbol_op long_op = {
@@ -291,7 +291,7 @@ static struct symbol_op int128_op = { .type = KW_SPECIFIER, .test = Set_S|Set_T|Set_Char|Set_Short|Set_Int|Set_Float|Set_Double|Set_Long|Set_Vlong|Set_Int128, .set = Set_T|Set_Int128|Set_Vlong, - .class = CInt, + .type_class = CInt, };static struct symbol_op if_op = {
@@ -583,7 +583,7 @@ static const char *ignored_attributes[] = { }; -static void init_keyword (int stream,struct init_keyword *kw,enum namespace ns) +static void init_keyword (int stream,struct init_keyword *kw,enum name_space ns) {struct symbol *sym =create_symbol (stream, kw->name,SYM_KEYWORD , ns); sym->ident->keyword =1 ;
@@ -1526,7 +1526,7 @@ static struct token *declaration_specifiers(struct token *token, struct decl_stawhile (token_type (token) ==TOKEN_IDENT ) {struct symbol *s =lookup_symbol (token->ident,NS_TYPEDEF |NS_SYMBOL ); -if (!s || !(s->namespace &NS_TYPEDEF )) +if (!s || !(s->ns &NS_TYPEDEF ))break ;if (s->type !=SYM_KEYWORD ) {if (seen & Set_Any)
@@ -1545,7 +1545,7 @@ static struct token *declaration_specifiers(struct token *token, struct decl_stabreak ; } seen |= s->op->set; - class += s->op->class; + class += s->op->type_class;if (s->op->set & Set_Int128) rank =3 ;else if (s->op->set & Set_Char)
@@ -1727,11 +1727,11 @@ static int is_nested(struct token *token, struct token **p,return 1 ; } -enum kind { +enum type_class { Empty,K_R , Proto, Bad_Func, }; -static enum kind which_func (struct token *token, +static enum type_class which_func (struct token *token,struct ident **n,int prefer_abstract) {
@@ -1792,14 +1792,14 @@ static struct token *direct_declarator(struct token *token, struct decl_state *c }if (match_op (token,'(' )) { -enum kind kind =which_func (token, p, ctx->prefer_abstract); +enum type_class class =which_func (token, p, ctx->prefer_abstract);struct symbol *fn; fn =alloc_indirect_symbol (token->pos, ctype,SYM_FN ); ctype->modifiers |= ctx->f_modifiers; token = token->next; -if (kind ==K_R ) +if (class ==K_R ) token =identifier_list (token, fn); -else if (kind == Proto) +else if (class == Proto) token =parameter_type_list (token, fn); token =expect (token,')' ,"in function declarator" ); fn->endpos = token->pos;
pre-process.c +5 -5
@@ -127,7 +127,7 @@ static void replace_with_integer(struct token *token, unsigned int val)static struct symbol *lookup_macro (struct ident *ident) {struct symbol *sym =lookup_symbol (ident,NS_MACRO |NS_UNDEF ); -if (sym && sym->namespace !=NS_MACRO ) +if (sym && sym->ns !=NS_MACRO ) sym =NULL ;return sym; }
@@ -1359,7 +1359,7 @@ static int do_define(struct position pos, struct token *token, struct ident *namif (attr < sym->attr) goto out; - clean = (attr == sym->attr && sym->namespace ==NS_MACRO ); + clean = (attr == sym->attr && sym->ns ==NS_MACRO );if (token_list_different (sym->expansion, expansion) ||token_list_different (sym->arglist, arglist)) {
@@ -1388,7 +1388,7 @@ static int do_define(struct position pos, struct token *token, struct ident *nam__free_token (token); } - sym->namespace =NS_MACRO ; + sym->ns =NS_MACRO ; sym->used_in =NULL ; sym->attr = attr; out:
@@ -1526,7 +1526,7 @@ static int do_handle_undef(struct stream *stream, struct token **line, struct toif (sym) {if (attr < sym->attr)return 1 ; -if (attr == sym->attr && sym->namespace ==NS_UNDEF ) +if (attr == sym->attr && sym->ns ==NS_UNDEF )return 1 ; }else if (attr <=SYM_ATTR_NORMAL )return 1 ;
@@ -1536,7 +1536,7 @@ static int do_handle_undef(struct stream *stream, struct token **line, struct tobind_symbol (sym, left->ident,NS_MACRO ); } - sym->namespace =NS_UNDEF ; + sym->ns =NS_UNDEF ; sym->used_in =NULL ; sym->attr = attr;
ptrlist.h +2 -0
@@ -28,6 +28,7 @@DECLARE_PTR_LIST (ptr_list, void); +#ifndef __cplusplusvoid *undo_ptr_list_last (struct ptr_list **head);void *delete_ptr_list_last (struct ptr_list **head);
@@ -310,4 +311,5 @@ static inline void *tag_ptr(void *ptr, unsigned long tag)return (void *)(tag | (unsigned long )ptr); } +#endif // __cpluplus #endif /* PTR_LIST_H */
symbol.c +10 -10
@@ -57,12 +57,12 @@ void access_symbol(struct symbol *sym) } } -struct symbol *lookup_symbol (struct ident *ident,enum namespace ns) +struct symbol *lookup_symbol (struct ident *ident,enum name_space ns) {struct symbol *sym;for (sym = ident->symbols; sym; sym = sym->next_id) { -if (sym->namespace & ns) { +if (sym->ns & ns) { sym->used =1 ;return sym; }
@@ -696,7 +696,7 @@ void check_declaration(struct symbol *sym)struct symbol *next = sym;while ((next = next->next_id) !=NULL ) { -if (next->namespace != sym->namespace) +if (next->ns != sym->ns)continue ;if (sym->scope == next->scope) { sym->same_symbol = next;
@@ -730,7 +730,7 @@ static void inherit_static(struct symbol *sym)return ;for (prev = sym->next_id; prev; prev = prev->next_id) { -if (prev->namespace !=NS_SYMBOL ) +if (prev->ns !=NS_SYMBOL )continue ;if (prev->scope != file_scope)continue ;
@@ -742,7 +742,7 @@ static void inherit_static(struct symbol *sym) } } -void bind_symbol_with_scope (struct symbol *sym,struct ident *ident,enum namespace ns,struct scope *scope) +void bind_symbol_with_scope (struct symbol *sym,struct ident *ident,enum name_space ns,struct scope *scope) {if (sym->bound) {sparse_error (sym->pos,"internal error: symbol type already bound" );
@@ -752,7 +752,7 @@ void bind_symbol_with_scope(struct symbol *sym, struct ident *ident, enum namespsparse_error (sym->pos,"Trying to use reserved word '%s' as identifier" ,show_ident (ident));return ; } - sym->namespace = ns; + sym->ns = ns; sym->next_id = ident->symbols; ident->symbols = sym;if (sym->ident && sym->ident != ident)
@@ -776,7 +776,7 @@ void bind_symbol_with_scope(struct symbol *sym, struct ident *ident, enum namespbind_scope (sym, scope); } -void bind_symbol (struct symbol *sym,struct ident *ident,enum namespace ns) +void bind_symbol (struct symbol *sym,struct ident *ident,enum name_space ns) {struct scope *scope = block_scope;;
@@ -787,10 +787,10 @@ void bind_symbol(struct symbol *sym, struct ident *ident, enum namespace ns)bind_symbol_with_scope (sym, ident, ns, scope); } -struct symbol *create_symbol (int stream,const char *name,int type,int namespace) +struct symbol *create_symbol (int stream,const char *name,int type,int ns) {struct ident *ident =built_in_ident (name); -struct symbol *sym =lookup_symbol (ident, namespace); +struct symbol *sym =lookup_symbol (ident, ns);if (sym && sym->type != type) die("symbol %s created with different types: %d old %d", name,
@@ -800,7 +800,7 @@ struct symbol *create_symbol(int stream, const char *name, int type, int namespastruct token *token =built_in_token (stream, ident); sym =alloc_symbol (token->pos, type); -bind_symbol (sym, token->ident, namespace); +bind_symbol (sym, token->ident, ns); }return sym; }
symbol.h +11 -8
@@ -40,7 +40,7 @@* token contains the information on where the symbol was * declared. */ -enum namespace { +enum name_space {NS_NONE =0 ,NS_MACRO =1 ,NS_TYPEDEF =2 ,
@@ -134,7 +134,7 @@ struct symbol_op {struct symbol *(*to_mode)(struct symbol *);void (*asm_modifier)(struct token *token,unsigned long *mods,unsigned long mod); -int test, set, class; +int test, set, type_class; };
@@ -144,7 +144,7 @@ struct symbol_op {struct symbol {enum type type:8 ; -enum namespace namespace:9 ; +enum name_space ns:9 ;unsigned char used:1 , attr:2 , enum_member:1 , bound:1 ;struct position pos;/* Where this symbol was declared */ struct position endpos;/* Where this symbol ends*/
@@ -220,6 +220,8 @@ struct symbol {pseudo_t pseudo; }; +#ifndef __cplusplus +/* Modifiers */ #define MOD_AUTO 0x00000001#define MOD_REGISTER 0x00000002
@@ -330,8 +332,8 @@ extern void access_symbol(struct symbol *);extern const char *type_difference (struct ctype *c1,struct ctype *c2,unsigned long mod1,unsigned long mod2); -extern struct symbol *lookup_symbol (struct ident *,enum namespace ); -extern struct symbol *create_symbol (int stream,const char *name,int type,int namespace); +extern struct symbol *lookup_symbol (struct ident *,enum name_space ); +extern struct symbol *create_symbol (int stream,const char *name,int type,int ns);extern void init_symbols (void );extern void init_builtins (int stream);extern void init_linearized_builtins (int stream);
@@ -345,8 +347,8 @@ extern int show_symbol_expr_init(struct symbol *sym);extern void show_type_list (struct symbol *);extern void show_symbol_list (struct symbol_list *);extern void add_symbol (struct symbol_list **,struct symbol *); -extern void bind_symbol (struct symbol *,struct ident *,enum namespace ); -extern void bind_symbol_with_scope (struct symbol *,struct ident *,enum namespace ,struct scope *); +extern void bind_symbol (struct symbol *,struct ident *,enum name_space ); +extern void bind_symbol_with_scope (struct symbol *,struct ident *,enum name_space ,struct scope *);extern struct symbol *examine_symbol_type (struct symbol *);extern struct symbol *examine_pointer_target (struct symbol *);
@@ -552,7 +554,7 @@ static inline long long extend_value(long long val, struct symbol *ctype)return bits_extend (val, size, is_signed); } -static inline struct symbol *lookup_keyword (struct ident *ident,enum namespace ns) +static inline struct symbol *lookup_keyword (struct ident *ident,enum name_space ns) {if (!ident->keyword)return NULL ;
@@ -589,4 +591,5 @@ static inline void combine_address_space(struct position pos, } } +#endif // __cplusplus #endif /* SYMBOL_H */
token.h +3 -0
@@ -197,6 +197,8 @@ struct token { }; }; +#ifndef __cplusplus +#define MAX_STRING 8191static inline struct token *containing_token (struct token **p)
@@ -249,4 +251,5 @@ static inline int match_token_zero(struct token *token)return token->number[0 ] =='0' && !token->number[1 ]; } +#endif // __cplusplus #endif
ast-inspect.c +1 -1
@@ -128,7 +128,7 @@ void inspect_symbol(AstNode *node) builtin_typename(sym) ?: show_ident(sym->ident));ast_append_child (node,"ctype.base_type:" , sym->ctype.base_type,inspect_symbol); -switch (sym->namespace) { +switch (sym->ns) {case NS_PREPROCESSOR :break ;default :
c2xml.c +3 -3
@@ -144,7 +144,7 @@ static void examine_modifiers(struct symbol *sym, xmlNodePtr node)int i; -if (sym->namespace !=NS_SYMBOL ) +if (sym->ns !=NS_SYMBOL )return ;/*iterate over the 32 bit bitfield*/
@@ -253,7 +253,7 @@ static void examine_namespace(struct symbol *sym)if (sym->ident && sym->ident->reserved)return ; -switch (sym->namespace) { +switch (sym->ns) {case NS_MACRO :examine_macro (sym, root_node);break ;
@@ -270,7 +270,7 @@ static void examine_namespace(struct symbol *sym)case NS_KEYWORD :break ;default : -die ("Unrecognised namespace type %d" ,sym->namespace); +die ("Unrecognised namespace type %d" ,sym->ns); } }
ctags.c +3 -3
@@ -161,7 +161,7 @@ static void examine_symbol(struct symbol *sym)default :die ("unknown symbol %s namespace:%d type:%d\n" ,show_ident (sym->ident), - sym->namespace, sym->type); + sym->ns, sym->type); }if (!sym->kind) sym->kind ='v' ;
@@ -175,7 +175,7 @@ static void examine_namespace(struct symbol *sym)if (sym->ident && sym->ident->reserved)return ; -switch (sym->namespace) { +switch (sym->ns) {case NS_KEYWORD :case NS_PREPROCESSOR :return ;
@@ -193,7 +193,7 @@ static void examine_namespace(struct symbol *sym)examine_symbol (sym);break ;default : -die ("unknown namespace %d symbol:%s type:%d\n" , sym->namespace, +die ("unknown namespace %d symbol:%s type:%d\n" , sym->ns,show_ident (sym->ident), sym->type); }add_tag (sym);
evaluate.c +1 -1
@@ -3308,7 +3308,7 @@ static struct symbol *evaluate_offsetof(struct expression *expr)static void check_label_declaration (struct position pos,struct symbol *label) { -switch (label->namespace) { +switch (label->ns) {case NS_LABEL :if (label->stmt)break ;
expression.c +1 -1
@@ -484,7 +484,7 @@ struct token *primary_expression(struct token *token, struct expression **tree)* * if (typeof(a) == int) .. */ -if (sym && sym->namespace ==NS_TYPEDEF ) { +if (sym && sym->ns ==NS_TYPEDEF ) {sparse_error (token->pos,"typename in expression" ); sym =NULL ; }
expression.h +4 -1
@@ -265,6 +265,8 @@ struct expression { }; }; +#ifndef __cplusplus +/// // Constant expression values // --------------------------
@@ -323,7 +325,7 @@ static inline int lookup_type(struct token *token) {if (token->pos.type ==TOKEN_IDENT ) {struct symbol *sym =lookup_symbol (token->ident,NS_SYMBOL |NS_TYPEDEF ); -return sym && (sym->namespace &NS_TYPEDEF ); +return sym && (sym->ns &NS_TYPEDEF ); }return 0 ; }
@@ -340,4 +342,5 @@ struct token *compound_statement(struct token *, struct statement *);void cast_value (struct expression *expr,struct symbol *newtype,struct expression *old,struct symbol *oldtype); +#endif // __cplusplus #endif
graph.c +1 -1
@@ -142,7 +142,7 @@ static void graph_calls(struct entrypoint *ep, int internal)if (insn->func->type ==PSEUDO_SYM ) {for (sym = insn->func->sym->ident->symbols; sym; sym = sym->next_id) { -if (sym->namespace &NS_SYMBOL && sym->ep) +if (sym->ns &NS_SYMBOL && sym->ep)break ; }
lib.h +3 -0
@@ -85,6 +85,8 @@ DECLARE_PTR_LIST(string_list, char);typedef struct pseudo *pseudo_t ; +#ifndef __cplusplus +#ifdef __GNUC__#define FORMAT_ATTR (pos) __attribute__ ((__format__ (__printf__, pos, pos+1)))#define NORETURN_ATTR __attribute__ ((__noreturn__))
@@ -247,4 +249,5 @@ static inline void add_ident(struct ident_list **list, struct ident *ident)#define hashval (x) ((unsigned long)(x)) +#endif // __cplusplus #endif
parse.c +14 -14
@@ -238,7 +238,7 @@ static struct symbol_op char_op = { .type = KW_SPECIFIER, .test = Set_T|Set_Long|Set_Short, .set = Set_T|Set_Char, - .class = CInt, + .type_class = CInt, };static struct symbol_op int_op = {
@@ -251,14 +251,14 @@ static struct symbol_op double_op = { .type = KW_SPECIFIER, .test = Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Vlong, .set = Set_T|Set_Double, - .class = CReal, + .type_class = CReal, };static struct symbol_op float_op = { .type =KW_SPECIFIER , .test = Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Long, .set = Set_T|Set_Float, - .class = CReal, + .type_class = CReal, };static struct symbol_op short_op = {
@@ -271,14 +271,14 @@ static struct symbol_op signed_op = { .type = KW_SPECIFIER, .test = Set_S|Set_Float|Set_Double|Set_Signed|Set_Unsigned, .set = Set_Signed, - .class = CSInt, + .type_class = CSInt, };static struct symbol_op unsigned_op = { .type =KW_SPECIFIER , .test = Set_S|Set_Float|Set_Double|Set_Signed|Set_Unsigned, .set = Set_Unsigned, - .class = CUInt, + .type_class = CUInt, };static struct symbol_op long_op = {
@@ -291,7 +291,7 @@ static struct symbol_op int128_op = { .type = KW_SPECIFIER, .test = Set_S|Set_T|Set_Char|Set_Short|Set_Int|Set_Float|Set_Double|Set_Long|Set_Vlong|Set_Int128, .set = Set_T|Set_Int128|Set_Vlong, - .class = CInt, + .type_class = CInt, };static struct symbol_op if_op = {
@@ -583,7 +583,7 @@ static const char *ignored_attributes[] = { }; -static void init_keyword (int stream,struct init_keyword *kw,enum namespace ns) +static void init_keyword (int stream,struct init_keyword *kw,enum name_space ns) {struct symbol *sym =create_symbol (stream, kw->name,SYM_KEYWORD , ns); sym->ident->keyword =1 ;
@@ -1526,7 +1526,7 @@ static struct token *declaration_specifiers(struct token *token, struct decl_stawhile (token_type (token) ==TOKEN_IDENT ) {struct symbol *s =lookup_symbol (token->ident,NS_TYPEDEF |NS_SYMBOL ); -if (!s || !(s->namespace &NS_TYPEDEF )) +if (!s || !(s->ns &NS_TYPEDEF ))break ;if (s->type !=SYM_KEYWORD ) {if (seen & Set_Any)
@@ -1545,7 +1545,7 @@ static struct token *declaration_specifiers(struct token *token, struct decl_stabreak ; } seen |= s->op->set; - class += s->op->class; + class += s->op->type_class;if (s->op->set & Set_Int128) rank =3 ;else if (s->op->set & Set_Char)
@@ -1727,11 +1727,11 @@ static int is_nested(struct token *token, struct token **p,return 1 ; } -enum kind { +enum type_class { Empty,K_R , Proto, Bad_Func, }; -static enum kind which_func (struct token *token, +static enum type_class which_func (struct token *token,struct ident **n,int prefer_abstract) {
@@ -1792,14 +1792,14 @@ static struct token *direct_declarator(struct token *token, struct decl_state *c }if (match_op (token,'(' )) { -enum kind kind =which_func (token, p, ctx->prefer_abstract); +enum type_class class =which_func (token, p, ctx->prefer_abstract);struct symbol *fn; fn =alloc_indirect_symbol (token->pos, ctype,SYM_FN ); ctype->modifiers |= ctx->f_modifiers; token = token->next; -if (kind ==K_R ) +if (class ==K_R ) token =identifier_list (token, fn); -else if (kind == Proto) +else if (class == Proto) token =parameter_type_list (token, fn); token =expect (token,')' ,"in function declarator" ); fn->endpos = token->pos;
pre-process.c +5 -5
@@ -127,7 +127,7 @@ static void replace_with_integer(struct token *token, unsigned int val)static struct symbol *lookup_macro (struct ident *ident) {struct symbol *sym =lookup_symbol (ident,NS_MACRO |NS_UNDEF ); -if (sym && sym->namespace !=NS_MACRO ) +if (sym && sym->ns !=NS_MACRO ) sym =NULL ;return sym; }
@@ -1359,7 +1359,7 @@ static int do_define(struct position pos, struct token *token, struct ident *namif (attr < sym->attr) goto out; - clean = (attr == sym->attr && sym->namespace ==NS_MACRO ); + clean = (attr == sym->attr && sym->ns ==NS_MACRO );if (token_list_different (sym->expansion, expansion) ||token_list_different (sym->arglist, arglist)) {
@@ -1388,7 +1388,7 @@ static int do_define(struct position pos, struct token *token, struct ident *nam__free_token (token); } - sym->namespace =NS_MACRO ; + sym->ns =NS_MACRO ; sym->used_in =NULL ; sym->attr = attr; out:
@@ -1526,7 +1526,7 @@ static int do_handle_undef(struct stream *stream, struct token **line, struct toif (sym) {if (attr < sym->attr)return 1 ; -if (attr == sym->attr && sym->namespace ==NS_UNDEF ) +if (attr == sym->attr && sym->ns ==NS_UNDEF )return 1 ; }else if (attr <=SYM_ATTR_NORMAL )return 1 ;
@@ -1536,7 +1536,7 @@ static int do_handle_undef(struct stream *stream, struct token **line, struct tobind_symbol (sym, left->ident,NS_MACRO ); } - sym->namespace =NS_UNDEF ; + sym->ns =NS_UNDEF ; sym->used_in =NULL ; sym->attr = attr;
ptrlist.h +2 -0
@@ -28,6 +28,7 @@DECLARE_PTR_LIST (ptr_list, void); +#ifndef __cplusplusvoid *undo_ptr_list_last (struct ptr_list **head);void *delete_ptr_list_last (struct ptr_list **head);
@@ -310,4 +311,5 @@ static inline void *tag_ptr(void *ptr, unsigned long tag)return (void *)(tag | (unsigned long )ptr); } +#endif // __cpluplus #endif /* PTR_LIST_H */
symbol.c +10 -10
@@ -57,12 +57,12 @@ void access_symbol(struct symbol *sym) } } -struct symbol *lookup_symbol (struct ident *ident,enum namespace ns) +struct symbol *lookup_symbol (struct ident *ident,enum name_space ns) {struct symbol *sym;for (sym = ident->symbols; sym; sym = sym->next_id) { -if (sym->namespace & ns) { +if (sym->ns & ns) { sym->used =1 ;return sym; }
@@ -696,7 +696,7 @@ void check_declaration(struct symbol *sym)struct symbol *next = sym;while ((next = next->next_id) !=NULL ) { -if (next->namespace != sym->namespace) +if (next->ns != sym->ns)continue ;if (sym->scope == next->scope) { sym->same_symbol = next;
@@ -730,7 +730,7 @@ static void inherit_static(struct symbol *sym)return ;for (prev = sym->next_id; prev; prev = prev->next_id) { -if (prev->namespace !=NS_SYMBOL ) +if (prev->ns !=NS_SYMBOL )continue ;if (prev->scope != file_scope)continue ;
@@ -742,7 +742,7 @@ static void inherit_static(struct symbol *sym) } } -void bind_symbol_with_scope (struct symbol *sym,struct ident *ident,enum namespace ns,struct scope *scope) +void bind_symbol_with_scope (struct symbol *sym,struct ident *ident,enum name_space ns,struct scope *scope) {if (sym->bound) {sparse_error (sym->pos,"internal error: symbol type already bound" );
@@ -752,7 +752,7 @@ void bind_symbol_with_scope(struct symbol *sym, struct ident *ident, enum namespsparse_error (sym->pos,"Trying to use reserved word '%s' as identifier" ,show_ident (ident));return ; } - sym->namespace = ns; + sym->ns = ns; sym->next_id = ident->symbols; ident->symbols = sym;if (sym->ident && sym->ident != ident)
@@ -776,7 +776,7 @@ void bind_symbol_with_scope(struct symbol *sym, struct ident *ident, enum namespbind_scope (sym, scope); } -void bind_symbol (struct symbol *sym,struct ident *ident,enum namespace ns) +void bind_symbol (struct symbol *sym,struct ident *ident,enum name_space ns) {struct scope *scope = block_scope;;
@@ -787,10 +787,10 @@ void bind_symbol(struct symbol *sym, struct ident *ident, enum namespace ns)bind_symbol_with_scope (sym, ident, ns, scope); } -struct symbol *create_symbol (int stream,const char *name,int type,int namespace) +struct symbol *create_symbol (int stream,const char *name,int type,int ns) {struct ident *ident =built_in_ident (name); -struct symbol *sym =lookup_symbol (ident, namespace); +struct symbol *sym =lookup_symbol (ident, ns);if (sym && sym->type != type) die("symbol %s created with different types: %d old %d", name,
@@ -800,7 +800,7 @@ struct symbol *create_symbol(int stream, const char *name, int type, int namespastruct token *token =built_in_token (stream, ident); sym =alloc_symbol (token->pos, type); -bind_symbol (sym, token->ident, namespace); +bind_symbol (sym, token->ident, ns); }return sym; }
symbol.h +11 -8
@@ -40,7 +40,7 @@* token contains the information on where the symbol was * declared. */ -enum namespace { +enum name_space {NS_NONE =0 ,NS_MACRO =1 ,NS_TYPEDEF =2 ,
@@ -134,7 +134,7 @@ struct symbol_op {struct symbol *(*to_mode)(struct symbol *);void (*asm_modifier)(struct token *token,unsigned long *mods,unsigned long mod); -int test, set, class; +int test, set, type_class; };
@@ -144,7 +144,7 @@ struct symbol_op {struct symbol {enum type type:8 ; -enum namespace namespace:9 ; +enum name_space ns:9 ;unsigned char used:1 , attr:2 , enum_member:1 , bound:1 ;struct position pos;/* Where this symbol was declared */ struct position endpos;/* Where this symbol ends*/
@@ -220,6 +220,8 @@ struct symbol {pseudo_t pseudo; }; +#ifndef __cplusplus +/* Modifiers */ #define MOD_AUTO 0x00000001#define MOD_REGISTER 0x00000002
@@ -330,8 +332,8 @@ extern void access_symbol(struct symbol *);extern const char *type_difference (struct ctype *c1,struct ctype *c2,unsigned long mod1,unsigned long mod2); -extern struct symbol *lookup_symbol (struct ident *,enum namespace ); -extern struct symbol *create_symbol (int stream,const char *name,int type,int namespace); +extern struct symbol *lookup_symbol (struct ident *,enum name_space ); +extern struct symbol *create_symbol (int stream,const char *name,int type,int ns);extern void init_symbols (void );extern void init_builtins (int stream);extern void init_linearized_builtins (int stream);
@@ -345,8 +347,8 @@ extern int show_symbol_expr_init(struct symbol *sym);extern void show_type_list (struct symbol *);extern void show_symbol_list (struct symbol_list *);extern void add_symbol (struct symbol_list **,struct symbol *); -extern void bind_symbol (struct symbol *,struct ident *,enum namespace ); -extern void bind_symbol_with_scope (struct symbol *,struct ident *,enum namespace ,struct scope *); +extern void bind_symbol (struct symbol *,struct ident *,enum name_space ); +extern void bind_symbol_with_scope (struct symbol *,struct ident *,enum name_space ,struct scope *);extern struct symbol *examine_symbol_type (struct symbol *);extern struct symbol *examine_pointer_target (struct symbol *);
@@ -552,7 +554,7 @@ static inline long long extend_value(long long val, struct symbol *ctype)return bits_extend (val, size, is_signed); } -static inline struct symbol *lookup_keyword (struct ident *ident,enum namespace ns) +static inline struct symbol *lookup_keyword (struct ident *ident,enum name_space ns) {if (!ident->keyword)return NULL ;
@@ -589,4 +591,5 @@ static inline void combine_address_space(struct position pos, } } +#endif // __cplusplus #endif /* SYMBOL_H */
token.h +3 -0
@@ -197,6 +197,8 @@ struct token { }; }; +#ifndef __cplusplus +#define MAX_STRING 8191static inline struct token *containing_token (struct token **p)
@@ -249,4 +251,5 @@ static inline int match_token_zero(struct token *token)return token->number[0 ] =='0' && !token->number[1 ]; } +#endif // __cplusplus #endif
cover fixes for using sparse as a C++ library
This series contain fixes to use sparse header files as C++ libaries. Please merge your previous changes you sent first, for this patchset depends on those changes.
Ke Du (2):
expose more functions to C++ in header files
make implicit type conversion explicit
lib.h | 4 ++-- ptrlist.h | 4 ++-- symbol.h | 3 --- token.h | 5 ++--- 4 files changed, 6 insertions(+), 10 deletions(-)
-- gst
2.31.1
PATCH 1/2 expose more functions to C++ in header files
Delete or shrink the ifndef __cplusplus guard to expose more facilities to C++, such as sparse_initialize and the FOR_EACH_PTR macro.
--- lib.h | 4 ++-- ptrlist.h | 2 +- symbol.h | 3 --- token.h | 3 +-- 4 files changed, 4 insertions(+), 8 deletions(-)
lib.h +2 -2
@@ -85,8 +85,6 @@ DECLARE_PTR_LIST(string_list, char);typedef struct pseudo *pseudo_t ; -#ifndef __cplusplus -#ifdef __GNUC__#define FORMAT_ATTR (pos) __attribute__ ((__format__ (__printf__, pos, pos+1)))#define NORETURN_ATTR __attribute__ ((__noreturn__))
@@ -143,6 +141,8 @@ extern struct symbol_list *sparse_keep_tokens(char *filename);extern struct symbol_list *sparse (char *filename);extern void report_stats (void ); +#ifndef __cplusplus +static inline int symbol_list_size (struct symbol_list *list) {return ptr_list_size ((struct ptr_list *)(list));
ptrlist.h +1 -1
@@ -109,6 +109,7 @@ extern void __free_ptr_list(struct ptr_list **); DO_REVERSE(ptr, __head##ptr, __list##ptr, __nr##ptr, __rname##new, \ new, __head##new, __list##new, __nr##new, PTR_ENTRY_UNTAG) +#endif // __cpluplus #define FOR_EACH_PTR (head, ptr) \ DO_FOR_EACH(head, ptr, __head##ptr, __list##ptr, __nr##ptr, __name##ptr, PTR_ENTRY_NOTAG)
@@ -311,5 +312,4 @@ static inline void *tag_ptr(void *ptr, unsigned long tag) return (void *)(tag | (unsigned long)ptr); } -#endif // __cpluplus #endif /* PTR_LIST_H */
symbol.h +0 -3
@@ -220,8 +220,6 @@ struct symbol {pseudo_t pseudo; }; -#ifndef __cplusplus -/* Modifiers */ #define MOD_AUTO 0x00000001#define MOD_REGISTER 0x00000002
@@ -591,5 +589,4 @@ static inline void combine_address_space(struct position pos, } } -#endif // __cplusplus #endif /* SYMBOL_H */
token.h +1 -2
@@ -197,7 +197,7 @@ struct token { }; }; -#ifndef __cplusplus +#define MAX_STRING 8191
@@ -251,5 +251,4 @@ static inline int match_token_zero(struct token *token) return token->number[0] == '0' && !token->number[1]; } -#endif // __cplusplus #endif
Signature · 1 line
2.31.1
Re: [PATCH 1/2] expose more functions to C++ in header files
Hi Luc,
Is this patchset getting your attention? Ke has finished his internship so if there's anything needed please let me know.
Thanks,
Xiao
Quoted 94 lines · Ke Du
Delete or shrink the ifndef
__cplusplusguard to expose more facilities to C++, such assparse_initializeand theFOR_EACH_PTRmacro.Signed-off-by Ke Du <[email protected]>Reviewed-by Xiao Jia <[email protected]>---
lib.h | 4 ++-- ptrlist.h | 2 +- symbol.h | 3 --- token.h | 3 +-- 4 files changed, 4 insertions(+), 8 deletions(-)lib.h +2
-2@@ -85,8 +85,6 @@ DECLARE_PTR_LIST(string_list, char);typedef struct pseudo *pseudo_t ; -#ifndef __cplusplus -#ifdef __GNUC__#define FORMAT_ATTR (pos) __attribute__ ((__format__ (__printf__, pos, pos+1)))#define NORETURN_ATTR __attribute__ ((__noreturn__))@@ -143,6 +141,8 @@ extern struct symbol_list *sparse_keep_tokens(char *filename);extern struct symbol_list *sparse (char *filename);extern void report_stats (void ); +#ifndef __cplusplus +static inline int symbol_list_size (struct symbol_list *list) {return ptr_list_size ((struct ptr_list *)(list));ptrlist.h +1
-1@@ -109,6 +109,7 @@ extern void __free_ptr_list(struct ptr_list **); DO_REVERSE(ptr, __head##ptr, __list##ptr, __nr##ptr, __rname##new, \ new, __head##new, __list##new, __nr##new, PTR_ENTRY_UNTAG) +#endif // __cpluplus #define FOR_EACH_PTR (head, ptr) \ DO_FOR_EACH(head, ptr, __head##ptr, __list##ptr, __nr##ptr, __name##ptr, PTR_ENTRY_NOTAG)@@ -311,5 +312,4 @@ static inline void *tag_ptr(void *ptr, unsigned long tag) return (void *)(tag | (unsigned long)ptr); } -#endif // __cpluplus #endif /* PTR_LIST_H */ symbol.h +0
-3@@ -220,8 +220,6 @@ struct symbol {pseudo_t pseudo; }; -#ifndef __cplusplus -/* Modifiers */ #define MOD_AUTO 0x00000001#define MOD_REGISTER 0x00000002@@ -591,5 +589,4 @@ static inline void combine_address_space(struct position pos, } } -#endif // __cplusplus #endif /* SYMBOL_H */ token.h +1
-2@@ -197,7 +197,7 @@ struct token { }; }; -#ifndef __cplusplus +#define MAX_STRING 8191@@ -251,5 +251,4 @@ static inline int match_token_zero(struct token *token) return token->number[0] == '0' && !token->number[1]; } -#endif // __cplusplus #endif Signature · 1 line
2.31.1
PATCH 2/2 make implicit type conversion explicit
In reply to Ke DuMake implicit type converison from void* to other types explicit so ptrlist.h and token.h can be included as C++ library. Without this change, when including these in C++, clang complains that it cannot initialize return object of a more specific type with an lvalue of type void*.
--- ptrlist.h | 2 +- token.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
ptrlist.h +1 -1
@@ -303,7 +303,7 @@ extern void split_ptr_list_head(struct ptr_list *);static inline void update_tag (void *p,unsigned long tag) { -unsigned long *ptr = p; +unsigned long *ptr = (unsigned long *)p; *ptr = tag | (~3UL & *ptr); }
token.h +1 -1
@@ -204,7 +204,7 @@ struct token {static inline struct token *containing_token (struct token **p) {void *addr = (char *)p - ((char *)&((struct token *)0 )->next - (char *)0 ); -return addr; +return (struct token *)addr; }#define token_type (x) ((x)->pos.type)
Signature · 1 line
2.31.1