Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

Add scoping for #define #51563

Open Quuxplusone opened 2 years ago

Quuxplusone commented 2 years ago
Bugzilla Link PR52596
Status NEW
Importance P enhancement
Reported by Karlo Miličević (karlo98.m@gmail.com)
Reported on 2021-11-24 02:59:33 -0800
Last modified on 2021-11-24 03:05:29 -0800
Version unspecified
Hardware All All
CC karlo98.m@gmail.com, llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
Fixed by commit(s)
Attachments
Blocks
Blocked by
See also
There is no way to have local "#define" in a header.
Best we can do is to define and undef at the end.
The problem with that is that there is no way of always picking unique names
for local defines.

That could be solved with something like preprocessor braces.
Something like '#{' and '#}' or something more verbose like '#pragma clang
context push' and '#pragma clang context pop'.

Aside for scoping definitions it could also scope other things like diagnostics
push/pop, optimizations, packing, etc.

Why I bother with something like this is not really because I'm writing widely
used library headers where that would be necessary, but rather because I use
macros extensively and it is bothersome to have to think about undefining
temporary macros everywhere.

#define _0 ...
#define _1 ...
... more defines and usage
#undef _1
#undef _0

Code above could then become:

#{
# define _0 ...
# define _1 ...
... more defines and usage
#}

And as I said, other scoping mechanisms in preprocessor could be made redundant.
e.g.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wsome-warning"
... some code
#pragma clang diagnostic pop

could become:

#{
# pragma clang diagnostic ignored "-Wsome-warning"
... some code
#}