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

Support __{FILE,LINE,DATE,TIME,TIMESTAMP}__ macros #125

Merged
merged 3 commits into from
Jan 3, 2025

Conversation

laurenthuberdeau
Copy link
Collaborator

Context

It was pointed out in #124 that pnut doesn't support the __FILE__ and __LINE__ built-in macros. This PR adds support for these macros and __{DATE,TIME,TIMESTAMP}__ for conformance with the standard.

Support for __FILE__ was relatively simple as we already keep track of the current file name. Same thing for __LINE__ as the line number is already computed when INCLUDE_LINE_NUMBER_ON_ERROR is activated (0 is used otherwise). The value for the other macros are hard coded to time 0 (Jan 1 1970/ 00:00:00) for simplicity.

In principle (C99 standard document section 6.10.8), the _ _STDC*_ _ macros should also be defined but pnut is far from respecting the full C99 standard so I think it's better to keep them undefined for now.

Copy link
Contributor

@VisenDev VisenDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the topic of standard compliance, C99 also defines __func__

Additionally, the preprocessor directives #file and #line can be used to override the values of __FILE__ and __LINE__, that may or may not be something we want to add

Debian woody doesn't include the base64 utility
and it cannot be installed with apt-get. base64
was only used to generate a unique ID from the
compilation options used, so it is replaced with
md5sum which is available on woody.
@laurenthuberdeau
Copy link
Collaborator Author

Thanks for the review :)

On the topic of standard compliance, C99 also defines func

True, however the C99 standard document doesn't group it with the built-in macros as it's cannot be implemented without parsing the code and is thus not a macro. I suppose it could be stubbed like __DATE__ but it could maybe break code that assumes unique __func__ values per function.

Supporting it properly would increase the complexity of pnut and because pnut's main use case is to bootstrap TCC/GCC, it's important to keep the code simple and short. This unfortunately means we often have to drop certain features that are too complex to implement properly and not useful for bootstrapping.

In an ideal world, pnut would be split in 2: a version for bootstrapping TCC/GCC that is kept as minimal as possible and a version for general use that aims to be C99 compliant with the bells and whistles of modern compilers.

Additionally, the preprocessor directives #file and #line can be used to override the values of FILE and LINE, that may or may not be something we want to add

Oh I didn't know about these directives. I'll add a comment but I don't think they will be supported by pnut in the near future.

@laurenthuberdeau laurenthuberdeau merged commit f13b91d into main Jan 3, 2025
31 checks passed
@laurenthuberdeau laurenthuberdeau deleted the laurent/support-c99-buildin-macros branch January 3, 2025 14:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants