[llvm-bugs] [Bug 52596] New: Add scoping for #define

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Nov 24 02:59:33 PST 2021


https://bugs.llvm.org/show_bug.cgi?id=52596

            Bug ID: 52596
           Summary: Add scoping for #define
           Product: clang
           Version: unspecified
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Frontend
          Assignee: unassignedclangbugs at nondot.org
          Reporter: karlo98.m at gmail.com
                CC: llvm-bugs at lists.llvm.org, neeilans at live.com,
                    richard-llvm at metafoo.co.uk

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
#}

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20211124/188d7fc2/attachment.html>


More information about the llvm-bugs mailing list