Changeset 11653d2154f46ac5076effeccc5e8a9db11cbdc8

Show
Ignore:
Timestamp:
03/14/2008 02:35:12 PM (10 months ago)
Author:
Robin Luckey <robin@Tangier.local>
git-committer:
Robin Luckey <robin@Tangier.local> 1205530512 -0700
git-parent:

[1311be2abb56f15be166183c6135216546c35833], [629f9cab359caeb39a81c40233f3369d904a7629]

git-author:
Robin Luckey <robin@Tangier.local> 1205530512 -0700
Message:

Merge branch 'cncpp'

Conflicts:

ext/ohcount_native/generator.rb
lib/ohcount/detector.rb

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • ext/ohcount_native/generator.rb

    r1311be2 r11653d2  
    2727      boo = PythonMonoglot.new("boo") 
    2828      clearsilver = CMonoglot.new("clearsilver", '#',              nil,                true,  true) 
    29       cncpp = CMonoglot.new("cncpp",             '//',             [e('/*'), e('*/')], true,  true) 
    30       csharp = CMonoglot.new("csharp",           '//',             [e('/*'), e('*/')], true,  true) 
     29      c = CMonoglot.new("c",                     '//',             [e('/*'), e('*/')], true,  true) 
     30      cpp = CMonoglot.new("cpp",                 '//',             [e('/*'), e('*/')], true,  true) 
     31      csharp = CMonoglot.new("csharp",           '//',             [e('/*'), e('*/')], true,  false) 
    3132      css = CMonoglot.new("css",                  nil,             [e('/*'), e('*/')], false,  false) 
    3233      dylan = CMonoglot.new("dylan",             '//',             nil,                true,  false) 
     
    7475        boo , 
    7576        clearsilver , 
    76         cncpp , 
     77        c , 
     78        cpp , 
    7779        csharp , 
    7880        css , 
  • ext/ohcount_native/ruby_binding.c

    r101d05d r629f9ca  
    105105 *   # Print each line to the console, labeled as code or comments 
    106106 *   buffer = File.read("helloworld.c") 
    107  *   results = Ohcount::parse(buffer, 'cncpp') do |language, semantic, line| 
     107 *   results = Ohcount::parse(buffer, 'c') do |language, semantic, line| 
    108108 *     puts "#{semantic.to_s} #{line}" 
    109109 *   end 
     
    113113 *   # Print total lines of code 
    114114 *   buffer = File.read("helloworld.c") 
    115  *   results = Ohcount::parse(buffer, 'cncpp') 
     115 *   results = Ohcount::parse(buffer, 'c') 
    116116 *   results.each do |result| 
    117117 *     puts "Lines of #{result.name} code: #{ result.code.split("\n").size }" 
  • lib/ohcount/detector.rb

    r1311be2 r11653d2  
    3939  # Example: 
    4040  # 
    41   #   # List all C/C++ files in the 'src' directory 
     41  #   # List all C files in the 'src' directory 
    4242  #   Dir.entries("src").each do |file| 
    4343  #     context = Ohcount::SimpleFileContext.new(file) 
    4444  #     polyglot = Ohcount::Detector.detect(context) 
    45   #     puts "#{file}" if polyglot == 'cncpp
     45  #     puts "#{file}" if polyglot == 'c
    4646  #   end 
    4747  # 
    4848  def self.detect(file_context) 
    4949    # start with extension 
    50     polyglot = EXTENSION_MAP[File.extname(file_context.filename).downcase] 
     50    polyglot = EXTENSION_MAP[File.extname(file_context.filename)] 
     51    polyglot = EXTENSION_MAP[File.extname(file_context.filename).downcase] unless polyglot 
    5152    case polyglot 
    5253    when String 
     
    107108    '.bat'  => "bat", 
    108109    '.boo'  => "boo", 
    109     '.c'    => "cncpp", 
    110     '.cc'   => "cncpp", 
    111     '.cpp'  => "cncpp", 
     110    '.c'    => "c", 
     111    '.C'    => "cpp", 
     112    '.cc'   => "cpp", 
     113    '.cpp'  => "cpp", 
    112114    '.css'  => "css", 
    113     '.c++'  => "cncpp", 
    114     '.cxx'  => "cncpp", 
     115    '.c++'  => "cpp", 
     116    '.cxx'  => "cpp", 
    115117    '.el'   => "emacslisp", 
    116118    #   '.cbl'  => "cobol", 
     
    133135    '.groovy'=> "groovy", 
    134136    '.h'    => :disambiguate_h_header, 
    135     '.hpp'  => "cncpp", 
    136     '.h++'  => "cncpp", 
     137    '.H'    => "cpp", 
     138    '.hpp'  => "cpp", 
     139    '.h++'  => "cpp", 
    137140    '.hs'   => "haskell", 
    138     '.hxx'  => "cncpp", 
    139     '.hh'   => "cncpp", 
     141    '.hxx'  => "cpp", 
     142    '.hh'   => "cpp", 
    140143    '.hrl'  => "erlang", 
    141144    '.htm'  => "html", 
     
    226229  end 
    227230 
    228   # For *.h files, differentiates C/C++ from Objective-C. 
     231  # For *.h files, differentiates C, C++ and Objective-C. 
    229232  # 
    230233  # This is done with a weighted heuristic that 
    231234  # scans the *.h file contents for Objective-C keywords, 
    232   # and also checks for the presence of matching *.m files. 
     235  # C++ keywords and C++ headers, and also checks for the 
     236  # presence of matching *.m files. 
    233237  def self.disambiguate_h_header(file_context) 
    234238    buffer = file_context.contents 
    235  
    236     objective_c = 0 
    237239 
    238240    # could it be realistically be objective_c ? are there any .m files at all? 
     
    243245      file_context.filenames.contains_pike_or_pmod = file_context.filenames.select { |a| a =~ /\.p(ike|mod)$/ }.any? 
    244246    end 
    245     unless file_context.filenames.contains_pike_or_pmod 
    246       return 'cncpp' unless file_context.filenames.contains_m 
    247     end 
    248247 
    249248    if file_context.filenames.contains_m 
     
    256255      # ok - it just might be objective_c, let's check contents for objective_c signatures 
    257256      objective_c_signatures = /(^\s*@interface)|(^\s*@end)/ 
    258       objective_c += lines_matching(buffer, objective_c_signatures) 
    259  
     257      objective_c = lines_matching(buffer, objective_c_signatures) 
    260258      return 'objective_c' if objective_c > 1 
    261259    end 
     
    268266    end 
    269267 
    270  
    271     return 'cncpp' 
     268    disambiguate_c_cpp(buffer) 
     269  end 
     270 
     271  # A map of headers that indicate C++, but that do not have C++-specific file 
     272  # extensions. This list is made from the Standard, plus Technical Report 1. 
     273  CPP_HEADERS_MAP = %w[ 
     274    algorithm 
     275    array 
     276    bitset 
     277    cassert 
     278    ccomplex 
     279    cctype 
     280    cerrno 
     281    cfenv 
     282    cfloat 
     283    cinttypes 
     284    ciso646 
     285    climits 
     286    clocale 
     287    cmath 
     288    csetjmp 
     289    csignal 
     290    cstdarg 
     291    cstdbool 
     292    cstddef 
     293    cstdint 
     294    cstdio 
     295    cstdlib 
     296    cstring 
     297    ctgmath 
     298    ctime 
     299    cwchar 
     300    cwctype 
     301    deque 
     302    exception 
     303    fstream 
     304    functional 
     305    iomanip 
     306    ios 
     307    iosfwd 
     308    iostream 
     309    istream 
     310    iterator 
     311    limits 
     312    list 
     313    locale 
     314    map 
     315    memory 
     316    new 
     317    numeric 
     318    ostream 
     319    queue 
     320    random 
     321    regex 
     322    set 
     323    sstream 
     324    stack 
     325    stdexcept 
     326    streambuf 
     327    string 
     328    system_error 
     329    tuple 
     330    type_traits 
     331    typeinfo 
     332    unordered_map 
     333    unordered_set 
     334    utility 
     335    valarray 
     336    vector 
     337    tr1/array 
     338    tr1/ccomplex 
     339    tr1/cctype 
     340    tr1/cfenv 
     341    tr1/cfloat 
     342    tr1/cinttypes 
     343    tr1/climits 
     344    tr1/cmath 
     345    tr1/complex 
     346    tr1/cstdarg 
     347    tr1/cstdbool 
     348    tr1/cstdint 
     349    tr1/cstdio 
     350    tr1/cstdlib 
     351    tr1/ctgmath 
     352    tr1/ctime 
     353    tr1/cwchar 
     354    tr1/cwctype 
     355    tr1/memory 
     356    tr1/random 
     357    tr1/regex 
     358    tr1/tuple 
     359    tr1/type_traits 
     360    tr1/unordered_map 
     361    tr1/unordered_set 
     362    tr1/utility 
     363  ].inject({}) { | h, k | h[k] = true ; h } 
     364 
     365  # A map of keywords that indicate C++. 
     366  CPP_KEYWORDS_MAP = %w[ 
     367    template 
     368    typename 
     369    class 
     370    namespace 
     371  ].inject({}) { | h, k | h[k] = true ; h } 
     372 
     373  # For *.h files that we know aren't Objective-C, differentiates C and C++. 
     374  # 
     375  # This is done with a weighted heuristic that 
     376  # scans the *.h file contents for C++ keywords and C++ headers. 
     377  def self.disambiguate_c_cpp(buffer) 
     378    # Look for C++ headers 
     379    return 'cpp' if extract_c_cpp_headers(buffer).detect do | header | 
     380      EXTENSION_MAP[File.extname(header)] == 'cpp' or CPP_HEADERS_MAP.include? header 
     381    end 
     382 
     383    # Look for C++ keywords. This could check for comments, but doesn't. 
     384    return 'cpp' if buffer.find do | line | 
     385      line.split(/\W/).find do | word | 
     386        CPP_KEYWORDS_MAP.include? word 
     387      end 
     388    end 
     389 
     390    # Nothing to suggest C++ 
     391    'c' 
     392  end 
     393 
     394  # Return a list of files included in a C or C++ source file. 
     395  def self.extract_c_cpp_headers(buffer) 
     396    buffer.map do | line | 
     397      m = line.match(/^#\s*include\s+[<"](.*)[>"]/) and m[1] 
     398    end.find_all { | a | a } 
    272399  end 
    273400 
  • lib/ohcount/sloc_info.rb

    r16a63b7 r11653d2  
    4545      'bat'           => {:nice_name => 'DOS batch script' , :category => 0}, 
    4646      'boo'           => {:nice_name => 'Boo'              , :category => 0}, 
    47       'cncpp'         => {:nice_name => 'C/C++'            , :category => 0}, 
     47      'c'             => {:nice_name => 'C'                , :category => 0}, 
     48      'cpp'           => {:nice_name => 'C++'              , :category => 0}, 
    4849      'clearsilver'   => {:nice_name => 'ClearSilver'      , :category => 0}, 
    4950      'csharp'        => {:nice_name => 'C#'               , :category => 0}, 
  • test/unit/c_test.rb

    rcb242a3 r11653d2  
    44 
    55  def test_comments 
    6     lb = [Ohcount::LanguageBreakdown.new("cncpp", "", "//comment", 0)] 
    7     assert_equal lb, Ohcount::parse(" //comment", "cncpp") 
     6    lb = [Ohcount::LanguageBreakdown.new("c", "", "//comment", 0)] 
     7    assert_equal lb, Ohcount::parse(" //comment", "c") 
    88  end 
    99 
    1010  def test_empty_comments 
    11     lb = [Ohcount::LanguageBreakdown.new("cncpp", "","//\n", 0)] 
    12     assert_equal lb, Ohcount::parse(" //\n", "cncpp") 
     11    lb = [Ohcount::LanguageBreakdown.new("c", "","//\n", 0)] 
     12    assert_equal lb, Ohcount::parse(" //\n", "c") 
    1313  end 
    1414 
    1515 
    1616  def test_block_comment 
    17     lb = [Ohcount::LanguageBreakdown.new("cncpp", "","/*c*/", 0)] 
    18     assert_equal lb, Ohcount::parse("/*c*/", "cncpp") 
     17    lb = [Ohcount::LanguageBreakdown.new("c", "","/*c*/", 0)] 
     18    assert_equal lb, Ohcount::parse("/*c*/", "c") 
    1919  end 
    2020 
  • test/unit/detector_test.rb

    rac2c8db r629f9ca  
    3838 
    3939  def test_detect_polyglot 
    40     assert_equal "cncpp", do_detect("foo.c") 
     40    assert_equal "c", do_detect("foo.c") 
     41    assert_equal "c", do_detect("uses_no_cpp.h") 
     42    assert_equal "cpp", do_detect("uses_cpp_headers.h") 
     43    assert_equal "cpp", do_detect("uses_cpp_stdlib_headers.h") 
     44    assert_equal "cpp", do_detect("uses_cpp_keywords.h") 
    4145    assert_equal "ruby", do_detect("foo.rb") 
    4246    assert_equal "matlab", do_detect("foo_matlab.m", ["foo_matlab.m", "bar.m", "README"]) 
     
    5357 
    5458  def test_upper_case_extensions 
    55     assert_equal "cncpp", do_detect("foo_upper_case.C") 
     59    assert_equal "cpp", do_detect("foo_upper_case.C") 
    5660    assert_equal "ruby", do_detect("foo_upper_case.RB") 
    5761  end 
  • test/unit/diff_test.rb

    r101d05d r629f9ca  
    2525    sloc_infos = Ohcount.diff_files(src_dir + 'diff2_old.c', src_dir + 'diff2_new.c') 
    2626 
    27     c = Ohcount::SlocInfo.new('cncpp') 
     27    c = Ohcount::SlocInfo.new('c') 
    2828    c.code_added,     c.code_removed     = [1,1] 
    2929    c.comments_added, c.comments_removed = [1,1]