Subversion Repositories tendra.SVN

Rev

Rev 2 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2 Rev 7
Line -... Line 1...
-
 
1
/*
-
 
2
 * Copyright (c) 2002-2006 The TenDRA Project <http://www.tendra.org/>.
-
 
3
 * All rights reserved.
-
 
4
 *
-
 
5
 * Redistribution and use in source and binary forms, with or without
-
 
6
 * modification, are permitted provided that the following conditions are met:
-
 
7
 *
-
 
8
 * 1. Redistributions of source code must retain the above copyright notice,
-
 
9
 *    this list of conditions and the following disclaimer.
-
 
10
 * 2. Redistributions in binary form must reproduce the above copyright notice,
-
 
11
 *    this list of conditions and the following disclaimer in the documentation
-
 
12
 *    and/or other materials provided with the distribution.
-
 
13
 * 3. Neither the name of The TenDRA Project nor the names of its contributors
-
 
14
 *    may be used to endorse or promote products derived from this software
-
 
15
 *    without specific, prior written permission.
-
 
16
 *
-
 
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
-
 
18
 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
-
 
19
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
-
 
20
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
-
 
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
-
 
22
 * EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-
 
23
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-
 
24
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-
 
25
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-
 
26
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-
 
27
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
 
28
 *
-
 
29
 * $Id$
-
 
30
 */
1
/*
31
/*
2
    		 Crown Copyright (c) 1997
32
    		 Crown Copyright (c) 1997
3
    
33
 
4
    This TenDRA(r) Computer Program is subject to Copyright
34
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
35
    owned by the United Kingdom Secretary of State for Defence
6
    acting through the Defence Evaluation and Research Agency
36
    acting through the Defence Evaluation and Research Agency
7
    (DERA).  It is made available to Recipients with a
37
    (DERA).  It is made available to Recipients with a
8
    royalty-free licence for its use, reproduction, transfer
38
    royalty-free licence for its use, reproduction, transfer
9
    to other parties and amendment for any purpose not excluding
39
    to other parties and amendment for any purpose not excluding
10
    product development provided that any such use et cetera
40
    product development provided that any such use et cetera
11
    shall be deemed to be acceptance of the following conditions:-
41
    shall be deemed to be acceptance of the following conditions:-
12
    
42
 
13
        (1) Its Recipients shall ensure that this Notice is
43
        (1) Its Recipients shall ensure that this Notice is
14
        reproduced upon any copies or amended versions of it;
44
        reproduced upon any copies or amended versions of it;
15
    
45
 
16
        (2) Any amended version of it shall be clearly marked to
46
        (2) Any amended version of it shall be clearly marked to
17
        show both the nature of and the organisation responsible
47
        show both the nature of and the organisation responsible
18
        for the relevant amendment or amendments;
48
        for the relevant amendment or amendments;
19
    
49
 
20
        (3) Its onward transfer from a recipient to another
50
        (3) Its onward transfer from a recipient to another
21
        party shall be deemed to be that party's acceptance of
51
        party shall be deemed to be that party's acceptance of
22
        these conditions;
52
        these conditions;
23
    
53
 
24
        (4) DERA gives no warranty or assurance as to its
54
        (4) DERA gives no warranty or assurance as to its
25
        quality or suitability for any purpose and DERA accepts
55
        quality or suitability for any purpose and DERA accepts
26
        no liability whatsoever in relation to any use to which
56
        no liability whatsoever in relation to any use to which
27
        it may be put.
57
        it may be put.
28
*/
58
*/
Line 41... Line 71...
41
/*
71
/*
42
    CHECKING CONTROL FLAG
72
    CHECKING CONTROL FLAG
43
 
73
 
44
    This flag may be set to true to temporarily disable the checks in
74
    This flag may be set to true to temporarily disable the checks in
45
    this module.
75
    this module.
46
*/
76
*/
47
 
77
 
48
int suppress_quality = 0 ;
78
int suppress_quality = 0;
49
 
79
 
50
 
80
 
51
/*
81
/*
52
    TABLE OF OPERATIONS
82
    TABLE OF OPERATIONS
53
 
83
 
54
    This table gives the list of operations checked by check_paren.  They
84
    This table gives the list of operations checked by check_paren.  They
55
    are listed in order of decreasing precedence.  Calls to check_paren
85
    are listed in order of decreasing precedence.  Calls to check_paren
56
    give an entry point into this table beyond which checks are applied.
86
    give an entry point into this table beyond which checks are applied.
57
    For example, left shift operations are checked from PAREN_PLUS which
87
    For example, left shift operations are checked from PAREN_PLUS which
58
    detects 'a << b + c', 'a << b - c', 'a + b << c' and 'a - b << c'.
88
    detects 'a << b + c', 'a << b - c', 'a + b << c' and 'a - b << c'.
59
*/
89
*/
60
 
90
 
61
static struct {
91
static struct {
62
    unsigned tag ;
92
	unsigned tag;
63
    int op ;
93
	int op;
64
} paren_ops [] = {
94
} paren_ops[] = {
65
    { exp_or_tag, lex_or_H1 },		/* PAREN_OR */
95
	{ exp_or_tag, lex_or_H1 },	/* PAREN_OR */
66
    { exp_xor_tag, lex_xor_H1 },		/* PAREN_XOR */
96
	{ exp_xor_tag, lex_xor_H1 },	/* PAREN_XOR */
67
    { exp_and_tag, lex_and_H1 },		/* PAREN_AND */
97
	{ exp_and_tag, lex_and_H1 },	/* PAREN_AND */
68
    { exp_compare_tag, lex_eq },	/* PAREN_EQUALITY */
98
	{ exp_compare_tag, lex_eq },	/* PAREN_EQUALITY */
69
    { exp_test_tag, lex_eq },		/* PAREN_RELATION */
99
	{ exp_test_tag, lex_eq },	/* PAREN_RELATION */
70
    { exp_plus_tag, lex_plus },		/* PAREN_PLUS */
100
	{ exp_plus_tag, lex_plus },	/* PAREN_PLUS */
71
    { exp_minus_tag, lex_minus }	/* PAREN_MINUS */
101
	{ exp_minus_tag, lex_minus }	/* PAREN_MINUS */
72
} ;
102
};
73
 
103
 
74
 
104
 
75
/*
105
/*
76
    FIND AN EXPRESSION TAG
106
    FIND AN EXPRESSION TAG
77
 
107
 
78
    This routine finds the tag associated with the expression e for unusual
108
    This routine finds the tag associated with the expression e for unusual
79
    parenthesis analysis etc.  Note that an integer constant expression may
109
    parenthesis analysis etc.  Note that an integer constant expression may
80
    have been evaluated to give a simple integer constant, however the top
110
    have been evaluated to give a simple integer constant, however the top
81
    level operation giving rise to this constant is held in the etag field.
111
    level operation giving rise to this constant is held in the etag field.
82
*/
112
*/
83
 
113
 
84
static unsigned exp_tag
114
static unsigned
85
    PROTO_N ( ( e ) )
-
 
86
    PROTO_T ( EXP e )
115
exp_tag (EXP e)
87
{
116
{
88
    unsigned tag = TAG_exp ( e ) ;
117
	unsigned tag = TAG_exp(e);
89
    if ( tag == exp_int_lit_tag ) {
118
	if (tag == exp_int_lit_tag) {
90
	/* Allow for evaluated constants */
119
		/* Allow for evaluated constants */
91
	tag = DEREF_unsigned ( exp_int_lit_etag ( e ) ) ;
120
		tag = DEREF_unsigned(exp_int_lit_etag(e));
92
    }
121
	}
93
    return ( tag ) ;
122
	return (tag);
94
}
123
}
95
 
124
 
96
 
125
 
97
/*
126
/*
98
    CHECK FOR DUBIOUS SHIFTS AND BIT OPERATIONS
127
    CHECK FOR DUBIOUS SHIFTS AND BIT OPERATIONS
99
 
128
 
100
    For various pairs of operations, op1 and op2, the resolution of the
129
    For various pairs of operations, op1 and op2, the resolution of the
101
    expression 'a op1 b op2 c' to '( a op1 b ) op2 c' or 'a op1 ( b op2 c )'
130
    expression 'a op1 b op2 c' to '( a op1 b ) op2 c' or 'a op1 ( b op2 c )'
102
    is not obvious.  This routine checks whether the expression 'a op b'
131
    is not obvious.  This routine checks whether the expression 'a op b'
103
    is of this form, checking the operations starting from position n in
132
    is of this form, checking the operations starting from position n in
104
    the table paren_ops.
133
    the table paren_ops.
105
*/
134
*/
106
 
135
 
107
void check_paren
136
void
108
    PROTO_N ( ( n, op, a, b ) )
-
 
109
    PROTO_T ( int n X int op X EXP a X EXP b )
137
check_paren(int n, int op, EXP a, EXP b)
110
{
138
{
111
    if ( !suppress_quality ) {
139
	if (!suppress_quality) {
112
	int i ;
140
		int i;
113
 
141
 
114
	/* Check first operand */
142
		/* Check first operand */
115
	unsigned tag = exp_tag ( a ) ;
143
		unsigned tag = exp_tag(a);
116
	for ( i = n ; i < array_size ( paren_ops ) ; i++ ) {
144
		for (i = n; i < array_size(paren_ops); i++) {
117
	    if ( tag == paren_ops [i].tag ) {
145
			if (tag == paren_ops[i].tag) {
118
		int op1 = paren_ops [i].op ;
146
				int op1 = paren_ops[i].op;
-
 
147
				if (op1 == lex_eq) {
119
		if ( op1 == lex_eq ) op1 = op_token ( a, op1 ) ;
148
					op1 = op_token(a, op1);
-
 
149
				}
120
		report ( crt_loc, ERR_expr_paren_left ( op1, op ) ) ;
150
				report(crt_loc, ERR_expr_paren_left(op1, op));
121
		break ;
151
				break;
122
	    }
152
			}
123
	}
153
		}
124
 
154
 
125
	/* Check second operand */
155
		/* Check second operand */
126
	tag = exp_tag ( b ) ;
156
		tag = exp_tag(b);
127
	for ( i = n ; i < array_size ( paren_ops ) ; i++ ) {
157
		for (i = n; i < array_size(paren_ops); i++) {
128
	    if ( tag == paren_ops [i].tag ) {
158
			if (tag == paren_ops[i].tag) {
129
		int op2 = paren_ops [i].op ;
159
				int op2 = paren_ops[i].op;
-
 
160
				if (op2 == lex_eq) {
130
		if ( op2 == lex_eq ) op2 = op_token ( b, op2 ) ;
161
					op2 = op_token(b, op2);
-
 
162
				}
131
		report ( crt_loc, ERR_expr_paren_right ( op, op2 ) ) ;
163
				report(crt_loc, ERR_expr_paren_right(op, op2));
132
		break ;
164
				break;
133
	    }
165
			}
-
 
166
		}
134
	}
167
	}
135
    }
-
 
136
    return ;
168
	return;
137
}
169
}
138
 
170
 
139
 
171
 
140
/*
172
/*
141
    CHECK FOR DUBIOUS RELATIONS
173
    CHECK FOR DUBIOUS RELATIONS
142
 
174
 
143
    This routine checks for dubious relations such as 'a < b < c' which
175
    This routine checks for dubious relations such as 'a < b < c' which
144
    do not have their mathematical meaning.
176
    do not have their mathematical meaning.
145
*/
177
*/
146
 
178
 
147
void check_relation
179
void
148
    PROTO_N ( ( op, a, b ) )
-
 
149
    PROTO_T ( int op X EXP a X EXP b )
180
check_relation(int op, EXP a, EXP b)
150
{
181
{
151
    if ( !suppress_quality ) {
182
	if (!suppress_quality) {
152
	/* Check first operand */
183
		/* Check first operand */
153
	unsigned tag = exp_tag ( a ) ;
184
		unsigned tag = exp_tag(a);
154
	if ( tag == exp_compare_tag ) {
185
		if (tag == exp_compare_tag) {
155
	    int tst = op_token ( a, op ) ;
186
			int tst = op_token(a, op);
156
	    report ( crt_loc, ERR_expr_rel_paren ( tst, op ) ) ;
187
			report(crt_loc, ERR_expr_rel_paren(tst, op));
157
	} else if ( tag == exp_test_tag ) {
188
		} else if (tag == exp_test_tag) {
158
	    int tst = op_token ( a, op ) ;
189
			int tst = op_token(a, op);
159
	    report ( crt_loc, ERR_expr_rel_paren ( tst, op ) ) ;
190
			report(crt_loc, ERR_expr_rel_paren(tst, op));
160
	}
191
		}
161
 
192
 
162
	/* Check second operand */
193
		/* Check second operand */
163
	tag = exp_tag ( b ) ;
194
		tag = exp_tag(b);
164
	if ( tag == exp_compare_tag ) {
195
		if (tag == exp_compare_tag) {
165
	    int tst = op_token ( b, op ) ;
196
			int tst = op_token(b, op);
166
	    report ( crt_loc, ERR_expr_rel_paren ( op, tst ) ) ;
197
			report(crt_loc, ERR_expr_rel_paren(op, tst));
167
	} else if ( tag == exp_test_tag ) {
198
		} else if (tag == exp_test_tag) {
168
	    int tst = op_token ( b, op ) ;
199
			int tst = op_token(b, op);
169
	    report ( crt_loc, ERR_expr_rel_paren ( op, tst ) ) ;
200
			report(crt_loc, ERR_expr_rel_paren(op, tst));
-
 
201
		}
170
	}
202
	}
171
    }
-
 
172
    return ;
203
	return;
173
}
204
}
174
 
205
 
175
 
206
 
176
/*
207
/*
177
    CHECK FOR DUBIOUS LOGICAL EXPRESSIONS
208
    CHECK FOR DUBIOUS LOGICAL EXPRESSIONS
178
 
209
 
179
    The resolution of 'a && b || c' to '( a && b ) || c' is odd.  This
210
    The resolution of 'a && b || c' to '( a && b ) || c' is odd.  This
180
    routine checks a logical or expression to see if it is of this form.
211
    routine checks a logical or expression to see if it is of this form.
181
*/
212
*/
182
 
213
 
183
void check_logic
214
void
184
    PROTO_N ( ( a, b ) )
-
 
185
    PROTO_T ( EXP a X EXP b )
215
check_logic(EXP a, EXP b)
186
{
216
{
187
    if ( !suppress_quality ) {
217
	if (!suppress_quality) {
188
	if ( exp_tag ( a ) == exp_log_and_tag ) {
218
		if (exp_tag(a) == exp_log_and_tag) {
189
	    int op1 = lex_logical_Hand_H1 ;
219
			int op1 = lex_logical_Hand_H1;
190
	    int op2 = lex_logical_Hor_H1 ;
220
			int op2 = lex_logical_Hor_H1;
191
	    report ( crt_loc, ERR_expr_paren_left ( op1, op2 ) ) ;
221
			report(crt_loc, ERR_expr_paren_left(op1, op2));
192
	}
222
		}
193
	if ( exp_tag ( b ) == exp_log_and_tag ) {
223
		if (exp_tag(b) == exp_log_and_tag) {
194
	    int op1 = lex_logical_Hor_H1 ;
224
			int op1 = lex_logical_Hor_H1;
195
	    int op2 = lex_logical_Hand_H1 ;
225
			int op2 = lex_logical_Hand_H1;
196
	    report ( crt_loc, ERR_expr_paren_right ( op1, op2 ) ) ;
226
			report(crt_loc, ERR_expr_paren_right(op1, op2));
-
 
227
		}
197
	}
228
	}
198
    }
-
 
199
    return ;
229
	return;
200
}
230
}