Adr*_*ian 47 c++ clang clang-format
是否有一个clang-format选项可以为所有if()/ do/while语句等添加大括号?
例如
if( i == 42 )
std::cout << "You found the meaning of life\n";
else
std::cout << "Wrong!\n";
Run Code Online (Sandbox Code Playgroud)
至
if( i == 42 )
{
std::cout << "You found the meaning of life\n";
}
else
{
std::cout << "Wrong!\n";
}
Run Code Online (Sandbox Code Playgroud)
运用
$ clang-format --version
clang-format version 3.6.0
Run Code Online (Sandbox Code Playgroud)
jbc*_*coe 31
clang-tidy可以使用FIXITS对代码进行语法更改
clang-tidy YOUR_FILE.cpp -fix -checks="readability-braces-around-statements" -- COMPILE_OPTIONS
Run Code Online (Sandbox Code Playgroud)
更新:
clang-tidy是一个重要的工具,因为它需要编译选项来解析文件,遗憾的是clang格式(截至v3.9)不会添加大括号.
COMPILE_OPTIONS 将是用于编译文件的包含路径等,即 -std=c++14 -stdlib=libc++ -O2 -I.
如果您有compile_options.json来自CMake 的文件,那么您可以将其包含的目录的路径传递给clang-tidy,它将查找该文件的相应编译选项:
clang-tidy YOUR_FILE.cpp -fix -checks="readability-braces-around-statements" -p COMPILE_OPTIONS_DIR
Run Code Online (Sandbox Code Playgroud)
val*_*ano 26
从 clang-format-15 (目前是 trunk)开始,答案是肯定的 - 使用InsertBraces今天刚刚发布的新选项:
\n https://github.com/llvm/llvm-project/commit/77e60bc42c48e16d646488d43210b1630cd4db49 \n https:/ /reviews.llvm.org/D120217
来自 clang-format文档:
\n\n\nInsertBraces(布尔值) clang-format 15
\n在 C++ 中的控制语句(if、else、for、do 和 while)之后插入大括号,除非控制语句位于宏定义内或者大括号将包围预处理器指令。
\n警告:
\n将此选项设置为 true 可能会由于 clang-format\xe2\x80\x99s 缺乏完整的语义信息而导致代码格式不正确。因此,应格外小心地检查此选项所做的代码更改。
\n
false: true:\n\nif (isa<FunctionDecl>(D)) vs. if (isa<FunctionDecl>(D)) {\n handleFunctionDecl(D); handleFunctionDecl(D);\nelse if (isa<VarDecl>(D)) } else if (isa<VarDecl>(D)) {\n handleVarDecl(D); handleVarDecl(D);\nelse } else {\n return; return;\n }\n\nwhile (i--) vs. while (i--) {\n for (auto *A : D.attrs()) for (auto *A : D.attrs()) {\n handleAttr(A); handleAttr(A);\n }\n }\n\ndo vs. do {\n --i; --i;\nwhile (i); } while (i);\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
7738 次 |
| 最近记录: |