Menu

Can Cppcheck detect initialization syntax?

samed
2020-12-30
2020-12-31
  • samed

    samed - 2020-12-30

    Hi.

    Does anybody know if Cppcheck can detect which initialization syntax is used or not? If so, I'll be glad if you can point me to corresponding source code. My goal is to report initialization syntax of variables in the XML dump.

     
  • Daniel Marjamäki

    I am not sure what you want to report.. can you give me an example code? I have the feeling you have the information you need in the xml dump.

     
  • samed

    samed - 2020-12-30

    I want to check if a variable is initialized using uniform initalization or some other form. Currently it's not in the dump. I'm guessing Cppcheck has to deal with how variables are initialized at some point so I think it should be possible to find it out.

     
  • Daniel Marjamäki

    Example code:

    int x{1};
    int y=2;
    

    you can see in the dump that x is initialized with {} and that y is initialized with =. Look in the <token.. .

    I'd suggest something like:

    for var in data.variables:
         if simpleMatch(var.nameToken.astParent, '{'):
             # uniform initialisation
    
     
  • samed

    samed - 2020-12-31

    I thought about it but it fails in arrays. For example:

    MyClass {
            int x;
            int y;
        public:
            Myclass(int x, int y): x(x), y(y) {} 
    }
    int main()
    {
    Myclass myArray[] = {Myclass(0,0)}; //should be {{0,0}}
    }
    

    Now I can of course handle this case but in case there are some special cases that I might miss, I wanted to ask if Cppcheck does it internally.

     
  • Daniel Marjamäki

    you need to parse out the exact details.

     
  • samed

    samed - 2020-12-31

    I don't consider myself very knowledgeable so I'm trying to resort to existing solutions. Anyways I did it myself in the end. All good.

     
  • Daniel Marjamäki

    I don't consider myself very knowledgeable so I'm trying to resort to existing solutions. Anyways I did it myself in the end. All good.

    well you did it in the end so I think there are many kinds of checks you could implement.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.