diff --git a/clang-tools-extra/clangd/Hover.cpp b/clang-tools-extra/clangd/Hover.cpp --- a/clang-tools-extra/clangd/Hover.cpp +++ b/clang-tools-extra/clangd/Hover.cpp @@ -777,6 +777,17 @@ return HI; } +HoverInfo getStringLiteralContents(const StringLiteral *SL, + const PrintingPolicy &PP) { + HoverInfo HI; + + HI.Name = "string-literal"; + HI.Size = (SL->getLength() + 1) * SL->getCharByteWidth(); + HI.Type = SL->getType().getAsString(PP).c_str(); + + return HI; +} + bool isLiteral(const Expr *E) { // Unfortunately there's no common base Literal classes inherits from // (apart from Expr), therefore these exclusions. @@ -785,7 +796,7 @@ llvm::isa(E) || llvm::isa(E) || llvm::isa(E) || llvm::isa(E) || llvm::isa(E) || - llvm::isa(E) || llvm::isa(E); + llvm::isa(E); } llvm::StringLiteral getNameForExpr(const Expr *E) { @@ -810,6 +821,9 @@ return llvm::None; HoverInfo HI; + // Print the type and the size for string literals + if (const StringLiteral *SL = dyn_cast(E)) + return getStringLiteralContents(SL, PP); // For `this` expr we currently generate hover with pointee type. if (const CXXThisExpr *CTE = dyn_cast(E)) return getThisExprHoverContents(CTE, AST.getASTContext(), PP); diff --git a/clang-tools-extra/clangd/unittests/HoverTests.cpp b/clang-tools-extra/clangd/unittests/HoverTests.cpp --- a/clang-tools-extra/clangd/unittests/HoverTests.cpp +++ b/clang-tools-extra/clangd/unittests/HoverTests.cpp @@ -1300,7 +1300,6 @@ "auto x = ^42.0i;", "auto x = ^42;", "auto x = ^nullptr;", - "auto x = ^\"asdf\";", }; for (const auto &Test : Tests) { @@ -1326,6 +1325,12 @@ HI.Type = "char"; HI.Value = "65 (0x41)"; }}, + {"auto s = ^[[\"Hello, world!\"]]; // string literal", + [](HoverInfo &HI) { + HI.Name = "string-literal"; + HI.Size = 14; + HI.Type = "const char[14]"; + }}, { R"cpp(// Local variable int main() {