Skip to content

Commit a3977c9

Browse files
authored
[clangd] check for synthesized symbols when tracking include locations (#75128)
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 f557f05 commit a3977c9

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
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.
@@ -879,16 +880,15 @@ void SymbolCollector::finish() {
879880
const Symbol *S = Symbols.find(SID);
880881
if (!S)
881882
continue;
882-
assert(IncludeFiles.contains(SID));
883883

884-
const auto FID = IncludeFiles.at(SID);
884+
FileID FID = IncludeFiles.lookup(SID);
885885
// Determine if the FID is #include'd or #import'ed.
886886
Symbol::IncludeDirective Directives = Symbol::Invalid;
887887
auto CollectDirectives = shouldCollectIncludePath(S->SymInfo.Kind);
888888
if ((CollectDirectives & Symbol::Include) != 0)
889889
Directives |= Symbol::Include;
890890
// Only allow #import for symbols from ObjC-like files.
891-
if ((CollectDirectives & Symbol::Import) != 0) {
891+
if ((CollectDirectives & Symbol::Import) != 0 && FID.isValid()) {
892892
auto [It, Inserted] = FileToContainsImportsOrObjC.try_emplace(FID);
893893
if (Inserted)
894894
It->second = FilesWithObjCConstructs.contains(FID) ||
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: rm -rf %t.dir && mkdir -p %t.dir
2+
// 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: clangd -enable-config=0 --compile-commands-dir=%t.dir -check=%s 2>&1 | FileCheck -strict-whitespace %s
4+
5+
// CHECK: Building preamble...
6+
// CHECK-NEXT: Built preamble
7+
// CHECK-NEXT: Indexing headers...
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
14+
15+
#define assert

0 commit comments

Comments
 (0)