什么是最好的命令行工具来清理code? [英] What is the best command-line tool to clean up code?
问题描述
当我编写C - code仅我使用编辑器和gcc。我想知道如果任何人都可以提出一个很好的和简单的工具,会发现未使用的变量,函数声明,并且可能使一些优化技术。
When I'm writing C - code I solely use an editor and gcc. I was wondering if anyone could suggest a good and simple tool that will find unused variables, function declarations and possibly make some optimisations.
是否有人知道一个很好的工具?
Does anybody know a good tool?
推荐答案
由于丹Fego指出,海湾合作委员会可以捕获未使用的变量和未使用的静态函数。它通常不会查找未使用extern函数,因为它通常工作一个源文件在同一时间。
As Dan Fego pointed out, GCC can catch unused variables and unused static functions. It won't normally find unused extern functions as it normally works one source file at a time.
GCC(V4.3.2)有成百上千的选择。一个可能有助于为 - 合并
源文件放在不同的源文件里面的相同功能或变量名的习惯,只要你不合并( )。
GCC (v4.3.2) has hundreds if not thousands of options. One that might help is '--combine
' to combine source files (as long as you're not in the habit of putting the same function or variable names inside different source files).
选项' - 帮助
'告诉你更多;选项 - 帮助=优化
和 - 帮助=警告
每次给你几百行输出。这些警告包括:
The option '--help
' tells you more; the options '--help=optimizers
' and '--help=warnings
' each give you a couple hundred lines of output. The warnings include:
-Wunused This switch lacks documentation
-Wunused-function Warn when a function is unused
-Wunused-label This switch lacks documentation
-Wunused-macros Warn about macros defined in the main file that
are not used
-Wunused-parameter Warn when a function parameter is unused
-Wunused-value Warn when an expression value is unused
-Wunused-variable Warn when a variable is unused
的添加的:这是一个调用的脚本闪烁
,我用它来清理我的code。这是很老,因此它不会使用#!/ bin / sh的
符号的第一行,它说 $ *
$ @
,这两者应该是固定的,但也需要亟待解决。请注意,即使GCC 4.x中不再支持 -fwriteable串
选项,它仍然支持 -Wwrite串
选项,并具有价值。
Added: this is a script called glint
that I use to sanitize my code. It is quite old so it doesn't use the '#!/bin/sh
' notation for the first line and it says '$*
' instead of '"$@"
', both of which should be fixed, but neither needs to be fixed urgently. Note that even though GCC 4.x no longer supports the '-fwriteable-strings
' option, it still supports the '-Wwrite-strings
' option and that has value.
本脚本演示,你可以得到很多的里程从现有的工具与工作只是一个小数目。您可以配置只是它使用的每一种选择 - 尽管主要是通过环境,而不是命令行。当然,你可以添加额外的警告选项到命令行;你不能做的是去除除了通过环境predetermined选项。但没关系;他们在默认情况下有充分理由选择。这些天来,我可能会设置 GLINT_ANSI = -std = C99
'或修复脚本;我,因为我code相当严密到闪烁
强制执行的标准已经不使用它受到太多晚了。 (请注意, -o的/ dev / null的
'意味着你只能做一个文件在同一时间;!劈死修复)
This script demonstrates that you can get a lot of mileage out of existing tools with just a small amount of work. You can configure just about every option it uses - albeit mainly via the environment rather than the command line. Of course, you can add extra warning options to the command line; what you can't do is remove predetermined options except via the environment. But that's OK; they're chosen by default for good reasons. These days, I'd probably set 'GLINT_ANSI=-std=c99
' or fix the script; I've not been using it much of late since I code fairly closely to the standard that glint
enforces. (Note that the '-o /dev/null
' means that you can only do one file at a time; hack to fix!)
: "@(#)$Id: glint.sh,v 1.5 2002/08/09 21:40:52 jleffler Exp jleffler $"
#
# Use GCC as excruciatingly pedantic lint
# Not a complete replacement for lint -- it doesn't do inter-file checking.
# Now configurable via the environment.
# Use GLINT_EXTRA_FLAGS to set extra flags via the environment.
# NB: much Solaris code won't work with -undef enabled.
: ${GLINT_GCC:='gcc'}
: ${GLINT_ANSI='-ansi'}
: ${GLINT_FNO_COMMON='-fno-common'}
: ${GLINT_FSHORT_ENUMS='-fshort-enums'}
: ${GLINT_PEDANTIC='-pedantic'}
: ${GLINT_UNDEF='-undef'}
: ${GLINT_W='-W'}
: ${GLINT_WAGGREGATE_RETURN='-Waggregate-return'}
: ${GLINT_WALL='-Wall'}
: ${GLINT_WCAST_ALIGN='-Wcast-align'}
: ${GLINT_WCAST_QUAL='-Wcast-qual'}
: ${GLINT_WCONVERSION='-Wconversion'}
: ${GLINT_WMISSING_DECLARATIONS='-Wmissing-declarations'}
: ${GLINT_WREDUNDANT_DECLS='-Wredundant-decls'}
: ${GLINT_WMISSING_PROTOTYPES='-Wmissing-prototypes'}
: ${GLINT_WNESTED_EXTERNS='-Wnested-externs'}
: ${GLINT_WPOINTER_ARITH='-Wpointer-arith'}
: ${GLINT_WSHADOW='-Wshadow'}
: ${GLINT_WSTRICT_PROTOTYPES='-Wstrict-prototypes'}
: # ${GLINT_WTRADITIONAL='-Wtraditional'}
: ${GLINT_WWRITE_STRINGS='-Wwrite-strings'}
exec ${GLINT_GCC} \
${GLINT_ANSI} \
${GLINT_FNO_COMMON} \
${GLINT_FSHORT_ENUMS} \
${GLINT_PEDANTIC} \
${GLINT_UNDEF} \
${GLINT_WAGGREGATE_RETURN} \
${GLINT_WALL} \
${GLINT_WCAST_ALIGN} \
${GLINT_WCAST_QUAL} \
${GLINT_WCONVERSION} \
${GLINT_WMISSING_DECLARATIONS} \
${GLINT_WREDUNDANT_DECLS} \
${GLINT_WMISSING_PROTOTYPES} \
${GLINT_WNESTED_EXTERNS} \
${GLINT_WPOINTER_ARITH} \
${GLINT_WSHADOW} \
${GLINT_WSTRICT_PROTOTYPES} \
${GLINT_WTRADITIONAL} \
${GLINT_WWRITE_STRINGS} \
${GLINT_W} \
${GLINT_EXTRA_FLAGS} \
-o /dev/null -O4 -g -c $*
这篇关于什么是最好的命令行工具来清理code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!