[PATCH] D113570: [clang] Do not crash in APValue::prettyPrint() on forward-decl structs.

Adam Czachorowski via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 10 08:18:01 PST 2021


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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D113570.386179.patch
Type: text/x-patch
Size: 1331 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211110/08137cbb/attachment-0001.bin>


More information about the cfe-commits mailing list