eliben / pycparser

:snake: Complete C99 parser in pure Python
Other
3.26k stars 612 forks source link

Incorrect Parsing of Hexadecimal Floating Point Literals #556

Open graypinkfurball opened 1 week ago

graypinkfurball commented 1 week ago

Related to #253. I noticed that in c_parser.py at L1882, it ignores type suffixes on hexadecimal floating point literals.

This commit explicitly ignoring hexadecimal literals, and this commit added an incorrect test.

float* a = { 0.1f, 0.1, 0.1l, 0x1p0f, 0x1p0, 0x1p0l };
FileAST:
  Decl: a, [], [], [], []
    PtrDecl: []
      TypeDecl: a, [], None
        IdentifierType: ['float']
    InitList:
      Constant: float, 0.1f
      Constant: double, 0.1
      Constant: long double, 0.1l
      Constant: float, 0x1p0f
      Constant: float, 0x1p0
      Constant: float, 0x1p0l

The type of the hexadecimal constants should be "float", "double" and "long double" in that order. Test cases should be amended.


I ran a little test with my compiler to verify the sizes of the literals.

#include <stdio.h>
int main(void) {
  printf("%llu %llu %llu\n%llu %llu %llu",
    sizeof(0.1f), sizeof(0.1), sizeof(0.1l),
    sizeof(0x1p0f), sizeof(0x1p0), sizeof(0x1p0l));
  return 0;
}
4 8 16
4 8 16
eliben commented 1 week ago

Thank you for the report. Please feel free to submit a PR with a fix