Add check for MISRA-C 2012 rule 4.2 - Trigraphs should not be used - as well as some test cases. (#2253)

This commit is contained in:
Mathias Schmid 2019-10-09 08:26:05 +02:00 committed by Daniel Marjamäki
parent c04e25e936
commit 1df97ac5c8
2 changed files with 21 additions and 0 deletions

View File

@ -776,6 +776,16 @@ class MisraChecker:
else:
self.reportError(token, 4, 1)
def misra_4_2(self, rawTokens):
for token in rawTokens:
if (token.str[0] != '"') or (token.str[-1] != '"'):
continue
# Check for trigraph sequence as defined by ISO/IEC 9899:1999
for sequence in ['??=', '??(', '??/', '??)', '??\'', '??<', '??!', '??>', '??-']:
if sequence in token.str[1:-1]:
# First trigraph sequence match, report error and leave loop.
self.reportError(token, 4, 2)
break
def misra_5_1(self, data):
long_vars = {}
@ -2395,6 +2405,7 @@ class MisraChecker:
if cfgNumber == 1:
self.misra_3_1(data.rawTokens)
self.misra_4_1(data.rawTokens)
self.misra_4_2(data.rawTokens)
self.misra_5_1(cfg)
self.misra_5_2(cfg)
self.misra_5_3(cfg)

View File

@ -101,6 +101,16 @@ void misra_4_1()
(void)printf("\x41" "g");
}
const char *s42_1 = "String containing trigraphs ??-??-??"; // 4.2
const char *s42_2 = "String containing trigraph???=preceeded by questionmark"; // 4.2
const char *s42_3 = "No trigraph?(?'?)";
void misra_4_2()
{
(void)printf("??=Trigraph\n"); // 4.2
(void)printf("No?/Trigraph\n");
}
extern int misra_5_3_var_hides_var______31x;
void misra_5_3_var_hides_function_31x (void) {}
enum misra_5_3_Enum {