From e3872f9718d8942ad248918875e73d58886eacbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 14 Apr 2017 21:09:25 +0200 Subject: [PATCH] Misra: Add rule 18.8 --- addons/misra-test.c | 6 ++++++ addons/misra.py | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/addons/misra-test.c b/addons/misra-test.c index 7e16adcde..b80d1c079 100644 --- a/addons/misra-test.c +++ b/addons/misra-test.c @@ -184,3 +184,9 @@ void misra_17_8(int x) { void misra_18_5() { int *** p; // 18.5 } + +void misra_18_8(int x) { + int buf1[10]; + int buf2[sizeof(int)]; + int vla[x]; // 18.8 +} diff --git a/addons/misra.py b/addons/misra.py index 369769ba0..b41808160 100644 --- a/addons/misra.py +++ b/addons/misra.py @@ -122,6 +122,8 @@ def isConstantExpression(expr): return True if expr.isName: return False + if simpleMatch(expr.previous, 'sizeof ('): + return True if expr.astOperand1 and not isConstantExpression(expr.astOperand1): return False if expr.astOperand2 and not isConstantExpression(expr.astOperand2): @@ -532,6 +534,16 @@ def misra_18_5(data): if count > 2: reportError(var.nameToken, 18, 5) +def misra_18_8(data): + for var in data.variables: + if not var.isArray: + continue + # TODO Array dimensions are not available in dump + typetok = var.nameToken.next + if not typetok or typetok.str != '[': + continue + if not isConstantExpression(typetok.astOperand2): + reportError(var.nameToken, 18, 8) if '-verify' in sys.argv[1:]: VERIFY = True @@ -596,6 +608,7 @@ for arg in sys.argv[1:]: misra_17_6(data.rawTokens) misra_17_8(cfg) misra_18_5(cfg) + misra_18_8(cfg) if VERIFY: for expected in VERIFY_EXPECTED: