Skip to content

Commit 41db595

Browse files
committed
Comments and partial refactor
1 parent 3d651d4 commit 41db595

File tree

3 files changed

+60
-17
lines changed

3 files changed

+60
-17
lines changed

clang-tools-extra/clangd/InlayHints.cpp

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -381,20 +381,18 @@ maybeDropCxxExplicitObjectParameters(ArrayRef<const ParmVarDecl *> Params) {
381381
return Params;
382382
}
383383

384-
template <typename R, typename P>
385-
std::string joinAndTruncate(R &&Range, size_t MaxLength,
386-
P &&GetAsStringFunction) {
384+
template <typename R>
385+
std::string joinAndTruncate(const R &Range, size_t MaxLength) {
387386
std::string Out;
388387
llvm::raw_string_ostream OS(Out);
389388
llvm::ListSeparator Sep(", ");
390389
for (auto &&Element : Range) {
391390
OS << Sep;
392-
auto AsString = GetAsStringFunction(Element);
393-
if (Out.size() + AsString.size() >= MaxLength) {
391+
if (Out.size() + Element.size() >= MaxLength) {
394392
OS << "...";
395393
break;
396394
}
397-
OS << AsString;
395+
OS << Element;
398396
}
399397
OS.flush();
400398
return Out;
@@ -738,11 +736,12 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
738736
private:
739737
using NameVec = SmallVector<StringRef, 8>;
740738

741-
void processCall(Callee Callee, SourceRange RParenOrBraceRange,
739+
void processCall(Callee Callee, SourceLocation RParenOrBraceLoc,
742740
llvm::ArrayRef<const Expr *> Args) {
743741
assert(Callee.Decl || Callee.Loc);
744742

745-
if (!Cfg.InlayHints.Parameters || Args.size() == 0)
743+
if ((!Cfg.InlayHints.Parameters && !Cfg.InlayHints.DefaultArguments) ||
744+
Args.size() == 0)
746745
return;
747746

748747
// The parameter name of a move or copy constructor is not very interesting.
@@ -785,9 +784,11 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
785784
}
786785

787786
StringRef Name = ParameterNames[I];
788-
const bool NameHint = shouldHintName(Args[I], Name);
787+
const bool NameHint =
788+
shouldHintName(Args[I], Name) && Cfg.InlayHints.Parameters;
789789
const bool ReferenceHint =
790-
shouldHintReference(Params[I], ForwardedParams[I]);
790+
shouldHintReference(Params[I], ForwardedParams[I]) &&
791+
Cfg.InlayHints.Parameters;
791792

792793
const bool IsDefault = isa<CXXDefaultArgExpr>(Args[I]);
793794
HasNonDefaultArgs |= !IsDefault;
@@ -796,9 +797,11 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
796797
const auto SourceText = Lexer::getSourceText(
797798
CharSourceRange::getTokenRange(Params[I]->getDefaultArgRange()),
798799
AST.getSourceManager(), AST.getLangOpts());
799-
const auto Abbrev = SourceText.size() > Cfg.InlayHints.TypeNameLimit
800-
? "..."
801-
: SourceText;
800+
const auto Abbrev =
801+
(SourceText.size() > Cfg.InlayHints.TypeNameLimit ||
802+
SourceText.contains("\n"))
803+
? "..."
804+
: SourceText;
802805
if (NameHint)
803806
FormattedDefaultArgs.emplace_back(
804807
llvm::formatv("{0}: {1}", Name, Abbrev));
@@ -814,9 +817,8 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
814817

815818
if (!FormattedDefaultArgs.empty()) {
816819
std::string Hint =
817-
joinAndTruncate(FormattedDefaultArgs, Cfg.InlayHints.TypeNameLimit,
818-
[](const auto &E) { return E; });
819-
addInlayHint(RParenOrBraceRange, HintSide::Left,
820+
joinAndTruncate(FormattedDefaultArgs, Cfg.InlayHints.TypeNameLimit);
821+
addInlayHint(SourceRange{RParenOrBraceLoc}, HintSide::Left,
820822
InlayHintKind::DefaultArgument,
821823
HasNonDefaultArgs ? ", " : "", Hint, "");
822824
}

clang-tools-extra/clangd/Protocol.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1686,7 +1686,7 @@ enum class InlayHintKind {
16861686
/// An example of a parameter hint for a default argument:
16871687
/// void foo(bool A = true);
16881688
/// foo(^);
1689-
/// Adds an inlay hint "A = true".
1689+
/// Adds an inlay hint "A: true".
16901690
/// This is a clangd extension.
16911691
DefaultArgument = 6,
16921692

clang-tools-extra/clangd/unittests/InlayHintTests.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,47 @@ TEST(DefaultArguments, Smoke) {
14971497
ExpectedHint{"A: ", "explicit", Left});
14981498
}
14991499

1500+
TEST(DefaultArguments, WithoutParameterNames) {
1501+
Config Cfg;
1502+
Cfg.InlayHints.Parameters = false; // To test just default args this time
1503+
Cfg.InlayHints.DeducedTypes = false;
1504+
Cfg.InlayHints.Designators = false;
1505+
Cfg.InlayHints.BlockEnd = false;
1506+
1507+
Cfg.InlayHints.DefaultArguments = true;
1508+
WithContextValue WithCfg(Config::Key, std::move(Cfg));
1509+
1510+
const auto *Code = R"cpp(
1511+
struct Baz {
1512+
Baz(float a = 3 //
1513+
+ 2);
1514+
};
1515+
struct Foo {
1516+
Foo(int, Baz baz = //
1517+
Baz{$unnamed[[}]]
1518+
1519+
//
1520+
) {}
1521+
};
1522+
1523+
int main() {
1524+
Foo foo1(1$paren[[)]];
1525+
Foo foo2{2$brace1[[}]];
1526+
Foo foo3 = {3$brace2[[}]];
1527+
auto foo4 = Foo{4$brace3[[}]];
1528+
}
1529+
)cpp";
1530+
1531+
assertHints(InlayHintKind::DefaultArgument, Code,
1532+
ExpectedHint{"...", "unnamed", Left},
1533+
ExpectedHint{", Baz{}", "paren", Left},
1534+
ExpectedHint{", Baz{}", "brace1", Left},
1535+
ExpectedHint{", Baz{}", "brace2", Left},
1536+
ExpectedHint{", Baz{}", "brace3", Left});
1537+
1538+
assertHints(InlayHintKind::Parameter, Code);
1539+
}
1540+
15001541
TEST(TypeHints, Deduplication) {
15011542
assertTypeHints(R"cpp(
15021543
template <typename T>

0 commit comments

Comments
 (0)