* [PATCH v2 1/2] struct_union_enum_specifier: always initialize sym->scope
@ 2020-02-19 16:29 Oleg Nesterov
2020-02-19 16:29 ` [PATCH v2 2/2] dissect: fix sym_is_local(SYM_STRUCT/UNION/ENUM) Oleg Nesterov
2020-02-20 0:56 ` [PATCH v2 1/2] struct_union_enum_specifier: always initialize sym->scope Luc Van Oostenryck
0 siblings, 2 replies; 5+ messages in thread
From: Oleg Nesterov @ 2020-02-19 16:29 UTC (permalink / raw)
To: Luc Van Oostenryck; +Cc: Alexey Gladkov, linux-sparse
Currently it is not possible to figure out the scope of the private
struct/union/enum type, its ->scope is NULL because bind_symbol() is
not called.
Change struct_union_enum_specifier() to set sym->scope = block_scope
in this case, this is what bind_symbol() does when type has a name.
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
parse.c | 2 ++
scope.c | 5 +++++
scope.h | 1 +
3 files changed, 8 insertions(+)
diff --git a/parse.c b/parse.c
index a08165a..3e7c60e 100644
--- a/parse.c
+++ b/parse.c
@@ -772,6 +772,8 @@ static struct token *struct_union_enum_specifier(enum type type,
}
sym = alloc_symbol(token->pos, type);
+ /* make toplevel(sym->scope) work */
+ set_current_scope(sym);
token = parse(token->next, sym);
ctx->ctype.base_type = sym;
token = expect(token, '}', "at end of specifier");
diff --git a/scope.c b/scope.c
index cbf2fcf..420c0f5 100644
--- a/scope.c
+++ b/scope.c
@@ -40,6 +40,11 @@ struct scope *block_scope = &builtin_scope, // regular automatic variables etc
*file_scope = &builtin_scope, // static
*global_scope = &builtin_scope; // externally visible
+void set_current_scope(struct symbol *sym)
+{
+ sym->scope = block_scope;
+}
+
void bind_scope(struct symbol *sym, struct scope *scope)
{
sym->scope = scope;
diff --git a/scope.h b/scope.h
index d9a14aa..3cad514 100644
--- a/scope.h
+++ b/scope.h
@@ -53,6 +53,7 @@ extern void end_symbol_scope(void);
extern void start_function_scope(void);
extern void end_function_scope(void);
+extern void set_current_scope(struct symbol *);
extern void bind_scope(struct symbol *, struct scope *);
extern void rebind_scope(struct symbol *, struct scope *);
--
2.5.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH v2 2/2] dissect: fix sym_is_local(SYM_STRUCT/UNION/ENUM)
2020-02-19 16:29 [PATCH v2 1/2] struct_union_enum_specifier: always initialize sym->scope Oleg Nesterov
@ 2020-02-19 16:29 ` Oleg Nesterov
2020-02-20 0:56 ` [PATCH v2 1/2] struct_union_enum_specifier: always initialize sym->scope Luc Van Oostenryck
1 sibling, 0 replies; 5+ messages in thread
From: Oleg Nesterov @ 2020-02-19 16:29 UTC (permalink / raw)
To: Luc Van Oostenryck; +Cc: Alexey Gladkov, linux-sparse
Now that struct_union_enum_specifier() always sets sym->scope we can
simplify sym_is_local(sym) and rely on toplevel() even if sym is type.
Test-case:
// copied from linux kernel
# define __force __attribute__((force))
#define WRITE_ONCE(x, val) \
({ \
union { typeof(x) __val; char __c[1]; } __u = \
{ .__val = (__force typeof(x)) (val) }; \
__write_once_size(&(x), __u.__c, sizeof(x)); \
__u.__val; \
})
void func(int *p)
{
WRITE_ONCE(*p, 0);
}
before this patch the widely used WRITE_ONCE() generates a lot of spam which
can't be filtered out using sym_is_local(),
11:6 def f func void ( ... )
11:11 func def . v p int *
13:9 def s :__u
13:9 --- . v p int *
13:9 def m :__u.__val int
13:9 def m :__u.__c char [1]
13:9 func def . v __u union :__u
13:9 func -w- . v __u union :__u
13:9 func -w- m :__u.__val int
13:9 func --- . v p int *
13:9 func --r f __write_once_size bad type
13:9 func -r- . v p int *
13:9 func -r- . v __u union :__u
13:9 func m-- m :__u.__c char [1]
13:9 func --- . v p int *
13:9 func --- . v __u union :__u
13:9 func --- m :__u.__val int
plus it triggers warning("no context") in test-dissect.c. With this patch
the only "nonlocal" report is __write_once_size() call.
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
dissect.c | 4 ++--
dissect.h | 3 ++-
2 files changed, 4 insertions(+), 3 deletions(-)
diff --git a/dissect.c b/dissect.c
index 40baf64..c48214b 100644
--- a/dissect.c
+++ b/dissect.c
@@ -152,7 +152,6 @@ static inline struct symbol *expr_symbol(struct expression *expr)
if (!sym) {
sym = alloc_symbol(expr->pos, SYM_BAD);
bind_symbol(sym, expr->symbol_name, NS_SYMBOL);
- sym->ctype.modifiers = MOD_EXTERN | MOD_TOPLEVEL;
sym->kind = expr->op ?: 'v'; /* see EXPR_CALL */
}
}
@@ -238,7 +237,8 @@ static void examine_sym_node(struct symbol *node, struct symbol *parent)
return;
dctx = dissect_ctx;
- dissect_ctx = NULL;
+ if (toplevel(base->scope))
+ dissect_ctx = NULL;
if (base->ident || deanon(base, name, parent))
reporter->r_symdef(base);
diff --git a/dissect.h b/dissect.h
index 326d3dc..a77a932 100644
--- a/dissect.h
+++ b/dissect.h
@@ -4,6 +4,7 @@
#include <stdio.h>
#include "parse.h"
#include "expression.h"
+#include "scope.h"
#define U_SHIFT 8
@@ -29,7 +30,7 @@ extern struct symbol *dissect_ctx;
static inline bool sym_is_local(struct symbol *sym)
{
- return sym->kind == 'v' && !(sym->ctype.modifiers & MOD_TOPLEVEL);
+ return !toplevel(sym->scope);
}
extern void dissect(struct reporter *, struct string_list *);
--
2.5.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH v2 1/2] struct_union_enum_specifier: always initialize sym->scope
2020-02-19 16:29 [PATCH v2 1/2] struct_union_enum_specifier: always initialize sym->scope Oleg Nesterov
2020-02-19 16:29 ` [PATCH v2 2/2] dissect: fix sym_is_local(SYM_STRUCT/UNION/ENUM) Oleg Nesterov
@ 2020-02-20 0:56 ` Luc Van Oostenryck
2020-02-20 11:57 ` Oleg Nesterov
1 sibling, 1 reply; 5+ messages in thread
From: Luc Van Oostenryck @ 2020-02-20 0:56 UTC (permalink / raw)
To: Oleg Nesterov; +Cc: Alexey Gladkov, linux-sparse
On Wed, Feb 19, 2020 at 05:29:11PM +0100, Oleg Nesterov wrote:
> Currently it is not possible to figure out the scope of the private
> struct/union/enum type, its ->scope is NULL because bind_symbol() is
> not called.
>
> Change struct_union_enum_specifier() to set sym->scope = block_scope
> in this case, this is what bind_symbol() does when type has a name.
Thanks.
I've just changed the comment to "used by dissect" because
elsewhere the scope or toplevel()s only relevant for symbols.
-- Luc
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2 1/2] struct_union_enum_specifier: always initialize sym->scope
2020-02-20 0:56 ` [PATCH v2 1/2] struct_union_enum_specifier: always initialize sym->scope Luc Van Oostenryck
@ 2020-02-20 11:57 ` Oleg Nesterov
2020-02-20 12:15 ` Luc Van Oostenryck
0 siblings, 1 reply; 5+ messages in thread
From: Oleg Nesterov @ 2020-02-20 11:57 UTC (permalink / raw)
To: Luc Van Oostenryck; +Cc: Alexey Gladkov, linux-sparse
On 02/20, Luc Van Oostenryck wrote:
>
> On Wed, Feb 19, 2020 at 05:29:11PM +0100, Oleg Nesterov wrote:
> > Currently it is not possible to figure out the scope of the private
> > struct/union/enum type, its ->scope is NULL because bind_symbol() is
> > not called.
> >
> > Change struct_union_enum_specifier() to set sym->scope = block_scope
> > in this case, this is what bind_symbol() does when type has a name.
>
> Thanks.
> I've just changed the comment to "used by dissect"
Great, thanks!
> because
> elsewhere the scope or toplevel()s only relevant for symbols.
Cough... can't resist ;)
Not really, see struct_union_enum_specifier()->is_outer_scope(). But
yes sure, this is only when ->ident != NULL.
Oleg.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2 1/2] struct_union_enum_specifier: always initialize sym->scope
2020-02-20 11:57 ` Oleg Nesterov
@ 2020-02-20 12:15 ` Luc Van Oostenryck
0 siblings, 0 replies; 5+ messages in thread
From: Luc Van Oostenryck @ 2020-02-20 12:15 UTC (permalink / raw)
To: Oleg Nesterov; +Cc: Alexey Gladkov, linux-sparse
On Thu, Feb 20, 2020 at 12:57:37PM +0100, Oleg Nesterov wrote:
> On 02/20, Luc Van Oostenryck wrote:
> >
> > On Wed, Feb 19, 2020 at 05:29:11PM +0100, Oleg Nesterov wrote:
> > > Currently it is not possible to figure out the scope of the private
> > > struct/union/enum type, its ->scope is NULL because bind_symbol() is
> > > not called.
> > >
> > > Change struct_union_enum_specifier() to set sym->scope = block_scope
> > > in this case, this is what bind_symbol() does when type has a name.
> >
> > Thanks.
> > I've just changed the comment to "used by dissect"
>
> Great, thanks!
>
> > because
> > elsewhere the scope or toplevel()s only relevant for symbols.
>
> Cough... can't resist ;)
>
> Not really, see struct_union_enum_specifier()->is_outer_scope(). But
> yes sure, this is only when ->ident != NULL.
Ah yes, sorry, I wasn't clear enough here. By 'symbol' here, I
effectively meant: "a name (with its associated semantic)", not
a 'struct symbol'.
-- Luc
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2020-02-20 12:15 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-02-19 16:29 [PATCH v2 1/2] struct_union_enum_specifier: always initialize sym->scope Oleg Nesterov
2020-02-19 16:29 ` [PATCH v2 2/2] dissect: fix sym_is_local(SYM_STRUCT/UNION/ENUM) Oleg Nesterov
2020-02-20 0:56 ` [PATCH v2 1/2] struct_union_enum_specifier: always initialize sym->scope Luc Van Oostenryck
2020-02-20 11:57 ` Oleg Nesterov
2020-02-20 12:15 ` Luc Van Oostenryck
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).