Questions tagged [clang-format]

clang-format is a tool to format C/C++/Java/JavaScript/Objective-C/Protobuf code.

ClangFormat describes a set of tools that are built on top of LibFormat. It can support your workflow in a variety of ways including a standalone tool and editor integrations.

clang-format is located in clang/tools/clang-format and can be used to format C/C++/Java/JavaScript/Objective-C/Protobuf code.

Documentation:

http://clang.llvm.org/docs/ClangFormat.html

Useful resources

566 questions
100
votes
10 answers

How to call clang-format over a cpp project folder?

Is there a way to call something like clang-format --style=Webkit for an entire cpp project folder, rather than running it separately for each file? I am using clang-format.py and vim to do this, but I assume there is a way to apply this once.
user3639557
  • 3,731
  • 4
  • 25
  • 50
76
votes
8 answers

Can clang-format tell me if formatting changes are necessary?

Is there an way that you can run clang-format in a mode where it reports if the file meets the specified format? A kind of dry-run mode where it reports if a change is needed, but doesn't make the change. Ideally I'd like clang-format to just return…
David Ogren
  • 2,776
  • 1
  • 14
  • 25
63
votes
4 answers

Clang-format line breaks

I'm looking for a clang-format setting to prevent the tool from removing line breaks. For example, I have my ColumnLimit set to 120, and here's what happens when I reformat some sample code. Before: #include #include…
zmb
  • 6,717
  • 3
  • 34
  • 52
58
votes
1 answer

Can clang format add braces to single line if statements etc

Is there an option for clang-format to add braces to all if()/do/while statements etc? eg if( i == 42 ) std::cout << "You found the meaning of life\n"; else std::cout << "Wrong!\n"; to if( i == 42 ) { std::cout << "You found the meaning of…
Adrian Cornish
  • 20,973
  • 11
  • 50
  • 74
55
votes
0 answers

clang-format: Setting to control C++ attributes

Searching through the Clang-Format Style Options, I can't seem to find a way to control the behavior on the placement of C++ attributes. As an example, take these two declarations, the first of which does not overflow the column limit and the second…
Travis Gockel
  • 24,743
  • 10
  • 80
  • 105
53
votes
2 answers

How to mark a region so clang-format won't touch it?

For example, in the MySQL++ library there are macros that can be used to define simple structs based on sql table definition, like this: sql_create_6(stock, 1, 6, mysqlpp::sql_char, item, mysqlpp::sql_bigint, num, mysqlpp::sql_double,…
Alex
  • 533
  • 1
  • 5
  • 6
52
votes
6 answers

Where are clang-format and clang-format.py in Mac OS X with Xcode Command Line Tools installed?

I am having a hard time finding clang-format and clang-format.py on my Mac OS 10.8.5 with Xcode 5 and its Command Line Tools. I tried find / -name "clang-format.py" without luck. Are they already here somewhere hiding, or how would I get this…
Ahmed Fasih
  • 5,979
  • 3
  • 43
  • 86
51
votes
2 answers

In clang-format, what do the penalties do?

The clang-format sytle options documentation includes a number of options called PenaltyXXX. The documentation doesn't explain how these penalties should be used. Can you describe how to use these penalty values and what effect they achieve (perhaps…
Scott Langham
  • 53,246
  • 34
  • 122
  • 193
46
votes
9 answers

Why are my Xcode plugins (such as clang format) installed with Alcatraz no longer working after updating to new version of Xcode?

Today I updated to Xcode 6.3.2 and I can't run the Clang code formatting – it seems like it's not even installed. Everytime I update Xcode, I have to reinstall Alcatraz and most of the packages (why btw?) to make them work on the new version of…
Pavel Smejkal
  • 3,526
  • 6
  • 25
  • 44
44
votes
4 answers

Using clang-format in CLion

I'd like to format my code using clang-format in CLion. I've gone through the settings and haven't found any setting that would allow me to run an external command to transform code. How are such things normally done in CLion?
Klemen Forstnerič
  • 864
  • 1
  • 9
  • 11
39
votes
2 answers

How do I specify a clang-format file?

I just built clang 5.0.0 on a Redhat 6 machine and tried to run clang-format. I'm unable to get the -style option to read in a style file. Here's an example of a set of commands that I think should work: ./clang-format -style=llvm -dump-config >…
dromodel
  • 7,827
  • 10
  • 41
  • 58
38
votes
2 answers

How can I apply only one clang-format action?

I want to use clang-format to align my comments, but nothing else. The option for that is: AlignTrailingComments (bool). But when I run the following: clang-format-3.6 -i -style='{AlignTrailingComments: true}' It performs all kinds of…
Trevor Hickey
  • 31,728
  • 22
  • 131
  • 236
36
votes
3 answers

How to auto indent a C++ class with 4 spaces using clang-format?

I got the next .clang-format file in my project's root directory: --- AlignTrailingComments: true AllowShortFunctionsOnASingleLine: false AllowShortIfStatementsOnASingleLine: true AllowShortLoopsOnASingleLine: true BreakBeforeBinaryOperators:…
BPL
  • 9,807
  • 7
  • 37
  • 90
35
votes
5 answers

Indenting preprocessor directives with clang-format

I am working on a c++ project where I am using a lot of #pragma omp. I use the wonderful clang-format for tidiness but it always deletes the indentation for all preprocessor directives. Is there a way to change that behavior? Or is there another…
dawirstejeck
  • 380
  • 4
  • 12
34
votes
5 answers

Can clang-format align variable or macro assignments in columns?

Is it possible to have clang-format align variable assignments in columns? For example: int someInteger = 42; std::string someString = "string"; const unsigned someUnsigned = 42; #define SOME_INTEGER 42 #define…
syvex
  • 7,006
  • 5
  • 38
  • 42
1
2 3
37 38