Skip to content

Commit d538315

Browse files
FrancescEliesdanmar
authored andcommitted
[python] classes are missing __repr__ methods (#2437)
* [python] classes don't have a __repr__ method * [python] removes unused import
1 parent cbd9713 commit d538315

2 files changed

Lines changed: 135 additions & 1 deletion

File tree

addons/cppcheckdata.py

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import argparse
1111
from fnmatch import fnmatch
1212
import json
13-
import os
1413
import sys
1514

1615

@@ -42,6 +41,13 @@ def __init__(self, element):
4241
self.file = element.get('file')
4342
self.linenr = int(element.get('linenr'))
4443

44+
def __repr__(self):
45+
attrs = ["str", "file", "linenr"]
46+
return "{}({})".format(
47+
"Directive",
48+
", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs))
49+
)
50+
4551

4652
class ValueType:
4753
"""
@@ -76,6 +82,15 @@ def __init__(self, element):
7682
else:
7783
self.pointer = 0
7884

85+
def __repr__(self):
86+
attrs = ["type", "sign", "bits", "typeScopeId", "originalTypeName",
87+
"constness", "constness", "pointer"]
88+
return "{}({})".format(
89+
"ValueType",
90+
", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs))
91+
)
92+
93+
7994
def setId(self, IdMap):
8095
self.typeScope = IdMap[self.typeScopeId]
8196

@@ -254,6 +269,21 @@ def __init__(self, element):
254269
self.linenr = int(element.get('linenr'))
255270
self.column = int(element.get('column'))
256271

272+
def __repr__(self):
273+
attrs = ["Id", "str", "next", "previous", "scopeId",
274+
"scope", "isName", "isUnsigned", "isSigned",
275+
"isNumber", "isInt", "isFloat", "isString", "strlen",
276+
"isChar", "isOp", "isArithmeticalOp", "isComparisonOp",
277+
"isLogicalOp", "isExpandedMacro", "linkId", "link",
278+
"varId", "variableId", "variable", "functionId", "function",
279+
"valuesId", "values", "valueType", "typeScopeId", "typeScope",
280+
"astParentId", "astParent", "astOperand1Id", "astOperand1",
281+
"astOperand2Id", "astOperand2", "file", "linenr", "column"]
282+
return "{}({})".format(
283+
"Token",
284+
", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs))
285+
)
286+
257287
def setId(self, IdMap):
258288
self.scope = IdMap[self.scopeId]
259289
self.link = IdMap[self.linkId]
@@ -325,6 +355,15 @@ def __init__(self, element):
325355
self.isExecutable = (self.type in ('Function', 'If', 'Else', 'For', 'While', 'Do',
326356
'Switch', 'Try', 'Catch', 'Unconditional', 'Lambda'))
327357

358+
def __repr__(self):
359+
attrs = ["Id", "className", "functionId", "function",
360+
"bodyStartId", "bodyStart", "bodyEndId", "bodyEnd",
361+
"nestedInId", "nestedIn", "type", "isExecutable"]
362+
return "{}({})".format(
363+
"Scope",
364+
", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs))
365+
)
366+
328367
def setId(self, IdMap):
329368
self.bodyStart = IdMap[self.bodyStartId]
330369
self.bodyEnd = IdMap[self.bodyEndId]
@@ -374,6 +413,14 @@ def __init__(self, element):
374413
for arg in element:
375414
self.argumentId[int(arg.get('nr'))] = arg.get('variable')
376415

416+
def __repr__(self):
417+
attrs = ["Id", "tokenDefId", "name", "type", "isVirtual",
418+
"isImplicitlyVirtual", "isStatic", "argument", "argumentId"]
419+
return "{}({})".format(
420+
"Function",
421+
", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs))
422+
)
423+
377424
def setId(self, IdMap):
378425
for argnr, argid in self.argumentId.items():
379426
self.argument[argnr] = IdMap[argid]
@@ -452,6 +499,17 @@ def __init__(self, element):
452499
if self.constness:
453500
self.constness = int(self.constness)
454501

502+
def __repr__(self):
503+
attrs = ["Id", "nameTokenId", "nameToken", "typeStartTokenId",
504+
"typeStartToken", "typeEndTokenId", "typeEndToken",
505+
"access", "scopeId", "scope", "isArgument", "isArray",
506+
"isClass", "isConst", "isGlobal", "isExtern", "isLocal",
507+
"isPointer", "isReference", "isStatic", "constness", ]
508+
return "{}({})".format(
509+
"Variable",
510+
", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs))
511+
)
512+
455513
def setId(self, IdMap):
456514
self.nameToken = IdMap[self.nameTokenId]
457515
self.typeStartToken = IdMap[self.typeStartTokenId]
@@ -519,12 +577,27 @@ def __init__(self, element):
519577
if element.get('inconclusive'):
520578
self.inconclusive = True
521579

580+
def __repr__(self):
581+
attrs = ["intvalue", "tokvalue", "floatvalue", "containerSize",
582+
"condition", "valueKind", "inconclusive"]
583+
return "{}({})".format(
584+
"Value",
585+
", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs))
586+
)
587+
522588
def __init__(self, element):
523589
self.Id = element.get('id')
524590
self.values = []
525591
for value in element:
526592
self.values.append(ValueFlow.Value(value))
527593

594+
def __repr__(self):
595+
attrs = ["Id", "values"]
596+
return "{}({})".format(
597+
"ValueFlow",
598+
", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs))
599+
)
600+
528601

529602
class Suppression:
530603
"""
@@ -549,6 +622,13 @@ def __init__(self, element):
549622
self.lineNumber = element.get('lineNumber')
550623
self.symbolName = element.get('symbolName')
551624

625+
def __repr__(self):
626+
attrs = ['errorId' , "fileName", "lineNumber", "symbolName"]
627+
return "{}({})".format(
628+
"Suppression",
629+
", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs))
630+
)
631+
552632
def isMatch(self, file, line, message, errorId):
553633
if ((self.fileName is None or fnmatch(file, self.fileName))
554634
and (self.lineNumber is None or line == self.lineNumber)
@@ -655,6 +735,14 @@ def __init__(self, confignode):
655735
for variable in arguments:
656736
variable.setId(IdMap)
657737

738+
def __repr__(self):
739+
attrs = ["name", "directives", "tokenlist",
740+
"scopes", "functions", "variables", "valueflow"]
741+
return "{}({})".format(
742+
"Configuration",
743+
", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs))
744+
)
745+
658746

659747
class Platform:
660748
"""
@@ -688,6 +776,14 @@ def __init__(self, platformnode):
688776
self.long_long_bit = int(platformnode.get('long_long_bit'))
689777
self.pointer_bit = int(platformnode.get('pointer_bit'))
690778

779+
def __repr__(self):
780+
attrs = ["name", "char_bit", "short_bit", "int_bit",
781+
"long_bit", "long_long_bit", "pointer_bit"]
782+
return "{}({})".format(
783+
"Platform",
784+
", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs))
785+
)
786+
691787

692788
class Standards:
693789
"""
@@ -705,6 +801,13 @@ def __init__(self, standardsnode):
705801
self.cpp = standardsnode.find("cpp").get("version")
706802
self.posix = standardsnode.find("posix") is not None
707803

804+
def __repr__(self):
805+
attrs = ["c", "cpp", "posix"]
806+
return "{}({})".format(
807+
"Standards",
808+
", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs))
809+
)
810+
708811

709812
class CppcheckData:
710813
"""
@@ -781,6 +884,13 @@ def __init__(self, filename):
781884
if cfgnode.tag == 'dump':
782885
self.configurations.append(Configuration(cfgnode))
783886

887+
def __repr__(self):
888+
attrs = ["configurations", ]
889+
return "{}({})".format(
890+
"CppcheckData",
891+
", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs))
892+
)
893+
784894

785895
# Get function arguments
786896
def getArgumentsRecursive(tok, arguments):

addons/misra.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,14 @@ def __init__(self, directive):
538538
self.args = res.group(1).split(',')
539539
self.expansionList = res.group(2)
540540

541+
def __repr__(self):
542+
attrs = ["args", "expansionList"]
543+
return "{}({})".format(
544+
"Define",
545+
", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs))
546+
)
547+
548+
541549

542550
def getAddonRules():
543551
"""Returns dict of MISRA rules handled by this addon."""
@@ -682,6 +690,13 @@ def __init__(self, args):
682690
if args.no_summary:
683691
self.show_summary = False
684692

693+
def __repr__(self):
694+
attrs = ["verify", "quiet", "show_summary", "verify"]
695+
return "{}({})".format(
696+
"MisraSettings",
697+
", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs))
698+
)
699+
685700

686701
class MisraChecker:
687702

@@ -726,6 +741,15 @@ def __init__(self, settings, stdversion="c90"):
726741

727742
self.stdversion = stdversion
728743

744+
def __repr__(self):
745+
attrs = ["settings", "verify_expected", "verify_actual", "violations",
746+
"ruleTexts", "suppressedRules", "dumpfileSuppressions",
747+
"filePrefix", "suppressionStats", "stdversion"]
748+
return "{}({})".format(
749+
"MisraChecker",
750+
", ".join(("{}={}".format(a, repr(getattr(self, a))) for a in attrs))
751+
)
752+
729753
def get_num_significant_naming_chars(self, cfg):
730754
if cfg.standards and cfg.standards.c == "c99":
731755
return 63

0 commit comments

Comments
 (0)