This revision was automatically updated to reflect the committed changes. Closed by commit rG7a2b1bdb4c8a: [clang] Do not crash in APValue::prettyPrint() on forward-decl structs. (authored by adamcz).
Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D113570/new/ https://reviews.llvm.org/D113570 Files: clang-tools-extra/clangd/unittests/HoverTests.cpp clang/lib/AST/APValue.cpp Index: clang/lib/AST/APValue.cpp =================================================================== --- clang/lib/AST/APValue.cpp +++ clang/lib/AST/APValue.cpp @@ -700,7 +700,9 @@ 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 << "*("; Index: clang-tools-extra/clangd/unittests/HoverTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/HoverTests.cpp +++ clang-tools-extra/clangd/unittests/HoverTests.cpp @@ -2963,6 +2963,20 @@ 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
Index: clang/lib/AST/APValue.cpp =================================================================== --- clang/lib/AST/APValue.cpp +++ clang/lib/AST/APValue.cpp @@ -700,7 +700,9 @@ 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 << "*("; Index: clang-tools-extra/clangd/unittests/HoverTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/HoverTests.cpp +++ clang-tools-extra/clangd/unittests/HoverTests.cpp @@ -2963,6 +2963,20 @@ 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
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits