cppclean icon indicating copy to clipboard operation
cppclean copied to clipboard

Crashes on member initialization using uniform initialization list

Open FuegoFro opened this issue 10 years ago • 4 comments

EDIT: I dug into this a bit, added some details below, and changed the name to be more descriptive. ~~Sorry for the non-descript issue name. I haven't actually dug into what's causing it to crash, so I don't have much more detail there.~~ I have however narrowed it down to a small example file which actually causes two separate crashes. If you run cppclean on the file as is it'll complain about popping from an empty namespaces list, but if you comment out the int member, it'll give you a parsing error. The example file is here:

class Foo {
private:
    atomic<bool> m_shutdown { false };
    int m_threads_started = 0; // Comment this line out for a different error
    vector<const function<void()> *> & list_of(const function<void()> &) { return m_callbacks; }
}

The stack trace the file as-is is:

Traceback (most recent call last):
  File "/Users/user/venv/bin/cppclean", line 145, in <module>
    sys.exit(main())
  File "/Users/user/venv/bin/cppclean", line 120, in main
    entire_ast = list([_f for _f in builder.generate() if _f])
  File "/Users/user/venv/lib/python2.7/site-packages/cpp/ast.py", line 651, in generate
    result = self._generate_one(token)
  File "/Users/user/venv/lib/python2.7/site-packages/cpp/ast.py", line 675, in _generate_one
    return method()
  File "/Users/user/venv/lib/python2.7/site-packages/cpp/ast.py", line 1158, in handle_class
    return self._handle_class_and_struct(Class)
  File "/Users/user/venv/lib/python2.7/site-packages/cpp/ast.py", line 1155, in _handle_class_and_struct
    return self._get_class(class_type, None)
  File "/Users/user/venv/lib/python2.7/site-packages/cpp/ast.py", line 1456, in _get_class
    body = list(ast.generate())
  File "/Users/user/venv/lib/python2.7/site-packages/cpp/ast.py", line 647, in generate
    if self.namespaces.pop():
IndexError: pop from empty list

And the error message when the int member is commented out:

/Users/user/test_header.hpp: parsing error: (Token(u'>', 128, 129), [Token(u'bool', 32, 36), Token(u'>', 36, 37), Token(u'm_shutdown', 38, 48), Token(u'{', 49, 50), Token(u'false', 51, 56), Token(u'}', 57, 58), Token(u';', 58, 59), Token(u'vector', 64, 70), Token(u'<', 70, 71), Token(u'const', 71, 76), Token(u'function', 77, 85), Token(u'<', 85, 86), Token(u'void', 86, 90), Token(u'(', 90, 91), Token(u')', 91, 92), Token(u'>', 92, 93), Token(u'*', 94, 95), Token(u'>', 95, 96), Token(u'&', 97, 98), Token(u'list_of', 99, 106), Token(u'(', 106, 107), Token(u'const', 107, 112), Token(u'function', 113, 121), Token(u'<', 121, 122)], [])

This is on cppclean 0.8 as installed by pip.

FuegoFro avatar Jan 15 '16 18:01 FuegoFro

Looking into this a bit more, it seems that the parser doesn't understand the uniform initialization list that is initializing m_shutdown, instead thinking that we're in a method. It has code to look for assignment/initialization, so a workaround is to put an = before the initializer list like so:

atomic<bool> m_shutdown = { false };

But ideally the tool would be able to handle member initializations like this without modifying the source code.

FuegoFro avatar Jan 18 '16 00:01 FuegoFro

Thanks for the report. Your analysis is correct, the problem is caused by the uniform initialization. I will try to improve the parser to handle this construct.

r-e-d avatar Jan 18 '16 17:01 r-e-d

Hi,

Member initialization list for bool is still triggering a parser error (with cppclean 0.12). Any plan to fix it ? Thanks, Jean-Paul

jguigui avatar Aug 30 '16 05:08 jguigui

I'm hitting this with 0.12 as well

Traceback (most recent call last): File "/usr/local/bin/cppclean", line 145, in sys.exit(main()) File "/usr/local/bin/cppclean", line 120, in main entire_ast = list([_f for _f in builder.generate() if _f]) File "/usr/local/lib/python2.7/dist-packages/cpp/ast.py", line 651, in generate result = self._generate_one(token) File "/usr/local/lib/python2.7/dist-packages/cpp/ast.py", line 675, in _generate_one return method() File "/usr/local/lib/python2.7/dist-packages/cpp/ast.py", line 1177, in handle_class return self._handle_class_and_struct(Class) File "/usr/local/lib/python2.7/dist-packages/cpp/ast.py", line 1174, in _handle_class_and_struct return self._get_class(class_type, None) File "/usr/local/lib/python2.7/dist-packages/cpp/ast.py", line 1486, in _get_class body = list(ast.generate()) File "/usr/local/lib/python2.7/dist-packages/cpp/ast.py", line 647, in generate if self.namespaces.pop(): IndexError: pop from empty list

JPArroyo avatar Jan 25 '17 18:01 JPArroyo