Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Index out of range #140

Open
bjaraujo opened this issue Nov 4, 2018 · 4 comments
Open

Index out of range #140

bjaraujo opened this issue Nov 4, 2018 · 4 comments

Comments

@bjaraujo
Copy link

bjaraujo commented Nov 4, 2018

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

@Omegastick
Copy link

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

@martinburchell
Copy link

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

@dyadav7
Copy link

dyadav7 commented Aug 17, 2019

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

@TomerJLevy
Copy link
Contributor

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 added a commit to TomerJLevy/cppclean that referenced this issue Sep 20, 2019
To avoid IndexError on tokens that has type PREPROCESSORS
Fixing issue - Index out of range myint#140
myint pushed a commit that referenced this issue Sep 28, 2019
To avoid IndexError on tokens that has type PREPROCESSORS
Fixing issue - Index out of range #140
rue-ryuzaki added a commit to rue-ryuzaki/cppclean that referenced this issue Feb 14, 2020
get_name() can return an empty list, see comment myint#140 (comment)
rue-ryuzaki added a commit to rue-ryuzaki/cppclean that referenced this issue Feb 14, 2020
barsnick pushed a commit to barsnick/cppclean that referenced this issue Jan 3, 2022
get_name() can return an empty list, see comment myint#140 (comment)
(cherry picked from commit f8cbe5d)
barsnick pushed a commit to barsnick/cppclean that referenced this issue Jan 20, 2022
get_name() can return an empty list, see comment myint#140 (comment)
(cherry picked from commit rue-ryuzaki/cppclean@f8cbe5d)
barsnick pushed a commit to barsnick/cppclean that referenced this issue Jan 20, 2022
get_name() can return an empty list, see comment myint#140 (comment)
(cherry picked from commit rue-ryuzaki/cppclean@f8cbe5d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants