Bug 81656 - incompatible _Alignas silently accepted
Summary: incompatible _Alignas silently accepted
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 8.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid
Depends on:
Blocks:
 
Reported: 2017-08-01 20:15 UTC by Martin Sebor
Modified: 2024-04-09 04:57 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Sebor 2017-08-01 20:15:45 UTC
While testing my improvements to detect mutually exclusive attribute specifications I came across another problem, this one specific to the _Alignas specifier (but it should also apply to attribute aligned. both on objects and functions).

In 6.7.5, p7, C11 specifies the following two requirements on _Alignas:

  If the definition of an object has an alignment specifier, any other declaration of that object shall either specify equivalent alignment or have no alignment specifier.  If the definition of an object does not have an alignment specifier, any other declaration of that object shall also have no alignment specifier.

The test case below violates both of these requirements and so should be diagnosed but isn't.  The problem seems to be  (as in bug 81544 and some others) that the handle_aligned_attribute() function in c-family/c-attribs.c doesn't have access to the last declaration as it processes a new one.  That GCC manages to do the reasonable thing despite it (i.e., use the most restrictive alignment) means that some other mechanism must detect and resolve the conflict, but without diagnosing it.

$ cat b.c && gcc -O2 -S -Wall -Wextra -Wpedantic -o /dev/stdout b.c
extern _Alignas (32) int a;

_Alignas (8) int a = 32;   // violates sentence 1


extern _Alignas (32) int b;
extern _Alignas (16) int b;
extern _Alignas (8) int b;

int b = 32;   // violates sentence 2

	.file	"b.c"
	.globl	b
	.data
	.align 32
	.type	b, @object
	.size	b, 4
b:
	.long	32
	.globl	a
	.align 32
	.type	a, @object
	.size	a, 4
a:
	.long	32
	.ident	"GCC: (GNU) 8.0.0 20170727 (experimental)"
	.section	.note.GNU-stack,"",@progbits
Comment 1 jsm-csl@polyomino.org.uk 2017-08-01 22:44:28 UTC
To be clear: that requirement is not a constraint, so no diagnostic is 
required.  Diagnosis may make sense for _Alignas, but I don't think 
different choices of __attribute__ ((aligned)) should be diagnosed, as a 
matter of compatibility with existing code.
Comment 2 Martin Sebor 2017-08-02 15:06:05 UTC
See also bug 81566 for a related problem with attribute aligned on function declarations.