计算 NLOC 的简单脚本?
您知道一个计算 NLOC(netto 代码行数)的简单脚本吗? 该脚本应该计算 C 代码的行数。 它不应该计算空行或仅带有大括号的行。 但它也不需要过于精确。
Do you know a simple script to count NLOCs (netto lines of code). The script should count lines of C Code. It should not count empty lines or lines with just braces. But it doesn't need to be overly exact either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
我会使用 awk & 来做到这一点 cpp(预处理器)& 厕所。 awk 删除所有大括号和空格,预处理器删除所有注释,wc 计算行数:
如果您想包含注释:
I would do that using awk & cpp (preprocessor) & wc . awk removes all braces and blanks, the preprocessor removes all comments and wc counts the lines:
If you want to have comments included:
在网上查找 NLOC,我发现大部分是“未注释的代码行”。
您没有指定是否必须跳过注释...
因此,如果我坚持您当前的消息,Perl 中的以下一行应该可以完成这项工作:
我可以扩展它来处理行注释:
或者
处理块注释可能稍微棘手一些(我不是 Perl 专家!)。
[编辑] 明白了...第一部分可能可以改进(更短)。 实验很有趣。
PS.:我使用双引号是因为我在 Windows 上测试过......
Looking NLOC on the Net, I found mostly "Non-commented lines of code".
You don't specify if comments must be skipped...
So if I stick to your current message, the following one-liner in Perl should do the job:
I can extend it to handle line comments:
or perhaps
Handling block comments is slightly more tricky (I am not a Perl expert!).
[EDIT] Got it... First part can be probably improved (shorter). Was fun to experiment with.
PS.: I use double quotes because I tested on Windows...
查看 Visual Studio 的 DPack 插件。 它有任何解决方案/项目的统计报告。
Check out DPack plugin for Visual Studio. It has a stats report for any solution/project.
不是脚本,但您可以尝试这个命令行开源工具:NLOC
Not a script, but you can try this command-line open source tool: NLOC
源监视器是免费软件源分析软件。 它是 Windows 应用程序,但也可以使用命令行参数运行。
它可以分析 C++、C、C#、VB.NET、Java、Delphi、Visual Basic (VB6) 或 HTML。
Source monitor is freeware source analysis software. It is windows application but it also can be run with parameters from command line.
It can analyze C++, C, C#, VB.NET, Java, Delphi, Visual Basic (VB6) or HTML.
Ohloh 提供免费的Ohcount 用于计算代码行数和注释行数。
Ohloh offers the free Ohcount which counts lines of code and comments.
如果注释还可以,标准的unix工具就足够了:
If the comments can still be in, the standard unix tool are sufficent:
SLOCCOunt 不是一个简单的脚本,它的功能远远超出您的需要。 然而,它是已经提到的 Ohcount 和 NLOC 的强大替代品。 :)
SLOCCOunt is not a simple script and does much more than what you need. However, it is a powerful alternative to the already mentioned Ohcount and NLOC. :)
我通常只是这样做:
仅当您的空行确实是空的(没有空格)时才有效。 对我来说足够了。
I usually just do this:
Works only if your empty lines are really empty (no spaces). Sufficient for me.
Locmetrics 效果很好。
Locmetrics works well.
这是一个简单的 Perl 脚本 eLOC.pl:
Here's a simple Perl script eLOC.pl:
以下脚本将获取给定目录中与模式匹配的所有文件的计数。
# 脚本开始
var str 文件
var str dir
set $files = "*.cpp" # <====================== 在此处设置文件名模式。
set $dir = "C:/myproject" # <====================== 在此处设置您的项目目录。
# 获取变量fileList中的文件列表。
var str 文件列表
查找 -rn 文件($files) dir($dir) > $fileList
# 声明变量,我们将在其中保存单个文件的计数。
var int c # 所有行
var int nb # 非空行
# 声明变量,我们将在其中保存所有文件的总计数。
var int Totalc # 所有行的总和
var int Totalnb # 所有非空白行的总和
# 声明我们将存储文件计数的变量。
var int fileCount
# 我们将在下面存储当前正在处理的文件的名称。
var str file
# 逐个浏览 $fileList 文件。
while ($fileList<>"")
做
# 提取下一个文件。
lex "1" $fileList >$file
# 检查这是否是平面文件。 我们对目录不感兴趣。
af $file >null # 我们不想看到输出。
# 我们只想设置 $ftype 变量。
if ($ftype=="f")
做
# 是的,这是一个平面文件。
完成
endif
完成
显示总和
echo "************************************ ********************************************************** **************************************************”
echo "所有行总数:\t" $totalc ",\t非空白行总数:\t" $totalnb ", 文件总数:" $fileCount
回声“**************************************************** ********************************************************** ********************************"
# END OF SCRIPT
如果您想要年份修改的文件中的行数仅 2008 年,添加 ($fmtime >= "2008") 等。
如果您没有 biterscripting,请从 .com 获取。
The following script will get count of all file matching a pattern in a given directory.
# START OF SCRIPT
var str files
var str dir
set $files = "*.cpp" # <===================== Set your file name pattern here.
set $dir = "C:/myproject" # <===================== Set your project directory here.
# Get the list of files in variable fileList.
var str fileList
find -rn files($files) dir($dir) > $fileList
# Declare variables where we will save counts of individual files.
var int c # all lines
var int nb # non-blank lines
# Declare variables where we will save total counts for all files.
var int totalc # sum-total of all lines
var int totalnb # sum-total of all non-blank lines
# Declare variable where we will store file count.
var int fileCount
# We will store the name of the file we are working on currently, in the following.
var str file
# Go thru the $fileList one by one file.
while ($fileList<>"")
do
# Extract the next file.
lex "1" $fileList >$file
# Check if this is a flat file. We are not interested in directories.
af $file >null # We don't want to see the output.
# We only want to set the $ftype variable.
if ($ftype=="f")
do
# Yes, this is a flat file.
done
endif
done
Show sum-totals
echo "**********************************************************************************************************************************"
echo "Total Count of all lines:\t" $totalc ",\tTotal Count of non-blank lines:\t" $totalnb ", Total files: " $fileCount
echo "**********************************************************************************************************************************"
# END OF SCRIPT
If you want line counts in files modified in year 2008 only, add ($fmtime >= "2008"), etc.
If you don't have biterscripting, get it from .com .
这不是一个简单的脚本,但是 CCCC(C 和 C++ 代码计数器)已经存在了一段时间并且可以工作对我来说很棒。
Not a simple script, but CCCC (C and C++ Code Counter) has been around for a while and it works great for me.
我有一个名为
scc
的程序,它会删除 C 注释(和 C++ 注释,尽管在 C99 中它们是相同的)。 应用该过滤器来删除空白行,如果需要,还可以删除仅包含左大括号和右大括号的行,以生成行数。 我已经在内部项目中使用过它 - 不需要打折开/闭大括号。 这些脚本更加复杂,比较了存储在 ClearCase 中的一个重要项目的两个不同版本的源代码。 他们还对添加和删除的文件以及从公共文件中添加和删除的行等进行了统计。不计算大括号会产生很大的差异:
因此,您的规则下有 144 行; 208 计算左支撑线和右支撑线; 271 计算一切。
让我知道您是否需要
scc
代码(发送电子邮件至 gmail dot com 的第一个点最后一个)。 它是 13 KB 的 gzip 压缩 tar 文件,包括手册页、酷刑测试和一些库文件。@litb 评论说“
cpp -fpreprocessed -P file
”处理剥离评论。 大部分都是这样。 但是,当我在压力测试中运行它时
对于 SCC,它在(在我看来)不应该的情况下抱怨:
当 GCC 4.3.2 中的 CPP 处理此问题时,它会抱怨(警告):
C99 标准的第 5.1.1.2 节翻译阶段说:
因此,在我看来,CPP 对示例文本中的第二阶段处理不当。 或者,至少,警告不是我想要的 - 构造是有效的 C,并且警告是否合理并不是不言而喻的。
当然,这是一种边缘情况,并且允许额外的警告。 但这会惹恼我的生活。 如果我没有自己的、可能更好的工具来完成这项工作,那么使用 '
cpp -fpreprocessed -P
' 就可以了 - 这是我抱怨的极端边缘情况(而且,认为存在问题的可能性大于没有问题的可能性可能是合理的——尽管更好的启发式方法会观察到该行被拼接并且结果是合法的单字符常量,因此如果出现以下情况,则应抑制投诉; result 不是一个合法的单字符常量,那么应该产生投诉(在我的测试用例中 - 无可否认,这是一个酷刑测试 - CPP 产生了 13 个问题,大部分与我所抱怨的问题有关,其中 SCC 正确地产生了 2 个问题。)(我观察到,“
-P
”设法抑制了省略该选项时出现的输出中的“#line
”指令。)I have a program called
scc
that strips C comments (and C++ comments, though with C99 they're the same). Apply that plus a filter to remove blank lines and, if so desired, lines containing just open and close braces, to generate the line counts. I've used that on internal projects - not needed to discount open/close braces. Those scripts were more complex, comparing the source code for two different versions of a substantial project stored in ClearCase. They also did statistics on files added and removed, and on lines added and removed from common files, etc.Not counting braces makes quite a difference:
So, 144 lines under your rules; 208 counting open and close brace lines; 271 counting everything.
Lemme know if you want the code for
scc
(send email to first dot last at gmail dot com). It's 13 KB of gzipped tar file including man page, torture test, and some library files.@litb commented that '
cpp -fpreprocessed -P file
' handles stripping ofcomments. It mostly does. However, when I run it on the stress test
for SCC, it complains when (in my opinion) it should not:
When the CPP from GCC 4.3.2 processes this, it complains (warns):
Section 5.1.1.2 Translation Phases of the C99 standard says:
Consequently, in my view, CPP is mishandling phase two in the example text. Or, at least, the warning is not what I want - the construct is valid C and it is not self-evident that the warning is warranted.
Granted, it is an edge case, and extra warnings are permitted. But it would annoy the living daylights out of me. If I didn't have my own, possibly better tool for the job, then using '
cpp -fpreprocessed -P
' would do - it is an extreme edge case that I'm complaining about (and, it might be legitimate to argue that it is more likely that there is a problem than not -- though a better heuristic would observe that the line was spliced and the result was a legitimate single character constant and therefore the complaint should be suppressed; if the result was not a legitimate single character constant, then the complaint should be produced. (On my test case - admittedly a torture test - CPP yields 13 problems, mostly related to the one I'm complaining about, where SCC correctly yields 2.)(I observe that the '
-P
' manages to suppress a '#line
' directive in the output that appears when the option is omitted.)