-
Notifications
You must be signed in to change notification settings - Fork 13.5k
clang-format: Disabling Formatting on a one line #54334
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
Comments
@llvm/issue-subscribers-clang-format |
I've seen this requested before, do you have a suggestion? |
We were thinking about it. What about E.g.:
instead of
|
Hi, Is there any update on this? |
Would also love to see this and the syntax proposed by @VasaMM seems quite tidy |
Another option similar to other tools (clang tidy) is:
|
Let me also vote for this extension. Both are really nice to have:
I would love to see some shorter magic phrase or maybe allow configuring some acronyms ( |
anything working on? |
Should this be marked as a good first issue? |
If no one else is working on this, can I pick it up? |
Sorry I haven't said anything in 3 weeks. I don't think I can work on this very soon. |
I've opened a draft PR adding support for two options: |
What are the use cases where you would use this? what is the reason its needed? |
@mydeveloperday one common use case I see is disabling formatting (column limit) for logs. The codebase I currently work on has lots of simple (5-15 lines) functions that are bloated with outputting logs. For example, int foo(Resources *resources, int i, int j, int k) {
if (i < 0 && j < 0) {
myproject::detail::LogErrorPrintf(
resources->logger,
"both i and j can not be negative at the same time.\ni = %d, j = %d\n",
i, j);
return -1;
}
if (i < 0) {
j *= 10;
}
if (j < 0) {
k += 5;
}
return i + j * k;
} int foo(Resources *resources, int i, int j, int k) {
if (i < 0 && j < 0) {
// clang-format off-next-line
myproject::detail::LogErrorPrintf( resources->logger, "both i and j can not be negative at the same time.\ni = %d, j = %d\n", i, j);
return -1;
}
if (i < 0) {
j *= 10;
}
if (j < 0) {
k += 5;
}
return i + j * k;
} IMO, the second case is more readable that the first one. |
It's very helpful to bypass missing features or bugs in clang-format. E.g. I missed this feature when there were no support for C++ concepts. It just did something odd with our style configuration. Also in C when you need a compile-time constant it could be useful to locally define anonymous enumeration: This is definitely not complete. Just a couple of examples from the top of my head. |
@mydeveloperday @owenca @HazardyKnusperkeks Would closing the issue with a |
I think there is some discussion to do. |
I've closed the PR due to concerns about the usefulness of this feature. If there are beneficial use cases for supporting these new options, please feel free to share them here to highlight their advantages. |
We have a use case: We run a separate scanner tool over our source code that extracts string literals for translation into different languages (e.g. Italian, German, etc.). Some string literals do not need any translation because they are not part of any message a user sees. We need to mark lines containing such string literals with some special trailing comment: std::string const msg = fmt::sprintf("Long string with %s placeholders and %d more.", someStr, someInt); // NO_TRANSLATION clang-format often ends up breaking the additional arguments and the comment to the next lines, e.g.: std::string const msg = fmt::sprintf(
"Long string with %s placeholders and %d more.",
someStr, someInt); // NO_TRANSLATION This then causes the string literal to end up in the translation database because the scanner tool incorrectly extracts the literal. This is one of the major blockers that prevents us from formatting documents on save in the IDE, or running clang-format over the whole code basis. Manually fixing the position of If there were a clang-format option to tell clang-format to leave lines alone that contain certain strings/regexes, it would solve the issue for us. So it would be great if the suggested I have a little bit of familiarity with the clang-format code base and would be willing to invest some work to implement the feature or add to the existing pull request #118566. Notes:
|
Another use-case (actually we regularly face with different, they are all relatively minor but very annoying). In C before C23 the best way to define a compile-time integral constant is enum:
Meanwhile the general code-style prescribes to put enumerators on its own line and more than this, use "non-Egypt" braces like:
Obviously it inflates the code and makes it less readable. |
@Sedeniono Thanks for sharing the additional use cases.
The closed PR can be revisited and used as a starting point for this feature. However, it seems that the main concern is whether this feature is necessary, given the existing |
@a-tarasyuk Yes, I hope that my use case convinces the others that the possibility to disable the formatting via a single-line comment is a good idea. And also, to make the recognized "markers" configurable. |
Have you tried one of the following? std::string const msg = fmt::sprintf( // NO_TRANSLATION
"Long string with %s placeholders and %d more.", someStr, someInt);
std::string const msg = /* NO_TRANSLATION */ fmt::sprintf(
"Long string with %s placeholders and %d more.", someStr, someInt);
std::string const msg = // NO_TRANSLATION
fmt::sprintf("Long string with %s placeholders and %d more.", someStr,
someInt);
See #118566 (comment), #118566 (comment), and #118566 (comment). |
@owenca Our external phrases scanner tool requires the We could write a script that rewrites the several million lines of C++/C# code once to put all I do understand the hesitance to add the proposed functionality. It just would have been a simple way for us to really ensure that the introduction of automatic formatting doesn't mess up our translation database, neither for existing nor future code. But there are certainly alternatives as outlined above. |
I've come up with the following for maximum flexibility and minimum interference with
|
Sample output:
|
@owenca That sounds great! 👍 |
Often I need disable formatting only on one line. In this case it is annoying write
// clang-format off
above line and// clang-format on
below line. And there is room for error also. It would be nice, if it will be exist some inline command for disabling.The text was updated successfully, but these errors were encountered: