llvm / llvm/llvm-project

Parameter pack decl printing oddity

Open
#211,737 9 comments 0 reactions 0 assignees View on GitHub
clang:frontend confirmed
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

In IWYU's logging, we've stumbled on a weird formatting when traversing and printing the AST of `std::vector`:

```
[ CXXMethodDecl ] template<> reference emplace_back< &>>(std::basic_string &__args)
```

The double angle-brackets in `emplace_back<<...>>(...)` raised our eyebrows.

This is reproducible with clang itself using:

`# cat t.cc`

```c++
template struct Struct {
template
void pack_method(Args&&...);
};

void specializeStructAndMethod() {
Struct v;
v.pack_method(short(10));
}

# clang -fsyntax-only -Xclang -ast-print t.cc
template struct Struct {
template void pack_method(Args &&...);
};
template<> struct Struct {
template void pack_method(Args &&...);
template<> void pack_method<>(short &&); // oopsie: <>
};
void specializeStructAndMethod() {
Struct v;
v.pack_method(short(10));
}
```

The main reason I'm opening this ticket is because I'm not sure if this is intentional or not.

`DeclPrinter` adds the outer `<>` pair to delineate the template arguments, and then `TemplateArgument::print` encloses packs in an additional `<>` pair:

* https://github.com/llvm/llvm-project/blob/906f520af01a4c788935f24caea00237b42624e2/clang/lib/AST/DeclPrinter.cpp#L729
* https://github.com/llvm/llvm-project/blob/906f520af01a4c788935f24caea00237b42624e2/clang/lib/AST/DeclPrinter.cpp#L1228
* https://github.com/llvm/llvm-project/blob/906f520af01a4c788935f24caea00237b42624e2/clang/lib/AST/TemplateBase.cpp#L606

Would it make sense to drop the inner brackets from `TemplateArgument::print` (figuring out how to make it behave consistently across all call-sites, of course)? Or is it valuable to show the nesting of parameter packs?

It's not a breaking behavior for us in any way, it just looks confusing in our logs, so I thought I'd ask.

Contributor guide

Open the contributing guide

Research direction

Run the cited clang -fsyntax-only -Xclang -ast-print reproducer in t.cc, then read the referenced sections of clang/lib/AST/DeclPrinter.cpp and clang/lib/AST/TemplateBase.cpp. Compare the relevant call sites to determine whether the nested angle brackets are intentional; done means reaching and implementing or documenting a maintainer-backed decision.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
43/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.