之前写了姊妹篇《Qt Creator使用clang-format实现源代码格式化排版(Windows/macOS)》
Qt Creator使用clang-format实现源代码格式化排版(Windows/macOS)_clang-format官网_利白的博客-CSDN博客
https://clang.llvm.org/docs/ClangFormatStyleOptions.html
今天写写VS2019这个IDE。
1、首先安装包自带了clang-format.exe
C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\Llvm\bin\clang-format.exe
C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\Llvm\x64\bin\clang-format.exe
2、IDE选项里面可以设置默认排版风格
VS2019自带了几个默认的clang-format风格。
3、如何使得样式生效?
在编辑菜单高级可以设置文档的格式,或者先后使用快捷键Ctrl+K,Ctrl+D来完成格式化排版。
4、如何自定义格式?
控制台新建一个工程,目的是通过中转来实现格式化,命名为mycustom,源码如下:
// mycustom.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <cassert>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
// 读文本文件, 并将带换行的文件读成一整行返回
string read_text_file(const char *file_name)
{
fstream fin(file_name, ios::in);
assert(fin.is_open());
string line_text;
string all_text;
while (getline(fin, line_text))
{
line_text += ","; //每行末尾添加逗号
all_text += line_text;
}
fin.close();
return all_text;
}
// 判断文件是否存在
bool file_exist(const char *file_name)
{
fstream fin(file_name, ios::in);
bool exist = false;
if (fin)
{
exist = true;
fin.close();
}
return exist;
}
int main(int argc, char *argv[])
{
const string space = " ";
string file_path = argv[0];
string file_dir = file_path.substr(0, file_path.find_last_of("\\") + 1); // 当前exe文件所在目录
stringstream command;
command << file_dir << "clang-format.exe";
// 如果项目目录存在.clang-format或_clang-format
// VS的调用路径在项目目录, 所以使用相对路径
if (file_exist(".clang-format") || file_exist("_clang-format"))
{
for (int i = 1; i < argc; i++)
{
string arg = argv[i];
command << space << arg;
}
}
else
{
// 否则去掉VS传的-style和-fallback-style参数
string format_text = read_text_file((file_dir + "clang-format.txt").data());
command << space << "-style=\"{" << format_text << "}\""; // 使用{key: value}形式写入自定义默认值
for (int i = 1; i < argc; i++)
{
string arg = argv[i];
if (arg.find("-style=") == string::npos)
{ // 去掉VS传的-style和-fallback-style参数
command << space << arg;
}
}
}
return system(command.str().data()); // 将clang-format的结束状态返回, 提供给VS结果信息
}
// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单
// 入门使用技巧:
// 1. 使用解决方案资源管理器窗口添加/管理文件
// 2. 使用团队资源管理器窗口连接到源代码管理
// 3. 使用输出窗口查看生成输出和其他消息
// 4. 使用错误列表窗口查看错误
// 5.
// 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
// 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
编译生成mycustom.exe,该exe复制外部调用clang-format.exe。进入VS IDE选项,自定义指向该exe路径D:\mycustomclang。注意:路径一定不要有空格!!
mycustom.exe是编译生成的exe。
clang-format.exe拷贝来自C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\Llvm\x64\bin\
clang-format.txt内容如下:
#https://clang.llvm.org/docs/ClangFormatStyleOptions.html
BasedOnStyle: Microsoft
AccessModifierOffset: -4
AlignConsecutiveMacros: true
AlignTrailingComments: true
AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: false
BreakBeforeBraces: Allman
ColumnLimit: 0
SortIncludes: Never
SeparateDefinitionBlocks: Always
AllowShortLambdasOnASingleLine: Empty
LambdaBodyIndentation: OuterScope
BreakBeforeBraces: Custom
BraceWrapping:
AfterCaseLabel: true
BeforeLambdaBody: false
SpacesInLineCommentPrefix:
Minimum: 0
Maximum: 0
每个关键词的解释,详情见:
Clang-Format Style Options — Clang 17.0.0git documentation
完整的工程源码请下载:
https://download.csdn.net/download/libaineu2004/14504846
5、常见问题
(1)clang-format格式化会把头文件按照字母排序。
例如,原本是这样的:
#include <windows.h>
#include <wincon.h>
结果格式化后排序为:
#include <wincon.h>
#include <windows.h>
从而导致编译出错:fatal error C1189: #error: "No Target Architecture"
解决办法:
手动把这两行隔离开,之间保留一行空白行。
#include <windows.h>
#include <wincon.h>
---
参考文献
Visual Studio和VS Code使用clang-format自定义C++代码默认格式化样式
Visual Studio和VS Code使用clang-format自定义C++代码默认格式化样式_vs clang-format_咔狼的博客-CSDN博客
std::string newcmd = command.str();
newcmd = ("\"") + newcmd + ("\""); //加引号是为了避免路径有空格,因为cmd命令不认空格
command.str(""); //这才行
command << newcmd;