[PATCH] D94919: [clangd] Fix a crash when indexing invalid ObjC method declaration
Adam Czachorowski via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Jan 18 09:19:47 PST 2021
adamcz created this revision.
Herald added subscribers: usaxena95, kadircet, arphaman.
adamcz requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang.
This fix will make us not crash, but ideally we would handle this case
better.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D94919
Files:
clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
clang/lib/Sema/SemaCodeComplete.cpp
Index: clang/lib/Sema/SemaCodeComplete.cpp
===================================================================
--- clang/lib/Sema/SemaCodeComplete.cpp
+++ clang/lib/Sema/SemaCodeComplete.cpp
@@ -3493,6 +3493,14 @@
return Result.TakeString();
}
+ if (Method->param_size() > Sel.getNumArgs()) {
+ // FIXME: We could be smarter about this, but for now just avoid a crash
+ // below by returning early.
+ Result.AddTypedTextChunk(
+ Result.getAllocator().CopyString(ND->getNameAsString()));
+ return Result.TakeString();
+ }
+
std::string SelName = Sel.getNameForSlot(0).str();
SelName += ':';
if (StartParameter == 0)
Index: clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
+++ clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
@@ -1801,6 +1801,23 @@
EXPECT_THAT(TU.headerSymbols(), Not(Contains(QName("X"))));
}
+TEST_F(SymbolCollectorTest, NoCrashOnObjCMethodCStyleParam) {
+ auto TU = TestTU::withCode(R"objc(
+ /*error-ok*/
+ @interface Foo
+ - (void)fun:(bool)foo
+ bar:(bool)bar,
+ baz:(bool)baz;
+ @end
+ )objc");
+ TU.ExtraArgs.push_back("-xobjective-c++");
+
+ TU.build();
+ // We mostly care about not crashing, but verify that we didn't insert garbage
+ // about X too.
+ EXPECT_THAT(TU.headerSymbols(), Not(Contains(QName("X"))));
+}
+
} // namespace
} // namespace clangd
} // namespace clang
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D94919.317377.patch
Type: text/x-patch
Size: 1558 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210118/4f9bd397/attachment.bin>
More information about the cfe-commits
mailing list