Commit 1535cc88 by Tom Tromey Committed by Tom Tromey

jv-scan.c (help): Document --complexity.

	* 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.

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