开发者

Lines of Code as a function of preprocessor definitions

开发者 https://www.devze.com 2023-03-01 10:18 出处:网络
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.

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.


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.

  -E  Stop after the preprocessing stage; do not run the compiler proper.
      The output is in the form of preprocessed source code, which is sent
      to the standard output.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号