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 "AST.h" |
10 | #include "Config.h" |
11 | #include "HeuristicResolver.h" |
12 | #include "ParsedAST.h" |
13 | #include "SourceCode.h" |
14 | #include "clang/AST/ASTDiagnostic.h" |
15 | #include "clang/AST/Decl.h" |
16 | #include "clang/AST/DeclarationName.h" |
17 | #include "clang/AST/Expr.h" |
18 | #include "clang/AST/ExprCXX.h" |
19 | #include "clang/AST/RecursiveASTVisitor.h" |
20 | #include "clang/AST/Stmt.h" |
21 | #include "clang/AST/StmtVisitor.h" |
22 | #include "clang/AST/Type.h" |
23 | #include "clang/Basic/Builtins.h" |
24 | #include "clang/Basic/OperatorKinds.h" |
25 | #include "clang/Basic/SourceManager.h" |
26 | #include "llvm/ADT/DenseSet.h" |
27 | #include "llvm/ADT/ScopeExit.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> |
37 | |
38 | namespace clang { |
39 | namespace clangd { |
40 | namespace { |
41 | |
42 | // For now, inlay hints are always anchored at the left or right of their range. |
43 | enum class HintSide { Left, Right }; |
44 | |
45 | // Helper class to iterate over the designator names of an aggregate type. |
46 | // |
47 | // For an array type, yields [0], [1], [2]... |
48 | // For aggregate classes, yields null for each base, then .field1, .field2, ... |
49 | class AggregateDesignatorNames { |
50 | public: |
51 | AggregateDesignatorNames(QualType T) { |
52 | if (!T.isNull()) { |
53 | T = T.getCanonicalType(); |
54 | if (T->isArrayType()) { |
55 | IsArray = true; |
56 | Valid = true; |
57 | return; |
58 | } |
59 | if (const RecordDecl *RD = T->getAsRecordDecl()) { |
60 | Valid = true; |
61 | FieldsIt = RD->field_begin(); |
62 | FieldsEnd = RD->field_end(); |
63 | if (const auto *CRD = llvm::dyn_cast<CXXRecordDecl>(Val: RD)) { |
64 | BasesIt = CRD->bases_begin(); |
65 | BasesEnd = CRD->bases_end(); |
66 | Valid = CRD->isAggregate(); |
67 | } |
68 | OneField = Valid && BasesIt == BasesEnd && FieldsIt != FieldsEnd && |
69 | std::next(x: FieldsIt) == FieldsEnd; |
70 | } |
71 | } |
72 | } |
73 | // Returns false if the type was not an aggregate. |
74 | operator bool() { return Valid; } |
75 | // Advance to the next element in the aggregate. |
76 | void next() { |
77 | if (IsArray) |
78 | ++Index; |
79 | else if (BasesIt != BasesEnd) |
80 | ++BasesIt; |
81 | else if (FieldsIt != FieldsEnd) |
82 | ++FieldsIt; |
83 | } |
84 | // Print the designator to Out. |
85 | // Returns false if we could not produce a designator for this element. |
86 | bool append(std::string &Out, bool ForSubobject) { |
87 | if (IsArray) { |
88 | Out.push_back(c: '['); |
89 | Out.append(str: std::to_string(val: Index)); |
90 | Out.push_back(c: ']'); |
91 | return true; |
92 | } |
93 | if (BasesIt != BasesEnd) |
94 | return false; // Bases can't be designated. Should we make one up? |
95 | if (FieldsIt != FieldsEnd) { |
96 | llvm::StringRef FieldName; |
97 | if (const IdentifierInfo *II = FieldsIt->getIdentifier()) |
98 | FieldName = II->getName(); |
99 | |
100 | // For certain objects, their subobjects may be named directly. |
101 | if (ForSubobject && |
102 | (FieldsIt->isAnonymousStructOrUnion() || |
103 | // std::array<int,3> x = {1,2,3}. Designators not strictly valid! |
104 | (OneField && isReservedName(Name: FieldName)))) |
105 | return true; |
106 | |
107 | if (!FieldName.empty() && !isReservedName(Name: FieldName)) { |
108 | Out.push_back(c: '.'); |
109 | Out.append(first: FieldName.begin(), last: FieldName.end()); |
110 | return true; |
111 | } |
112 | return false; |
113 | } |
114 | return false; |
115 | } |
116 | |
117 | private: |
118 | bool Valid = false; |
119 | bool IsArray = false; |
120 | bool OneField = false; // e.g. std::array { T __elements[N]; } |
121 | unsigned Index = 0; |
122 | CXXRecordDecl::base_class_const_iterator BasesIt; |
123 | CXXRecordDecl::base_class_const_iterator BasesEnd; |
124 | RecordDecl::field_iterator FieldsIt; |
125 | RecordDecl::field_iterator FieldsEnd; |
126 | }; |
127 | |
128 | // Collect designator labels describing the elements of an init list. |
129 | // |
130 | // This function contributes the designators of some (sub)object, which is |
131 | // represented by the semantic InitListExpr Sem. |
132 | // This includes any nested subobjects, but *only* if they are part of the same |
133 | // original syntactic init list (due to brace elision). |
134 | // In other words, it may descend into subobjects but not written init-lists. |
135 | // |
136 | // For example: struct Outer { Inner a,b; }; struct Inner { int x, y; } |
137 | // Outer o{{1, 2}, 3}; |
138 | // This function will be called with Sem = { {1, 2}, {3, ImplicitValue} } |
139 | // It should generate designators '.a:' and '.b.x:'. |
140 | // '.a:' is produced directly without recursing into the written sublist. |
141 | // (The written sublist will have a separate collectDesignators() call later). |
142 | // Recursion with Prefix='.b' and Sem = {3, ImplicitValue} produces '.b.x:'. |
143 | void collectDesignators(const InitListExpr *Sem, |
144 | llvm::DenseMap<SourceLocation, std::string> &Out, |
145 | const llvm::DenseSet<SourceLocation> &NestedBraces, |
146 | std::string &Prefix) { |
147 | if (!Sem || Sem->isTransparent()) |
148 | return; |
149 | assert(Sem->isSemanticForm()); |
150 | |
151 | // The elements of the semantic form all correspond to direct subobjects of |
152 | // the aggregate type. `Fields` iterates over these subobject names. |
153 | AggregateDesignatorNames Fields(Sem->getType()); |
154 | if (!Fields) |
155 | return; |
156 | for (const Expr *Init : Sem->inits()) { |
157 | auto Next = llvm::make_scope_exit(F: [&, Size(Prefix.size())] { |
158 | Fields.next(); // Always advance to the next subobject name. |
159 | Prefix.resize(n: Size); // Erase any designator we appended. |
160 | }); |
161 | // Skip for a broken initializer or if it is a "hole" in a subobject that |
162 | // was not explicitly initialized. |
163 | if (!Init || llvm::isa<ImplicitValueInitExpr>(Val: Init)) |
164 | continue; |
165 | |
166 | const auto *BraceElidedSubobject = llvm::dyn_cast<InitListExpr>(Val: Init); |
167 | if (BraceElidedSubobject && |
168 | NestedBraces.contains(V: BraceElidedSubobject->getLBraceLoc())) |
169 | BraceElidedSubobject = nullptr; // there were braces! |
170 | |
171 | if (!Fields.append(Out&: Prefix, ForSubobject: BraceElidedSubobject != nullptr)) |
172 | continue; // no designator available for this subobject |
173 | if (BraceElidedSubobject) { |
174 | // If the braces were elided, this aggregate subobject is initialized |
175 | // inline in the same syntactic list. |
176 | // Descend into the semantic list describing the subobject. |
177 | // (NestedBraces are still correct, they're from the same syntactic list). |
178 | collectDesignators(Sem: BraceElidedSubobject, Out, NestedBraces, Prefix); |
179 | continue; |
180 | } |
181 | Out.try_emplace(Init->getBeginLoc(), Prefix); |
182 | } |
183 | } |
184 | |
185 | // Get designators describing the elements of a (syntactic) init list. |
186 | // This does not produce designators for any explicitly-written nested lists. |
187 | llvm::DenseMap<SourceLocation, std::string> |
188 | getDesignators(const InitListExpr *Syn) { |
189 | assert(Syn->isSyntacticForm()); |
190 | |
191 | // collectDesignators needs to know which InitListExprs in the semantic tree |
192 | // were actually written, but InitListExpr::isExplicit() lies. |
193 | // Instead, record where braces of sub-init-lists occur in the syntactic form. |
194 | llvm::DenseSet<SourceLocation> NestedBraces; |
195 | for (const Expr *Init : Syn->inits()) |
196 | if (auto *Nested = llvm::dyn_cast<InitListExpr>(Val: Init)) |
197 | NestedBraces.insert(V: Nested->getLBraceLoc()); |
198 | |
199 | // Traverse the semantic form to find the designators. |
200 | // We use their SourceLocation to correlate with the syntactic form later. |
201 | llvm::DenseMap<SourceLocation, std::string> Designators; |
202 | std::string EmptyPrefix; |
203 | collectDesignators(Sem: Syn->isSemanticForm() ? Syn : Syn->getSemanticForm(), |
204 | Out&: Designators, NestedBraces, Prefix&: EmptyPrefix); |
205 | return Designators; |
206 | } |
207 | |
208 | void stripLeadingUnderscores(StringRef &Name) { Name = Name.ltrim(Char: '_'); } |
209 | |
210 | // getDeclForType() returns the decl responsible for Type's spelling. |
211 | // This is the inverse of ASTContext::getTypeDeclType(). |
212 | template <typename Ty, typename = decltype(((Ty *)nullptr)->getDecl())> |
213 | const NamedDecl *getDeclForTypeImpl(const Ty *T) { |
214 | return T->getDecl(); |
215 | } |
216 | const NamedDecl *getDeclForTypeImpl(const void *T) { return nullptr; } |
217 | const NamedDecl *getDeclForType(const Type *T) { |
218 | switch (T->getTypeClass()) { |
219 | #define ABSTRACT_TYPE(TY, BASE) |
220 | #define TYPE(TY, BASE) \ |
221 | case Type::TY: \ |
222 | return getDeclForTypeImpl(llvm::cast<TY##Type>(T)); |
223 | #include "clang/AST/TypeNodes.inc" |
224 | } |
225 | llvm_unreachable("Unknown TypeClass enum" ); |
226 | } |
227 | |
228 | // getSimpleName() returns the plain identifier for an entity, if any. |
229 | llvm::StringRef getSimpleName(const DeclarationName &DN) { |
230 | if (IdentifierInfo *Ident = DN.getAsIdentifierInfo()) |
231 | return Ident->getName(); |
232 | return "" ; |
233 | } |
234 | llvm::StringRef getSimpleName(const NamedDecl &D) { |
235 | return getSimpleName(DN: D.getDeclName()); |
236 | } |
237 | llvm::StringRef getSimpleName(QualType T) { |
238 | if (const auto *ET = llvm::dyn_cast<ElaboratedType>(T)) |
239 | return getSimpleName(ET->getNamedType()); |
240 | if (const auto *BT = llvm::dyn_cast<BuiltinType>(T)) { |
241 | PrintingPolicy PP(LangOptions{}); |
242 | PP.adjustForCPlusPlus(); |
243 | return BT->getName(PP); |
244 | } |
245 | if (const auto *D = getDeclForType(T: T.getTypePtr())) |
246 | return getSimpleName(DN: D->getDeclName()); |
247 | return "" ; |
248 | } |
249 | |
250 | // Returns a very abbreviated form of an expression, or "" if it's too complex. |
251 | // For example: `foo->bar()` would produce "bar". |
252 | // This is used to summarize e.g. the condition of a while loop. |
253 | std::string summarizeExpr(const Expr *E) { |
254 | struct Namer : ConstStmtVisitor<Namer, std::string> { |
255 | std::string Visit(const Expr *E) { |
256 | if (E == nullptr) |
257 | return "" ; |
258 | return ConstStmtVisitor::Visit(E->IgnoreImplicit()); |
259 | } |
260 | |
261 | // Any sort of decl reference, we just use the unqualified name. |
262 | std::string VisitMemberExpr(const MemberExpr *E) { |
263 | return getSimpleName(*E->getMemberDecl()).str(); |
264 | } |
265 | std::string VisitDeclRefExpr(const DeclRefExpr *E) { |
266 | return getSimpleName(D: *E->getFoundDecl()).str(); |
267 | } |
268 | std::string VisitCallExpr(const CallExpr *E) { |
269 | return Visit(E: E->getCallee()); |
270 | } |
271 | std::string |
272 | VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E) { |
273 | return getSimpleName(DN: E->getMember()).str(); |
274 | } |
275 | std::string |
276 | VisitDependentScopeDeclRefExpr(const DependentScopeDeclRefExpr *E) { |
277 | return getSimpleName(DN: E->getDeclName()).str(); |
278 | } |
279 | std::string VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *E) { |
280 | return getSimpleName(E->getType()).str(); |
281 | } |
282 | std::string VisitCXXTemporaryObjectExpr(const CXXTemporaryObjectExpr *E) { |
283 | return getSimpleName(E->getType()).str(); |
284 | } |
285 | |
286 | // Step through implicit nodes that clang doesn't classify as such. |
287 | std::string VisitCXXMemberCallExpr(const CXXMemberCallExpr *E) { |
288 | // Call to operator bool() inside if (X): dispatch to X. |
289 | if (E->getNumArgs() == 0 && E->getMethodDecl() && |
290 | E->getMethodDecl()->getDeclName().getNameKind() == |
291 | DeclarationName::CXXConversionFunctionName && |
292 | E->getSourceRange() == |
293 | E->getImplicitObjectArgument()->getSourceRange()) |
294 | return Visit(E: E->getImplicitObjectArgument()); |
295 | return ConstStmtVisitor::VisitCXXMemberCallExpr(E); |
296 | } |
297 | std::string VisitCXXConstructExpr(const CXXConstructExpr *E) { |
298 | if (E->getNumArgs() == 1) |
299 | return Visit(E: E->getArg(Arg: 0)); |
300 | return "" ; |
301 | } |
302 | |
303 | // Literals are just printed |
304 | std::string VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
305 | return E->getValue() ? "true" : "false" ; |
306 | } |
307 | std::string VisitIntegerLiteral(const IntegerLiteral *E) { |
308 | return llvm::to_string(E->getValue()); |
309 | } |
310 | std::string VisitFloatingLiteral(const FloatingLiteral *E) { |
311 | std::string Result; |
312 | llvm::raw_string_ostream OS(Result); |
313 | E->getValue().print(OS); |
314 | // Printer adds newlines?! |
315 | Result.resize(n: llvm::StringRef(Result).rtrim().size()); |
316 | return Result; |
317 | } |
318 | std::string VisitStringLiteral(const StringLiteral *E) { |
319 | std::string Result = "\"" ; |
320 | if (E->containsNonAscii()) { |
321 | Result += "..." ; |
322 | } else if (E->getLength() > 10) { |
323 | Result += E->getString().take_front(N: 7); |
324 | Result += "..." ; |
325 | } else { |
326 | llvm::raw_string_ostream OS(Result); |
327 | llvm::printEscapedString(Name: E->getString(), Out&: OS); |
328 | } |
329 | Result.push_back(c: '"'); |
330 | return Result; |
331 | } |
332 | |
333 | // Simple operators. Motivating cases are `!x` and `I < Length`. |
334 | std::string printUnary(llvm::StringRef Spelling, const Expr *Operand, |
335 | bool Prefix) { |
336 | std::string Sub = Visit(E: Operand); |
337 | if (Sub.empty()) |
338 | return "" ; |
339 | if (Prefix) |
340 | return (Spelling + Sub).str(); |
341 | Sub += Spelling; |
342 | return Sub; |
343 | } |
344 | bool InsideBinary = false; // No recursing into binary expressions. |
345 | std::string printBinary(llvm::StringRef Spelling, const Expr *LHSOp, |
346 | const Expr *RHSOp) { |
347 | if (InsideBinary) |
348 | return "" ; |
349 | llvm::SaveAndRestore InBinary(InsideBinary, true); |
350 | |
351 | std::string LHS = Visit(E: LHSOp); |
352 | std::string RHS = Visit(E: RHSOp); |
353 | if (LHS.empty() && RHS.empty()) |
354 | return "" ; |
355 | |
356 | if (LHS.empty()) |
357 | LHS = "..." ; |
358 | LHS.push_back(c: ' '); |
359 | LHS += Spelling; |
360 | LHS.push_back(c: ' '); |
361 | if (RHS.empty()) |
362 | LHS += "..." ; |
363 | else |
364 | LHS += RHS; |
365 | return LHS; |
366 | } |
367 | std::string VisitUnaryOperator(const UnaryOperator *E) { |
368 | return printUnary(Spelling: E->getOpcodeStr(Op: E->getOpcode()), Operand: E->getSubExpr(), |
369 | Prefix: !E->isPostfix()); |
370 | } |
371 | std::string VisitBinaryOperator(const BinaryOperator *E) { |
372 | return printBinary(Spelling: E->getOpcodeStr(Op: E->getOpcode()), LHSOp: E->getLHS(), |
373 | RHSOp: E->getRHS()); |
374 | } |
375 | std::string VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *E) { |
376 | const char *Spelling = getOperatorSpelling(Operator: E->getOperator()); |
377 | // Handle weird unary-that-look-like-binary postfix operators. |
378 | if ((E->getOperator() == OO_PlusPlus || |
379 | E->getOperator() == OO_MinusMinus) && |
380 | E->getNumArgs() == 2) |
381 | return printUnary(Spelling, Operand: E->getArg(0), Prefix: false); |
382 | if (E->isInfixBinaryOp()) |
383 | return printBinary(Spelling, LHSOp: E->getArg(0), RHSOp: E->getArg(1)); |
384 | if (E->getNumArgs() == 1) { |
385 | switch (E->getOperator()) { |
386 | case OO_Plus: |
387 | case OO_Minus: |
388 | case OO_Star: |
389 | case OO_Amp: |
390 | case OO_Tilde: |
391 | case OO_Exclaim: |
392 | case OO_PlusPlus: |
393 | case OO_MinusMinus: |
394 | return printUnary(Spelling, Operand: E->getArg(0), Prefix: true); |
395 | default: |
396 | break; |
397 | } |
398 | } |
399 | return "" ; |
400 | } |
401 | }; |
402 | return Namer{}.Visit(E); |
403 | } |
404 | |
405 | // Determines if any intermediate type in desugaring QualType QT is of |
406 | // substituted template parameter type. Ignore pointer or reference wrappers. |
407 | bool isSugaredTemplateParameter(QualType QT) { |
408 | static auto PeelWrapper = [](QualType QT) { |
409 | // Neither `PointerType` nor `ReferenceType` is considered as sugared |
410 | // type. Peel it. |
411 | QualType Peeled = QT->getPointeeType(); |
412 | return Peeled.isNull() ? QT : Peeled; |
413 | }; |
414 | |
415 | // This is a bit tricky: we traverse the type structure and find whether or |
416 | // not a type in the desugaring process is of SubstTemplateTypeParmType. |
417 | // During the process, we may encounter pointer or reference types that are |
418 | // not marked as sugared; therefore, the desugar function won't apply. To |
419 | // move forward the traversal, we retrieve the pointees using |
420 | // QualType::getPointeeType(). |
421 | // |
422 | // However, getPointeeType could leap over our interests: The QT::getAs<T>() |
423 | // invoked would implicitly desugar the type. Consequently, if the |
424 | // SubstTemplateTypeParmType is encompassed within a TypedefType, we may lose |
425 | // the chance to visit it. |
426 | // For example, given a QT that represents `std::vector<int *>::value_type`: |
427 | // `-ElaboratedType 'value_type' sugar |
428 | // `-TypedefType 'vector<int *>::value_type' sugar |
429 | // |-Typedef 'value_type' |
430 | // `-SubstTemplateTypeParmType 'int *' sugar class depth 0 index 0 T |
431 | // |-ClassTemplateSpecialization 'vector' |
432 | // `-PointerType 'int *' |
433 | // `-BuiltinType 'int' |
434 | // Applying `getPointeeType` to QT results in 'int', a child of our target |
435 | // node SubstTemplateTypeParmType. |
436 | // |
437 | // As such, we always prefer the desugared over the pointee for next type |
438 | // in the iteration. It could avoid the getPointeeType's implicit desugaring. |
439 | while (true) { |
440 | if (QT->getAs<SubstTemplateTypeParmType>()) |
441 | return true; |
442 | QualType Desugared = QT->getLocallyUnqualifiedSingleStepDesugaredType(); |
443 | if (Desugared != QT) |
444 | QT = Desugared; |
445 | else if (auto Peeled = PeelWrapper(Desugared); Peeled != QT) |
446 | QT = Peeled; |
447 | else |
448 | break; |
449 | } |
450 | return false; |
451 | } |
452 | |
453 | // A simple wrapper for `clang::desugarForDiagnostic` that provides optional |
454 | // semantic. |
455 | std::optional<QualType> desugar(ASTContext &AST, QualType QT) { |
456 | bool ShouldAKA = false; |
457 | auto Desugared = clang::desugarForDiagnostic(Context&: AST, QT, ShouldAKA); |
458 | if (!ShouldAKA) |
459 | return std::nullopt; |
460 | return Desugared; |
461 | } |
462 | |
463 | // Apply a series of heuristic methods to determine whether or not a QualType QT |
464 | // is suitable for desugaring (e.g. getting the real name behind the using-alias |
465 | // name). If so, return the desugared type. Otherwise, return the unchanged |
466 | // parameter QT. |
467 | // |
468 | // This could be refined further. See |
469 | // https://github.com/clangd/clangd/issues/1298. |
470 | QualType maybeDesugar(ASTContext &AST, QualType QT) { |
471 | // Prefer desugared type for name that aliases the template parameters. |
472 | // This can prevent things like printing opaque `: type` when accessing std |
473 | // containers. |
474 | if (isSugaredTemplateParameter(QT)) |
475 | return desugar(AST, QT).value_or(QT); |
476 | |
477 | // Prefer desugared type for `decltype(expr)` specifiers. |
478 | if (QT->isDecltypeType()) |
479 | return QT.getCanonicalType(); |
480 | if (const AutoType *AT = QT->getContainedAutoType()) |
481 | if (!AT->getDeducedType().isNull() && |
482 | AT->getDeducedType()->isDecltypeType()) |
483 | return QT.getCanonicalType(); |
484 | |
485 | return QT; |
486 | } |
487 | |
488 | // Given a callee expression `Fn`, if the call is through a function pointer, |
489 | // try to find the declaration of the corresponding function pointer type, |
490 | // so that we can recover argument names from it. |
491 | // FIXME: This function is mostly duplicated in SemaCodeComplete.cpp; unify. |
492 | static FunctionProtoTypeLoc getPrototypeLoc(Expr *Fn) { |
493 | TypeLoc Target; |
494 | Expr *NakedFn = Fn->IgnoreParenCasts(); |
495 | if (const auto *T = NakedFn->getType().getTypePtr()->getAs<TypedefType>()) { |
496 | Target = T->getDecl()->getTypeSourceInfo()->getTypeLoc(); |
497 | } else if (const auto *DR = dyn_cast<DeclRefExpr>(NakedFn)) { |
498 | const auto *D = DR->getDecl(); |
499 | if (const auto *const VD = dyn_cast<VarDecl>(D)) { |
500 | Target = VD->getTypeSourceInfo()->getTypeLoc(); |
501 | } |
502 | } |
503 | |
504 | if (!Target) |
505 | return {}; |
506 | |
507 | // Unwrap types that may be wrapping the function type |
508 | while (true) { |
509 | if (auto P = Target.getAs<PointerTypeLoc>()) { |
510 | Target = P.getPointeeLoc(); |
511 | continue; |
512 | } |
513 | if (auto A = Target.getAs<AttributedTypeLoc>()) { |
514 | Target = A.getModifiedLoc(); |
515 | continue; |
516 | } |
517 | if (auto P = Target.getAs<ParenTypeLoc>()) { |
518 | Target = P.getInnerLoc(); |
519 | continue; |
520 | } |
521 | break; |
522 | } |
523 | |
524 | if (auto F = Target.getAs<FunctionProtoTypeLoc>()) { |
525 | return F; |
526 | } |
527 | |
528 | return {}; |
529 | } |
530 | |
531 | ArrayRef<const ParmVarDecl *> |
532 | maybeDropCxxExplicitObjectParameters(ArrayRef<const ParmVarDecl *> Params) { |
533 | if (!Params.empty() && Params.front()->isExplicitObjectParameter()) |
534 | Params = Params.drop_front(1); |
535 | return Params; |
536 | } |
537 | |
538 | struct Callee { |
539 | // Only one of Decl or Loc is set. |
540 | // Loc is for calls through function pointers. |
541 | const FunctionDecl *Decl = nullptr; |
542 | FunctionProtoTypeLoc Loc; |
543 | }; |
544 | |
545 | class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> { |
546 | public: |
547 | InlayHintVisitor(std::vector<InlayHint> &Results, ParsedAST &AST, |
548 | const Config &Cfg, std::optional<Range> RestrictRange) |
549 | : Results(Results), AST(AST.getASTContext()), Tokens(AST.getTokens()), |
550 | Cfg(Cfg), RestrictRange(std::move(RestrictRange)), |
551 | MainFileID(AST.getSourceManager().getMainFileID()), |
552 | Resolver(AST.getHeuristicResolver()), |
553 | TypeHintPolicy(this->AST.getPrintingPolicy()) { |
554 | bool Invalid = false; |
555 | llvm::StringRef Buf = |
556 | AST.getSourceManager().getBufferData(FID: MainFileID, Invalid: &Invalid); |
557 | MainFileBuf = Invalid ? StringRef{} : Buf; |
558 | |
559 | TypeHintPolicy.SuppressScope = true; // keep type names short |
560 | TypeHintPolicy.AnonymousTagLocations = |
561 | false; // do not print lambda locations |
562 | |
563 | // Not setting PrintCanonicalTypes for "auto" allows |
564 | // SuppressDefaultTemplateArgs (set by default) to have an effect. |
565 | } |
566 | |
567 | bool VisitTypeLoc(TypeLoc TL) { |
568 | if (const auto *DT = llvm::dyn_cast<DecltypeType>(TL.getType())) |
569 | if (QualType UT = DT->getUnderlyingType(); !UT->isDependentType()) |
570 | addTypeHint(R: TL.getSourceRange(), T: UT, Prefix: ": " ); |
571 | return true; |
572 | } |
573 | |
574 | bool VisitCXXConstructExpr(CXXConstructExpr *E) { |
575 | // Weed out constructor calls that don't look like a function call with |
576 | // an argument list, by checking the validity of getParenOrBraceRange(). |
577 | // Also weed out std::initializer_list constructors as there are no names |
578 | // for the individual arguments. |
579 | if (!E->getParenOrBraceRange().isValid() || |
580 | E->isStdInitListInitialization()) { |
581 | return true; |
582 | } |
583 | |
584 | Callee Callee; |
585 | Callee.Decl = E->getConstructor(); |
586 | if (!Callee.Decl) |
587 | return true; |
588 | processCall(Callee, Args: {E->getArgs(), E->getNumArgs()}); |
589 | return true; |
590 | } |
591 | |
592 | // Carefully recurse into PseudoObjectExprs, which typically incorporate |
593 | // a syntactic expression and several semantic expressions. |
594 | bool TraversePseudoObjectExpr(PseudoObjectExpr *E) { |
595 | Expr *SyntacticExpr = E->getSyntacticForm(); |
596 | if (isa<CallExpr>(SyntacticExpr)) |
597 | // Since the counterpart semantics usually get the identical source |
598 | // locations as the syntactic one, visiting those would end up presenting |
599 | // confusing hints e.g., __builtin_dump_struct. |
600 | // Thus, only traverse the syntactic forms if this is written as a |
601 | // CallExpr. This leaves the door open in case the arguments in the |
602 | // syntactic form could possibly get parameter names. |
603 | return RecursiveASTVisitor<InlayHintVisitor>::TraverseStmt(SyntacticExpr); |
604 | // We don't want the hints for some of the MS property extensions. |
605 | // e.g. |
606 | // struct S { |
607 | // __declspec(property(get=GetX, put=PutX)) int x[]; |
608 | // void PutX(int y); |
609 | // void Work(int y) { x = y; } // Bad: `x = y: y`. |
610 | // }; |
611 | if (isa<BinaryOperator>(SyntacticExpr)) |
612 | return true; |
613 | // FIXME: Handle other forms of a pseudo object expression. |
614 | return RecursiveASTVisitor<InlayHintVisitor>::TraversePseudoObjectExpr(E); |
615 | } |
616 | |
617 | bool VisitCallExpr(CallExpr *E) { |
618 | if (!Cfg.InlayHints.Parameters) |
619 | return true; |
620 | |
621 | bool IsFunctor = isFunctionObjectCallExpr(E); |
622 | // Do not show parameter hints for user-defined literals or |
623 | // operator calls except for operator(). (Among other reasons, the resulting |
624 | // hints can look awkward, e.g. the expression can itself be a function |
625 | // argument and then we'd get two hints side by side). |
626 | if ((isa<CXXOperatorCallExpr>(E) && !IsFunctor) || |
627 | isa<UserDefinedLiteral>(E)) |
628 | return true; |
629 | |
630 | auto CalleeDecls = Resolver->resolveCalleeOfCallExpr(E); |
631 | if (CalleeDecls.size() != 1) |
632 | return true; |
633 | |
634 | Callee Callee; |
635 | if (const auto *FD = dyn_cast<FunctionDecl>(CalleeDecls[0])) |
636 | Callee.Decl = FD; |
637 | else if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(CalleeDecls[0])) |
638 | Callee.Decl = FTD->getTemplatedDecl(); |
639 | else if (FunctionProtoTypeLoc Loc = getPrototypeLoc(E->getCallee())) |
640 | Callee.Loc = Loc; |
641 | else |
642 | return true; |
643 | |
644 | // N4868 [over.call.object]p3 says, |
645 | // The argument list submitted to overload resolution consists of the |
646 | // argument expressions present in the function call syntax preceded by the |
647 | // implied object argument (E). |
648 | // |
649 | // As well as the provision from P0847R7 Deducing This [expr.call]p7: |
650 | // ...If the function is an explicit object member function and there is an |
651 | // implied object argument ([over.call.func]), the list of provided |
652 | // arguments is preceded by the implied object argument for the purposes of |
653 | // this correspondence... |
654 | llvm::ArrayRef<const Expr *> Args = {E->getArgs(), E->getNumArgs()}; |
655 | // We don't have the implied object argument through a function pointer |
656 | // either. |
657 | if (const CXXMethodDecl *Method = |
658 | dyn_cast_or_null<CXXMethodDecl>(Callee.Decl)) |
659 | if (IsFunctor || Method->hasCXXExplicitFunctionObjectParameter()) |
660 | Args = Args.drop_front(N: 1); |
661 | processCall(Callee, Args); |
662 | return true; |
663 | } |
664 | |
665 | bool VisitFunctionDecl(FunctionDecl *D) { |
666 | if (auto *FPT = |
667 | llvm::dyn_cast<FunctionProtoType>(D->getType().getTypePtr())) { |
668 | if (!FPT->hasTrailingReturn()) { |
669 | if (auto FTL = D->getFunctionTypeLoc()) |
670 | addReturnTypeHint(D, Range: FTL.getRParenLoc()); |
671 | } |
672 | } |
673 | if (Cfg.InlayHints.BlockEnd && D->isThisDeclarationADefinition()) { |
674 | // We use `printName` here to properly print name of ctor/dtor/operator |
675 | // overload. |
676 | if (const Stmt *Body = D->getBody()) |
677 | addBlockEndHint(BraceRange: Body->getSourceRange(), DeclPrefix: "" , Name: printName(AST, *D), OptionalPunctuation: "" ); |
678 | } |
679 | return true; |
680 | } |
681 | |
682 | bool VisitForStmt(ForStmt *S) { |
683 | if (Cfg.InlayHints.BlockEnd) { |
684 | std::string Name; |
685 | // Common case: for (int I = 0; I < N; I++). Use "I" as the name. |
686 | if (auto *DS = llvm::dyn_cast_or_null<DeclStmt>(S->getInit()); |
687 | DS && DS->isSingleDecl()) |
688 | Name = getSimpleName(llvm::cast<NamedDecl>(*DS->getSingleDecl())); |
689 | else |
690 | Name = summarizeExpr(E: S->getCond()); |
691 | markBlockEnd(Body: S->getBody(), Label: "for" , Name); |
692 | } |
693 | return true; |
694 | } |
695 | |
696 | bool VisitCXXForRangeStmt(CXXForRangeStmt *S) { |
697 | if (Cfg.InlayHints.BlockEnd) |
698 | markBlockEnd(Body: S->getBody(), Label: "for" , Name: getSimpleName(*S->getLoopVariable())); |
699 | return true; |
700 | } |
701 | |
702 | bool VisitWhileStmt(WhileStmt *S) { |
703 | if (Cfg.InlayHints.BlockEnd) |
704 | markBlockEnd(Body: S->getBody(), Label: "while" , Name: summarizeExpr(E: S->getCond())); |
705 | return true; |
706 | } |
707 | |
708 | bool VisitSwitchStmt(SwitchStmt *S) { |
709 | if (Cfg.InlayHints.BlockEnd) |
710 | markBlockEnd(Body: S->getBody(), Label: "switch" , Name: summarizeExpr(E: S->getCond())); |
711 | return true; |
712 | } |
713 | |
714 | // If/else chains are tricky. |
715 | // if (cond1) { |
716 | // } else if (cond2) { |
717 | // } // mark as "cond1" or "cond2"? |
718 | // For now, the answer is neither, just mark as "if". |
719 | // The ElseIf is a different IfStmt that doesn't know about the outer one. |
720 | llvm::DenseSet<const IfStmt *> ElseIfs; // not eligible for names |
721 | bool VisitIfStmt(IfStmt *S) { |
722 | if (Cfg.InlayHints.BlockEnd) { |
723 | if (const auto *ElseIf = llvm::dyn_cast_or_null<IfStmt>(S->getElse())) |
724 | ElseIfs.insert(ElseIf); |
725 | // Don't use markBlockEnd: the relevant range is [then.begin, else.end]. |
726 | if (const auto *EndCS = llvm::dyn_cast<CompoundStmt>( |
727 | S->getElse() ? S->getElse() : S->getThen())) { |
728 | addBlockEndHint( |
729 | {S->getThen()->getBeginLoc(), EndCS->getRBracLoc()}, "if" , |
730 | ElseIfs.contains(S) ? "" : summarizeExpr(S->getCond()), "" ); |
731 | } |
732 | } |
733 | return true; |
734 | } |
735 | |
736 | void markBlockEnd(const Stmt *Body, llvm::StringRef Label, |
737 | llvm::StringRef Name = "" ) { |
738 | if (const auto *CS = llvm::dyn_cast_or_null<CompoundStmt>(Body)) |
739 | addBlockEndHint(BraceRange: CS->getSourceRange(), DeclPrefix: Label, Name, OptionalPunctuation: "" ); |
740 | } |
741 | |
742 | bool VisitTagDecl(TagDecl *D) { |
743 | if (Cfg.InlayHints.BlockEnd && D->isThisDeclarationADefinition()) { |
744 | std::string DeclPrefix = D->getKindName().str(); |
745 | if (const auto *ED = dyn_cast<EnumDecl>(D)) { |
746 | if (ED->isScoped()) |
747 | DeclPrefix += ED->isScopedUsingClassTag() ? " class" : " struct" ; |
748 | }; |
749 | addBlockEndHint(BraceRange: D->getBraceRange(), DeclPrefix, Name: getSimpleName(*D), OptionalPunctuation: ";" ); |
750 | } |
751 | return true; |
752 | } |
753 | |
754 | bool VisitNamespaceDecl(NamespaceDecl *D) { |
755 | if (Cfg.InlayHints.BlockEnd) { |
756 | // For namespace, the range actually starts at the namespace keyword. But |
757 | // it should be fine since it's usually very short. |
758 | addBlockEndHint(BraceRange: D->getSourceRange(), DeclPrefix: "namespace" , Name: getSimpleName(*D), OptionalPunctuation: "" ); |
759 | } |
760 | return true; |
761 | } |
762 | |
763 | bool VisitLambdaExpr(LambdaExpr *E) { |
764 | FunctionDecl *D = E->getCallOperator(); |
765 | if (!E->hasExplicitResultType()) |
766 | addReturnTypeHint(D, Range: E->hasExplicitParameters() |
767 | ? D->getFunctionTypeLoc().getRParenLoc() |
768 | : E->getIntroducerRange().getEnd()); |
769 | return true; |
770 | } |
771 | |
772 | void addReturnTypeHint(FunctionDecl *D, SourceRange Range) { |
773 | auto *AT = D->getReturnType()->getContainedAutoType(); |
774 | if (!AT || AT->getDeducedType().isNull()) |
775 | return; |
776 | addTypeHint(R: Range, T: D->getReturnType(), /*Prefix=*/"-> " ); |
777 | } |
778 | |
779 | bool VisitVarDecl(VarDecl *D) { |
780 | // Do not show hints for the aggregate in a structured binding, |
781 | // but show hints for the individual bindings. |
782 | if (auto *DD = dyn_cast<DecompositionDecl>(D)) { |
783 | for (auto *Binding : DD->bindings()) { |
784 | // For structured bindings, print canonical types. This is important |
785 | // because for bindings that use the tuple_element protocol, the |
786 | // non-canonical types would be "tuple_element<I, A>::type". |
787 | if (auto Type = Binding->getType(); |
788 | !Type.isNull() && !Type->isDependentType()) |
789 | addTypeHint(Binding->getLocation(), Type.getCanonicalType(), |
790 | /*Prefix=*/": " ); |
791 | } |
792 | return true; |
793 | } |
794 | |
795 | if (auto *AT = D->getType()->getContainedAutoType()) { |
796 | if (AT->isDeduced() && !D->getType()->isDependentType()) { |
797 | // Our current approach is to place the hint on the variable |
798 | // and accordingly print the full type |
799 | // (e.g. for `const auto& x = 42`, print `const int&`). |
800 | // Alternatively, we could place the hint on the `auto` |
801 | // (and then just print the type deduced for the `auto`). |
802 | addTypeHint(R: D->getLocation(), T: D->getType(), /*Prefix=*/": " ); |
803 | } |
804 | } |
805 | |
806 | // Handle templates like `int foo(auto x)` with exactly one instantiation. |
807 | if (auto *PVD = llvm::dyn_cast<ParmVarDecl>(D)) { |
808 | if (D->getIdentifier() && PVD->getType()->isDependentType() && |
809 | !getContainedAutoParamType(D->getTypeSourceInfo()->getTypeLoc()) |
810 | .isNull()) { |
811 | if (auto *IPVD = getOnlyParamInstantiation(PVD)) |
812 | addTypeHint(R: D->getLocation(), T: IPVD->getType(), /*Prefix=*/": " ); |
813 | } |
814 | } |
815 | |
816 | return true; |
817 | } |
818 | |
819 | ParmVarDecl *getOnlyParamInstantiation(ParmVarDecl *D) { |
820 | auto *TemplateFunction = llvm::dyn_cast<FunctionDecl>(D->getDeclContext()); |
821 | if (!TemplateFunction) |
822 | return nullptr; |
823 | auto *InstantiatedFunction = llvm::dyn_cast_or_null<FunctionDecl>( |
824 | getOnlyInstantiation(TemplateFunction)); |
825 | if (!InstantiatedFunction) |
826 | return nullptr; |
827 | |
828 | unsigned ParamIdx = 0; |
829 | for (auto *Param : TemplateFunction->parameters()) { |
830 | // Can't reason about param indexes in the presence of preceding packs. |
831 | // And if this param is a pack, it may expand to multiple params. |
832 | if (Param->isParameterPack()) |
833 | return nullptr; |
834 | if (Param == D) |
835 | break; |
836 | ++ParamIdx; |
837 | } |
838 | assert(ParamIdx < TemplateFunction->getNumParams() && |
839 | "Couldn't find param in list?" ); |
840 | assert(ParamIdx < InstantiatedFunction->getNumParams() && |
841 | "Instantiated function has fewer (non-pack) parameters?" ); |
842 | return InstantiatedFunction->getParamDecl(ParamIdx); |
843 | } |
844 | |
845 | bool VisitInitListExpr(InitListExpr *Syn) { |
846 | // We receive the syntactic form here (shouldVisitImplicitCode() is false). |
847 | // This is the one we will ultimately attach designators to. |
848 | // It may have subobject initializers inlined without braces. The *semantic* |
849 | // form of the init-list has nested init-lists for these. |
850 | // getDesignators will look at the semantic form to determine the labels. |
851 | assert(Syn->isSyntacticForm() && "RAV should not visit implicit code!" ); |
852 | if (!Cfg.InlayHints.Designators) |
853 | return true; |
854 | if (Syn->isIdiomaticZeroInitializer(LangOpts: AST.getLangOpts())) |
855 | return true; |
856 | llvm::DenseMap<SourceLocation, std::string> Designators = |
857 | getDesignators(Syn); |
858 | for (const Expr *Init : Syn->inits()) { |
859 | if (llvm::isa<DesignatedInitExpr>(Init)) |
860 | continue; |
861 | auto It = Designators.find(Init->getBeginLoc()); |
862 | if (It != Designators.end() && |
863 | !isPrecededByParamNameComment(E: Init, ParamName: It->second)) |
864 | addDesignatorHint(R: Init->getSourceRange(), Text: It->second); |
865 | } |
866 | return true; |
867 | } |
868 | |
869 | // FIXME: Handle RecoveryExpr to try to hint some invalid calls. |
870 | |
871 | private: |
872 | using NameVec = SmallVector<StringRef, 8>; |
873 | |
874 | void processCall(Callee Callee, llvm::ArrayRef<const Expr *> Args) { |
875 | assert(Callee.Decl || Callee.Loc); |
876 | |
877 | if (!Cfg.InlayHints.Parameters || Args.size() == 0) |
878 | return; |
879 | |
880 | // The parameter name of a move or copy constructor is not very interesting. |
881 | if (Callee.Decl) |
882 | if (auto *Ctor = dyn_cast<CXXConstructorDecl>(Callee.Decl)) |
883 | if (Ctor->isCopyOrMoveConstructor()) |
884 | return; |
885 | |
886 | ArrayRef<const ParmVarDecl *> Params, ForwardedParams; |
887 | // Resolve parameter packs to their forwarded parameter |
888 | SmallVector<const ParmVarDecl *> ForwardedParamsStorage; |
889 | if (Callee.Decl) { |
890 | Params = maybeDropCxxExplicitObjectParameters(Callee.Decl->parameters()); |
891 | ForwardedParamsStorage = resolveForwardingParameters(Callee.Decl); |
892 | ForwardedParams = |
893 | maybeDropCxxExplicitObjectParameters(ForwardedParamsStorage); |
894 | } else { |
895 | Params = maybeDropCxxExplicitObjectParameters(Callee.Loc.getParams()); |
896 | ForwardedParams = {Params.begin(), Params.end()}; |
897 | } |
898 | |
899 | NameVec ParameterNames = chooseParameterNames(Parameters: ForwardedParams); |
900 | |
901 | // Exclude setters (i.e. functions with one argument whose name begins with |
902 | // "set"), and builtins like std::move/forward/... as their parameter name |
903 | // is also not likely to be interesting. |
904 | if (Callee.Decl && |
905 | (isSetter(Callee: Callee.Decl, ParamNames: ParameterNames) || isSimpleBuiltin(Callee: Callee.Decl))) |
906 | return; |
907 | |
908 | for (size_t I = 0; I < ParameterNames.size() && I < Args.size(); ++I) { |
909 | // Pack expansion expressions cause the 1:1 mapping between arguments and |
910 | // parameters to break down, so we don't add further inlay hints if we |
911 | // encounter one. |
912 | if (isa<PackExpansionExpr>(Args[I])) { |
913 | break; |
914 | } |
915 | |
916 | StringRef Name = ParameterNames[I]; |
917 | bool NameHint = shouldHintName(Arg: Args[I], ParamName: Name); |
918 | bool ReferenceHint = shouldHintReference(Param: Params[I], ForwardedParam: ForwardedParams[I]); |
919 | |
920 | if (NameHint || ReferenceHint) { |
921 | addInlayHint(Args[I]->getSourceRange(), HintSide::Left, |
922 | InlayHintKind::Parameter, ReferenceHint ? "&" : "" , |
923 | NameHint ? Name : "" , ": " ); |
924 | } |
925 | } |
926 | } |
927 | |
928 | static bool isSetter(const FunctionDecl *Callee, const NameVec &ParamNames) { |
929 | if (ParamNames.size() != 1) |
930 | return false; |
931 | |
932 | StringRef Name = getSimpleName(*Callee); |
933 | if (!Name.starts_with_insensitive(Prefix: "set" )) |
934 | return false; |
935 | |
936 | // In addition to checking that the function has one parameter and its |
937 | // name starts with "set", also check that the part after "set" matches |
938 | // the name of the parameter (ignoring case). The idea here is that if |
939 | // the parameter name differs, it may contain extra information that |
940 | // may be useful to show in a hint, as in: |
941 | // void setTimeout(int timeoutMillis); |
942 | // This currently doesn't handle cases where params use snake_case |
943 | // and functions don't, e.g. |
944 | // void setExceptionHandler(EHFunc exception_handler); |
945 | // We could improve this by replacing `equals_insensitive` with some |
946 | // `sloppy_equals` which ignores case and also skips underscores. |
947 | StringRef WhatItIsSetting = Name.substr(Start: 3).ltrim(Chars: "_" ); |
948 | return WhatItIsSetting.equals_insensitive(RHS: ParamNames[0]); |
949 | } |
950 | |
951 | // Checks if the callee is one of the builtins |
952 | // addressof, as_const, forward, move(_if_noexcept) |
953 | static bool isSimpleBuiltin(const FunctionDecl *Callee) { |
954 | switch (Callee->getBuiltinID()) { |
955 | case Builtin::BIaddressof: |
956 | case Builtin::BIas_const: |
957 | case Builtin::BIforward: |
958 | case Builtin::BImove: |
959 | case Builtin::BImove_if_noexcept: |
960 | return true; |
961 | default: |
962 | return false; |
963 | } |
964 | } |
965 | |
966 | bool shouldHintName(const Expr *Arg, StringRef ParamName) { |
967 | if (ParamName.empty()) |
968 | return false; |
969 | |
970 | // If the argument expression is a single name and it matches the |
971 | // parameter name exactly, omit the name hint. |
972 | if (ParamName == getSpelledIdentifier(E: Arg)) |
973 | return false; |
974 | |
975 | // Exclude argument expressions preceded by a /*paramName*/. |
976 | if (isPrecededByParamNameComment(E: Arg, ParamName)) |
977 | return false; |
978 | |
979 | return true; |
980 | } |
981 | |
982 | bool shouldHintReference(const ParmVarDecl *Param, |
983 | const ParmVarDecl *ForwardedParam) { |
984 | // We add a & hint only when the argument is passed as mutable reference. |
985 | // For parameters that are not part of an expanded pack, this is |
986 | // straightforward. For expanded pack parameters, it's likely that they will |
987 | // be forwarded to another function. In this situation, we only want to add |
988 | // the reference hint if the argument is actually being used via mutable |
989 | // reference. This means we need to check |
990 | // 1. whether the value category of the argument is preserved, i.e. each |
991 | // pack expansion uses std::forward correctly. |
992 | // 2. whether the argument is ever copied/cast instead of passed |
993 | // by-reference |
994 | // Instead of checking this explicitly, we use the following proxy: |
995 | // 1. the value category can only change from rvalue to lvalue during |
996 | // forwarding, so checking whether both the parameter of the forwarding |
997 | // function and the forwarded function are lvalue references detects such |
998 | // a conversion. |
999 | // 2. if the argument is copied/cast somewhere in the chain of forwarding |
1000 | // calls, it can only be passed on to an rvalue reference or const lvalue |
1001 | // reference parameter. Thus if the forwarded parameter is a mutable |
1002 | // lvalue reference, it cannot have been copied/cast to on the way. |
1003 | // Additionally, we should not add a reference hint if the forwarded |
1004 | // parameter was only partially resolved, i.e. points to an expanded pack |
1005 | // parameter, since we do not know how it will be used eventually. |
1006 | auto Type = Param->getType(); |
1007 | auto ForwardedType = ForwardedParam->getType(); |
1008 | return Type->isLValueReferenceType() && |
1009 | ForwardedType->isLValueReferenceType() && |
1010 | !ForwardedType.getNonReferenceType().isConstQualified() && |
1011 | !isExpandedFromParameterPack(D: ForwardedParam); |
1012 | } |
1013 | |
1014 | // Checks if "E" is spelled in the main file and preceded by a C-style comment |
1015 | // whose contents match ParamName (allowing for whitespace and an optional "=" |
1016 | // at the end. |
1017 | bool (const Expr *E, StringRef ParamName) { |
1018 | auto &SM = AST.getSourceManager(); |
1019 | auto FileLoc = SM.getFileLoc(Loc: E->getBeginLoc()); |
1020 | auto Decomposed = SM.getDecomposedLoc(Loc: FileLoc); |
1021 | if (Decomposed.first != MainFileID) |
1022 | return false; |
1023 | |
1024 | StringRef SourcePrefix = MainFileBuf.substr(Start: 0, N: Decomposed.second); |
1025 | // Allow whitespace between comment and expression. |
1026 | SourcePrefix = SourcePrefix.rtrim(); |
1027 | // Check for comment ending. |
1028 | if (!SourcePrefix.consume_back(Suffix: "*/" )) |
1029 | return false; |
1030 | // Ignore some punctuation and whitespace around comment. |
1031 | // In particular this allows designators to match nicely. |
1032 | llvm::StringLiteral IgnoreChars = " =." ; |
1033 | SourcePrefix = SourcePrefix.rtrim(Chars: IgnoreChars); |
1034 | ParamName = ParamName.trim(Chars: IgnoreChars); |
1035 | // Other than that, the comment must contain exactly ParamName. |
1036 | if (!SourcePrefix.consume_back(Suffix: ParamName)) |
1037 | return false; |
1038 | SourcePrefix = SourcePrefix.rtrim(Chars: IgnoreChars); |
1039 | return SourcePrefix.ends_with(Suffix: "/*" ); |
1040 | } |
1041 | |
1042 | // If "E" spells a single unqualified identifier, return that name. |
1043 | // Otherwise, return an empty string. |
1044 | static StringRef getSpelledIdentifier(const Expr *E) { |
1045 | E = E->IgnoreUnlessSpelledInSource(); |
1046 | |
1047 | if (auto *DRE = dyn_cast<DeclRefExpr>(E)) |
1048 | if (!DRE->getQualifier()) |
1049 | return getSimpleName(*DRE->getDecl()); |
1050 | |
1051 | if (auto *ME = dyn_cast<MemberExpr>(E)) |
1052 | if (!ME->getQualifier() && ME->isImplicitAccess()) |
1053 | return getSimpleName(*ME->getMemberDecl()); |
1054 | |
1055 | return {}; |
1056 | } |
1057 | |
1058 | NameVec chooseParameterNames(ArrayRef<const ParmVarDecl *> Parameters) { |
1059 | NameVec ParameterNames; |
1060 | for (const auto *P : Parameters) { |
1061 | if (isExpandedFromParameterPack(P)) { |
1062 | // If we haven't resolved a pack paramater (e.g. foo(Args... args)) to a |
1063 | // non-pack parameter, then hinting as foo(args: 1, args: 2, args: 3) is |
1064 | // unlikely to be useful. |
1065 | ParameterNames.emplace_back(); |
1066 | } else { |
1067 | auto SimpleName = getSimpleName(*P); |
1068 | // If the parameter is unnamed in the declaration: |
1069 | // attempt to get its name from the definition |
1070 | if (SimpleName.empty()) { |
1071 | if (const auto *PD = getParamDefinition(P)) { |
1072 | SimpleName = getSimpleName(*PD); |
1073 | } |
1074 | } |
1075 | ParameterNames.emplace_back(SimpleName); |
1076 | } |
1077 | } |
1078 | |
1079 | // Standard library functions often have parameter names that start |
1080 | // with underscores, which makes the hints noisy, so strip them out. |
1081 | for (auto &Name : ParameterNames) |
1082 | stripLeadingUnderscores(Name); |
1083 | |
1084 | return ParameterNames; |
1085 | } |
1086 | |
1087 | // for a ParmVarDecl from a function declaration, returns the corresponding |
1088 | // ParmVarDecl from the definition if possible, nullptr otherwise. |
1089 | static const ParmVarDecl *getParamDefinition(const ParmVarDecl *P) { |
1090 | if (auto *Callee = dyn_cast<FunctionDecl>(P->getDeclContext())) { |
1091 | if (auto *Def = Callee->getDefinition()) { |
1092 | auto I = std::distance(Callee->param_begin(), |
1093 | llvm::find(Callee->parameters(), P)); |
1094 | if (I < (int)Callee->getNumParams()) { |
1095 | return Def->getParamDecl(I); |
1096 | } |
1097 | } |
1098 | } |
1099 | return nullptr; |
1100 | } |
1101 | |
1102 | // We pass HintSide rather than SourceLocation because we want to ensure |
1103 | // it is in the same file as the common file range. |
1104 | void addInlayHint(SourceRange R, HintSide Side, InlayHintKind Kind, |
1105 | llvm::StringRef Prefix, llvm::StringRef Label, |
1106 | llvm::StringRef Suffix) { |
1107 | auto LSPRange = getHintRange(R); |
1108 | if (!LSPRange) |
1109 | return; |
1110 | |
1111 | addInlayHint(LSPRange: *LSPRange, Side, Kind, Prefix, Label, Suffix); |
1112 | } |
1113 | |
1114 | void addInlayHint(Range LSPRange, HintSide Side, InlayHintKind Kind, |
1115 | llvm::StringRef Prefix, llvm::StringRef Label, |
1116 | llvm::StringRef Suffix) { |
1117 | // We shouldn't get as far as adding a hint if the category is disabled. |
1118 | // We'd like to disable as much of the analysis as possible above instead. |
1119 | // Assert in debug mode but add a dynamic check in production. |
1120 | assert(Cfg.InlayHints.Enabled && "Shouldn't get here if disabled!" ); |
1121 | switch (Kind) { |
1122 | #define CHECK_KIND(Enumerator, ConfigProperty) \ |
1123 | case InlayHintKind::Enumerator: \ |
1124 | assert(Cfg.InlayHints.ConfigProperty && \ |
1125 | "Shouldn't get here if kind is disabled!"); \ |
1126 | if (!Cfg.InlayHints.ConfigProperty) \ |
1127 | return; \ |
1128 | break |
1129 | CHECK_KIND(Parameter, Parameters); |
1130 | CHECK_KIND(Type, DeducedTypes); |
1131 | CHECK_KIND(Designator, Designators); |
1132 | CHECK_KIND(BlockEnd, BlockEnd); |
1133 | #undef CHECK_KIND |
1134 | } |
1135 | |
1136 | Position LSPPos = Side == HintSide::Left ? LSPRange.start : LSPRange.end; |
1137 | if (RestrictRange && |
1138 | (LSPPos < RestrictRange->start || !(LSPPos < RestrictRange->end))) |
1139 | return; |
1140 | bool PadLeft = Prefix.consume_front(Prefix: " " ); |
1141 | bool PadRight = Suffix.consume_back(Suffix: " " ); |
1142 | Results.push_back(InlayHint{LSPPos, (Prefix + Label + Suffix).str(), Kind, |
1143 | PadLeft, PadRight, LSPRange}); |
1144 | } |
1145 | |
1146 | // Get the range of the main file that *exactly* corresponds to R. |
1147 | std::optional<Range> getHintRange(SourceRange R) { |
1148 | const auto &SM = AST.getSourceManager(); |
1149 | auto Spelled = Tokens.spelledForExpanded(Tokens.expandedTokens(R)); |
1150 | // TokenBuffer will return null if e.g. R corresponds to only part of a |
1151 | // macro expansion. |
1152 | if (!Spelled || Spelled->empty()) |
1153 | return std::nullopt; |
1154 | // Hint must be within the main file, not e.g. a non-preamble include. |
1155 | if (SM.getFileID(Spelled->front().location()) != SM.getMainFileID() || |
1156 | SM.getFileID(Spelled->back().location()) != SM.getMainFileID()) |
1157 | return std::nullopt; |
1158 | return Range{sourceLocToPosition(SM, Spelled->front().location()), |
1159 | sourceLocToPosition(SM, Spelled->back().endLocation())}; |
1160 | } |
1161 | |
1162 | void addTypeHint(SourceRange R, QualType T, llvm::StringRef Prefix) { |
1163 | if (!Cfg.InlayHints.DeducedTypes || T.isNull()) |
1164 | return; |
1165 | |
1166 | // The sugared type is more useful in some cases, and the canonical |
1167 | // type in other cases. |
1168 | auto Desugared = maybeDesugar(AST, QT: T); |
1169 | std::string TypeName = Desugared.getAsString(Policy: TypeHintPolicy); |
1170 | if (T != Desugared && !shouldPrintTypeHint(TypeName)) { |
1171 | // If the desugared type is too long to display, fallback to the sugared |
1172 | // type. |
1173 | TypeName = T.getAsString(Policy: TypeHintPolicy); |
1174 | } |
1175 | if (shouldPrintTypeHint(TypeName)) |
1176 | addInlayHint(R, Side: HintSide::Right, Kind: InlayHintKind::Type, Prefix, Label: TypeName, |
1177 | /*Suffix=*/"" ); |
1178 | } |
1179 | |
1180 | void addDesignatorHint(SourceRange R, llvm::StringRef Text) { |
1181 | addInlayHint(R, Side: HintSide::Left, Kind: InlayHintKind::Designator, |
1182 | /*Prefix=*/"" , Label: Text, /*Suffix=*/"=" ); |
1183 | } |
1184 | |
1185 | bool shouldPrintTypeHint(llvm::StringRef TypeName) const noexcept { |
1186 | return Cfg.InlayHints.TypeNameLimit == 0 || |
1187 | TypeName.size() < Cfg.InlayHints.TypeNameLimit; |
1188 | } |
1189 | |
1190 | void addBlockEndHint(SourceRange BraceRange, StringRef DeclPrefix, |
1191 | StringRef Name, StringRef OptionalPunctuation) { |
1192 | auto HintRange = computeBlockEndHintRange(BraceRange, OptionalPunctuation); |
1193 | if (!HintRange) |
1194 | return; |
1195 | |
1196 | std::string Label = DeclPrefix.str(); |
1197 | if (!Label.empty() && !Name.empty()) |
1198 | Label += ' '; |
1199 | Label += Name; |
1200 | |
1201 | constexpr unsigned HintMaxLengthLimit = 60; |
1202 | if (Label.length() > HintMaxLengthLimit) |
1203 | return; |
1204 | |
1205 | addInlayHint(LSPRange: *HintRange, Side: HintSide::Right, Kind: InlayHintKind::BlockEnd, Prefix: " // " , |
1206 | Label, Suffix: "" ); |
1207 | } |
1208 | |
1209 | // Compute the LSP range to attach the block end hint to, if any allowed. |
1210 | // 1. "}" is the last non-whitespace character on the line. The range of "}" |
1211 | // is returned. |
1212 | // 2. After "}", if the trimmed trailing text is exactly |
1213 | // `OptionalPunctuation`, say ";". The range of "} ... ;" is returned. |
1214 | // Otherwise, the hint shouldn't be shown. |
1215 | std::optional<Range> computeBlockEndHintRange(SourceRange BraceRange, |
1216 | StringRef OptionalPunctuation) { |
1217 | constexpr unsigned HintMinLineLimit = 2; |
1218 | |
1219 | auto &SM = AST.getSourceManager(); |
1220 | auto [BlockBeginFileId, BlockBeginOffset] = |
1221 | SM.getDecomposedLoc(SM.getFileLoc(Loc: BraceRange.getBegin())); |
1222 | auto RBraceLoc = SM.getFileLoc(Loc: BraceRange.getEnd()); |
1223 | auto [RBraceFileId, RBraceOffset] = SM.getDecomposedLoc(RBraceLoc); |
1224 | |
1225 | // Because we need to check the block satisfies the minimum line limit, we |
1226 | // require both source location to be in the main file. This prevents hint |
1227 | // to be shown in weird cases like '{' is actually in a "#include", but it's |
1228 | // rare anyway. |
1229 | if (BlockBeginFileId != MainFileID || RBraceFileId != MainFileID) |
1230 | return std::nullopt; |
1231 | |
1232 | StringRef RestOfLine = MainFileBuf.substr(Start: RBraceOffset).split('\n').first; |
1233 | if (!RestOfLine.starts_with(Prefix: "}" )) |
1234 | return std::nullopt; |
1235 | |
1236 | StringRef TrimmedTrailingText = RestOfLine.drop_front().trim(); |
1237 | if (!TrimmedTrailingText.empty() && |
1238 | TrimmedTrailingText != OptionalPunctuation) |
1239 | return std::nullopt; |
1240 | |
1241 | auto BlockBeginLine = SM.getLineNumber(FID: BlockBeginFileId, FilePos: BlockBeginOffset); |
1242 | auto RBraceLine = SM.getLineNumber(FID: RBraceFileId, FilePos: RBraceOffset); |
1243 | |
1244 | // Don't show hint on trivial blocks like `class X {};` |
1245 | if (BlockBeginLine + HintMinLineLimit - 1 > RBraceLine) |
1246 | return std::nullopt; |
1247 | |
1248 | // This is what we attach the hint to, usually "}" or "};". |
1249 | StringRef HintRangeText = RestOfLine.take_front( |
1250 | N: TrimmedTrailingText.empty() |
1251 | ? 1 |
1252 | : TrimmedTrailingText.bytes_end() - RestOfLine.bytes_begin()); |
1253 | |
1254 | Position HintStart = sourceLocToPosition(SM, Loc: RBraceLoc); |
1255 | Position HintEnd = sourceLocToPosition( |
1256 | SM, Loc: RBraceLoc.getLocWithOffset(Offset: HintRangeText.size())); |
1257 | return Range{.start: HintStart, .end: HintEnd}; |
1258 | } |
1259 | |
1260 | static bool isFunctionObjectCallExpr(CallExpr *E) noexcept { |
1261 | if (auto *CallExpr = dyn_cast<CXXOperatorCallExpr>(E)) |
1262 | return CallExpr->getOperator() == OverloadedOperatorKind::OO_Call; |
1263 | return false; |
1264 | } |
1265 | |
1266 | std::vector<InlayHint> &Results; |
1267 | ASTContext &AST; |
1268 | const syntax::TokenBuffer &Tokens; |
1269 | const Config &Cfg; |
1270 | std::optional<Range> RestrictRange; |
1271 | FileID MainFileID; |
1272 | StringRef MainFileBuf; |
1273 | const HeuristicResolver *Resolver; |
1274 | PrintingPolicy TypeHintPolicy; |
1275 | }; |
1276 | |
1277 | } // namespace |
1278 | |
1279 | std::vector<InlayHint> inlayHints(ParsedAST &AST, |
1280 | std::optional<Range> RestrictRange) { |
1281 | std::vector<InlayHint> Results; |
1282 | const auto &Cfg = Config::current(); |
1283 | if (!Cfg.InlayHints.Enabled) |
1284 | return Results; |
1285 | InlayHintVisitor Visitor(Results, AST, Cfg, std::move(RestrictRange)); |
1286 | Visitor.TraverseAST(AST.getASTContext()); |
1287 | |
1288 | // De-duplicate hints. Duplicates can sometimes occur due to e.g. explicit |
1289 | // template instantiations. |
1290 | llvm::sort(Results); |
1291 | Results.erase(std::unique(Results.begin(), Results.end()), Results.end()); |
1292 | |
1293 | return Results; |
1294 | } |
1295 | |
1296 | } // namespace clangd |
1297 | } // namespace clang |
1298 | |