diff --git a/docs/en/features/inlay-hints.md b/docs/en/features/inlay-hints.md index c27d81bb97..eac5960f30 100644 --- a/docs/en/features/inlay-hints.md +++ b/docs/en/features/inlay-hints.md @@ -1,209 +1,974 @@ # Inlay Hints -## Parameter Name Hints + -- [x] Named parameter hints at call sites -- [x] Skip when argument name matches parameter name -- [x] Skip for single-parameter calls with obvious semantics -- [x] Expand parameter packs (`underlying_pack_type` detection) -- [x] Resolve parameter names from definition (not just declaration) -- [x] Forwarding function parameter resolution — show the underlying constructor parameters for `std::make_unique`, `emplace_back`, etc. ([clangd#2324](https://github.com/clangd/clangd/issues/2324)) +clice renders inline annotations for the information the code leaves implicit: parameter names at call sites, deduced types, and the field names behind positional aggregate initialization. Hint categories can be toggled individually through the `[inlay_hints]` configuration section; the sections below describe the categories that are on by default. + +## Parameter Hints + + + +- [x] Parameter name hints — argument names at call sites and constructor calls ```cpp - struct Widget { Widget(int width, int height); }; - auto p = std::make_unique(800, 600); - // ^^^ ^^^ - // width: 800, height: 600 (not __args: 800, __args: 600) + void draw(int width, int height); + + struct Point { + Point(int x, int y); + Point(const Point& other); + Point(Point&& other); + }; + + void use() { + draw(10, 20); + Point p(1, 2); + Point q{3, 4}; + // Copy and move constructors stay quiet; a temporary's own braces + // still hint (the outer prvalue construction is elided anyway). + Point r(p); + Point m(Point{5, 6}); + Point s(static_cast(r)); + } ``` -- [x] Function pointer and `operator()` parameter hints ([clangd#1734](https://github.com/clangd/clangd/issues/1734), [clangd#1742](https://github.com/clangd/clangd/issues/1742)) +- [x] Hint suppression — arguments that already spell the parameter name, and `/*name=*/` comments ([clangd#1877](https://github.com/clangd/clangd/issues/1877)) ```cpp - void (*callback)(int status, const char* msg); - callback(0, "ok"); - // ^ ^^ - // status: 0, msg: "ok" + void draw(int width, int height); + + void use() { + int width = 5; + int h = 2; + // `width` matches the parameter spelling: only `height:` hints. + draw(width, h); + // An inline comment naming the parameter serves the same purpose; + // a comment naming something else does not. + draw(/*width=*/1, /*height=*/2); + draw(/*margin=*/6, 7); + } + + struct Sizes { + static int width; + int height; + + void member() { + // A bare member access spells the parameter name: suppressed. + draw(5, height); + } + }; + + void qualified(Sizes s) { + // A qualified name is not a plain spelling match. + draw(Sizes::width, 3); + // Neither is an access through a written base object. + draw(4, s.height); + } + ``` + +- [x] Setter and builtin suppression — `setX(x)` and `std::move`/`std::forward` arguments stay bare - auto cmp = [](int a, int b) { return a < b; }; - cmp(1, 2); - // ^ ^ - // a: 1, b: 2 + ```cpp + namespace std { + + template + struct remove_reference { + using type = T; + }; + + template + struct remove_reference { + using type = T; + }; + + template + struct remove_reference { + using type = T; + }; + + template + constexpr T&& forward(typename remove_reference::type& t) noexcept; + + template + constexpr typename remove_reference::type&& move(T&& t) noexcept; + + } // namespace std + + struct Config { + void setWidth(int width); + void set_height(int height); + // The parameter carries extra information beyond the setter name, so + // it still hints. + void setTimeout(int timeout_millis); + }; + + void consume(int&& sink); + + // The three-argument algorithm form of std::move is a real call whose + // parameters deserve hints; only the single-argument cast stays bare. + namespace std { + + template + T* move(T* first, T* last, T* result); + + } // namespace std + + void use(Config& config) { + config.setWidth(3); + config.set_height(4); + config.setTimeout(5); + int value = 1; + consume(std::move(value)); + int buffer[4]; + std::move(buffer, buffer + 2, buffer + 2); + } ``` -- [ ] Template parameter hints — show deduced/explicit template arguments ([clangd#2583](https://github.com/clangd/clangd/issues/2583)) +- [x] Mutable reference markers — `&` flags arguments passed by non-const lvalue reference ([clangd#1123](https://github.com/clangd/clangd/issues/1123)) ```cpp - template - auto convert(U val) -> T; - convert(42); - // ^^^^^ ^^ - // T: float, val: 42 + void mutate(int& value); + void observe(const int& value); + void take(int&& value); + + void use() { + int v = 0; + mutate(v); + observe(v); + take(static_cast(v)); + } ``` -- [ ] Case-insensitive parameter name matching — `aParam` should suppress hint for argument `param` ([clangd#2248](https://github.com/clangd/clangd/issues/2248)) +- [x] Forwarding resolution — packs forwarded through wrappers resolve to the target's parameter names ([clangd#2324](https://github.com/clangd/clangd/issues/2324)) + + ```cpp + namespace std { + + template + struct remove_reference { + using type = T; + }; + + template + constexpr T&& forward(typename remove_reference::type& t) noexcept; + + } // namespace std + + void target(int first, int second); -- [x] Suppress hints when an inline comment already names the parameter ([clangd#1877](https://github.com/clangd/clangd/issues/1877)) + template + void wrap(Args&&... args) { + target(std::forward(args)...); + } + + // A plain pass-through works without std::forward as well. + void sink(int a, int b, int c); + + template + void call_with(Ts... ts) { + sink(ts...); + } + + // Forwarding also resolves through packs sandwiched between fixed + // head and tail arguments. + int accumulate(int, int b, double); + + template + int head_tail(int a, Args&&... args) { + return accumulate(1, std::forward(args)..., 1.0); + } + + template + int chain(Args&&... args) { + return head_tail(std::forward(args)...); + } + + void use() { + wrap(1, 2); + call_with(1, 2, 3); + chain(32, 42); + } + ``` + +- [x] Names from definitions — unnamed declaration parameters take the definition's names; leading underscores strip ```cpp - draw(/*x=*/10, /*y=*/20); // no hints needed — inline comments serve the same purpose + void resize(int, int); + + void fill(int _value, int __count); + + int scale(int good); + + void use() { + resize(800, 600); + fill(1, 2); + // When both name their parameter, the declaration wins. + scale(7); + } + + void resize(int width, int height) {} + + int scale(int bad) { + return bad; + } ``` -- [x] Default argument value hints +- [x] Function pointers and call operators — indirect calls still name their parameters ([clangd#1734](https://github.com/clangd/clangd/issues/1734), [clangd#1742](https://github.com/clangd/clangd/issues/1742)) -## Type Hints + ```cpp + struct Callback { + void operator()(int status, int detail) const; + }; + + void (*handler)(int status, const char* message); + + void use() { + Callback cb; + cb(1, 2); + cb.operator()(3, 4); + handler(0, "ok"); + auto cmp = [](int lhs, int rhs) { return lhs < rhs; }; + cmp(1, 2); + } + ``` -- [x] `auto` deduced type hints -- [x] Structured binding type hints -- [x] Lambda return type hints -- [x] Range-based for loop variable type hints -- [ ] Lambda init-capture type hints ([clangd#1163](https://github.com/clangd/clangd/issues/1163)) +- [x] Deducing `this` — the explicit object parameter never hints (C++23) ([clangd#1777](https://github.com/clangd/clangd/issues/1777)) ```cpp - auto f = [val = compute()] {}; - // ^^^ : int + struct Widget { + void resize(this Widget& self, int width, int height); + }; + + void use() { + Widget w; + w.resize(800, 600); + } ``` -- [ ] Dependent `auto` type hints — show deduced type when useful even in template bodies ([clangd#2275](https://github.com/clangd/clangd/issues/2275)) +- [x] Dependent calls — parameter names appear even when the callee is only known inside a template + + Candidates are matched by argument count; only a unique surviving + candidate names the parameters, so a call that could still hit several + overloads stays bare rather than guessing. -- [x] Don't show type hint when deduction fails ([clangd#1475](https://github.com/clangd/clangd/issues/1475)) + ```cpp + template + void apply(T scale); + + template + struct Holder { + void member(T item); + static void static_member(T slot); + }; + + void overload(int value); + void overload(double value); + + template + struct Runner { + void run(Holder holder, T value) { + apply(value); + holder.member(value); + Holder::static_member(value); + // Several overloads remain viable: no hint. + overload(T{}); + } + }; + ``` -- [ ] Don't show type hint when the type is explicitly specified ([clangd#1749](https://github.com/clangd/clangd/issues/1749)) +- [x] Unexpanded packs — a written pack expansion breaks the 1:1 argument mapping and stops hinting ```cpp - auto x = static_cast(val); // no type hint — already explicit - auto y = int{42}; // same + void plot(int x, int y, int z); + + template + void relay(Ts... ts) { + // `ts...` may instantiate to any number of arguments. + plot(0, ts...); + } + + void use() { + // The outer call still resolves through pack forwarding: 1 and 2 land + // in plot's y and z. + relay(1, 2); + } ``` -- [ ] Prefer desugared types — show `std::vector` rather than a typedef alias ([clangd#1298](https://github.com/clangd/clangd/issues/1298), [clangd#1668](https://github.com/clangd/clangd/issues/1668)) +- [x] Macros at call sites — arguments spelled as macros hint; calls generated inside macro bodies do not ([clangd#2620](https://github.com/clangd/clangd/issues/2620)) ```cpp - using IntVec = std::vector; - IntVec create(); - auto v = create(); - // ^ : std::vector (not IntVec) + void report(double value); + void plot(double x, double y); + int check(int status); + + #define PI 3.14 + #define CALL_REPORT() report(2.71) + #define PAIR 1.0, 2.0 + #define ASSERT(expr) if(!(expr)) {} + + void use() { + // An object-like macro is still one written argument. + report(PI); + // The call only exists inside the macro body. + CALL_REPORT(); + // One macro covering several arguments has no place to anchor. + plot(PAIR); + // Code written as a macro argument keeps its hints. + ASSERT(check(42) == 0); + } ``` -- [x] Configurable type hint length limit ([clangd#1357](https://github.com/clangd/clangd/issues/1357)) +- [x] Implicit constructor calls — conversions the code never wrote produce no hints of their own + + ```cpp + struct Seconds { + Seconds(int raw); + }; + + void wait(Seconds); + void hold(Seconds duration); + + Seconds use() { + // The implicit Seconds(5) must not surface `raw:`. + wait(5); + // The written call still hints its own parameter. + hold(6); + // Nor does the conversion in a return statement. + return 7; + } + ``` -- [ ] Abbreviated type hints with expandable label parts — use LSP `InlayHintLabelPart` to allow expanding truncated types ([clangd#2269](https://github.com/clangd/clangd/issues/2269)) +- [x] Pseudo-object expressions — MS property accesses stay quiet; written subscripts keep the accessor's names + ```cpp + int printf(const char* Format, ...); + + struct State { + __declspec(property(get = GetX, put = PutX)) int x[]; + int GetX(int row, int column); + void PutX(int value); + + // The syntactic form is a binary operator: no `value:` hint on `y`. + void Work(int y) { + x = y; + } + }; + + int use() { + State s; + // The semantic form of __builtin_dump_struct calls printf; none of it + // is written here. + __builtin_dump_struct(&s, printf); + printf("%d", 42); + // Property subscripts read best with the accessor's parameter names. + return s.x[1][2]; + } ``` - auto it = map.begin(); - // ^^ : map::iterator [click to expand] + +- [x] Explicit instantiation — an explicit instantiation definition adds no duplicate hints ([clangd#1034](https://github.com/clangd/clangd/issues/1034)) + + ```cpp + template + void apply(T value) {} + + template void apply(int value); + + void use() { + apply(42); + } ``` -- [ ] Shorten hints by suppressing obvious scopes ([clangd#2270](https://github.com/clangd/clangd/issues/2270)) +- [ ] Sloppy name matching — `aParam` does not yet suppress an argument spelled `param` _(partial)_ ([clangd#2248](https://github.com/clangd/clangd/issues/2248)) ```cpp - // inside namespace foo: - auto x = create(); - // ^ : Bar (not foo::Bar) + void draw(int aParam); + + void use() { + int param = 3; + // Ideally the near-match would suppress the hint; today it still shows. + draw(param); + } ``` -## Designator Hints +- [ ] Inherited constructors — `using Base::Base` calls lose their parameter names _(partial)_ ([clangd#1364](https://github.com/clangd/clangd/issues/1364)) + + ```cpp + struct Base { + Base(int width); + }; + + struct Derived : Base { + using Base::Base; + }; + + // No `width:` hint yet. + Derived d(7); + ``` -- [ ] Aggregate initializer designators (`.field =`) — show field names for positional aggregate initialization +- [x] Anonymous parameters — nothing to name, though a mutable reference still flags `&` ```cpp - struct Point { int x, y, z; }; - Point p = {1, 2, 3}; - // ^ ^ ^ - // .x = 1, .y = 2, .z = 3 + void value_sink(int); + void ref_sink(int&); + void const_ref_sink(const int&); + void rvalue_sink(int&&); + + void use() { + int v = 0; + value_sink(1); + // Only the `&` marker survives without a name. + ref_sink(v); + const_ref_sink(v); + rvalue_sink(2); + } ``` -- [ ] Parenthesized aggregate initialization (C++20) ([clangd#2540](https://github.com/clangd/clangd/issues/2540)) +- [x] Operators and literals — operator syntax and user-defined literals stay bare; member and default member initializers hint ```cpp - Point p(1, 2, 3); - // ^ ^ ^ - // .x = 1, .y = 2, .z = 3 + struct S { + S(int param); + }; + + void operator+(S lhs, S rhs); + + long double operator""_w(long double param); + + struct Holder { + S member; + S defaulted{3}; + Holder() : member(42) {} + }; + + void use() { + S a(1); + S b(2); + a + b; + 1.2_w; + } ``` -- [ ] Compact array designator formatting ([clangd#2303](https://github.com/clangd/clangd/issues/2303)) +- [ ] Packs in constructor arguments — outer calls resolve; hints inside the expansion are still missing _(partial)_ ```cpp - int arr[] = {10, 20, 30}; - // ^^ ^^ ^^ - // [0]= 10, [1]= 20, [2]= 30 + struct Foo { + Foo(); + Foo(int x); + }; + + void consume(Foo a, int b); + + template + void relay(Args... args) { + consume(args...); + } + + template + void construct(Args... args) { + // The written Foo{args...} and the literal after it get no hints yet. + consume(Foo{args...}, 1); + } + + void use() { + relay(Foo{}, 42); + relay(42, 42); + construct(42); + } ``` -## Implicit Conversion Hints + -- [ ] Show implicit type conversions at call sites and assignments ([clangd#2254](https://github.com/clangd/clangd/issues/2254)) +## Type Hints + + + +- [x] Deduced `auto` variables — the hint shows the full variable type, qualifiers included ```cpp - void process(double val); - process(42); - // ^^ - // (double) 42 — implicit int→double conversion + int make(); - std::string s = "hello"; - // ^^^^^^^ - // (std::string) "hello" — implicit const char*→string conversion + void use() { + auto value = make(); + const auto& ref = value; + auto* ptr = &value; + } ``` -## Reference / Pointer Hints +- [x] Type sugar and the length limit — aliases keep their spelling; over-long types fall back to the sugared name ([clangd#1298](https://github.com/clangd/clangd/issues/1298), [clangd#1357](https://github.com/clangd/clangd/issues/1357)) + + ```cpp + using Integer = int; + + Integer make_alias(); + + template + struct extremely_long_template_name {}; + + using Compact = extremely_long_template_name; + + Compact make_compact(); + + extremely_long_template_name make_long(); + + template + struct Defaulted {}; -- [x] Show `&` / `&&` to indicate the argument is passed by mutable reference ([clangd#1123](https://github.com/clangd/clangd/issues/1123)) + Defaulted make_defaulted(); + + void use() { + auto aliased = make_alias(); + auto shortened = make_compact(); + // No sugar short enough to fall back to: the hint is dropped. + auto dropped = make_long(); + // Default template arguments never print. + auto defaulted = make_defaulted(); + } + ``` + +- [x] Structured bindings — each binding hints its canonical type; the aggregate itself stays bare ```cpp - void sort(std::vector& v); - sort(data); - // ^^^^ - // &data — passed by mutable reference + struct Pair { + int first; + float second; + }; + + Pair make(); + + int array[2]; + + void use() { + auto [a, b] = make(); + auto [x, y] = array; + } ``` -## Template Argument Hints +- [x] Lambdas — variables, deduced return types, and init-captures all hint ([clangd#1163](https://github.com/clangd/clangd/issues/1163)) -- [ ] CTAD deduced template arguments ([clangd#2331](https://github.com/clangd/clangd/issues/2331)) + ```cpp + int compute(); + + void use() { + auto callback = [captured = compute()](int x) { + return x + captured; + }; + auto bare = [] { + return 1.5; + }; + } + ``` + +- [x] Deduced return types — `-> T` after the parameter list, declarations included ```cpp - std::pair p(1, 3.14); - // ^ + auto answer() { + return 42; + } + + auto& ref_answer() { + static int storage = 0; + return storage; + } + + // A declaration hints once a later definition supplies the deduction; a + // definition-less one stays silent. + auto declared(int x); + auto deducible(int x); + + auto deducible(int x) { + return x + 1; + } + + // Written trailing return types need no hint. + auto spelled() -> int; + auto pointer() -> auto* { + return "text"; + } + + struct Convertible { + operator auto() { + return 42; + } + }; ``` -## Block End Hints +- [x] `decltype` spellings — the underlying type shows next to the written `decltype` + + ```cpp + int source(); + + decltype(source()) value = 1; + + int& ref = value; + // decltype(auto) preserves the reference. + decltype(auto) forwarded = ref; + + // Every written decltype spelling hints: declarators, alias targets, + // return types and functional casts. + const decltype(0)& bound = value; + + decltype(0) declared(); + + auto trailing() -> decltype(0); + + template + struct Wrap; + + using Alias = Wrap; + + auto constructed = decltype(0){}; + ``` + +- [x] `auto` parameters — a template with exactly one instantiation reveals the deduced type + + ```cpp + int twice(auto x) { + return x + x; + } + + int result = twice(21); + + // A second instantiation makes the deduction ambiguous: no hint. + int measure(auto x) { + return 1; + } + + int a = measure(1); + int b = measure(2.0); + + // Packs and parameters after them never hint. + int spread(auto first, auto... rest, auto last) { + return 0; + } + + int c = spread(nullptr, 'x', 2.0f, 3); + + // Deduplication: a template body hints once across instantiations of the + // same deduced type. + template + void body() { + auto var = 42; + } + + template void body(); + template void body(); + ``` + +- [ ] Explicitly spelled initializers — casts and functional casts still hint redundantly _(partial)_ ([clangd#1749](https://github.com/clangd/clangd/issues/1749)) + + ```cpp + int compute(); + + void use() { + // The type is already written on the right-hand side; ideally these + // two hints would be suppressed. + auto widened = static_cast(compute()); + auto braced = int{42}; + } + ``` -- [x] Show the declared name after a closing brace for long blocks ([clangd#1634](https://github.com/clangd/clangd/issues/1634)) +- [ ] Dependent `auto` — deduction inside an uninstantiated template body stays silent _(partial)_ ([clangd#2275](https://github.com/clangd/clangd/issues/2275)) ```cpp - void Widget::processData(const Config& cfg) { - // ... 50+ lines ... - } // Widget::processData + template + void body(T input) { + // No hint: the deduced type depends on T. + auto derived = input + 1; + // A dependence-free initializer still hints normally. + auto counter = 0; + } ``` -- [ ] `#endif` hints — show the corresponding macro condition ([clangd#2487](https://github.com/clangd/clangd/issues/2487)) +- [x] Scope suppression — namespace qualifiers drop from hints; class scopes stay ```cpp - #ifdef _WIN32 - // ... - #endif // _WIN32 + namespace outer { + namespace inner { + + struct S1 {}; + S1 make_s1(); + auto x = make_s1(); + + struct S2 { + template + struct Nested {}; + }; + + S2::Nested make_nested(); + auto y = make_nested(); + + } // namespace inner + } // namespace outer ``` -## Interactive Hints +- [x] Tuple-protocol bindings — hints print the canonical type, not `tuple_element::type` + + ```cpp + struct IntPair { + int a; + int b; + }; + + namespace std { + + template + struct tuple_size {}; + + template <> + struct tuple_size { + constexpr static unsigned value = 2; + }; + + template + struct tuple_element {}; -- [x] Range-filtered results (hints outside the requested range are discarded; AST traversal is not range-limited) -- [x] Left-anchored hints (parameter names before argument) -- [x] Right-anchored hints (types after variable name) -- [ ] Clickable type names — go-to-definition on hinted type names via LSP `InlayHintLabelPart` ([clangd#1535](https://github.com/clangd/clangd/issues/1535)) + template + struct tuple_element { + using type = int; + }; + } // namespace std + + template + int get(const IntPair& p) { + if constexpr(I == 0) { + return p.a; + } else { + return p.b; + } + } + + IntPair make(); + + auto [x, y] = make(); ``` - auto widget = create(); - // ^^^^^^ : Widget ← click "Widget" to go to Widget's definition + + + +## Designator Hints + + + +- [x] Field and index designators — positional aggregate initialization shows `.field=` and `[index]=` ([clangd#2303](https://github.com/clangd/clangd/issues/2303)) + + ```cpp + struct Point { + int x; + int y; + int z; + }; + + Point p{1, 2 + 2}; + + int coordinates[2] = {7, 8}; + + // Array designators survive dependent-sized members; reserved names are + // skipped rather than printed. + template + struct Array { + T __elements[N]; + }; + + Array pair = {0, 1}; + ``` + +- [x] Nested aggregates — written braces recurse; omitted braces flatten into `.outer.inner=` + + ```cpp + struct Inner { + int x; + int y; + }; + + struct Outer { + Inner a; + Inner b; + }; + + Outer o{{1, 2}, 3}; + ``` + +- [x] Anonymous members — unnamed unions and structs vanish from the designator path + + ```cpp + struct State { + union { + struct { + struct { + int y; + }; + } x; + }; + }; + + State s{42}; ``` -## Hint Correctness +- [x] Designator suppression — written designators and `/*name=*/` comments keep their inits bare + + ```cpp + struct Point { + int a; + int b; + int c; + int d; + int e; + }; + + // Mixing written designators with positional inits is a C99 extension + // clang accepts with a warning; only the bare `4` needs help. + Point p{/*a=*/1, .c = 2, /* .d = */ 3, 4}; + ``` + +- [x] Aggregates only — constructor calls, copies and idiomatic zero-init produce no designators + + ```cpp + struct Constructible { + Constructible(int amount); + }; + + // A braced constructor call names parameters, not fields. + Constructible built{5}; + + struct Copyable { + int x; + }; + + Copyable original{1}; + Copyable duplicate{original}; + + // The idiomatic `{}` zero-initializer stays quiet. + struct Wide { + int fields[8]; + }; + + Wide zeroed{}; + ``` + +- [x] Broken initializers — designators survive next to initializers that fail to compile + + ```cpp + // error-ok: the first initializer deliberately fails to convert. + struct Empty {}; + + struct Mixed { + int a; + int b; + }; + + void use() { + Mixed m{Empty(), 1}; + } + ``` + +- [ ] Parenthesized aggregate initialization — C++20 `Point(1, 2)` gets no hints yet ([clangd#2540](https://github.com/clangd/clangd/issues/2540)) + + ```cpp + struct Point { + int x; + int y; + }; + + Point p(1, 2); + ``` + + + +## Other Hint Kinds + + + +- [ ] Template parameter hints — deduced and explicit template arguments at call sites ([clangd#2583](https://github.com/clangd/clangd/issues/2583)) + + ```cpp + template + T convert(U val); + + // Could hint `T: float` next to the explicit argument list. + float converted = convert(42); + ``` + +- [ ] CTAD arguments — deduced class template arguments after the template name ([clangd#2331](https://github.com/clangd/clangd/issues/2331)) + + ```cpp + template + struct Pair { + A first; + B second; + Pair(A a, B b); + }; + + // Could hint `` after `pair`. + Pair pair(1, 2.5); + ``` + +- [ ] Implicit conversion hints — surface the conversions a call site performs ([clangd#2254](https://github.com/clangd/clangd/issues/2254)) + + ```cpp + void process(double val); + + // Could hint `(double)` before the argument. + void use() { + process(42); + } + ``` + + + +## Block End Hints + +Off by default (`inlay_hints.block_end`). After the closing brace of a block spanning at least two lines, clice shows the name of what the brace closes — functions, types, namespaces, and control-flow statements: + +```cpp +void Widget::process(const Config& cfg) { + // ... +} // Widget::process + +namespace detail { + // ... +} // namespace detail + +while (running) { + // ... +} // while running +``` + +Condition summaries print for `if`/`while`/`switch`/`for` where a short spelling exists; an `else if` chain hints as plain `// if`. Labels longer than 60 characters are dropped. + +A related idea, `#endif` hints showing the matching condition ([clangd#2487](https://github.com/clangd/clangd/issues/2487)), is not implemented. + +## Default Argument Hints + +Off by default (`inlay_hints.default_arguments`). Call sites that rely on default arguments show what was omitted, abbreviated past the type-name limit: + +```cpp +void log(int level, bool flush = true, int repeat = 1); +log(2); +// ^ , flush: true, repeat: 1 +``` + +## Configuration + +The `[inlay_hints]` section of `clice.toml` (or the same keys via `initializationOptions`) controls every category: `enabled`, `parameters`, `deduced_types`, `designators`, `block_end`, `default_arguments`, and `type_name_limit`. See the [configuration guide](../guide/configuration.md#inlay-hints) for details. Configuration changes take effect after a server restart — a recompile is never involved. + +## Interactive Behavior + +- Requests are range-scoped: hints outside the requested range are discarded. +- Parameter hints anchor to the left of their argument; type and designator hints anchor to their declaration side with LSP padding flags instead of embedded spaces. +- Identical duplicate hints (e.g. from template instantiations) collapse into one. -Known issues that should be handled correctly: +## Other Known Gaps -- [ ] Nested macro invocations must not produce incorrect parameter name hints ([clangd#2620](https://github.com/clangd/clangd/issues/2620)) -- [ ] No duplicate hints in the presence of explicit function template instantiation ([clangd#1034](https://github.com/clangd/clangd/issues/1034)) -- [ ] Parameter hints for inherited constructors called from derived classes ([clangd#1364](https://github.com/clangd/clangd/issues/1364)) -- [ ] Parameter hints must not be lost when a coroutine returns a template type ([clangd#2437](https://github.com/clangd/clangd/issues/2437)) -- [x] C++23 deducing `this` — strip explicit object parameter from hint display ([clangd#1777](https://github.com/clangd/clangd/issues/1777)) +- Abbreviated type hints with expandable label parts via `InlayHintLabelPart` ([clangd#2269](https://github.com/clangd/clangd/issues/2269)) +- Clickable type names — go-to-definition on the hinted type ([clangd#1535](https://github.com/clangd/clangd/issues/1535)) +- Scope-aware type shortening — print `Bar` instead of `foo::Bar` inside `namespace foo` ([clangd#2270](https://github.com/clangd/clangd/issues/2270)) +- Parameter hints lost when a coroutine returns a template type ([clangd#2437](https://github.com/clangd/clangd/issues/2437)) ## Changelog -| Date | Change | PR | -| ---- | ------------------------------------------------------ | --- | -| — | Parameter name hints, type hints, range-scoped queries | — | +| Date | Change | PR | +| ---------- | ------------------------------------------------------------------------------------------------------- | --- | +| — | Parameter name hints, type hints, range-scoped queries | — | +| 2026-07-31 | Designator hints, dependent-call parameter hints, `[inlay_hints]` configuration, fixture-generated docs | — | diff --git a/docs/en/guide/configuration.md b/docs/en/guide/configuration.md index b0c3c0149a..99c1ea2dcb 100644 --- a/docs/en/guide/configuration.md +++ b/docs/en/guide/configuration.md @@ -130,6 +130,66 @@ Interval for re-checking the compilation database file. Interval for sweeping workspace files for on-disk changes. +## Inlay Hints + +The `[inlay_hints]` section controls which inlay hint categories the server produces. Configuration changes take effect after a server restart; a client-side refresh then requests hints with the updated values. No recompile is involved. + +### `inlay_hints.enabled` + +| Type | Default | +| ------ | ------- | +| `bool` | `true` | + +Master switch: `false` disables all inlay hints. + +### `inlay_hints.parameters` + +| Type | Default | +| ------ | ------- | +| `bool` | `true` | + +Parameter name hints at call sites, e.g. `draw(width: 800, height: 600)`, including `&` markers for arguments passed by mutable reference. + +### `inlay_hints.deduced_types` + +| Type | Default | +| ------ | ------- | +| `bool` | `true` | + +Deduced type hints for `auto` variables, structured bindings, and deduced return types. + +### `inlay_hints.designators` + +| Type | Default | +| ------ | ------- | +| `bool` | `true` | + +Field designator hints in aggregate initialization, e.g. `Point{.x=1, .y=2}` for `Point{1, 2}`. + +### `inlay_hints.block_end` + +| Type | Default | +| ------ | ------- | +| `bool` | `false` | + +`// name` hints after the closing brace of long blocks (functions, types, namespaces, control flow). + +### `inlay_hints.default_arguments` + +| Type | Default | +| ------ | ------- | +| `bool` | `false` | + +Show the default arguments a call omitted, abbreviated when long. + +### `inlay_hints.type_name_limit` + +| Type | Default | +| -------- | ------- | +| `uint32` | `32` | + +Maximum length for printed type names; longer types fall back to a sugared spelling or are dropped. `0` means no limit. + ## Rules `[[rules]]` is an array of rule objects. Rules are matched in declaration order — later rules override earlier ones. diff --git a/src/feature/inlay_hints.cpp b/src/feature/inlay_hints.cpp index a2190fd24e..2e8ba44c71 100644 --- a/src/feature/inlay_hints.cpp +++ b/src/feature/inlay_hints.cpp @@ -8,15 +8,23 @@ #include #include +#include "compile/compilation_unit.h" #include "feature/feature.h" #include "semantic/decls.h" #include "semantic/display.h" -#include "semantic/filtered_ast_visitor.h" +#include "semantic/resolver.h" +#include "semantic/semantics.h" #include "semantic/types.h" +#include "llvm/ADT/DenseSet.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Support/Casting.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/Expr.h" +#include "clang/AST/ExprCXX.h" #include "clang/Lex/Lexer.h" +#include "clang-tidy/utils/DesignatedInitializers.h" namespace clice::feature { @@ -73,15 +81,37 @@ bool is_setter(const clang::FunctionDecl* callee, // Checks if the callee is one of the builtins // addressof, as_const, forward, move(_if_noexcept) +// +// The real criterion is "so common and so obvious that a parameter hint is +// pure noise", and neither the builtin-ID check nor the name fallback below +// expresses it well: the ID set is whatever clang happens to fold, and the +// single-parameter guard only shields the std::move algorithm overload. +// FIXME: Replace both with a curated suppression table (qualified name + +// signature shape) so the set is explicit and extensible beyond cast-like +// single-parameter forms. bool is_simple_builtin(const clang::FunctionDecl* callee) { switch(callee->getBuiltinID()) { case clang::Builtin::BIaddressof: case clang::Builtin::BIas_const: case clang::Builtin::BIforward: + case clang::Builtin::BIforward_like: case clang::Builtin::BImove: case clang::Builtin::BImove_if_noexcept: return true; - default: return false; + default: break; } + + // Freestanding compiles (-ffreestanding) strip library-builtin IDs, so + // match the same set by name; their arguments stay uninteresting either + // way. All of these are single-parameter cast-like forms — the check + // must not swallow e.g. the three-argument std::move algorithm, whose + // parameters are worth hinting. + if(callee->getNumParams() == 1 && callee->isInStdNamespace()) { + llvm::StringRef name = display::identifier_of(callee); + return name == "addressof" || name == "as_const" || name == "forward" || + name == "forward_like" || name == "move" || name == "move_if_noexcept"; + } + + return false; } struct Callee { @@ -91,14 +121,51 @@ struct Callee { clang::FunctionProtoTypeLoc loc; }; -class Builder { +/// Collects hints by walking the unit's cached Semantics node table — the +/// DFS pre-order record of the interested file's written AST — instead of +/// running another RecursiveASTVisitor over the TU. The table stores only +/// structure; everything a hint needs is derived from the recorded node. +class Collector { public: - Builder(std::vector& result, - CompilationUnitRef unit, - LocalSourceRange restrict_range, - const InlayHintsOptions& options) : + Collector(std::vector& result, + CompilationUnitRef unit, + LocalSourceRange restrict_range, + const InlayHintsOptions& options) : result(result), unit(unit), restrict_range(restrict_range), options(options) {} + void run() { + auto nodes = unit.semantics().node_entries(); + std::uint32_t index = 0; + while(index < nodes.size()) { + const Semantics::Node& entry = nodes[index]; + + // The preprocessor segment follows the AST segment; directives + // produce no hints. + if(!entry.node.is_ast()) { + break; + } + + switch(entry.node.kind()) { + case SemanticNode::Kind::Decl: handle_decl(entry.node.get()); break; + + case SemanticNode::Kind::Stmt: + if(!handle_stmt(entry.node.get())) { + index = entry.subtree_end; + continue; + } + break; + + case SemanticNode::Kind::TypeLoc: + handle_type_loc(*entry.node.get()); + break; + + default: break; + } + + index += 1; + } + } + private: // Get the range of the main file that *exactly* corresponds to R. std::optional hint_range(clang::SourceRange R) { @@ -286,7 +353,6 @@ class Builder { return param_names; } -public: void add_params(Callee callee, clang::SourceLocation rpunc_location, llvm::ArrayRef args) { @@ -358,9 +424,10 @@ class Builder { clang::CharSourceRange::getTokenRange(params[i]->getDefaultArgRange()), unit.context().getSourceManager(), unit.lang_options()); - const auto abbrev = - (text.size() > options.type_name_limit || text.contains("\n")) ? "..." - : text; + // type_name_limit = 0 means unlimited, as for type hints. + const bool too_long = + options.type_name_limit && text.size() > options.type_name_limit; + const auto abbrev = (too_long || text.contains("\n")) ? "..." : text; if(name_hint) { formatted_default_args.emplace_back(std::format("{0}: {1}", name, abbrev)); } else { @@ -383,7 +450,8 @@ class Builder { llvm::ListSeparator sep(", "); for(auto&& element: formatted_default_args) { os << sep; - if(hint.size() + element.size() >= options.type_name_limit) { + if(options.type_name_limit && + hint.size() + element.size() >= options.type_name_limit) { os << "..."; break; } @@ -508,7 +576,24 @@ class Builder { /*Suffix=*/"="); } - void add_return_type_hint(clang::FunctionDecl* decl, clang::SourceRange range) { + /// Hint the unwritten designators of a syntactic init list, e.g. `.x` + /// for the `1` in `Point{1}`. Explicitly written designators and inits + /// already carrying a `/*name=*/` comment stay bare. + void add_designators(const clang::InitListExpr* syntactic) { + auto designators = clang::tidy::utils::getUnwrittenDesignators(syntactic); + for(const clang::Expr* init: syntactic->inits()) { + if(llvm::isa(init)) { + continue; + } + auto it = designators.find(init->getBeginLoc()); + if(it == designators.end() || has_param_name_comment(init, it->second)) { + continue; + } + add_designator_hint(init->getSourceRange(), it->second); + } + } + + void add_return_type_hint(const clang::FunctionDecl* decl, clang::SourceRange range) { auto* type = decl->getReturnType()->getContainedAutoType(); if(!type || type->getDeducedType().isNull()) { return; @@ -516,43 +601,19 @@ class Builder { add_type_hint(range, decl->getReturnType(), /*Prefix=*/"-> "); } -private: - std::vector& result; - CompilationUnitRef unit; - LocalSourceRange restrict_range; - const InlayHintsOptions& options; - - // The sugared type is more useful in some cases, and the canonical - // type in other cases. Suppress scopes to keep type names short; - // anonymous tag locations stay off so lambda locations don't print. - // Not setting PrintCanonicalTypes for "auto" allows - // SuppressDefaultTemplateArgs (set by default) to have an effect. - display::Options display_options = {.suppress_scope = true}; -}; - -class Visitor : public FilteredASTVisitor { -public: - using Base = FilteredASTVisitor; - - Visitor(Builder& builder, - CompilationUnitRef unit, - std::optional restrict_range, - const InlayHintsOptions& options) : - Base(unit, true), builder(builder), unit(unit), options(options) {} - -public: - // Carefully recurse into PseudoObjectExprs, which typically incorporate - // a syntactic expression and several semantic expressions. - bool TraversePseudoObjectExpr(clang::PseudoObjectExpr* expr) { - clang::Expr* syntactic_expr = expr->getSyntacticForm(); + // A PseudoObjectExpr typically incorporates a syntactic expression and + // several semantic expressions; the table records only the syntactic + // subtree. Returns false to skip that subtree. + bool handle_pseudo_object(const clang::PseudoObjectExpr* expr) { + const clang::Expr* syntactic_expr = expr->getSyntacticForm(); if(llvm::isa(syntactic_expr)) { // Since the counterpart semantics usually get the identical source // locations as the syntactic one, visiting those would end up presenting // confusing hints e.g., __builtin_dump_struct. - // Thus, only traverse the syntactic forms if this is written as a + // Thus, only visit the syntactic forms if this is written as a // CallExpr. This leaves the door open in case the arguments in the // syntactic form could possibly get parameter names. - return Base::TraverseStmt(syntactic_expr); + return true; } // We don't want the hints for some of the MS property extensions. @@ -563,27 +624,75 @@ class Visitor : public FilteredASTVisitor { // void Work(int y) { x = y; } // Bad: `x = y: y`. // }; if(llvm::isa(syntactic_expr)) { - return true; + return false; + } + + // Other forms (an MS property subscript read `s.x[1][2]`) execute an + // accessor call that exists only in the semantic expressions, so its + // parameter hints are derived here. The written arguments appear in + // both forms as the same OpaqueValueExprs, so everything below the + // call is already covered by the recorded syntactic subtree. + for(const clang::Expr* semantic_expr: expr->semantics()) { + if(auto* call = dyn_cast(semantic_expr)) { + handle_call(call); + } + } + return true; + } + + void handle_decl(const clang::Decl* decl) { + if(auto* ns = dyn_cast(decl)) { + handle_namespace(ns); + } else if(auto* tag = dyn_cast(decl)) { + handle_tag(tag); + } else if(auto* function = dyn_cast(decl)) { + handle_function(function); + } else if(auto* var = dyn_cast(decl)) { + handle_var(var); } + } - // FIXME: Handle other forms of a pseudo object expression. - return Base::TraversePseudoObjectExpr(expr); + /// Returns false to skip the node's recorded subtree. + bool handle_stmt(const clang::Stmt* stmt) { + if(auto* expr = dyn_cast(stmt)) { + return handle_pseudo_object(expr); + } + + if(auto* expr = dyn_cast(stmt)) { + handle_call(expr); + } else if(auto* expr = dyn_cast(stmt)) { + handle_construct(expr); + } else if(auto* expr = dyn_cast(stmt)) { + handle_lambda(expr); + } else if(auto* expr = dyn_cast(stmt)) { + handle_init_list(expr); + } else if(auto* for_stmt = dyn_cast(stmt)) { + handle_for(for_stmt); + } else if(auto* range_for = dyn_cast(stmt)) { + handle_range_for(range_for); + } else if(auto* while_stmt = dyn_cast(stmt)) { + handle_while(while_stmt); + } else if(auto* switch_stmt = dyn_cast(stmt)) { + handle_switch(switch_stmt); + } else if(auto* if_stmt = dyn_cast(stmt)) { + handle_if(if_stmt); + } + return true; } - bool VisitNamespaceDecl(clang::NamespaceDecl* decl) { + void handle_namespace(const clang::NamespaceDecl* decl) { if(options.block_end) { // For namespace, the range actually starts at the namespace keyword. But // it should be fine since it's usually very short. // Anonymous namespaces hint as plain "// namespace". - builder.add_block_end_hint(decl->getSourceRange(), - "namespace", - display::identifier_of(decl), - ""); + add_block_end_hint(decl->getSourceRange(), + "namespace", + display::identifier_of(decl), + ""); } - return true; } - bool VisitTagDecl(clang::TagDecl* decl) { + void handle_tag(const clang::TagDecl* decl) { if(options.block_end && decl->isThisDeclarationADefinition()) { std::string prefix = decl->getKindName().str(); @@ -594,19 +703,15 @@ class Visitor : public FilteredASTVisitor { }; // Anonymous tags hint with the bare kind, e.g. "// struct". - builder.add_block_end_hint(decl->getBraceRange(), - prefix, - display::identifier_of(decl), - ";"); + add_block_end_hint(decl->getBraceRange(), prefix, display::identifier_of(decl), ";"); } - return true; } - bool VisitFunctionDecl(clang::FunctionDecl* decl) { + void handle_function(const clang::FunctionDecl* decl) { if(auto* proto_type = llvm::dyn_cast(decl->getType())) { if(!proto_type->hasTrailingReturn()) { if(auto FTL = decl->getFunctionTypeLoc()) { - builder.add_return_type_hint(decl, FTL.getRParenLoc()); + add_return_type_hint(decl, FTL.getRParenLoc()); } } } @@ -615,14 +720,12 @@ class Visitor : public FilteredASTVisitor { // We use `printName` here to properly print name of ctor/dtor/operator // overload. if(const clang::Stmt* body = decl->getBody()) { - builder.add_block_end_hint(body->getSourceRange(), "", display::name_of(decl), ""); + add_block_end_hint(body->getSourceRange(), "", display::name_of(decl), ""); } } - - return true; } - bool VisitVarDecl(clang::VarDecl* var) { + void handle_var(const clang::VarDecl* var) { // Do not show hints for the aggregate in a structured binding, // but show hints for the individual bindings. if(auto* decl = dyn_cast(var)) { @@ -631,12 +734,12 @@ class Visitor : public FilteredASTVisitor { // because for bindings that use the tuple_element protocol, the // non-canonical types would be "tuple_element::type". if(auto type = binding->getType(); !type.isNull() && !type->isDependentType()) { - builder.add_type_hint(binding->getLocation(), - type.getCanonicalType(), - /*Prefix=*/": "); + add_type_hint(binding->getLocation(), + type.getCanonicalType(), + /*Prefix=*/": "); } } - return true; + return; } auto type = var->getType(); @@ -647,7 +750,7 @@ class Visitor : public FilteredASTVisitor { // (e.g. for `const auto& x = 42`, print `const int&`). // Alternatively, we could place the hint on the `auto` // (and then just print the type deduced for the `auto`). - builder.add_type_hint(var->getLocation(), var->getType(), /*Prefix=*/": "); + add_type_hint(var->getLocation(), var->getType(), /*Prefix=*/": "); } } @@ -657,46 +760,46 @@ class Visitor : public FilteredASTVisitor { auto unwrapped = types::unwrap(var->getTypeSourceInfo()->getTypeLoc()); if(auto type = unwrapped.getAs()) { if(auto decl = type.getDecl(); decl && decl->isImplicit()) { - if(auto* IPVD = decls::only_instantiation(param)) { - builder.add_type_hint(var->getLocation(), - IPVD->getType(), - /*Prefix=*/": "); + // The helper keeps its clangd signature, which speaks + // mutable pointers; it only reads through them. + if(auto* IPVD = + decls::only_instantiation(const_cast(param))) { + add_type_hint(var->getLocation(), + IPVD->getType(), + /*Prefix=*/": "); } } } } } - - return true; } - bool VisitCXXConstructExpr(clang::CXXConstructExpr* expr) { + void handle_construct(const clang::CXXConstructExpr* expr) { // Weed out constructor calls that don't look like a function call with // an argument list, by checking the validity of getParenOrBraceRange(). // Also weed out std::initializer_list constructors as there are no names // for the individual arguments. if(!expr->getParenOrBraceRange().isValid() || expr->isStdInitListInitialization()) { - return true; + return; } Callee callee; callee.decl = expr->getConstructor(); if(!callee.decl) { - return true; + return; } - builder.add_params(callee, - expr->getParenOrBraceRange().getEnd(), - {expr->getArgs(), expr->getNumArgs()}); - return true; + add_params(callee, + expr->getParenOrBraceRange().getEnd(), + {expr->getArgs(), expr->getNumArgs()}); } - bool VisitCallExpr(clang::CallExpr* expr) { - if(!options.parameters) { - return true; + void handle_call(const clang::CallExpr* expr) { + if(!options.parameters && !options.default_arguments) { + return; } - auto isFunctionObjectCallExpr = [](clang::CallExpr* E) { + auto isFunctionObjectCallExpr = [](const clang::CallExpr* E) { if(auto* call_expr = dyn_cast(E)) { return call_expr->getOperator() == clang::OverloadedOperatorKind::OO_Call; } @@ -712,24 +815,45 @@ class Visitor : public FilteredASTVisitor { // argument and then we'd get two hints side by side). if((llvm::isa(expr) && !is_functor) || llvm::isa(expr)) - return true; + return; - /// FIXME: Use template resolver here. + Callee callee; + bool dependent_callee = false; if(expr->isTypeDependent() || expr->isValueDependent()) { - return true; - } - - auto callee_decl = expr->getCalleeDecl(); + dependent_callee = true; + // A dependent call has no resolved callee. The template + // resolver's arity-filtered candidate set stands in; only a + // unique candidate gives trustworthy parameter names — with + // several overloads left we must not pick one arbitrarily. + auto candidates = unit.resolver().lookup(expr); + if(candidates.size() != 1) { + return; + } - Callee callee; - if(const auto* FD = llvm::dyn_cast(callee_decl)) { + auto* target = candidates.front(); + if(auto* shadow = dyn_cast(target)) { + target = shadow->getTargetDecl(); + } + if(auto* FTD = dyn_cast(target)) { + callee.decl = FTD->getTemplatedDecl(); + } else { + callee.decl = dyn_cast(target); + } + if(!callee.decl) { + return; + } + } else if(const auto* FD = dyn_cast_or_null(expr->getCalleeDecl())) { callee.decl = FD; - } else if(const auto* FTD = llvm::dyn_cast(callee_decl)) { + } else if(const auto* FTD = + dyn_cast_or_null(expr->getCalleeDecl())) { callee.decl = FTD->getTemplatedDecl(); - } else if(clang::FunctionProtoTypeLoc loc = decls::proto_type_loc(expr->getCallee())) { + } else if(clang::FunctionProtoTypeLoc loc = + // The helper keeps its clangd signature, which speaks + // mutable pointers; it only reads through them. + decls::proto_type_loc(const_cast(expr->getCallee()))) { callee.loc = loc; } else { - return true; + return; } // N4868 [over.call.object]p3 says, @@ -744,18 +868,20 @@ class Visitor : public FilteredASTVisitor { // this correspondence... llvm::ArrayRef args = {expr->getArgs(), expr->getNumArgs()}; - // We don't have the implied object argument through a function pointer either. + // We don't have the implied object argument through a function pointer + // either, nor in a dependent member call: there the object stays the + // member expression's base and never joins the argument list. if(const auto* method = llvm::dyn_cast_or_null(callee.decl)) { - if(is_functor || method->hasCXXExplicitFunctionObjectParameter()) { + if(is_functor || + (!dependent_callee && method->hasCXXExplicitFunctionObjectParameter())) { args = args.drop_front(1); } } - builder.add_params(callee, expr->getRParenLoc(), args); - return true; + add_params(callee, expr->getRParenLoc(), args); } - bool VisitForStmt(clang::ForStmt* S) { + void handle_for(const clang::ForStmt* S) { if(options.block_end) { std::string name; // Common case: for (int I = 0; I < N; I++). Use "I" as the name. @@ -765,37 +891,32 @@ class Visitor : public FilteredASTVisitor { } else if(const auto* cond = S->getCond()) { name = display::summarize(cond); } - builder.mark_block_end(S->getBody(), "for", name); + mark_block_end(S->getBody(), "for", name); } - return true; } - bool VisitCXXForRangeStmt(clang::CXXForRangeStmt* S) { + void handle_range_for(const clang::CXXForRangeStmt* S) { if(options.block_end) { // Decomposition loop variables have no identifier: plain "// for". - builder.mark_block_end(S->getBody(), - "for", - display::identifier_of(S->getLoopVariable())); + mark_block_end(S->getBody(), "for", display::identifier_of(S->getLoopVariable())); } - return true; } - bool VisitWhileStmt(clang::WhileStmt* S) { + void handle_while(const clang::WhileStmt* S) { if(options.block_end) { - builder.mark_block_end(S->getBody(), "while", display::summarize(S->getCond())); + mark_block_end(S->getBody(), "while", display::summarize(S->getCond())); } - return true; } - bool VisitSwitchStmt(clang::SwitchStmt* S) { + void handle_switch(const clang::SwitchStmt* S) { if(options.block_end) { - builder.mark_block_end(S->getBody(), "switch", display::summarize(S->getCond())); + mark_block_end(S->getBody(), "switch", display::summarize(S->getCond())); } - return true; } - bool VisitIfStmt(clang::IfStmt* S) { + void handle_if(const clang::IfStmt* S) { if(options.block_end) { + // Pre-order guarantees the outer if is handled before its else-if. if(const auto* else_if = llvm::dyn_cast_or_null(S->getElse())) { else_ifs.insert(else_if); } @@ -808,17 +929,16 @@ class Visitor : public FilteredASTVisitor { if(const auto* cond = S->getCond(); cond && !else_ifs.contains(S)) { name = display::summarize(cond); } - builder.add_block_end_hint({S->getThen()->getBeginLoc(), if_end->getRBracLoc()}, - "if", - name, - ""); + add_block_end_hint({S->getThen()->getBeginLoc(), if_end->getRBracLoc()}, + "if", + name, + ""); } } - return true; } - bool VisitLambdaExpr(clang::LambdaExpr* expr) { - clang::FunctionDecl* decl = expr->getCallOperator(); + void handle_lambda(const clang::LambdaExpr* expr) { + const clang::FunctionDecl* decl = expr->getCallOperator(); if(!expr->hasExplicitResultType()) { clang::SourceLocation type_hint_loc; if(!expr->hasExplicitParameters()) { @@ -828,55 +948,54 @@ class Visitor : public FilteredASTVisitor { } if(type_hint_loc.isValid()) { - builder.add_return_type_hint(decl, type_hint_loc); + add_return_type_hint(decl, type_hint_loc); } } - return true; } - bool VisitInitListExpr(clang::InitListExpr* expr) { - // We receive the syntactic form here (shouldVisitImplicitCode() is false). - // This is the one we will ultimately attach designators to. - // It may have subobject initializers inlined without braces. The *semantic* - // form of the init-list has nested init-lists for these. - // getUnwrittenDesignators will look at the semantic form to determine the - // labels. - assert(expr->isSyntacticForm() && "RAV should not visit implicit code!"); + void handle_init_list(const clang::InitListExpr* expr) { + // The table records the form the enclosing AST node stores, which for + // the outermost list is the *semantic* form. Designators attach to the + // syntactic form — the written one, which may have subobject + // initializers inlined without braces where the semantic form has + // nested init-lists. getUnwrittenDesignators will look at the semantic + // form to determine the labels. + if(const auto* syntactic = expr->getSyntacticForm()) { + expr = syntactic; + } + if(!options.designators) { - return true; + return; } if(expr->isIdiomaticZeroInitializer(unit.lang_options())) { - return true; + return; } - /// FIXME: - // llvm::DenseMap Designators = - // tidy::utils::getUnwrittenDesignators(Syn); - // for(const Expr* Init: Syn->inits()) { - // if(llvm::isa(Init)) - // continue; - // auto It = Designators.find(Init->getBeginLoc()); - // if(It != Designators.end() && !isPrecededByParamNameComment(Init, It->second)) - // addDesignatorHint(Init->getSourceRange(), It->second); - // } - return true; + add_designators(expr); } - bool VisitTypeLoc(clang::TypeLoc TL) { + void handle_type_loc(clang::TypeLoc TL) { if(const auto* DT = llvm::dyn_cast(TL.getType())) if(clang::QualType UT = DT->getUnderlyingType(); !UT->isDependentType()) - builder.add_type_hint(TL.getSourceRange(), UT, ": "); - return true; + add_type_hint(TL.getSourceRange(), UT, ": "); } // FIXME: Handle RecoveryExpr to try to hint some invalid calls. private: - Builder& builder; + std::vector& result; CompilationUnitRef unit; + LocalSourceRange restrict_range; const InlayHintsOptions& options; + // The sugared type is more useful in some cases, and the canonical + // type in other cases. Suppress scopes to keep type names short; + // anonymous tag locations stay off so lambda locations don't print. + // Not setting PrintCanonicalTypes for "auto" allows + // SuppressDefaultTemplateArgs (set by default) to have an effect. + display::Options display_options = {.suppress_scope = true}; + // If/else chains are tricky. // if (cond1) { // } else if (cond2) { @@ -896,9 +1015,8 @@ auto inlay_hints(CompilationUnitRef unit, LocalSourceRange target, const InlayHi std::vector raw_hints; - Builder builder(raw_hints, unit, target, options); - Visitor visitor(builder, unit, target, options); - visitor.TraverseDecl(unit.tu()); + Collector collector(raw_hints, unit, target, options); + collector.run(); std::ranges::sort(raw_hints, [](const InlayHint& lhs, const InlayHint& rhs) { return std::tie(lhs.offset, lhs.label, lhs.kind, lhs.padding_left, lhs.padding_right) < diff --git a/src/server/compiler/compiler.cpp b/src/server/compiler/compiler.cpp index 9c21025143..c925a37b6d 100644 --- a/src/server/compiler/compiler.cpp +++ b/src/server/compiler/compiler.cpp @@ -1392,6 +1392,20 @@ Compiler::RawResult Compiler::forward_query(worker::QueryKind kind, wp.kind = kind; wp.path = path; + if(kind == worker::QueryKind::InlayHints) { + // apply_defaults() has filled every field of the config section. + auto& ih = workspace.config.inlay_hints; + wp.inlay_options = { + .enabled = *ih.enabled, + .parameters = *ih.parameters, + .deduced_types = *ih.deduced_types, + .designators = *ih.designators, + .block_end = *ih.block_end, + .default_arguments = *ih.default_arguments, + .type_name_limit = *ih.type_name_limit, + }; + } + if(position) { wp.offset = clamped_offset(map, *position); } diff --git a/src/server/protocol/worker.h b/src/server/protocol/worker.h index 9d25c4029f..6b9a7743f6 100644 --- a/src/server/protocol/worker.h +++ b/src/server/protocol/worker.h @@ -9,6 +9,7 @@ #include "compile/dep_file.h" #include "feature/document_link.h" +#include "feature/feature.h" #include "syntax/token.h" #include "kota/codec/json/json.h" @@ -100,6 +101,11 @@ struct QueryParams { std::string path; uint32_t offset = 0; ///< Byte offset for position-sensitive queries (Hover, GoToDefinition). LocalSourceRange range; ///< Byte range for range-sensitive queries (InlayHints). + + /// Options for InlayHints queries, resolved from the workspace config by + /// the master per query — the worker holds no config state and a config + /// change simply shows up on the next request. + feature::InlayHintsOptions inlay_options; }; /// Parameters for stateful compilation (builds AST, publishes diagnostics). diff --git a/src/server/state/config.cpp b/src/server/state/config.cpp index 826871d643..9d7d031306 100644 --- a/src/server/state/config.cpp +++ b/src/server/state/config.cpp @@ -2,6 +2,7 @@ #include +#include "feature/feature.h" #include "support/filesystem.h" #include "support/glob_pattern.h" #include "support/logging.h" @@ -96,6 +97,24 @@ void Config::apply_defaults(llvm::StringRef workspace_root) { if(!t.workspace_poll_seconds) t.workspace_poll_seconds = 30; + // The feature options struct is the single source of the defaults. + auto& ih = inlay_hints; + feature::InlayHintsOptions inlay_defaults; + if(!ih.enabled) + ih.enabled = inlay_defaults.enabled; + if(!ih.parameters) + ih.parameters = inlay_defaults.parameters; + if(!ih.deduced_types) + ih.deduced_types = inlay_defaults.deduced_types; + if(!ih.designators) + ih.designators = inlay_defaults.designators; + if(!ih.block_end) + ih.block_end = inlay_defaults.block_end; + if(!ih.default_arguments) + ih.default_arguments = inlay_defaults.default_arguments; + if(!ih.type_name_limit) + ih.type_name_limit = inlay_defaults.type_name_limit; + if(p.cache_dir.empty() && !workspace_root.empty()) { p.cache_dir = resolve_xdg_cache_dir(workspace_root); if(p.cache_dir.empty()) diff --git a/src/server/state/config.h b/src/server/state/config.h index d86e66ea81..f2478e8888 100644 --- a/src/server/state/config.h +++ b/src/server/state/config.h @@ -46,6 +46,19 @@ struct ProjectConfig { defaulted worker_memory_limit = {}; }; +/// Corresponds to the `[inlay_hints]` section in clice.toml. Mirrors +/// feature::InlayHintsOptions field by field; unset fields take that +/// struct's defaults in apply_defaults(), so the two never disagree. +struct InlayHintsConfig { + std::optional enabled; + std::optional parameters; + std::optional deduced_types; + std::optional designators; + std::optional block_end; + std::optional default_arguments; + std::optional type_name_limit; +}; + /// Corresponds to the `[tracker]` section in clice.toml: the stat-polling /// file tracker's intervals. 0 disables the loop (integration tests drive /// ticks through the clice/internal/poll hook instead). @@ -88,6 +101,8 @@ struct Config { defaulted tracker; + defaulted inlay_hints; + defaulted> rules; kota::meta::annotation, kota::meta::attrs::skip> compiled_rules; diff --git a/src/server/worker/stateful_worker.cpp b/src/server/worker/stateful_worker.cpp index 874f420611..ccfc240a67 100644 --- a/src/server/worker/stateful_worker.cpp +++ b/src/server/worker/stateful_worker.cpp @@ -388,7 +388,7 @@ void StatefulWorker::register_handlers() { range = LocalSourceRange{0, static_cast(doc.text.size())}; return to_raw(feature::inlay_hints(doc.unit, range, - {}, + params.inlay_options, feature::PositionEncoding::UTF16)); }); case K::FoldingRange: diff --git a/tests/integration/lifecycle/config.test.ts b/tests/integration/lifecycle/config.test.ts index 13560d3aab..f9e55d6656 100644 --- a/tests/integration/lifecycle/config.test.ts +++ b/tests/integration/lifecycle/config.test.ts @@ -144,3 +144,46 @@ test("config dump logged", async ({ session }) => { expect(text).toContain("Effective configuration:"); expect(text).toContain('"cache_dir"'); }); + +test("inlay hint options from config", async ({ session }) => { + // block_end hints are off by default and reachable only through the + // [inlay_hints] config section, so their appearance proves the options + // travel master → worker instead of the old hardcoded defaults. + const source = [ + "int compute() {", + " int total = 0;", + " for (int i = 0; i < 10; i += 1) {", + " total += i;", + " }", + " return total;", + "}", + "", + ].join("\n"); + const wholeFile = { + start: { line: 0, character: 0 }, + end: { line: 7, character: 0 }, + }; + + const workspace = session.tmpdir(); + workspace.write("main.cpp", source); + workspace.writeCDB(["main.cpp"]); + const client = session.spawn(workspace, {}); + await client.initialize(workspace, { + initializationOptions: { inlay_hints: { block_end: true, deduced_types: false } }, + }); + const [uri] = await client.openAndWait("main.cpp"); + const hints = (await client.inlayHints(uri, wholeFile)) ?? []; + const labels = hints.map((h) => (typeof h.label === "string" ? h.label : "")); + expect(labels, `expected a block-end hint, got: ${JSON.stringify(labels)}`).toContain( + "// compute", + ); + await client.shutdown(); + + // Control: a default-config session must not produce block-end hints. + const control = session.spawn(workspace, {}); + await control.initialize(workspace); + const [controlUri] = await control.openAndWait("main.cpp"); + const controlHints = (await control.inlayHints(controlUri, wholeFile)) ?? []; + const controlLabels = controlHints.map((h) => (typeof h.label === "string" ? h.label : "")); + expect(controlLabels).not.toContain("// compute"); +}); diff --git a/tests/snap/inlay_hint/basic.cpp b/tests/snap/inlay_hint/basic.cpp deleted file mode 100644 index ce9f3f5ffa..0000000000 --- a/tests/snap/inlay_hint/basic.cpp +++ /dev/null @@ -1,14 +0,0 @@ -// Seed case for the per-feature snapshot corpus: parameter and type hints. -namespace demo { - -int clamp(int value, int low, int high) { - return value < low ? low : (value > high ? high : value); -} - -void test() { - auto result = clamp(42, 0, 10); - auto& ref = result; - [[maybe_unused]] auto copy = ref; -} - -} // namespace demo diff --git a/tests/snap/inlay_hint/basic.snap.yml b/tests/snap/inlay_hint/basic.snap.yml deleted file mode 100644 index af53f045a5..0000000000 --- a/tests/snap/inlay_hint/basic.snap.yml +++ /dev/null @@ -1,10 +0,0 @@ ---- -created_at: 2026-07-28 -input_file: basic.cpp ---- -- { pos: "8:15", kind: Type, label: ": int" } -- { pos: "8:24", kind: Parameter, label: "value:", padding_right: true } -- { pos: "8:28", kind: Parameter, label: "low:", padding_right: true } -- { pos: "8:31", kind: Parameter, label: "high:", padding_right: true } -- { pos: "9:13", kind: Type, label: ": int &" } -- { pos: "10:30", kind: Type, label: ": int" } diff --git a/tests/snap/inlay_hint/conversion_hints.cpp b/tests/snap/inlay_hint/conversion_hints.cpp new file mode 100644 index 0000000000..6b9b110e80 --- /dev/null +++ b/tests/snap/inlay_hint/conversion_hints.cpp @@ -0,0 +1,14 @@ +/// # Other Hint Kinds +/// +/// ## Implicit conversion hints — surface the conversions a call site performs +/// +/// - status: unsupported +/// - issues: clangd#2254 +/// - order: 3 + +void process(double val); + +// Could hint `(double)` before the argument. +void use() { + process(42); +} diff --git a/tests/snap/inlay_hint/ctad_arguments.cpp b/tests/snap/inlay_hint/ctad_arguments.cpp new file mode 100644 index 0000000000..bf4b875d08 --- /dev/null +++ b/tests/snap/inlay_hint/ctad_arguments.cpp @@ -0,0 +1,17 @@ +/// # Other Hint Kinds +/// +/// ## CTAD arguments — deduced class template arguments after the template name +/// +/// - status: unsupported +/// - issues: clangd#2331 +/// - order: 2 + +template +struct Pair { + A first; + B second; + Pair(A a, B b); +}; + +// Could hint `` after `pair`. +Pair pair(1, 2.5); diff --git a/tests/snap/inlay_hint/designator_aggregates_only.cpp b/tests/snap/inlay_hint/designator_aggregates_only.cpp new file mode 100644 index 0000000000..22e6ee4423 --- /dev/null +++ b/tests/snap/inlay_hint/designator_aggregates_only.cpp @@ -0,0 +1,27 @@ +/// # Designator Hints +/// +/// ## Aggregates only — constructor calls, copies and idiomatic zero-init produce no designators +/// +/// - status: supported +/// - order: 5 + +struct Constructible { + Constructible(int amount); +}; + +// A braced constructor call names parameters, not fields. +Constructible built{5}; + +struct Copyable { + int x; +}; + +Copyable original{1}; +Copyable duplicate{original}; + +// The idiomatic `{}` zero-initializer stays quiet. +struct Wide { + int fields[8]; +}; + +Wide zeroed{}; diff --git a/tests/snap/inlay_hint/designator_aggregates_only.snap.yml b/tests/snap/inlay_hint/designator_aggregates_only.snap.yml new file mode 100644 index 0000000000..7c216fd629 --- /dev/null +++ b/tests/snap/inlay_hint/designator_aggregates_only.snap.yml @@ -0,0 +1,6 @@ +--- +created_at: 2026-07-31 +input_file: designator_aggregates_only.cpp +--- +- { pos: "12:20", kind: Parameter, label: "amount:", padding_right: true } +- { pos: "18:18", kind: Type, label: ".x=" } diff --git a/tests/snap/inlay_hint/designator_anonymous.cpp b/tests/snap/inlay_hint/designator_anonymous.cpp new file mode 100644 index 0000000000..f508a9fb0b --- /dev/null +++ b/tests/snap/inlay_hint/designator_anonymous.cpp @@ -0,0 +1,18 @@ +/// # Designator Hints +/// +/// ## Anonymous members — unnamed unions and structs vanish from the designator path +/// +/// - status: supported +/// - order: 3 + +struct State { + union { + struct { + struct { + int y; + }; + } x; + }; +}; + +State s{42}; diff --git a/tests/snap/inlay_hint/designator_anonymous.snap.yml b/tests/snap/inlay_hint/designator_anonymous.snap.yml new file mode 100644 index 0000000000..bc1c40f774 --- /dev/null +++ b/tests/snap/inlay_hint/designator_anonymous.snap.yml @@ -0,0 +1,5 @@ +--- +created_at: 2026-07-31 +input_file: designator_anonymous.cpp +--- +- { pos: "17:8", kind: Type, label: ".x.y=" } diff --git a/tests/snap/inlay_hint/designator_basic.cpp b/tests/snap/inlay_hint/designator_basic.cpp new file mode 100644 index 0000000000..0c865fa222 --- /dev/null +++ b/tests/snap/inlay_hint/designator_basic.cpp @@ -0,0 +1,26 @@ +/// # Designator Hints +/// +/// ## Field and index designators — positional aggregate initialization shows `.field=` and `[index]=` +/// +/// - status: supported +/// - issues: clangd#2303 +/// - order: 1 + +struct Point { + int x; + int y; + int z; +}; + +Point p{1, 2 + 2}; + +int coordinates[2] = {7, 8}; + +// Array designators survive dependent-sized members; reserved names are +// skipped rather than printed. +template +struct Array { + T __elements[N]; +}; + +Array pair = {0, 1}; diff --git a/tests/snap/inlay_hint/designator_basic.snap.yml b/tests/snap/inlay_hint/designator_basic.snap.yml new file mode 100644 index 0000000000..147a67e0e8 --- /dev/null +++ b/tests/snap/inlay_hint/designator_basic.snap.yml @@ -0,0 +1,10 @@ +--- +created_at: 2026-07-31 +input_file: designator_basic.cpp +--- +- { pos: "14:8", kind: Type, label: ".x=" } +- { pos: "14:11", kind: Type, label: ".y=" } +- { pos: "16:22", kind: Type, label: "[0]=" } +- { pos: "16:25", kind: Type, label: "[1]=" } +- { pos: "25:22", kind: Type, label: "[0]=" } +- { pos: "25:25", kind: Type, label: "[1]=" } diff --git a/tests/snap/inlay_hint/designator_nested.cpp b/tests/snap/inlay_hint/designator_nested.cpp new file mode 100644 index 0000000000..a667f183e1 --- /dev/null +++ b/tests/snap/inlay_hint/designator_nested.cpp @@ -0,0 +1,18 @@ +/// # Designator Hints +/// +/// ## Nested aggregates — written braces recurse; omitted braces flatten into `.outer.inner=` +/// +/// - status: supported +/// - order: 2 + +struct Inner { + int x; + int y; +}; + +struct Outer { + Inner a; + Inner b; +}; + +Outer o{{1, 2}, 3}; diff --git a/tests/snap/inlay_hint/designator_nested.snap.yml b/tests/snap/inlay_hint/designator_nested.snap.yml new file mode 100644 index 0000000000..efc6a4e88d --- /dev/null +++ b/tests/snap/inlay_hint/designator_nested.snap.yml @@ -0,0 +1,8 @@ +--- +created_at: 2026-07-31 +input_file: designator_nested.cpp +--- +- { pos: "17:8", kind: Type, label: ".a=" } +- { pos: "17:9", kind: Type, label: ".x=" } +- { pos: "17:12", kind: Type, label: ".y=" } +- { pos: "17:16", kind: Type, label: ".b.x=" } diff --git a/tests/snap/inlay_hint/designator_parenthesized.cpp b/tests/snap/inlay_hint/designator_parenthesized.cpp new file mode 100644 index 0000000000..4efb36887f --- /dev/null +++ b/tests/snap/inlay_hint/designator_parenthesized.cpp @@ -0,0 +1,14 @@ +/// # Designator Hints +/// +/// ## Parenthesized aggregate initialization — C++20 `Point(1, 2)` gets no hints yet +/// +/// - status: unsupported +/// - issues: clangd#2540 +/// - order: 7 + +struct Point { + int x; + int y; +}; + +Point p(1, 2); diff --git a/tests/snap/inlay_hint/designator_recovery.cpp b/tests/snap/inlay_hint/designator_recovery.cpp new file mode 100644 index 0000000000..17bad902ea --- /dev/null +++ b/tests/snap/inlay_hint/designator_recovery.cpp @@ -0,0 +1,18 @@ +/// # Designator Hints +/// +/// ## Broken initializers — designators survive next to initializers that fail to compile +/// +/// - status: supported +/// - order: 6 + +// error-ok: the first initializer deliberately fails to convert. +struct Empty {}; + +struct Mixed { + int a; + int b; +}; + +void use() { + Mixed m{Empty(), 1}; +} diff --git a/tests/snap/inlay_hint/designator_recovery.snap.yml b/tests/snap/inlay_hint/designator_recovery.snap.yml new file mode 100644 index 0000000000..8b4a56ca68 --- /dev/null +++ b/tests/snap/inlay_hint/designator_recovery.snap.yml @@ -0,0 +1,5 @@ +--- +created_at: 2026-07-31 +input_file: designator_recovery.cpp +--- +- { pos: "16:21", kind: Type, label: ".b=" } diff --git a/tests/snap/inlay_hint/designator_suppression.cpp b/tests/snap/inlay_hint/designator_suppression.cpp new file mode 100644 index 0000000000..e1c9011e4b --- /dev/null +++ b/tests/snap/inlay_hint/designator_suppression.cpp @@ -0,0 +1,18 @@ +/// # Designator Hints +/// +/// ## Designator suppression — written designators and `/*name=*/` comments keep their inits bare +/// +/// - status: supported +/// - order: 4 + +struct Point { + int a; + int b; + int c; + int d; + int e; +}; + +// Mixing written designators with positional inits is a C99 extension +// clang accepts with a warning; only the bare `4` needs help. +Point p{/*a=*/1, .c = 2, /* .d = */ 3, 4}; diff --git a/tests/snap/inlay_hint/designator_suppression.snap.yml b/tests/snap/inlay_hint/designator_suppression.snap.yml new file mode 100644 index 0000000000..10e1e41406 --- /dev/null +++ b/tests/snap/inlay_hint/designator_suppression.snap.yml @@ -0,0 +1,5 @@ +--- +created_at: 2026-07-31 +input_file: designator_suppression.cpp +--- +- { pos: "17:39", kind: Type, label: ".e=" } diff --git a/tests/snap/inlay_hint/designators.cpp b/tests/snap/inlay_hint/designators.cpp deleted file mode 100644 index 51bdcef153..0000000000 --- a/tests/snap/inlay_hint/designators.cpp +++ /dev/null @@ -1,39 +0,0 @@ -// Designator hints are currently unimplemented (VisitInitListExpr is a -// FIXME stub), so aggregate initializations produce no hints; only the -// constructor call at the end yields a parameter hint. This fixture pins -// the absence: implementing designators will surface here as a diff. - -namespace designators { - -struct Point { - int x; - int y; -}; - -Point origin{1, 2}; - -struct Box { - Point top_left; - Point bottom_right; -}; - -Box box{{1, 2}, {3, 4}}; - -int coordinates[2] = {7, 8}; - -struct Mixed { - struct { - int inner; - }; - int outer; -}; - -Mixed mixed{{5}, 6}; - -struct NotAggregate { - NotAggregate(int amount); -}; - -NotAggregate built{5}; - -} // namespace designators diff --git a/tests/snap/inlay_hint/designators.snap.yml b/tests/snap/inlay_hint/designators.snap.yml deleted file mode 100644 index 2ededb0afb..0000000000 --- a/tests/snap/inlay_hint/designators.snap.yml +++ /dev/null @@ -1,5 +0,0 @@ ---- -created_at: 2026-07-30 -input_file: designators.cpp ---- -- { pos: "36:19", kind: Parameter, label: "amount:", padding_right: true } diff --git a/tests/snap/inlay_hint/param_anonymous.cpp b/tests/snap/inlay_hint/param_anonymous.cpp new file mode 100644 index 0000000000..1d5f7f104e --- /dev/null +++ b/tests/snap/inlay_hint/param_anonymous.cpp @@ -0,0 +1,20 @@ +/// # Parameter Hints +/// +/// ## Anonymous parameters — nothing to name, though a mutable reference still flags `&` +/// +/// - status: supported +/// - order: 17 + +void value_sink(int); +void ref_sink(int&); +void const_ref_sink(const int&); +void rvalue_sink(int&&); + +void use() { + int v = 0; + value_sink(1); + // Only the `&` marker survives without a name. + ref_sink(v); + const_ref_sink(v); + rvalue_sink(2); +} diff --git a/tests/snap/inlay_hint/param_anonymous.snap.yml b/tests/snap/inlay_hint/param_anonymous.snap.yml new file mode 100644 index 0000000000..8e399dfd13 --- /dev/null +++ b/tests/snap/inlay_hint/param_anonymous.snap.yml @@ -0,0 +1,5 @@ +--- +created_at: 2026-07-31 +input_file: param_anonymous.cpp +--- +- { pos: "16:13", kind: Parameter, label: "&:", padding_right: true } diff --git a/tests/snap/inlay_hint/param_case_insensitive.cpp b/tests/snap/inlay_hint/param_case_insensitive.cpp new file mode 100644 index 0000000000..efc49a6a1b --- /dev/null +++ b/tests/snap/inlay_hint/param_case_insensitive.cpp @@ -0,0 +1,15 @@ +/// # Parameter Hints +/// +/// ## Sloppy name matching — `aParam` does not yet suppress an argument spelled `param` +/// +/// - status: partial +/// - issues: clangd#2248 +/// - order: 15 + +void draw(int aParam); + +void use() { + int param = 3; + // Ideally the near-match would suppress the hint; today it still shows. + draw(param); +} diff --git a/tests/snap/inlay_hint/param_case_insensitive.snap.yml b/tests/snap/inlay_hint/param_case_insensitive.snap.yml new file mode 100644 index 0000000000..565354bd6c --- /dev/null +++ b/tests/snap/inlay_hint/param_case_insensitive.snap.yml @@ -0,0 +1,5 @@ +--- +created_at: 2026-07-31 +input_file: param_case_insensitive.cpp +--- +- { pos: "13:9", kind: Parameter, label: "aParam:", padding_right: true } diff --git a/tests/snap/inlay_hint/param_deducing_this.cpp b/tests/snap/inlay_hint/param_deducing_this.cpp new file mode 100644 index 0000000000..8d3711d838 --- /dev/null +++ b/tests/snap/inlay_hint/param_deducing_this.cpp @@ -0,0 +1,16 @@ +/// # Parameter Hints +/// +/// ## Deducing `this` — the explicit object parameter never hints (C++23) +/// +/// - status: supported +/// - issues: clangd#1777 +/// - order: 8 + +struct Widget { + void resize(this Widget& self, int width, int height); +}; + +void use() { + Widget w; + w.resize(800, 600); +} diff --git a/tests/snap/inlay_hint/param_deducing_this.snap.yml b/tests/snap/inlay_hint/param_deducing_this.snap.yml new file mode 100644 index 0000000000..b8423d39b5 --- /dev/null +++ b/tests/snap/inlay_hint/param_deducing_this.snap.yml @@ -0,0 +1,6 @@ +--- +created_at: 2026-07-31 +input_file: param_deducing_this.cpp +--- +- { pos: "14:13", kind: Parameter, label: "width:", padding_right: true } +- { pos: "14:18", kind: Parameter, label: "height:", padding_right: true } diff --git a/tests/snap/inlay_hint/param_definition_names.cpp b/tests/snap/inlay_hint/param_definition_names.cpp new file mode 100644 index 0000000000..64dfbb61bf --- /dev/null +++ b/tests/snap/inlay_hint/param_definition_names.cpp @@ -0,0 +1,25 @@ +/// # Parameter Hints +/// +/// ## Names from definitions — unnamed declaration parameters take the definition's names; leading underscores strip +/// +/// - status: supported +/// - order: 6 + +void resize(int, int); + +void fill(int _value, int __count); + +int scale(int good); + +void use() { + resize(800, 600); + fill(1, 2); + // When both name their parameter, the declaration wins. + scale(7); +} + +void resize(int width, int height) {} + +int scale(int bad) { + return bad; +} diff --git a/tests/snap/inlay_hint/param_definition_names.snap.yml b/tests/snap/inlay_hint/param_definition_names.snap.yml new file mode 100644 index 0000000000..d6ce0c82ff --- /dev/null +++ b/tests/snap/inlay_hint/param_definition_names.snap.yml @@ -0,0 +1,9 @@ +--- +created_at: 2026-07-31 +input_file: param_definition_names.cpp +--- +- { pos: "14:11", kind: Parameter, label: "width:", padding_right: true } +- { pos: "14:16", kind: Parameter, label: "height:", padding_right: true } +- { pos: "15:9", kind: Parameter, label: "value:", padding_right: true } +- { pos: "15:12", kind: Parameter, label: "count:", padding_right: true } +- { pos: "17:10", kind: Parameter, label: "good:", padding_right: true } diff --git a/tests/snap/inlay_hint/param_dependent.cpp b/tests/snap/inlay_hint/param_dependent.cpp new file mode 100644 index 0000000000..f8265d5b05 --- /dev/null +++ b/tests/snap/inlay_hint/param_dependent.cpp @@ -0,0 +1,33 @@ +/// # Parameter Hints +/// +/// ## Dependent calls — parameter names appear even when the callee is only known inside a template +/// +/// - status: supported +/// - order: 9 +/// +/// Candidates are matched by argument count; only a unique surviving +/// candidate names the parameters, so a call that could still hit several +/// overloads stays bare rather than guessing. + +template +void apply(T scale); + +template +struct Holder { + void member(T item); + static void static_member(T slot); +}; + +void overload(int value); +void overload(double value); + +template +struct Runner { + void run(Holder holder, T value) { + apply(value); + holder.member(value); + Holder::static_member(value); + // Several overloads remain viable: no hint. + overload(T{}); + } +}; diff --git a/tests/snap/inlay_hint/param_dependent.snap.yml b/tests/snap/inlay_hint/param_dependent.snap.yml new file mode 100644 index 0000000000..92ba629f69 --- /dev/null +++ b/tests/snap/inlay_hint/param_dependent.snap.yml @@ -0,0 +1,7 @@ +--- +created_at: 2026-07-31 +input_file: param_dependent.cpp +--- +- { pos: "26:14", kind: Parameter, label: "scale:", padding_right: true } +- { pos: "27:22", kind: Parameter, label: "item:", padding_right: true } +- { pos: "28:33", kind: Parameter, label: "slot:", padding_right: true } diff --git a/tests/snap/inlay_hint/param_explicit_instantiation.cpp b/tests/snap/inlay_hint/param_explicit_instantiation.cpp new file mode 100644 index 0000000000..d5d5361a05 --- /dev/null +++ b/tests/snap/inlay_hint/param_explicit_instantiation.cpp @@ -0,0 +1,16 @@ +/// # Parameter Hints +/// +/// ## Explicit instantiation — an explicit instantiation definition adds no duplicate hints +/// +/// - status: supported +/// - issues: clangd#1034 +/// - order: 14 + +template +void apply(T value) {} + +template void apply(int value); + +void use() { + apply(42); +} diff --git a/tests/snap/inlay_hint/param_explicit_instantiation.snap.yml b/tests/snap/inlay_hint/param_explicit_instantiation.snap.yml new file mode 100644 index 0000000000..a809e22201 --- /dev/null +++ b/tests/snap/inlay_hint/param_explicit_instantiation.snap.yml @@ -0,0 +1,5 @@ +--- +created_at: 2026-07-31 +input_file: param_explicit_instantiation.cpp +--- +- { pos: "14:10", kind: Parameter, label: "value:", padding_right: true } diff --git a/tests/snap/inlay_hint/param_forwarding.cpp b/tests/snap/inlay_hint/param_forwarding.cpp new file mode 100644 index 0000000000..7055ca2c6a --- /dev/null +++ b/tests/snap/inlay_hint/param_forwarding.cpp @@ -0,0 +1,54 @@ +/// # Parameter Hints +/// +/// ## Forwarding resolution — packs forwarded through wrappers resolve to the target's parameter names +/// +/// - status: supported +/// - issues: clangd#2324 +/// - order: 5 + +namespace std { + +template +struct remove_reference { + using type = T; +}; + +template +constexpr T&& forward(typename remove_reference::type& t) noexcept; + +} // namespace std + +void target(int first, int second); + +template +void wrap(Args&&... args) { + target(std::forward(args)...); +} + +// A plain pass-through works without std::forward as well. +void sink(int a, int b, int c); + +template +void call_with(Ts... ts) { + sink(ts...); +} + +// Forwarding also resolves through packs sandwiched between fixed +// head and tail arguments. +int accumulate(int, int b, double); + +template +int head_tail(int a, Args&&... args) { + return accumulate(1, std::forward(args)..., 1.0); +} + +template +int chain(Args&&... args) { + return head_tail(std::forward(args)...); +} + +void use() { + wrap(1, 2); + call_with(1, 2, 3); + chain(32, 42); +} diff --git a/tests/snap/inlay_hint/param_forwarding.snap.yml b/tests/snap/inlay_hint/param_forwarding.snap.yml new file mode 100644 index 0000000000..ddfa1330c5 --- /dev/null +++ b/tests/snap/inlay_hint/param_forwarding.snap.yml @@ -0,0 +1,11 @@ +--- +created_at: 2026-07-31 +input_file: param_forwarding.cpp +--- +- { pos: "50:9", kind: Parameter, label: "first:", padding_right: true } +- { pos: "50:12", kind: Parameter, label: "second:", padding_right: true } +- { pos: "51:14", kind: Parameter, label: "a:", padding_right: true } +- { pos: "51:17", kind: Parameter, label: "b:", padding_right: true } +- { pos: "51:20", kind: Parameter, label: "c:", padding_right: true } +- { pos: "52:10", kind: Parameter, label: "a:", padding_right: true } +- { pos: "52:14", kind: Parameter, label: "b:", padding_right: true } diff --git a/tests/snap/inlay_hint/param_function_objects.cpp b/tests/snap/inlay_hint/param_function_objects.cpp new file mode 100644 index 0000000000..6c43e1d52d --- /dev/null +++ b/tests/snap/inlay_hint/param_function_objects.cpp @@ -0,0 +1,22 @@ +/// # Parameter Hints +/// +/// ## Function pointers and call operators — indirect calls still name their parameters +/// +/// - status: supported +/// - issues: clangd#1734, clangd#1742 +/// - order: 7 + +struct Callback { + void operator()(int status, int detail) const; +}; + +void (*handler)(int status, const char* message); + +void use() { + Callback cb; + cb(1, 2); + cb.operator()(3, 4); + handler(0, "ok"); + auto cmp = [](int lhs, int rhs) { return lhs < rhs; }; + cmp(1, 2); +} diff --git a/tests/snap/inlay_hint/param_function_objects.snap.yml b/tests/snap/inlay_hint/param_function_objects.snap.yml new file mode 100644 index 0000000000..517899b97b --- /dev/null +++ b/tests/snap/inlay_hint/param_function_objects.snap.yml @@ -0,0 +1,14 @@ +--- +created_at: 2026-07-31 +input_file: param_function_objects.cpp +--- +- { pos: "16:7", kind: Parameter, label: "status:", padding_right: true } +- { pos: "16:10", kind: Parameter, label: "detail:", padding_right: true } +- { pos: "17:18", kind: Parameter, label: "status:", padding_right: true } +- { pos: "17:21", kind: Parameter, label: "detail:", padding_right: true } +- { pos: "18:12", kind: Parameter, label: "status:", padding_right: true } +- { pos: "18:15", kind: Parameter, label: "message:", padding_right: true } +- { pos: "19:12", kind: Type, label: ": (lambda)" } +- { pos: "19:35", kind: Type, label: "-> bool" } +- { pos: "20:8", kind: Parameter, label: "lhs:", padding_right: true } +- { pos: "20:11", kind: Parameter, label: "rhs:", padding_right: true } diff --git a/tests/snap/inlay_hint/param_hints.cpp b/tests/snap/inlay_hint/param_hints.cpp deleted file mode 100644 index 32dde73c48..0000000000 --- a/tests/snap/inlay_hint/param_hints.cpp +++ /dev/null @@ -1,169 +0,0 @@ -// Parameter name hints and their suppression rules: spelled-name match, -// /*name=*/ comments, setters, std builtins, mutable-reference markers, -// pack forwarding, names recovered from definitions, underscore stripping -// and constructors. - -namespace std { - -template -struct remove_reference { - using type = T; -}; - -template -struct remove_reference { - using type = T; -}; - -template -struct remove_reference { - using type = T; -}; - -template -constexpr T&& forward(typename remove_reference::type& t) noexcept; - -template -constexpr typename remove_reference::type&& move(T&& t) noexcept; - -} // namespace std - -namespace basic { - -void draw(int width, int height); - -void use() { - draw(10, 20); -} - -} // namespace basic - -namespace spelled_name_suppression { - -void draw(int width, int height); - -void use() { - int width = 5; - int h = 2; - draw(width, h); -} - -} // namespace spelled_name_suppression - -namespace comment_suppression { - -void draw(int width, int height); - -void use() { - draw(/*width=*/1, /*height=*/2); -} - -} // namespace comment_suppression - -namespace setters { - -struct Config { - void setWidth(int width); - void set_timeout(int timeout_millis); -}; - -void use(Config& config) { - config.setWidth(3); - config.set_timeout(5); -} - -} // namespace setters - -namespace builtins { - -void consume(int&& sink); - -void use() { - int value = 1; - consume(std::move(value)); -} - -} // namespace builtins - -namespace ref_markers { - -void mutate(int& value); -void observe(const int& value); - -void use() { - int v = 0; - mutate(v); - observe(v); -} - -} // namespace ref_markers - -namespace forwarding { - -void target(int first, int second); - -template -void wrap(Args&&... args) { - target(std::forward(args)...); -} - -void use() { - wrap(1, 2); -} - -} // namespace forwarding - -namespace names_from_definition { - -void resize(int, int); - -void use() { - resize(800, 600); -} - -void resize(int width, int height) {} - -} // namespace names_from_definition - -namespace underscores { - -void fill(int _value, int __count); - -void use() { - fill(1, 2); -} - -} // namespace underscores - -// Pack parameters resolve through a plain pass-through call (no -// std::forward needed): the wrapper's pack is mapped to the target's -// named parameters. -namespace pack_forwarding { - -void sink(int a, int b, int c); - -template -void call_with(Ts... ts) { - sink(ts...); -} - -void use() { - call_with(1, 2, 3); -} - -} // namespace pack_forwarding - -namespace constructors { - -struct Point { - Point(int x, int y); - Point(const Point& other); -}; - -void use() { - Point p(1, 2); - Point q(p); - Point r{3, 4}; -} - -} // namespace constructors diff --git a/tests/snap/inlay_hint/param_hints.snap.yml b/tests/snap/inlay_hint/param_hints.snap.yml deleted file mode 100644 index 6b0bfc83d1..0000000000 --- a/tests/snap/inlay_hint/param_hints.snap.yml +++ /dev/null @@ -1,24 +0,0 @@ ---- -created_at: 2026-07-30 -input_file: param_hints.cpp ---- -- { pos: "35:9", kind: Parameter, label: "width:", padding_right: true } -- { pos: "35:13", kind: Parameter, label: "height:", padding_right: true } -- { pos: "47:16", kind: Parameter, label: "height:", padding_right: true } -- { pos: "71:23", kind: Parameter, label: "timeout_millis:", padding_right: true } -- { pos: "82:12", kind: Parameter, label: "sink:", padding_right: true } -- { pos: "94:11", kind: Parameter, label: "&value:", padding_right: true } -- { pos: "95:12", kind: Parameter, label: "value:", padding_right: true } -- { pos: "110:9", kind: Parameter, label: "first:", padding_right: true } -- { pos: "110:12", kind: Parameter, label: "second:", padding_right: true } -- { pos: "120:11", kind: Parameter, label: "width:", padding_right: true } -- { pos: "120:16", kind: Parameter, label: "height:", padding_right: true } -- { pos: "132:9", kind: Parameter, label: "value:", padding_right: true } -- { pos: "132:12", kind: Parameter, label: "count:", padding_right: true } -- { pos: "150:14", kind: Parameter, label: "a:", padding_right: true } -- { pos: "150:17", kind: Parameter, label: "b:", padding_right: true } -- { pos: "150:20", kind: Parameter, label: "c:", padding_right: true } -- { pos: "163:12", kind: Parameter, label: "x:", padding_right: true } -- { pos: "163:15", kind: Parameter, label: "y:", padding_right: true } -- { pos: "165:12", kind: Parameter, label: "x:", padding_right: true } -- { pos: "165:15", kind: Parameter, label: "y:", padding_right: true } diff --git a/tests/snap/inlay_hint/param_implicit_conversions.cpp b/tests/snap/inlay_hint/param_implicit_conversions.cpp new file mode 100644 index 0000000000..16282e7b9a --- /dev/null +++ b/tests/snap/inlay_hint/param_implicit_conversions.cpp @@ -0,0 +1,22 @@ +/// # Parameter Hints +/// +/// ## Implicit constructor calls — conversions the code never wrote produce no hints of their own +/// +/// - status: supported +/// - order: 12 + +struct Seconds { + Seconds(int raw); +}; + +void wait(Seconds); +void hold(Seconds duration); + +Seconds use() { + // The implicit Seconds(5) must not surface `raw:`. + wait(5); + // The written call still hints its own parameter. + hold(6); + // Nor does the conversion in a return statement. + return 7; +} diff --git a/tests/snap/inlay_hint/param_implicit_conversions.snap.yml b/tests/snap/inlay_hint/param_implicit_conversions.snap.yml new file mode 100644 index 0000000000..a4ed9f4747 --- /dev/null +++ b/tests/snap/inlay_hint/param_implicit_conversions.snap.yml @@ -0,0 +1,5 @@ +--- +created_at: 2026-07-31 +input_file: param_implicit_conversions.cpp +--- +- { pos: "18:9", kind: Parameter, label: "duration:", padding_right: true } diff --git a/tests/snap/inlay_hint/param_inherited_constructors.cpp b/tests/snap/inlay_hint/param_inherited_constructors.cpp new file mode 100644 index 0000000000..3225f94429 --- /dev/null +++ b/tests/snap/inlay_hint/param_inherited_constructors.cpp @@ -0,0 +1,18 @@ +/// # Parameter Hints +/// +/// ## Inherited constructors — `using Base::Base` calls lose their parameter names +/// +/// - status: partial +/// - issues: clangd#1364 +/// - order: 16 + +struct Base { + Base(int width); +}; + +struct Derived : Base { + using Base::Base; +}; + +// No `width:` hint yet. +Derived d(7); diff --git a/tests/snap/inlay_hint/param_inherited_constructors.snap.yml b/tests/snap/inlay_hint/param_inherited_constructors.snap.yml new file mode 100644 index 0000000000..d341d2c9fe --- /dev/null +++ b/tests/snap/inlay_hint/param_inherited_constructors.snap.yml @@ -0,0 +1,4 @@ +--- +created_at: 2026-07-31 +input_file: param_inherited_constructors.cpp +--- diff --git a/tests/snap/inlay_hint/param_macros.cpp b/tests/snap/inlay_hint/param_macros.cpp new file mode 100644 index 0000000000..5a1fe2dd78 --- /dev/null +++ b/tests/snap/inlay_hint/param_macros.cpp @@ -0,0 +1,27 @@ +/// # Parameter Hints +/// +/// ## Macros at call sites — arguments spelled as macros hint; calls generated inside macro bodies do not +/// +/// - status: supported +/// - issues: clangd#2620 +/// - order: 11 + +void report(double value); +void plot(double x, double y); +int check(int status); + +#define PI 3.14 +#define CALL_REPORT() report(2.71) +#define PAIR 1.0, 2.0 +#define ASSERT(expr) if(!(expr)) {} + +void use() { + // An object-like macro is still one written argument. + report(PI); + // The call only exists inside the macro body. + CALL_REPORT(); + // One macro covering several arguments has no place to anchor. + plot(PAIR); + // Code written as a macro argument keeps its hints. + ASSERT(check(42) == 0); +} diff --git a/tests/snap/inlay_hint/param_macros.snap.yml b/tests/snap/inlay_hint/param_macros.snap.yml new file mode 100644 index 0000000000..03a548c695 --- /dev/null +++ b/tests/snap/inlay_hint/param_macros.snap.yml @@ -0,0 +1,6 @@ +--- +created_at: 2026-07-31 +input_file: param_macros.cpp +--- +- { pos: "19:11", kind: Parameter, label: "value:", padding_right: true } +- { pos: "25:17", kind: Parameter, label: "status:", padding_right: true } diff --git a/tests/snap/inlay_hint/param_names.cpp b/tests/snap/inlay_hint/param_names.cpp new file mode 100644 index 0000000000..6071d96873 --- /dev/null +++ b/tests/snap/inlay_hint/param_names.cpp @@ -0,0 +1,25 @@ +/// # Parameter Hints +/// +/// ## Parameter name hints — argument names at call sites and constructor calls +/// +/// - status: supported +/// - order: 1 + +void draw(int width, int height); + +struct Point { + Point(int x, int y); + Point(const Point& other); + Point(Point&& other); +}; + +void use() { + draw(10, 20); + Point p(1, 2); + Point q{3, 4}; + // Copy and move constructors stay quiet; a temporary's own braces + // still hint (the outer prvalue construction is elided anyway). + Point r(p); + Point m(Point{5, 6}); + Point s(static_cast(r)); +} diff --git a/tests/snap/inlay_hint/param_names.snap.yml b/tests/snap/inlay_hint/param_names.snap.yml new file mode 100644 index 0000000000..03a052b4ba --- /dev/null +++ b/tests/snap/inlay_hint/param_names.snap.yml @@ -0,0 +1,12 @@ +--- +created_at: 2026-07-31 +input_file: param_names.cpp +--- +- { pos: "16:9", kind: Parameter, label: "width:", padding_right: true } +- { pos: "16:13", kind: Parameter, label: "height:", padding_right: true } +- { pos: "17:12", kind: Parameter, label: "x:", padding_right: true } +- { pos: "17:15", kind: Parameter, label: "y:", padding_right: true } +- { pos: "18:12", kind: Parameter, label: "x:", padding_right: true } +- { pos: "18:15", kind: Parameter, label: "y:", padding_right: true } +- { pos: "22:18", kind: Parameter, label: "x:", padding_right: true } +- { pos: "22:21", kind: Parameter, label: "y:", padding_right: true } diff --git a/tests/snap/inlay_hint/param_operators.cpp b/tests/snap/inlay_hint/param_operators.cpp new file mode 100644 index 0000000000..eadaee985e --- /dev/null +++ b/tests/snap/inlay_hint/param_operators.cpp @@ -0,0 +1,27 @@ +/// # Parameter Hints +/// +/// ## Operators and literals — operator syntax and user-defined literals stay bare; member and default member initializers hint +/// +/// - status: supported +/// - order: 18 + +struct S { + S(int param); +}; + +void operator+(S lhs, S rhs); + +long double operator""_w(long double param); + +struct Holder { + S member; + S defaulted{3}; + Holder() : member(42) {} +}; + +void use() { + S a(1); + S b(2); + a + b; + 1.2_w; +} diff --git a/tests/snap/inlay_hint/param_operators.snap.yml b/tests/snap/inlay_hint/param_operators.snap.yml new file mode 100644 index 0000000000..18324986d9 --- /dev/null +++ b/tests/snap/inlay_hint/param_operators.snap.yml @@ -0,0 +1,8 @@ +--- +created_at: 2026-07-31 +input_file: param_operators.cpp +--- +- { pos: "17:16", kind: Parameter, label: "param:", padding_right: true } +- { pos: "18:22", kind: Parameter, label: "param:", padding_right: true } +- { pos: "22:8", kind: Parameter, label: "param:", padding_right: true } +- { pos: "23:8", kind: Parameter, label: "param:", padding_right: true } diff --git a/tests/snap/inlay_hint/param_pack_constructors.cpp b/tests/snap/inlay_hint/param_pack_constructors.cpp new file mode 100644 index 0000000000..d5d85a865f --- /dev/null +++ b/tests/snap/inlay_hint/param_pack_constructors.cpp @@ -0,0 +1,30 @@ +/// # Parameter Hints +/// +/// ## Packs in constructor arguments — outer calls resolve; hints inside the expansion are still missing +/// +/// - status: partial +/// - order: 19 + +struct Foo { + Foo(); + Foo(int x); +}; + +void consume(Foo a, int b); + +template +void relay(Args... args) { + consume(args...); +} + +template +void construct(Args... args) { + // The written Foo{args...} and the literal after it get no hints yet. + consume(Foo{args...}, 1); +} + +void use() { + relay(Foo{}, 42); + relay(42, 42); + construct(42); +} diff --git a/tests/snap/inlay_hint/param_pack_constructors.snap.yml b/tests/snap/inlay_hint/param_pack_constructors.snap.yml new file mode 100644 index 0000000000..3430f29dbf --- /dev/null +++ b/tests/snap/inlay_hint/param_pack_constructors.snap.yml @@ -0,0 +1,9 @@ +--- +created_at: 2026-07-31 +input_file: param_pack_constructors.cpp +--- +- { pos: "26:10", kind: Parameter, label: "a:", padding_right: true } +- { pos: "26:17", kind: Parameter, label: "b:", padding_right: true } +- { pos: "27:10", kind: Parameter, label: "a:", padding_right: true } +- { pos: "27:14", kind: Parameter, label: "b:", padding_right: true } +- { pos: "28:14", kind: Parameter, label: "x:", padding_right: true } diff --git a/tests/snap/inlay_hint/param_packs.cpp b/tests/snap/inlay_hint/param_packs.cpp new file mode 100644 index 0000000000..9b234c9c0b --- /dev/null +++ b/tests/snap/inlay_hint/param_packs.cpp @@ -0,0 +1,20 @@ +/// # Parameter Hints +/// +/// ## Unexpanded packs — a written pack expansion breaks the 1:1 argument mapping and stops hinting +/// +/// - status: supported +/// - order: 10 + +void plot(int x, int y, int z); + +template +void relay(Ts... ts) { + // `ts...` may instantiate to any number of arguments. + plot(0, ts...); +} + +void use() { + // The outer call still resolves through pack forwarding: 1 and 2 land + // in plot's y and z. + relay(1, 2); +} diff --git a/tests/snap/inlay_hint/param_packs.snap.yml b/tests/snap/inlay_hint/param_packs.snap.yml new file mode 100644 index 0000000000..2ddd31c44e --- /dev/null +++ b/tests/snap/inlay_hint/param_packs.snap.yml @@ -0,0 +1,7 @@ +--- +created_at: 2026-07-31 +input_file: param_packs.cpp +--- +- { pos: "12:9", kind: Parameter, label: "x:", padding_right: true } +- { pos: "18:10", kind: Parameter, label: "y:", padding_right: true } +- { pos: "18:13", kind: Parameter, label: "z:", padding_right: true } diff --git a/tests/snap/inlay_hint/param_pseudo_objects.cpp b/tests/snap/inlay_hint/param_pseudo_objects.cpp new file mode 100644 index 0000000000..81af55413f --- /dev/null +++ b/tests/snap/inlay_hint/param_pseudo_objects.cpp @@ -0,0 +1,29 @@ +/// # Parameter Hints +/// +/// ## Pseudo-object expressions — MS property accesses stay quiet; written subscripts keep the accessor's names +/// +/// - status: supported +/// - order: 13 + +int printf(const char* Format, ...); + +struct State { + __declspec(property(get = GetX, put = PutX)) int x[]; + int GetX(int row, int column); + void PutX(int value); + + // The syntactic form is a binary operator: no `value:` hint on `y`. + void Work(int y) { + x = y; + } +}; + +int use() { + State s; + // The semantic form of __builtin_dump_struct calls printf; none of it + // is written here. + __builtin_dump_struct(&s, printf); + printf("%d", 42); + // Property subscripts read best with the accessor's parameter names. + return s.x[1][2]; +} diff --git a/tests/snap/inlay_hint/param_pseudo_objects.snap.yml b/tests/snap/inlay_hint/param_pseudo_objects.snap.yml new file mode 100644 index 0000000000..2960ed26fa --- /dev/null +++ b/tests/snap/inlay_hint/param_pseudo_objects.snap.yml @@ -0,0 +1,7 @@ +--- +created_at: 2026-07-31 +input_file: param_pseudo_objects.cpp +--- +- { pos: "25:11", kind: Parameter, label: "Format:", padding_right: true } +- { pos: "27:15", kind: Parameter, label: "row:", padding_right: true } +- { pos: "27:18", kind: Parameter, label: "column:", padding_right: true } diff --git a/tests/snap/inlay_hint/param_references.cpp b/tests/snap/inlay_hint/param_references.cpp new file mode 100644 index 0000000000..a61fe11ffd --- /dev/null +++ b/tests/snap/inlay_hint/param_references.cpp @@ -0,0 +1,18 @@ +/// # Parameter Hints +/// +/// ## Mutable reference markers — `&` flags arguments passed by non-const lvalue reference +/// +/// - status: supported +/// - issues: clangd#1123 +/// - order: 4 + +void mutate(int& value); +void observe(const int& value); +void take(int&& value); + +void use() { + int v = 0; + mutate(v); + observe(v); + take(static_cast(v)); +} diff --git a/tests/snap/inlay_hint/param_references.snap.yml b/tests/snap/inlay_hint/param_references.snap.yml new file mode 100644 index 0000000000..f228dd4c11 --- /dev/null +++ b/tests/snap/inlay_hint/param_references.snap.yml @@ -0,0 +1,7 @@ +--- +created_at: 2026-07-31 +input_file: param_references.cpp +--- +- { pos: "14:11", kind: Parameter, label: "&value:", padding_right: true } +- { pos: "15:12", kind: Parameter, label: "value:", padding_right: true } +- { pos: "16:9", kind: Parameter, label: "value:", padding_right: true } diff --git a/tests/snap/inlay_hint/param_setters_builtins.cpp b/tests/snap/inlay_hint/param_setters_builtins.cpp new file mode 100644 index 0000000000..c3be3fc113 --- /dev/null +++ b/tests/snap/inlay_hint/param_setters_builtins.cpp @@ -0,0 +1,60 @@ +/// # Parameter Hints +/// +/// ## Setter and builtin suppression — `setX(x)` and `std::move`/`std::forward` arguments stay bare +/// +/// - status: supported +/// - order: 3 + +namespace std { + +template +struct remove_reference { + using type = T; +}; + +template +struct remove_reference { + using type = T; +}; + +template +struct remove_reference { + using type = T; +}; + +template +constexpr T&& forward(typename remove_reference::type& t) noexcept; + +template +constexpr typename remove_reference::type&& move(T&& t) noexcept; + +} // namespace std + +struct Config { + void setWidth(int width); + void set_height(int height); + // The parameter carries extra information beyond the setter name, so + // it still hints. + void setTimeout(int timeout_millis); +}; + +void consume(int&& sink); + +// The three-argument algorithm form of std::move is a real call whose +// parameters deserve hints; only the single-argument cast stays bare. +namespace std { + +template +T* move(T* first, T* last, T* result); + +} // namespace std + +void use(Config& config) { + config.setWidth(3); + config.set_height(4); + config.setTimeout(5); + int value = 1; + consume(std::move(value)); + int buffer[4]; + std::move(buffer, buffer + 2, buffer + 2); +} diff --git a/tests/snap/inlay_hint/param_setters_builtins.snap.yml b/tests/snap/inlay_hint/param_setters_builtins.snap.yml new file mode 100644 index 0000000000..2e60a240c8 --- /dev/null +++ b/tests/snap/inlay_hint/param_setters_builtins.snap.yml @@ -0,0 +1,9 @@ +--- +created_at: 2026-07-31 +input_file: param_setters_builtins.cpp +--- +- { pos: "54:22", kind: Parameter, label: "timeout_millis:", padding_right: true } +- { pos: "56:12", kind: Parameter, label: "sink:", padding_right: true } +- { pos: "58:14", kind: Parameter, label: "first:", padding_right: true } +- { pos: "58:22", kind: Parameter, label: "last:", padding_right: true } +- { pos: "58:34", kind: Parameter, label: "result:", padding_right: true } diff --git a/tests/snap/inlay_hint/param_suppression.cpp b/tests/snap/inlay_hint/param_suppression.cpp new file mode 100644 index 0000000000..ada6853e2c --- /dev/null +++ b/tests/snap/inlay_hint/param_suppression.cpp @@ -0,0 +1,37 @@ +/// # Parameter Hints +/// +/// ## Hint suppression — arguments that already spell the parameter name, and `/*name=*/` comments +/// +/// - status: supported +/// - issues: clangd#1877 +/// - order: 2 + +void draw(int width, int height); + +void use() { + int width = 5; + int h = 2; + // `width` matches the parameter spelling: only `height:` hints. + draw(width, h); + // An inline comment naming the parameter serves the same purpose; + // a comment naming something else does not. + draw(/*width=*/1, /*height=*/2); + draw(/*margin=*/6, 7); +} + +struct Sizes { + static int width; + int height; + + void member() { + // A bare member access spells the parameter name: suppressed. + draw(5, height); + } +}; + +void qualified(Sizes s) { + // A qualified name is not a plain spelling match. + draw(Sizes::width, 3); + // Neither is an access through a written base object. + draw(4, s.height); +} diff --git a/tests/snap/inlay_hint/param_suppression.snap.yml b/tests/snap/inlay_hint/param_suppression.snap.yml new file mode 100644 index 0000000000..183c8d9aea --- /dev/null +++ b/tests/snap/inlay_hint/param_suppression.snap.yml @@ -0,0 +1,12 @@ +--- +created_at: 2026-07-31 +input_file: param_suppression.cpp +--- +- { pos: "14:16", kind: Parameter, label: "height:", padding_right: true } +- { pos: "18:20", kind: Parameter, label: "width:", padding_right: true } +- { pos: "18:23", kind: Parameter, label: "height:", padding_right: true } +- { pos: "27:13", kind: Parameter, label: "width:", padding_right: true } +- { pos: "33:9", kind: Parameter, label: "width:", padding_right: true } +- { pos: "33:23", kind: Parameter, label: "height:", padding_right: true } +- { pos: "35:9", kind: Parameter, label: "width:", padding_right: true } +- { pos: "35:12", kind: Parameter, label: "height:", padding_right: true } diff --git a/tests/snap/inlay_hint/template_parameter_hints.cpp b/tests/snap/inlay_hint/template_parameter_hints.cpp new file mode 100644 index 0000000000..54b8e1aa40 --- /dev/null +++ b/tests/snap/inlay_hint/template_parameter_hints.cpp @@ -0,0 +1,13 @@ +/// # Other Hint Kinds +/// +/// ## Template parameter hints — deduced and explicit template arguments at call sites +/// +/// - status: unsupported +/// - issues: clangd#2583 +/// - order: 1 + +template +T convert(U val); + +// Could hint `T: float` next to the explicit argument list. +float converted = convert(42); diff --git a/tests/snap/inlay_hint/type_auto.cpp b/tests/snap/inlay_hint/type_auto.cpp new file mode 100644 index 0000000000..59705f9f33 --- /dev/null +++ b/tests/snap/inlay_hint/type_auto.cpp @@ -0,0 +1,14 @@ +/// # Type Hints +/// +/// ## Deduced `auto` variables — the hint shows the full variable type, qualifiers included +/// +/// - status: supported +/// - order: 1 + +int make(); + +void use() { + auto value = make(); + const auto& ref = value; + auto* ptr = &value; +} diff --git a/tests/snap/inlay_hint/type_auto.snap.yml b/tests/snap/inlay_hint/type_auto.snap.yml new file mode 100644 index 0000000000..226e442c77 --- /dev/null +++ b/tests/snap/inlay_hint/type_auto.snap.yml @@ -0,0 +1,7 @@ +--- +created_at: 2026-07-31 +input_file: type_auto.cpp +--- +- { pos: "10:14", kind: Type, label: ": int" } +- { pos: "11:19", kind: Type, label: ": const int &" } +- { pos: "12:13", kind: Type, label: ": int *" } diff --git a/tests/snap/inlay_hint/type_auto_params.cpp b/tests/snap/inlay_hint/type_auto_params.cpp new file mode 100644 index 0000000000..2b7d3d0cec --- /dev/null +++ b/tests/snap/inlay_hint/type_auto_params.cpp @@ -0,0 +1,37 @@ +/// # Type Hints +/// +/// ## `auto` parameters — a template with exactly one instantiation reveals the deduced type +/// +/// - status: supported +/// - order: 7 + +int twice(auto x) { + return x + x; +} + +int result = twice(21); + +// A second instantiation makes the deduction ambiguous: no hint. +int measure(auto x) { + return 1; +} + +int a = measure(1); +int b = measure(2.0); + +// Packs and parameters after them never hint. +int spread(auto first, auto... rest, auto last) { + return 0; +} + +int c = spread(nullptr, 'x', 2.0f, 3); + +// Deduplication: a template body hints once across instantiations of the +// same deduced type. +template +void body() { + auto var = 42; +} + +template void body(); +template void body(); diff --git a/tests/snap/inlay_hint/type_auto_params.snap.yml b/tests/snap/inlay_hint/type_auto_params.snap.yml new file mode 100644 index 0000000000..99190e3e1c --- /dev/null +++ b/tests/snap/inlay_hint/type_auto_params.snap.yml @@ -0,0 +1,12 @@ +--- +created_at: 2026-07-31 +input_file: type_auto_params.cpp +--- +- { pos: "7:16", kind: Type, label: ": int" } +- { pos: "11:19", kind: Parameter, label: "x:", padding_right: true } +- { pos: "18:16", kind: Parameter, label: "x:", padding_right: true } +- { pos: "19:16", kind: Parameter, label: "x:", padding_right: true } +- { pos: "22:21", kind: Type, label: ": void *" } +- { pos: "26:35", kind: Parameter, label: "first:", padding_right: true } +- { pos: "26:55", kind: Parameter, label: "last:", padding_right: true } +- { pos: "32:12", kind: Type, label: ": int" } diff --git a/tests/snap/inlay_hint/type_auto_return.cpp b/tests/snap/inlay_hint/type_auto_return.cpp new file mode 100644 index 0000000000..245c26c458 --- /dev/null +++ b/tests/snap/inlay_hint/type_auto_return.cpp @@ -0,0 +1,36 @@ +/// # Type Hints +/// +/// ## Deduced return types — `-> T` after the parameter list, declarations included +/// +/// - status: supported +/// - order: 5 + +auto answer() { + return 42; +} + +auto& ref_answer() { + static int storage = 0; + return storage; +} + +// A declaration hints once a later definition supplies the deduction; a +// definition-less one stays silent. +auto declared(int x); +auto deducible(int x); + +auto deducible(int x) { + return x + 1; +} + +// Written trailing return types need no hint. +auto spelled() -> int; +auto pointer() -> auto* { + return "text"; +} + +struct Convertible { + operator auto() { + return 42; + } +}; diff --git a/tests/snap/inlay_hint/type_auto_return.snap.yml b/tests/snap/inlay_hint/type_auto_return.snap.yml new file mode 100644 index 0000000000..01ae8523b4 --- /dev/null +++ b/tests/snap/inlay_hint/type_auto_return.snap.yml @@ -0,0 +1,9 @@ +--- +created_at: 2026-07-31 +input_file: type_auto_return.cpp +--- +- { pos: "7:13", kind: Type, label: "-> int" } +- { pos: "11:18", kind: Type, label: "-> int &" } +- { pos: "19:21", kind: Type, label: "-> int" } +- { pos: "21:21", kind: Type, label: "-> int" } +- { pos: "32:19", kind: Type, label: "-> int" } diff --git a/tests/snap/inlay_hint/type_bindings_tuple.cpp b/tests/snap/inlay_hint/type_bindings_tuple.cpp new file mode 100644 index 0000000000..48dae10b9c --- /dev/null +++ b/tests/snap/inlay_hint/type_bindings_tuple.cpp @@ -0,0 +1,44 @@ +/// # Type Hints +/// +/// ## Tuple-protocol bindings — hints print the canonical type, not `tuple_element::type` +/// +/// - status: supported +/// - order: 11 + +struct IntPair { + int a; + int b; +}; + +namespace std { + +template +struct tuple_size {}; + +template <> +struct tuple_size { + constexpr static unsigned value = 2; +}; + +template +struct tuple_element {}; + +template +struct tuple_element { + using type = int; +}; + +} // namespace std + +template +int get(const IntPair& p) { + if constexpr(I == 0) { + return p.a; + } else { + return p.b; + } +} + +IntPair make(); + +auto [x, y] = make(); diff --git a/tests/snap/inlay_hint/type_bindings_tuple.snap.yml b/tests/snap/inlay_hint/type_bindings_tuple.snap.yml new file mode 100644 index 0000000000..38de046cd1 --- /dev/null +++ b/tests/snap/inlay_hint/type_bindings_tuple.snap.yml @@ -0,0 +1,6 @@ +--- +created_at: 2026-07-31 +input_file: type_bindings_tuple.cpp +--- +- { pos: "43:7", kind: Type, label: ": int" } +- { pos: "43:10", kind: Type, label: ": int" } diff --git a/tests/snap/inlay_hint/type_decltype.cpp b/tests/snap/inlay_hint/type_decltype.cpp new file mode 100644 index 0000000000..afd710be1b --- /dev/null +++ b/tests/snap/inlay_hint/type_decltype.cpp @@ -0,0 +1,29 @@ +/// # Type Hints +/// +/// ## `decltype` spellings — the underlying type shows next to the written `decltype` +/// +/// - status: supported +/// - order: 6 + +int source(); + +decltype(source()) value = 1; + +int& ref = value; +// decltype(auto) preserves the reference. +decltype(auto) forwarded = ref; + +// Every written decltype spelling hints: declarators, alias targets, +// return types and functional casts. +const decltype(0)& bound = value; + +decltype(0) declared(); + +auto trailing() -> decltype(0); + +template +struct Wrap; + +using Alias = Wrap; + +auto constructed = decltype(0){}; diff --git a/tests/snap/inlay_hint/type_decltype.snap.yml b/tests/snap/inlay_hint/type_decltype.snap.yml new file mode 100644 index 0000000000..67e944832a --- /dev/null +++ b/tests/snap/inlay_hint/type_decltype.snap.yml @@ -0,0 +1,12 @@ +--- +created_at: 2026-07-31 +input_file: type_decltype.cpp +--- +- { pos: "9:18", kind: Type, label: ": int" } +- { pos: "13:24", kind: Type, label: ": int &" } +- { pos: "17:17", kind: Type, label: ": int" } +- { pos: "19:11", kind: Type, label: ": int" } +- { pos: "21:30", kind: Type, label: ": int" } +- { pos: "26:30", kind: Type, label: ": int" } +- { pos: "28:16", kind: Type, label: ": int" } +- { pos: "28:30", kind: Type, label: ": int" } diff --git a/tests/snap/inlay_hint/type_dependent.cpp b/tests/snap/inlay_hint/type_dependent.cpp new file mode 100644 index 0000000000..4ff815b51c --- /dev/null +++ b/tests/snap/inlay_hint/type_dependent.cpp @@ -0,0 +1,15 @@ +/// # Type Hints +/// +/// ## Dependent `auto` — deduction inside an uninstantiated template body stays silent +/// +/// - status: partial +/// - issues: clangd#2275 +/// - order: 9 + +template +void body(T input) { + // No hint: the deduced type depends on T. + auto derived = input + 1; + // A dependence-free initializer still hints normally. + auto counter = 0; +} diff --git a/tests/snap/inlay_hint/type_dependent.snap.yml b/tests/snap/inlay_hint/type_dependent.snap.yml new file mode 100644 index 0000000000..529333e4a2 --- /dev/null +++ b/tests/snap/inlay_hint/type_dependent.snap.yml @@ -0,0 +1,5 @@ +--- +created_at: 2026-07-31 +input_file: type_dependent.cpp +--- +- { pos: "13:16", kind: Type, label: ": int" } diff --git a/tests/snap/inlay_hint/type_explicit_source.cpp b/tests/snap/inlay_hint/type_explicit_source.cpp new file mode 100644 index 0000000000..8f48823bd9 --- /dev/null +++ b/tests/snap/inlay_hint/type_explicit_source.cpp @@ -0,0 +1,16 @@ +/// # Type Hints +/// +/// ## Explicitly spelled initializers — casts and functional casts still hint redundantly +/// +/// - status: partial +/// - issues: clangd#1749 +/// - order: 8 + +int compute(); + +void use() { + // The type is already written on the right-hand side; ideally these + // two hints would be suppressed. + auto widened = static_cast(compute()); + auto braced = int{42}; +} diff --git a/tests/snap/inlay_hint/type_explicit_source.snap.yml b/tests/snap/inlay_hint/type_explicit_source.snap.yml new file mode 100644 index 0000000000..873137e324 --- /dev/null +++ b/tests/snap/inlay_hint/type_explicit_source.snap.yml @@ -0,0 +1,6 @@ +--- +created_at: 2026-07-31 +input_file: type_explicit_source.cpp +--- +- { pos: "13:16", kind: Type, label: ": long" } +- { pos: "14:15", kind: Type, label: ": int" } diff --git a/tests/snap/inlay_hint/type_hints.cpp b/tests/snap/inlay_hint/type_hints.cpp deleted file mode 100644 index a95b718505..0000000000 --- a/tests/snap/inlay_hint/type_hints.cpp +++ /dev/null @@ -1,91 +0,0 @@ -// Deduced type hints: sugar preservation vs desugaring, the length-limit -// fallback to the sugared spelling, structured bindings (canonical types), -// lambdas, auto parameters with a single instantiation, and auto return -// types. - -namespace basic_auto { - -int make(); - -void use() { - auto value = make(); - const auto& ref = value; - auto* ptr = &value; -} - -} // namespace basic_auto - -namespace alias_desugar { - -using Integer = int; - -Integer make(); - -void use() { - auto value = make(); -} - -} // namespace alias_desugar - -namespace long_type_fallback { - -template -struct extremely_long_template_name {}; - -using Compact = extremely_long_template_name; - -Compact make(); - -void use() { - auto value = make(); -} - -} // namespace long_type_fallback - -namespace bindings { - -struct Pair { - int first; - float second; -}; - -Pair make(); - -void use() { - auto [a, b] = make(); -} - -} // namespace bindings - -namespace lambdas { - -void use() { - auto callback = [](int x) { - return x; - }; -} - -} // namespace lambdas - -namespace auto_params { - -int twice(auto x) { - return x + x; -} - -int result = twice(21); - -} // namespace auto_params - -namespace return_types { - -auto answer() { - return 42; -} - -auto& ref_answer() { - static int storage = 0; - return storage; -} - -} // namespace return_types diff --git a/tests/snap/inlay_hint/type_hints.snap.yml b/tests/snap/inlay_hint/type_hints.snap.yml deleted file mode 100644 index c33d91c28e..0000000000 --- a/tests/snap/inlay_hint/type_hints.snap.yml +++ /dev/null @@ -1,17 +0,0 @@ ---- -created_at: 2026-07-30 -input_file: type_hints.cpp ---- -- { pos: "10:14", kind: Type, label: ": int" } -- { pos: "11:19", kind: Type, label: ": const int &" } -- { pos: "12:13", kind: Type, label: ": int *" } -- { pos: "24:14", kind: Type, label: ": Integer" } -- { pos: "39:14", kind: Type, label: ": Compact" } -- { pos: "54:11", kind: Type, label: ": int" } -- { pos: "54:14", kind: Type, label: ": float" } -- { pos: "62:17", kind: Type, label: ": (lambda)" } -- { pos: "62:29", kind: Type, label: "-> int" } -- { pos: "71:16", kind: Type, label: ": int" } -- { pos: "75:19", kind: Parameter, label: "x:", padding_right: true } -- { pos: "81:13", kind: Type, label: "-> int" } -- { pos: "85:18", kind: Type, label: "-> int &" } diff --git a/tests/snap/inlay_hint/type_lambdas.cpp b/tests/snap/inlay_hint/type_lambdas.cpp new file mode 100644 index 0000000000..f9ea5d41c1 --- /dev/null +++ b/tests/snap/inlay_hint/type_lambdas.cpp @@ -0,0 +1,18 @@ +/// # Type Hints +/// +/// ## Lambdas — variables, deduced return types, and init-captures all hint +/// +/// - status: supported +/// - issues: clangd#1163 +/// - order: 4 + +int compute(); + +void use() { + auto callback = [captured = compute()](int x) { + return x + captured; + }; + auto bare = [] { + return 1.5; + }; +} diff --git a/tests/snap/inlay_hint/type_lambdas.snap.yml b/tests/snap/inlay_hint/type_lambdas.snap.yml new file mode 100644 index 0000000000..292ac94e79 --- /dev/null +++ b/tests/snap/inlay_hint/type_lambdas.snap.yml @@ -0,0 +1,9 @@ +--- +created_at: 2026-07-31 +input_file: type_lambdas.cpp +--- +- { pos: "11:17", kind: Type, label: ": (lambda)" } +- { pos: "11:29", kind: Type, label: ": int" } +- { pos: "11:49", kind: Type, label: "-> int" } +- { pos: "14:13", kind: Type, label: ": (lambda)" } +- { pos: "14:18", kind: Type, label: "-> double" } diff --git a/tests/snap/inlay_hint/type_scopes.cpp b/tests/snap/inlay_hint/type_scopes.cpp new file mode 100644 index 0000000000..a8f7fb85e4 --- /dev/null +++ b/tests/snap/inlay_hint/type_scopes.cpp @@ -0,0 +1,24 @@ +/// # Type Hints +/// +/// ## Scope suppression — namespace qualifiers drop from hints; class scopes stay +/// +/// - status: supported +/// - order: 10 + +namespace outer { +namespace inner { + +struct S1 {}; +S1 make_s1(); +auto x = make_s1(); + +struct S2 { + template + struct Nested {}; +}; + +S2::Nested make_nested(); +auto y = make_nested(); + +} // namespace inner +} // namespace outer diff --git a/tests/snap/inlay_hint/type_scopes.snap.yml b/tests/snap/inlay_hint/type_scopes.snap.yml new file mode 100644 index 0000000000..ab3b799f46 --- /dev/null +++ b/tests/snap/inlay_hint/type_scopes.snap.yml @@ -0,0 +1,6 @@ +--- +created_at: 2026-07-31 +input_file: type_scopes.cpp +--- +- { pos: "12:6", kind: Type, label: ": S1" } +- { pos: "20:6", kind: Type, label: ": S2::Nested" } diff --git a/tests/snap/inlay_hint/type_structured_bindings.cpp b/tests/snap/inlay_hint/type_structured_bindings.cpp new file mode 100644 index 0000000000..16e45e15e7 --- /dev/null +++ b/tests/snap/inlay_hint/type_structured_bindings.cpp @@ -0,0 +1,20 @@ +/// # Type Hints +/// +/// ## Structured bindings — each binding hints its canonical type; the aggregate itself stays bare +/// +/// - status: supported +/// - order: 3 + +struct Pair { + int first; + float second; +}; + +Pair make(); + +int array[2]; + +void use() { + auto [a, b] = make(); + auto [x, y] = array; +} diff --git a/tests/snap/inlay_hint/type_structured_bindings.snap.yml b/tests/snap/inlay_hint/type_structured_bindings.snap.yml new file mode 100644 index 0000000000..793d88cbc0 --- /dev/null +++ b/tests/snap/inlay_hint/type_structured_bindings.snap.yml @@ -0,0 +1,8 @@ +--- +created_at: 2026-07-31 +input_file: type_structured_bindings.cpp +--- +- { pos: "17:11", kind: Type, label: ": int" } +- { pos: "17:14", kind: Type, label: ": float" } +- { pos: "18:11", kind: Type, label: ": int" } +- { pos: "18:14", kind: Type, label: ": int" } diff --git a/tests/snap/inlay_hint/type_sugar.cpp b/tests/snap/inlay_hint/type_sugar.cpp new file mode 100644 index 0000000000..410d332612 --- /dev/null +++ b/tests/snap/inlay_hint/type_sugar.cpp @@ -0,0 +1,34 @@ +/// # Type Hints +/// +/// ## Type sugar and the length limit — aliases keep their spelling; over-long types fall back to the sugared name +/// +/// - status: supported +/// - issues: clangd#1298, clangd#1357 +/// - order: 2 + +using Integer = int; + +Integer make_alias(); + +template +struct extremely_long_template_name {}; + +using Compact = extremely_long_template_name; + +Compact make_compact(); + +extremely_long_template_name make_long(); + +template +struct Defaulted {}; + +Defaulted make_defaulted(); + +void use() { + auto aliased = make_alias(); + auto shortened = make_compact(); + // No sugar short enough to fall back to: the hint is dropped. + auto dropped = make_long(); + // Default template arguments never print. + auto defaulted = make_defaulted(); +} diff --git a/tests/snap/inlay_hint/type_sugar.snap.yml b/tests/snap/inlay_hint/type_sugar.snap.yml new file mode 100644 index 0000000000..aa367b4e76 --- /dev/null +++ b/tests/snap/inlay_hint/type_sugar.snap.yml @@ -0,0 +1,7 @@ +--- +created_at: 2026-07-31 +input_file: type_sugar.cpp +--- +- { pos: "27:16", kind: Type, label: ": Integer" } +- { pos: "28:18", kind: Type, label: ": Compact" } +- { pos: "32:18", kind: Type, label: ": Defaulted" } diff --git a/tests/unit/feature/inlay_hint_tests.cpp b/tests/unit/feature/inlay_hint_tests.cpp index 3c8c6fb8b0..16187b5fa2 100644 --- a/tests/unit/feature/inlay_hint_tests.cpp +++ b/tests/unit/feature/inlay_hint_tests.cpp @@ -1,3 +1,9 @@ +/// Primary inlay-hint coverage lives in the snapshot corpus +/// (tests/snap/inlay_hint/), which pins both the standalone and the server +/// path under default options. This file keeps only the categories the +/// corpus cannot reach: block-end and default-argument hints are off by +/// default and only selectable through InlayHintsOptions. + #include #include @@ -69,840 +75,6 @@ void EXPECT_HINT(llvm::StringRef pos, ASSERT_EQ(label, name); }; -TEST_CASE(Parameters) { - /// Name hint for normal param. - run(R"c( - int foo(int param); - int x = foo(§(0)42); - )c"); - EXPECT_SIZE(1); - EXPECT_HINT("0", "param:"); - - // No hint for anonymous param. - run(R"c( - int foo(int); - int x = foo(42); - )c"); - EXPECT_SIZE(0); - - /// Reference hint for anonymous lvalue ref param. - run(R"c( - int foo(int&); - int x = 1; - int y = foo(§(0)x); - )c"); - EXPECT_SIZE(1); - EXPECT_HINT("0", "&:"); - - // No hint for anonymous const lvalue ref param. - run(R"c( - int foo(const int&); - int x = foo(42); - )c"); - EXPECT_SIZE(0); - - run(R"c( - template - int foo(Args&& ...); - int x = foo(42); - )c"); - EXPECT_SIZE(0); - - run(R"c( - namespace std { - template T&& forward(T&); - } - - int foo(int); - template - int bar(Args&&... args) { return foo(std::forward(args)...); } - int x = bar(42); - )c"); - EXPECT_SIZE(0); - - // No hint for anonymous r-value ref parameter - run(R"c( - int foo(int&&); - int x = foo(42); - )c"); - EXPECT_SIZE(0); - - // Parameter name picked up from definition if necessary - run(R"c( - int foo(int); - int x = foo(§(0)42); - int foo(int param) { - return 0; - } - )c"); - EXPECT_SIZE(1); - EXPECT_HINT("0", "param:"); - - // Parameter name picked up from definition if necessary - run(R"c( - int foo(int, int b); - int x = foo(§(0)42, §(1)42); - int foo(int a, int) { - return 0; - } - )c"); - EXPECT_SIZE(2); - EXPECT_HINT("0", "a:"); - EXPECT_HINT("1", "b:"); - - // Parameter name picked up from definition in a resolved forwarded parameter - run(R"c( - int foo(int, int); - template - int bar(Args... args) { - return foo(args...); - } - int x = bar(§(0)42, §(1)42); - int foo(int a, int b) { - return 0; - } - )c"); - EXPECT_SIZE(2); - EXPECT_HINT("0", "a:"); - EXPECT_HINT("1", "b:"); - - // Prefer name from declaration - run(R"c( - int foo(int good); - int x = foo(§(0)42); - int foo(int bad) { - return 0; - } - )c"); - EXPECT_SIZE(1); - EXPECT_HINT("0", "good:"); - - // Only name hint for const l-value ref parameter - run(R"c( - int foo(const int& param); - int x = foo(§(0)42); - )c"); - EXPECT_SIZE(1); - EXPECT_HINT("0", "param:"); - - // Only name hint for const l-value ref parameter via type alias - run(R"c( - using alias = const int&; - int foo(alias param); - int x = 1; - int y = foo(§(0)x); - )c"); - EXPECT_SIZE(1); - EXPECT_HINT("0", "param:"); - - // Reference and name hint for l-value ref parameter - run(R"c( - int foo(int& param); - int x = 1; - int y = foo(§(0)x); - )c"); - EXPECT_SIZE(1); - EXPECT_HINT("0", "¶m:"); - - // Reference and name hint for l-value ref parameter via type alias - run(R"c( - using alias = int&; - int foo(alias param); - int x = 1; - int y = foo(§(0)x); - )c"); - EXPECT_SIZE(1); - EXPECT_HINT("0", "¶m:"); - - // Only name hint for r-value ref parameter - run(R"c( - int foo(int&& param); - int x = foo(§(0)42); - )c"); - EXPECT_SIZE(1); - EXPECT_HINT("0", "param:"); - - // Arg matches param - run(R"c( - void foo(int param); - struct S { - static const int param = 42; - }; - void bar() { - int param = 42; - // Do not show redundant "param: param". - foo(param); - // But show it if the argument is qualified. - foo(§(0)S::param); - } - struct A { - int param; - void bar() { - // Do not show "param: param" for member-expr. - foo(param); - } - }; - )c"); - EXPECT_SIZE(1); - EXPECT_HINT("0", "param:"); - - // Arg matches param reference - run(R"c( - void foo(int& param); - void foo2(const int& param); - void bar() { - int param; - // show reference hint on mutable reference - foo(§(0)param); - // but not on const reference - foo2(param); - } - )c"); - EXPECT_SIZE(1); - EXPECT_HINT("0", "&:"); - - // Name hint for variadic parameter using std::forward in a constructor call - run(R"c( - namespace std { template T&& forward(T&); } - struct S { S(int a); }; - template - T bar(Args&&... args) { return T{std::forward(args)...}; } - int x = 1; - S y = bar(§(0)x); - )c"); - EXPECT_SIZE(1); - EXPECT_HINT("0", "a:"); - - // Name hint for variadic parameter in a constructor call - run(R"c( - struct S { S(int a); }; - template - T bar(Args&&... args) { return T{args...}; } - int x = 1; - S y = bar(§(0)x); - )c"); - EXPECT_SIZE(1); - EXPECT_HINT("0", "a:"); - - // Name for variadic parameter using std::forward - run(R"c( - namespace std { template T&& forward(T&); } - int foo(int a); - template - int bar(Args&&... args) { return foo(std::forward(args)...); } - int x = 1; - int y = bar(§(0)x); - )c"); - EXPECT_SIZE(1); - EXPECT_HINT("0", "a:"); - - // Name hint for variadic parameter - run(R"c( - int foo(int a); - template - int bar(Args&&... args) { return foo(args...); } - int x = bar(§(0)42); - )c"); - EXPECT_SIZE(1); - EXPECT_HINT("0", "a:"); - - // Name hint for variadic parameter when the parameter pack is not the last template - // parameter - run(R"c( - int foo(int a); - template - int bar(Arg, Args&&... args) { return foo(args...); } - int x = bar(1, §(0)42); - )c"); - EXPECT_SIZE(1); - EXPECT_HINT("0", "a:"); - - // Name for variadic parameter that involves both head and tail parameters - run(R"c( - namespace std { template T&& forward(T&); } - int baz(int, int b, double); - template - int foo(int a, Args&&... args) { - return baz(1, std::forward(args)..., 1.0); - } - template - int bar(Args&&... args) { return foo(std::forward(args)...); } - int x = bar(§(0)32, §(1)42); - )c"); - EXPECT_SIZE(2); - EXPECT_HINT("0", "a:"); - EXPECT_HINT("1", "b:"); - - // No hint for operator call with operator syntax - run(R"c( - struct S {}; - void operator+(S lhs, S rhs); - void bar() { - S a, b; - a + b; - } - )c"); - EXPECT_SIZE(0); - - // Function call operator - run(R"c( - struct W { - void operator()(int x); - }; - - struct S : W { - using W::operator(); - static void operator()(int x, int y); - }; - - void bar() { - auto l1 = [](int x) -> void {}; - auto l2 = [](int x) static -> void {}; - - S s; - s(§(0)1); - s.operator()(§(1)1); - s.operator()(§(2)1, §(3)2); - S::operator()(§(4)1, §(5)2); - - l1(§(6)1); - l1.operator()(§(7)1); - l2(§(8)1); - l2.operator()(§(9)1); - - void (*ptr)(int a, int b) = &S::operator(); - ptr(§(10)1, §(11)2); - } - )c"); - - EXPECT_HINT("0", "x:"); - EXPECT_HINT("1", "x:"); - EXPECT_HINT("2", "x:"); - EXPECT_HINT("3", "y:"); - EXPECT_HINT("4", "x:"); - EXPECT_HINT("5", "y:"); - EXPECT_HINT("6", "x:"); - EXPECT_HINT("7", "x:"); - EXPECT_HINT("8", "x:"); - EXPECT_HINT("9", "x:"); - EXPECT_HINT("10", "a:"); - EXPECT_HINT("11", "b:"); - - // Deducing this - run(R"c( - struct S { - template - int operator()(this This &&Self, int Param) { - return 42; - } - - int function(this auto &Self, int Param) { - return Param; - } - }; - void work() { - S s; - s(§(0)42); - s.function(§(1)42); - S()(§(2)42); - auto lambda = [](this auto &Self, char C) -> void { - return Self(C); - }; - lambda(§(3)'A'); - } - )c"); - - EXPECT_HINT("0", "Param:"); - EXPECT_HINT("1", "Param:"); - EXPECT_HINT("2", "Param:"); - EXPECT_HINT("3", "C:"); - - // Constructor with parentheses - run(R"c( - struct S { - S(int param); - }; - void bar() { - S obj(§(0)42); - } - )c"); - EXPECT_SIZE(1); - EXPECT_HINT("0", "param:"); - - // Constructor with braces - run(R"c( - struct S { - S(int param); - }; - void bar() { - S obj{§(0)42}; - } - )c"); - EXPECT_SIZE(1); - EXPECT_HINT("0", "param:"); - - // Member initialization - run(R"c( - struct S { - S(int param); - }; - - struct T { - S member; - T() : member(§(0)42) {} - }; - )c"); - EXPECT_SIZE(1); - EXPECT_HINT("0", "param:"); - - // Function pointer - run(R"c( - void (*f1)(int param); - void (*f2)(int param) noexcept; - using f3_t = void(*)(int param); - f3_t f3; - using f4_t = void(*)(int param) noexcept; - f4_t f4; - - void bar() { - f1(§(0)42); - f2(§(1)42); - f3(§(2)42); - f4(§(3)42); - } - )c"); - EXPECT_SIZE(4); - EXPECT_HINT("0", "param:"); - EXPECT_HINT("1", "param:"); - EXPECT_HINT("2", "param:"); - EXPECT_HINT("3", "param:"); - - // Leading underscore - run(R"c( - void foo(int p1, int _p2, int __p3); - void bar() { - foo(§(0)41, §(1)42, §(2)43); - } - )c"); - EXPECT_SIZE(3); - EXPECT_HINT("0", "p1:"); - EXPECT_HINT("1", "p2:"); - EXPECT_HINT("2", "p3:"); - - // Variadic function - run(R"c( - template - void foo(int fixed, T... variadic); - - void bar() { - foo(§(0)41, 42, 43); - } - )c"); - EXPECT_SIZE(1); - EXPECT_HINT("0", "fixed:"); - - // Varargs function - run(R"c( - void foo(int fixed, ...); - - void bar() { - foo(§(0)41, 42, 43); - } - )c"); - EXPECT_SIZE(1); - EXPECT_HINT("0", "fixed:"); - - // Do not show hint for parameter of copy or move constructor - run(R"c( - struct S { - S(); - S(const S& other); - S(S&& other); - }; - - void bar() { - S a; - S b = S(a); // copy - S c = S(S()); // move - } - )c"); - EXPECT_SIZE(0); - - // Do not hint call to user-defined literal operator - run(R"c( - long double operator ""_w(long double param); - void bar() { - 1.2_w; - } - )c"); - EXPECT_SIZE(0); - - // Parameter name comment - run(R"c( - void foo(int param); - void bar() { - foo(/*param*/42); - foo( /* param = */ 42); - #define X 42 - #define Y X - #define Z(...) Y - foo(/*param=*/Z(a)); - foo(§(0)Z(a)); - foo(/* the answer */§(1)42); - } - )c"); - EXPECT_SIZE(2); - EXPECT_HINT("0", "param:"); - EXPECT_HINT("1", "param:"); - - // Setter functions - run(R"c( - struct S { - void setParent(S* parent); - void set_parent(S* parent); - void setTimeout(int timeoutMillis); - void setTimeoutMillis(int timeout_millis); - }; - void bar() { - S s; - // Parameter name matches setter name - omit hint. - s.setParent(nullptr); - // Support snake_case - s.set_parent(nullptr); - // Parameter name may contain extra info - show hint. - s.setTimeout(§(0)120); - // FIXME: Ideally we'd want to omit this. - s.setTimeoutMillis(§(1)120); - } - )c"); - EXPECT_SIZE(2); - EXPECT_HINT("0", "timeoutMillis:"); - EXPECT_HINT("1", "timeout_millis:"); -}; - -TEST_CASE(Types) { - // Basic type hint - run(R"c( - auto waldo§(0) = 42; - )c"); - EXPECT_SIZE(1); - EXPECT_HINT("0", ": int"); - - // Decorations - run(R"c( - int x = 42; - auto* var1§(0) = &x; - auto&& var2§(1) = x; - const auto& var3§(2) = x; - )c"); - EXPECT_SIZE(3); - EXPECT_HINT("0", ": int *"); - EXPECT_HINT("1", ": int &"); - EXPECT_HINT("2", ": const int &"); - - // Decltype auto - run(R"c( - int x = 42; - int& y = x; - decltype(auto) z§(0) = y; - )c"); - EXPECT_SIZE(1); - EXPECT_HINT("0", ": int &"); - - // No qualifiers - run(R"c( - namespace A { - namespace B { - struct S1 {}; - S1 foo(); - auto x§(0) = foo(); - - struct S2 { - template - struct Inner {}; - }; - - S2::Inner bar(); - auto y§(1) = bar(); - } - } - )c"); - EXPECT_SIZE(2); - EXPECT_HINT("0", ": S1"); - EXPECT_HINT("1", ": S2::Inner"); - - // Lambda - run(R"c( - void f() { - int cap = 42; - auto L§(0) = [cap, init§(1) = 1 + 1](int a)§(2) { - return a + cap + init; - }; - } - )c"); - EXPECT_SIZE(3); - EXPECT_HINT("0", ": (lambda)"); - EXPECT_HINT("1", ": int"); - EXPECT_HINT("2", "-> int"); - - // Lambda return hint shown even if no param list - run(R"c( - auto x§(0) = []§(1){return 42;}; - )c"); - EXPECT_SIZE(2); - EXPECT_HINT("0", ": (lambda)"); - EXPECT_HINT("1", "-> int"); - - // Structured bindings - public struct - run(R"c( - struct Point { - int x; - int y; - }; - Point foo(); - auto [x§(0), y§(1)] = foo(); - )c"); - EXPECT_SIZE(2); - EXPECT_HINT("0", ": int"); - EXPECT_HINT("1", ": int"); - - // Structured bindings - array - run(R"c( - int arr[2]; - auto [x§(0), y§(1)] = arr; - )c"); - EXPECT_SIZE(2); - EXPECT_HINT("0", ": int"); - EXPECT_HINT("1", ": int"); - - // Structured bindings - tuple-like - run(R"c( - struct IntPair { - int a; - int b; - }; - - namespace std { - template - struct tuple_size {}; - - template <> - struct tuple_size { - constexpr static unsigned value = 2; - }; - - template - struct tuple_element {}; - - template - struct tuple_element { - using type = int; - }; - } - - template - int get(const IntPair& p) { - if constexpr (I == 0) { - return p.a; - } else if constexpr (I == 1) { - return p.b; - } - } - - IntPair bar(); - auto [x§(0), y§(1)] = bar(); - )c"); - EXPECT_SIZE(2); - EXPECT_HINT("0", ": int"); - EXPECT_HINT("1", ": int"); - - // Return type deduction - run(R"c( - auto f1(int x)§(0); // Hint forward declaration too - auto f1(int x)§(1) { return x + 1; } - - // Include pointer operators in hint - int s; - auto& f2()§(2) { return s; } - - // Do not hint `auto` for trailing return type. - auto f3() -> int; - - // Do not hint when a trailing return type is specified. - auto f4() -> auto* { return "foo"; } - - auto f5()§(3) {} - - // `auto` conversion operator - struct A { - operator auto()§(4) { return 42; } - }; - )c"); - EXPECT_SIZE(5); - EXPECT_HINT("0", "-> int"); - EXPECT_HINT("1", "-> int"); - EXPECT_HINT("2", "-> int &"); - EXPECT_HINT("3", "-> void"); - EXPECT_HINT("4", "-> int"); - - // Decltype - run(R"c( - decltype(0)§(0) a; - decltype(a)§(1) b; - const decltype(0)§(2) &c = b; - - decltype(0)§(3) e(); - auto f() -> decltype(0)§(4); - - template struct Foo; - using G = Foo; - - auto h§(6) = decltype(0)§(7){}; - )c"); - EXPECT_SIZE(8); - EXPECT_HINT("0", ": int"); - EXPECT_HINT("1", ": int"); - EXPECT_HINT("2", ": int"); - EXPECT_HINT("3", ": int"); - EXPECT_HINT("4", ": int"); - EXPECT_HINT("5", ": int"); - EXPECT_HINT("6", ": int"); - EXPECT_HINT("7", ": int"); - - // Long type name - run(R"c( - template - struct A {}; - struct MultipleWords {}; - A foo(); - // Omit type hint past a certain length (currently 32) - auto var = foo(); - )c"); - EXPECT_SIZE(0); - - // Default template args - run(R"c( - template - struct A {}; - A foo(); - auto var§(0) = foo(); - A bar[1]; - auto [binding§(1)] = bar; - )c"); - EXPECT_SIZE(2); - EXPECT_HINT("0", ": A"); - EXPECT_HINT("1", ": A"); - - // Deduplication - run(R"c( - template - void foo() { - auto var§(0) = 42; - } - - template void foo(); - template void foo(); - )c"); - EXPECT_SIZE(1); - EXPECT_HINT("0", ": int"); - - // Singly instantiated template - run(R"c( - auto lambda§(0) = [](auto* param§(1), auto) { return 42; }; - int m = lambda("foo", 3); - )c"); - EXPECT_HINT("0", ": (lambda)"); - EXPECT_HINT("1", ": const char *"); - - // No hint for packs, or auto params following packs - run(R"c( - int x(auto a§(0), auto... b, auto c) { return 42; } - int m = x(nullptr, 'c', 2.0, 2); - )c"); - EXPECT_HINT("0", ": void *"); -}; - -TEST_CASE(Designators, skip = true) { - // Basic designator hints - run(R"c( - struct S { int x, y, z; }; - S s {§(0)1, §(1)2+2}; - - int x[] = {§(2)0, §(3)1}; - )c"); - EXPECT_SIZE(4); - EXPECT_HINT("0", ".x="); - EXPECT_HINT("1", ".y="); - EXPECT_HINT("2", "[0]="); - EXPECT_HINT("3", "[1]="); - - // Nested designators - run(R"c( - struct Inner { int x, y; }; - struct Outer { Inner a, b; }; - Outer o{ §(0)a{ §(1)1, §(2)2 }, §(3)bx3 }; - )c"); - EXPECT_SIZE(4); - EXPECT_HINT("0", ".a="); - EXPECT_HINT("1", ".x="); - EXPECT_HINT("2", ".y="); - EXPECT_HINT("3", ".b.x="); - - // Anonymous record - run(R"c( - struct S { - union { - struct { - struct { - int y; - }; - } x; - }; - }; - S s{§(0)xy42}; - )c"); - EXPECT_SIZE(1); - EXPECT_HINT("0", ".x.y="); - - // Suppression - run(R"c( - struct Point { int a, b, c, d, e, f, g, h; }; - Point p{/*a=*/1, .c=2, /* .d = */3, §(0)4}; - )c"); - EXPECT_SIZE(1); - EXPECT_HINT("0", ".e="); - - // Std array - run(R"c( - template struct Array { T __elements[N]; }; - Array x = {§(0)0, §(1)1}; - )c"); - EXPECT_SIZE(2); - EXPECT_HINT("0", "[0]="); - EXPECT_HINT("1", "[1]="); - - // Only aggregate init - run(R"c( - struct Copyable { int x; } c; - Copyable d{c}; - - struct Constructible { Constructible(int x); }; - Constructible x{42}; - )c"); - EXPECT_SIZE(0); - - // No crash - run(R"c( - struct A {}; - struct Foo {int a; int b;}; - void test() { - Foo f{A(), §(0)1}; - } - )c"); - EXPECT_SIZE(1); - EXPECT_HINT("0", ".b="); -}; - TEST_CASE(BlockEnd) { // Functions run(R"c( @@ -927,7 +99,7 @@ TEST_CASE(BlockEnd) { return true; }§(2) )c", - {.parameters = false, .deduced_types = false, .block_end = true}); + {.parameters = false, .deduced_types = false, .designators = false, .block_end = true}); EXPECT_SIZE(3); EXPECT_HINT("0", "// foo"); EXPECT_HINT("1", "// bar"); @@ -975,7 +147,7 @@ TEST_CASE(BlockEnd) { void Test::method4() { }§(6) )c", - {.parameters = false, .deduced_types = false, .block_end = true}); + {.parameters = false, .deduced_types = false, .designators = false, .block_end = true}); EXPECT_SIZE(7); EXPECT_HINT("0", "// ~Test"); EXPECT_HINT("1", "// method1"); @@ -995,7 +167,7 @@ TEST_CASE(BlockEnd) { void bar(); }§(1) )c", - {.parameters = false, .deduced_types = false, .block_end = true}); + {.parameters = false, .deduced_types = false, .designators = false, .block_end = true}); EXPECT_SIZE(2); EXPECT_HINT("0", "// namespace"); EXPECT_HINT("1", "// namespace ns"); @@ -1017,7 +189,7 @@ TEST_CASE(BlockEnd) { enum class E2 { };§(4) )c", - {.parameters = false, .deduced_types = false, .block_end = true}); + {.parameters = false, .deduced_types = false, .designators = false, .block_end = true}); EXPECT_SIZE(5); EXPECT_HINT("0", "// struct S"); EXPECT_HINT("1", "// class C"); @@ -1055,7 +227,7 @@ TEST_CASE(BlockEnd) { }§(6) } )c", - {.parameters = false, .deduced_types = false, .block_end = true}); + {.parameters = false, .deduced_types = false, .designators = false, .block_end = true}); EXPECT_SIZE(8); EXPECT_HINT("0", "// if cond"); EXPECT_HINT("1", "// if cond"); @@ -1073,7 +245,7 @@ TEST_CASE(BlockEnd) { }§(0) }§(1) )c", - {.parameters = false, .deduced_types = false, .block_end = true}); + {.parameters = false, .deduced_types = false, .designators = false, .block_end = true}); EXPECT_SIZE(2); EXPECT_HINT("0", "// if"); EXPECT_HINT("1", "// foo"); @@ -1101,7 +273,7 @@ TEST_CASE(BlockEnd) { }§(3) } )c", - {.parameters = false, .deduced_types = false, .block_end = true}); + {.parameters = false, .deduced_types = false, .designators = false, .block_end = true}); EXPECT_SIZE(5); EXPECT_HINT("0", "// while true"); EXPECT_HINT("1", "// for true"); @@ -1116,7 +288,7 @@ TEST_CASE(BlockEnd) { }§(0) } )c", - {.parameters = false, .deduced_types = false, .block_end = true}); + {.parameters = false, .deduced_types = false, .designators = false, .block_end = true}); EXPECT_SIZE(2); EXPECT_HINT("0", "// switch I"); @@ -1140,7 +312,7 @@ TEST_CASE(BlockEnd) { }§(4) } )c", - {.parameters = false, .deduced_types = false, .block_end = true}); + {.parameters = false, .deduced_types = false, .designators = false, .block_end = true}); EXPECT_SIZE(6); EXPECT_HINT("0", "// while \"foo\""); EXPECT_HINT("1", "// while \"foo but...\""); @@ -1172,7 +344,7 @@ TEST_CASE(BlockEnd) { }§(3) } )c", - {.parameters = false, .deduced_types = false, .block_end = true}); + {.parameters = false, .deduced_types = false, .designators = false, .block_end = true}); EXPECT_SIZE(7); EXPECT_HINT("0", "// while Var"); EXPECT_HINT("1", "// while func"); @@ -1197,7 +369,7 @@ TEST_CASE(BlockEnd) { }§(2) } )c", - {.parameters = false, .deduced_types = false, .block_end = true}); + {.parameters = false, .deduced_types = false, .designators = false, .block_end = true}); EXPECT_SIZE(5); EXPECT_HINT("0", "// while float"); EXPECT_HINT("1", "// while S"); @@ -1229,7 +401,7 @@ TEST_CASE(BlockEnd) { }§(6) } )c", - {.parameters = false, .deduced_types = false, .block_end = true}); + {.parameters = false, .deduced_types = false, .designators = false, .block_end = true}); EXPECT_SIZE(8); EXPECT_HINT("0", "// while ++I"); EXPECT_HINT("1", "// while I++"); @@ -1267,7 +439,7 @@ TEST_CASE(BlockEnd) { s2; )c", - {.parameters = false, .deduced_types = false, .block_end = true}); + {.parameters = false, .deduced_types = false, .designators = false, .block_end = true}); EXPECT_SIZE(3); EXPECT_HINT("0", "// struct S1"); EXPECT_HINT("1", "// struct S2"); @@ -1291,7 +463,7 @@ TEST_CASE(BlockEnd) { namespace ns { } // namespace ns )c", - {.parameters = false, .deduced_types = false, .block_end = true}); + {.parameters = false, .deduced_types = false, .designators = false, .block_end = true}); EXPECT_SIZE(2); EXPECT_HINT("0", "// struct S1"); EXPECT_HINT("1", "// struct S3"); @@ -1308,7 +480,7 @@ TEST_CASE(BlockEnd) { DECL_STRUCT(S2) RBRACE; )c", - {.parameters = false, .deduced_types = false, .block_end = true}); + {.parameters = false, .deduced_types = false, .designators = false, .block_end = true}); EXPECT_SIZE(1); EXPECT_HINT("0", "// struct S1"); @@ -1321,7 +493,7 @@ TEST_CASE(BlockEnd) { }§(0) } )c", - {.parameters = false, .deduced_types = false, .block_end = true}); + {.parameters = false, .deduced_types = false, .designators = false, .block_end = true}); EXPECT_SIZE(2); EXPECT_HINT("0", "// if"); }; @@ -1370,204 +542,56 @@ TEST_CASE(DefaultArguments) { EXPECT_HINT("2", ", baz: Baz{}"); EXPECT_HINT("3", ", baz: Baz{}"); EXPECT_HINT("4", ", baz: Baz{}"); -}; -// FIXME: flaky on some platforms, skip until root cause is identified. -TEST_CASE(Special, skip = true) { - // Macros + // type_name_limit = 0 means unlimited: a default longer than the + // usual limit still prints in full. run(R"c( - void foo(int param); - #define ExpandsToCall() foo(42) - void bar() { - ExpandsToCall(); - } - )c"); - EXPECT_SIZE(0); - - run(R"c( - #define PI 3.14 - void foo(double param); - void bar() { - foo(§(0)PI); - } - )c"); - EXPECT_SIZE(1); - EXPECT_HINT("0", "param:"); + void configure(int first, const char* name = "this default is longer than the limit"); + void use() { configure(1§(0)); } + )c", + {.default_arguments = true, .type_name_limit = 0}); + EXPECT_SIZE(2); + EXPECT_HINT("0", R"(, name: "this default is longer than the limit")"); + // Default-argument hints survive with parameter hints disabled; the + // parameter name drops out with them. run(R"c( - void abort(); - #define ASSERT(expr) if (!(expr)) abort() - int foo(int param); - void bar() { - ASSERT(foo(§(0)42) == 0); - } - )c"); + void log(int level, bool flush = true); + void use() { log(2§(0)); } + )c", + {.parameters = false, .default_arguments = true}); EXPECT_SIZE(1); - EXPECT_HINT("0", "param:"); - - run(R"c( - void foo(double x, double y); - #define CONSTANTS 3.14, 2.72 - void bar() { - foo(CONSTANTS); - } - )c"); - EXPECT_SIZE(0); - - // Implicit constructor - run(R"c( - struct S { - S(int param); - }; - void bar(S); - S foo() { - // Do not show hint for implicit constructor call in argument. - bar(42); - // Do not show hint for implicit constructor call in return. - return 42; - } - )c"); - EXPECT_SIZE(0); + EXPECT_HINT("0", ", true"); +}; - // Aggregate init +TEST_CASE(EnabledOff) { run(R"c( - struct Point { - int x; - int y; - }; - void bar() { - Point p{41, 42}; + void draw(int width, int height); + void use() { + auto value = 1; + draw(value, 2); } - )c"); + )c", + {.enabled = false}); EXPECT_SIZE(0); +}; - // Builtin functions +TEST_CASE(FreestandingBuiltins) { + // The tester compiles with -ffreestanding, which strips library-builtin + // IDs: std::forward suppression must hold through the name fallback. + // The snap corpus compiles hosted and only reaches the builtin-ID path. run(R"c( namespace std { template T&& forward(T&); } - void foo() { - int i; + void use() { + int i = 0; + // Without the fallback this argument would carry a stray + // `&` reference hint. int&& s = std::forward(i); } )c"); EXPECT_SIZE(0); - - // Pseudo object expression - run(R"c( - struct S { - __declspec(property(get=GetX, put=PutX)) int x[]; - int GetX(int y, int z) { return 42 + y; } - void PutX(int) { } - - // This is a PseudoObjectExpression whose syntactic form is a binary - // operator. - void Work(int y) { x = y; } // Not `x = y: y`. - }; - - int printf(const char *Format, ...); - - int main() { - S s; - __builtin_dump_struct(&s, printf); // Not `Format: __builtin_dump_struct()` - printf(§(0)"Hello, %d", 42); // Normal calls are not affected. - // This builds a PseudoObjectExpr, but here it's useful for showing the - // arguments from the semantic form. - return s.x[ §(1)1 ][ §(2)2 ]; // `x[y: 1][z: 2]` - } - )c"); - EXPECT_SIZE(3); - EXPECT_HINT("0", "Format:"); - EXPECT_HINT("1", "y:"); - EXPECT_HINT("2", "z:"); }; -TEST_CASE(VariadicTemplate) { - // Arg packs and constructors - run(R"c( - struct Foo{ Foo(); Foo(int x); }; - - void foo(Foo a, int b); - - template - void bar(Args... args) { - foo(args...); - } - - template - void baz(Args... args) { foo(§(0)Foo{args...}, §(1)1); } - - template - void bax(Args... args) { foo(§(2){args...}, args...); } - - void foo() { - bar(§(3)Foo{}, §(4)42); - bar(§(5)42, §(6)42); - baz(§(7)42); - bax(§(8)42); - } - )c"); - /// FIXME: - /// EXPECT_HINT("0", "a:"); - /// EXPECT_HINT("1", "b:"); - /// EXPECT_HINT("2", "a:"); - EXPECT_HINT("3", "a:"); - EXPECT_HINT("4", "b:"); - EXPECT_HINT("5", "a:"); - EXPECT_HINT("6", "b:"); - EXPECT_HINT("7", "x:"); - EXPECT_HINT("8", "b:"); - - // Doesn't expand all args - run(R"c( - void foo(int x, int y); - int id(int a, int b, int c); - template - void bar(Args... args) { - foo(id(§(0)args, §(1)1, §(2)args)...); - } - void foo() { - bar(1, 2); // FIXME: We could have `bar(a: 1, a: 2)` here. - } - )c"); - /// FIXME: - /// EXPECT_SIZE(3); - /// EXPECT_HINT("0", "a:"); - /// EXPECT_HINT("1", "b:"); - /// EXPECT_HINT("2", "c:"); -} - -TEST_CASE(Dependent, skip = true) { - // Dependent calls - run(R"c( - template - void nonmember(T par1); - - template - struct A { - void member(T par2); - static void static_member(T par3); - }; - - void overload(int anInt); - void overload(double aDouble); - - template - struct S { - void bar(A a, T t) { - nonmember(§(0)t); - a.member(§(1)t); - A::static_member(§(2)t); - // We don't want to arbitrarily pick between - // "anInt" or "aDouble", so just show no hint. - overload(T{}); - } - }; - )c"); - EXPECT_SIZE(3); - EXPECT_HINT("0", "par1:"); - EXPECT_HINT("1", "par2:"); - EXPECT_HINT("2", "par3:"); -} - }; // TEST_SUITE(inlay_hint) } // namespace diff --git a/tests/unit/server/config_tests.cpp b/tests/unit/server/config_tests.cpp index 14e000a601..f7bf05a2fc 100644 --- a/tests/unit/server/config_tests.cpp +++ b/tests/unit/server/config_tests.cpp @@ -71,6 +71,20 @@ append = ["-std=c++20"] EXPECT_EQ(result->rules[0].patterns[0], "**/*.cpp"); } +TEST_CASE(ParseInlayHints) { + auto result = kota::codec::toml::parse(R"( +[inlay_hints] +block_end = true +parameters = false +type_name_limit = 64 +)"); + EXPECT_TRUE(result.has_value()); + EXPECT_EQ(*result->inlay_hints.block_end, true); + EXPECT_EQ(*result->inlay_hints.parameters, false); + EXPECT_EQ(*result->inlay_hints.type_name_limit, 64u); + EXPECT_FALSE(result->inlay_hints.designators.has_value()); +} + TEST_CASE(ParseEmptyConfig) { auto result = kota::codec::toml::parse(""); EXPECT_TRUE(result.has_value()); @@ -151,6 +165,13 @@ TEST_CASE(ApplyDefaults) { EXPECT_GE(config.project.stateless_worker_count.value, 2u); EXPECT_FALSE(config.project.cache_dir.empty()); EXPECT_FALSE(config.project.logging_dir.empty()); + EXPECT_EQ(*config.inlay_hints.enabled, true); + EXPECT_EQ(*config.inlay_hints.parameters, true); + EXPECT_EQ(*config.inlay_hints.deduced_types, true); + EXPECT_EQ(*config.inlay_hints.designators, true); + EXPECT_EQ(*config.inlay_hints.block_end, false); + EXPECT_EQ(*config.inlay_hints.default_arguments, false); + EXPECT_EQ(*config.inlay_hints.type_name_limit, 32u); } TEST_CASE(ApplyDefaultsEmptyWorkspace) { @@ -164,9 +185,13 @@ TEST_CASE(ApplyDefaultsPreserveSet) { Config config; config.project.cache_dir = "/custom"; config.project.enable_indexing = false; + config.inlay_hints.parameters = false; + config.inlay_hints.block_end = true; config.apply_defaults("/workspace"); EXPECT_EQ(std::string_view(config.project.cache_dir), "/custom"); EXPECT_EQ(*config.project.enable_indexing, false); + EXPECT_EQ(*config.inlay_hints.parameters, false); + EXPECT_EQ(*config.inlay_hints.block_end, true); } TEST_CASE(LoadFromJson) { diff --git a/tools/feature_docs.ts b/tools/feature_docs.ts index 01e7996e95..984de75256 100644 --- a/tools/feature_docs.ts +++ b/tools/feature_docs.ts @@ -50,6 +50,7 @@ import { parseAnnotations } from "./snap/annotation.ts"; // adopt fixture-generated docs. const FEATURES: Record = { folding_range: "docs/en/features/folding-ranges.md", + inlay_hint: "docs/en/features/inlay-hints.md", semantic_tokens: "docs/en/features/semantic-tokens.md", }; diff --git a/tools/snap/standalone.ts b/tools/snap/standalone.ts index b9fb10d66b..4e3c0f37e4 100644 --- a/tools/snap/standalone.ts +++ b/tools/snap/standalone.ts @@ -44,6 +44,9 @@ import { SnapshotContext } from "./snapshot.ts"; const CORPUS_FLAGS: Record string[]> = { document_links: (corpus) => [`-I${posix(corpus)}`, "-std=c++23"], hover: () => ["-std=c++20", "--target=x86_64-unknown-linux-gnu"], + // c++23 for deducing-this fixtures; ms-extensions for __declspec + // property pseudo-object fixtures. + inlay_hint: () => ["-std=c++23", "-fms-extensions"], // inc/ backs angled-include fixtures; sys/ simulates a system header so // the DefaultLibrary modifier pins deterministically on every host. semantic_tokens: (corpus) => [