<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Add scoping for #define"
   href="https://bugs.llvm.org/show_bug.cgi?id=52596">52596</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Add scoping for #define
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Frontend
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>karlo98.m@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>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
#}</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>