1 //===--- InlayHints.cpp ------------------------------------------*- C++-*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
8 #include "InlayHints.h"
9 #include "../clang-tidy/utils/DesignatedInitializers.h"
12 #include "HeuristicResolver.h"
13 #include "ParsedAST.h"
14 #include "SourceCode.h"
15 #include "clang/AST/ASTDiagnostic.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/AST/DeclarationName.h"
18 #include "clang/AST/Expr.h"
19 #include "clang/AST/ExprCXX.h"
20 #include "clang/AST/RecursiveASTVisitor.h"
21 #include "clang/AST/Stmt.h"
22 #include "clang/AST/StmtVisitor.h"
23 #include "clang/AST/Type.h"
24 #include "clang/Basic/Builtins.h"
25 #include "clang/Basic/OperatorKinds.h"
26 #include "clang/Basic/SourceManager.h"
27 #include "llvm/ADT/DenseSet.h"
28 #include "llvm/ADT/StringExtras.h"
29 #include "llvm/ADT/StringRef.h"
30 #include "llvm/ADT/Twine.h"
31 #include "llvm/Support/Casting.h"
32 #include "llvm/Support/SaveAndRestore.h"
33 #include "llvm/Support/ScopedPrinter.h"
34 #include "llvm/Support/raw_ostream.h"
42 // For now, inlay hints are always anchored at the left or right of their range.
43 enum class HintSide
{ Left
, Right
};
45 void stripLeadingUnderscores(StringRef
&Name
) { Name
= Name
.ltrim('_'); }
47 // getDeclForType() returns the decl responsible for Type's spelling.
48 // This is the inverse of ASTContext::getTypeDeclType().
49 template <typename Ty
, typename
= decltype(((Ty
*)nullptr)->getDecl())>
50 const NamedDecl
*getDeclForTypeImpl(const Ty
*T
) {
53 const NamedDecl
*getDeclForTypeImpl(const void *T
) { return nullptr; }
54 const NamedDecl
*getDeclForType(const Type
*T
) {
55 switch (T
->getTypeClass()) {
56 #define ABSTRACT_TYPE(TY, BASE)
57 #define TYPE(TY, BASE) \
59 return getDeclForTypeImpl(llvm::cast<TY##Type>(T));
60 #include "clang/AST/TypeNodes.inc"
62 llvm_unreachable("Unknown TypeClass enum");
65 // getSimpleName() returns the plain identifier for an entity, if any.
66 llvm::StringRef
getSimpleName(const DeclarationName
&DN
) {
67 if (IdentifierInfo
*Ident
= DN
.getAsIdentifierInfo())
68 return Ident
->getName();
71 llvm::StringRef
getSimpleName(const NamedDecl
&D
) {
72 return getSimpleName(D
.getDeclName());
74 llvm::StringRef
getSimpleName(QualType T
) {
75 if (const auto *ET
= llvm::dyn_cast
<ElaboratedType
>(T
))
76 return getSimpleName(ET
->getNamedType());
77 if (const auto *BT
= llvm::dyn_cast
<BuiltinType
>(T
)) {
78 PrintingPolicy
PP(LangOptions
{});
79 PP
.adjustForCPlusPlus();
80 return BT
->getName(PP
);
82 if (const auto *D
= getDeclForType(T
.getTypePtr()))
83 return getSimpleName(D
->getDeclName());
87 // Returns a very abbreviated form of an expression, or "" if it's too complex.
88 // For example: `foo->bar()` would produce "bar".
89 // This is used to summarize e.g. the condition of a while loop.
90 std::string
summarizeExpr(const Expr
*E
) {
91 struct Namer
: ConstStmtVisitor
<Namer
, std::string
> {
92 std::string
Visit(const Expr
*E
) {
95 return ConstStmtVisitor::Visit(E
->IgnoreImplicit());
98 // Any sort of decl reference, we just use the unqualified name.
99 std::string
VisitMemberExpr(const MemberExpr
*E
) {
100 return getSimpleName(*E
->getMemberDecl()).str();
102 std::string
VisitDeclRefExpr(const DeclRefExpr
*E
) {
103 return getSimpleName(*E
->getFoundDecl()).str();
105 std::string
VisitCallExpr(const CallExpr
*E
) {
106 return Visit(E
->getCallee());
109 VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr
*E
) {
110 return getSimpleName(E
->getMember()).str();
113 VisitDependentScopeDeclRefExpr(const DependentScopeDeclRefExpr
*E
) {
114 return getSimpleName(E
->getDeclName()).str();
116 std::string
VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr
*E
) {
117 return getSimpleName(E
->getType()).str();
119 std::string
VisitCXXTemporaryObjectExpr(const CXXTemporaryObjectExpr
*E
) {
120 return getSimpleName(E
->getType()).str();
123 // Step through implicit nodes that clang doesn't classify as such.
124 std::string
VisitCXXMemberCallExpr(const CXXMemberCallExpr
*E
) {
125 // Call to operator bool() inside if (X): dispatch to X.
126 if (E
->getNumArgs() == 0 && E
->getMethodDecl() &&
127 E
->getMethodDecl()->getDeclName().getNameKind() ==
128 DeclarationName::CXXConversionFunctionName
&&
129 E
->getSourceRange() ==
130 E
->getImplicitObjectArgument()->getSourceRange())
131 return Visit(E
->getImplicitObjectArgument());
132 return ConstStmtVisitor::VisitCXXMemberCallExpr(E
);
134 std::string
VisitCXXConstructExpr(const CXXConstructExpr
*E
) {
135 if (E
->getNumArgs() == 1)
136 return Visit(E
->getArg(0));
140 // Literals are just printed
141 std::string
VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr
*E
) {
142 return E
->getValue() ? "true" : "false";
144 std::string
VisitIntegerLiteral(const IntegerLiteral
*E
) {
145 return llvm::to_string(E
->getValue());
147 std::string
VisitFloatingLiteral(const FloatingLiteral
*E
) {
149 llvm::raw_string_ostream
OS(Result
);
150 E
->getValue().print(OS
);
151 // Printer adds newlines?!
152 Result
.resize(llvm::StringRef(Result
).rtrim().size());
155 std::string
VisitStringLiteral(const StringLiteral
*E
) {
156 std::string Result
= "\"";
157 if (E
->containsNonAscii()) {
159 } else if (E
->getLength() > 10) {
160 Result
+= E
->getString().take_front(7);
163 llvm::raw_string_ostream
OS(Result
);
164 llvm::printEscapedString(E
->getString(), OS
);
166 Result
.push_back('"');
170 // Simple operators. Motivating cases are `!x` and `I < Length`.
171 std::string
printUnary(llvm::StringRef Spelling
, const Expr
*Operand
,
173 std::string Sub
= Visit(Operand
);
177 return (Spelling
+ Sub
).str();
181 bool InsideBinary
= false; // No recursing into binary expressions.
182 std::string
printBinary(llvm::StringRef Spelling
, const Expr
*LHSOp
,
186 llvm::SaveAndRestore
InBinary(InsideBinary
, true);
188 std::string LHS
= Visit(LHSOp
);
189 std::string RHS
= Visit(RHSOp
);
190 if (LHS
.empty() && RHS
.empty())
204 std::string
VisitUnaryOperator(const UnaryOperator
*E
) {
205 return printUnary(E
->getOpcodeStr(E
->getOpcode()), E
->getSubExpr(),
208 std::string
VisitBinaryOperator(const BinaryOperator
*E
) {
209 return printBinary(E
->getOpcodeStr(E
->getOpcode()), E
->getLHS(),
212 std::string
VisitCXXOperatorCallExpr(const CXXOperatorCallExpr
*E
) {
213 const char *Spelling
= getOperatorSpelling(E
->getOperator());
214 // Handle weird unary-that-look-like-binary postfix operators.
215 if ((E
->getOperator() == OO_PlusPlus
||
216 E
->getOperator() == OO_MinusMinus
) &&
217 E
->getNumArgs() == 2)
218 return printUnary(Spelling
, E
->getArg(0), false);
219 if (E
->isInfixBinaryOp())
220 return printBinary(Spelling
, E
->getArg(0), E
->getArg(1));
221 if (E
->getNumArgs() == 1) {
222 switch (E
->getOperator()) {
231 return printUnary(Spelling
, E
->getArg(0), true);
239 return Namer
{}.Visit(E
);
242 // Determines if any intermediate type in desugaring QualType QT is of
243 // substituted template parameter type. Ignore pointer or reference wrappers.
244 bool isSugaredTemplateParameter(QualType QT
) {
245 static auto PeelWrapper
= [](QualType QT
) {
246 // Neither `PointerType` nor `ReferenceType` is considered as sugared
248 QualType Peeled
= QT
->getPointeeType();
249 return Peeled
.isNull() ? QT
: Peeled
;
252 // This is a bit tricky: we traverse the type structure and find whether or
253 // not a type in the desugaring process is of SubstTemplateTypeParmType.
254 // During the process, we may encounter pointer or reference types that are
255 // not marked as sugared; therefore, the desugar function won't apply. To
256 // move forward the traversal, we retrieve the pointees using
257 // QualType::getPointeeType().
259 // However, getPointeeType could leap over our interests: The QT::getAs<T>()
260 // invoked would implicitly desugar the type. Consequently, if the
261 // SubstTemplateTypeParmType is encompassed within a TypedefType, we may lose
262 // the chance to visit it.
263 // For example, given a QT that represents `std::vector<int *>::value_type`:
264 // `-ElaboratedType 'value_type' sugar
265 // `-TypedefType 'vector<int *>::value_type' sugar
266 // |-Typedef 'value_type'
267 // `-SubstTemplateTypeParmType 'int *' sugar class depth 0 index 0 T
268 // |-ClassTemplateSpecialization 'vector'
269 // `-PointerType 'int *'
270 // `-BuiltinType 'int'
271 // Applying `getPointeeType` to QT results in 'int', a child of our target
272 // node SubstTemplateTypeParmType.
274 // As such, we always prefer the desugared over the pointee for next type
275 // in the iteration. It could avoid the getPointeeType's implicit desugaring.
277 if (QT
->getAs
<SubstTemplateTypeParmType
>())
279 QualType Desugared
= QT
->getLocallyUnqualifiedSingleStepDesugaredType();
282 else if (auto Peeled
= PeelWrapper(Desugared
); Peeled
!= QT
)
290 // A simple wrapper for `clang::desugarForDiagnostic` that provides optional
292 std::optional
<QualType
> desugar(ASTContext
&AST
, QualType QT
) {
293 bool ShouldAKA
= false;
294 auto Desugared
= clang::desugarForDiagnostic(AST
, QT
, ShouldAKA
);
300 // Apply a series of heuristic methods to determine whether or not a QualType QT
301 // is suitable for desugaring (e.g. getting the real name behind the using-alias
302 // name). If so, return the desugared type. Otherwise, return the unchanged
305 // This could be refined further. See
306 // https://github.com/clangd/clangd/issues/1298.
307 QualType
maybeDesugar(ASTContext
&AST
, QualType QT
) {
308 // Prefer desugared type for name that aliases the template parameters.
309 // This can prevent things like printing opaque `: type` when accessing std
311 if (isSugaredTemplateParameter(QT
))
312 return desugar(AST
, QT
).value_or(QT
);
314 // Prefer desugared type for `decltype(expr)` specifiers.
315 if (QT
->isDecltypeType())
316 return QT
.getCanonicalType();
317 if (const AutoType
*AT
= QT
->getContainedAutoType())
318 if (!AT
->getDeducedType().isNull() &&
319 AT
->getDeducedType()->isDecltypeType())
320 return QT
.getCanonicalType();
325 // Given a callee expression `Fn`, if the call is through a function pointer,
326 // try to find the declaration of the corresponding function pointer type,
327 // so that we can recover argument names from it.
328 // FIXME: This function is mostly duplicated in SemaCodeComplete.cpp; unify.
329 static FunctionProtoTypeLoc
getPrototypeLoc(Expr
*Fn
) {
331 Expr
*NakedFn
= Fn
->IgnoreParenCasts();
332 if (const auto *T
= NakedFn
->getType().getTypePtr()->getAs
<TypedefType
>()) {
333 Target
= T
->getDecl()->getTypeSourceInfo()->getTypeLoc();
334 } else if (const auto *DR
= dyn_cast
<DeclRefExpr
>(NakedFn
)) {
335 const auto *D
= DR
->getDecl();
336 if (const auto *const VD
= dyn_cast
<VarDecl
>(D
)) {
337 Target
= VD
->getTypeSourceInfo()->getTypeLoc();
344 // Unwrap types that may be wrapping the function type
346 if (auto P
= Target
.getAs
<PointerTypeLoc
>()) {
347 Target
= P
.getPointeeLoc();
350 if (auto A
= Target
.getAs
<AttributedTypeLoc
>()) {
351 Target
= A
.getModifiedLoc();
354 if (auto P
= Target
.getAs
<ParenTypeLoc
>()) {
355 Target
= P
.getInnerLoc();
361 if (auto F
= Target
.getAs
<FunctionProtoTypeLoc
>()) {
368 ArrayRef
<const ParmVarDecl
*>
369 maybeDropCxxExplicitObjectParameters(ArrayRef
<const ParmVarDecl
*> Params
) {
370 if (!Params
.empty() && Params
.front()->isExplicitObjectParameter())
371 Params
= Params
.drop_front(1);
376 // Only one of Decl or Loc is set.
377 // Loc is for calls through function pointers.
378 const FunctionDecl
*Decl
= nullptr;
379 FunctionProtoTypeLoc Loc
;
382 class InlayHintVisitor
: public RecursiveASTVisitor
<InlayHintVisitor
> {
384 InlayHintVisitor(std::vector
<InlayHint
> &Results
, ParsedAST
&AST
,
385 const Config
&Cfg
, std::optional
<Range
> RestrictRange
)
386 : Results(Results
), AST(AST
.getASTContext()), Tokens(AST
.getTokens()),
387 Cfg(Cfg
), RestrictRange(std::move(RestrictRange
)),
388 MainFileID(AST
.getSourceManager().getMainFileID()),
389 Resolver(AST
.getHeuristicResolver()),
390 TypeHintPolicy(this->AST
.getPrintingPolicy()) {
391 bool Invalid
= false;
392 llvm::StringRef Buf
=
393 AST
.getSourceManager().getBufferData(MainFileID
, &Invalid
);
394 MainFileBuf
= Invalid
? StringRef
{} : Buf
;
396 TypeHintPolicy
.SuppressScope
= true; // keep type names short
397 TypeHintPolicy
.AnonymousTagLocations
=
398 false; // do not print lambda locations
400 // Not setting PrintCanonicalTypes for "auto" allows
401 // SuppressDefaultTemplateArgs (set by default) to have an effect.
404 bool VisitTypeLoc(TypeLoc TL
) {
405 if (const auto *DT
= llvm::dyn_cast
<DecltypeType
>(TL
.getType()))
406 if (QualType UT
= DT
->getUnderlyingType(); !UT
->isDependentType())
407 addTypeHint(TL
.getSourceRange(), UT
, ": ");
411 bool VisitCXXConstructExpr(CXXConstructExpr
*E
) {
412 // Weed out constructor calls that don't look like a function call with
413 // an argument list, by checking the validity of getParenOrBraceRange().
414 // Also weed out std::initializer_list constructors as there are no names
415 // for the individual arguments.
416 if (!E
->getParenOrBraceRange().isValid() ||
417 E
->isStdInitListInitialization()) {
422 Callee
.Decl
= E
->getConstructor();
425 processCall(Callee
, {E
->getArgs(), E
->getNumArgs()});
429 // Carefully recurse into PseudoObjectExprs, which typically incorporate
430 // a syntactic expression and several semantic expressions.
431 bool TraversePseudoObjectExpr(PseudoObjectExpr
*E
) {
432 Expr
*SyntacticExpr
= E
->getSyntacticForm();
433 if (isa
<CallExpr
>(SyntacticExpr
))
434 // Since the counterpart semantics usually get the identical source
435 // locations as the syntactic one, visiting those would end up presenting
436 // confusing hints e.g., __builtin_dump_struct.
437 // Thus, only traverse the syntactic forms if this is written as a
438 // CallExpr. This leaves the door open in case the arguments in the
439 // syntactic form could possibly get parameter names.
440 return RecursiveASTVisitor
<InlayHintVisitor
>::TraverseStmt(SyntacticExpr
);
441 // We don't want the hints for some of the MS property extensions.
444 // __declspec(property(get=GetX, put=PutX)) int x[];
446 // void Work(int y) { x = y; } // Bad: `x = y: y`.
448 if (isa
<BinaryOperator
>(SyntacticExpr
))
450 // FIXME: Handle other forms of a pseudo object expression.
451 return RecursiveASTVisitor
<InlayHintVisitor
>::TraversePseudoObjectExpr(E
);
454 bool VisitCallExpr(CallExpr
*E
) {
455 if (!Cfg
.InlayHints
.Parameters
)
458 bool IsFunctor
= isFunctionObjectCallExpr(E
);
459 // Do not show parameter hints for user-defined literals or
460 // operator calls except for operator(). (Among other reasons, the resulting
461 // hints can look awkward, e.g. the expression can itself be a function
462 // argument and then we'd get two hints side by side).
463 if ((isa
<CXXOperatorCallExpr
>(E
) && !IsFunctor
) ||
464 isa
<UserDefinedLiteral
>(E
))
467 auto CalleeDecls
= Resolver
->resolveCalleeOfCallExpr(E
);
468 if (CalleeDecls
.size() != 1)
472 if (const auto *FD
= dyn_cast
<FunctionDecl
>(CalleeDecls
[0]))
474 else if (const auto *FTD
= dyn_cast
<FunctionTemplateDecl
>(CalleeDecls
[0]))
475 Callee
.Decl
= FTD
->getTemplatedDecl();
476 else if (FunctionProtoTypeLoc Loc
= getPrototypeLoc(E
->getCallee()))
481 // N4868 [over.call.object]p3 says,
482 // The argument list submitted to overload resolution consists of the
483 // argument expressions present in the function call syntax preceded by the
484 // implied object argument (E).
486 // As well as the provision from P0847R7 Deducing This [expr.call]p7:
487 // ...If the function is an explicit object member function and there is an
488 // implied object argument ([over.call.func]), the list of provided
489 // arguments is preceded by the implied object argument for the purposes of
490 // this correspondence...
491 llvm::ArrayRef
<const Expr
*> Args
= {E
->getArgs(), E
->getNumArgs()};
492 // We don't have the implied object argument through a function pointer
494 if (const CXXMethodDecl
*Method
=
495 dyn_cast_or_null
<CXXMethodDecl
>(Callee
.Decl
))
496 if (IsFunctor
|| Method
->hasCXXExplicitFunctionObjectParameter())
497 Args
= Args
.drop_front(1);
498 processCall(Callee
, Args
);
502 bool VisitFunctionDecl(FunctionDecl
*D
) {
504 llvm::dyn_cast
<FunctionProtoType
>(D
->getType().getTypePtr())) {
505 if (!FPT
->hasTrailingReturn()) {
506 if (auto FTL
= D
->getFunctionTypeLoc())
507 addReturnTypeHint(D
, FTL
.getRParenLoc());
510 if (Cfg
.InlayHints
.BlockEnd
&& D
->isThisDeclarationADefinition()) {
511 // We use `printName` here to properly print name of ctor/dtor/operator
513 if (const Stmt
*Body
= D
->getBody())
514 addBlockEndHint(Body
->getSourceRange(), "", printName(AST
, *D
), "");
519 bool VisitForStmt(ForStmt
*S
) {
520 if (Cfg
.InlayHints
.BlockEnd
) {
522 // Common case: for (int I = 0; I < N; I++). Use "I" as the name.
523 if (auto *DS
= llvm::dyn_cast_or_null
<DeclStmt
>(S
->getInit());
524 DS
&& DS
->isSingleDecl())
525 Name
= getSimpleName(llvm::cast
<NamedDecl
>(*DS
->getSingleDecl()));
527 Name
= summarizeExpr(S
->getCond());
528 markBlockEnd(S
->getBody(), "for", Name
);
533 bool VisitCXXForRangeStmt(CXXForRangeStmt
*S
) {
534 if (Cfg
.InlayHints
.BlockEnd
)
535 markBlockEnd(S
->getBody(), "for", getSimpleName(*S
->getLoopVariable()));
539 bool VisitWhileStmt(WhileStmt
*S
) {
540 if (Cfg
.InlayHints
.BlockEnd
)
541 markBlockEnd(S
->getBody(), "while", summarizeExpr(S
->getCond()));
545 bool VisitSwitchStmt(SwitchStmt
*S
) {
546 if (Cfg
.InlayHints
.BlockEnd
)
547 markBlockEnd(S
->getBody(), "switch", summarizeExpr(S
->getCond()));
551 // If/else chains are tricky.
553 // } else if (cond2) {
554 // } // mark as "cond1" or "cond2"?
555 // For now, the answer is neither, just mark as "if".
556 // The ElseIf is a different IfStmt that doesn't know about the outer one.
557 llvm::DenseSet
<const IfStmt
*> ElseIfs
; // not eligible for names
558 bool VisitIfStmt(IfStmt
*S
) {
559 if (Cfg
.InlayHints
.BlockEnd
) {
560 if (const auto *ElseIf
= llvm::dyn_cast_or_null
<IfStmt
>(S
->getElse()))
561 ElseIfs
.insert(ElseIf
);
562 // Don't use markBlockEnd: the relevant range is [then.begin, else.end].
563 if (const auto *EndCS
= llvm::dyn_cast
<CompoundStmt
>(
564 S
->getElse() ? S
->getElse() : S
->getThen())) {
566 {S
->getThen()->getBeginLoc(), EndCS
->getRBracLoc()}, "if",
567 ElseIfs
.contains(S
) ? "" : summarizeExpr(S
->getCond()), "");
573 void markBlockEnd(const Stmt
*Body
, llvm::StringRef Label
,
574 llvm::StringRef Name
= "") {
575 if (const auto *CS
= llvm::dyn_cast_or_null
<CompoundStmt
>(Body
))
576 addBlockEndHint(CS
->getSourceRange(), Label
, Name
, "");
579 bool VisitTagDecl(TagDecl
*D
) {
580 if (Cfg
.InlayHints
.BlockEnd
&& D
->isThisDeclarationADefinition()) {
581 std::string DeclPrefix
= D
->getKindName().str();
582 if (const auto *ED
= dyn_cast
<EnumDecl
>(D
)) {
584 DeclPrefix
+= ED
->isScopedUsingClassTag() ? " class" : " struct";
586 addBlockEndHint(D
->getBraceRange(), DeclPrefix
, getSimpleName(*D
), ";");
591 bool VisitNamespaceDecl(NamespaceDecl
*D
) {
592 if (Cfg
.InlayHints
.BlockEnd
) {
593 // For namespace, the range actually starts at the namespace keyword. But
594 // it should be fine since it's usually very short.
595 addBlockEndHint(D
->getSourceRange(), "namespace", getSimpleName(*D
), "");
600 bool VisitLambdaExpr(LambdaExpr
*E
) {
601 FunctionDecl
*D
= E
->getCallOperator();
602 if (!E
->hasExplicitResultType())
603 addReturnTypeHint(D
, E
->hasExplicitParameters()
604 ? D
->getFunctionTypeLoc().getRParenLoc()
605 : E
->getIntroducerRange().getEnd());
609 void addReturnTypeHint(FunctionDecl
*D
, SourceRange Range
) {
610 auto *AT
= D
->getReturnType()->getContainedAutoType();
611 if (!AT
|| AT
->getDeducedType().isNull())
613 addTypeHint(Range
, D
->getReturnType(), /*Prefix=*/"-> ");
616 bool VisitVarDecl(VarDecl
*D
) {
617 // Do not show hints for the aggregate in a structured binding,
618 // but show hints for the individual bindings.
619 if (auto *DD
= dyn_cast
<DecompositionDecl
>(D
)) {
620 for (auto *Binding
: DD
->bindings()) {
621 // For structured bindings, print canonical types. This is important
622 // because for bindings that use the tuple_element protocol, the
623 // non-canonical types would be "tuple_element<I, A>::type".
624 if (auto Type
= Binding
->getType();
625 !Type
.isNull() && !Type
->isDependentType())
626 addTypeHint(Binding
->getLocation(), Type
.getCanonicalType(),
632 if (auto *AT
= D
->getType()->getContainedAutoType()) {
633 if (AT
->isDeduced() && !D
->getType()->isDependentType()) {
634 // Our current approach is to place the hint on the variable
635 // and accordingly print the full type
636 // (e.g. for `const auto& x = 42`, print `const int&`).
637 // Alternatively, we could place the hint on the `auto`
638 // (and then just print the type deduced for the `auto`).
639 addTypeHint(D
->getLocation(), D
->getType(), /*Prefix=*/": ");
643 // Handle templates like `int foo(auto x)` with exactly one instantiation.
644 if (auto *PVD
= llvm::dyn_cast
<ParmVarDecl
>(D
)) {
645 if (D
->getIdentifier() && PVD
->getType()->isDependentType() &&
646 !getContainedAutoParamType(D
->getTypeSourceInfo()->getTypeLoc())
648 if (auto *IPVD
= getOnlyParamInstantiation(PVD
))
649 addTypeHint(D
->getLocation(), IPVD
->getType(), /*Prefix=*/": ");
656 ParmVarDecl
*getOnlyParamInstantiation(ParmVarDecl
*D
) {
657 auto *TemplateFunction
= llvm::dyn_cast
<FunctionDecl
>(D
->getDeclContext());
658 if (!TemplateFunction
)
660 auto *InstantiatedFunction
= llvm::dyn_cast_or_null
<FunctionDecl
>(
661 getOnlyInstantiation(TemplateFunction
));
662 if (!InstantiatedFunction
)
665 unsigned ParamIdx
= 0;
666 for (auto *Param
: TemplateFunction
->parameters()) {
667 // Can't reason about param indexes in the presence of preceding packs.
668 // And if this param is a pack, it may expand to multiple params.
669 if (Param
->isParameterPack())
675 assert(ParamIdx
< TemplateFunction
->getNumParams() &&
676 "Couldn't find param in list?");
677 assert(ParamIdx
< InstantiatedFunction
->getNumParams() &&
678 "Instantiated function has fewer (non-pack) parameters?");
679 return InstantiatedFunction
->getParamDecl(ParamIdx
);
682 bool VisitInitListExpr(InitListExpr
*Syn
) {
683 // We receive the syntactic form here (shouldVisitImplicitCode() is false).
684 // This is the one we will ultimately attach designators to.
685 // It may have subobject initializers inlined without braces. The *semantic*
686 // form of the init-list has nested init-lists for these.
687 // getUnwrittenDesignators will look at the semantic form to determine the
689 assert(Syn
->isSyntacticForm() && "RAV should not visit implicit code!");
690 if (!Cfg
.InlayHints
.Designators
)
692 if (Syn
->isIdiomaticZeroInitializer(AST
.getLangOpts()))
694 llvm::DenseMap
<SourceLocation
, std::string
> Designators
=
695 tidy::utils::getUnwrittenDesignators(Syn
);
696 for (const Expr
*Init
: Syn
->inits()) {
697 if (llvm::isa
<DesignatedInitExpr
>(Init
))
699 auto It
= Designators
.find(Init
->getBeginLoc());
700 if (It
!= Designators
.end() &&
701 !isPrecededByParamNameComment(Init
, It
->second
))
702 addDesignatorHint(Init
->getSourceRange(), It
->second
);
707 // FIXME: Handle RecoveryExpr to try to hint some invalid calls.
710 using NameVec
= SmallVector
<StringRef
, 8>;
712 void processCall(Callee Callee
, llvm::ArrayRef
<const Expr
*> Args
) {
713 assert(Callee
.Decl
|| Callee
.Loc
);
715 if (!Cfg
.InlayHints
.Parameters
|| Args
.size() == 0)
718 // The parameter name of a move or copy constructor is not very interesting.
720 if (auto *Ctor
= dyn_cast
<CXXConstructorDecl
>(Callee
.Decl
))
721 if (Ctor
->isCopyOrMoveConstructor())
724 ArrayRef
<const ParmVarDecl
*> Params
, ForwardedParams
;
725 // Resolve parameter packs to their forwarded parameter
726 SmallVector
<const ParmVarDecl
*> ForwardedParamsStorage
;
728 Params
= maybeDropCxxExplicitObjectParameters(Callee
.Decl
->parameters());
729 ForwardedParamsStorage
= resolveForwardingParameters(Callee
.Decl
);
731 maybeDropCxxExplicitObjectParameters(ForwardedParamsStorage
);
733 Params
= maybeDropCxxExplicitObjectParameters(Callee
.Loc
.getParams());
734 ForwardedParams
= {Params
.begin(), Params
.end()};
737 NameVec ParameterNames
= chooseParameterNames(ForwardedParams
);
739 // Exclude setters (i.e. functions with one argument whose name begins with
740 // "set"), and builtins like std::move/forward/... as their parameter name
741 // is also not likely to be interesting.
743 (isSetter(Callee
.Decl
, ParameterNames
) || isSimpleBuiltin(Callee
.Decl
)))
746 for (size_t I
= 0; I
< ParameterNames
.size() && I
< Args
.size(); ++I
) {
747 // Pack expansion expressions cause the 1:1 mapping between arguments and
748 // parameters to break down, so we don't add further inlay hints if we
750 if (isa
<PackExpansionExpr
>(Args
[I
])) {
754 StringRef Name
= ParameterNames
[I
];
755 bool NameHint
= shouldHintName(Args
[I
], Name
);
756 bool ReferenceHint
= shouldHintReference(Params
[I
], ForwardedParams
[I
]);
758 if (NameHint
|| ReferenceHint
) {
759 addInlayHint(Args
[I
]->getSourceRange(), HintSide::Left
,
760 InlayHintKind::Parameter
, ReferenceHint
? "&" : "",
761 NameHint
? Name
: "", ": ");
766 static bool isSetter(const FunctionDecl
*Callee
, const NameVec
&ParamNames
) {
767 if (ParamNames
.size() != 1)
770 StringRef Name
= getSimpleName(*Callee
);
771 if (!Name
.starts_with_insensitive("set"))
774 // In addition to checking that the function has one parameter and its
775 // name starts with "set", also check that the part after "set" matches
776 // the name of the parameter (ignoring case). The idea here is that if
777 // the parameter name differs, it may contain extra information that
778 // may be useful to show in a hint, as in:
779 // void setTimeout(int timeoutMillis);
780 // This currently doesn't handle cases where params use snake_case
781 // and functions don't, e.g.
782 // void setExceptionHandler(EHFunc exception_handler);
783 // We could improve this by replacing `equals_insensitive` with some
784 // `sloppy_equals` which ignores case and also skips underscores.
785 StringRef WhatItIsSetting
= Name
.substr(3).ltrim("_");
786 return WhatItIsSetting
.equals_insensitive(ParamNames
[0]);
789 // Checks if the callee is one of the builtins
790 // addressof, as_const, forward, move(_if_noexcept)
791 static bool isSimpleBuiltin(const FunctionDecl
*Callee
) {
792 switch (Callee
->getBuiltinID()) {
793 case Builtin::BIaddressof
:
794 case Builtin::BIas_const
:
795 case Builtin::BIforward
:
796 case Builtin::BImove
:
797 case Builtin::BImove_if_noexcept
:
804 bool shouldHintName(const Expr
*Arg
, StringRef ParamName
) {
805 if (ParamName
.empty())
808 // If the argument expression is a single name and it matches the
809 // parameter name exactly, omit the name hint.
810 if (ParamName
== getSpelledIdentifier(Arg
))
813 // Exclude argument expressions preceded by a /*paramName*/.
814 if (isPrecededByParamNameComment(Arg
, ParamName
))
820 bool shouldHintReference(const ParmVarDecl
*Param
,
821 const ParmVarDecl
*ForwardedParam
) {
822 // We add a & hint only when the argument is passed as mutable reference.
823 // For parameters that are not part of an expanded pack, this is
824 // straightforward. For expanded pack parameters, it's likely that they will
825 // be forwarded to another function. In this situation, we only want to add
826 // the reference hint if the argument is actually being used via mutable
827 // reference. This means we need to check
828 // 1. whether the value category of the argument is preserved, i.e. each
829 // pack expansion uses std::forward correctly.
830 // 2. whether the argument is ever copied/cast instead of passed
832 // Instead of checking this explicitly, we use the following proxy:
833 // 1. the value category can only change from rvalue to lvalue during
834 // forwarding, so checking whether both the parameter of the forwarding
835 // function and the forwarded function are lvalue references detects such
837 // 2. if the argument is copied/cast somewhere in the chain of forwarding
838 // calls, it can only be passed on to an rvalue reference or const lvalue
839 // reference parameter. Thus if the forwarded parameter is a mutable
840 // lvalue reference, it cannot have been copied/cast to on the way.
841 // Additionally, we should not add a reference hint if the forwarded
842 // parameter was only partially resolved, i.e. points to an expanded pack
843 // parameter, since we do not know how it will be used eventually.
844 auto Type
= Param
->getType();
845 auto ForwardedType
= ForwardedParam
->getType();
846 return Type
->isLValueReferenceType() &&
847 ForwardedType
->isLValueReferenceType() &&
848 !ForwardedType
.getNonReferenceType().isConstQualified() &&
849 !isExpandedFromParameterPack(ForwardedParam
);
852 // Checks if "E" is spelled in the main file and preceded by a C-style comment
853 // whose contents match ParamName (allowing for whitespace and an optional "="
855 bool isPrecededByParamNameComment(const Expr
*E
, StringRef ParamName
) {
856 auto &SM
= AST
.getSourceManager();
857 auto FileLoc
= SM
.getFileLoc(E
->getBeginLoc());
858 auto Decomposed
= SM
.getDecomposedLoc(FileLoc
);
859 if (Decomposed
.first
!= MainFileID
)
862 StringRef SourcePrefix
= MainFileBuf
.substr(0, Decomposed
.second
);
863 // Allow whitespace between comment and expression.
864 SourcePrefix
= SourcePrefix
.rtrim();
865 // Check for comment ending.
866 if (!SourcePrefix
.consume_back("*/"))
868 // Ignore some punctuation and whitespace around comment.
869 // In particular this allows designators to match nicely.
870 llvm::StringLiteral IgnoreChars
= " =.";
871 SourcePrefix
= SourcePrefix
.rtrim(IgnoreChars
);
872 ParamName
= ParamName
.trim(IgnoreChars
);
873 // Other than that, the comment must contain exactly ParamName.
874 if (!SourcePrefix
.consume_back(ParamName
))
876 SourcePrefix
= SourcePrefix
.rtrim(IgnoreChars
);
877 return SourcePrefix
.ends_with("/*");
880 // If "E" spells a single unqualified identifier, return that name.
881 // Otherwise, return an empty string.
882 static StringRef
getSpelledIdentifier(const Expr
*E
) {
883 E
= E
->IgnoreUnlessSpelledInSource();
885 if (auto *DRE
= dyn_cast
<DeclRefExpr
>(E
))
886 if (!DRE
->getQualifier())
887 return getSimpleName(*DRE
->getDecl());
889 if (auto *ME
= dyn_cast
<MemberExpr
>(E
))
890 if (!ME
->getQualifier() && ME
->isImplicitAccess())
891 return getSimpleName(*ME
->getMemberDecl());
896 NameVec
chooseParameterNames(ArrayRef
<const ParmVarDecl
*> Parameters
) {
897 NameVec ParameterNames
;
898 for (const auto *P
: Parameters
) {
899 if (isExpandedFromParameterPack(P
)) {
900 // If we haven't resolved a pack paramater (e.g. foo(Args... args)) to a
901 // non-pack parameter, then hinting as foo(args: 1, args: 2, args: 3) is
902 // unlikely to be useful.
903 ParameterNames
.emplace_back();
905 auto SimpleName
= getSimpleName(*P
);
906 // If the parameter is unnamed in the declaration:
907 // attempt to get its name from the definition
908 if (SimpleName
.empty()) {
909 if (const auto *PD
= getParamDefinition(P
)) {
910 SimpleName
= getSimpleName(*PD
);
913 ParameterNames
.emplace_back(SimpleName
);
917 // Standard library functions often have parameter names that start
918 // with underscores, which makes the hints noisy, so strip them out.
919 for (auto &Name
: ParameterNames
)
920 stripLeadingUnderscores(Name
);
922 return ParameterNames
;
925 // for a ParmVarDecl from a function declaration, returns the corresponding
926 // ParmVarDecl from the definition if possible, nullptr otherwise.
927 static const ParmVarDecl
*getParamDefinition(const ParmVarDecl
*P
) {
928 if (auto *Callee
= dyn_cast
<FunctionDecl
>(P
->getDeclContext())) {
929 if (auto *Def
= Callee
->getDefinition()) {
930 auto I
= std::distance(Callee
->param_begin(),
931 llvm::find(Callee
->parameters(), P
));
932 if (I
< (int)Callee
->getNumParams()) {
933 return Def
->getParamDecl(I
);
940 // We pass HintSide rather than SourceLocation because we want to ensure
941 // it is in the same file as the common file range.
942 void addInlayHint(SourceRange R
, HintSide Side
, InlayHintKind Kind
,
943 llvm::StringRef Prefix
, llvm::StringRef Label
,
944 llvm::StringRef Suffix
) {
945 auto LSPRange
= getHintRange(R
);
949 addInlayHint(*LSPRange
, Side
, Kind
, Prefix
, Label
, Suffix
);
952 void addInlayHint(Range LSPRange
, HintSide Side
, InlayHintKind Kind
,
953 llvm::StringRef Prefix
, llvm::StringRef Label
,
954 llvm::StringRef Suffix
) {
955 // We shouldn't get as far as adding a hint if the category is disabled.
956 // We'd like to disable as much of the analysis as possible above instead.
957 // Assert in debug mode but add a dynamic check in production.
958 assert(Cfg
.InlayHints
.Enabled
&& "Shouldn't get here if disabled!");
960 #define CHECK_KIND(Enumerator, ConfigProperty) \
961 case InlayHintKind::Enumerator: \
962 assert(Cfg.InlayHints.ConfigProperty && \
963 "Shouldn't get here if kind is disabled!"); \
964 if (!Cfg.InlayHints.ConfigProperty) \
967 CHECK_KIND(Parameter
, Parameters
);
968 CHECK_KIND(Type
, DeducedTypes
);
969 CHECK_KIND(Designator
, Designators
);
970 CHECK_KIND(BlockEnd
, BlockEnd
);
974 Position LSPPos
= Side
== HintSide::Left
? LSPRange
.start
: LSPRange
.end
;
976 (LSPPos
< RestrictRange
->start
|| !(LSPPos
< RestrictRange
->end
)))
978 bool PadLeft
= Prefix
.consume_front(" ");
979 bool PadRight
= Suffix
.consume_back(" ");
980 Results
.push_back(InlayHint
{LSPPos
,
981 /*label=*/{(Prefix
+ Label
+ Suffix
).str()},
982 Kind
, PadLeft
, PadRight
, LSPRange
});
985 // Get the range of the main file that *exactly* corresponds to R.
986 std::optional
<Range
> getHintRange(SourceRange R
) {
987 const auto &SM
= AST
.getSourceManager();
988 auto Spelled
= Tokens
.spelledForExpanded(Tokens
.expandedTokens(R
));
989 // TokenBuffer will return null if e.g. R corresponds to only part of a
991 if (!Spelled
|| Spelled
->empty())
993 // Hint must be within the main file, not e.g. a non-preamble include.
994 if (SM
.getFileID(Spelled
->front().location()) != SM
.getMainFileID() ||
995 SM
.getFileID(Spelled
->back().location()) != SM
.getMainFileID())
997 return Range
{sourceLocToPosition(SM
, Spelled
->front().location()),
998 sourceLocToPosition(SM
, Spelled
->back().endLocation())};
1001 void addTypeHint(SourceRange R
, QualType T
, llvm::StringRef Prefix
) {
1002 if (!Cfg
.InlayHints
.DeducedTypes
|| T
.isNull())
1005 // The sugared type is more useful in some cases, and the canonical
1006 // type in other cases.
1007 auto Desugared
= maybeDesugar(AST
, T
);
1008 std::string TypeName
= Desugared
.getAsString(TypeHintPolicy
);
1009 if (T
!= Desugared
&& !shouldPrintTypeHint(TypeName
)) {
1010 // If the desugared type is too long to display, fallback to the sugared
1012 TypeName
= T
.getAsString(TypeHintPolicy
);
1014 if (shouldPrintTypeHint(TypeName
))
1015 addInlayHint(R
, HintSide::Right
, InlayHintKind::Type
, Prefix
, TypeName
,
1019 void addDesignatorHint(SourceRange R
, llvm::StringRef Text
) {
1020 addInlayHint(R
, HintSide::Left
, InlayHintKind::Designator
,
1021 /*Prefix=*/"", Text
, /*Suffix=*/"=");
1024 bool shouldPrintTypeHint(llvm::StringRef TypeName
) const noexcept
{
1025 return Cfg
.InlayHints
.TypeNameLimit
== 0 ||
1026 TypeName
.size() < Cfg
.InlayHints
.TypeNameLimit
;
1029 void addBlockEndHint(SourceRange BraceRange
, StringRef DeclPrefix
,
1030 StringRef Name
, StringRef OptionalPunctuation
) {
1031 auto HintRange
= computeBlockEndHintRange(BraceRange
, OptionalPunctuation
);
1035 std::string Label
= DeclPrefix
.str();
1036 if (!Label
.empty() && !Name
.empty())
1040 constexpr unsigned HintMaxLengthLimit
= 60;
1041 if (Label
.length() > HintMaxLengthLimit
)
1044 addInlayHint(*HintRange
, HintSide::Right
, InlayHintKind::BlockEnd
, " // ",
1048 // Compute the LSP range to attach the block end hint to, if any allowed.
1049 // 1. "}" is the last non-whitespace character on the line. The range of "}"
1051 // 2. After "}", if the trimmed trailing text is exactly
1052 // `OptionalPunctuation`, say ";". The range of "} ... ;" is returned.
1053 // Otherwise, the hint shouldn't be shown.
1054 std::optional
<Range
> computeBlockEndHintRange(SourceRange BraceRange
,
1055 StringRef OptionalPunctuation
) {
1056 constexpr unsigned HintMinLineLimit
= 2;
1058 auto &SM
= AST
.getSourceManager();
1059 auto [BlockBeginFileId
, BlockBeginOffset
] =
1060 SM
.getDecomposedLoc(SM
.getFileLoc(BraceRange
.getBegin()));
1061 auto RBraceLoc
= SM
.getFileLoc(BraceRange
.getEnd());
1062 auto [RBraceFileId
, RBraceOffset
] = SM
.getDecomposedLoc(RBraceLoc
);
1064 // Because we need to check the block satisfies the minimum line limit, we
1065 // require both source location to be in the main file. This prevents hint
1066 // to be shown in weird cases like '{' is actually in a "#include", but it's
1068 if (BlockBeginFileId
!= MainFileID
|| RBraceFileId
!= MainFileID
)
1069 return std::nullopt
;
1071 StringRef RestOfLine
= MainFileBuf
.substr(RBraceOffset
).split('\n').first
;
1072 if (!RestOfLine
.starts_with("}"))
1073 return std::nullopt
;
1075 StringRef TrimmedTrailingText
= RestOfLine
.drop_front().trim();
1076 if (!TrimmedTrailingText
.empty() &&
1077 TrimmedTrailingText
!= OptionalPunctuation
)
1078 return std::nullopt
;
1080 auto BlockBeginLine
= SM
.getLineNumber(BlockBeginFileId
, BlockBeginOffset
);
1081 auto RBraceLine
= SM
.getLineNumber(RBraceFileId
, RBraceOffset
);
1083 // Don't show hint on trivial blocks like `class X {};`
1084 if (BlockBeginLine
+ HintMinLineLimit
- 1 > RBraceLine
)
1085 return std::nullopt
;
1087 // This is what we attach the hint to, usually "}" or "};".
1088 StringRef HintRangeText
= RestOfLine
.take_front(
1089 TrimmedTrailingText
.empty()
1091 : TrimmedTrailingText
.bytes_end() - RestOfLine
.bytes_begin());
1093 Position HintStart
= sourceLocToPosition(SM
, RBraceLoc
);
1094 Position HintEnd
= sourceLocToPosition(
1095 SM
, RBraceLoc
.getLocWithOffset(HintRangeText
.size()));
1096 return Range
{HintStart
, HintEnd
};
1099 static bool isFunctionObjectCallExpr(CallExpr
*E
) noexcept
{
1100 if (auto *CallExpr
= dyn_cast
<CXXOperatorCallExpr
>(E
))
1101 return CallExpr
->getOperator() == OverloadedOperatorKind::OO_Call
;
1105 std::vector
<InlayHint
> &Results
;
1107 const syntax::TokenBuffer
&Tokens
;
1109 std::optional
<Range
> RestrictRange
;
1111 StringRef MainFileBuf
;
1112 const HeuristicResolver
*Resolver
;
1113 PrintingPolicy TypeHintPolicy
;
1118 std::vector
<InlayHint
> inlayHints(ParsedAST
&AST
,
1119 std::optional
<Range
> RestrictRange
) {
1120 std::vector
<InlayHint
> Results
;
1121 const auto &Cfg
= Config::current();
1122 if (!Cfg
.InlayHints
.Enabled
)
1124 InlayHintVisitor
Visitor(Results
, AST
, Cfg
, std::move(RestrictRange
));
1125 Visitor
.TraverseAST(AST
.getASTContext());
1127 // De-duplicate hints. Duplicates can sometimes occur due to e.g. explicit
1128 // template instantiations.
1129 llvm::sort(Results
);
1130 Results
.erase(std::unique(Results
.begin(), Results
.end()), Results
.end());
1135 } // namespace clangd
1136 } // namespace clang