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

clangd segfault during constant evaluation of #embed inside lambda initializer across headers

Open dargem opened this issue 4 months ago • 3 comments

clangd segfaults on valid code. The error happens when using #embed to embed a config file at compile time, then sort of parses them into an array at compile time. Then when another header file tries to access my loaded in configs clangd segfaults. Crash and minimal reproduction code below, can send original code also if wanted. Replacing #embed with just the file contents stops the crash from happening. If its the same header file and not a different one accessing the configs it doesn't crash also.

clangd -check=/home/tristan/Documents/github/static-engine/repro_dir/repro.hpp
I[12:56:56.875] clangd version 22.1.6
I[12:56:56.875] Features: linux
I[12:56:56.875] PID: 65003
I[12:56:56.875] Working directory: /home/tristan/Documents/github/static-engine/repro_dir
I[12:56:56.875] argv[0]: clangd
I[12:56:56.875] argv[1]: -check=/home/tristan/Documents/github/static-engine/repro_dir/repro.hpp
I[12:56:56.875] Entering check mode (no LSP server)
I[12:56:56.875] Testing on source file /home/tristan/Documents/github/static-engine/repro_dir/repro.hpp
I[12:56:56.875] Loading config file at /home/tristan/Documents/github/static-engine/.clangd
I[12:56:56.875] Loading compilation database...
I[12:56:56.875] Loaded compilation database from /home/tristan/Documents/github/static-engine/build/compile_commands.json
I[12:56:56.876] Compile command inferred from /home/tristan/Documents/github/static-engine/engine/src/main.cpp is: [/home/tristan/Documents/github/static-engine/build] /usr/bin/clang++ --driver-mode=g++ "-DCONFIGS_FILE_PATH=\"/home/tristan/Documents/github/static-engine/engine/resources/configs.txt\"" -I/home/tristan/Documents/github/static-engine/engine/src -I/home/tristan/Documents/github/static-engine/engine/include -I/home/tristan/Documents/github/static-engine/engine -Wall -Wfloat-conversion -c -std=gnu++23 -std=c++23 -Wall -Wextra -resource-dir=/usr/lib/clang/22 -- /home/tristan/Documents/github/static-engine/repro_dir/repro.hpp
I[12:56:56.876] Parsing command...
I[12:56:56.876] 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 repro.hpp -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=/home/tristan/Documents/github/static-engine/build -fcoverage-compilation-dir=/home/tristan/Documents/github/static-engine/build -resource-dir /usr/lib/clang/22 -D "CONFIGS_FILE_PATH=\"/home/tristan/Documents/github/static-engine/engine/resources/configs.txt\"" -I /home/tristan/Documents/github/static-engine/engine/src -I /home/tristan/Documents/github/static-engine/engine/include -I /home/tristan/Documents/github/static-engine/engine -internal-isystem /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/16/../../../../include/c++/16 -internal-isystem /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/16/../../../../include/c++/16/x86_64-pc-linux-gnu -internal-isystem /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/16/../../../../include/c++/16/backward -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 -Wall -Wfloat-conversion -Wall -Wextra -std=c++23 -fdeprecated-macro -ferror-limit 19 -fmessage-length=132 -stack-protector 2 -fgnuc-version=4.2.1 -fno-implicit-modules -fskip-odr-check-in-gmf -fcxx-exceptions -fexceptions -no-round-trip-args -faddrsig -fdwarf2-cfi-asm -x c++-header /home/tristan/Documents/github/static-engine/repro_dir/repro.hpp
I[12:56:56.876] Building preamble...
I[12:56:57.399] Built preamble of size 9701484 for file /home/tristan/Documents/github/static-engine/repro_dir/repro.hpp version null in 0.52 seconds
I[12:56:57.399] Indexing headers...
I[12:56:57.475] 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=/home/tristan/Documents/github/static-engine/repro_dir/repro.hpp
1.      /home/tristan/Documents/github/static-engine/repro_dir/repro.hpp:7:50: current parser token ')'
2.      /home/tristan/Documents/github/static-engine/repro_dir/repro.hpp:6:44: lambda expression parsing
3.      /home/tristan/Documents/github/static-engine/repro_dir/repro.hpp:6:59: in compound statement ('{}')
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      0x00007f9d53b98a97 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 71
1  libLLVM.so.22.1      0x00007f9d53b95827
2  libc.so.6            0x00007f9d52a3e270
3  libclang-cpp.so.22.1 0x00007f9d5e5f1c56 clang::Expr::isUnusedResultAWarning(clang::Expr const*&, clang::SourceLocation&, clang::SourceRange&, clang::SourceRange&, clang::ASTContext&) const + 214
4  libclang-cpp.so.22.1 0x00007f9d5e653ee3
5  libclang-cpp.so.22.1 0x00007f9d5e6a0ab8
6  libclang-cpp.so.22.1 0x00007f9d5e639305
7  libclang-cpp.so.22.1 0x00007f9d5e665235
8  libclang-cpp.so.22.1 0x00007f9d5e6ab895
9  libclang-cpp.so.22.1 0x00007f9d5e6648ad
10 libclang-cpp.so.22.1 0x00007f9d5e6b18f8 clang::Expr::EvaluateAsInitializer(clang::APValue&, clang::ASTContext const&, clang::VarDecl const*, llvm::SmallVectorImpl<std::pair<clang::SourceLocation, clang::PartialDiagnostic>>&, bool) const + 1976
11 libclang-cpp.so.22.1 0x00007f9d5e48c2e7 clang::VarDecl::evaluateValueImpl(llvm::SmallVectorImpl<std::pair<clang::SourceLocation, clang::PartialDiagnostic>>&, bool) const + 199
12 libclang-cpp.so.22.1 0x00007f9d5e48c5a1 clang::VarDecl::evaluateValue() const + 97
13 libclang-cpp.so.22.1 0x00007f9d5e60e809
14 libclang-cpp.so.22.1 0x00007f9d5e61bc90
15 libclang-cpp.so.22.1 0x00007f9d5e62f445
16 libclang-cpp.so.22.1 0x00007f9d5e6a0876
17 libclang-cpp.so.22.1 0x00007f9d5e6a0ab8
18 libclang-cpp.so.22.1 0x00007f9d5e639305
19 libclang-cpp.so.22.1 0x00007f9d5e6c04d1
20 libclang-cpp.so.22.1 0x00007f9d5e639305
21 libclang-cpp.so.22.1 0x00007f9d5e641e90
22 libclang-cpp.so.22.1 0x00007f9d5e669a7e
23 libclang-cpp.so.22.1 0x00007f9d5e66c263
24 libclang-cpp.so.22.1 0x00007f9d5e66a958
25 libclang-cpp.so.22.1 0x00007f9d5e670c02
26 libclang-cpp.so.22.1 0x00007f9d5e66c0d9
27 libclang-cpp.so.22.1 0x00007f9d5e66a958
28 libclang-cpp.so.22.1 0x00007f9d5e672d46
29 libclang-cpp.so.22.1 0x00007f9d5e67796a
30 libclang-cpp.so.22.1 0x00007f9d5e6a600f
31 libclang-cpp.so.22.1 0x00007f9d5e664988
32 libclang-cpp.so.22.1 0x00007f9d5e66910e
33 libclang-cpp.so.22.1 0x00007f9d5e6696c5
34 libclang-cpp.so.22.1 0x00007f9d5e66a893
35 libclang-cpp.so.22.1 0x00007f9d5e66a958
36 libclang-cpp.so.22.1 0x00007f9d5e672d46
37 libclang-cpp.so.22.1 0x00007f9d5e67796a
38 libclang-cpp.so.22.1 0x00007f9d5e6a600f
39 libclang-cpp.so.22.1 0x00007f9d5e6a600f
40 libclang-cpp.so.22.1 0x00007f9d5e664988
41 libclang-cpp.so.22.1 0x00007f9d5e6b18f8 clang::Expr::EvaluateAsInitializer(clang::APValue&, clang::ASTContext const&, clang::VarDecl const*, llvm::SmallVectorImpl<std::pair<clang::SourceLocation, clang::PartialDiagnostic>>&, bool) const + 1976
42 libclang-cpp.so.22.1 0x00007f9d5e48c2e7 clang::VarDecl::evaluateValueImpl(llvm::SmallVectorImpl<std::pair<clang::SourceLocation, clang::PartialDiagnostic>>&, bool) const + 199
43 libclang-cpp.so.22.1 0x00007f9d5e48c5a1 clang::VarDecl::evaluateValue() const + 97
44 libclang-cpp.so.22.1 0x00007f9d5e60e809
45 libclang-cpp.so.22.1 0x00007f9d5e61bc90
46 libclang-cpp.so.22.1 0x00007f9d5e62b508
47 libclang-cpp.so.22.1 0x00007f9d5e67b9ef
48 libclang-cpp.so.22.1 0x00007f9d5e6bfd48
49 libclang-cpp.so.22.1 0x00007f9d5e6392b3
50 libclang-cpp.so.22.1 0x00007f9d5e66910e
51 libclang-cpp.so.22.1 0x00007f9d5e6696c5
52 libclang-cpp.so.22.1 0x00007f9d5e66a893
53 libclang-cpp.so.22.1 0x00007f9d5e66bf4d
54 libclang-cpp.so.22.1 0x00007f9d5e66a958
55 libclang-cpp.so.22.1 0x00007f9d5e672d46
56 libclang-cpp.so.22.1 0x00007f9d5e67796a
57 libclang-cpp.so.22.1 0x00007f9d5e664988
58 libclang-cpp.so.22.1 0x00007f9d5e6735e5 clang::Expr::EvaluateAsConstantExpr(clang::Expr::EvalResult&, clang::ASTContext const&, clang::Expr::ConstantExprKind) const + 1285
59 libclang-cpp.so.22.1 0x00007f9d5f2441a9 clang::Sema::CheckForImmediateInvocation(clang::ActionResult<clang::Expr*, true>, clang::FunctionDecl*) + 1145
60 libclang-cpp.so.22.1 0x00007f9d5f673693 clang::Sema::BuildOverloadedCallExpr(clang::Scope*, clang::Expr*, clang::UnresolvedLookupExpr*, clang::SourceLocation, llvm::MutableArrayRef<clang::Expr*>, clang::SourceLocation, clang::Expr*, bool, bool) + 1107
61 libclang-cpp.so.22.1 0x00007f9d5f29bfcf clang::Sema::BuildCallExpr(clang::Scope*, clang::Expr*, clang::SourceLocation, llvm::MutableArrayRef<clang::Expr*>, clang::SourceLocation, clang::Expr*, bool, bool) + 3951
62 libclang-cpp.so.22.1 0x00007f9d5f29cdb1 clang::Sema::ActOnCallExpr(clang::Scope*, clang::Expr*, clang::SourceLocation, llvm::MutableArrayRef<clang::Expr*>, clang::SourceLocation, clang::Expr*) + 81
63 libclang-cpp.so.22.1 0x00007f9d5e1478a9 clang::Parser::ParsePostfixExpressionSuffix(clang::ActionResult<clang::Expr*, true>) + 3369
64 libclang-cpp.so.22.1 0x00007f9d5e13f9b2 clang::Parser::ParseCastExpression(clang::CastParseKind, bool, bool&, clang::TypoCorrectionTypeBehavior, bool, bool*) + 690
65 libclang-cpp.so.22.1 0x00007f9d5e140bf9 clang::Parser::ParseCastExpression(clang::CastParseKind, bool, bool&, clang::TypoCorrectionTypeBehavior, bool, bool*) + 5369
66 libclang-cpp.so.22.1 0x00007f9d5e1424cb
67 libclang-cpp.so.22.1 0x00007f9d5e14695c clang::Parser::ParseAssignmentExpression(clang::TypoCorrectionTypeBehavior) + 60
68 libclang-cpp.so.22.1 0x00007f9d5e184722 clang::Parser::ParseInitializer(clang::Decl*) + 258
69 libclang-cpp.so.22.1 0x00007f9d5e0f2e51 clang::Parser::ParseDeclarationAfterDeclaratorAndAttributes(clang::Declarator&, clang::Parser::ParsedTemplateInfo const&, clang::Parser::ForRangeInit*) + 497
70 libclang-cpp.so.22.1 0x00007f9d5e1094c7 clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&, clang::DeclaratorContext, clang::ParsedAttributes&, clang::Parser::ParsedTemplateInfo&, clang::SourceLocation*, clang::Parser::ForRangeInit*) + 3879
71 libclang-cpp.so.22.1 0x00007f9d5e10ae77 clang::Parser::ParseSimpleDeclaration(clang::DeclaratorContext, clang::SourceLocation&, clang::ParsedAttributes&, clang::ParsedAttributes&, bool, clang::Parser::ForRangeInit*, clang::SourceLocation*) + 807
72 libclang-cpp.so.22.1 0x00007f9d5e10b360 clang::Parser::ParseDeclaration(clang::DeclaratorContext, clang::SourceLocation&, clang::ParsedAttributes&, clang::ParsedAttributes&, clang::SourceLocation*) + 256
73 libclang-cpp.so.22.1 0x00007f9d5e1eaeee clang::Parser::ParseStatementOrDeclarationAfterAttributes(llvm::SmallVector<clang::Stmt*, 24u>&, clang::Parser::ParsedStmtContext, clang::SourceLocation*, clang::ParsedAttributes&, clang::ParsedAttributes&, clang::LabelDecl*) + 6798
74 libclang-cpp.so.22.1 0x00007f9d5e1eb9e6 clang::Parser::ParseStatementOrDeclaration(llvm::SmallVector<clang::Stmt*, 24u>&, clang::Parser::ParsedStmtContext, clang::SourceLocation*, clang::LabelDecl*) + 390
75 libclang-cpp.so.22.1 0x00007f9d5e1ecfd2 clang::Parser::ParseCompoundStatementBody(bool) + 1250
76 libclang-cpp.so.22.1 0x00007f9d5e163d93 clang::Parser::ParseLambdaExpressionAfterIntroducer(clang::LambdaIntroducer&) + 4307
77 libclang-cpp.so.22.1 0x00007f9d5e16560f clang::Parser::ParseLambdaExpression() + 287
78 libclang-cpp.so.22.1 0x00007f9d5e141c86 clang::Parser::ParseCastExpression(clang::CastParseKind, bool, bool&, clang::TypoCorrectionTypeBehavior, bool, bool*) + 9606
79 libclang-cpp.so.22.1 0x00007f9d5e1424cb
80 libclang-cpp.so.22.1 0x00007f9d5e14695c clang::Parser::ParseAssignmentExpression(clang::TypoCorrectionTypeBehavior) + 60
81 libclang-cpp.so.22.1 0x00007f9d5e184722 clang::Parser::ParseInitializer(clang::Decl*) + 258
82 libclang-cpp.so.22.1 0x00007f9d5e0f2e51 clang::Parser::ParseDeclarationAfterDeclaratorAndAttributes(clang::Declarator&, clang::Parser::ParsedTemplateInfo const&, clang::Parser::ForRangeInit*) + 497
83 libclang-cpp.so.22.1 0x00007f9d5e1094c7 clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&, clang::DeclaratorContext, clang::ParsedAttributes&, clang::Parser::ParsedTemplateInfo&, clang::SourceLocation*, clang::Parser::ForRangeInit*) + 3879
84 libclang-cpp.so.22.1 0x00007f9d5e21f6e8 clang::Parser::ParseDeclOrFunctionDefInternal(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec&, clang::AccessSpecifier) + 1016
85 libclang-cpp.so.22.1 0x00007f9d5e21ff0d clang::Parser::ParseDeclarationOrFunctionDefinition(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*, clang::AccessSpecifier) + 957
86 libclang-cpp.so.22.1 0x00007f9d5e229502 clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*) + 642
87 libclang-cpp.so.22.1 0x00007f9d5e22a97c clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) + 540
88 libclang-cpp.so.22.1 0x00007f9d5e0d0ceb clang::ParseAST(clang::Sema&, bool, bool) + 587
89 libclang-cpp.so.22.1 0x00007f9d608f8aeb clang::FrontendAction::Execute() + 59
90 clangd               0x000055e6dcc2b173
91 clangd               0x000055e6dc999db4
92 clangd               0x000055e6dc989bac
93 libc.so.6            0x00007f9d52a27741
94 libc.so.6            0x00007f9d52a27879 __libc_start_main + 137
95 clangd               0x000055e6dc8ca7d5
Segmentation fault         (core dumped) clangd -check=/home/tristan/Documents/github/static-engine/repro_dir/repro.hpp

Code for reproduction, the error only happens across headers.

// config.hpp
#pragma once
#include <algorithm>
#include <array>
#include <ranges>
#include <string_view>
#include <vector>

template <size_t N> struct CompString {
  constexpr CompString(const char (&arr)[N]) {
    std::copy_n(arr, N, data.data());
  }
  constexpr CompString(std::array<char, N> arr) {
    std::copy_n(arr.data(), N, data.data());
  }
  consteval auto view() const { return std::string_view(data.data(), N - 1); }
  std::array<char, N> data;
};

namespace detail {
constexpr char RAW_KV_CONFIG[] = {
#embed "config.txt"
    , '\0'};

constexpr std::string_view VIEW_KV_CONFIGS{RAW_KV_CONFIG,
                                           sizeof(RAW_KV_CONFIG) - 1};

struct KeyValueStringPair {
  std::string_view key;
  std::string_view value;
};
} // namespace detail

consteval auto split(std::string_view s, char delimiter)
    -> std::vector<std::string_view> {
  std::vector<std::string_view> separated;
  size_t last_split{};
  for (auto [index, letter] : std::views::enumerate(s)) {
    if (letter == delimiter) {
      if (index - last_split != 0) {
        separated.push_back(s.substr(last_split, index - last_split));
      }
      last_split = index + 1;
    } else if (index + 1 == s.size()) {
      if (index - last_split + 1 != 0) {
        separated.push_back(s.substr(last_split, index - last_split + 1));
      }
    }
  }
  return separated;
}

consteval auto sanitize(std::string_view s, char discarded = ' ')
    -> std::string_view {
  size_t start{};
  size_t end{s.size()};
  for (; start < end && s[start] == discarded; ++start);
  for (; start < end && s[end - 1] == discarded; --end);
  return s.substr(start, end - start);
}

constexpr size_t NUM_CONFIGS = std::count(detail::VIEW_KV_CONFIGS.begin(),
                                          detail::VIEW_KV_CONFIGS.end(), '\n') +
                               1;

constexpr auto KV_STRING_PAIRS =
    [] -> std::array<detail::KeyValueStringPair, NUM_CONFIGS> {
  std::array<detail::KeyValueStringPair, NUM_CONFIGS> configs;
  auto lines = split(detail::VIEW_KV_CONFIGS, '\n');
  for (auto [index, line] : std::views::enumerate(lines)) {
    auto pair = split(line, ':');
    if (pair.size() != 2) {
      throw "Invalid config pair";
    }
    configs[index] =
        detail::KeyValueStringPair(sanitize(pair[0]), sanitize(pair[1]));
  }
  return configs;
}();

template <CompString S> constexpr auto get_config() -> std::string_view {
  for (auto [key, value] : KV_STRING_PAIRS) {
    if (key == S.view())
      return sanitize(value);
  }
  throw "Key not found";
}
// repro.hpp
#pragma once
#include "config.hpp"

enum class LogLevel { TRACE, DEBUG, INFO, WARN, ERROR, FATAL };

static constexpr LogLevel KEPT_LOG_LEVEL = [] -> LogLevel {
  constexpr auto level = get_config<"log_level">();
  if (level == "INFO")
    return LogLevel::INFO;
  return LogLevel::ERROR;
}();

config.txt

log_level: INFO

dargem avatar Jul 21 '26 03:07 dargem

@llvm/issue-subscribers-clangd

Author: tristan (dargem)

clangd segfaults on valid code. The error happens when using #embed to embed a config file at compile time, then sort of parses them into an array at compile time. Then when another header file tries to access my loaded in configs clangd segfaults. Crash and minimal reproduction code below, can send original code also if wanted. Replacing #embed with just the file contents stops the crash from happening. If its the same header file and not a different one accessing the configs it doesn't crash also.
clangd -check=/home/tristan/Documents/github/static-engine/repro_dir/repro.hpp
I[12:56:56.875] clangd version 22.1.6
I[12:56:56.875] Features: linux
I[12:56:56.875] PID: 65003
I[12:56:56.875] Working directory: /home/tristan/Documents/github/static-engine/repro_dir
I[12:56:56.875] argv[0]: clangd
I[12:56:56.875] argv[1]: -check=/home/tristan/Documents/github/static-engine/repro_dir/repro.hpp
I[12:56:56.875] Entering check mode (no LSP server)
I[12:56:56.875] Testing on source file /home/tristan/Documents/github/static-engine/repro_dir/repro.hpp
I[12:56:56.875] Loading config file at /home/tristan/Documents/github/static-engine/.clangd
I[12:56:56.875] Loading compilation database...
I[12:56:56.875] Loaded compilation database from /home/tristan/Documents/github/static-engine/build/compile_commands.json
I[12:56:56.876] Compile command inferred from /home/tristan/Documents/github/static-engine/engine/src/main.cpp is: [/home/tristan/Documents/github/static-engine/build] /usr/bin/clang++ --driver-mode=g++ "-DCONFIGS_FILE_PATH=\"/home/tristan/Documents/github/static-engine/engine/resources/configs.txt\"" -I/home/tristan/Documents/github/static-engine/engine/src -I/home/tristan/Documents/github/static-engine/engine/include -I/home/tristan/Documents/github/static-engine/engine -Wall -Wfloat-conversion -c -std=gnu++23 -std=c++23 -Wall -Wextra -resource-dir=/usr/lib/clang/22 -- /home/tristan/Documents/github/static-engine/repro_dir/repro.hpp
I[12:56:56.876] Parsing command...
I[12:56:56.876] 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 repro.hpp -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=/home/tristan/Documents/github/static-engine/build -fcoverage-compilation-dir=/home/tristan/Documents/github/static-engine/build -resource-dir /usr/lib/clang/22 -D "CONFIGS_FILE_PATH=\"/home/tristan/Documents/github/static-engine/engine/resources/configs.txt\"" -I /home/tristan/Documents/github/static-engine/engine/src -I /home/tristan/Documents/github/static-engine/engine/include -I /home/tristan/Documents/github/static-engine/engine -internal-isystem /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/16/../../../../include/c++/16 -internal-isystem /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/16/../../../../include/c++/16/x86_64-pc-linux-gnu -internal-isystem /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/16/../../../../include/c++/16/backward -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 -Wall -Wfloat-conversion -Wall -Wextra -std=c++23 -fdeprecated-macro -ferror-limit 19 -fmessage-length=132 -stack-protector 2 -fgnuc-version=4.2.1 -fno-implicit-modules -fskip-odr-check-in-gmf -fcxx-exceptions -fexceptions -no-round-trip-args -faddrsig -fdwarf2-cfi-asm -x c++-header /home/tristan/Documents/github/static-engine/repro_dir/repro.hpp
I[12:56:56.876] Building preamble...
I[12:56:57.399] Built preamble of size 9701484 for file /home/tristan/Documents/github/static-engine/repro_dir/repro.hpp version null in 0.52 seconds
I[12:56:57.399] Indexing headers...
I[12:56:57.475] 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=/home/tristan/Documents/github/static-engine/repro_dir/repro.hpp
1.      /home/tristan/Documents/github/static-engine/repro_dir/repro.hpp:7:50: current parser token ')'
2.      /home/tristan/Documents/github/static-engine/repro_dir/repro.hpp:6:44: lambda expression parsing
3.      /home/tristan/Documents/github/static-engine/repro_dir/repro.hpp:6:59: in compound statement ('{}')
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      0x00007f9d53b98a97 llvm::sys::PrintStackTrace(llvm::raw_ostream&amp;, int) + 71
1  libLLVM.so.22.1      0x00007f9d53b95827
2  libc.so.6            0x00007f9d52a3e270
3  libclang-cpp.so.22.1 0x00007f9d5e5f1c56 clang::Expr::isUnusedResultAWarning(clang::Expr const*&amp;, clang::SourceLocation&amp;, clang::SourceRange&amp;, clang::SourceRange&amp;, clang::ASTContext&amp;) const + 214
4  libclang-cpp.so.22.1 0x00007f9d5e653ee3
5  libclang-cpp.so.22.1 0x00007f9d5e6a0ab8
6  libclang-cpp.so.22.1 0x00007f9d5e639305
7  libclang-cpp.so.22.1 0x00007f9d5e665235
8  libclang-cpp.so.22.1 0x00007f9d5e6ab895
9  libclang-cpp.so.22.1 0x00007f9d5e6648ad
10 libclang-cpp.so.22.1 0x00007f9d5e6b18f8 clang::Expr::EvaluateAsInitializer(clang::APValue&amp;, clang::ASTContext const&amp;, clang::VarDecl const*, llvm::SmallVectorImpl&lt;std::pair&lt;clang::SourceLocation, clang::PartialDiagnostic&gt;&gt;&amp;, bool) const + 1976
11 libclang-cpp.so.22.1 0x00007f9d5e48c2e7 clang::VarDecl::evaluateValueImpl(llvm::SmallVectorImpl&lt;std::pair&lt;clang::SourceLocation, clang::PartialDiagnostic&gt;&gt;&amp;, bool) const + 199
12 libclang-cpp.so.22.1 0x00007f9d5e48c5a1 clang::VarDecl::evaluateValue() const + 97
13 libclang-cpp.so.22.1 0x00007f9d5e60e809
14 libclang-cpp.so.22.1 0x00007f9d5e61bc90
15 libclang-cpp.so.22.1 0x00007f9d5e62f445
16 libclang-cpp.so.22.1 0x00007f9d5e6a0876
17 libclang-cpp.so.22.1 0x00007f9d5e6a0ab8
18 libclang-cpp.so.22.1 0x00007f9d5e639305
19 libclang-cpp.so.22.1 0x00007f9d5e6c04d1
20 libclang-cpp.so.22.1 0x00007f9d5e639305
21 libclang-cpp.so.22.1 0x00007f9d5e641e90
22 libclang-cpp.so.22.1 0x00007f9d5e669a7e
23 libclang-cpp.so.22.1 0x00007f9d5e66c263
24 libclang-cpp.so.22.1 0x00007f9d5e66a958
25 libclang-cpp.so.22.1 0x00007f9d5e670c02
26 libclang-cpp.so.22.1 0x00007f9d5e66c0d9
27 libclang-cpp.so.22.1 0x00007f9d5e66a958
28 libclang-cpp.so.22.1 0x00007f9d5e672d46
29 libclang-cpp.so.22.1 0x00007f9d5e67796a
30 libclang-cpp.so.22.1 0x00007f9d5e6a600f
31 libclang-cpp.so.22.1 0x00007f9d5e664988
32 libclang-cpp.so.22.1 0x00007f9d5e66910e
33 libclang-cpp.so.22.1 0x00007f9d5e6696c5
34 libclang-cpp.so.22.1 0x00007f9d5e66a893
35 libclang-cpp.so.22.1 0x00007f9d5e66a958
36 libclang-cpp.so.22.1 0x00007f9d5e672d46
37 libclang-cpp.so.22.1 0x00007f9d5e67796a
38 libclang-cpp.so.22.1 0x00007f9d5e6a600f
39 libclang-cpp.so.22.1 0x00007f9d5e6a600f
40 libclang-cpp.so.22.1 0x00007f9d5e664988
41 libclang-cpp.so.22.1 0x00007f9d5e6b18f8 clang::Expr::EvaluateAsInitializer(clang::APValue&amp;, clang::ASTContext const&amp;, clang::VarDecl const*, llvm::SmallVectorImpl&lt;std::pair&lt;clang::SourceLocation, clang::PartialDiagnostic&gt;&gt;&amp;, bool) const + 1976
42 libclang-cpp.so.22.1 0x00007f9d5e48c2e7 clang::VarDecl::evaluateValueImpl(llvm::SmallVectorImpl&lt;std::pair&lt;clang::SourceLocation, clang::PartialDiagnostic&gt;&gt;&amp;, bool) const + 199
43 libclang-cpp.so.22.1 0x00007f9d5e48c5a1 clang::VarDecl::evaluateValue() const + 97
44 libclang-cpp.so.22.1 0x00007f9d5e60e809
45 libclang-cpp.so.22.1 0x00007f9d5e61bc90
46 libclang-cpp.so.22.1 0x00007f9d5e62b508
47 libclang-cpp.so.22.1 0x00007f9d5e67b9ef
48 libclang-cpp.so.22.1 0x00007f9d5e6bfd48
49 libclang-cpp.so.22.1 0x00007f9d5e6392b3
50 libclang-cpp.so.22.1 0x00007f9d5e66910e
51 libclang-cpp.so.22.1 0x00007f9d5e6696c5
52 libclang-cpp.so.22.1 0x00007f9d5e66a893
53 libclang-cpp.so.22.1 0x00007f9d5e66bf4d
54 libclang-cpp.so.22.1 0x00007f9d5e66a958
55 libclang-cpp.so.22.1 0x00007f9d5e672d46
56 libclang-cpp.so.22.1 0x00007f9d5e67796a
57 libclang-cpp.so.22.1 0x00007f9d5e664988
58 libclang-cpp.so.22.1 0x00007f9d5e6735e5 clang::Expr::EvaluateAsConstantExpr(clang::Expr::EvalResult&amp;, clang::ASTContext const&amp;, clang::Expr::ConstantExprKind) const + 1285
59 libclang-cpp.so.22.1 0x00007f9d5f2441a9 clang::Sema::CheckForImmediateInvocation(clang::ActionResult&lt;clang::Expr*, true&gt;, clang::FunctionDecl*) + 1145
60 libclang-cpp.so.22.1 0x00007f9d5f673693 clang::Sema::BuildOverloadedCallExpr(clang::Scope*, clang::Expr*, clang::UnresolvedLookupExpr*, clang::SourceLocation, llvm::MutableArrayRef&lt;clang::Expr*&gt;, clang::SourceLocation, clang::Expr*, bool, bool) + 1107
61 libclang-cpp.so.22.1 0x00007f9d5f29bfcf clang::Sema::BuildCallExpr(clang::Scope*, clang::Expr*, clang::SourceLocation, llvm::MutableArrayRef&lt;clang::Expr*&gt;, clang::SourceLocation, clang::Expr*, bool, bool) + 3951
62 libclang-cpp.so.22.1 0x00007f9d5f29cdb1 clang::Sema::ActOnCallExpr(clang::Scope*, clang::Expr*, clang::SourceLocation, llvm::MutableArrayRef&lt;clang::Expr*&gt;, clang::SourceLocation, clang::Expr*) + 81
63 libclang-cpp.so.22.1 0x00007f9d5e1478a9 clang::Parser::ParsePostfixExpressionSuffix(clang::ActionResult&lt;clang::Expr*, true&gt;) + 3369
64 libclang-cpp.so.22.1 0x00007f9d5e13f9b2 clang::Parser::ParseCastExpression(clang::CastParseKind, bool, bool&amp;, clang::TypoCorrectionTypeBehavior, bool, bool*) + 690
65 libclang-cpp.so.22.1 0x00007f9d5e140bf9 clang::Parser::ParseCastExpression(clang::CastParseKind, bool, bool&amp;, clang::TypoCorrectionTypeBehavior, bool, bool*) + 5369
66 libclang-cpp.so.22.1 0x00007f9d5e1424cb
67 libclang-cpp.so.22.1 0x00007f9d5e14695c clang::Parser::ParseAssignmentExpression(clang::TypoCorrectionTypeBehavior) + 60
68 libclang-cpp.so.22.1 0x00007f9d5e184722 clang::Parser::ParseInitializer(clang::Decl*) + 258
69 libclang-cpp.so.22.1 0x00007f9d5e0f2e51 clang::Parser::ParseDeclarationAfterDeclaratorAndAttributes(clang::Declarator&amp;, clang::Parser::ParsedTemplateInfo const&amp;, clang::Parser::ForRangeInit*) + 497
70 libclang-cpp.so.22.1 0x00007f9d5e1094c7 clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&amp;, clang::DeclaratorContext, clang::ParsedAttributes&amp;, clang::Parser::ParsedTemplateInfo&amp;, clang::SourceLocation*, clang::Parser::ForRangeInit*) + 3879
71 libclang-cpp.so.22.1 0x00007f9d5e10ae77 clang::Parser::ParseSimpleDeclaration(clang::DeclaratorContext, clang::SourceLocation&amp;, clang::ParsedAttributes&amp;, clang::ParsedAttributes&amp;, bool, clang::Parser::ForRangeInit*, clang::SourceLocation*) + 807
72 libclang-cpp.so.22.1 0x00007f9d5e10b360 clang::Parser::ParseDeclaration(clang::DeclaratorContext, clang::SourceLocation&amp;, clang::ParsedAttributes&amp;, clang::ParsedAttributes&amp;, clang::SourceLocation*) + 256
73 libclang-cpp.so.22.1 0x00007f9d5e1eaeee clang::Parser::ParseStatementOrDeclarationAfterAttributes(llvm::SmallVector&lt;clang::Stmt*, 24u&gt;&amp;, clang::Parser::ParsedStmtContext, clang::SourceLocation*, clang::ParsedAttributes&amp;, clang::ParsedAttributes&amp;, clang::LabelDecl*) + 6798
74 libclang-cpp.so.22.1 0x00007f9d5e1eb9e6 clang::Parser::ParseStatementOrDeclaration(llvm::SmallVector&lt;clang::Stmt*, 24u&gt;&amp;, clang::Parser::ParsedStmtContext, clang::SourceLocation*, clang::LabelDecl*) + 390
75 libclang-cpp.so.22.1 0x00007f9d5e1ecfd2 clang::Parser::ParseCompoundStatementBody(bool) + 1250
76 libclang-cpp.so.22.1 0x00007f9d5e163d93 clang::Parser::ParseLambdaExpressionAfterIntroducer(clang::LambdaIntroducer&amp;) + 4307
77 libclang-cpp.so.22.1 0x00007f9d5e16560f clang::Parser::ParseLambdaExpression() + 287
78 libclang-cpp.so.22.1 0x00007f9d5e141c86 clang::Parser::ParseCastExpression(clang::CastParseKind, bool, bool&amp;, clang::TypoCorrectionTypeBehavior, bool, bool*) + 9606
79 libclang-cpp.so.22.1 0x00007f9d5e1424cb
80 libclang-cpp.so.22.1 0x00007f9d5e14695c clang::Parser::ParseAssignmentExpression(clang::TypoCorrectionTypeBehavior) + 60
81 libclang-cpp.so.22.1 0x00007f9d5e184722 clang::Parser::ParseInitializer(clang::Decl*) + 258
82 libclang-cpp.so.22.1 0x00007f9d5e0f2e51 clang::Parser::ParseDeclarationAfterDeclaratorAndAttributes(clang::Declarator&amp;, clang::Parser::ParsedTemplateInfo const&amp;, clang::Parser::ForRangeInit*) + 497
83 libclang-cpp.so.22.1 0x00007f9d5e1094c7 clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&amp;, clang::DeclaratorContext, clang::ParsedAttributes&amp;, clang::Parser::ParsedTemplateInfo&amp;, clang::SourceLocation*, clang::Parser::ForRangeInit*) + 3879
84 libclang-cpp.so.22.1 0x00007f9d5e21f6e8 clang::Parser::ParseDeclOrFunctionDefInternal(clang::ParsedAttributes&amp;, clang::ParsedAttributes&amp;, clang::ParsingDeclSpec&amp;, clang::AccessSpecifier) + 1016
85 libclang-cpp.so.22.1 0x00007f9d5e21ff0d clang::Parser::ParseDeclarationOrFunctionDefinition(clang::ParsedAttributes&amp;, clang::ParsedAttributes&amp;, clang::ParsingDeclSpec*, clang::AccessSpecifier) + 957
86 libclang-cpp.so.22.1 0x00007f9d5e229502 clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&amp;, clang::ParsedAttributes&amp;, clang::ParsingDeclSpec*) + 642
87 libclang-cpp.so.22.1 0x00007f9d5e22a97c clang::Parser::ParseTopLevelDecl(clang::OpaquePtr&lt;clang::DeclGroupRef&gt;&amp;, clang::Sema::ModuleImportState&amp;) + 540
88 libclang-cpp.so.22.1 0x00007f9d5e0d0ceb clang::ParseAST(clang::Sema&amp;, bool, bool) + 587
89 libclang-cpp.so.22.1 0x00007f9d608f8aeb clang::FrontendAction::Execute() + 59
90 clangd               0x000055e6dcc2b173
91 clangd               0x000055e6dc999db4
92 clangd               0x000055e6dc989bac
93 libc.so.6            0x00007f9d52a27741
94 libc.so.6            0x00007f9d52a27879 __libc_start_main + 137
95 clangd               0x000055e6dc8ca7d5
Segmentation fault         (core dumped) clangd -check=/home/tristan/Documents/github/static-engine/repro_dir/repro.hpp

Code for reproduction, the error only happens across headers.

// config.hpp
#pragma once
#include &lt;algorithm&gt;
#include &lt;array&gt;
#include &lt;ranges&gt;
#include &lt;string_view&gt;
#include &lt;vector&gt;

template &lt;size_t N&gt; struct CompString {
  constexpr CompString(const char (&amp;arr)[N]) {
    std::copy_n(arr, N, data.data());
  }
  constexpr CompString(std::array&lt;char, N&gt; arr) {
    std::copy_n(arr.data(), N, data.data());
  }
  consteval auto view() const { return std::string_view(data.data(), N - 1); }
  std::array&lt;char, N&gt; data;
};

namespace detail {
constexpr char RAW_KV_CONFIG[] = {
#embed "config.txt"
    , '\0'};

constexpr std::string_view VIEW_KV_CONFIGS{RAW_KV_CONFIG,
                                           sizeof(RAW_KV_CONFIG) - 1};

struct KeyValueStringPair {
  std::string_view key;
  std::string_view value;
};
} // namespace detail

consteval auto split(std::string_view s, char delimiter)
    -&gt; std::vector&lt;std::string_view&gt; {
  std::vector&lt;std::string_view&gt; separated;
  size_t last_split{};
  for (auto [index, letter] : std::views::enumerate(s)) {
    if (letter == delimiter) {
      if (index - last_split != 0) {
        separated.push_back(s.substr(last_split, index - last_split));
      }
      last_split = index + 1;
    } else if (index + 1 == s.size()) {
      if (index - last_split + 1 != 0) {
        separated.push_back(s.substr(last_split, index - last_split + 1));
      }
    }
  }
  return separated;
}

consteval auto sanitize(std::string_view s, char discarded = ' ')
    -&gt; std::string_view {
  size_t start{};
  size_t end{s.size()};
  for (; start &lt; end &amp;&amp; s[start] == discarded; ++start);
  for (; start &lt; end &amp;&amp; s[end - 1] == discarded; --end);
  return s.substr(start, end - start);
}

constexpr size_t NUM_CONFIGS = std::count(detail::VIEW_KV_CONFIGS.begin(),
                                          detail::VIEW_KV_CONFIGS.end(), '\n') +
                               1;

constexpr auto KV_STRING_PAIRS =
    [] -&gt; std::array&lt;detail::KeyValueStringPair, NUM_CONFIGS&gt; {
  std::array&lt;detail::KeyValueStringPair, NUM_CONFIGS&gt; configs;
  auto lines = split(detail::VIEW_KV_CONFIGS, '\n');
  for (auto [index, line] : std::views::enumerate(lines)) {
    auto pair = split(line, ':');
    if (pair.size() != 2) {
      throw "Invalid config pair";
    }
    configs[index] =
        detail::KeyValueStringPair(sanitize(pair[0]), sanitize(pair[1]));
  }
  return configs;
}();

template &lt;CompString S&gt; constexpr auto get_config() -&gt; std::string_view {
  for (auto [key, value] : KV_STRING_PAIRS) {
    if (key == S.view())
      return sanitize(value);
  }
  throw "Key not found";
}
// repro.hpp
#pragma once
#include "config.hpp"

enum class LogLevel { TRACE, DEBUG, INFO, WARN, ERROR, FATAL };

static constexpr LogLevel KEPT_LOG_LEVEL = [] -&gt; LogLevel {
  constexpr auto level = get_config&lt;"log_level"&gt;();
  if (level == "INFO")
    return LogLevel::INFO;
  return LogLevel::ERROR;
}();

config.txt

log_level: INFO

I can reproduce with clangd 22 but not with trunk.

@dargem could you try with the latest weekly snapshot of trunk from https://github.com/clangd/clangd/releases/tag/snapshot_20260712?

HighCommander4 avatar Jul 24 '26 03:07 HighCommander4

I can reproduce with clangd 22 but not with trunk.

@dargem could you try with the latest weekly snapshot of trunk from https://github.com/clangd/clangd/releases/tag/snapshot_20260712?

Thank you, with trunk it works properly

dargem avatar Jul 26 '26 12:07 dargem