OSDN Git Service

* jv-scan.c (help): Document --complexity.
authortromey <tromey@138bc75d-0d04-0410-961f-82ee72b054a4>
Mon, 20 Nov 2000 22:56:14 +0000 (22:56 +0000)
committertromey <tromey@138bc75d-0d04-0410-961f-82ee72b054a4>
Mon, 20 Nov 2000 22:56:14 +0000 (22:56 +0000)
(options): Added --complexity.
(flag_complexity): New global.
(main): Call `report'.
* parse-scan.y (complexity): New global.
(if_then_statement, if_then_else_statement,
if_then_else_statement_nsi, switch_block_statement_group,
while_expression, do_statement, for_begin, continue_statement,
throw_statement, catch_clause, finally, method_invocation,
conditional_and_expression, conditional_or_expression,
conditional_expression): Update complexity.
(reset_report): Reset complexity.
(report): New function.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@37595 138bc75d-0d04-0410-961f-82ee72b054a4

gcc/java/ChangeLog
gcc/java/jv-scan.c
gcc/java/parse-scan.y

index f1275cb..8d233cd 100644 (file)
@@ -1,5 +1,21 @@
 2000-11-20  Tom Tromey  <tromey@cygnus.com>
 
+       * jv-scan.c (help): Document --complexity.
+       (options): Added --complexity.
+       (flag_complexity): New global.
+       (main): Call `report'.
+       * parse-scan.y (complexity): New global.
+       (if_then_statement, if_then_else_statement,
+       if_then_else_statement_nsi, switch_block_statement_group,
+       while_expression, do_statement, for_begin, continue_statement,
+       throw_statement, catch_clause, finally, method_invocation,
+       conditional_and_expression, conditional_or_expression,
+       conditional_expression): Update complexity.
+       (reset_report): Reset complexity.
+       (report): New function.
+
+2000-11-20  Tom Tromey  <tromey@cygnus.com>
+
        * lex.c (yylex): Added STRICT_TK case.
        * parse.y (STRICT_TK): Added.
        * parse-scan.y (STRICT_TK): Added.
index a96b805..e1ef70a 100644 (file)
@@ -39,6 +39,7 @@ Boston, MA 02111-1307, USA.  */
 void fatal PARAMS ((const char *s, ...)) ATTRIBUTE_PRINTF_1 ATTRIBUTE_NORETURN;
 void warning PARAMS ((const char *s, ...)) ATTRIBUTE_PRINTF_1;
 void gcc_obstack_init PARAMS ((struct obstack *obstack));
+void report PARAMS ((void));
 
 static void usage PARAMS ((void)) ATTRIBUTE_NORETURN;
 static void help PARAMS ((void)) ATTRIBUTE_NORETURN;
@@ -61,6 +62,7 @@ char *exec_name;
 int flag_find_main = 0;
 int flag_dump_class = 0;
 int flag_list_filename = 0;
+int flag_complexity = 0;
 
 int pedantic = 0;
 
@@ -81,6 +83,7 @@ static struct option options[] =
   { "list-filename", no_argument,   &flag_list_filename, 1 },
   { "list-class", no_argument,      &flag_dump_class, 1 },
   { "encoding",  required_argument, NULL, OPT_ENCODING },
+  { "complexity", no_argument,     &flag_complexity, 1 },
   { NULL,        no_argument,       NULL, 0 }
 };
 
@@ -96,6 +99,7 @@ help ()
 {
   printf ("Usage: jv-scan [OPTION]... FILE...\n\n");
   printf ("Print useful information read from Java source files.\n\n");
+  printf ("  --complexity            Print cyclomatic complexity of input file\n");
   printf ("  --encoding NAME         Specify encoding of input file\n");
   printf ("  --print-main            Print name of class containing `main'\n");
   printf ("  --list-class            List all classes defined in file\n");
@@ -169,12 +173,12 @@ DEFUN (main, (argc, argv),
     }
 
   /* No flags? Do nothing */
-  if (!flag_find_main && !flag_dump_class)
+  if (! flag_find_main && ! flag_dump_class && ! flag_complexity)
     return 0;
 
   /* Check on bad usage */
-  if (flag_find_main && flag_dump_class)
-    fatal ("Options `--print-main' and `--list-class' can't be turned on at the same time");
+  if (flag_find_main + flag_dump_class + flag_complexity > 1)
+    fatal ("Only one of `--print-main', `--list-class', and `--complexity' allowed");
 
   if (output_file && !(out = fopen (output_file, "w")))
     fatal ("Can't open output file `%s'", output_file);
@@ -205,6 +209,7 @@ DEFUN (main, (argc, argv),
 
            java_init_lex (finput, encoding);
            yyparse ();
+           report ();
            if (ftell (out) != ft)
              fputc ('\n', out);
            ft = ftell (out);
index 9207b9f..20522da 100644 (file)
@@ -75,6 +75,9 @@ static int previous_output;
 /* Record modifier uses  */
 static int modifier_value;
 
+/* Record (almost) cyclomatic complexity.  */
+static int complexity; 
+
 /* Keeps track of number of bracket pairs after a variable declarator
    id.  */
 static int bracket_count; 
@@ -101,6 +104,8 @@ static void report_main_declaration PARAMS ((struct method_declarator *));
 static void push_class_context PARAMS ((const char *));
 static void pop_class_context PARAMS ((void));
 
+void report PARAMS ((void)); 
+
 #include "lex.h"
 #include "parse.h"
 %}
@@ -727,15 +732,17 @@ statement_expression:
 ;
 
 if_then_statement:
-       IF_TK OP_TK expression CP_TK statement
+       IF_TK OP_TK expression CP_TK statement { ++complexity; }
 ;
 
 if_then_else_statement:
        IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
+       { ++complexity; }
 ;
 
 if_then_else_statement_nsi:
        IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
+       { ++complexity; }
 ;
 
 switch_statement:
@@ -755,7 +762,7 @@ switch_block_statement_groups:
 ;
 
 switch_block_statement_group:
-       switch_labels block_statements
+       switch_labels block_statements { ++complexity; }
 ;
 
 
@@ -770,7 +777,7 @@ switch_label:
 ;
 
 while_expression:
-       WHILE_TK OP_TK expression CP_TK
+       WHILE_TK OP_TK expression CP_TK { ++complexity; }
 ;
 
 while_statement:
@@ -787,6 +794,7 @@ do_statement_begin:
 
 do_statement: 
        do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
+       { ++complexity; }
 ;
 
 for_statement:
@@ -804,7 +812,7 @@ for_header:
 ;
 
 for_begin:
-       for_header for_init
+       for_header for_init { ++complexity; }
 ;
 for_init:                      /* Can be empty */
 |      statement_expression_list
@@ -825,9 +833,11 @@ break_statement:
 |      BREAK_TK identifier SC_TK
 ;
 
+/* `continue' with a label is considered for complexity but ordinary
+   continue is not.  */
 continue_statement:
        CONTINUE_TK SC_TK
-|       CONTINUE_TK identifier SC_TK
+       |       CONTINUE_TK identifier SC_TK { ++complexity; }
 ;
 
 return_statement:
@@ -836,7 +846,7 @@ return_statement:
 ;
 
 throw_statement:
-       THROW_TK expression SC_TK
+       THROW_TK expression SC_TK { ++complexity; }
 ;
 
 synchronized_statement:
@@ -861,11 +871,11 @@ catches:
 ;
 
 catch_clause:
-       CATCH_TK OP_TK formal_parameter CP_TK block
+       CATCH_TK OP_TK formal_parameter CP_TK block { ++complexity; }
 ;
 
 finally:
-       FINALLY_TK block
+       FINALLY_TK block { ++complexity; }
 ;
 
 /* 19.12 Production from 15: Expressions  */
@@ -958,15 +968,18 @@ field_access:
 |      SUPER_TK DOT_TK identifier
 ;
 
+/* We include method invocation in the complexity measure on the
+   theory that most method calls are virtual and therefore involve a
+   decision point.  */
 method_invocation:
        name OP_TK CP_TK
-               { USE_ABSORBER; }
+               { USE_ABSORBER; ++complexity; }
 |      name OP_TK argument_list CP_TK
-               { USE_ABSORBER; }
-|      primary DOT_TK identifier OP_TK CP_TK
-|      primary DOT_TK identifier OP_TK argument_list CP_TK
-|      SUPER_TK DOT_TK identifier OP_TK CP_TK
-|      SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
+               { USE_ABSORBER; ++complexity; }
+|      primary DOT_TK identifier OP_TK CP_TK { ++complexity; }
+|      primary DOT_TK identifier OP_TK argument_list CP_TK { ++complexity; }
+|      SUPER_TK DOT_TK identifier OP_TK CP_TK { ++complexity; }
+|      SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK { ++complexity; }
 ;
 
 array_access:
@@ -1074,16 +1087,19 @@ inclusive_or_expression:
 conditional_and_expression:
        inclusive_or_expression
 |      conditional_and_expression BOOL_AND_TK inclusive_or_expression
+       { ++complexity; }
 ;
 
 conditional_or_expression:
        conditional_and_expression
 |      conditional_or_expression BOOL_OR_TK conditional_and_expression
+       { ++complexity; }
 ;
 
 conditional_expression:                /* Error handling here is weak */
        conditional_or_expression
 |      conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
+       { ++complexity; }
 ;
 
 assignment_expression:
@@ -1238,6 +1254,14 @@ report_main_declaration (declarator)
     }
 }
 
+void
+report ()
+{
+  extern int flag_complexity;
+  if (flag_complexity)
+    fprintf (out, "%s %d\n", input_filename, complexity);
+}
+
 /* Reset global status used by the report functions.  */
 
 void reset_report ()
@@ -1245,6 +1269,7 @@ void reset_report ()
   previous_output = 0;
   package_name = NULL;
   current_class = NULL;
+  complexity = 0;
 }
 
 void