我想用clang-format,但我在我的系统中找不到它.我从macports安装了llvm.安装/启用它的方法是什么?
正如标题所示,我试图使用 clang-format 来删除文件的尾随空格,但我找不到相关的选项名称。
有人能指出我显而易见的事情吗?
我正在尝试为在 Windows 10 Pro 64 位上运行的 clang-format v6.0.0 配置自定义样式选项文件。我首先使用以下命令行基于 llvm 样式生成一个选项文件,该文件运行良好:
clang-format -style=llvm -dump-config > .clang-format
Run Code Online (Sandbox Code Playgroud)
clang-format 的文档说明如下:“当所需的代码格式样式与可用选项不同时,可以使用 -style="{key: value, ...}" 选项或通过放置您的项目目录中的 .clang-format 或 _clang-format 文件中的样式配置,并使用 clang-format -style=file。”
因此,作为测试,我使用了上面生成的未更改的 .clang 格式文件,并使用了以下命令行:
clang-format -style=.clang-format Test.c
Run Code Online (Sandbox Code Playgroud)
结果是一条消息说
"Invalid value for -style"
Run Code Online (Sandbox Code Playgroud)
然后我将 .clang-format 的名称更改为 _clang-format 并再次尝试,但结果是相同的。所以,我的问题是,“如何指定特定的样式选项文件?”
我安装了一个 FreeBSD 虚拟机,然后我运行了sudo pkg install clang-devel.
clang-format 然而,似乎缺少:
-sh:clang 格式:未找到
如何clang-format在 FreeBSD 11.2 中安装?
我在 vscode 中使用 clang-format 扩展来格式化我的 C++ 代码。我正在寻找一种可以在代码中添加一个空行作为最后一行的配置。但是没有发现。
如果我有一个案例:
#include <iostream>
using namespace std;
int main() {
string s = "asfasdf";
cout << "Type IIanything: ";
// cin >> s;
for (int i = s.length() - 1; i > -1; i--) {
cout << s[i];
}
cout << endl;
cout << "NOT using build 2" << endl;
}
---empty line1---
---empty line2---
---empty line3---
Run Code Online (Sandbox Code Playgroud)
然后当我点击保存时,第 2 行和第 3 行的空行消失了。而且只剩下1个了。这对我来说很好。但是如果 1、2 和 3 都不在那里,并且我点击了保存,那么右大括号就是代码中的最后一行。在这种情况下,我该怎么做才能在右大括号后添加一个空行。
我正在尝试使用 clang-format 将 if 语句与同一行上的一个语句分开。
输入示例:
if(is_finished) break;
if(foo) {
// do something
}
Run Code Online (Sandbox Code Playgroud)
clang 格式输出:
if(is_finished)
break;
if(foo) {
// do something
}
Run Code Online (Sandbox Code Playgroud)
想要的输出:
if(is_finished) break;
if(foo) {
// do something
}
Run Code Online (Sandbox Code Playgroud)
与空间相关的选项似乎都不符合这种风格。
当前配置:
---
Language: Cpp
BasedOnStyle: LLVM
IndentWidth: 8
UseTab: ForIndentation
SpaceBeforeParens: Never
BraceWrapping:
AfterControlStatement: false
Run Code Online (Sandbox Code Playgroud) 我有一个基于 LLVM 的 clang 格式,但我发现很难找到一个在评论开始后添加空间的设置。基本上,我想要:
// This is a comment
Run Code Online (Sandbox Code Playgroud)
代替:
//This is a comment
Run Code Online (Sandbox Code Playgroud)
这是.clang-format我目前正在使用的:
# Generated from CLion C/C++ Code Style settings
BasedOnStyle: LLVM
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: Consecutive
AlignOperands: true
AllowAllArgumentsOnNextLine: false
AllowAllConstructorInitializersOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: Always
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Always
AllowShortLambdasOnASingleLine: All
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterReturnType: None
AlwaysBreakTemplateDeclarations: Yes
BreakBeforeBraces: Custom
AlignTrailingComments: true
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: Never
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterUnion: false
BeforeCatch: false
BeforeElse: …Run Code Online (Sandbox Code Playgroud) 我有以下.cpp文件:
////////////////////////////////////////////////////////////////////////////////
void call_long_function_name(bool) {}
void sf(bool) {}
int main() {
bool test = true;
if (test) { call_function_name(test); }
if (test) { sf(test); }
return 0;
}
Run Code Online (Sandbox Code Playgroud)
(斜杠分隔80个字符).使用以下配置文件,clang-format建议:
////////////////////////////////////////////////////////////////////////////////
void call_long_function_name(bool) {}
void sf(bool) {}
int main() {
bool test = true;
if (test) {
call_function_name(test);
}
if (test) {
sf(test);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
甚至在文件中我允许简短的if语句适合单行.
我设置了错误的选项吗?
我可以使用哪些选项来最小化浪费的垂直空间?
Clang-format的.clang格式文件
BasedOnStyle: Google
AccessModifierOffset: -1
AlignEscapedNewlinesLeft: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortFunctionsOnASingleLine: true
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: false …Run Code Online (Sandbox Code Playgroud) 现在,我有一个非常愚蠢的漂亮打印脚本,该脚本做了一点git-fu查找(无条件地)格式化的文件,然后通过clang-format -i运行它们。这种方法有几个缺点:
过去,我能够使用CMake进行处理,这些处理具有一些好特性,我想在bazel中重现这些特性:
在CMake-land中,我受SCons代理目标欺骗的启发使用了这种策略:
介绍一个虚拟目标(例如source-> source.formatted)。与该目标关联的操作有两件事:a)运行clang-format -i source,b)输出/触摸名为source.formatted的文件(如果source.formatted比source更新,则对于合理的文件系统,这保证了合理的文件系统)不需要重新格式化)
添加一个虚拟目标(target_name.aggregated_formatted),该目标汇总与特定库/可执行目标的源相对应的所有.formatted文件
使库/可执行目标依赖于target_name.aggregated_formatted作为预构建步骤
任何帮助将不胜感激。
想不出该选项后,增加了空间if,while,catch,等...
目前我的 .clang 格式文件产生这个:
while(true)
{
if(flushedCount == count)
{
break;
}
}
Run Code Online (Sandbox Code Playgroud)