linux 代码风格扫描,Linux内核代码风格自动检查和自动整理

一、代码风格自动检查

scripts/checkpatch.pl -f   directory / *.c

二、代码风格自动整理

1、使用开源软件astyle自动整理

http://sourceforge.net/projects/astyle/files/astyle/

下载最新linux版本astyle ,编译:

cd astyle/build/gcc

make release

#!/bin/sh

#./linux-coding-style --style=linux --indent=force-tab=8 --align-pointer=name --max-code-length=90 --break-after-logical -p -H -U ./*.c  ./*.h

./linux-coding-style --style=linux --indent=force-tab=8 --align-pointer=name -p -H -U ./*.c  ./*.h

参考:

http://blog.csdn.net/lixianlin/article/details/7820753

3、注释风格C99自动转换C89

cc.l 源码:

%{

#include

char buffer[1024 * 1024];

char *substitute(char *str1, char *from, char *to)

{

char *buffer;

char *anchor;

char *pos;

anchor = strstr(str1, from);

if (anchor == NULL) {

return str1;

}

buffer = malloc(strlen(str1) + - strlen(from) + strlen(to) + 1);

pos = buffer;

strncpy(pos, str1, anchor - str1);

pos += anchor - str1;

strcpy(pos, to);

pos += strlen(to);

strcpy(pos, anchor + strlen(from));

return buffer;

}

char * g_indent = NULL;

%}

%x COMMENT

%x CHUNK

%x INLINE

%%

^[ \t]*\/\/ {

char *text;

g_indent = strdup(yytext);

text = substitute(yytext, "//", "/*\n");

strcpy(buffer, text);

free(text);

text = substitute(yytext, "//", " *");

strcat(buffer, text);

free(text);

BEGIN CHUNK;

}

\/\* {

printf("%s", yytext);

BEGIN COMMENT;

}

^.*\/\/ {

char *text = substitute(yytext, "//", "/*");

printf("%s", text);

free(text);

BEGIN INLINE;

}

^[ \t]*\/\/ {

char *text = substitute(yytext, "//", " *");

strcat(buffer, text);

}

^[ \t]*[^/] {

char *text;

text = substitute(g_indent, "//", " */\n");

strcat(buffer, text);

free(text);

free(g_indent);

strcat(buffer, yytext);

printf("%s", buffer);

BEGIN INITIAL;

}

\n { strcat(buffer, yytext); }

. { strcat(buffer, yytext); }

\n {

printf(" */\n");

BEGIN INITIAL;

}

. { printf("%s", yytext); }

{

\*\/ {

printf("%s", yytext);

BEGIN INITIAL;

}

. {

printf("%s", yytext);

}

}

%%

main(int argc, char **argv)

{

int i;

if (argc < 2) {

printf("Input from console:\n");

yylex();

}

for (i = 1; i < argc; i++) {

FILE *f = fopen(argv[i], "r");

if (!f) {

perror(argv[i]);

}

yyrestart(f);

yylex();

fclose(f);

}

return 0;

}

Makefile :

cconvert : cc.l

flex $<

gcc lex.yy.c -lfl -o $@

rm lex.yy.c

Usage :

1. compile :

$ make

2. run command:

$ cconverttext.c

使用上面代码编译出的工具cconvert,写自动bash脚本:

#!/bin/sh

files=$(find . -name "*.c") for f in ${files} do     mv ${f} ${f}.org     ./cconvert ${f}.org > ${f} done files=$(find . -name '*.h') for f in ${files} do     mv ${f} ${f}.org     ./cconvert ${f}.org > ${f} done

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值