如果语句等可以用clang格式在一行中添加大括号 [英] Can clang format add braces to single line if statements etc

查看:416
本文介绍了如果语句等可以用clang格式在一行中添加大括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

clang格式是否可以在所有if()/ do / while语句等中添加括号?

Is there an option for clang-format to add braces to all if()/do/while statements etc?

例如

if( i == 42 )
   std::cout << "You found the meaning of life\n";
else
   std::cout << "Wrong!\n";

if( i == 42 )
{
   std::cout << "You found the meaning of life\n";
}
else
{
   std::cout << "Wrong!\n";
}

使用

$ clang-format --version
clang-format version 3.6.0


推荐答案

clang-tidy可以使用FIXITS对代码进行语法更改

clang-tidy can make syntactic changes to your code using FIXITS

clang-tidy YOUR_FILE.cpp -fix -checks="readability-braces-around-statements" -- COMPILE_OPTIONS

已更新:

对此,clang-tidy有点重量级,因为它需要编译选项来解析文件,可悲的是clang格式(自v3.9)不会添加花括号。

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 将是您用来编译的包含路径等文件,即 -std = c ++ 14 -stdlib = libc ++ -O2 -I。

COMPILE_OPTIONS would be the include paths etc that you use to compile the file with, ie -std=c++14 -stdlib=libc++ -O2 -I.

如果您有一个来自CMake的 compile_options.json 文件,然后可以将其包含的目录路径传递给clang-tidy,它将为该对象查找适当的编译选项。文件:

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

这篇关于如果语句等可以用clang格式在一行中添加大括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆