Skip to content

Commit 0005438

Browse files
committed
[clangd] Fix a crash when indexing invalid ObjC method declaration
This fix will make us not crash, but ideally we would handle this case better. Differential Revision: https://reviews.llvm.org/D94919
1 parent 3546b37 commit 0005438

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,6 +1838,20 @@ TEST_F(SymbolCollectorTest, UndefOfModuleMacro) {
18381838
EXPECT_THAT(TU.headerSymbols(), Not(Contains(QName("X"))));
18391839
}
18401840

1841+
TEST_F(SymbolCollectorTest, NoCrashOnObjCMethodCStyleParam) {
1842+
auto TU = TestTU::withCode(R"objc(
1843+
@interface Foo
1844+
- (void)fun:(bool)foo, bool bar;
1845+
@end
1846+
)objc");
1847+
TU.ExtraArgs.push_back("-xobjective-c++");
1848+
1849+
TU.build();
1850+
// We mostly care about not crashing.
1851+
EXPECT_THAT(TU.headerSymbols(),
1852+
UnorderedElementsAre(QName("Foo"), QName("Foo::fun:")));
1853+
}
1854+
18411855
} // namespace
18421856
} // namespace clangd
18431857
} // namespace clang

clang/lib/Sema/SemaCodeComplete.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3529,9 +3529,11 @@ CodeCompletionString *CodeCompletionResult::createCodeCompletionStringForDecl(
35293529
Result.AddTypedTextChunk("");
35303530
}
35313531
unsigned Idx = 0;
3532+
// The extra Idx < Sel.getNumArgs() check is needed due to legacy C-style
3533+
// method parameters.
35323534
for (ObjCMethodDecl::param_const_iterator P = Method->param_begin(),
35333535
PEnd = Method->param_end();
3534-
P != PEnd; (void)++P, ++Idx) {
3536+
P != PEnd && Idx < Sel.getNumArgs(); (void)++P, ++Idx) {
35353537
if (Idx > 0) {
35363538
std::string Keyword;
35373539
if (Idx > StartParameter)

0 commit comments

Comments
 (0)