1

I'm using clang-format and I'm trying to only set e.g. 120 column limit but no matter what I try clang applies many rules besides that. If I specify all the rules and set their values then it does what I want but I, e.g., only want to set a line wrap rule. Can clang-format do that?

Thanks

Here's my .clang-format file which is in a parent dir of the source file:

---
Language:        Cpp
ColumnLimit:     120
...

Note that I don't have a BasedOnStyle field so I'd expect clang won't use defaults from style.

Here's how I run:

clang-format -style=file -i src/foo.cpp

Here's foo.cpp

int main(int argc, char const *argv[]) {

    int rc = -1;

    // This if clause should be wrapped at 120 by clang which it does
    if (argc == 1 && && argc != 2 && argc != 3 && argc != 4 && argc != 5 && argc != 6 && argc != 7 && argc != 8 && argc != 9 && argc != 10) {
        rc = 0;
    }

    // This if clause shouldn't be touched by clang. but it is.
    if(args > 1)
    {
        rc = 1;
    }

    return rc;
}

Expected result: Only lines longer than 120 chars are wrapped.

int main(int argc, char const *argv[]) {

    int rc = -1;

    // This if clause should be wrapped at 120 by clang which it does
    if (argc == 1 && && argc != 2 && argc != 3 && argc != 4 && argc != 5 && argc != 6 && argc != 7 && argc != 8 &&
        argc != 9 && argc != 10) {
        rc = 0;
    }

    // This if clause shouldn't be touched by clang. but it is.
    if(args > 1)
    {
        rc = 1;
    }

    return rc;
}

Actual Result: Many rules are applied and every line is changed. In this case e.g. indentation becomes 2 spaces. I a real world example many rules are applied to files with more code.

int main(int argc, char const *argv[]) {

  int rc = -1;

  // This if clause should be wrapped at 120 by clang which it does
  if (argc == 1 && &&argc != 2 && argc != 3 && argc != 4 && argc != 5 && argc != 6 && argc != 7 && argc != 8 &&
      argc != 9 && argc != 10) {
    rc = 0;
  }

  // This if clause shouldn't be touched by clang. but it is.
  if (args > 1) {
    rc = 1;
  }

  return rc;
}
  • Does this answer your question? [How can I apply only one clang-format action?](https://stackoverflow.com/questions/30763869/how-can-i-apply-only-one-clang-format-action) – CleanCoder265 Nov 23 '22 at 13:38

1 Answers1

0

This question is a duplicate of How can I apply only one clang-format action?

The accepted answer to that question is still correct/valid, hence what you are asking is not possible with the latest version of clang-format.

CleanCoder265
  • 574
  • 1
  • 5
  • 22