一、代码风格自动检查
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