代码行数作为预处理器定义的函数
我正在开发的一个项目(使用C)有很多代码段,可以使用预处理器指令根据编译时配置包含或省略这些代码段。
我有兴趣估计不同配置在我的核心项目中添加或减少了多少行代码。换句话说,我想在某处编写一些 #define
和 #undef
行,并了解它们对 LOC 计数的影响。
我不熟悉 LOC 计数器,但从粗略搜索来看,似乎大多数易于使用的工具都没有这样做。我认为这不是一个困难的问题,只是一个相当不常见的衡量指标。
是否有现有的工具可以完成我正在寻找的任务,或者有一些简单的方法可以自己完成?排除注释和空行也是一个重要的优点。
A project I'm working on (in C) has a lot of sections of code that can be included or omitted based on compile-time configuration, using preprocessor directives.
I'm interested in estimating how many lines of code different configurations are adding to, or subtracting from, my core project. In other words, I'd like to write a few #define
and #undef
lines somewhere, and get a sense of what that does to the LOC count.
I'm not familiar with LOC counters, but from a cursory search, it doesn't seem like most of the easily-available tools do that. I'm assuming this isn't a difficult problem, but just a rather uncommon metric to measure.
Is there an existing tool that would do what I'm looking for, or some easy way to do it myself? Excluding comments and blank lines would be a major nice-to-have, too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过预处理器运行它。例如,在 gcc 下,使用选项
-E
,我相信,可以获得您想要的输出类型。Run it through a preprocessor. For example, under gcc, use the option
-E
, I believe, to get just the kind of output you seem to want.您可以从编译器获取预处理器输出,但这可能会产生其他不需要的副作用,例如扩展复杂的多行宏,以及以您意想不到的方式添加 LOC 计数。
为什么不编写自己的简单预处理器,并使用自己的包含/排除指令?您可以使它们变得非常容易解析,然后将代码通过此预处理器进行管道传输,然后将其发送到功能齐全的 LOC 计数器(如 CLOC)。
You could get the preprocessor output from your compiler, but this might have other unwanted side effects, like expanding complex multi-line macros, and adding to the LOC count in ways you didn't expect.
Why not write your own simple pre-processor, and use your own include/exclude directives? You can make them trivially simple to parse, and then pipe your code through this pre-processor before sending it to a full featured LOC counter like CLOC.