MCPcopy Create free account
hub / github.com/cppcheck-opensource/cppcheck / getForLoopCounterVariables

Function getForLoopCounterVariables

addons/misra.py:893–959  ·  view source on GitHub ↗

Return a set of Variable objects defined in ``for`` statement and satisfy requirements to loop counter term from section 8.14 of MISRA document.

(forToken, cfg)

Source from the content-addressed store, hash-verified

891 return False
892
893def getForLoopCounterVariables(forToken, cfg):
894 """ Return a set of Variable objects defined in ``for`` statement and
895 satisfy requirements to loop counter term from section 8.14 of MISRA
896 document.
897 """
898 if not forToken or forToken.str != 'for':
899 return None
900 tn = forToken.next
901 if not tn or tn.str != '(':
902 return None
903 vars_defined = set()
904 vars_initialized = set()
905 vars_exit = set()
906 vars_modified = set()
907 cur_clause = 1
908 te = tn.link
909 while tn and tn != te:
910 if tn.variable:
911 if cur_clause == 1 and tn.variable.nameToken == tn:
912 vars_defined.add(tn.variable)
913 elif cur_clause == 2:
914 vars_exit.add(tn.variable)
915 elif cur_clause == 3:
916 if tn.next and countSideEffectsRecursive(tn.next) > 0:
917 vars_modified.add(tn.variable)
918 elif tn.previous and tn.previous.str in ('++', '--'):
919 tn_ast = tn.astParent
920 if tn_ast and tn_ast == tn.previous:
921 vars_modified.add(tn.variable)
922 elif tn_ast and tn_ast.str == '.' and tn_ast.astOperand2 and tn_ast.astOperand2.variable:
923 vars_modified.add(tn_ast.astOperand2.variable)
924 if cur_clause == 1 and tn.isAssignmentOp:
925 var_token = tn.astOperand1
926 while var_token and var_token.str == '.':
927 var_token = var_token.astOperand2
928 if var_token and var_token.variable:
929 vars_initialized.add(var_token.variable)
930 if cur_clause == 1 and tn.isName and tn.next.str == '(':
931 function_args_in_init = getArguments(tn.next)
932 function_scope = get_function_scope(cfg, tn.function)
933 for arg_nr in range(len(function_args_in_init)):
934 init_arg = function_args_in_init[arg_nr]
935 if init_arg is None or not init_arg.isUnaryOp('&'):
936 continue
937 var_token = init_arg.astOperand1
938 while var_token and var_token.str == '.':
939 var_token = var_token.astOperand2
940 if var_token is None or var_token.variable is None:
941 continue
942 changed = False
943 if function_scope is None:
944 changed = True
945 elif tn.function is None:
946 changed = True
947 else:
948 function_body_start = function_scope.bodyStart
949 function_body_end = function_scope.bodyEnd
950 args = tn.function.argument[arg_nr + 1]

Callers 1

misra_14_2Method · 0.85

Calls 7

setFunction · 0.85
get_function_scopeFunction · 0.85
is_variable_changedFunction · 0.85
isUnaryOpMethod · 0.80
getArgumentsFunction · 0.70
addMethod · 0.45

Tested by

no test coverage detected