Bump version to 19.1.0git
[llvm-project.git] / clang-tools-extra / clangd / InlayHints.cpp
blobcd4f1931b3ce1dabd27195366c66349f0ff01cf0
1 //===--- InlayHints.cpp ------------------------------------------*- C++-*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 #include "InlayHints.h"
9 #include "../clang-tidy/utils/DesignatedInitializers.h"
10 #include "AST.h"
11 #include "Config.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"
35 #include <optional>
36 #include <string>
38 namespace clang {
39 namespace clangd {
40 namespace {
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) {
51 return T->getDecl();
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) \
58 case Type::TY: \
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();
69 return "";
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());
84 return "";
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) {
93 if (E == nullptr)
94 return "";
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());
108 std::string
109 VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E) {
110 return getSimpleName(E->getMember()).str();
112 std::string
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));
137 return "";
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) {
148 std::string Result;
149 llvm::raw_string_ostream OS(Result);
150 E->getValue().print(OS);
151 // Printer adds newlines?!
152 Result.resize(llvm::StringRef(Result).rtrim().size());
153 return Result;
155 std::string VisitStringLiteral(const StringLiteral *E) {
156 std::string Result = "\"";
157 if (E->containsNonAscii()) {
158 Result += "...";
159 } else if (E->getLength() > 10) {
160 Result += E->getString().take_front(7);
161 Result += "...";
162 } else {
163 llvm::raw_string_ostream OS(Result);
164 llvm::printEscapedString(E->getString(), OS);
166 Result.push_back('"');
167 return Result;
170 // Simple operators. Motivating cases are `!x` and `I < Length`.
171 std::string printUnary(llvm::StringRef Spelling, const Expr *Operand,
172 bool Prefix) {
173 std::string Sub = Visit(Operand);
174 if (Sub.empty())
175 return "";
176 if (Prefix)
177 return (Spelling + Sub).str();
178 Sub += Spelling;
179 return Sub;
181 bool InsideBinary = false; // No recursing into binary expressions.
182 std::string printBinary(llvm::StringRef Spelling, const Expr *LHSOp,
183 const Expr *RHSOp) {
184 if (InsideBinary)
185 return "";
186 llvm::SaveAndRestore InBinary(InsideBinary, true);
188 std::string LHS = Visit(LHSOp);
189 std::string RHS = Visit(RHSOp);
190 if (LHS.empty() && RHS.empty())
191 return "";
193 if (LHS.empty())
194 LHS = "...";
195 LHS.push_back(' ');
196 LHS += Spelling;
197 LHS.push_back(' ');
198 if (RHS.empty())
199 LHS += "...";
200 else
201 LHS += RHS;
202 return LHS;
204 std::string VisitUnaryOperator(const UnaryOperator *E) {
205 return printUnary(E->getOpcodeStr(E->getOpcode()), E->getSubExpr(),
206 !E->isPostfix());
208 std::string VisitBinaryOperator(const BinaryOperator *E) {
209 return printBinary(E->getOpcodeStr(E->getOpcode()), E->getLHS(),
210 E->getRHS());
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()) {
223 case OO_Plus:
224 case OO_Minus:
225 case OO_Star:
226 case OO_Amp:
227 case OO_Tilde:
228 case OO_Exclaim:
229 case OO_PlusPlus:
230 case OO_MinusMinus:
231 return printUnary(Spelling, E->getArg(0), true);
232 default:
233 break;
236 return "";
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
247 // type. Peel it.
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.
276 while (true) {
277 if (QT->getAs<SubstTemplateTypeParmType>())
278 return true;
279 QualType Desugared = QT->getLocallyUnqualifiedSingleStepDesugaredType();
280 if (Desugared != QT)
281 QT = Desugared;
282 else if (auto Peeled = PeelWrapper(Desugared); Peeled != QT)
283 QT = Peeled;
284 else
285 break;
287 return false;
290 // A simple wrapper for `clang::desugarForDiagnostic` that provides optional
291 // semantic.
292 std::optional<QualType> desugar(ASTContext &AST, QualType QT) {
293 bool ShouldAKA = false;
294 auto Desugared = clang::desugarForDiagnostic(AST, QT, ShouldAKA);
295 if (!ShouldAKA)
296 return std::nullopt;
297 return Desugared;
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
303 // parameter QT.
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
310 // containers.
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();
322 return QT;
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) {
330 TypeLoc Target;
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();
341 if (!Target)
342 return {};
344 // Unwrap types that may be wrapping the function type
345 while (true) {
346 if (auto P = Target.getAs<PointerTypeLoc>()) {
347 Target = P.getPointeeLoc();
348 continue;
350 if (auto A = Target.getAs<AttributedTypeLoc>()) {
351 Target = A.getModifiedLoc();
352 continue;
354 if (auto P = Target.getAs<ParenTypeLoc>()) {
355 Target = P.getInnerLoc();
356 continue;
358 break;
361 if (auto F = Target.getAs<FunctionProtoTypeLoc>()) {
362 return F;
365 return {};
368 ArrayRef<const ParmVarDecl *>
369 maybeDropCxxExplicitObjectParameters(ArrayRef<const ParmVarDecl *> Params) {
370 if (!Params.empty() && Params.front()->isExplicitObjectParameter())
371 Params = Params.drop_front(1);
372 return Params;
375 struct Callee {
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> {
383 public:
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, ": ");
408 return true;
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()) {
418 return true;
421 Callee Callee;
422 Callee.Decl = E->getConstructor();
423 if (!Callee.Decl)
424 return true;
425 processCall(Callee, {E->getArgs(), E->getNumArgs()});
426 return true;
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.
442 // e.g.
443 // struct S {
444 // __declspec(property(get=GetX, put=PutX)) int x[];
445 // void PutX(int y);
446 // void Work(int y) { x = y; } // Bad: `x = y: y`.
447 // };
448 if (isa<BinaryOperator>(SyntacticExpr))
449 return true;
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)
456 return true;
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))
465 return true;
467 auto CalleeDecls = Resolver->resolveCalleeOfCallExpr(E);
468 if (CalleeDecls.size() != 1)
469 return true;
471 Callee Callee;
472 if (const auto *FD = dyn_cast<FunctionDecl>(CalleeDecls[0]))
473 Callee.Decl = FD;
474 else if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(CalleeDecls[0]))
475 Callee.Decl = FTD->getTemplatedDecl();
476 else if (FunctionProtoTypeLoc Loc = getPrototypeLoc(E->getCallee()))
477 Callee.Loc = Loc;
478 else
479 return true;
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
493 // either.
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);
499 return true;
502 bool VisitFunctionDecl(FunctionDecl *D) {
503 if (auto *FPT =
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
512 // overload.
513 if (const Stmt *Body = D->getBody())
514 addBlockEndHint(Body->getSourceRange(), "", printName(AST, *D), "");
516 return true;
519 bool VisitForStmt(ForStmt *S) {
520 if (Cfg.InlayHints.BlockEnd) {
521 std::string Name;
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()));
526 else
527 Name = summarizeExpr(S->getCond());
528 markBlockEnd(S->getBody(), "for", Name);
530 return true;
533 bool VisitCXXForRangeStmt(CXXForRangeStmt *S) {
534 if (Cfg.InlayHints.BlockEnd)
535 markBlockEnd(S->getBody(), "for", getSimpleName(*S->getLoopVariable()));
536 return true;
539 bool VisitWhileStmt(WhileStmt *S) {
540 if (Cfg.InlayHints.BlockEnd)
541 markBlockEnd(S->getBody(), "while", summarizeExpr(S->getCond()));
542 return true;
545 bool VisitSwitchStmt(SwitchStmt *S) {
546 if (Cfg.InlayHints.BlockEnd)
547 markBlockEnd(S->getBody(), "switch", summarizeExpr(S->getCond()));
548 return true;
551 // If/else chains are tricky.
552 // if (cond1) {
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())) {
565 addBlockEndHint(
566 {S->getThen()->getBeginLoc(), EndCS->getRBracLoc()}, "if",
567 ElseIfs.contains(S) ? "" : summarizeExpr(S->getCond()), "");
570 return true;
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)) {
583 if (ED->isScoped())
584 DeclPrefix += ED->isScopedUsingClassTag() ? " class" : " struct";
586 addBlockEndHint(D->getBraceRange(), DeclPrefix, getSimpleName(*D), ";");
588 return true;
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), "");
597 return true;
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());
606 return true;
609 void addReturnTypeHint(FunctionDecl *D, SourceRange Range) {
610 auto *AT = D->getReturnType()->getContainedAutoType();
611 if (!AT || AT->getDeducedType().isNull())
612 return;
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(),
627 /*Prefix=*/": ");
629 return true;
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())
647 .isNull()) {
648 if (auto *IPVD = getOnlyParamInstantiation(PVD))
649 addTypeHint(D->getLocation(), IPVD->getType(), /*Prefix=*/": ");
653 return true;
656 ParmVarDecl *getOnlyParamInstantiation(ParmVarDecl *D) {
657 auto *TemplateFunction = llvm::dyn_cast<FunctionDecl>(D->getDeclContext());
658 if (!TemplateFunction)
659 return nullptr;
660 auto *InstantiatedFunction = llvm::dyn_cast_or_null<FunctionDecl>(
661 getOnlyInstantiation(TemplateFunction));
662 if (!InstantiatedFunction)
663 return nullptr;
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())
670 return nullptr;
671 if (Param == D)
672 break;
673 ++ParamIdx;
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
688 // labels.
689 assert(Syn->isSyntacticForm() && "RAV should not visit implicit code!");
690 if (!Cfg.InlayHints.Designators)
691 return true;
692 if (Syn->isIdiomaticZeroInitializer(AST.getLangOpts()))
693 return true;
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))
698 continue;
699 auto It = Designators.find(Init->getBeginLoc());
700 if (It != Designators.end() &&
701 !isPrecededByParamNameComment(Init, It->second))
702 addDesignatorHint(Init->getSourceRange(), It->second);
704 return true;
707 // FIXME: Handle RecoveryExpr to try to hint some invalid calls.
709 private:
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)
716 return;
718 // The parameter name of a move or copy constructor is not very interesting.
719 if (Callee.Decl)
720 if (auto *Ctor = dyn_cast<CXXConstructorDecl>(Callee.Decl))
721 if (Ctor->isCopyOrMoveConstructor())
722 return;
724 ArrayRef<const ParmVarDecl *> Params, ForwardedParams;
725 // Resolve parameter packs to their forwarded parameter
726 SmallVector<const ParmVarDecl *> ForwardedParamsStorage;
727 if (Callee.Decl) {
728 Params = maybeDropCxxExplicitObjectParameters(Callee.Decl->parameters());
729 ForwardedParamsStorage = resolveForwardingParameters(Callee.Decl);
730 ForwardedParams =
731 maybeDropCxxExplicitObjectParameters(ForwardedParamsStorage);
732 } else {
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.
742 if (Callee.Decl &&
743 (isSetter(Callee.Decl, ParameterNames) || isSimpleBuiltin(Callee.Decl)))
744 return;
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
749 // encounter one.
750 if (isa<PackExpansionExpr>(Args[I])) {
751 break;
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)
768 return false;
770 StringRef Name = getSimpleName(*Callee);
771 if (!Name.starts_with_insensitive("set"))
772 return false;
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:
798 return true;
799 default:
800 return false;
804 bool shouldHintName(const Expr *Arg, StringRef ParamName) {
805 if (ParamName.empty())
806 return false;
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))
811 return false;
813 // Exclude argument expressions preceded by a /*paramName*/.
814 if (isPrecededByParamNameComment(Arg, ParamName))
815 return false;
817 return true;
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
831 // by-reference
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
836 // a conversion.
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 "="
854 // at the end.
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)
860 return false;
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("*/"))
867 return false;
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))
875 return false;
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());
893 return {};
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();
904 } else {
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);
937 return nullptr;
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);
946 if (!LSPRange)
947 return;
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!");
959 switch (Kind) {
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) \
965 return; \
966 break
967 CHECK_KIND(Parameter, Parameters);
968 CHECK_KIND(Type, DeducedTypes);
969 CHECK_KIND(Designator, Designators);
970 CHECK_KIND(BlockEnd, BlockEnd);
971 #undef CHECK_KIND
974 Position LSPPos = Side == HintSide::Left ? LSPRange.start : LSPRange.end;
975 if (RestrictRange &&
976 (LSPPos < RestrictRange->start || !(LSPPos < RestrictRange->end)))
977 return;
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
990 // macro expansion.
991 if (!Spelled || Spelled->empty())
992 return std::nullopt;
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())
996 return std::nullopt;
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())
1003 return;
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
1011 // type.
1012 TypeName = T.getAsString(TypeHintPolicy);
1014 if (shouldPrintTypeHint(TypeName))
1015 addInlayHint(R, HintSide::Right, InlayHintKind::Type, Prefix, TypeName,
1016 /*Suffix=*/"");
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);
1032 if (!HintRange)
1033 return;
1035 std::string Label = DeclPrefix.str();
1036 if (!Label.empty() && !Name.empty())
1037 Label += ' ';
1038 Label += Name;
1040 constexpr unsigned HintMaxLengthLimit = 60;
1041 if (Label.length() > HintMaxLengthLimit)
1042 return;
1044 addInlayHint(*HintRange, HintSide::Right, InlayHintKind::BlockEnd, " // ",
1045 Label, "");
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 "}"
1050 // is returned.
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
1067 // rare anyway.
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;
1102 return false;
1105 std::vector<InlayHint> &Results;
1106 ASTContext &AST;
1107 const syntax::TokenBuffer &Tokens;
1108 const Config &Cfg;
1109 std::optional<Range> RestrictRange;
1110 FileID MainFileID;
1111 StringRef MainFileBuf;
1112 const HeuristicResolver *Resolver;
1113 PrintingPolicy TypeHintPolicy;
1116 } // namespace
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)
1123 return Results;
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());
1132 return Results;
1135 } // namespace clangd
1136 } // namespace clang