From d538315268dd84440ddb6dc482672df095e19258 Mon Sep 17 00:00:00 2001 From: Francesc Elies Date: Mon, 9 Dec 2019 19:08:45 +0100 Subject: [PATCH] [python] classes are missing __repr__ methods (#2437) * [python] classes don't have a __repr__ method * [python] removes unused import --- addons/cppcheckdata.py | 112 ++++++++++++++++++++++++++++++++++++++++- addons/misra.py | 24 +++++++++ 2 files changed, 135 insertions(+), 1 deletion(-) diff --git a/addons/cppcheckdata.py b/addons/cppcheckdata.py index 78f837cc8..1bab87891 100755 --- a/addons/cppcheckdata.py +++ b/addons/cppcheckdata.py @@ -10,7 +10,6 @@ from xml.etree import ElementTree import argparse from fnmatch import fnmatch import json -import os import sys @@ -42,6 +41,13 @@ class Directive: self.file = element.get('file') self.linenr = int(element.get('linenr')) + def __repr__(self): + attrs = ["str", "file", "linenr"] + return "{}({})".format( + "Directive", + ", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs)) + ) + class ValueType: """ @@ -76,6 +82,15 @@ class ValueType: else: self.pointer = 0 + def __repr__(self): + attrs = ["type", "sign", "bits", "typeScopeId", "originalTypeName", + "constness", "constness", "pointer"] + return "{}({})".format( + "ValueType", + ", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs)) + ) + + def setId(self, IdMap): self.typeScope = IdMap[self.typeScopeId] @@ -254,6 +269,21 @@ class Token: self.linenr = int(element.get('linenr')) self.column = int(element.get('column')) + def __repr__(self): + attrs = ["Id", "str", "next", "previous", "scopeId", + "scope", "isName", "isUnsigned", "isSigned", + "isNumber", "isInt", "isFloat", "isString", "strlen", + "isChar", "isOp", "isArithmeticalOp", "isComparisonOp", + "isLogicalOp", "isExpandedMacro", "linkId", "link", + "varId", "variableId", "variable", "functionId", "function", + "valuesId", "values", "valueType", "typeScopeId", "typeScope", + "astParentId", "astParent", "astOperand1Id", "astOperand1", + "astOperand2Id", "astOperand2", "file", "linenr", "column"] + return "{}({})".format( + "Token", + ", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs)) + ) + def setId(self, IdMap): self.scope = IdMap[self.scopeId] self.link = IdMap[self.linkId] @@ -325,6 +355,15 @@ class Scope: self.isExecutable = (self.type in ('Function', 'If', 'Else', 'For', 'While', 'Do', 'Switch', 'Try', 'Catch', 'Unconditional', 'Lambda')) + def __repr__(self): + attrs = ["Id", "className", "functionId", "function", + "bodyStartId", "bodyStart", "bodyEndId", "bodyEnd", + "nestedInId", "nestedIn", "type", "isExecutable"] + return "{}({})".format( + "Scope", + ", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs)) + ) + def setId(self, IdMap): self.bodyStart = IdMap[self.bodyStartId] self.bodyEnd = IdMap[self.bodyEndId] @@ -374,6 +413,14 @@ class Function: for arg in element: self.argumentId[int(arg.get('nr'))] = arg.get('variable') + def __repr__(self): + attrs = ["Id", "tokenDefId", "name", "type", "isVirtual", + "isImplicitlyVirtual", "isStatic", "argument", "argumentId"] + return "{}({})".format( + "Function", + ", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs)) + ) + def setId(self, IdMap): for argnr, argid in self.argumentId.items(): self.argument[argnr] = IdMap[argid] @@ -452,6 +499,17 @@ class Variable: if self.constness: self.constness = int(self.constness) + def __repr__(self): + attrs = ["Id", "nameTokenId", "nameToken", "typeStartTokenId", + "typeStartToken", "typeEndTokenId", "typeEndToken", + "access", "scopeId", "scope", "isArgument", "isArray", + "isClass", "isConst", "isGlobal", "isExtern", "isLocal", + "isPointer", "isReference", "isStatic", "constness", ] + return "{}({})".format( + "Variable", + ", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs)) + ) + def setId(self, IdMap): self.nameToken = IdMap[self.nameTokenId] self.typeStartToken = IdMap[self.typeStartTokenId] @@ -519,12 +577,27 @@ class ValueFlow: if element.get('inconclusive'): self.inconclusive = True + def __repr__(self): + attrs = ["intvalue", "tokvalue", "floatvalue", "containerSize", + "condition", "valueKind", "inconclusive"] + return "{}({})".format( + "Value", + ", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs)) + ) + def __init__(self, element): self.Id = element.get('id') self.values = [] for value in element: self.values.append(ValueFlow.Value(value)) + def __repr__(self): + attrs = ["Id", "values"] + return "{}({})".format( + "ValueFlow", + ", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs)) + ) + class Suppression: """ @@ -549,6 +622,13 @@ class Suppression: self.lineNumber = element.get('lineNumber') self.symbolName = element.get('symbolName') + def __repr__(self): + attrs = ['errorId' , "fileName", "lineNumber", "symbolName"] + return "{}({})".format( + "Suppression", + ", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs)) + ) + def isMatch(self, file, line, message, errorId): if ((self.fileName is None or fnmatch(file, self.fileName)) and (self.lineNumber is None or line == self.lineNumber) @@ -655,6 +735,14 @@ class Configuration: for variable in arguments: variable.setId(IdMap) + def __repr__(self): + attrs = ["name", "directives", "tokenlist", + "scopes", "functions", "variables", "valueflow"] + return "{}({})".format( + "Configuration", + ", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs)) + ) + class Platform: """ @@ -688,6 +776,14 @@ class Platform: self.long_long_bit = int(platformnode.get('long_long_bit')) self.pointer_bit = int(platformnode.get('pointer_bit')) + def __repr__(self): + attrs = ["name", "char_bit", "short_bit", "int_bit", + "long_bit", "long_long_bit", "pointer_bit"] + return "{}({})".format( + "Platform", + ", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs)) + ) + class Standards: """ @@ -705,6 +801,13 @@ class Standards: self.cpp = standardsnode.find("cpp").get("version") self.posix = standardsnode.find("posix") is not None + def __repr__(self): + attrs = ["c", "cpp", "posix"] + return "{}({})".format( + "Standards", + ", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs)) + ) + class CppcheckData: """ @@ -781,6 +884,13 @@ class CppcheckData: if cfgnode.tag == 'dump': self.configurations.append(Configuration(cfgnode)) + def __repr__(self): + attrs = ["configurations", ] + return "{}({})".format( + "CppcheckData", + ", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs)) + ) + # Get function arguments def getArgumentsRecursive(tok, arguments): diff --git a/addons/misra.py b/addons/misra.py index 1569a852e..1220bf73a 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -538,6 +538,14 @@ class Define: self.args = res.group(1).split(',') self.expansionList = res.group(2) + def __repr__(self): + attrs = ["args", "expansionList"] + return "{}({})".format( + "Define", + ", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs)) + ) + + def getAddonRules(): """Returns dict of MISRA rules handled by this addon.""" @@ -682,6 +690,13 @@ class MisraSettings(object): if args.no_summary: self.show_summary = False + def __repr__(self): + attrs = ["verify", "quiet", "show_summary", "verify"] + return "{}({})".format( + "MisraSettings", + ", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs)) + ) + class MisraChecker: @@ -726,6 +741,15 @@ class MisraChecker: self.stdversion = stdversion + def __repr__(self): + attrs = ["settings", "verify_expected", "verify_actual", "violations", + "ruleTexts", "suppressedRules", "dumpfileSuppressions", + "filePrefix", "suppressionStats", "stdversion"] + return "{}({})".format( + "MisraChecker", + ", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs)) + ) + def get_num_significant_naming_chars(self, cfg): if cfg.standards and cfg.standards.c == "c99": return 63