Ticket #211: split-cncpp-into-c-cpp-1.patch

File split-cncpp-into-c-cpp-1.patch, 17.9 kB (added by ciaranm, 4 months ago)
  • a/ext/ohcount_native/generator.rb

    old new  
    2626      bat = LineCommentMonoglot.new("bat",        '^\\\\s*(?i)REM(?-i)') 
    2727      boo = PythonMonoglot.new("boo") 
    2828      clearsilver = CMonoglot.new("clearsilver", '#',              nil,                true,  true) 
    29       cncpp = CMonoglot.new("cncpp",             '//',             [e('/*'), e('*/')], true,  false) 
     29      c = CMonoglot.new("c",             '//',             [e('/*'), e('*/')], true,  false) 
     30      cpp = CMonoglot.new("cpp",             '//',             [e('/*'), e('*/')], true,  false) 
    3031      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) 
     
    7273        bat , 
    7374        boo , 
    7475        clearsilver , 
    75         cncpp , 
     76        c , 
     77        cpp , 
    7678        csharp , 
    7779        css , 
    7880        dylan , 
  • a/ext/ohcount_native/ruby_binding.c

    old new  
    104104 * 
    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 
    110110 * 
     
    112112 * 
    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 }" 
    118118 *   end 
  • a/lib/ohcount/detector.rb

    old new  
    3535  # 
    3636  # Example: 
    3737  # 
    38   #   # List all C/C++ files in the 'src' directory 
     38  #   # List all C files in the 'src' directory 
    3939  #   Dir.entries("src").each do |file| 
    4040  #     context = Ohcount::SimpleFileContext.new(file) 
    4141  #     polyglot = Ohcount::Detector.detect(context) 
    42   #     puts "#{file}" if polyglot == 'cncpp
     42  #     puts "#{file}" if polyglot == 'c
    4343  #   end 
    4444  # 
    4545  def self.detect(file_context) 
    4646    # start with extension 
    47     polyglot = EXTENSION_MAP[File.extname(file_context.filename).downcase] 
     47    polyglot = EXTENSION_MAP[File.extname(file_context.filename)] 
     48    polyglot = EXTENSION_MAP[File.extname(file_context.filename).downcase] unless polyglot 
    4849    case polyglot 
    4950    when String 
    5051      # simplest case 
     
    103104    '.bas'  => "visualbasic", 
    104105    '.bat'  => "bat", 
    105106    '.boo'  => "boo", 
    106     '.c'    => "cncpp", 
    107     '.cc'   => "cncpp", 
    108     '.cpp'  => "cncpp", 
     107    '.c'    => "c", 
     108    '.C'    => "cpp", 
     109    '.cc'   => "cpp", 
     110    '.cpp'  => "cpp", 
    109111    '.css'  => "css", 
    110     '.c++'  => "cncpp", 
    111     '.cxx'  => "cncpp", 
     112    '.c++'  => "cpp", 
     113    '.cxx'  => "cpp", 
    112114    '.el'   => "emacslisp", 
    113115    #   '.cbl'  => "cobol", 
    114116    #   '.cob'  => "cobol", 
     
    129131    '.frx'  => "visualbasic", 
    130132    '.groovy'=> "groovy", 
    131133    '.h'    => :disambiguate_h_header, 
    132     '.hpp'  => "cncpp", 
    133     '.h++'  => "cncpp", 
     134    '.H'    => "cpp", 
     135    '.hpp'  => "cpp", 
     136    '.h++'  => "cpp", 
    134137    '.hs'   => "haskell", 
    135     '.hxx'  => "cncpp", 
    136     '.hh'   => "cncpp", 
     138    '.hxx'  => "cpp", 
     139    '.hh'   => "cpp", 
    137140    '.hrl'  => "erlang", 
    138141    '.htm'  => "html", 
    139142    '.html' => "html", 
     
    216219    matlab > 0 ? 'matlab' : 'objective_c' 
    217220  end 
    218221 
    219   # For *.h files, differentiates C/C++ from Objective-C. 
     222  # For *.h files, differentiates C, C++ and Objective-C. 
    220223  # 
    221224  # This is done with a weighted heuristic that 
    222225  # scans the *.h file contents for Objective-C keywords, 
    223   # and also checks for the presence of matching *.m files. 
     226  # C++ keywords and C++ headers, and also checks for the 
     227  # presence of matching *.m files. 
    224228  def self.disambiguate_h_header(file_context) 
    225229    buffer = file_context.contents 
    226230 
     
    232236      file_context.filenames.extend(ContainsM) 
    233237      file_context.filenames.contains_m = file_context.filenames.select { |a| a =~ /\.m$/ }.any? 
    234238    end 
    235     return 'cncpp' unless file_context.filenames.contains_m 
     239    return disambiguate_c_cpp(buffer) unless file_context.filenames.contains_m 
    236240 
    237241    # if the dir contains a matching *.m file, likely objective_c 
    238242    if file_context.filename =~ /\.h$/ 
     
    244248    objective_c_signatures = /(^\s*@interface)|(^\s*@end)/ 
    245249    objective_c += lines_matching(buffer, objective_c_signatures) 
    246250 
    247     return objective_c > 1 ? 'objective_c' : 'cncpp' 
     251    return objective_c > 1 ? 'objective_c' : disambiguate_c_cpp(buffer) 
     252  end 
     253 
     254  # A map of headers that indicate C++, but that do not have C++-specific file 
     255  # extensions. This list is made from the Standard, plus Technical Report 1. 
     256  CPP_HEADERS_MAP = %w[ 
     257    algorithm 
     258    array 
     259    bitset 
     260    cassert 
     261    ccomplex 
     262    cctype 
     263    cerrno 
     264    cfenv 
     265    cfloat 
     266    cinttypes 
     267    ciso646 
     268    climits 
     269    clocale 
     270    cmath 
     271    csetjmp 
     272    csignal 
     273    cstdarg 
     274    cstdbool 
     275    cstddef 
     276    cstdint 
     277    cstdio 
     278    cstdlib 
     279    cstring 
     280    ctgmath 
     281    ctime 
     282    cwchar 
     283    cwctype 
     284    deque 
     285    exception 
     286    fstream 
     287    functional 
     288    iomanip 
     289    ios 
     290    iosfwd 
     291    iostream 
     292    istream 
     293    iterator 
     294    limits 
     295    list 
     296    locale 
     297    map 
     298    memory 
     299    new 
     300    numeric 
     301    ostream 
     302    queue 
     303    random 
     304    regex 
     305    set 
     306    sstream 
     307    stack 
     308    stdexcept 
     309    streambuf 
     310    string 
     311    system_error 
     312    tuple 
     313    type_traits 
     314    typeinfo 
     315    unordered_map 
     316    unordered_set 
     317    utility 
     318    valarray 
     319    vector 
     320    tr1/array 
     321    tr1/ccomplex 
     322    tr1/cctype 
     323    tr1/cfenv 
     324    tr1/cfloat 
     325    tr1/cinttypes 
     326    tr1/climits 
     327    tr1/cmath 
     328    tr1/complex 
     329    tr1/cstdarg 
     330    tr1/cstdbool 
     331    tr1/cstdint 
     332    tr1/cstdio 
     333    tr1/cstdlib 
     334    tr1/ctgmath 
     335    tr1/ctime 
     336    tr1/cwchar 
     337    tr1/cwctype 
     338    tr1/memory 
     339    tr1/random 
     340    tr1/regex 
     341    tr1/tuple 
     342    tr1/type_traits 
     343    tr1/unordered_map 
     344    tr1/unordered_set 
     345    tr1/utility 
     346  ].inject({}) { | h, k | h[k] = true ; h } 
     347 
     348  # A map of keywords that indicate C++. 
     349  CPP_KEYWORDS_MAP = %w[ 
     350    template 
     351    typename 
     352    class 
     353    namespace 
     354  ].inject({}) { | h, k | h[k] = true ; h } 
     355 
     356  # For *.h files that we know aren't Objective-C, differentiates C and C++. 
     357  # 
     358  # This is done with a weighted heuristic that 
     359  # scans the *.h file contents for C++ keywords and C++ headers. 
     360  def self.disambiguate_c_cpp(buffer) 
     361    # Look for C++ headers 
     362    return 'cpp' if extract_c_cpp_headers(buffer).detect do | header | 
     363      EXTENSION_MAP[File.extname(header)] == 'cpp' or CPP_HEADERS_MAP.include? header 
     364    end 
     365 
     366    # Look for C++ keywords. This could check for comments, but doesn't. 
     367    return 'cpp' if buffer.find do | line | 
     368      line.split(/\W/).find do | word | 
     369        CPP_KEYWORDS_MAP.include? word 
     370      end 
     371    end 
     372 
     373    # Nothing to suggest C++ 
     374    'c' 
     375  end 
     376 
     377  # Return a list of files included in a C or C++ source file. 
     378  def self.extract_c_cpp_headers(buffer) 
     379    buffer.map do | line | 
     380      m = line.match(/^#\s*include\s+[<"](.*)[>"]/) and m[1] 
     381    end.find_all { | a | a } 
    248382  end 
    249383 
    250384  # Tests whether the provided buffer contains binary or text content. 
  • a/lib/ohcount/sloc_info.rb

    old new  
    4444      'awk'           => {:nice_name => 'AWK'              , :category => 0}, 
    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}, 
    5051      'css'           => {:nice_name => 'CSS'              , :category => 1}, 
  • /dev/null

    old new  
     1#include <foo/foo.hh> 
     2 
     3foo::Foo get_foo(); 
     4 
  • /dev/null

    old new  
     1namespace monkey { 
     2  template <typename> struct Monkey; 
     3} 
     4 
  • /dev/null

    old new  
     1#include <string> 
     2 
     3std::string foo(); 
  • /dev/null

    old new  
     1#include <sys/types.h> 
     2#include <sys/stat.h> 
     3#include <unistd.h> 
     4 
     5int superstat(const char * path, struct stat * buf); 
  • /dev/null

    old new  
  • /dev/null

    old new  
     1#include <cmqc.h>      /* MQ API header file       */ 
     2#define NUMBEROFSELECTORS  2 
     3const MQHCONN Hconn = MQHC_DEF_HCONN; 
     4static void InquireGetAndPut(char   *Message, 
     5PMQHOBJ pHobj, 
     6char   *Object) 
     7{ 
     8MQLONG  SelectorCount = NUMBEROFSELECTORS; 
     9MQLONG  IntAttrCount  = NUMBEROFSELECTORS; 
     10MQLONG  CharAttrLength = 0; 
     11MQCHAR *CharAttrs ; 
     12MQLONG  SelectorsTable[NUMBEROFSELECTORS]; 
     13MQLONG  IntAttrsTable[NUMBEROFSELECTORS]; 
     14MQLONG  CompCode;             /* Completion code      */ 
     15MQLONG  Reason;               /* Qualifying reason    */ 
     16SelectorsTable[0] = MQIA_INHIBIT_GET; 
     17SelectorsTable[1] = MQIA_INHIBIT_PUT; 
     18MQINQ(Hconn, 
     19*pHobj, 
     20SelectorCount, 
     21SelectorsTable, 
     22IntAttrCount, 
     23IntAttrsTable, 
     24CharAttrLength, 
     25CharAttrs, 
     26&CompCode, 
     27&Reason); 
     28if (CompCode != MQCC_OK) 
     29{ 
     30sprintf(Message, MESSAGE_4_E, 
     31ERROR_IN_MQINQ, CompCode, Reason); 
     32SetMsg(Message); 
     33} 
     34else 
     35{ 
     36} /* end if CompCode */ 
  • /dev/null

    old new  
     1// 
     2/*      Declare local variables                       */ 
     3/*                                                    */ 
     4/* Number of selectors  */ 
     5/* Number of int attrs  */ 
     6/* Length of char attribute buffer  */ 
     7/* Character attribute buffer       */ 
     8/* attribute selectors  */ 
     9/* integer attributes   */ 
     10/*                                                    */ 
     11/*     Open the queue.  If successful, do the inquire */ 
     12/*     call.                                          */ 
     13/*                                                    */ 
     14/*                                                 */ 
     15/*   Initialize the variables for the inquire      */ 
     16/*   call:                                         */ 
     17/*    - Set SelectorsTable to the attributes whose */ 
     18/*      status is                                  */ 
     19/*       required                                  */ 
     20/*    - All other variables are already set        */ 
     21/*                                                 */ 
     22/*                                                 */ 
     23/*   Issue the inquire call                        */ 
     24/*     Test the output of the inquire call. If the */ 
     25/*     call failed, display an error message       */ 
     26/*     showing the completion code and reason code,*/ 
     27/*     otherwise display the status of the         */ 
     28/*     INHIBIT-GET and INHIBIT-PUT attributes      */ 
     29/*                                                 */ 
     30/* Process the changes */ 
  • /dev/null

    old new  
  • /dev/null

    old new  
    1 #include <cmqc.h>      /* MQ API header file       */ 
    2 #define NUMBEROFSELECTORS  2 
    3 const MQHCONN Hconn = MQHC_DEF_HCONN; 
    4 static void InquireGetAndPut(char   *Message, 
    5 PMQHOBJ pHobj, 
    6 char   *Object) 
    7 { 
    8 MQLONG  SelectorCount = NUMBEROFSELECTORS; 
    9 MQLONG  IntAttrCount  = NUMBEROFSELECTORS; 
    10 MQLONG  CharAttrLength = 0; 
    11 MQCHAR *CharAttrs ; 
    12 MQLONG  SelectorsTable[NUMBEROFSELECTORS]; 
    13 MQLONG  IntAttrsTable[NUMBEROFSELECTORS]; 
    14 MQLONG  CompCode;             /* Completion code      */ 
    15 MQLONG  Reason;               /* Qualifying reason    */ 
    16 SelectorsTable[0] = MQIA_INHIBIT_GET; 
    17 SelectorsTable[1] = MQIA_INHIBIT_PUT; 
    18 MQINQ(Hconn, 
    19 *pHobj, 
    20 SelectorCount, 
    21 SelectorsTable, 
    22 IntAttrCount, 
    23 IntAttrsTable, 
    24 CharAttrLength, 
    25 CharAttrs, 
    26 &CompCode, 
    27 &Reason); 
    28 if (CompCode != MQCC_OK) 
    29 { 
    30 sprintf(Message, MESSAGE_4_E, 
    31 ERROR_IN_MQINQ, CompCode, Reason); 
    32 SetMsg(Message); 
    33 } 
    34 else 
    35 { 
    36 } /* end if CompCode */ 
  • /dev/null

    old new  
    1 // 
    2 /*      Declare local variables                       */ 
    3 /*                                                    */ 
    4 /* Number of selectors  */ 
    5 /* Number of int attrs  */ 
    6 /* Length of char attribute buffer  */ 
    7 /* Character attribute buffer       */ 
    8 /* attribute selectors  */ 
    9 /* integer attributes   */ 
    10 /*                                                    */ 
    11 /*     Open the queue.  If successful, do the inquire */ 
    12 /*     call.                                          */ 
    13 /*                                                    */ 
    14 /*                                                 */ 
    15 /*   Initialize the variables for the inquire      */ 
    16 /*   call:                                         */ 
    17 /*    - Set SelectorsTable to the attributes whose */ 
    18 /*      status is                                  */ 
    19 /*       required                                  */ 
    20 /*    - All other variables are already set        */ 
    21 /*                                                 */ 
    22 /*                                                 */ 
    23 /*   Issue the inquire call                        */ 
    24 /*     Test the output of the inquire call. If the */ 
    25 /*     call failed, display an error message       */ 
    26 /*     showing the completion code and reason code,*/ 
    27 /*     otherwise display the status of the         */ 
    28 /*     INHIBIT-GET and INHIBIT-PUT attributes      */ 
    29 /*                                                 */ 
    30 /* Process the changes */ 
  • a/test/unit/c_test.rb

    old new  
    33class Ohcount::CTest < Ohcount::Test 
    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 
    2121  def test_comprehensive 
  • a/test/unit/detector_test.rb

    old new  
    3737  end 
    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"]) 
    4347    assert_equal "objective_c", do_detect("foo_objective_c.m", ["foo_objective_c.m", "bar.h", "README"]) 
     
    5256  end 
    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 
    5862 
  • a/test/unit/diff_test.rb

    old new  
    2424    src_dir = File.dirname(__FILE__) + '/../src_dir/' 
    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] 
    3030