diff --git a/addons/misra-test.c b/addons/misra-test.c index 199e43d17..f90a28846 100644 --- a/addons/misra-test.c +++ b/addons/misra-test.c @@ -30,6 +30,10 @@ void misra28() { register int x = 3; // 28 } +enum misra32 { A=1, B, C=10 }; // 32 +enum misra32_ok1 { A, B, C }; +enum misra32_ok2 { A=1, B, C }; + void misra33() { if (x && (y++ < 123)){} // 33 } diff --git a/addons/misra.py b/addons/misra.py index 59b51adeb..306a3a3cf 100644 --- a/addons/misra.py +++ b/addons/misra.py @@ -253,9 +253,47 @@ def misra31(data): return # 32 In an enumerator list the = construct shall not be used to explicitly initialise members other than the first unless it is used to initialise all items -# STATUS: TODO +# STATUS: Done def misra32(data): - return + for token in data.tokenlist: + if token.str != 'enum': + continue + + # Goto start '{' + tok = token.next + if tok and tok.isName: + tok = tok.next + if not tok or tok.str != '{': + continue + + # Parse enum body and remember which members are assigned + eqList = [] + eq = False + tok = tok.next + while tok and tok.str != '}': + if tok.str == '=': + eq = True + elif tok.str == ',': + eqList.append(eq) + eq = False + elif tok.link and (tok.str in ['(','[','{','<']): + tok = tok.link + tok = tok.next + eqList.append(eq) + #print(str(token.linenr) + ':' + str(eqList)) + + # is there error? + if len(eqList) <= 1: + continue + err = False + if eqList[0] and eqList[1]: + err = (False in eqList[1:]) + else: + err = (True in eqList[1:]) + if err: + reportError(token, 'style', '32 In an enumerator list the = construct shall not be used to explicitly initialise members other than the first unless it is used to initialise all items') + + # Operators # ---------