Questions tagged [clang-format]
A code formatter for C, C++, Java, JavaScript, Objective-C and Protobuf. Use this tag with questions about setting up and configuring clang-format.
825
questions
-1
votes
0
answers
74
views
Can clang-format align "using enum" C++20 statement inside switch expression?
Assuming the following snippet:
switch (val) {
case SomeEnum::SomeValue:
doSomething(val);
case SomeEnum::SomeOtherValue:
doSomethingElse(val);
default:
break;
}
in C++20 we can instead ...
0
votes
0
answers
40
views
How can I stop clang-format breaking a function call after the parenthesis?
I have this function:
return dispatch<ostream &>(date,
[&](Date d) -> ostream & { return out << d.year << "-" << d.month &...
1
vote
1
answer
42
views
How to make clang format not place comments?
Every time I create a namespace or start a namespace block, clang-format adds a comment at the namespace closing. I don't want this. How do I disable it?
Example:
} // namespace geometry
My ...
0
votes
0
answers
20
views
How to make every parameter on the next line with AlignAfterOpenBracket=BlockIndent?
I have the following clang-format configuration
---
Language: Cpp
AlignAfterOpenBracket: BlockIndent
BreakBeforeBraces: Allman
ColumnLimit: 80
IndentWidth: 4
And my code is formatted like this:...
0
votes
0
answers
24
views
clang-format formats documentation comment
How can I tell clang-format to ignore documentation comments as follows
/***************************************************************************//**
* @brief
* ...
***************************...
1
vote
0
answers
33
views
clang-format style file path format is not working
I'm using clang-format 15. According tot he documentation it is possible to specify custom style file with the --style option using --style='file:<format_file_path>.
However, I'm having problem ...
0
votes
1
answer
51
views
How to set-up clang format to keep return type and protoype in the same line
I am discovering clang-format. I set ColumnLimit to 120. Generaly speaking this works well, meaning set parameters of function on different lines.
void myLongFunctionNameMoreThan120Col(int foo,
...
0
votes
0
answers
42
views
clang-format works but git clang-format returns errors
I have created a custom style file for clang-format and am able to run the following command to apply the stylistic coding changes:
$ clang-format file_name.c --style=file
I am now trying to use git ...
0
votes
0
answers
10
views
CLANG compiler generates an error stating that "the clang compiler does not support 'armv4t-none-unknown-eabi'"
I'm trying to compile using Clang compiler of version V8.1.0. I got a compilation error stating that "the clang compiler does not support 'armv4t-none-unknown-eabi'".
1- I need to know if ...
0
votes
1
answer
20
views
How to use ContinuationWidth from the start of the conditional, not start of function call in clang-format?
Here the problematic code snippet:
int open_server_socket(Config config) {
if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &optVal,
sizeof(optVal)) < 0) { // THIS LINE
...
-1
votes
1
answer
25
views
stopping clang-format to modify the CMake substitution strings?
I have different projects using a CMake template to propagate some info from the CMakeList.txt into the .cpp/.h files.
CMake use replacement strings sandwiched between @.
Example (in MyAppConfig.in.h):...
0
votes
0
answers
44
views
clang-format removes markdown-style linebreaks in doxygen comments
Doxygen allow to use markdown-style formatting which make in-code documentation much better readable compared to HTML-style.
A markdown line-break consist of 2 or more spaces at the end of a line:
/**
...
0
votes
0
answers
36
views
ClangFormat weird behavior
I am using the following setting:
"C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 2, TabWidth: 2, BreakBeforeBraces: Attach, ...
0
votes
1
answer
104
views
clang-format: How to format an 'Array of Structures' initialization
For a retro C project I have among other things a ColumnLimit: 72 set, and all braces should be on a new line: BreakBeforeBraces: Allman. Additionally I would like to achieve that initialization of '...
2
votes
0
answers
60
views
properly indent braces with clang-format [closed]
I'm trying to make clang-format accept the following code indentation:
while (condition)
{
}
So, I literally tried with one-line in my .clang-format file:
BreakBeforeBraces: Allman
But I'm always ...
0
votes
0
answers
61
views
New line after the :: for member function definitions in C++
I use clang-format and I want my member function definitions to be formatted in a new line like this:
void MyClass::
configure(const std::string& p) {
// code
}
With my current clang format ...
1
vote
0
answers
56
views
Is it possible to align initializer lists with clang-format?
I recently set up clang-format so that I can easily format my c++ code in a simple and consistent manner. However, I usually like to leave a space between the identifier and the initializer list, but ...
0
votes
2
answers
229
views
clang-format is not formatting C header files (.h) according to its config file
I am running x86_64 GNU/Linux machine and using clang-format in neovim to format my C code. Additionally I am using my own .clang-format config file that is mostly based on style from Mozilla.
Changes ...
2
votes
1
answer
159
views
How to allow 2-line if statements using clang-format
I am working on setting up a style format check for a large library. Currently, our team styleguide prefers that instead of one line if statements, we use a "2 line" statement, which looks ...
1
vote
1
answer
62
views
How to set up clang format to not trim empty lines at the end of C++ files?
I'm using VS Code for C++ and clang format for formatting. I like to leave several empty lines at the end of every code file, but clang format trims them. My .clang-format file is:
BasedOnStyle: LLVM
...
0
votes
0
answers
32
views
How to generate sequence diagram from cpp code in windows?
I know that clang-uml can generate cpp code to sequence diagram. But I am unable to use it. I have installed it but some configuration issue occurred.
I want to generate sequence code from cpp code.
...
0
votes
1
answer
137
views
Python not found when trying to execute git-clang-format on Windows
I am trying to set up git-clang-format on Windows 10. I have following programs installed:
git
python 3.11.5
LLVM 16.0.4 (which includes clang-format and git-clang-format)
Both python executable and ...
0
votes
0
answers
61
views
Pipe-chain in clang format
At the moment I have some code like so:
const auto results =
some_container | ranges::views::transform(function1)
| ranges::views::filter(function2) | ranges::views::filter(function3)
| ...
-1
votes
1
answer
44
views
Which LLVM release will contain the specific issue closed on GitHub?
For instance, there is a recently closed issue on LLVM GitHub repository related to clang-format. Question about the issue was posted on Stack Overflow as well: Leave short lambda used as a middle ...
0
votes
0
answers
13
views
Filter clang style options in .clang-format file by language
Is any way to leave in a language block only the options that affects the language ?
e.g the options about namespaces are not applied for Json files
2
votes
0
answers
58
views
How to make clang-format not remove a space between override and empty curly braces?
clang-format keeps formatting this code:
class Child : public Parent {
public:
void firstMethod() override { someFunction(*this); }
void secondMethod() override {};
};
Into this:
class Child ...
1
vote
0
answers
40
views
clang-format: indentation of preprocessor continuation line
Clang-format v14 formats the following line like this:
#pragma omp parallel for firstprivate(signal_rates, reference_rates) private( \
exposure_times, weights, jacobian)
But Clang-format v16 ...
0
votes
1
answer
53
views
git clang-format removes unstaged changes
I am writing a pre-commit hook for a repo where if some of the staged files are not formatted the hook will format the files and gives a message. Here is the content of the hook
#!/bin/bash
PYTHON=&...
0
votes
1
answer
109
views
How to add custom separator between functions using clang format?
I have a C and C++ code base which I format using clang format. I'd like to make my code easy to read by placing separators between functions in the following way:
//-----------------------------------...
0
votes
0
answers
13
views
How can I allow extra line break when the line is not too long?
I have an if condition of the following form:
if ((foo == cond1)
&& (bar == cond2))
{
// Code
}
Without the break inside the condition, the line is not too long for clang-format. Thus it ...
2
votes
1
answer
92
views
pointer alignment right for variables, and left for functions
If I use this option in clang-format configuration:
PointerAlignment: Right
the star would be attached to variable's name. For example:
char *p;
If I try this option on a function, it will produce ...
1
vote
0
answers
53
views
Clang format stop operators aligning on wrap
clang-format is formatting like this:
std::ostream& operator<<( std::ostream& os, const Camera& c ) {
return os << c.latitude << ", " << c.longitude &...
2
votes
1
answer
55
views
Why are the results of `clang-format` and `git clang-format` different
this is my main.cpp:
#include "mainwindow.h"
#include <QApplication>
struct test {
int a;
int b;
QString c;
};
struct test demo[] = {{56, 23, "hello"}, {-1, 93463, &...
0
votes
0
answers
44
views
Preserving macro line continuations ('\') with clang-format
I have code that uses multi-line Macros with the continuation backslash.
Before running clang-format it looks like...
#include "uart0.h"
#if \
defined(__AVR_ATmega8__) || \
...
1
vote
1
answer
70
views
can clang-format sort "using namespace <...> " alphabetically?
Say, I have a bunch of .cc source files with statements like
using namespace lib::a;
using namespace lib::d;
using namespace lib::c;
using namespace lib::b;
I want them to be sorted
using namespace ...
0
votes
0
answers
112
views
How do I force a job to be a success in my GitHub Actions even if the steps fail?
I'm trying to make it such that when someone pushes to my repo, the code-formatter is run on files changed in the push. To do so, I use git diff in my GitHub Workflow .yml file.
It looks like this (...
0
votes
0
answers
72
views
Make clang-format break after __attribute__
I have a function that want to be formatted like this:
__attribute__((naked, noreturn))
void _reset(void) {
...
}
But clang-format does this:
__attribute__((naked, noreturn)) void _reset(void) {
...
-2
votes
1
answer
35
views
Declearing a string array in header
The code works:
#include <stdio.h>
char *arr[1] = {"Hello"};
int main()
{
printf("%s", arr[0]);
return 0;
}
However, the code does not work:
#include <stdio.h>...
0
votes
1
answer
63
views
How to make clang-format not break after = signs and not BinPackArguments
I'm using clang-format with a small configuration file, based on the Microsoft style.
---
BasedOnStyle: Microsoft
BinPackParameters: 'false'
BinPackArguments: 'false'
ColumnLimit: '120'
#Other options....
2
votes
0
answers
59
views
Clang-format BlockIndent option but for braces
I was trying to configure my .clang-format file to satisfy my needs, and came pretty close. However, I haven't figure out how to deal with formatting like this:
std::vector<std::string> {
&...
0
votes
1
answer
38
views
.clang-format file for editing of c++ code: is clang version relevant?
I have a .clang-format file in my source directory for c++ code editing.
The file has entries like
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: true
...
0
votes
1
answer
170
views
How to tell clang-format to put a line break after [[likely]] & [[unlikely]]
I use clang-format to format my code in vim. I'm mostly happy with the configuration I came up with, however I can't find a setting for this particular issue. When having a [[likely]] or [[unlikely]] ...
5
votes
1
answer
135
views
clang-format: don't break between string and << operator
I have the following line in my C++ code:
std::cout << "done" << "\n";
which clang-format breaks into
std::cout << "done"
<< "\n&...
0
votes
0
answers
16
views
CLANG version 15, indentation issue
I have enabled IndentPPDirectives to BeforeHash, and I am getting the correct expected behavior in which the following:
#if FOO
#if BAR
#include <foo>
#endif
#endif
However, the case is ...
0
votes
0
answers
54
views
Clang-Format struggeling with `std::visit` and templated struct
Consider the following clang-format file:
Language: Cpp
# BasedOnStyle: Google
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: None
...
1
vote
1
answer
105
views
Clang-format: Always wrap function signature (C/C++)
At work, we have a specific convention on how to format function signatures.
In summary:
Always wrapped
Opening argument bracket on a new line
One function argument per line
Closing argument bracket ...
0
votes
0
answers
89
views
How to force clang-format to put array initialization on one line within a class instantiation?
I can't manage to make clang-format to put a array initialisation withing a class instantiation on a single line.
I have this basic code :
class Foo
{
Foo(const std::vector<int>& a, ...
0
votes
0
answers
93
views
.clang-format BlockIndent doesn't work with curly brackets
I'm currently setting up a .clang-format file for our code repository. However, I'm a stuck with getting AlignAfterOpenBracket: BlockIndent to work with constructor calls using curly brackets (...
0
votes
0
answers
48
views
Setting the clang auto formatter on VS code to have different settings for .c and .h files
My company currently uses VS Code for programming in C, and most people in the company use the embedded VS Code auto formatter. Currently this is set using the C_Cpp.clang_format_fallbackStyle setting ...
0
votes
0
answers
16
views
clang-format defaults to not format if no style file found
I am working with some repos where no .clang-format style file is provided, in this caseI have to disable auto-formatting in vscode to avoidclang-format` nuking the entire file.
Is it possible to have ...