Crashes on member initialization using uniform initialization list
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.
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.
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.
Hi,
Member initialization list for bool is still triggering a parser error (with cppclean 0.12). Any plan to fix it ? Thanks, Jean-Paul
I'm hitting this with 0.12 as well
Traceback (most recent call last):
File "/usr/local/bin/cppclean", line 145, in