Skip to content

A newline is added after a string token if the next 2 tokens are lessless and string #44363

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

Closed
JvdGlind opened this issue Feb 25, 2020 · 4 comments · Fixed by #69871 or #92214
Closed
Assignees
Labels
bugzilla Issues migrated from bugzilla clang-format

Comments

@JvdGlind
Copy link

Bugzilla Link 45018
Version trunk
OS Windows NT
Attachments File to verify two streamed strings aren't split

Extended Description

Given the code:

QStringList() << "something" << "something" << variable;

If I then run clang-format with the default config file, the output is the following code:

QStringList() << "something"
<< "something" << variable

What I expected, was that it stays on a single line, unless it exceeds the column limit and possible penalties. In this case, untouched.

It stays untouched when I add a variable in between:

QStringList() << "something" << a << "something" << variable;

Looking at the code, I noticed that somebody intentionally introduced this to split endline characters in commit df28f7b8dd6a032515109de4ff5b4067be95da8e (currently at lib/Format/TokenAnnotator.cpp, line 3073).

In the attachment I added a file with a slightly different example that I used when debugging clang-format.

@llvmbot
Copy link
Member

llvmbot commented Mar 21, 2021

This is a really annoying issue, especially because the results seem pretty random: If you use QStringLiteral() the strings are not wrapped.

One workaround for the usecase you described would be to use initializer lists, which is formatted really well and looks even better IMHO.

But in QTest::newRow() calls one can not use initializer lists and using QStringLiteral and such would add unwanted noise. Especially with the many usages in the KDE codebase this becomes a problem.

For example:
QTest::newRow("test") << "" << "" << "" << "" << "" << 1 << 1 << 1 << 1 << 1;

would be formatted to

QTest::newRow("test") << ""
<< ""
<< ""
<< ""
<< "" << 1 << 1 << 1 << 1 << 1;

I would appreciate if the developers could look into this :)

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 10, 2021
valassi added a commit to valassi/madgraph4gpu that referenced this issue Feb 24, 2022
Note that 'std::cout << "parameter " << "=" <<' gets split onto two lines...
I would need to change this as 'std::cout << "parameter =" <<'
This is a bug, see llvm/llvm-project/issues/44363
@dan-42
Copy link

dan-42 commented Sep 7, 2022

Using LLVM 15.0.0 on Windows we have a similar issue.
We use the operator<< for collecting sigslot_connection when calling connect(sigslot_slot) in our sigslot library
code looks like this:

connection_store_ 
  << some_module.signals().scanning_started.connect(
     [this]()
     { 
        /*business logic*/
     })
  << some_module.signals().scanning_finished.connect(
    [this](...)
    {
      /*business logic*/
    }
    });

Clang-format does this:

connection_store_ << some_module.signals().on_started.connect(
                         [this]()
                         { 
                            /*business logic*/
                         })

                    << some_module.signals().on_finished.connect(
                         [this](...)
                         {
                            /*business logic*/
                         }
                          });

I suggest having an option allowing specific behaviour, something like

BreakAfterStreamOperator: {Leave, Never, Always, FitLine, ...}

Anyone else having issues in general with the stream operators?
Is there any better suggestion?

@mohad12211
Copy link

same issue here, I agree with the above suggestion.

@owenca
Copy link
Contributor

owenca commented Oct 22, 2023

@dan-42, it seems that clang-format 17 formats your example alright although the indent of the comments are wrong, which you can open another issue for.

s1Sharp pushed a commit to s1Sharp/llvm-project that referenced this issue Oct 23, 2023
the problem occurred while checking for the correctness of the break after binary operators.
The output statement 'tok::lessless' is then break line every possible time,
which is not expected with the BreakBeforeBinaryOperators: None

Fixes llvm#59797
Fixes llvm#44363
owenca added a commit to owenca/llvm-project that referenced this issue Oct 24, 2023
@owenca owenca reopened this Apr 18, 2024
owenca added a commit to owenca/llvm-project that referenced this issue May 15, 2024
owenca added a commit that referenced this issue May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment