[clang-tools-extra] 254b016 - [clangd] More complete fix for hover crashes on invalid record.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 6 08:13:38 PDT 2020


Author: Haojian Wu
Date: 2020-07-06T17:12:39+02:00
New Revision: 254b016c6561e4ec4d145b81c4d0aaf3d2c7fca6

URL: https://github.com/llvm/llvm-project/commit/254b016c6561e4ec4d145b81c4d0aaf3d2c7fca6
DIFF: https://github.com/llvm/llvm-project/commit/254b016c6561e4ec4d145b81c4d0aaf3d2c7fca6.diff

LOG: [clangd] More complete fix for hover crashes on invalid record.

 We should not call getFieldOffset on invalid record decls.

Differential Revision: https://reviews.llvm.org/D83189

Added: 
    

Modified: 
    clang-tools-extra/clangd/Hover.cpp
    clang-tools-extra/clangd/unittests/HoverTests.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/Hover.cpp b/clang-tools-extra/clangd/Hover.cpp
index f495052d2d30..cba933508fd5 100644
--- a/clang-tools-extra/clangd/Hover.cpp
+++ b/clang-tools-extra/clangd/Hover.cpp
@@ -659,8 +659,10 @@ bool isHardLineBreakAfter(llvm::StringRef Line, llvm::StringRef Rest) {
 }
 
 void addLayoutInfo(const NamedDecl &ND, HoverInfo &HI) {
-  const auto &Ctx = ND.getASTContext();
+  if (ND.isInvalidDecl())
+    return;
 
+  const auto &Ctx = ND.getASTContext();
   if (auto *RD = llvm::dyn_cast<RecordDecl>(&ND)) {
     if (auto Size = Ctx.getTypeSizeInCharsIfKnown(RD->getTypeForDecl()))
       HI.Size = Size->getQuantity();
@@ -671,11 +673,10 @@ void addLayoutInfo(const NamedDecl &ND, HoverInfo &HI) {
     const auto *Record = FD->getParent();
     if (Record)
       Record = Record->getDefinition();
-    if (Record && !Record->isDependentType()) {
+    if (Record && !Record->isInvalidDecl() && !Record->isDependentType()) {
+      HI.Offset = Ctx.getFieldOffset(FD) / 8;
       if (auto Size = Ctx.getTypeSizeInCharsIfKnown(FD->getType()))
         HI.Size = Size->getQuantity();
-      if (!FD->isInvalidDecl())
-        HI.Offset = Ctx.getFieldOffset(FD) / 8;
     }
     return;
   }

diff  --git a/clang-tools-extra/clangd/unittests/HoverTests.cpp b/clang-tools-extra/clangd/unittests/HoverTests.cpp
index 3be796a6fb89..43f1e7142550 100644
--- a/clang-tools-extra/clangd/unittests/HoverTests.cpp
+++ b/clang-tools-extra/clangd/unittests/HoverTests.cpp
@@ -784,11 +784,24 @@ class Foo {})cpp";
          HI.NamespaceScope = "";
          HI.Definition = "int xx";
          HI.LocalScope = "Foo::";
-         HI.Size = 4;
          HI.Type = "int";
          HI.AccessSpecifier = "public";
        }},
-  };
+      {R"cpp(
+        // error-ok
+        struct Foo {
+          Bar xx;
+          int [[y^y]];
+        };)cpp",
+       [](HoverInfo &HI) {
+         HI.Name = "yy";
+         HI.Kind = index::SymbolKind::Field;
+         HI.NamespaceScope = "";
+         HI.Definition = "int yy";
+         HI.LocalScope = "Foo::";
+         HI.Type = "int";
+         HI.AccessSpecifier = "public";
+       }}};
   for (const auto &Case : Cases) {
     SCOPED_TRACE(Case.Code);
 


        


More information about the cfe-commits mailing list