Questions tagged [clang-format]
clang-format is a tool to format C/C++/Java/JavaScript/Objective-C/Protobuf code.
667
questions
2
votes
1
answer
44
views
clang-format causing compiled object files to change
I'm working on a large codebase that needs to be formatted. With many hiccups, it's been formatted with a clean start to finish run of clang-format, and I've moved on to comparing the object files ...
1
vote
0
answers
36
views
Getting clang-format to format requires-expression on one line
Here is my code:
if constexpr (requires { { N::uuid() } -> std::convertible_to<score::uuid_t>; })
clang-format (14, 15) insists in formatting it in 5 lines:
if constexpr (requires {
...
0
votes
0
answers
7
views
Clang-format doesn't join function to return type if type is ALL_CAPS
I'm noticing a strange behavior where clang-format will join a function name with its return type when the type is lower case, but not when the return type is all upper case.
Is there a way to get ...
0
votes
0
answers
42
views
Short lambda being split by clang-format, even with AllowShortLambdasOnASingleLine
I have this code block:
int main() {
SomeType something1(
[](const Input& i) -> Output { return Output {1, 2}; },
[](const Input& i) -> Output { return Output {1, 2}; ...
0
votes
0
answers
14
views
clang-format giving inconsistent results
I'm working on a program that does a clang-format check of any newly submitted git files and shows a diff of what the user needs to change to match the correct format.
The issue here is that I've ...
0
votes
0
answers
44
views
Is there a clang-format setting that will expand tabs to spaces in comments?
I am using VSCode with clang-format, and I have the editor set to insert spaces for tabs, and I am using the following clang-format (LLVM v14.0.0) options:
"C_Cpp.clang_format_fallbackStyle":...
2
votes
0
answers
14
views
disable spaces in single line function in clang-format
In our code base we use the following style for single line functions:
int func() {return 1;}
However clang-format formats this as:
int func() { return 1; }
Is there a setting for clang-format to ...
0
votes
0
answers
6
views
How to align number of functions but different args (clang-format )
How to modify the clang-format the configuration to make code:
rt_adc_read_buf(adc_dev_0, E_ADC_API_CH_TEMP1, &adc_buff_sort[0][0], ADC_API_BUFF_SIZE);
rt_adc_read_buf(adc_dev_0, ...
0
votes
0
answers
20
views
VSCode clang-format Xaver extension with no parent location for .clang-format
I would like to configure the VSCode Xaver extension to use a .clang-format file that is not in the parent folder, E.g. on the command line you could do this
"-style=file:<format_file_path>&...
2
votes
0
answers
39
views
Can clang-format be instructed to maintain newlines in a function call parameter list?
For example, in Go, the following is a correctly go-fmt'ed code
func main() {
fmt.Println("Hello World",
"some",
"more", "args", "s&...
2
votes
0
answers
30
views
Clang-format setting that not touches nested structs
I'm using VisualStudioCode and the C++ extension. In a C language file I have some nested structs like
struct NewMenu appMenu[] =
{
{ NM_TITLE, "Project", 0 , 0, 0, 0 ...
0
votes
1
answer
37
views
Understanding clang-tidy "DeadStores" and "NewDeleteLeaks" warnings
I am relatively new to c++ programming, especially when dealing with OOP and pointers.
I am trying to implement a binary search tree, and here is the code I have
struct TreeNode {
int val;
...
1
vote
2
answers
142
views
How to apply format when saving in Visual Studio 2022?
I have placed a .clang-format file in my project and can apply the formatting with Ctrl+K, Ctrl+D. I have also configured and enabled "Run Code Cleanup profile on Save" under Options -> ...
0
votes
0
answers
26
views
clang-format: macro in a function
Some c code
Before format:
#define MS_DEF(type) extern type
MS_DEF(int) my_func(int a, int b, int c, const char *x, const char *y, const char *z)
{
// do something
return 0;
}
After format (...
1
vote
0
answers
39
views
C++, vscode) Is there a way to change the formatting of curly braces inside parameters?
clang formatter in vscode formats my code weird.
onUpdate([=]() mutable {
countText = *text + std::to_string(*count);
},
text, count);
look at that, why the text, count params are there? I ...
1
vote
0
answers
46
views
clang-format excessively indents concepts
I am using clang-format-14 to format my C++ code. I wrote a concept for a buffer class which acts similar to iostream objects, and I want it to look like this:
template <typename T>
concept Data ...
0
votes
0
answers
11
views
Is there a way to always insert an empty lines between if-else statements and try-catch blocks with clang-format?
I would like to convert
if (true) {
...
} else {
...
}
to
if (true) {
...
}
else {
...
}
The same for try-catch blocks. I couldn't find anything on that in the clang-format docs.
https:...
0
votes
0
answers
18
views
clang-format break `=>` in vala
=> break as = >
eg:
gesture.released.connect((n_press, x, y) => {
always break as gesture.released.connect((n_press, x, y) = > { that bring errors during compile.
I use -assume-filename=xx....
0
votes
0
answers
24
views
How to make clang-format to apply both Align and BlockIndent in AlignAfterOpenBracket?
There're Align and BlockIndent in AlignAfterOpenBracket with BlockIndent I can do this
// what I want(only with BlockIndent)
socket.async_write_some(
asio::buffer(_data, length),
[this, self](...
5
votes
0
answers
36
views
Clang-Format wrongly detects & operator (boost serialization)
I tried to apply clang-format (11) on a file containing a boost serializable file with content along the lines
class MyClass
{
int _myNumber;
int &_myRefNumber;
friend class boost::...
0
votes
0
answers
58
views
How can I get Qt creator clang-format to work on google protocol buffer .proto files?
I'm using Qt Creator. In my project settings Beautifier/Clang Format the "Restrict to MIME types:" field contains
text/x-c++src; text/x-c++hdr; text/x-csrc; text/x-chdr; text/x-objcsrc; text/...
0
votes
1
answer
30
views
clang-format (v10.0.0) doesn't recognise keys such as `BraceBreakingStyle` (introduced in v3.7)
I am on ubuntu 20.04, and I installed clang-format (the default package, v10.0.0), and can't get it to format my c++ files because keys such as "BraceBreakingStyle" (introduced in clang-...
0
votes
0
answers
27
views
ClangFormat Single Line While Loop Semicolon Issue [duplicate]
I often have statements like:
...
// Wait until resource is not busy anymore
while (resource_busy());
...
for busy waiting. ClangFormat moves the semicolon into its own line:
...
// Wait until ...
1
vote
0
answers
86
views
clang-format: Why does disabling AlignAfterOpenBracket cause a line break?
I have the following line
const float CELL_WORLD_WIDTH{SharedConfig::CELL_WIDTH * SharedConfig::TILE_WORLD_WIDTH};
With AlignAfterOpenBracket: Align, it gets formatted as such
const float ...
1
vote
1
answer
92
views
How to make clang-format not align parameters to function call?
I want to make clang-format not align call parameters to the '(' symbol. I had tried setting PenaltyBreakBeforeFirstCallParameter to 0, but it didn't help.
How I want it to be:
...
0
votes
0
answers
144
views
Clang format array initializer braces not aligning
I have this .clang-format file here:
TabWidth: 4
IndentWidth: 4
UseTab: Never
ColumnLimit: 100
Language: Cpp
DisableFormat: false
Standard: Latest
AccessModifierOffset: -4
AlignAfterOpenBracket: ...
1
vote
1
answer
91
views
clang-format and references: can I get a "true" left alignment?
I've recently tried the ReferenceAlignment: Left option introduced in clang-format 13.
In combination with AlignConsecutiveDeclarations: true it produces a rather strange results:
ShortType &...
0
votes
0
answers
43
views
Vertically align for-loops with clang-format
Scientific code often features loop nests such as
for(int x=0;x<10;x++)
for(int y=0;y<10;y++)
for(int z=0;z<10;z++){
func(x, y, z);
}
The extra levels of indentation can pose a readability ...
6
votes
0
answers
79
views
Preventing clang-format from inserting line breaks in template instantiations
I'm having a problem where clang-format inserts line breaks in template instantiation parameters too easily.
For example, this:
return std::is_trivially_constructible_v<T> && std::...
0
votes
0
answers
18
views
VSCode autoindent = "none" not working on my C Code
I need to stop VS Code to add an indent after a line which doesn't end with semicolon or any kind of braces. When I removed the semicolon the following lines are indented. But if I put the semicolon ...
0
votes
1
answer
297
views
clang-format file not working in Visual Studio code
I have put a .clang-format file in my workspace. But that formating is not applied on my code. I have checked the settings and they are set to None for fallback-style and file for clang_format_style.
...
0
votes
0
answers
31
views
How to tell Eclipse CDT Clang Formatter to take settings from customized `.clang-format` file?
I am using the Clang-Format code formatter in Eclipse CDT.
I have set the executable path under
Window->Preferences->C/C++->CppStyle:
Clang-format path: /opt/clang/11.0.0/bin/clang-format
...
1
vote
0
answers
221
views
Pretty-printing Designated Initializers in Visual Studio 2022
C++ supports designated initializers officially since C++20, C supports them since C99.
How can Visual Studio C++ 2022 be configured to pretty-print them with a space after the opening curly bracket?
...
2
votes
0
answers
79
views
Clang format: macros which include braces
I have created a .clang-format file to format my code. I noticed a problem that the format of macros, e.g. #defines gets garbled by clang-format when using braces in the define, for example:
#define ...
3
votes
1
answer
82
views
How to force .clang-format on multiple github repos
I'm using multiple C++ github repositories, they all share (as a company standard) the same .clang-format file.
We decided to put this file in a separate repository in order to be able to track ...
0
votes
0
answers
18
views
is there a way to have clang format to require (or not require), parentheses around the argument?
Want clang-format to enforce a C language return keyword style. I'd prefer:
return (foo);
but can live without the parentheses, I just want clang-format to do enforcement. -- Thanks!
0
votes
0
answers
55
views
How to properly align types, variables, storage specifiers and values with clang-format?
I am compelled to investigate using clang-format to modify my code, and I am looking for some help with certain things.
Here is an example of our style, which I need to replicate:
/********************...
0
votes
0
answers
72
views
clang-format pointer syntax alignment with multiple declarations on one line
With a C++ input file such as:
int *p1;
int *p2, *p3;
I can run clang-format using the Chromium style; which has a Left value for the PointerAlignment option. But when I do, only p1 has its pointer ...
2
votes
1
answer
230
views
clang-format doesn't recognize matching header's name
I have a .clang-format file configured, where I want to do the following sorting
#include <stdafx.h>
#include "the_header_of_this_source_file.h"
#include <includes_with_arrow>
...
0
votes
0
answers
23
views
Clang-format with 8-space rule for complex if statements in Java
I'm trying to set-up clang-format so that it confirms to Oracle's Java Code Conventions. The document provides numerous examples, of which the most important are:
function(longExpression1, ...
0
votes
1
answer
42
views
How to specify suboptions for clang-format?
In the .clang-format file I can say:
BasedOnStyle: LLVM
BreakBeforeBraces: Custom
BraceWrapping:
AfterClass: false
AfterFunction: true
How do I specify the same on the command line? (For ...
0
votes
1
answer
29
views
Implement a SCons source code formater. Modify a source file before compiling it
I want to format my C/C++ source code before every compilation.
I found no information how to do it in SCons.
Ideas I tried:
What I would need: a Builder that has the same files for source and target....
3
votes
1
answer
69
views
There are always line breaks after an enum declaration
I have the following BraceWrapping options:
BraceWrapping:
AfterEnum: false
AfterStruct: false
SplitEmptyFunction: false
AfterControlStatement: "Never"
AfterFunction: false
...
0
votes
1
answer
238
views
How to position asterisk near to variable name when declaring pointers in C in vscode auto formatting?
Currently if I write int * ptr; then auto-formatting would convert it to int* ptr; which I don't want. After googling a bit, I found here the fix for that. I have to use PointerAlignment: PAS_Middle. ...
7
votes
0
answers
133
views
Tweaking clang-format for C++20 ranges pipelines
C++20 (and 23 with std::ranges::to<T>()) makes idiomatic the use of operator| to make a pipeline of transformations such as this:
return numbers
| std::views::filter([](int n) { ...
0
votes
0
answers
35
views
[Clang Format]Make MACRO after if statement stay on same line
I'm looking for a configuration options(s) for .clang-format that will make clang-format keep a MACRO on the same line as an if statement.
Current:
What I want:
Here is my current .clang-format: ...
0
votes
0
answers
161
views
Tell clang-format to not add newline after control statement
When I have this minimal program
int main(void) {
for (;;) ;
if (0) return 1;
}
clang-format is adding a new line before the statements:
int main(void)
{
for (;;)
...
0
votes
0
answers
95
views
Indent braces with clang-format
I'd like to use clang-format to indent braces like so,
for(...)
{
[...]
[...]
}
Clang-format can do this with BreakBeforeBraces: Whitesmiths, but I would like to customise that. The ...
2
votes
1
answer
48
views
clang-format and fluent setters
I'm using fluent style for my setters and as soon as the line length has been exceeded, clang format is formating my code as requested.
But if the line is shorter, it aligns the setters in one line.
...
0
votes
1
answer
121
views
clang-format indentation of class fields and methods, opening curly braces for functions and enums
My clang-format produces code like this:
enum class SomeEnum{ VAL1, VAL2, VAL3 };
class SomeClass {
public:
void someMethod();
private:
int m_field;
};
void someFunc()
{
// ...
...