Repository navigation
Read a define value untill the end of the line or when it encounters a comment #170
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -251,11 +251,14 @@ def test_define(cs: cstruct) -> None: | |||||||||||||||||||||
| #define MULTILINE (1 + \ | ||||||||||||||||||||||
| 2 + \ | ||||||||||||||||||||||
| 3) | ||||||||||||||||||||||
| #define QUOTES "\'\"a'b\"" | ||||||||||||||||||||||
| #define QUOTES "'\\"a'b\\"" | ||||||||||||||||||||||
| #define ESCAPE "\\'\\"a'b\\"\\n" | ||||||||||||||||||||||
| #define BYTES_ESCAPE b"`\\n" | ||||||||||||||||||||||
| #define FUNC(x) ( x == 0 ) | ||||||||||||||||||||||
| #define TERNARY(x) ( x ? 1 : 0 ) | ||||||||||||||||||||||
| #define MULTI_LINE_QUOTE "\ | ||||||||||||||||||||||
| #define DEFINITION " | ||||||||||||||||||||||
| #define SPACED_BYTES b" " | ||||||||||||||||||||||
| """ | ||||||||||||||||||||||
| cs.load(cdef) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
@@ -275,6 +278,8 @@ def test_define(cs: cstruct) -> None: | |||||||||||||||||||||
| # We don't evaluate function-like macros yet, so they should be stored as their raw string representation | ||||||||||||||||||||||
| assert cs.consts["FUNC"] == "(x) ( x == 0 )" | ||||||||||||||||||||||
| assert cs.consts["TERNARY"] == "(x) ( x ? 1 : 0 )" | ||||||||||||||||||||||
| assert cs.consts["MULTI_LINE_QUOTE"] == " #define DEFINITION " | ||||||||||||||||||||||
| assert cs.consts["SPACED_BYTES"] == b" " | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def test_define_flag_value(cs: cstruct) -> None: | ||||||||||||||||||||||
|
|
@@ -515,6 +520,34 @@ def test_preprocessor_in_struct_body(cs: cstruct) -> None: | |||||||||||||||||||||
| assert cs.test.fields["bonus"].type == cs.uint64 | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def test_preprocessor_define_with_comments(cs: cstruct) -> None: | ||||||||||||||||||||||
| cdef = """ | ||||||||||||||||||||||
| #define IGNORE_SCOPED_COMMENT data1 /* ... */ | ||||||||||||||||||||||
| #define IGNORE_LINE_COMMENT data2 // | ||||||||||||||||||||||
| #define IGNORE_MULTILINE_COMMENT data3 /* Multiline | ||||||||||||||||||||||
| comment | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| #define IGNORE_DEFINE_IN_COMMENT data4 /* define | ||||||||||||||||||||||
| #define TEST_NO data_no inside | ||||||||||||||||||||||
| comment | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| #define TEXT_CONTAINING_COMMENT "text with comments /* data5 */" | ||||||||||||||||||||||
| #define IGNORE_STRING_AFTER_COMMENT "data6" // \ | ||||||||||||||||||||||
| "string that cannot be reached" | ||||||||||||||||||||||
| #define NO_COMMENT_BETWEEN_CONCATINATION "data7" /* Comment */ \ | ||||||||||||||||||||||
| "should get concatinated" | ||||||||||||||||||||||
| """ | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| cs.load(cdef) | ||||||||||||||||||||||
| assert cs.consts["IGNORE_SCOPED_COMMENT"] == "data1" | ||||||||||||||||||||||
| assert cs.consts["IGNORE_LINE_COMMENT"] == "data2" | ||||||||||||||||||||||
| assert cs.consts["IGNORE_MULTILINE_COMMENT"] == "data3" | ||||||||||||||||||||||
| assert cs.consts["IGNORE_DEFINE_IN_COMMENT"] == "data4" | ||||||||||||||||||||||
| assert cs.consts["TEXT_CONTAINING_COMMENT"] == "text with comments /* data5 */" | ||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cf comment below, I didn't find how to make a suggestion edit from Github gui for two lines....
Suggested change
|
||||||||||||||||||||||
| assert cs.consts["IGNORE_STRING_AFTER_COMMENT"] == "data6" | ||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We lost the difference between quoted and unquoted value (which is the difference between and int, a char and a string. I don't known dissect.cstruct enough but a first glance it's look like a potential issue
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i will take a look whether that will be an issue
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no difference between char and string in cstruct. As for an int, it is properly resolved: Python 3.14.3 (main, Feb 3 2026, 22:52:18) [Clang 21.1.4 ] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from dissect.cstruct import cstruct
>>> cstruct()
<dissect.cstruct.cstruct.cstruct object at 0x7c966b671a90>
>>> x = cstruct()
>>> xxx = """
... #define A_SIMPLE_INT 1 //
... #define A_INT_AS_STRING "1"
... #define A_INT_AS_CHAR '1'
... """
>>> x.load(xxx)
<dissect.cstruct.cstruct.cstruct object at 0x7c966b732710>
>>> x.A_SIMPLE_INT
1
>>> x.A_INT_AS_STRING
'1'
>>> x.A_INT_AS_CHAR
'1' |
||||||||||||||||||||||
| assert cs.consts["NO_COMMENT_BETWEEN_CONCATINATION"] == 'data7" "should get concatinated' | ||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From my understanting and test the value should be ``'data7should get concatinated' E.g link on compiler exporer/godbolt
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is true, I currently kept them separate in the define parsing because of a different issue: #define DATA1 "world"
#define CONCAT "hello" \
DATA1where |
||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Ensure TEST_NO is actually ignored |
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def test_preprocessor_define_from_enum_in_struct(cs: cstruct) -> None: | ||||||||||||||||||||||
| """Test #define referencing enum values used for conditional fields and array sizes.""" | ||||||||||||||||||||||
| cdef = """ | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically this does not compile as it is compiler explorer link
Maybe change with integer for something more correct in C?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
only using L530 does indeed not compile, as the comment is not closed. using the full comment does work no? https://godbolt.org/z/zvfT8T4cT
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue was more related to the quoted /unquoted 'data4' vs '"data4"' (I was not really clear), but this is related to #170 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh that, yeah cstruct sees everything as a string after the define. I think we kept it that way for backwards compatibility with <5.0