Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang-format] Wrong formatting when template variable is last thing before closing parenthesis #120148

Closed
vient opened this issue Dec 16, 2024 · 3 comments · Fixed by #121318
Closed
Assignees
Labels
clang-format confirmed Verified by a second party

Comments

@vient
Copy link
Member

vient commented Dec 16, 2024

auto t1 = a<int> + b;
auto t2 = (b) + b;
auto t3 = (a<int>) + b;
auto t4 = (a<int> + 1) + b;

is formatted as

auto t1 = a<int> + b;
auto t2 = (b) + b;
auto t3 = (a<int>)+b;  // wrong, no spaces around +
auto t4 = (a<int> + 1) + b;

https://godbolt.org/z/9K11MrPz9 clang-format version 20.0.0git (https://github.com/llvm/llvm-project.git 8daf4f16fa08b5d876e98108721dd1743a360326)

@vient vient changed the title [clang-format] Wrong formatting when template variable in last thing before closing parenthesis [clang-format] Wrong formatting when template variable is last thing before closing parenthesis Dec 16, 2024
@owenca owenca added the confirmed Verified by a second party label Dec 17, 2024
@dty2
Copy link
Contributor

dty2 commented Dec 18, 2024

Hi, I guess the problem you mentioned comes from this part of the code. The below code in llvm-project/clang/lib/Format/TokenAnnotator.cpp. To verify this, you can add " SpaceAfterCStyleCast : true
" to your .clang-format file. Then you will find the "auto r2 = (c)+b;" will change to "auto r2 = (c) +b;"
And I will try to fix it. (:

  // If the next token is a binary operator or a selector name, we have
  // incorrectly classified the parenthesis as a cast. FIXME: Detect correctly.
  if (Left.is(TT_CastRParen)) {
    return Style.SpaceAfterCStyleCast ||
           Right.isOneOf(TT_BinaryOperator, TT_SelectorName);
  }

@leijurv
Copy link

leijurv commented Dec 18, 2024

Probably the issue is earlier. Why is this being treated as a cast? Given that the next token is a TT_BinaryOperator, the code says "fixme: detect correctly".

@owenca
Copy link
Contributor

owenca commented Dec 30, 2024

We probably need to add a new option, e.g. VariableTemplates:

$ echo 'auto t3 = (a<int>) + b;' | clang-format -style='{VariableTemplates: [a]}'
auto t3 = (a<int>) + b;

@owenca owenca self-assigned this Dec 30, 2024
owenca added a commit to owenca/llvm-project that referenced this issue Dec 30, 2024
owenca added a commit to owenca/llvm-project that referenced this issue Dec 31, 2024
owenca added a commit to owenca/llvm-project that referenced this issue Dec 31, 2024
owenca added a commit that referenced this issue Jan 2, 2025
meltq pushed a commit to meltq/llvm-project that referenced this issue Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang-format confirmed Verified by a second party
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants