LoginSignup
0
0

More than 5 years have passed since last update.

インクルードしているファイル一覧を作る(1)

Last updated at Posted at 2015-07-04

はじめに

ソースコードの依存関係調査をするにあたり、c、cpp、ヘッダファイルでインクルードしている箇所を抜き出すrubyスクリプトを作成。

環境

  • Windows8.1
  • ruby 2.1.6p336 (2015-04-13 revision 50298) [x64-mingw32]

スクリプト

  • depend_check.rb
require 'nkf'

CODES = {
  NKF::JIS   => 'stateless-ISO-2022-JP',
  NKF::EUC   => 'euc-jp',
  NKF::SJIS  => 'Shift_JIS',
  NKF::UTF8  => 'utf-8',
  NKF::BINARY => 'ascii',
  NKF::ASCII  => 'ascii',
  NKF::UNKNOWN => 'ascii',
  Encoding::EUCJP_MS => 'euc-jp',
  Encoding::CP51932  => 'euc-jp',
  Encoding::WINDOWS_31J => 'windows-31j',
}

def check(file)
  str = open(file) { |io| io.gets(nil) }
  if str.nil?
    '' # "EMPTY"
  else
    CODES.fetch(NKF.guess(str))
  end
end

def depend(fpath)
  list = []
  encoding = check(fpath)

  File.open(fpath, "r:#{encoding}") do |f|
    f.each_line do |line|
      line.chomp!
      if line =~ /^#include/
        x = line.split(/(\"|<|>)/)
        list << x[2]
      end
    end
  end
  list
end

file = ARGV[0]
exit(-1) if file == nil

depend(file).each do |x|
  puts x
end

まあ、とりあえずこんな感じで。

実際に次のようなコードを読み込ませてみる

> ruby depend_check.rb a.c
  • a.c
#include <stdio.h>
#include "a.h"
#include "include\b.h"
//#include "c.h"
#include "../c.h"
  • 結果
stdio.h
a.h
include\b.h
../c.h

これでは、いったいどこのヘッダファイルが参照されているのかわからない。
ちゃんと知るには、includeパスを与えてやる必要がある。

ひとまず、このスクリプトを再帰的に実行

  • depend_check.bat
@echo off
for /f "usebackq" %%i in (`dir /B /S *.c`) do (
    ruby depend_check.rb %%i
)

ひとまずこれでcファイルからインクルードしている一覧ができた。

まだこれでは使い物ならんな。
ということで、次回へ続く(予定)。

0
0
3

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
0
0