sourc insigt使用clang format进行格式管理

1、文件编码规则并实现自动按照clang-format格式进行自动保存

a、安装LLVM工具: LLVM-18.1.8-win64.exe

下载链接:https://github.com/llvm/llvm-project/releases/tag/llvmorg-18.1.8

需要注意选择 Add LLVM to the system PATH for current userAdd LLVM to the system PATH for all users 其中的一项

安装完成后,在命令行中测试:

clang-format -version

2、打开 Options-> Custom Commands

3、点击 Add... 按钮,具体内容为:Command 值为 FormatWithClang

Run 值为 clang-format --style=file -i %f

Dir 留空(即项目目录,这个目录里需要有 .clang-format 文件)

勾选 Save Files First

不要勾选 Pause When Done

勾选 Wait Until Done

4、点击 Menu... 按钮,在合适的菜单中加入刚刚定义的命令,例如在FILE中,insert ,然后就可以在tools菜单中看到clang-format选项,点击后会格式化当前源文件

5、点击 Keys... 按钮,为刚刚定义的命令分配合适的快捷键

6、把.clang-format文件放到根目录下

bash 复制代码
BasedOnStyle: LLVM
Language: Cpp             

IndentWidth: 4
TabWidth: 4
UseTab: Never              
PointerAlignment: Right   

ColumnLimit: 120
BreakBeforeBraces: Allman 


AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Empty


SpaceBeforeParens: ControlStatements
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceInEmptyParentheses: false


AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignOperands: true


IndentCaseLabels: true
AllowAllArgumentsOnNextLine: false

7、可以使用以下文件进行测试

souce insight 点击 FormatWithClang或着快捷键(刚刚配置的,例如我的ctrl+shift+s)以指定格式保存当前文件

8、提供已下内容可以进行测试,查看是否按照指定地格式进行保存

cpp 复制代码
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>

#define MAX(a,b)   ((a) > (b) ? (a) : (b))
#define MIN( a , b ) (( a ) < ( b ) ? ( a ) : ( b ) )

#define VERY_LONG_MACRO_DEFINITION(x, y, z) do { if((x) > (y)) { (z) = (x) + (y); } else { (z) = (x) - (y); } } while(0)

typedef enum
{
VALUE_A=0,
  VALUE_B   =1 ,
VALUE_C = 2 ,
}ValueType;

typedef struct InnerStruct {
int a;char b;
    const char* name;
}InnerStruct;

typedef struct VeryLongStructNameTag
{
    int           id;
     char*        description;
InnerStruct  inner;
int (*callback)(int arg1,int arg2);
} VeryLongStructName;

static int global_var1=1,global_var2   =2;
static const char * global_str = "hello";

static int sum(int x,int y){return x+y;}

static int very_long_function_name_with_many_parameters(int a, int b, const char* message, VeryLongStructName* s, int (*func)(int, int))
{
int result=0;
if(a> b){result = a+b;}else
{
result=a-b;
}
   if (message!=NULL) { printf("msg: %s\n", message ); }

for(int i=0;i<10;i++){ result+=i; if(i%2==0){printf("even %d\n",i);}else{printf("odd %d\n",i);} }

while(result<100){result +=   3;}

switch(result%3){
case 0: printf("mod 0\n");break;
  case 1:
printf("mod 1\n");
break;
default:
        printf("mod other\n");
            break;
}

if (s != NULL && s->callback!=NULL)
{
int cb = s->callback(a,b);
printf("callback=%d\n",cb);
}

VERY_LONG_MACRO_DEFINITION(a,b,result);

return result;
}

/* 测试注释对齐:
 * 1. 这一行很长很长很长很长很长很长很长很长很长很长很长很长,看 clang-format 会不会换行
 * 2. 看看星号对齐方式
 */

void test_pointer_and_array (void)
{
int arr[5]={1,2,3,4,5};
int * p1 = arr; int* p2= &arr[0] ;
int **pp = &p1;
for ( size_t i=0;i<5;i++ )
printf("arr[%zu]=%d\n",i,arr[i]);

*pp = p2;
if( p1 == p2 )
{
printf("same pointer\n");
}
}

int func_add(int x,int y){return x+y;}
int func_sub(int x,int y){ return x-y; }

void test_function_pointer()
{
int (*op)(int,int) = func_add;
int r1 = op(10,3);
op = func_sub;
int r2 = op(10,3);
printf("r1=%d, r2=%d\n",r1,r2);
}

int main(   void )
{
VeryLongStructName s = {
.id    = 1,
.description = "test",
.inner = { .a = 10, .b ='c', .name = "inner" },
.callback = sum,
};

int x=  10;
int y =20;
int z=0;
int res = very_long_function_name_with_many_parameters(x,y,"hello world",&s,sum);
printf("res=%d\n",res);

test_pointer_and_array();
test_function_pointer();

/* 行内注释测试 */ int inline_var=123; // 尾部注释

return 0;
}
相关推荐
Hhy_1107几秒前
《C++深度解构04》类和对象(下)——类型转换、static、友元与编译器优化
c语言·c++·学习·类和对象·visual studio
qq_1998868726 分钟前
第7板块·第1节:通用算子分类与设计模式
c++·人工智能·gpu算力·cuda
6Hzlia2 小时前
【Classic 150 刷题计划】 LeetCode 205. 同构字符串 | C++ 双向绑定与哈希表映射
c++·算法·leetcode
神仙别闹8 小时前
基于 C++ 实现(控制台)学生成绩管理系统
开发语言·c++
会周易的程序员8 小时前
5 节点边缘冗余方案(上):基于 aiRaft 的物联网高可用控制面设计
c++·分布式·物联网·raft·iot·共识
qq_1998868711 小时前
第6板块·第4节:构建系统与多文件项目
c++·人工智能·gpu算力
汉克老师13 小时前
GESP2026年9月认证C++三级( 第三部分编程题(2、分割字符串))精讲
c++·gesp·小学生·学c++编程
估值探索者13 小时前
【Python量化系统工程化 #08】关了 SSH 就停?systemd 让脚本开机自启 + 异常自动拉起
java·c++·人工智能·分类·数据挖掘
天若有情67313 小时前
C++花式魔改!手写头文件复刻Java语法,彻底打破编码思维定式
java·c++·语言做语言·jac++
qq_1998868714 小时前
第7板块·第3节:CUTLASS 的 GEMM 实现与优化策略
c++·人工智能·gpu算力·cuda