aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/parse.c
diff options
authorLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2017-01-05 04:22:19 +0100
committerChristopher Li <sparse@chrisli.org>2017-02-13 09:34:45 +0800
commit0f89a2c57648682bb4c8c195feadcb750f10fd52 (patch)
treed46a350fb1881848440a286b2c44e071e982c03b /parse.c
parentcb172dce6555c0b1eb1771a374018721ddf8b8a5 (diff)
downloadsparse-dev-0f89a2c57648682bb4c8c195feadcb750f10fd52.tar.gz
C11: teach sparse about '_Alignas()'
This is quite similar to GCC's 'attribute(aligned(..))' but defined as a new specifier and also accepting a typename as argument. Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com> Signed-off-by: Christopher Li <sparse@chrisli.org>
Diffstat (limited to 'parse.c')
-rw-r--r--parse.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/parse.c b/parse.c
index 1a2139a3..b65a6ebc 100644
--- a/parse.c
+++ b/parse.c
@@ -129,6 +129,12 @@ static struct symbol_op noreturn_op = {
.declarator = noreturn_specifier,
};
+static declarator_t alignas_specifier;
+static struct symbol_op alignas_op = {
+ .type = KW_MODIFIER,
+ .declarator = alignas_specifier,
+};
+
static struct symbol_op auto_op = {
.type = KW_MODIFIER,
.declarator = auto_specifier,
@@ -448,6 +454,8 @@ static struct init_keyword {
{ "_Noreturn", NS_TYPEDEF, .op = &noreturn_op },
+ { "_Alignas", NS_TYPEDEF, .op = &alignas_op },
+
/* Ignored for now.. */
{ "restrict", NS_TYPEDEF, .op = &restrict_op},
{ "__restrict", NS_TYPEDEF, .op = &restrict_op},
@@ -1391,6 +1399,41 @@ static struct token *noreturn_specifier(struct token *next, struct decl_state *c
return next;
}
+static struct token *alignas_specifier(struct token *token, struct decl_state *ctx)
+{
+ int alignment = 0;
+
+ if (!match_op(token, '(')) {
+ sparse_error(token->pos, "expected '(' after _Alignas");
+ return token;
+ }
+ if (lookup_type(token->next)) {
+ struct symbol *sym = NULL;
+ token = typename(token->next, &sym, NULL);
+ sym = examine_symbol_type(sym);
+ alignment = sym->ctype.alignment;
+ token = expect(token, ')', "after _Alignas(...");
+ } else {
+ struct expression *expr = NULL;
+ token = parens_expression(token, &expr, "after _Alignas");
+ if (!expr)
+ return token;
+ alignment = const_expression_value(expr);
+ }
+
+ if (alignment < 0) {
+ warning(token->pos, "non-positive alignment");
+ return token;
+ }
+ if (alignment & (alignment-1)) {
+ warning(token->pos, "non-power-of-2 alignment");
+ return token;
+ }
+ if (alignment > ctx->ctype.alignment)
+ ctx->ctype.alignment = alignment;
+ return token;
+}
+
static struct token *const_qualifier(struct token *next, struct decl_state *ctx)
{
apply_qualifier(&next->pos, &ctx->ctype, MOD_CONST);