Skip to content

Commit 2c9e7c1

Browse files
committed
just defaults
1 parent 9d3a576 commit 2c9e7c1

File tree

8 files changed

+112
-5
lines changed

8 files changed

+112
-5
lines changed

clang-tools-extra/clangd/Config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ struct Config {
148148
bool DeducedTypes = true;
149149
bool Designators = true;
150150
bool BlockEnd = false;
151+
bool DefaultArguments = false;
151152
// Limit the length of type names in inlay hints. (0 means no limit)
152153
uint32_t TypeNameLimit = 32;
153154
} InlayHints;

clang-tools-extra/clangd/ConfigCompile.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
#include "llvm/Support/Regex.h"
4444
#include "llvm/Support/SMLoc.h"
4545
#include "llvm/Support/SourceMgr.h"
46-
#include <algorithm>
4746
#include <memory>
4847
#include <optional>
4948
#include <string>
@@ -654,6 +653,11 @@ struct FragmentCompiler {
654653
Out.Apply.push_back([Value(**F.BlockEnd)](const Params &, Config &C) {
655654
C.InlayHints.BlockEnd = Value;
656655
});
656+
if (F.DefaultArguments)
657+
Out.Apply.push_back(
658+
[Value(**F.DefaultArguments)](const Params &, Config &C) {
659+
C.InlayHints.DefaultArguments = Value;
660+
});
657661
if (F.TypeNameLimit)
658662
Out.Apply.push_back(
659663
[Value(**F.TypeNameLimit)](const Params &, Config &C) {

clang-tools-extra/clangd/ConfigFragment.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,9 @@ struct Fragment {
331331
std::optional<Located<bool>> Designators;
332332
/// Show defined symbol names at the end of a definition block.
333333
std::optional<Located<bool>> BlockEnd;
334+
/// Show parameter names and default values of default arguments after all
335+
/// of the explicit arguments.
336+
std::optional<Located<bool>> DefaultArguments;
334337
/// Limit the length of type name hints. (0 means no limit)
335338
std::optional<Located<uint32_t>> TypeNameLimit;
336339
};

clang-tools-extra/clangd/ConfigYAML.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include "llvm/Support/YAMLParser.h"
1515
#include <optional>
1616
#include <string>
17-
#include <system_error>
1817

1918
namespace clang {
2019
namespace clangd {
@@ -264,6 +263,10 @@ class Parser {
264263
if (auto Value = boolValue(N, "BlockEnd"))
265264
F.BlockEnd = *Value;
266265
});
266+
Dict.handle("DefaultArguments", [&](Node &N) {
267+
if (auto Value = boolValue(N, "DefaultArguments"))
268+
F.DefaultArguments = *Value;
269+
});
267270
Dict.handle("TypeNameLimit", [&](Node &N) {
268271
if (auto Value = uint32Value(N, "TypeNameLimit"))
269272
F.TypeNameLimit = *Value;

clang-tools-extra/clangd/InlayHints.cpp

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
#include "Config.h"
1212
#include "HeuristicResolver.h"
1313
#include "ParsedAST.h"
14+
#include "Protocol.h"
1415
#include "SourceCode.h"
1516
#include "clang/AST/ASTDiagnostic.h"
1617
#include "clang/AST/Decl.h"
18+
#include "clang/AST/DeclBase.h"
1719
#include "clang/AST/DeclarationName.h"
1820
#include "clang/AST/Expr.h"
1921
#include "clang/AST/ExprCXX.h"
@@ -23,15 +25,22 @@
2325
#include "clang/AST/Type.h"
2426
#include "clang/Basic/Builtins.h"
2527
#include "clang/Basic/OperatorKinds.h"
28+
#include "clang/Basic/SourceLocation.h"
2629
#include "clang/Basic/SourceManager.h"
2730
#include "llvm/ADT/DenseSet.h"
31+
#include "llvm/ADT/STLExtras.h"
32+
#include "llvm/ADT/SmallVector.h"
2833
#include "llvm/ADT/StringExtras.h"
2934
#include "llvm/ADT/StringRef.h"
3035
#include "llvm/ADT/Twine.h"
3136
#include "llvm/Support/Casting.h"
37+
#include "llvm/Support/ErrorHandling.h"
38+
#include "llvm/Support/FormatVariadic.h"
3239
#include "llvm/Support/SaveAndRestore.h"
3340
#include "llvm/Support/ScopedPrinter.h"
3441
#include "llvm/Support/raw_ostream.h"
42+
#include <algorithm>
43+
#include <iterator>
3544
#include <optional>
3645
#include <string>
3746

@@ -372,6 +381,25 @@ maybeDropCxxExplicitObjectParameters(ArrayRef<const ParmVarDecl *> Params) {
372381
return Params;
373382
}
374383

384+
template <typename R, typename P>
385+
std::string joinAndTruncate(R &&Range, size_t MaxLength,
386+
P &&GetAsStringFunction) {
387+
std::string Out;
388+
llvm::raw_string_ostream OS(Out);
389+
llvm::ListSeparator Sep(", ");
390+
for (auto &&Element : Range) {
391+
OS << Sep;
392+
auto AsString = GetAsStringFunction(Element);
393+
if (Out.size() + AsString.size() >= MaxLength) {
394+
OS << "...";
395+
break;
396+
}
397+
OS << AsString;
398+
}
399+
OS.flush();
400+
return Out;
401+
}
402+
375403
struct Callee {
376404
// Only one of Decl or Loc is set.
377405
// Loc is for calls through function pointers.
@@ -422,7 +450,8 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
422450
Callee.Decl = E->getConstructor();
423451
if (!Callee.Decl)
424452
return true;
425-
processCall(Callee, {E->getArgs(), E->getNumArgs()});
453+
processCall(Callee, E->getParenOrBraceRange().getEnd(),
454+
{E->getArgs(), E->getNumArgs()});
426455
return true;
427456
}
428457

@@ -495,7 +524,7 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
495524
dyn_cast_or_null<CXXMethodDecl>(Callee.Decl))
496525
if (IsFunctor || Method->hasCXXExplicitFunctionObjectParameter())
497526
Args = Args.drop_front(1);
498-
processCall(Callee, Args);
527+
processCall(Callee, E->getRParenLoc(), Args);
499528
return true;
500529
}
501530

@@ -709,7 +738,8 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
709738
private:
710739
using NameVec = SmallVector<StringRef, 8>;
711740

712-
void processCall(Callee Callee, llvm::ArrayRef<const Expr *> Args) {
741+
void processCall(Callee Callee, SourceRange LParenOrBraceRange,
742+
llvm::ArrayRef<const Expr *> Args) {
713743
assert(Callee.Decl || Callee.Loc);
714744

715745
if (!Cfg.InlayHints.Parameters || Args.size() == 0)
@@ -721,6 +751,9 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
721751
if (Ctor->isCopyOrMoveConstructor())
722752
return;
723753

754+
SmallVector<std::string> FormattedDefaultArgs;
755+
bool HasNonDefaultArgs = false;
756+
724757
ArrayRef<const ParmVarDecl *> Params, ForwardedParams;
725758
// Resolve parameter packs to their forwarded parameter
726759
SmallVector<const ParmVarDecl *> ForwardedParamsStorage;
@@ -755,12 +788,33 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
755788
bool NameHint = shouldHintName(Args[I], Name);
756789
bool ReferenceHint = shouldHintReference(Params[I], ForwardedParams[I]);
757790

791+
bool IsDefault = isa<CXXDefaultArgExpr>(Args[I]);
792+
HasNonDefaultArgs |= !IsDefault;
793+
if (Cfg.InlayHints.DefaultArguments && IsDefault) {
794+
auto SourceText = Lexer::getSourceText(
795+
CharSourceRange::getTokenRange(Params[I]->getDefaultArgRange()),
796+
AST.getSourceManager(), AST.getLangOpts());
797+
FormattedDefaultArgs.emplace_back(llvm::formatv(
798+
"{0}: {1}", Name,
799+
SourceText.size() > Cfg.InlayHints.TypeNameLimit ? "..."
800+
: SourceText));
801+
}
802+
758803
if (NameHint || ReferenceHint) {
759804
addInlayHint(Args[I]->getSourceRange(), HintSide::Left,
760805
InlayHintKind::Parameter, ReferenceHint ? "&" : "",
761806
NameHint ? Name : "", ": ");
762807
}
763808
}
809+
810+
if (!FormattedDefaultArgs.empty()) {
811+
std::string Hint =
812+
joinAndTruncate(FormattedDefaultArgs, Cfg.InlayHints.TypeNameLimit,
813+
[](const auto &E) { return E; });
814+
addInlayHint(LParenOrBraceRange, HintSide::Left,
815+
InlayHintKind::DefaultArgument,
816+
HasNonDefaultArgs ? ", " : "", Hint, "");
817+
}
764818
}
765819

766820
static bool isSetter(const FunctionDecl *Callee, const NameVec &ParamNames) {
@@ -968,6 +1022,7 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
9681022
CHECK_KIND(Type, DeducedTypes);
9691023
CHECK_KIND(Designator, Designators);
9701024
CHECK_KIND(BlockEnd, BlockEnd);
1025+
CHECK_KIND(DefaultArgument, DefaultArguments);
9711026
#undef CHECK_KIND
9721027
}
9731028

clang-tools-extra/clangd/Protocol.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,6 +1477,7 @@ llvm::json::Value toJSON(const InlayHintKind &Kind) {
14771477
return 2;
14781478
case InlayHintKind::Designator:
14791479
case InlayHintKind::BlockEnd:
1480+
case InlayHintKind::DefaultArgument:
14801481
// This is an extension, don't serialize.
14811482
return nullptr;
14821483
}
@@ -1517,6 +1518,8 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, InlayHintKind Kind) {
15171518
return "designator";
15181519
case InlayHintKind::BlockEnd:
15191520
return "block-end";
1521+
case InlayHintKind::DefaultArgument:
1522+
return "default-argument";
15201523
}
15211524
llvm_unreachable("Unknown clang.clangd.InlayHintKind");
15221525
};

clang-tools-extra/clangd/Protocol.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,6 +1681,15 @@ enum class InlayHintKind {
16811681
/// This is a clangd extension.
16821682
BlockEnd = 4,
16831683

1684+
/// An inlay hint that is for a default argument.
1685+
///
1686+
/// An example of a parameter hint for a default argument:
1687+
/// void foo(bool A = true);
1688+
/// foo(^);
1689+
/// Adds an inlay hint "A = true".
1690+
/// This is a clangd extension.
1691+
DefaultArgument = 6,
1692+
16841693
/// Other ideas for hints that are not currently implemented:
16851694
///
16861695
/// * Chaining hints, showing the types of intermediate expressions

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
#include "support/Context.h"
1616
#include "llvm/ADT/StringRef.h"
1717
#include "llvm/Support/ScopedPrinter.h"
18+
#include "llvm/Support/raw_ostream.h"
1819
#include "gmock/gmock.h"
1920
#include "gtest/gtest.h"
21+
#include <optional>
2022
#include <string>
23+
#include <utility>
2124
#include <vector>
2225

2326
namespace clang {
@@ -81,6 +84,7 @@ Config noHintsConfig() {
8184
C.InlayHints.DeducedTypes = false;
8285
C.InlayHints.Designators = false;
8386
C.InlayHints.BlockEnd = false;
87+
C.InlayHints.DefaultArguments = false;
8488
return C;
8589
}
8690

@@ -1465,6 +1469,31 @@ TEST(TypeHints, DefaultTemplateArgs) {
14651469
ExpectedHint{": A<float>", "binding"});
14661470
}
14671471

1472+
TEST(DefaultArguments, Smoke) {
1473+
Config Cfg;
1474+
Cfg.InlayHints.Parameters =
1475+
true; // To test interplay of parameters and default parameters
1476+
Cfg.InlayHints.DeducedTypes = false;
1477+
Cfg.InlayHints.Designators = false;
1478+
Cfg.InlayHints.BlockEnd = false;
1479+
1480+
Cfg.InlayHints.DefaultArguments = true;
1481+
WithContextValue WithCfg(Config::Key, std::move(Cfg));
1482+
1483+
const auto *Code = R"cpp(
1484+
int foo(int A = 4) { return A; }
1485+
int bar(int A, int B = 1, bool C = foo($default1[[)]]) { return A; }
1486+
int A = bar($explicit[[2]]$default2[[)]];
1487+
)cpp";
1488+
1489+
assertHints(InlayHintKind::DefaultArgument, Code,
1490+
ExpectedHint{"A: 4", "default1", Left},
1491+
ExpectedHint{", B: 1, C: foo()", "default2", Left});
1492+
1493+
assertHints(InlayHintKind::Parameter, Code,
1494+
ExpectedHint{"A: ", "explicit", Left});
1495+
}
1496+
14681497
TEST(TypeHints, Deduplication) {
14691498
assertTypeHints(R"cpp(
14701499
template <typename T>

0 commit comments

Comments
 (0)