<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </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 --- - undef doesn't work well with pch files"
   href="https://llvm.org/bugs/show_bug.cgi?id=31311">31311</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>undef doesn't work well with pch files
          </td>
        </tr>

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

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

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

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

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

        <tr>
          <th>Severity</th>
          <td>normal
          </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>nicolasweber@gmx.de
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>$ cat foo2.h
#undef ARG
$ cat foo2.c
const int i = ARG;
$ ./bin/clang -cc1 -DARG -include foo2.h foo2.c -emit-obj -o a.o
foo2.c:1:15: error: use of undeclared identifier 'ARG'
const int i = ARG;
              ^
1 error generated.
$ ./bin/clang -cc1 -emit-pch -o foo2.h.pch foo2.h
$ ./bin/clang -cc1 -DARG -include-pch foo2.h.pch foo2.c -emit-obj -o a.o


This should err when going through a pch as well.


Similar but slightly more difficult to fix: The same with a builtin macro:

$ cat foo2.h
#undef __FILE__
$ cat foo2.c
const char s[] = __FILE__;
s$ bin/clang -cc1 -include foo2.h foo2.c -emit-obj -o a.o
In file included from <built-in>:1:
./foo2.h:1:8: warning: undefining builtin macro
#undef __FILE__
       ^
foo2.c:1:18: error: use of undeclared identifier '__FILE__'
const char s[] = __FILE__;
                 ^
1 warning and 1 error generated.

$ ./bin/clang -cc1 -emit-pch -o foo2.h.pch foo2.h
foo2.h:1:8: warning: undefining builtin macro
#undef __FILE__
       ^
1 warning generated.
$ ./bin/clang -cc1 -include-pch foo2.h.pch foo2.c -emit-obj -o a.o


This too should error out but doesn't.


Problems, partial list:

1. shouldIgnoreMacro() in ASTWriter.cpp stops at isBuiltinMacro(), but that's
true for undefs of builtin macros as well. It probably should have `if
(MD->getKind() != MacroDirective::MD_Define) return false;` as its first line.

2. ASTWriter only writes out identifiers with a ref (see AddIdentifierRef).
ASTWriter::WritePreprocessor() only calls this for macros with a MacroInfo, and
macros that are only #undef'd but never defined (at pch creation time) get
dropped.

3. It seems that pch files get loaded before commandline -D flags are
processed, and Preprocessor::HandleUndefDirective() ignores undefs for macros
without a MacroInfo (which makes sense) (not really sure if the ordering here
is like I say, but if it weren't then `#define FOO` in the .h and -DFOO on the
command passing -include-pch would behave differently)

(Spin-off from <a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - Redefining __DATE__ or __TIME__ with pch causes Assertion failed: (!StoredMD.getLatest() && "the macro history was modified before initializing it from a pch")"
   href="show_bug.cgi?id=29119">bug 29119</a>)</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>