pycparser icon indicating copy to clipboard operation
pycparser copied to clipboard

TypeError: 'NoneType' object is not iterable in visit_case function of c_generator.py

Open SpringRi opened this issue 5 years ago • 1 comments

Hi eliben, thanks for your pycparser.

I noticed there may be a small issue in c_generator.py.

The statement in case could be none. For example,

enum 
{
  enum_constant_0,
  enum_constant_1,
  enum_constant_2,
  enum_constant_3
} var_0;
extern void foo();
extern void bar();
void test()
{
  switch (var_0)
  {
    case enum_constant_1:

    case enum_constant_2:
      foo();

    case enum_constant_3:
      bar();

  }
}

However, when we use c_generator.CGenerator() to recover code from json ast fomat, it will meet the error

  File "python3.6/site-packages/pycparser/c_generator.py", line 253, in visit_Case
    for stmt in n.stmts:
TypeError: 'NoneType' object is not iterable

The code is

    def visit_Case(self, n):
        s = 'case ' + self.visit(n.expr) + ':\n'
        for stmt in n.stmts:
            s += self._generate_stmt(stmt, add_indent=True)
        return s

A possible fix

    def visit_Case(self, n):
        s = 'case ' + self.visit(n.expr) + ':\n'
        if n.stmts != None:
            for stmt in n.stmts:
                s += self._generate_stmt(stmt, add_indent=True)
        return s

SpringRi avatar May 01 '20 09:05 SpringRi

Thanks for the report. Feel free to send a PR with a fix + test

eliben avatar May 01 '20 12:05 eliben