Linux下C/C++开发辅助工具astyle、cppcheck和valgrind的使用

一、astyle


astyle(Artistic Style) is a source code indenter, formatter, and beautifier for the C, C++, C++/CLI, Objective‑C, C# and Java programming languages.

1 Linux下的安装使用

1.1 安装

  1. http://astyle.sourceforge.net/ ,解压、编译、安装;
  2. Ubunut 终端下安装:sudo apt-get install astyle。

1.2 使用


astyle --style=kr -p *.cpp *.h(我司代码规范要求,使用K&R代码风格,操作符两边插入空格)。

2 编写shell脚本批量格式化源码

#! /bin/bash
#批量格式化
for f in $(find . -name '*.c' -or -name '*.cpp' -or -name '*.h' -type f)
do
     astyle --style=kr -p $f
done

# 删除.orig文件
for f in $(find . -name '*.orig' -type f)
do
     rm $f
done

3 astyle使用帮助

                           Artistic Style 2.03
                         Maintained by: Jim Pattee
                       Original Author: Tal Davidson

Usage  :  astyle [options] Source1.cpp Source2.cpp  [...]
          astyle [options] < Original > Beautified

When indenting a specific file, the resulting indented file RETAINS the
original file-name. The original pre-indented file is renamed, with a
suffix of ".orig" added to the original filename.

Wildcards (* and ?) may be used in the filename.
A 'recursive' option can process directories recursively.

By default, astyle is set up to indent C/C++/C#/Java files, with four
spaces per indent, a maximal indentation of 40 spaces inside continuous
statements, a minimum indentation of eight spaces inside conditional
statements, and NO formatting options.

Option's Format:
----------------
    Long options (starting with '--') must be written one at a time.
    Short options (starting with '-') may be appended together.
    Thus, -bps4 is the same as -b -p -s4.

Default options file:
---------------------
    Artistic Style looks for a default options file in the
    following order:
    1. The contents of the ARTISTIC_STYLE_OPTIONS environment
       variable if it exists.
    2. The file called .astylerc in the directory pointed to by the
       HOME environment variable ( i.e. $HOME/.astylerc ).
    3. The file called astylerc in the directory pointed to by the
       USERPROFILE environment variable ( i.e. %USERPROFILE%\astylerc ).
    If a default options file is found, the options in this file
    will be parsed BEFORE the command-line options.
    Long options within the default option file may be written without
    the preliminary '--'.

Bracket Style Options:
----------------------
    --style=allman  OR  --style=ansi  OR  --style=bsd
	    OR  --style=break  OR  -A1
    Allman style formatting/indenting.
    Broken brackets.

    --style=java  OR  --style=attach  OR  -A2
    Java style formatting/indenting.
    Attached brackets.

    --style=kr  OR  --style=k&r  OR  --style=k/r  OR  -A3
    Kernighan & Ritchie style formatting/indenting.
    Linux brackets.

    --style=stroustrup  OR  -A4
    Stroustrup style formatting/indenting.
    Stroustrup brackets.

    --style=whitesmith  OR  -A5
    Whitesmith style formatting/indenting.
    Broken, indented brackets.
    Indented class blocks and switch blocks.

    --style=banner  OR  -A6
    Banner style formatting/indenting.
    Attached, indented brackets.
    Indented class blocks and switch blocks.

    --style=gnu  OR  -A7
    GNU style formatting/indenting.
    Broken brackets, indented blocks.

    --style=linux  OR  -A8
    Linux style formatting/indenting.
    Linux brackets, minimum conditional indent is one-half indent.

    --style=horstmann  OR  -A9
    Horstmann style formatting/indenting.
    Run-in brackets, indented switches.

    --style=1tbs  OR  --style=otbs  OR  -A10
    One True Brace Style formatting/indenting.
    Linux brackets, add brackets to all conditionals.

    --style=pico  OR  -A11
    Pico style formatting/indenting.
    Run-in opening brackets and attached closing brackets.
    Uses keep one line blocks and keep one line statements.

    --style=lisp  OR  -A12
    Lisp style formatting/indenting.
    Attached opening brackets and attached closing brackets.
    Uses keep one line statements.

Tab Options:
------------
    default indent option
    If no indentation option is set, the default
    option of 4 spaces per indent will be used.

    --indent=spaces=#  OR  -s#
    Indent using # spaces per indent. Not specifying #
    will result in a default of 4 spaces per indent.

    --indent=tab  OR  --indent=tab=#  OR  -t  OR  -t#
    Indent using tab characters, assuming that each
    indent is # spaces long. Not specifying # will result
    in a default assumption of 4 spaces per indent.

    --indent=force-tab=#  OR  -T#
    Indent using tab characters, assuming that each
    indent is # spaces long. Force tabs to be used in areas
    AStyle would prefer to use spaces.

    --indent=force-tab-x=#  OR  -xT#
    Allows the tab length to be set to a length that is different
    from the indent length. This may cause the indentation to be
    a mix of both spaces and tabs. This option sets the tab length.

Indentation options:
--------------------
    --indent-classes  OR  -C
    Indent 'class' blocks, so that the inner 'public:',
    'protected:' and 'private: headers are indented in
    relation to the class block.

    --indent-switches  OR  -S
    Indent 'switch' blocks, so that the inner 'case XXX:'
    headers are indented in relation to the switch block.

    --indent-cases  OR  -K
    Indent case blocks from the 'case XXX:' headers.
    Case statements not enclosed in blocks are NOT indented.

    --indent-namespaces  OR  -N
    Indent the contents of namespace blocks.

    --indent-labels  OR  -L
    Indent labels so that they appear one indent less than
    the current indentation level, rather than being
    flushed completely to the left (which is the default).

    --indent-preprocessor  OR  -w
    Indent multi-line #define statements.

    --indent-col1-comments  OR  -Y
    Indent line comments that start in column one.

    --min-conditional-indent=#  OR  -m#
    Indent a minimal # spaces in a continuous conditional
    belonging to a conditional header.
    The valid values are:
    0 - no minimal indent.
    1 - indent at least one additional indent.
    2 - indent at least two additional indents.
    3 - indent at least one-half an additional indent.
    The default value is 2, two additional indents.

    --max-instatement-indent=#  OR  -M#
    Indent a maximal # spaces in a continuous statement,
    relative to the previous line.
    The valid values are 40 thru 120.
    The default value is 40.

Padding options:
--------------------
    --break-blocks  OR  -f
    Insert empty lines around unrelated blocks, labels, classes, ...

    --break-blocks=all  OR  -F
    Like --break-blocks, except also insert empty lines 
    around closing headers (e.g. 'else', 'catch', ...).

    --pad-oper  OR  -p
    Insert space padding around operators.

    --pad-paren  OR  -P
    Insert space padding around parenthesis on both the outside
    and the inside.

    --pad-paren-out  OR  -d
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值