cppclean icon indicating copy to clipboard operation
cppclean copied to clipboard

Index out of range

Open bjaraujo opened this issue 6 years ago • 4 comments

I installed the latest master and I get the following error when running cppclean:

Traceback (most recent call last): File "/usr/local/bin/cppclean", line 166, in sys.exit(main()) File "/usr/local/bin/cppclean", line 139, 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 675, in generate result = self._generate_one(token) File "/usr/local/lib/python2.7/dist-packages/cpp/ast.py", line 753, in _generate_one return self._get_method(temp_tokens, 0, None, False) File "/usr/local/lib/python2.7/dist-packages/cpp/ast.py", line 1094, in _get_method member = member[0] IndexError: list index out of range

bjaraujo avatar Nov 04 '18 10:11 bjaraujo

I'm getting this too. Cppclean gets about halfway through the GLM codebase before throwing this error.

Omegastick avatar Feb 16 '19 07:02 Omegastick

I'm seeing this too. I don't know what C++ code causes this to fail. It looks like get_name() can return an empty list so there should probably be a check for it not being empty

https://github.com/myint/cppclean/blob/master/cpp/ast.py#L1097

martinburchell avatar Aug 13 '19 11:08 martinburchell

I am attaching a sample cxx file. This reproduces the bug mentioned in this issue. file.cxx.log

dyadav7 avatar Aug 17 '19 06:08 dyadav7

I had the same problem. Trying to parse this code:

class C {
public:
  C()
  : a(2)
#   pragma warning(disable: 4996) // name has been marked as 
  , b(0)
  {}
  int a;
  int b;
};

Throws:

Traceback (most recent call last):
  File "/usr/local/bin/cppclean", line 166, in <module>
    sys.exit(main())
  File "/usr/local/bin/cppclean", line 139, 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 675, in generate
    result = self._generate_one(token)
  File "/usr/local/lib/python2.7/dist-packages/cpp/ast.py", line 702, in _generate_one
    return method()
  File "/usr/local/lib/python2.7/dist-packages/cpp/ast.py", line 1246, in handle_class
    return self._handle_class_and_struct(Class)
  File "/usr/local/lib/python2.7/dist-packages/cpp/ast.py", line 1243, in _handle_class_and_struct
    return self._get_class(class_type, None)
  File "/usr/local/lib/python2.7/dist-packages/cpp/ast.py", line 1558, in _get_class
    body = list(ast.generate())
  File "/usr/local/lib/python2.7/dist-packages/cpp/ast.py", line 675, in generate
    result = self._generate_one(token)
  File "/usr/local/lib/python2.7/dist-packages/cpp/ast.py", line 753, in _generate_one
    return self._get_method(temp_tokens, 0, None, False)
  File "/usr/local/lib/python2.7/dist-packages/cpp/ast.py", line 1098, in _get_method
    member = member[0]
IndexError: list index out of range

Because you only skip those macros in https://github.com/myint/cppclean/blob/master/cpp/ast.py#L1093

# skip preprocesors macros
if token.name.startswith(('#if', '#elif', '#else', '#endif')):
    token = self._get_next_token()
    continue

And than you enter the self.get_name function with 'token_type' of 'PREPROCESSOR' which return an empty list and cabum - you're failing to get the first element and throws IndexError for trying to get index 0.

A fix can be:

# skip preprocesors macros
if token.token_type == tokenize.PREPROCESSOR:
    token = self._get_next_token()
    continue

TomerJLevy avatar Sep 20 '19 08:09 TomerJLevy