The thing is, in the second iteration, data in parsedDump.rawTokens from the first iteration is not destroy and second iteration's data is simply appended to first one's. I'm not very good on Python but I think it's related to how the Cppcheckdata class is created. Any help is appreciated.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
importcppcheckdataasfimportargparseclassCppCheckFormatter(argparse.HelpFormatter):""" Properly formats multiline argument helps """def_split_lines(self,text,width):# this is the RawTextHelpFormatter._split_linesiftext.startswith('R|'):returntext[2:].splitlines()returnargparse.HelpFormatter._split_lines(self,text,width)defArgumentParser():parser=argparse.ArgumentParser(formatter_class=CppCheckFormatter)parser.add_argument("dumpfile",nargs='*',help="Path of dump files from cppcheck.")parser.add_argument("--cli",help="Addon is executed from command line.",action="store_true")returnparserdefmain():#Get path and parse dataparser=ArgumentParser()args=parser.parse_args()#Invoke checkers for each dumpfilefordumpinargs.dumpfile:parsedDump=f.parsedump(dump)print("Dump name:")print(dump)print(parsedDump)print("Raw tokens:")print(*parsedDump.rawTokens,sep="\n")#Check tokens in rawToken. When you run it with multiple files, tokens just get appendedforcfginparsedDump.configurations:#For comparisonprint("Configuration")print("cfg")print("Tokens")fortokenincfg.tokenlist:printprint(token)#Tokens are okay.if__name__=="__main__":main()
Create multiple dumps and run like this: python code.py 1.cpp.dump 2.cpp.dump
Edit:
I think problem lies in the parsedDump = f.parsedump(dump) where content of Raw Tokens doesn't get replaced and instead gets appended. Which means problem should be somewhere in Cppcheck data. parsedump(filename) calls Cppcheckdata(filename) which creates a class object then calls __init(...) to initialize class variables. Which means somewhere in Cppcheck data in the below lines something should be going wrong:
I guess it ElementTree that causes the behaviour. Though I have no idea why data persists from one call to other. Wouldn't every object get destroyed after the function ends?
Last edit: samed 2021-01-08
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I guess I have found the culprit. This read explains it (and this one is a good addition). Apparently in python we have class attributes and instance attributes. Since rawTokens is a class attributes and is mutable (list), when you append to it, it doesn't promote to instance variable which means when you call parsedump() multiple times data just gets appended to it. If you add self.rawTokens = [] to __init__()(or simple remove rawTokens from class and define it in __init__() to make it an instance variable) it fixes the situation.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
@samed You offer the good improvements in this area. Feel free to open PRs in cppcheck. I think, it will be easier for you and allows to discuss/merge new features faster.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
What I'm doing is basically this. I invoke a script with a bunch of different dumps:
In code, I parse the arguments and check them in loop like this:
The thing is, in the second iteration, data in parsedDump.rawTokens from the first iteration is not destroy and second iteration's data is simply appended to first one's. I'm not very good on Python but I think it's related to how the Cppcheckdata class is created. Any help is appreciated.
hmm sounds very strange.. that should not happen. If you get that behavior then it's not your fault but some bug in Cppcheckdata!
Could you provide the source code of your script or a minimal example that reproduces this problem?
Create multiple dumps and run like this:
python code.py 1.cpp.dump 2.cpp.dump
Edit:
I think problem lies in the
parsedDump = f.parsedump(dump)
where content ofRaw Tokens
doesn't get replaced and instead gets appended. Which means problem should be somewhere in Cppcheck data.parsedump(filename)
callsCppcheckdata(filename)
which creates a class object then calls__init(...)
to initialize class variables. Which means somewhere in Cppcheck data in the below lines something should be going wrong:I guess it ElementTree that causes the behaviour. Though I have no idea why data persists from one call to other. Wouldn't every object get destroyed after the function ends?
Last edit: samed 2021-01-08
I guess I have found the culprit. This read explains it (and this one is a good addition). Apparently in python we have class attributes and instance attributes. Since
rawTokens
is a class attributes and is mutable (list), when you append to it, it doesn't promote to instance variable which means when you callparsedump()
multiple times data just gets appended to it. If you addself.rawTokens = []
to__init__()
(or simple removerawTokens
from class and define it in__init__()
to make it an instance variable) it fixes the situation.Nice catch! Thank you very much.
We've never seen this issue, because we normally call the addons through cppcheck binary. So we never used it with multiple dump files.
This PR will fix it: https://github.com/danmar/cppcheck/pull/3029
Glad I could help. On a quick note though. I use
files
attribute in my checker. I suggest you make it accessible from outside.https://github.com/danmar/cppcheck/pull/3030
@samed You offer the good improvements in this area. Feel free to open PRs in cppcheck. I think, it will be easier for you and allows to discuss/merge new features faster.