Skip to content

Commit 6f4cc2d

Browse files
committed
[clangd] check for synthesized symbols when tracking include locations
This fixes #75115 In C mode with MSVC compatibility, when the `assert` macro is defined, as a workaround, `static_assert` is implicitly defined as well, if not already so, in order to work around a broken `assert.h` implementation. This workaround was implemented in 8da0903 A synthesized symbol does not occur in source code, and therefore should not have valid source location, but this was not checked when inserting this into a `symbol -> include file` map. The invalid FileID value is used for empty key representation in the include file hash table, so it's not valid to insert it.
1 parent 04580ed commit 6f4cc2d

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

clang-tools-extra/clangd/index/SymbolCollector.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,8 @@ void SymbolCollector::setIncludeLocation(const Symbol &S, SourceLocation DefLoc,
821821

822822
// Use the expansion location to get the #include header since this is
823823
// where the symbol is exposed.
824-
IncludeFiles[S.ID] = SM.getDecomposedExpansionLoc(DefLoc).first;
824+
if (FileID FID = SM.getDecomposedExpansionLoc(DefLoc).first; FID.isValid())
825+
IncludeFiles[S.ID] = FID;
825826

826827
// We update providers for a symbol with each occurence, as SymbolCollector
827828
// might run while parsing, rather than at the end of a translation unit.
@@ -893,16 +894,15 @@ void SymbolCollector::finish() {
893894
const Symbol *S = Symbols.find(SID);
894895
if (!S)
895896
continue;
896-
assert(IncludeFiles.contains(SID));
897897

898-
const auto FID = IncludeFiles.at(SID);
898+
FileID FID = IncludeFiles.lookup(SID);
899899
// Determine if the FID is #include'd or #import'ed.
900900
Symbol::IncludeDirective Directives = Symbol::Invalid;
901901
auto CollectDirectives = shouldCollectIncludePath(S->SymInfo.Kind);
902902
if ((CollectDirectives & Symbol::Include) != 0)
903903
Directives |= Symbol::Include;
904904
// Only allow #import for symbols from ObjC-like files.
905-
if ((CollectDirectives & Symbol::Import) != 0) {
905+
if ((CollectDirectives & Symbol::Import) != 0 && FID.isValid()) {
906906
auto [It, Inserted] = FileToContainsImportsOrObjC.try_emplace(FID);
907907
if (Inserted)
908908
It->second = FilesWithObjCConstructs.contains(FID) ||
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
// RUN: rm -rf %t.dir && mkdir -p %t.dir
22
// RUN: echo '[{"directory": "%/t.dir", "command": "clang --target=x86_64-pc-windows-msvc -x c GH75115.test", "file": "GH75115.test"}]' > %t.dir/compile_commands.json
3-
// RUN: not --crash clangd -enable-config=0 --compile-commands-dir=%t.dir -check=%s 2>&1 | FileCheck -strict-whitespace %s
4-
5-
// FIXME: Crashes
3+
// RUN: clangd -enable-config=0 --compile-commands-dir=%t.dir -check=%s 2>&1 | FileCheck -strict-whitespace %s
64

75
// CHECK: Building preamble...
86
// CHECK-NEXT: Built preamble
97
// CHECK-NEXT: Indexing headers...
10-
// CHECK-NEXT: !KeyInfoT::isEqual(Val, EmptyKey) && !KeyInfoT::isEqual(Val, TombstoneKey) && "Empty/Tombstone value shouldn't be inserted into map!"
8+
// CHECK-NEXT: Building AST...
9+
// CHECK-NEXT: Indexing AST...
10+
// CHECK-NEXT: Building inlay hints
11+
// CHECK-NEXT: semantic highlighting
12+
// CHECK-NEXT: Testing features at each token
13+
// CHECK-NEXT: All checks completed, 0 errors
1114

1215
#define assert

0 commit comments

Comments
 (0)