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.

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 and Protobuf code.

Documentation:

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

Useful resources

804 questions
151
votes
18 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
  • 4,791
  • 6
  • 30
  • 55
99
votes
9 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
  • 4,396
  • 1
  • 19
  • 29
92
votes
3 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
  • 923
  • 1
  • 7
  • 6
79
votes
5 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
  • 7,605
  • 4
  • 40
  • 55
78
votes
2 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
  • 26,877
  • 14
  • 89
  • 116
78
votes
2 answers

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
  • 23,227
  • 13
  • 61
  • 77
69
votes
2 answers

In clang-format, what do the penalties do?

The clang-format style 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
  • 58,735
  • 39
  • 131
  • 204
63
votes
3 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
  • 9,581
  • 12
  • 47
  • 65
63
votes
7 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
  • 6,458
  • 7
  • 54
  • 95
56
votes
9 answers

How can I install clang-format in Ubuntu?

I am trying to use clang-tools in particular clang-format for automatic code formatting in vim but I couldn't find this tool with apt-get search. Is there anybody experienced this problem before, do you have any suggestion?
Validus Oculus
  • 2,756
  • 1
  • 25
  • 34
52
votes
4 answers

Line breaks between function definitions

Is there any way to to automatically insert spaces between function definitions. E.g. my initial sources are: void func1() { // func1 body. } void func2() { // func2 body. } I would like it to be reformatted to: void func1() { // func1…
PovilasB
  • 1,488
  • 1
  • 15
  • 25
49
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č
  • 914
  • 1
  • 10
  • 11
48
votes
2 answers

how to make clang-format add new line before opening brace of a function?

I'm interested in putting an opening brace for functions (but not if statements and other contexts). For example void foo() { ... } Flamewars aside, is there a good rationale for not doing this? Although I use same-line open-brackets for if/else…
daj
  • 6,962
  • 9
  • 45
  • 79
47
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
  • 36,288
  • 32
  • 162
  • 271
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,600
  • 6
  • 27
  • 45
1
2 3
53 54