* [PATCH 0/3] fix ignored type attributes
@ 2018-01-22 6:36 Luc Van Oostenryck
2018-01-22 6:36 ` [PATCH 1/3] add testcases for " Luc Van Oostenryck
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Luc Van Oostenryck @ 2018-01-22 6:36 UTC (permalink / raw)
To: linux-sparse; +Cc: Luc Van Oostenryck
These patches contains a fix for struct/union/enum
attributes that were wrongly either ignored or
associated with the subsequent variables if present.
These patches are available in the Git repository at:
git://github.com/lucvoo/sparse-dev.git fix-ignored-type-attributes
Luc Van Oostenryck (3):
add testcases for ignored type attributes
move up apply_ctype()'s declaration
fix: do not ignore struct/union/enum type attributes
parse.c | 9 +++++++--
validation/type-attribute-align.c | 19 +++++++++++++++++++
validation/type-attribute-as.c | 33 +++++++++++++++++++++++++++++++++
validation/type-attribute-mod.c | 21 +++++++++++++++++++++
4 files changed, 80 insertions(+), 2 deletions(-)
create mode 100644 validation/type-attribute-align.c
create mode 100644 validation/type-attribute-as.c
create mode 100644 validation/type-attribute-mod.c
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 1/3] add testcases for ignored type attributes
2018-01-22 6:36 [PATCH 0/3] fix ignored type attributes Luc Van Oostenryck
@ 2018-01-22 6:36 ` Luc Van Oostenryck
2018-01-22 6:36 ` [PATCH 2/3] move up apply_ctype()'s declaration Luc Van Oostenryck
2018-01-22 6:36 ` [PATCH 3/3] fix: do not ignore struct/union/enum type attributes Luc Van Oostenryck
2 siblings, 0 replies; 4+ messages in thread
From: Luc Van Oostenryck @ 2018-01-22 6:36 UTC (permalink / raw)
To: linux-sparse; +Cc: Luc Van Oostenryck
Currently, sparse doesn't correctly take in account
type attributes.
Add some test cases for those.
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
validation/type-attribute-align.c | 20 ++++++++++++++++++++
validation/type-attribute-as.c | 34 ++++++++++++++++++++++++++++++++++
validation/type-attribute-mod.c | 22 ++++++++++++++++++++++
3 files changed, 76 insertions(+)
create mode 100644 validation/type-attribute-align.c
create mode 100644 validation/type-attribute-as.c
create mode 100644 validation/type-attribute-mod.c
diff --git a/validation/type-attribute-align.c b/validation/type-attribute-align.c
new file mode 100644
index 000000000..d56769c44
--- /dev/null
+++ b/validation/type-attribute-align.c
@@ -0,0 +1,20 @@
+#define __aligned(N) __attribute__((aligned(N)))
+#define alignof(X) __alignof__(X)
+
+struct s {
+ short a, b, c;
+} __aligned(2*sizeof(short));
+
+static int fs(void) { return sizeof(struct s); }
+static int fa(void) { return alignof(struct s); }
+
+void main(void)
+{
+ _Static_assert( sizeof(struct s) == 4 * sizeof(short), "size");
+ _Static_assert(alignof(struct s) == 2 * sizeof(short), "alignment");
+}
+
+/*
+ * check-name: type-attribute-align
+ * check-known-to-fail
+ */
diff --git a/validation/type-attribute-as.c b/validation/type-attribute-as.c
new file mode 100644
index 000000000..375ad4c76
--- /dev/null
+++ b/validation/type-attribute-as.c
@@ -0,0 +1,34 @@
+#define __user __attribute__((address_space(1)))
+
+struct s {
+ int i;
+} __user;
+
+
+extern void use0(void *);
+extern void use1(void __user *);
+
+void main(void)
+{
+ struct s s;
+ int i;
+
+ use0(&s); // KO
+ use0(&i); // OK
+ use1(&s); // OK
+ use1(&i); // KO
+}
+
+/*
+ * check-name: type-attribute-as
+ * check-known-to-fail
+ *
+ * check-error-start
+type-attribute-as.c:16:15: warning: incorrect type in argument 1 (different address spaces)
+type-attribute-as.c:16:15: expected void *<noident>
+type-attribute-as.c:16:15: got struct s <asn:1>*<noident>
+type-attribute-as.c:19:15: warning: incorrect type in argument 1 (different address spaces)
+type-attribute-as.c:19:15: expected void <asn:1>*<noident>
+type-attribute-as.c:19:15: got int *<noident>
+ * check-error-end
+ */
diff --git a/validation/type-attribute-mod.c b/validation/type-attribute-mod.c
new file mode 100644
index 000000000..0e7b166a4
--- /dev/null
+++ b/validation/type-attribute-mod.c
@@ -0,0 +1,22 @@
+#define __noderef __attribute__((noderef))
+
+struct s {
+ int i;
+} __noderef;
+
+
+void main(void)
+{
+ struct s s;
+
+ s.i = 0;
+}
+
+/*
+ * check-name: type-attribute-mod
+ * check-known-to-fail
+ *
+ * check-error-start
+type-attribute-mod.c:12:9: warning: dereference of noderef expression
+ * check-error-end
+ */
--
2.16.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH 2/3] move up apply_ctype()'s declaration
2018-01-22 6:36 [PATCH 0/3] fix ignored type attributes Luc Van Oostenryck
2018-01-22 6:36 ` [PATCH 1/3] add testcases for " Luc Van Oostenryck
@ 2018-01-22 6:36 ` Luc Van Oostenryck
2018-01-22 6:36 ` [PATCH 3/3] fix: do not ignore struct/union/enum type attributes Luc Van Oostenryck
2 siblings, 0 replies; 4+ messages in thread
From: Luc Van Oostenryck @ 2018-01-22 6:36 UTC (permalink / raw)
To: linux-sparse; +Cc: Luc Van Oostenryck
No functional changes, just to make the following patch
more readable.
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
parse.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/parse.c b/parse.c
index e255345fd..a60fcff45 100644
--- a/parse.c
+++ b/parse.c
@@ -627,6 +627,8 @@ struct statement *alloc_statement(struct position pos, int type)
static struct token *struct_declaration_list(struct token *token, struct symbol_list **list);
+static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype);
+
static void apply_modifiers(struct position pos, struct decl_state *ctx)
{
struct symbol *ctype;
@@ -962,8 +964,6 @@ static struct token *enum_specifier(struct token *token, struct decl_state *ctx)
return ret;
}
-static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype);
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH 3/3] fix: do not ignore struct/union/enum type attributes
2018-01-22 6:36 [PATCH 0/3] fix ignored type attributes Luc Van Oostenryck
2018-01-22 6:36 ` [PATCH 1/3] add testcases for " Luc Van Oostenryck
2018-01-22 6:36 ` [PATCH 2/3] move up apply_ctype()'s declaration Luc Van Oostenryck
@ 2018-01-22 6:36 ` Luc Van Oostenryck
2 siblings, 0 replies; 4+ messages in thread
From: Luc Van Oostenryck @ 2018-01-22 6:36 UTC (permalink / raw)
To: linux-sparse; +Cc: Luc Van Oostenryck
GCC's syntax for type attributes is specified as:
An attribute specifier list may appear as part of a struct,
union or enum specifier. It may go either immediately after
the struct, union or enum keyword, or after the closing brace.
The former syntax is preferred. Where attribute specifiers
follow the closing brace, they are considered to relate to
the structure, union or enumerated type defined, not to any
enclosing declaration the type specifier appears in, and the type
defined is not complete until after the attribute specifiers.
In the section about type attributes, it's also said:
You may specify type attributes in an enum, struct or union type
declaration or definition by placing them immediately after the
struct, union or enum keyword. A less preferred syntax is to
place them just past the closing curly brace of the definition.
So, while placing the attribute after the closing curly is not
preferred, it is cleary legal (and it seems to be much more popular
than placing them just after the struct, union or enum keyword).
However, currently sparse doesn't handle this correctly:
- these attributes are parsed in declaration_specifiers() and
added to the current decl_state
- when the ';' ending the type declaration is reached, the plain
struct/union/enum is used and the content of the decl_state is
simply ignored.
- if the declaration is for a variable, then those attributes
are assigned to the variable (but not to the type).
Fix this by calling handle_attribute() once we have reached the
closing '}' of a struct/union/enum definition and applying these
attributes, if any, directly to the current base type.
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
parse.c | 5 +++++
validation/type-attribute-align.c | 1 -
validation/type-attribute-as.c | 1 -
validation/type-attribute-mod.c | 1 -
4 files changed, 5 insertions(+), 3 deletions(-)
diff --git a/parse.c b/parse.c
index a60fcff45..9df4ad359 100644
--- a/parse.c
+++ b/parse.c
@@ -695,6 +695,8 @@ static struct token *struct_union_enum_specifier(enum type type,
repos = &token->pos;
token = token->next;
if (match_op(token, '{')) {
+ struct decl_state attr = { .ctype.base_type = sym, };
+
// The following test is actually wrong for empty
// structs, but (1) they are not C99, (2) gcc does
// the same thing, and (3) it's easier.
@@ -704,6 +706,9 @@ static struct token *struct_union_enum_specifier(enum type type,
token = parse(token->next, sym);
token = expect(token, '}', "at end of struct-union-enum-specifier");
+ token = handle_attributes(token, &attr, KW_ATTRIBUTE);
+ apply_ctype(token->pos, &attr.ctype, &sym->ctype);
+
// Mark the structure as needing re-examination
sym->examined = 0;
sym->endpos = token->pos;
diff --git a/validation/type-attribute-align.c b/validation/type-attribute-align.c
index d56769c44..473177c56 100644
--- a/validation/type-attribute-align.c
+++ b/validation/type-attribute-align.c
@@ -16,5 +16,4 @@ void main(void)
/*
* check-name: type-attribute-align
- * check-known-to-fail
*/
diff --git a/validation/type-attribute-as.c b/validation/type-attribute-as.c
index 375ad4c76..43021e49c 100644
--- a/validation/type-attribute-as.c
+++ b/validation/type-attribute-as.c
@@ -21,7 +21,6 @@ void main(void)
/*
* check-name: type-attribute-as
- * check-known-to-fail
*
* check-error-start
type-attribute-as.c:16:15: warning: incorrect type in argument 1 (different address spaces)
diff --git a/validation/type-attribute-mod.c b/validation/type-attribute-mod.c
index 0e7b166a4..d55011dfa 100644
--- a/validation/type-attribute-mod.c
+++ b/validation/type-attribute-mod.c
@@ -14,7 +14,6 @@ void main(void)
/*
* check-name: type-attribute-mod
- * check-known-to-fail
*
* check-error-start
type-attribute-mod.c:12:9: warning: dereference of noderef expression
--
2.16.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
end of thread, other threads:[~2018-01-22 6:36 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-01-22 6:36 [PATCH 0/3] fix ignored type attributes Luc Van Oostenryck
2018-01-22 6:36 ` [PATCH 1/3] add testcases for " Luc Van Oostenryck
2018-01-22 6:36 ` [PATCH 2/3] move up apply_ctype()'s declaration Luc Van Oostenryck
2018-01-22 6:36 ` [PATCH 3/3] fix: do not ignore struct/union/enum type attributes 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).