Skip to content

Read a define value untill the end of the line or when it encounters a comment - #170

Draft
Miauwkeru wants to merge 2 commits into
mainfrom
read-define-value-till-comment
Draft

Read a define value untill the end of the line or when it encounters a comment#170
Miauwkeru wants to merge 2 commits into
mainfrom
read-define-value-till-comment

Conversation

@Miauwkeru

Copy link
Copy Markdown
Contributor
  • Keeps track of the whitespece between the value and the comment
  • Will keep track of a single qouted character

fixes #169

…a comment

- Keeps track of the whitespece between the value and the comment
- Will keep track of a single qouted character
@codecov

codecov Bot commented Jul 29, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 28 lines in your changes missing coverage. Please review.
✅ Project coverage is 0.00%. Comparing base (3032ad7) to head (e633073).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
dissect/cstruct/lexer.py 0.00% 28 Missing ⚠️
Additional details and impacted files
@@          Coverage Diff          @@
##            main    #170   +/-   ##
=====================================
  Coverage   0.00%   0.00%           
=====================================
  Files         22      22           
  Lines       2798    2881   +83     
=====================================
- Misses      2798    2881   +83     
Flag Coverage Δ
unittests 0.00% <0.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Miauwkeru

Copy link
Copy Markdown
Contributor Author

This was the first one that got through all the tests, I will think of a better solution for the quoted string as it will break if you add a single quote somewhere between double quotes

@codspeed-hq

codspeed-hq Bot commented Jul 29, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 14 untouched benchmarks


Comparing read-define-value-till-comment (e633073) with main (46b850f)

Open in CodSpeed

@Miauwkeru

Copy link
Copy Markdown
Contributor Author

I can replace it with _read_string() however, this does require to escape:

#define QUOTES "\'\"a'b\""

oterwise _read_string can't distinguish the quotes from each other. @Schamper is it an idea to always escape the characters? (and document that this is required) If not, I will look for another way

@Schamper

Copy link
Copy Markdown
Member

What about multiline defines with quotes at the end of each (or some) line? Note that I'm not sure how that'd work in C, just a thought. Worth a test.

@Miauwkeru

Copy link
Copy Markdown
Contributor Author

What about multiline defines with quotes at the end of each (or some) line? Note that I'm not sure how that'd work in C, just a thought. Worth a test.

Do you mean something like this:

#define HELP " \
#define NLD "

it won't work without the backslash according to some tests with godbolt

@Schamper

Copy link
Copy Markdown
Member

I mean something like this:

#define foo bar \  // comment here
            zomg \  // and here
            yolo

@Miauwkeru

Copy link
Copy Markdown
Contributor Author

I mean something like this:

#define foo bar \  // comment here
            zomg \  // and here
            yolo

it will error in that case in c:

main.c:4:13: error: expected identifier or ‘(’ before string constant
    4 |             "zomg" \  // and here
      |             ^~~~~~
main.c:4:20: error: stray ‘\’ in program
    4 |             "zomg" \  // and here
      |                    ^
main.c:3:19: error: stray ‘\’ in program
    3 | #define foo "bar" \  // comment here
      |                   ^
main.c:8:12: note: in expansion of macrofoo8 |     printf(foo);

and in cstruct it creates a lexer error for an unexpected \\ character next to zomg

@Schamper

Schamper commented Aug 1, 2026

Copy link
Copy Markdown
Member

This is valid C though:

#define foo "bar" /* comment */ \
        "zomg"

Might be nice to at least put something like this in the unit test.

@Miauwkeru

Copy link
Copy Markdown
Contributor Author

This is valid C though:

#define foo "bar" /* comment */ \
        "zomg"

Might be nice to at least put something like this in the unit test.

I'll add it and see whether I can make that work :)

@Miauwkeru

Copy link
Copy Markdown
Contributor Author

In C there is also the case that you can concatenate two defines if they have the same type. This doesn't work yet as it doesn't resolve yet. I think it might be better to put that functionality in a different pr if we want it.

def test_preprocessor_define_substitute_define(cs: cstruct) -> None:
    """Test whether we can concatinate defines with other strings."""
    c_def = """
    #define DATA1 "world"
    #define CONCAT "hello" \\
            DATA1
    """
    cs.load(c_def)

    assert cs.consts["CONCAT"] == "helloworld"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Issue when parsing comments after a define

2 participants