[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
This commit is contained in:
Adam Czachorowski 2021-01-18 18:17:52 +01:00
parent 3546b37221
commit 00054382b9
2 changed files with 17 additions and 1 deletions

View File

@ -1838,6 +1838,20 @@ TEST_F(SymbolCollectorTest, UndefOfModuleMacro) {
EXPECT_THAT(TU.headerSymbols(), Not(Contains(QName("X"))));
}
TEST_F(SymbolCollectorTest, NoCrashOnObjCMethodCStyleParam) {
auto TU = TestTU::withCode(R"objc(
@interface Foo
- (void)fun:(bool)foo, bool bar;
@end
)objc");
TU.ExtraArgs.push_back("-xobjective-c++");
TU.build();
// We mostly care about not crashing.
EXPECT_THAT(TU.headerSymbols(),
UnorderedElementsAre(QName("Foo"), QName("Foo::fun:")));
}
} // namespace
} // namespace clangd
} // namespace clang

View File

@ -3529,9 +3529,11 @@ CodeCompletionString *CodeCompletionResult::createCodeCompletionStringForDecl(
Result.AddTypedTextChunk("");
}
unsigned Idx = 0;
// The extra Idx < Sel.getNumArgs() check is needed due to legacy C-style
// method parameters.
for (ObjCMethodDecl::param_const_iterator P = Method->param_begin(),
PEnd = Method->param_end();
P != PEnd; (void)++P, ++Idx) {
P != PEnd && Idx < Sel.getNumArgs(); (void)++P, ++Idx) {
if (Idx > 0) {
std::string Keyword;
if (Idx > StartParameter)