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

    <tr>
        <th>Summary</th>
        <td>
            [clang] PCH generation fails with pragma pack inside templated function
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

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

<pre>
    During pch generation/the compilation of a c++ header, a `pragma pack` inside a templated function will cause an assertion failure.

```
Assertion failed: !isAnnotation() && "Annotation tokens have no length field", file \antares\ephemeral\llvm-project\clang\include\clang/Lex/Token.h, line 129
```

### Repro File (`mr.hh`)

```c++
template<int v> // must be templated
void func()
{
#pragma pack(push, 1)
    // anything, nothing
#pragma pack(pop)
}
```

### Repro Steps

```sh-session
C:\> clang++ mr.hh
```

### Output

```
Assertion failed: !isAnnotation() && "Annotation tokens have no length field", file \antares\ephemeral\llvm-project\clang\include\clang/Lex/Token.h, line 129
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0. Program arguments: C:\\Users\\Dustin\\AppData\\Local\\Temp\\c\\bin\\clang-cl.exe mr.hh
1.      <eof> parser at end of file
Exception Code: 0x80000003
 #0 0x00007ff6d1f6c55c HandleAbort \\antares\ephemeral\llvm-project\llvm\lib\Support\Windows\Signals.inc:414:0
 #1 0x00007fff78a290ed (C:\WINDOWS\SYSTEM32\ucrtbased.dll+0xa90ed)
 #2 0x00007fff78a2ae49 (C:\WINDOWS\SYSTEM32\ucrtbased.dll+0xaae49)
 #3 0x00007fff78a31345 (C:\WINDOWS\SYSTEM32\ucrtbased.dll+0xb1345)
 #4 0x00007fff78a30bd7 (C:\WINDOWS\SYSTEM32\ucrtbased.dll+0xb0bd7)
 #5 0x00007fff78a2eba1 (C:\WINDOWS\SYSTEM32\ucrtbased.dll+0xaeba1)
 #6 0x00007fff78a318af (C:\WINDOWS\SYSTEM32\ucrtbased.dll+0xb18af)
 #7 0x00007ff6d39669c1 clang::Token::getLength(void) const \\antares\ephemeral\llvm-project\clang\include\clang\Lex\Token.h:129:0
 #8 0x00007ff6d3b1cc71 clang::ASTWriter::AddToken(class clang::Token const &, class llvm::SmallVectorImpl<unsigned __int64> &) \\antares\ephemeral\llvm-project\clang\lib\Serialization\ASTWriter.cpp:4378:0
 #9 0x00007ff6d3b1835b clang::ASTWriter::WriteLateParsedTemplates(class clang::Sema &) \\antares\ephemeral\llvm-project\clang\lib\Serialization\ASTWriter.cpp:4240:0
#10 0x00007ff6d3b1b250 clang::ASTWriter::WriteASTCore(class clang::Sema &, class llvm::StringRef, class std::basic_string<char, struct std::char_traits<char>, class std::allocator<char>> const &, class clang::Module *) \\antares\ephemeral\llvm-project\clang\lib\Serialization\ASTWriter.cpp:4911:0
#11 0x00007ff6d3b1ca85 clang::ASTWriter::WriteAST(class clang::Sema &, class std::basic_string<char, struct std::char_traits<char>, class std::allocator<char>> const &, class clang::Module *, class llvm::StringRef, bool, bool) \\antares\ephemeral\llvm-project\clang\lib\Serialization\ASTWriter.cpp:4506:0
#12 0x00007ff6d3af4edd clang::PCHGenerator::HandleTranslationUnit(class clang::ASTContext &) \\antares\ephemeral\llvm-project\clang\lib\Serialization\GeneratePCH.cpp:68:0
```

### Real-world applicability

Windows' `<TraceLoggingActivity.h>` emits code that looks like this. This minimal repro was generated from that case.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzcWFFv47gR_jX0y2ANibRk-8EPjpw0B-R6wdnXRZ8WFDWS2FCkQFJJtr--ICXHdjZ3uwm6QFHDsClqOJr5vuFwRtw52WjEDcmuSLab8cG3xm52t-YJvZ-Vpvq62Q1W6gZ60UKDGi330mhCb3yLIEzXSxVnwNTAQRB6RegVtMgrtIQWwIHkSW9503HouXggeQJSO1khcPDY9Yp7rKAetIhqnqRSIPjgELgG7hz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