Search code examples
c++code-formattingclang-format

Clang Formatter multiline formatting config error


I'm having a problem with how vscode formats my code

The formatting i want:

std::cout << std::endl << "Something";

The formatting i get:

std::cout << std::endl
          << "Something";

My current config:

"C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: Google, IndentWidth: 4, ColumnLimit: 0}"


Solution

  • This seems to be an interaction between ColumnLimit: 0 and the << operator. I verified this with clang-format 6.0.0. Using the configurator, I can verify this defect first appeared at version 3.6.0, and is there through 10.0.0, so this has been there awhile.

    Interestingly, even >> works correctly, it's just the << operator which does this.

    I don't see any good workarounds - you're left with:

    • Live with it.
    • Set the ColumnLimit to something else.
    • Uglify your code with operator<< instead of just <<. For example, you'd use something like std::cout.operator<<(std::endl).operator<<("Something").