Skip to content

Commit 808c141

Browse files
committed
[clangd] Don't show inlay hints for PseudoObjectExprs in C++
This closes clangd/clangd#1813. PseudoObjectExprs in C++ are currently not very interesting but probably mess up inlay hints.
1 parent 89ecb80 commit 808c141

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

clang-tools-extra/clangd/InlayHints.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,19 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
589589
return true;
590590
}
591591

592+
bool dataTraverseStmtPre(Stmt *S) {
593+
// Do not show inlay hints for PseudoObjectExprs. They're never
594+
// genuine user codes in C++.
595+
//
596+
// For example, __builtin_dump_struct would expand to a PseudoObjectExpr
597+
// that includes a couple of calls to a printf function. Printing parameter
598+
// names for that anyway would end up with duplicate parameter names (which,
599+
// however, got de-duplicated after visiting) for the printf function.
600+
if (AST.getLangOpts().CPlusPlus && isa<PseudoObjectExpr>(S))
601+
return false;
602+
return true;
603+
}
604+
592605
bool VisitCallExpr(CallExpr *E) {
593606
if (!Cfg.InlayHints.Parameters)
594607
return true;

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1724,6 +1724,30 @@ TEST(InlayHints, RestrictRange) {
17241724
ElementsAre(labelIs(": int"), labelIs(": char")));
17251725
}
17261726

1727+
TEST(ParameterHints, PseudoObjectExpr) {
1728+
Annotations Code(R"cpp(
1729+
struct S {
1730+
__declspec(property(get=GetX, put=PutX)) int x[];
1731+
int GetX(int y, int z) { return 42 + y; }
1732+
void PutX(int y) { x = y; } // Not `x = y: y`
1733+
};
1734+
1735+
int printf(const char *Format, ...);
1736+
1737+
int main() {
1738+
S s;
1739+
__builtin_dump_struct(&s, printf); // Not `Format: __builtin_dump_struct()`
1740+
printf($Param[["Hello, %d"]], 42); // Normal calls are not affected.
1741+
return s.x[1][2]; // Not `x[y: 1][z: 2]`
1742+
}
1743+
)cpp");
1744+
auto TU = TestTU::withCode(Code.code());
1745+
TU.ExtraArgs.push_back("-fms-extensions");
1746+
auto AST = TU.build();
1747+
EXPECT_THAT(inlayHints(AST, std::nullopt),
1748+
ElementsAre(HintMatcher(ExpectedHint{"Format: ", "Param"}, Code)));
1749+
}
1750+
17271751
TEST(ParameterHints, ArgPacksAndConstructors) {
17281752
assertParameterHints(
17291753
R"cpp(

0 commit comments

Comments
 (0)