Author: Adam Czachorowski Date: 2021-11-10T17:17:00+01:00 New Revision: 7a2b1bdb4c8a099ebc38b7f802988244ad21fcc0
URL: https://github.com/llvm/llvm-project/commit/7a2b1bdb4c8a099ebc38b7f802988244ad21fcc0 DIFF: https://github.com/llvm/llvm-project/commit/7a2b1bdb4c8a099ebc38b7f802988244ad21fcc0.diff LOG: [clang] Do not crash in APValue::prettyPrint() on forward-decl structs. The call to getTypeSizeInChars() is replaced with getTypeSizeInCharsIfKnown(), which does not crash on forward declared structs. This only affects printing. Differential Revision: https://reviews.llvm.org/D113570 Added: Modified: clang-tools-extra/clangd/unittests/HoverTests.cpp clang/lib/AST/APValue.cpp Removed: ################################################################################ diff --git a/clang-tools-extra/clangd/unittests/HoverTests.cpp b/clang-tools-extra/clangd/unittests/HoverTests.cpp index 2e33ce4c5d101..53df965fef2fa 100644 --- a/clang-tools-extra/clangd/unittests/HoverTests.cpp +++ b/clang-tools-extra/clangd/unittests/HoverTests.cpp @@ -2963,6 +2963,20 @@ TEST(Hover, SpaceshipTemplateNoCrash) { EXPECT_EQ(HI->Documentation, "Foo bar baz"); } +TEST(Hover, ForwardStructNoCrash) { + Annotations T(R"cpp( + struct Foo; + int bar; + auto baz = (Fo^o*)&bar; + )cpp"); + + TestTU TU = TestTU::withCode(T.code()); + auto AST = TU.build(); + auto HI = getHover(AST, T.point(), format::getLLVMStyle(), nullptr); + ASSERT_TRUE(HI); + EXPECT_EQ(*HI->Value, "&bar"); +} + } // namespace } // namespace clangd } // namespace clang diff --git a/clang/lib/AST/APValue.cpp b/clang/lib/AST/APValue.cpp index 9a9233bc1ea74..ef333c7711663 100644 --- a/clang/lib/AST/APValue.cpp +++ b/clang/lib/AST/APValue.cpp @@ -700,7 +700,9 @@ void APValue::printPretty(raw_ostream &Out, const PrintingPolicy &Policy, if (!hasLValuePath()) { // No lvalue path: just print the offset. CharUnits O = getLValueOffset(); - CharUnits S = Ctx ? Ctx->getTypeSizeInChars(InnerTy) : CharUnits::Zero(); + CharUnits S = Ctx ? Ctx->getTypeSizeInCharsIfKnown(InnerTy).getValueOr( + CharUnits::Zero()) + : CharUnits::Zero(); if (!O.isZero()) { if (IsReference) Out << "*("; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits