Skip to content

Commit f87cd98

Browse files
jubnzvdanmar
authored andcommitted
misra.py: Fix false negative for rule 10.1 (#2449)
This will close Trac 9489.
1 parent 36f3694 commit f87cd98

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

addons/misra.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1021,11 +1021,16 @@ def misra_10_1(self, data):
10211021
e2 = getEssentialTypeCategory(token.astOperand2)
10221022
if not e1 or not e2:
10231023
continue
1024-
if token.str in ['<<', '>>']:
1024+
if token.str in ('<<', '>>'):
10251025
if e1 != 'unsigned':
10261026
self.reportError(token, 10, 1)
10271027
elif e2 != 'unsigned' and not token.astOperand2.isNumber:
10281028
self.reportError(token, 10, 1)
1029+
elif token.str in ('~', '&', '|', '^'):
1030+
e1_et = getEssentialType(token.astOperand1)
1031+
e2_et = getEssentialType(token.astOperand2)
1032+
if e1_et == 'char' and e2_et == 'char':
1033+
self.reportError(token, 10, 1)
10291034

10301035
def misra_10_4(self, data):
10311036
op = {'+', '-', '*', '/', '%', '&', '|', '^', '+=', '-=', ':'}

addons/test/misra/misra-test.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,16 +206,33 @@ void misra_9_5() {
206206
int x[] = {[0]=23}; // 9.5
207207
}
208208

209+
typedef char misra_10_1_char_t;
210+
#define MISRA_10_1_CHAR char
209211
void misra_10_1(uint8_t u, char c1, char c2) {
210212
int32_t i;
211213
char c;
212214
enum { E1 = 1 };
213215
i = 3 << 1; // 10.1
214216
i = (u & u) << 4; // no-warning
215-
c = c1 & c2; // FIXME: This is not compliant to "10.1". Trac #9489
217+
c = c1 & c2; // 10.1
216218
c = c1 << 1; // 10.1
217219
i = c1 > c2; // no-warning
218220
i = E1 + i; // no-warning
221+
222+
char ch1 = 'a';
223+
char ch2 = 'b';
224+
char ch3;
225+
ch3 = ch1 & ch2; // 10.1
226+
227+
misra_10_1_char_t ct1 = 'a';
228+
misra_10_1_char_t ct2 = 'b';
229+
misra_10_1_char_t ct3;
230+
ct3 = ct1 & ct2; // 10.1
231+
232+
MISRA_10_1_CHAR cd1 = 'a';
233+
MISRA_10_1_CHAR cd2 = 'b';
234+
MISRA_10_1_CHAR cd3;
235+
cd3 = cd1 & cd2; // 10.1
219236
}
220237

221238
void misra_10_4(u32 x, s32 y) {

0 commit comments

Comments
 (0)