Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_Pragma is not supported #485

Closed
jordr opened this issue Jan 2, 2023 · 2 comments · Fixed by #487
Closed

_Pragma is not supported #485

jordr opened this issue Jan 2, 2023 · 2 comments · Fixed by #487

Comments

@jordr
Copy link
Contributor

jordr commented Jan 2, 2023

The use of _Pragma instead of #pragma is accepted in C99. [1]
pycparser raises a ParseError on this minimal main.c:

/* main.c */
_Pragma ("GCC dependency \"parse.y\"")

int main() {
        return 0;
}

Python3+pycparser prompt:

from pycparser import parse_file, c_generator
parse_file('./main.c', use_cpp=False)

Output:

>>> parse_file('./main.c', use_cpp=False)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/jordy/.local/lib/python3.8/site-packages/pycparser/__init__.py", line 90, in parse_file
    return parser.parse(text, filename)
  File "/home/jordy/.local/lib/python3.8/site-packages/pycparser/c_parser.py", line 147, in parse
    return self.cparser.parse(
  File "/home/jordy/.local/lib/python3.8/site-packages/pycparser/ply/yacc.py", line 331, in parse
    return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc)
  File "/home/jordy/.local/lib/python3.8/site-packages/pycparser/ply/yacc.py", line 1199, in parseopt_notrack
    tok = call_errorfunc(self.errorfunc, errtoken, self)
  File "/home/jordy/.local/lib/python3.8/site-packages/pycparser/ply/yacc.py", line 193, in call_errorfunc
    r = errorfunc(token)
  File "/home/jordy/.local/lib/python3.8/site-packages/pycparser/c_parser.py", line 1931, in p_error
    self._parse_error(
  File "/home/jordy/.local/lib/python3.8/site-packages/pycparser/plyparser.py", line 67, in _parse_error
    raise ParseError("%s: %s" % (coord, msg))
pycparser.plyparser.ParseError: ./main.c:1:10: before: "GCC dependency \"parse.y\""

[1] https://gcc.gnu.org/onlinedocs/cpp/Pragmas.html

@JulianKemmerer
Copy link

Interesting - maybe a regression or something I am using pycparser right now to parse _Pragmas just fine
(though surely my version is quite old, ~2018?)

my code where I use it (string replacement for macro stuff that doesnt work with
#pragma THING syntax doesnt work where THING could be another macro etc.)

#define PRAGMA_MESSAGE(x) _Pragma(#x)
// And then PRAGMA_MESSAGE() macro is used places
PRAGMA_MESSAGE(THING)

Maybe someone with string escape syntax or preprocessor used or not
🤷

@eliben
Copy link
Owner

eliben commented Jan 7, 2023

This is a gray area for pycparser since preprocessor directives start with #. The preprocessor can leave some pragmas in the source so they are kind-of supported. I don't mind supporting _Pragma similarly if the PR is not too complicated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants