pycparser icon indicating copy to clipboard operation
pycparser copied to clipboard

_Alignas(x) does not work with custom types

Open vit9696 opened this issue 3 years ago • 3 comments

typedef int test_int;

typedef struct
{
  _Alignas(int) _Atomic test_int a1;
  int a2;
} test_struct;

Adding the following sequence to c11.c causes the following error:

======================================================================
ERROR: test_c11_with_cpp (tests.test_general.TestParsing)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/user/Developer/pycparser/tests/test_general.py", line 62, in test_c11_with_cpp
    ast = parse_file(self._find_file('c11.c'), use_cpp=True,
  File "/Users/user/Developer/pycparser/pycparser/__init__.py", line 90, in parse_file
    return parser.parse(text, filename)
  File "/Users/user/Developer/pycparser/pycparser/c_parser.py", line 147, in parse
    return self.cparser.parse(
  File "/Users/user/Developer/pycparser/pycparser/ply/yacc.py", line 331, in parse
    return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc)
  File "/Users/user/Developer/pycparser/pycparser/ply/yacc.py", line 1199, in parseopt_notrack
    tok = call_errorfunc(self.errorfunc, errtoken, self)
  File "/Users/user/Developer/pycparser/pycparser/ply/yacc.py", line 193, in call_errorfunc
    r = errorfunc(token)
  File "/Users/user/Developer/pycparser/pycparser/c_parser.py", line 1931, in p_error
    self._parse_error(
  File "/Users/user/Developer/pycparser/pycparser/plyparser.py", line 67, in _parse_error
    raise ParseError("%s: %s" % (coord, msg))
pycparser.plyparser.ParseError: /Users/user/Developer/pycparser/tests/c_files/c11.c:25:34: before: a1

----------------------------------------------------------------------
Ran 130 tests in 6.860s

FAILED (errors=1)

vit9696 avatar Sep 06 '22 16:09 vit9696

It is enough to use:

typedef int test_int;

typedef struct
{
  _Alignas(int) test_int a1;
  int a2;
} test_struct;

for this issue to reproduce. The issue is actually with _Alignas, not with _Atomic.

vit9696 avatar Sep 06 '22 16:09 vit9696

@eliben, I have a suspect the issue is caused by:

    def p_declaration_specifiers_7(self, p):
        """ declaration_specifiers  : declaration_specifiers alignment_specifier
        """
        p[0] = self._add_declaration_specifier(p[1], p[2], 'alignment', append=True)

The error feels like after we parse alignment we believe our type declaration is complete, and test_int is read as a variable name. However, so far I cannot easily deduce what exactly is wrong.

vit9696 avatar Sep 06 '22 16:09 vit9696