hokein created this revision. hokein added a reviewer: kadircet. Herald added subscribers: usaxena95, arphaman, jkorous, MaskRay, ilya-biryukov. Herald added a project: clang.
Fixes https://github.com/clangd/clangd/issues/455 Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D83546 Files: clang-tools-extra/clangd/Hover.cpp clang-tools-extra/clangd/unittests/HoverTests.cpp Index: clang-tools-extra/clangd/unittests/HoverTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/HoverTests.cpp +++ clang-tools-extra/clangd/unittests/HoverTests.cpp @@ -801,6 +801,20 @@ HI.LocalScope = "Foo::"; HI.Type = "int"; HI.AccessSpecifier = "public"; + }}, + {R"cpp( + struct Foo { + int a[10]; + }; + constexpr Foo k2 = { + ^[[{]]1} // FIXME: why the hover range is 1 character? + }; + )cpp", + [](HoverInfo &HI) { + HI.Name = "expression"; + HI.Kind = index::SymbolKind::Unknown; + HI.Type = "int [10]"; + HI.Value = "{1}"; }}}; for (const auto &Case : Cases) { SCOPED_TRACE(Case.Code); Index: clang-tools-extra/clangd/Hover.cpp =================================================================== --- clang-tools-extra/clangd/Hover.cpp +++ clang-tools-extra/clangd/Hover.cpp @@ -336,6 +336,15 @@ if (T.isNull() || T->isFunctionType() || T->isFunctionPointerType() || T->isFunctionReferenceType()) return llvm::None; + // InitListExpr has two forms, syntactic and semantic. They are the same thing + // (refer to a same AST node) in most cases. + // When they are different, RAV returns the syntacic form, and we should feed + // the semantic form to EvaluateAsRValue. + if (const auto *ILE = llvm::dyn_cast<InitListExpr>(E)) { + if (!ILE->isSemanticForm()) + E = ILE->getSemanticForm(); + } + // Attempt to evaluate. If expr is dependent, evaluation crashes! if (E->isValueDependent() || !E->EvaluateAsRValue(Constant, Ctx) || // Disable printing for record-types, as they are usually confusing and
Index: clang-tools-extra/clangd/unittests/HoverTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/HoverTests.cpp +++ clang-tools-extra/clangd/unittests/HoverTests.cpp @@ -801,6 +801,20 @@ HI.LocalScope = "Foo::"; HI.Type = "int"; HI.AccessSpecifier = "public"; + }}, + {R"cpp( + struct Foo { + int a[10]; + }; + constexpr Foo k2 = { + ^[[{]]1} // FIXME: why the hover range is 1 character? + }; + )cpp", + [](HoverInfo &HI) { + HI.Name = "expression"; + HI.Kind = index::SymbolKind::Unknown; + HI.Type = "int [10]"; + HI.Value = "{1}"; }}}; for (const auto &Case : Cases) { SCOPED_TRACE(Case.Code); Index: clang-tools-extra/clangd/Hover.cpp =================================================================== --- clang-tools-extra/clangd/Hover.cpp +++ clang-tools-extra/clangd/Hover.cpp @@ -336,6 +336,15 @@ if (T.isNull() || T->isFunctionType() || T->isFunctionPointerType() || T->isFunctionReferenceType()) return llvm::None; + // InitListExpr has two forms, syntactic and semantic. They are the same thing + // (refer to a same AST node) in most cases. + // When they are different, RAV returns the syntacic form, and we should feed + // the semantic form to EvaluateAsRValue. + if (const auto *ILE = llvm::dyn_cast<InitListExpr>(E)) { + if (!ILE->isSemanticForm()) + E = ILE->getSemanticForm(); + } + // Attempt to evaluate. If expr is dependent, evaluation crashes! if (E->isValueDependent() || !E->EvaluateAsRValue(Constant, Ctx) || // Disable printing for record-types, as they are usually confusing and
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits