Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent clang-format from adding newlines between stream operator calls <<

We are currently in the process of formatting our code base with clang-format. We found a situation where for some reason the stream operator to std::cout is moved to the next line if two consecutive strings literals are present. Putting a variable in between thw two string literals causes clang-format to not change the format. What needs to be changed in the .clang-format file to avoid this?

int main()
{
    std::cout << "something" << "something" << std::endl;
}

becomes

int main()
{
    std::cout << "something"
              << "something" << std::endl;
}

while

int main()
{
    int a = 0;
    std::cout << "something" << a << "something" << std::endl;
}

stays untouched. Note while this last snippet is wider, it is not split across multiple lines, while the shorter snippet above is.

This is with LLVM 9.0.0 Windows installer and is reproducible with the default config file.

like image 608
JefGli Avatar asked Mar 03 '23 19:03

JefGli


1 Answers

This behavior can't be altered via the .clang-format file, as it is part of the code.

This behavior was introduced in this commit: https://github.com/llvm-mirror/clang/commit/df28f7b8dd6a032515109de4ff5b4067be95da8e

Link to bug report: https://bugs.llvm.org/show_bug.cgi?id=45018

like image 156
JefGli Avatar answered Mar 05 '23 16:03

JefGli