llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[clangd] "EOF seen while discarding directive tokens" assertion failure in `Preprocessor::DiscardUntilEndOfDirective()` on malformed #if with unbalanced parenthesis

Open Moutigll opened this issue 2 months ago • 7 comments

clangd segfaults with a SIGSEGV crash while parsing a source file whose first line contains an #if directive with unbalanced/multi-line parentheses.

Note that clang -fsyntax-only/clang -E on the exact same file does NOT crash - it correctly emits a recoverable diagnostic:

test.c:1:18: error: expected ')' in preprocessor expression

Minimal reproducer:

#if (defined(FOO)
&& defined(BAR))

#endif

Reproduce with:

clangd --check=test.c

Output:

clangd --check=test.c --log=verbose                                            % 
I[17:25:17.541] clangd version 22.1.8
I[17:25:17.542] Features: linux
I[17:25:17.542] PID: 1342910
I[17:25:17.542] Working directory: /tmp/testclang
I[17:25:17.542] argv[0]: clangd
I[17:25:17.542] argv[1]: --check=test.c
I[17:25:17.542] argv[2]: --log=verbose
V[17:25:17.542] User config file is /home/***/.config/clangd/config.yaml
I[17:25:17.542] Entering check mode (no LSP server)
I[17:25:17.542] Testing on source file /tmp/testclang/test.c
V[17:25:17.542] Config fragment: compiling <unknown>:0 -> 0x00005BB64EDD1130 (trusted=false)
I[17:25:17.542] Loading compilation database...
I[17:25:17.544] Failed to find compilation database for /tmp/testclang/test.c
I[17:25:17.545] Generic fallback command is: [/tmp/testclang] /usr/bin/clang -resource-dir=/usr/lib/clang/22 -- /tmp/testclang/test.c
I[17:25:17.545] Parsing command...
I[17:25:17.551] internal (cc1) args are: -cc1 -triple x86_64-pc-linux-gnu -fsyntax-only -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name test.c -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/tmp/testclang -fcoverage-compilation-dir=/tmp/testclang -resource-dir /usr/lib/clang/22 -internal-isystem /usr/lib/clang/22/include -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/16/../../../../x86_64-pc-linux-gnu/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -ferror-limit 19 -fmessage-length=120 -stack-protector 2 -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -no-round-trip-args -faddrsig -fdwarf2-cfi-asm -x c /tmp/testclang/test.c
I[17:25:17.551] Building preamble...
I[17:25:17.558] Built preamble of size 241128 for file /tmp/testclang/test.c version null in 0.01 seconds
I[17:25:17.558] Indexing headers...
V[17:25:17.558] indexed preamble AST for /tmp/testclang/test.c version null:
  symbol slab: 0 symbols, 120 bytes
  ref slab: 0 symbols, 0 refs, 128 bytes
  relations slab: 0 relations, 24 bytes
E[17:25:17.558] [pp_expected_rparen] Line 2: expected ')' in preprocessor expression
I[17:25:17.558] Building AST...
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and instructions to reproduce the bug.
Stack dump:
0.	Program arguments: clangd --check=test.c --log=verbose
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  libLLVM.so.22.1      0x00007f73779aa297 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 71
1  libLLVM.so.22.1      0x00007f73779a7027
2  libc.so.6            0x00007f7376a236f0
3  libclang-cpp.so.22.1 0x00007f7381c674ba clang::Lexer::Lex(clang::Token&) + 26
4  libclang-cpp.so.22.1 0x00007f7381d2543c clang::Preprocessor::Lex(clang::Token&) + 60
5  libclang-cpp.so.22.1 0x00007f7381cc2336 clang::Preprocessor::DiscardUntilEndOfDirective(clang::Token&) + 118
6  libclang-cpp.so.22.1 0x00007f7381cdf038
7  libclang-cpp.so.22.1 0x00007f7381ce3b1d
8  libclang-cpp.so.22.1 0x00007f7381ce6a1c clang::Preprocessor::EvaluateDirectiveExpression(clang::IdentifierInfo*&, clang::Token&, bool&, bool) + 268
9  libclang-cpp.so.22.1 0x00007f7381ce75fe clang::Preprocessor::EvaluateDirectiveExpression(clang::IdentifierInfo*&, bool) + 46
10 libclang-cpp.so.22.1 0x00007f7381cd0eff clang::Preprocessor::HandleIfDirective(clang::Token&, clang::Token const&, bool) + 95
11 libclang-cpp.so.22.1 0x00007f7381cda566 clang::Preprocessor::HandleDirective(clang::Token&) + 1494
12 libclang-cpp.so.22.1 0x00007f7381c65b53 clang::Lexer::LexTokenInternal(clang::Token&, bool) + 3763
13 libclang-cpp.so.22.1 0x00007f7381d2543c clang::Preprocessor::Lex(clang::Token&) + 60
14 libclang-cpp.so.22.1 0x00007f738456556d clang::PreprocessOnlyAction::ExecuteAction() + 77
15 libclang-cpp.so.22.1 0x00007f7384556aeb clang::FrontendAction::Execute() + 59
16 clangd               0x00005bb64458a042
17 clangd               0x00005bb644590d7b
18 clangd               0x00005bb644593eb8
19 clangd               0x00005bb64457de0b
20 clangd               0x00005bb6442eedb4
21 clangd               0x00005bb6442debac
22 libc.so.6            0x00007f7376a0c781
23 libc.so.6            0x00007f7376a0c8b9 __libc_start_main + 137
24 clangd               0x00005bb64421f7d5

System information

Output of clangd --version:

clangd version 22.1.8
Features: linux
Platform: x86_64-pc-linux-gnu

Editor/LSP plugin: Reproduced directly via clangd --check=test.c on the command line, not through an editor/LSP client.

Operating system: Ubuntu 22.04.4 LTS

Note: This was originally reported to clangd at https://github.com/clangd/clangd/issues/2708#issue-5586286124, where the maintainer asked me to file it upstream because the crash is in clang's frontend libraries.

Moutigll avatar Sep 25 '26 18:09 Moutigll

@llvm/issue-subscribers-clangd

Author: Moutig (Moutigll)

clangd segfaults with a SIGSEGV crash while parsing a source file whose first line contains an #if directive with unbalanced/multi-line parentheses.

Note that clang -fsyntax-only/clang -E on the exact same file does NOT crash - it correctly emits a recoverable diagnostic:

test.c:1:18: error: expected ')' in preprocessor expression

Minimal reproducer:

#if (defined(FOO)
&amp;&amp; defined(BAR))

#endif

Reproduce with:

clangd --check=test.c

Output:

clangd --check=test.c --log=verbose                                            % 
I[17:25:17.541] clangd version 22.1.8
I[17:25:17.542] Features: linux
I[17:25:17.542] PID: 1342910
I[17:25:17.542] Working directory: /tmp/testclang
I[17:25:17.542] argv[0]: clangd
I[17:25:17.542] argv[1]: --check=test.c
I[17:25:17.542] argv[2]: --log=verbose
V[17:25:17.542] User config file is /home/***/.config/clangd/config.yaml
I[17:25:17.542] Entering check mode (no LSP server)
I[17:25:17.542] Testing on source file /tmp/testclang/test.c
V[17:25:17.542] Config fragment: compiling &lt;unknown&gt;:0 -&gt; 0x00005BB64EDD1130 (trusted=false)
I[17:25:17.542] Loading compilation database...
I[17:25:17.544] Failed to find compilation database for /tmp/testclang/test.c
I[17:25:17.545] Generic fallback command is: [/tmp/testclang] /usr/bin/clang -resource-dir=/usr/lib/clang/22 -- /tmp/testclang/test.c
I[17:25:17.545] Parsing command...
I[17:25:17.551] internal (cc1) args are: -cc1 -triple x86_64-pc-linux-gnu -fsyntax-only -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name test.c -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/tmp/testclang -fcoverage-compilation-dir=/tmp/testclang -resource-dir /usr/lib/clang/22 -internal-isystem /usr/lib/clang/22/include -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/16/../../../../x86_64-pc-linux-gnu/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -ferror-limit 19 -fmessage-length=120 -stack-protector 2 -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -no-round-trip-args -faddrsig -fdwarf2-cfi-asm -x c /tmp/testclang/test.c
I[17:25:17.551] Building preamble...
I[17:25:17.558] Built preamble of size 241128 for file /tmp/testclang/test.c version null in 0.01 seconds
I[17:25:17.558] Indexing headers...
V[17:25:17.558] indexed preamble AST for /tmp/testclang/test.c version null:
  symbol slab: 0 symbols, 120 bytes
  ref slab: 0 symbols, 0 refs, 128 bytes
  relations slab: 0 relations, 24 bytes
E[17:25:17.558] [pp_expected_rparen] Line 2: expected ')' in preprocessor expression
I[17:25:17.558] Building AST...
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and instructions to reproduce the bug.
Stack dump:
0.	Program arguments: clangd --check=test.c --log=verbose
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  libLLVM.so.22.1      0x00007f73779aa297 llvm::sys::PrintStackTrace(llvm::raw_ostream&amp;, int) + 71
1  libLLVM.so.22.1      0x00007f73779a7027
2  libc.so.6            0x00007f7376a236f0
3  libclang-cpp.so.22.1 0x00007f7381c674ba clang::Lexer::Lex(clang::Token&amp;) + 26
4  libclang-cpp.so.22.1 0x00007f7381d2543c clang::Preprocessor::Lex(clang::Token&amp;) + 60
5  libclang-cpp.so.22.1 0x00007f7381cc2336 clang::Preprocessor::DiscardUntilEndOfDirective(clang::Token&amp;) + 118
6  libclang-cpp.so.22.1 0x00007f7381cdf038
7  libclang-cpp.so.22.1 0x00007f7381ce3b1d
8  libclang-cpp.so.22.1 0x00007f7381ce6a1c clang::Preprocessor::EvaluateDirectiveExpression(clang::IdentifierInfo*&amp;, clang::Token&amp;, bool&amp;, bool) + 268
9  libclang-cpp.so.22.1 0x00007f7381ce75fe clang::Preprocessor::EvaluateDirectiveExpression(clang::IdentifierInfo*&amp;, bool) + 46
10 libclang-cpp.so.22.1 0x00007f7381cd0eff clang::Preprocessor::HandleIfDirective(clang::Token&amp;, clang::Token const&amp;, bool) + 95
11 libclang-cpp.so.22.1 0x00007f7381cda566 clang::Preprocessor::HandleDirective(clang::Token&amp;) + 1494
12 libclang-cpp.so.22.1 0x00007f7381c65b53 clang::Lexer::LexTokenInternal(clang::Token&amp;, bool) + 3763
13 libclang-cpp.so.22.1 0x00007f7381d2543c clang::Preprocessor::Lex(clang::Token&amp;) + 60
14 libclang-cpp.so.22.1 0x00007f738456556d clang::PreprocessOnlyAction::ExecuteAction() + 77
15 libclang-cpp.so.22.1 0x00007f7384556aeb clang::FrontendAction::Execute() + 59
16 clangd               0x00005bb64458a042
17 clangd               0x00005bb644590d7b
18 clangd               0x00005bb644593eb8
19 clangd               0x00005bb64457de0b
20 clangd               0x00005bb6442eedb4
21 clangd               0x00005bb6442debac
22 libc.so.6            0x00007f7376a0c781
23 libc.so.6            0x00007f7376a0c8b9 __libc_start_main + 137
24 clangd               0x00005bb64421f7d5

System information

Output of clangd --version:

clangd version 22.1.8
Features: linux
Platform: x86_64-pc-linux-gnu

Editor/LSP plugin: Reproduced directly via clangd --check=test.c on the command line, not through an editor/LSP client.

Operating system: Ubuntu 22.04.4 LTS

Note: This was originally reported to clangd at https://github.com/clangd/clangd/issues/2708#issue-5586286124, where the maintainer asked me to file it upstream because the crash is in clang's frontend libraries.

Here is a symbolicated stack trace from a debug build:

clangd-dev: clang/lib/Lex/PPDirectives.cpp:97: SourceRange clang::Preprocessor::DiscardUntilEndOfDirective(Token &, SmallVectorImpl<Token> *): Assertion `Tmp.isNot(tok::eof) && "EOF seen while discarding directive tokens"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and instructions to reproduce the bug.
Stack dump:
0.      Program arguments: clangd-dev --check=test.c
 #0 0x00007fc628977e59 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) llvm/lib/Support/Unix/Signals.inc:880:11
 #1 0x00007fc62897847b PrintStackTraceSignalHandler(void*) llvm/lib/Support/Unix/Signals.inc:962:1
 #2 0x00007fc628975e85 llvm::sys::RunSignalHandlers() llvm/lib/Support/Signals.cpp:108:5
 #3 0x00007fc628978c8e SignalHandler(int, siginfo_t*, void*) llvm/lib/Support/Unix/Signals.inc:448:38
 #4 0x00007fc627e3e6f0 (/usr/lib/libc.so.6+0x3e6f0)
 #5 0x00007fc627e9a17c (/usr/lib/libc.so.6+0x9a17c)
 #6 0x00007fc627e3e5d0 raise (/usr/lib/libc.so.6+0x3e5d0)
 #7 0x00007fc627e25685 abort (/usr/lib/libc.so.6+0x25685)
 #8 0x00007fc627e26704 (/usr/lib/libc.so.6+0x26704)
 #9 0x00007fc627e361f2 __assert_fail (/usr/lib/libc.so.6+0x361f2)
#10 0x00007fc629957734 clang::Preprocessor::DiscardUntilEndOfDirective(clang::Token&, llvm::SmallVectorImpl<clang::Token>*) clang/lib/Lex/PPDirectives.cpp:98:5
#11 0x00007fc62997f821 EvaluateDirectiveSubExpr((anonymous namespace)::PPValue&, unsigned int, clang::Token&, bool, bool&, clang::Preprocessor&) clang/lib/Lex/PPExpressions.cpp:603:8
#12 0x00007fc62997f0bb EvaluateValue((anonymous namespace)::PPValue&, clang::Token&, DefinedTracker&, bool, clang::Preprocessor&) clang/lib/Lex/PPExpressions.cpp:453:11
#13 0x00007fc62997dae7 clang::Preprocessor::EvaluateDirectiveExpression(clang::IdentifierInfo*&, clang::Token&, bool&, bool) clang/lib/Lex/PPExpressions.cpp:906:7
#14 0x00007fc62998112d clang::Preprocessor::EvaluateDirectiveExpression(clang::IdentifierInfo*&, bool) clang/lib/Lex/PPExpressions.cpp:981:3
#15 0x00007fc62995e74f clang::Preprocessor::HandleIfDirective(clang::Token&, clang::Token const&, bool) clang/lib/Lex/PPDirectives.cpp:3588:35
#16 0x00007fc62995d8b7 clang::Preprocessor::HandleDirective(clang::Token&) clang/lib/Lex/PPDirectives.cpp:1416:14
#17 0x00007fc6298cdcc2 clang::Lexer::LexTokenInternal(clang::Token&) clang/lib/Lex/Lexer.cpp:4591:7
#18 0x00007fc6298c8dfe clang::Lexer::Lex(clang::Token&) clang/lib/Lex/Lexer.cpp:3786:8
#19 0x00007fc629989b5c clang::Preprocessor::CLK_Lexer(clang::Preprocessor&, clang::Token&) clang/include/clang/Lex/Preprocessor.h:3149:5
#20 0x00007fc6299ce345 clang::Preprocessor::Lex(clang::Token&) clang/lib/Lex/Preprocessor.cpp:927:10
#21 0x00007fc62720942d clang::PreprocessOnlyAction::ExecuteAction() clang/lib/Frontend/FrontendActions.cpp:1063:16
#22 0x00007fc6271ef6a6 clang::FrontendAction::Execute() clang/lib/Frontend/FrontendAction.cpp:1346:7
#23 0x00007fc62ebbac3f clang::clangd::(anonymous namespace)::scanPreamble(llvm::StringRef, clang::tooling::CompileCommand const&, bool) clang-tools-extra/clangd/Preamble.cpp:376:19
#24 0x00007fc62ebb9b6d clang::clangd::PreamblePatch::create(llvm::StringRef, clang::clangd::ParseInputs const&, clang::clangd::PreambleData const&, clang::clangd::PreamblePatch::PatchType) clang-tools-extra/clangd/Preamble.cpp:794:8
#25 0x00007fc62ebbb603 clang::clangd::PreamblePatch::createFullPatch(llvm::StringRef, clang::clangd::ParseInputs const&, clang::clangd::PreambleData const&) clang-tools-extra/clangd/Preamble.cpp:903:3
#26 0x00007fc62eb6b669 clang::clangd::ParsedAST::build(llvm::StringRef, clang::clangd::ParseInputs const&, std::unique_ptr<clang::CompilerInvocation, std::default_delete<clang::CompilerInvocation>>, llvm::ArrayRef<clang::clangd::Diag>, std::shared_ptr<clang::clangd::PreambleData const>) clang-tools-extra/clangd/ParsedAST.cpp:463:11
#27 0x00007fc62f92fd4e clang::clangd::(anonymous namespace)::Checker::buildAST() clang-tools-extra/clangd/tool/Check.cpp:265:11
#28 0x00007fc62f92ecda clang::clangd::check(llvm::StringRef, clang::clangd::ThreadsafeFS const&, clang::clangd::ClangdLSPServer::Options const&) clang-tools-extra/clangd/tool/Check.cpp:525:7
#29 0x00007fc62f8eb323 clang::clangd::clangdMain(int, char**) clang-tools-extra/clangd/tool/ClangdMain.cpp:1038:12
#30 0x000055f31d907bd2 main clang-tools-extra/clangd/tool/ClangdToolMain.cpp:12:3
#31 0x00007fc627e27781 (/usr/lib/libc.so.6+0x27781)
#32 0x00007fc627e278b9 __libc_start_main (/usr/lib/libc.so.6+0x278b9)
#33 0x000055f31d533fe5 _start (build/bin/clangd+0x1cfe5)

HighCommander4 avatar Sep 25 '26 18:09 HighCommander4

I was able to reproduce this and traced it down to PPExpressions.cpp:604 (here)

PP.DiscardUntilEndOfDirective(PeekTok);

When checking the example source file, PeekTok is of kind tok::eod and this seems to be not allowed in the DiscardUntilEndOfDirective-Method:

SourceRange Preprocessor::DiscardUntilEndOfDirective(
    Token &Tmp, SmallVectorImpl<Token> *DiscardedToks) {
  SourceRange Res;
  auto ReadNextTok = [&]() {
    LexUnexpandedToken(Tmp);
    if (DiscardedToks && Tmp.isNot(tok::eod))
      DiscardedToks->push_back(Tmp);
  };
  ReadNextTok();
  Res.setBegin(Tmp.getLocation());
  while (Tmp.isNot(tok::eod)) {
    assert(Tmp.isNot(tok::eof) && "EOF seen while discarding directive tokens");
    ReadNextTok();
  }
  Res.setEnd(Tmp.getLocation());
  return Res;
}

With Tmp=PeekTok being of kind tok::eod, the ReadNextTok() function will set it after the first call to tok::eof which triggers the assertion.

Note also, that all other callers of Preprocessor::DiscardUntilEndOfDirective, e.g., PPExpressions.cpp:910 (here), see also line 949 and 964 check for the eod-case before:

    if (Tok.isNot(tok::eod))
      DiscardUntilEndOfDirective(Tok);

Adding the same guard in PPExpressions.cpp:604 fixes it for me and I get

I[23:43:22.604] Building preamble...
I[23:43:22.873] Built preamble of size 255048 for file .../test.cpp version null in 0.25 seconds
I[23:43:22.876] Indexing headers...
E[23:43:22.893] [pp_expected_rparen] Line 2: expected ')' in preprocessor expression
I[23:43:22.893] Building AST...
I[23:43:23.146] Indexing AST...
I[23:43:23.146] Building inlay hints
I[23:43:23.153] Building semantic highlighting
I[23:43:23.168] Testing features at each token (may be slow in large files)
I[23:43:23.204] All checks completed, 1 errors

Nevertheless, it is said that a ')' is missing in line 2. Shouldn't it be in line 1?

max-winkler avatar Sep 25 '26 21:09 max-winkler

Nice analysis — the line 2 location is expected. The pp_expected_rparen diagnostic is emitted when the expression parser reaches the end-of-directive token, and tok::eod is positioned at the line boundary, so clangd attributes it to line 2. The missing ) logically belongs to line 1, but the parser can only discover it at eod — which is also why the unguarded DiscardUntilEndOfDirective runs off the end. Moving the caret onto line 1 would be a separate diagnostics tweak; for the crash itself, the isNot(tok::eod) guard matching the other call sites is the right fix. I have a patch with that guard plus a regression test in flight.

iliasabk avatar Sep 25 '26 21:09 iliasabk

@iliasabk, is it OK for you if I open a PR? I investigated this bug a bit further and prepared a fix plus unit tests.

max-winkler avatar Oct 23 '26 12:10 max-winkler

Sure, go ahead — I'm not working on a fix myself. Your analysis looked right to me; feel free to open the PR and I'll take a look once it's up.

iliasabk avatar Oct 23 '26 12:10 iliasabk

@llvm/issue-subscribers-clang-frontend

Author: Moutig (Moutigll)

clangd segfaults with a SIGSEGV crash while parsing a source file whose first line contains an #if directive with unbalanced/multi-line parentheses.

Note that clang -fsyntax-only/clang -E on the exact same file does NOT crash - it correctly emits a recoverable diagnostic:

test.c:1:18: error: expected ')' in preprocessor expression

Minimal reproducer:

#if (defined(FOO)
&amp;&amp; defined(BAR))

#endif

Reproduce with:

clangd --check=test.c

Output:

clangd --check=test.c --log=verbose                                            % 
I[17:25:17.541] clangd version 22.1.8
I[17:25:17.542] Features: linux
I[17:25:17.542] PID: 1342910
I[17:25:17.542] Working directory: /tmp/testclang
I[17:25:17.542] argv[0]: clangd
I[17:25:17.542] argv[1]: --check=test.c
I[17:25:17.542] argv[2]: --log=verbose
V[17:25:17.542] User config file is /home/***/.config/clangd/config.yaml
I[17:25:17.542] Entering check mode (no LSP server)
I[17:25:17.542] Testing on source file /tmp/testclang/test.c
V[17:25:17.542] Config fragment: compiling &lt;unknown&gt;:0 -&gt; 0x00005BB64EDD1130 (trusted=false)
I[17:25:17.542] Loading compilation database...
I[17:25:17.544] Failed to find compilation database for /tmp/testclang/test.c
I[17:25:17.545] Generic fallback command is: [/tmp/testclang] /usr/bin/clang -resource-dir=/usr/lib/clang/22 -- /tmp/testclang/test.c
I[17:25:17.545] Parsing command...
I[17:25:17.551] internal (cc1) args are: -cc1 -triple x86_64-pc-linux-gnu -fsyntax-only -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name test.c -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/tmp/testclang -fcoverage-compilation-dir=/tmp/testclang -resource-dir /usr/lib/clang/22 -internal-isystem /usr/lib/clang/22/include -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/16/../../../../x86_64-pc-linux-gnu/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -ferror-limit 19 -fmessage-length=120 -stack-protector 2 -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -no-round-trip-args -faddrsig -fdwarf2-cfi-asm -x c /tmp/testclang/test.c
I[17:25:17.551] Building preamble...
I[17:25:17.558] Built preamble of size 241128 for file /tmp/testclang/test.c version null in 0.01 seconds
I[17:25:17.558] Indexing headers...
V[17:25:17.558] indexed preamble AST for /tmp/testclang/test.c version null:
  symbol slab: 0 symbols, 120 bytes
  ref slab: 0 symbols, 0 refs, 128 bytes
  relations slab: 0 relations, 24 bytes
E[17:25:17.558] [pp_expected_rparen] Line 2: expected ')' in preprocessor expression
I[17:25:17.558] Building AST...
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and instructions to reproduce the bug.
Stack dump:
0.	Program arguments: clangd --check=test.c --log=verbose
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  libLLVM.so.22.1      0x00007f73779aa297 llvm::sys::PrintStackTrace(llvm::raw_ostream&amp;, int) + 71
1  libLLVM.so.22.1      0x00007f73779a7027
2  libc.so.6            0x00007f7376a236f0
3  libclang-cpp.so.22.1 0x00007f7381c674ba clang::Lexer::Lex(clang::Token&amp;) + 26
4  libclang-cpp.so.22.1 0x00007f7381d2543c clang::Preprocessor::Lex(clang::Token&amp;) + 60
5  libclang-cpp.so.22.1 0x00007f7381cc2336 clang::Preprocessor::DiscardUntilEndOfDirective(clang::Token&amp;) + 118
6  libclang-cpp.so.22.1 0x00007f7381cdf038
7  libclang-cpp.so.22.1 0x00007f7381ce3b1d
8  libclang-cpp.so.22.1 0x00007f7381ce6a1c clang::Preprocessor::EvaluateDirectiveExpression(clang::IdentifierInfo*&amp;, clang::Token&amp;, bool&amp;, bool) + 268
9  libclang-cpp.so.22.1 0x00007f7381ce75fe clang::Preprocessor::EvaluateDirectiveExpression(clang::IdentifierInfo*&amp;, bool) + 46
10 libclang-cpp.so.22.1 0x00007f7381cd0eff clang::Preprocessor::HandleIfDirective(clang::Token&amp;, clang::Token const&amp;, bool) + 95
11 libclang-cpp.so.22.1 0x00007f7381cda566 clang::Preprocessor::HandleDirective(clang::Token&amp;) + 1494
12 libclang-cpp.so.22.1 0x00007f7381c65b53 clang::Lexer::LexTokenInternal(clang::Token&amp;, bool) + 3763
13 libclang-cpp.so.22.1 0x00007f7381d2543c clang::Preprocessor::Lex(clang::Token&amp;) + 60
14 libclang-cpp.so.22.1 0x00007f738456556d clang::PreprocessOnlyAction::ExecuteAction() + 77
15 libclang-cpp.so.22.1 0x00007f7384556aeb clang::FrontendAction::Execute() + 59
16 clangd               0x00005bb64458a042
17 clangd               0x00005bb644590d7b
18 clangd               0x00005bb644593eb8
19 clangd               0x00005bb64457de0b
20 clangd               0x00005bb6442eedb4
21 clangd               0x00005bb6442debac
22 libc.so.6            0x00007f7376a0c781
23 libc.so.6            0x00007f7376a0c8b9 __libc_start_main + 137
24 clangd               0x00005bb64421f7d5

System information

Output of clangd --version:

clangd version 22.1.8
Features: linux
Platform: x86_64-pc-linux-gnu

Editor/LSP plugin: Reproduced directly via clangd --check=test.c on the command line, not through an editor/LSP client.

Operating system: Ubuntu 22.04.4 LTS

Note: This was originally reported to clangd at https://github.com/clangd/clangd/issues/2708#issue-5586286124, where the maintainer asked me to file it upstream because the crash is in clang's frontend libraries.