<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/151476>151476</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            TableGen compiler points to non-existent line when source is just a single #ifdef (and crashes on one platform)
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            tablegen
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          DavidSpickett
      </td>
    </tr>
</table>

<pre>
    ```
#ifdef FOO
```
```
$ ./bin/llvm-tblgen /tmp/test.td
/tmp/test.td:2:1: error: reached EOF without matching #endif

^
/tmp/test.td:1:8: error: the latest preprocessor control is here
#ifdef FOO
       ^
/tmp/test.td:2:1: error: Unexpected token at top level

^
```
Clearly `test.td:2:1` does not exist, or is the actual EOF character. It should not be complaining about that as well as the lack of `#endif`.

Also, this currently crashes on Compiler Explorer https://godbolt.org/z/KvETG5Yah:
```
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: /opt/compiler-explorer/clang-trunk/bin/llvm-tblgen -o /app/output.s -I/opt/compiler-explorer/clang-trunk/include/ <source>
 #0 0x0000000000729758 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-trunk/bin/llvm-tblgen+0x729758)
 #1 0x0000000000726bd4 SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
 #2 0x00007947c3042520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #3 0x0000000000764cd0 llvm::TGLexer::prepSkipRegion(bool) (/opt/compiler-explorer/clang-trunk/bin/llvm-tblgen+0x764cd0)
 #4 0x0000000000763c29 llvm::TGLexer::lexPreprocessor(llvm::tgtok::TokKind, bool) (/opt/compiler-explorer/clang-trunk/bin/llvm-tblgen+0x763c29)
 #5 0x000000000076409d llvm::TGLexer::LexToken(bool) (/opt/compiler-explorer/clang-trunk/bin/llvm-tblgen+0x76409d)
 #6 0x000000000077d794 llvm::TGParser::ParseFile() (/opt/compiler-explorer/clang-trunk/bin/llvm-tblgen+0x77d794)
 #7 0x0000000000735cfc llvm::TableGenMain(char const*, std::function<bool (llvm::raw_ostream&, llvm::RecordKeeper const&)>) (/opt/compiler-explorer/clang-trunk/bin/llvm-tblgen+0x735cfc)
 #8 0x000000000044e5af tblgen_main(int, char**) (/opt/compiler-explorer/clang-trunk/bin/llvm-tblgen+0x44e5af)
 #9 0x00007947c3029d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#10 0x00007947c3029e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#11 0x000000000040b2b5 _start (/opt/compiler-explorer/clang-trunk/bin/llvm-tblgen+0x40b2b5)
Program terminated with signal: SIGSEGV
Compiler returned: 139
```
I could not get it to crash on x86 or AArch64 with asserts on or off or with ubsan or asan. Looking at prepSkipRegion, I suspect that the pointer to the current character is incremented multiple times during a loop body, but the `curPtr != end of text` is only being checked at the end or start.

Perhaps memory allocation on various systems hides this problem. Compiler Explorer's copy is running on x86 Linux.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJysV11v47oR_TX0yyCGRH3YevCDb26cBptig5ttgT4FFDmWeE2RAkllnf76YignjrN3UWwbYzeWJXLmnMPh8EiEoDuLuGHVb6z6fSGm2Du_-V08a_U4annAGBetUy8bVmenf9mW8ULvFe5h9_Ur_Xz_6GJYCUvGd622jO-MeR6uYms6tMD4Lg4j_cUQl1GlwR9uFVvOim3Oii2g987ThUche1Rw83UH33Xs3RRhEFH22nbAeIFW6T0Fy7asuvnrqBRxfRE19ghG0AgYPY7eSQzBeZDORu8M6AA9evyROMyfn6b6kcA_LB5HlBEVRHdACyJCdCMYfEZzAfydjtcGhTcvwOrsY-w6A-UwgHUR8KhDZPwanCfIxErIOAmT5JK98EJG9Eu4ixB6NxmVprUI0g2jEdqSiqIlUWMvIogA39EY-p4lkgdwe4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