5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Source lines of code(SLOC)を数える

Posted at

はじめに

ソースコード行数;Source lines of code(SLOC)を数える方法を紹介します。
busyboxのソースコードを例にしてSLOCを計測してみます。
PC OSは Ubuntu 14.04です。

cloc

clocをinstallします。clocはPerl script fileです。

console
sudo apt-get install cloc

busybox-1.26.2を解析します。次のような結果になりました。

console
$ cloc busybox-1.26.2
    2056 text files.
    1657 unique files.                                          
    1145 files ignored.

http://cloc.sourceforge.net v 1.60  T=17.35 s (50.8 files/s, 15274.9 lines/s)
--------------------------------------------------------------------------------
Language                      files          blank        comment           code
--------------------------------------------------------------------------------
C                               631          24595          53365         158590
Bourne Shell                    163           1317           1278           7419
C/C++ Header                     60           1155           2176           5370
HTML                              9            420             32           3934
C++                               1            166             62           1197
make                              5            306            441            900
yacc                              1             93             20            570
Perl                              3             97            166            315
lex                               1             40             12            303
NAnt scripts                      1             84              0            260
Bourne Again Shell                4             47             86            203
IDL                               1              0              0             33
awk                               1              2              8             30
--------------------------------------------------------------------------------
SUM:                            881          28322          57646         179124
--------------------------------------------------------------------------------

言語別の集計になります。ファイル数、空白、コメント、コード別に行数が集計されています。

"--by-file" option の結果です。

console
$ cloc --by-file busybox-1.26.2
    2056 text files.
    1657 unique files.                                          
    1145 files ignored.

http://cloc.sourceforge.net v 1.60  T=16.65 s (52.9 files/s, 15924.0 lines/s)
----------------------------------------------------------------------------------------------------------------------
File                                                                               blank        comment           code
----------------------------------------------------------------------------------------------------------------------
busybox-1.26.2/shell/ash.c                                                          1153           1969          10476
(...)
busybox-1.26.2/arch/i386/Makefile                                                      1              5              1
----------------------------------------------------------------------------------------------------------------------
SUM:                                                                               28322          57646         179124
----------------------------------------------------------------------------------------------------------------------

"--diff" optionの実行結果です。

console
$ cloc --diff busybox-1.26.2 busybox-1.27.2
    2056 text files.
    2101 text files.s
    2303 files ignored.                                         

http://cloc.sourceforge.net v 1.60  T=101.38 s (0.0 files/s, 0.0 lines/s)
--------------------------------------------------------------------------------
Language                      files          blank        comment           code
--------------------------------------------------------------------------------
(...)
C++
 same                             1              0             62           1197
 modified                         0              0              0              0
 added                            0              0              0              0
 removed                          0              0              0              0
(...)
C/C++ Header
 same                            52              0           2162           5316
 modified                         8              0              5             33
 added                            5            169            298            673
 removed                          0              0              9             21
(...)
C
 same                           389              0          51434         155066
 modified                       227              0            574           1237
 added                           38           1305           3978           8354
 removed                         15            295           1357           2287
-------------------------------------------------------------------------------
SUM:
 same                           609              0          55690         175471
 modified                       256              0            586           1306
 added                           48           1515           4322           9190
 removed                         16            301           1370           2347
-------------------------------------------------------------------------------

sample

ソースコード例を使ってclocの動作を確認します。

test.c
  1 #if 0
  2 const int rodata = 10;
  3 int data = 20;
  4 int bss = 0;
  5 #endif
  6 
  7 /*-
  8  * main function
  9  */
 10 int main(int argc, char* argv[])
 11 {
 12     return 0; /* no count comment */
 13 }
 14 
console
$ cloc test.c
       1 text file.
       1 unique file.                              
       0 files ignored.

http://cloc.sourceforge.net v 1.60  T=0.02 s (60.8 files/s, 850.7 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
C                                1              2              3              9
-------------------------------------------------------------------------------
$ cloc test.c
       1 text file.
       1 unique file.                              
       0 files ignored.

http://cloc.sourceforge.net v 1.60  T=0.02 s (57.8 files/s, 808.7 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
C                                1              2              3              9
-------------------------------------------------------------------------------

codeは9です。
コメントだけの行、空白はカウントされません。
プリプロセッサ命令はカウントされます。
#if 0はカウントされます。

5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?