eliben / pycparser

:snake: Complete C99 parser in pure Python
Other
3.24k stars 609 forks source link

patch: fix NoneType error for 'visit_Case' function in c_generator.py… #548

Open kmg3821 opened 3 months ago

kmg3821 commented 3 months ago

… when a case is empty by adding none type checker

kmg3821 commented 3 months ago

Thanks!

Would it be possible to add a test that triggers this? (fails before the PR, succeeds after)

Scenario : C code --> AST --> C code

Input C code :

void foo(int n) { switch (n) { case 0: case 1: printf("Hello"); break; case 2: printf("World"); default: break; } }

Error occurs when converting AST into C code :

image

This happens because of the "case 0" statement, which is empty :

image

After adding the None type checker : The following C code is successfully generated. image

eliben commented 3 months ago

Yes, I understand

My question is - can you add a test to https://github.com/eliben/pycparser/blob/main/tests/test_c_generator.py as part of your PR that triggers this behavior and verifies that the change fixes it

kmg3821 commented 3 months ago

I'm sorry for the confusion, but the problem is not with the c_generator.py.

The issue lies with the "to_dict" function in c_json.py within the examples folder, which saves None instead of an empty list for the case node type.

It appears that tests are not performed for the examples, so I did not add the test you requested.

However, if needed, I can add it.