Can clang format add braces to single line if statements etc
clang-tidy can make syntactic changes to your code using FIXITS
clang-tidy YOUR_FILE.cpp -fix -checks="readability-braces-around-statements" -- COMPILE_OPTIONS
Updated:
clang-tidy is a bit of a heavyweight tool for this as it needs compile options to parse the file, sadly clang-format (as of v3.9) won't add braces.
COMPILE_OPTIONS
would be the include paths etc that you use to compile the file with, ie -std=c++14 -stdlib=libc++ -O2 -I.
If you have a compile_options.json
file from CMake then you can pass the path of the directory it is contained in to clang-tidy and it will look up the appropriate compile options for the file:
clang-tidy YOUR_FILE.cpp -fix -checks="readability-braces-around-statements" -p COMPILE_OPTIONS_DIR
Adrian Cornish
Current specialty is real time financial market data with sub microsecond latency Protocols: UDP, TCP, UDP Mutlicast, FIX, FIX/FAST Specialist in: US Treasury/Fixed Income related trading systems include: ICAP, BrokerTec, ESpeed, Govex, ELX, Cantor, BGC. Languages: C++ (25 years), STL (20 years), SQL (17 years), C (18 years), PHP (18 years) Databases: MySql (18 years), C-ISAM (3 years), D-ISAM (1 year) Web Related: HTML (18 years), Apache (18 years) OS: Unix’s (13 Years), VMS (11 years), Window/Dos (25 years), Linux (20 years) Been programing with real computers since I was 16 - been paid for it since I was 18 Worked on VAX, RS600, Intel, Sparx, Ultrix and all sorts of in between
Updated on July 25, 2022Comments
-
Adrian Cornish almost 2 years
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 life\n"; } else { std::cout << "Wrong!\n"; }
Using
$ clang-format --version clang-format version 3.6.0
-
Novice C over 7 yearsIs it possible to run this without making a compilation database? Say I purely only wanted to run the readability check, without looking for compile errors. I ask because I want to edit individual files independent of the entire project. When I try to do this I get
Error while trying to load a compilation database
andRunning without flags.
Which I think is to say that it ignores the readability check, since it does not add the braces as I'd wished. -
Novice C over 7 yearsTo give a concrete example, say my file has
#include "../header.h"
but I am editing the file in a directory with noheader.h
in the parent directory. Is it possible to still use clang-tidy in this scenario? -
jbcoe over 7 yearsI've updated my answer with extra info as it won't neatly fit in a comment.
-
jbcoe over 7 yearsI'm not sure I can think of compiler flags that would get your particular example, with a file referenced by a relative include, working.
-
Droopycom almost 6 yearsclang-tidy is just really a front-end for the static analyzer. It really needs to be part of your build process.
-
Tyler Shellberg over 4 years@jbcoe Checking in, as of version clang-format 7.0, is it still impossible for it to add braces?
-
dovedevic about 4 years@TylerShellberg Have you found anything out about this?