robotpy / robotpy-cppheaderparser

DEPRECATED: use cxxheaderparser instead
Other
123 stars 39 forks source link

[BUG]: `#define` immediately after a class definition before opening bracket hides the class #83

Open alandtse opened 1 year ago

alandtse commented 1 year ago

Problem description

I am parsing a project which uses ifdefs. While using pcpp to test variants of the code, some classes were no longer being detected by cppheaderparser. This is because cppheaderparse will leave behind #line 2 where 2 is the line number wherever a preprocessor directive is processed/replaced.

While I'm not sure if #line is valid cpp syntax, I've determined the existence of the # does break cppheaderparser so a valid #define 1 also causes the behavior.

Specifically, the #define 1 below immediately after the class declaration but before the { will break parsing of the class. While this is a toy example and it won't break if the #define is after the {, this may be valid syntax if the class inheritance is controlled by an ifdef.

This was tested with the command line:

python -m CppHeaderParser.tojson processed_playercharacter.h > test_processed.json

Operating System

MacOS

Installed Python Packages

Package                 Version
----------------------- ---------
aiocsv                  1.2.3
aiofiles                22.1.0
black                   23.1.0
bleach                  6.0.0
certifi                 2022.12.7
charset-normalizer      3.1.0
click                   8.1.3
click-log               0.4.0
colorama                0.4.6
docutils                0.19
dotty-dict              1.3.1
gitdb                   4.0.10
GitPython               3.1.31
idna                    3.4
importlib-metadata      6.1.0
invoke                  1.7.3
jaraco.classes          3.2.3
keyring                 23.13.1
more-itertools          9.1.0
mypy                    1.1.1
mypy-extensions         1.0.0
numpy                   1.24.2
packaging               23.0
pandas                  1.5.3
pathspec                0.11.1
pcpp                    1.30
pip                     22.3.1
pkginfo                 1.9.6
platformdirs            3.1.1
ply                     3.11
Pygments                2.14.0
python-dateutil         2.8.2
python-gitlab           3.13.0
python-semantic-release 7.33.2
pytz                    2023.2
readme-renderer         37.3
requests                2.28.2
requests-toolbelt       0.10.1
rfc3986                 2.0.0
robotpy-cppheaderparser 5.1.0
semver                  2.13.0
setuptools              65.5.1
six                     1.16.0
smmap                   5.0.0
tomli                   2.0.1
tomlkit                 0.11.6
tqdm                    4.65.0
twine                   3.8.0
typing_extensions       4.5.0
urllib3                 1.26.15
webencodings            0.5.1
wheel                   0.40.0
zipp                    3.15.0

Reproducible example code

namespace RE
{
    class PlayerCharacter
#define 1 // This define breaks the discovery of PlayerCharacter. If this line is commented out, the class will be discovered.
    {
    }
}
virtuald commented 1 year ago

Weird. I miiiight be able to look at it next week, but no promises. Happy to accept a PR though.

Have you tried using cxxheaderparser for your use case?

alandtse commented 1 year ago

Thanks. I have a workaround where I strip out the #line using a regex so I'm unlikely to be able to really learn the code to do a PR. I did try cxxheaderparser at one point, but I believe it had an uncaught exception on a double template declaration and I at least could get past that part using cppheaderparser.

virtuald commented 1 year ago

Was the cxxheaderparser bug this one: https://github.com/robotpy/cxxheaderparser/issues/20 ?

alandtse commented 1 year ago

It may be the same root cause. I think it choked on something like this (but this is from memory): https://github.com/alandtse/CommonLibVR/blob/95c6de6bbee89d0195fd3e1ba0a469b44928e038/include/REL/Relocation.h#L139-L147

I just pulled the latest and it's crashing a bit earlier on that file due to the newline for the define. I'll open an issue. https://github.com/robotpy/cxxheaderparser/issues/48