Skip to content
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

Update c_parser.py to fix string concatenation error #547

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pycparser/c_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1915,7 +1915,7 @@ def p_unified_string_literal(self, p):
p[0] = c_ast.Constant(
'string', p[1], self._token_coord(p, 1))
else:
p[1].value = p[1].value[:-1] + p[2][1:]
p[1].value = p[1].value + p[2]
p[0] = p[1]

def p_unified_wstring_literal(self, p):
Expand Down
13 changes: 8 additions & 5 deletions tests/test_c_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1674,7 +1674,7 @@ def test_unified_string_literals(self):
self.assertEqual(d1, ['Constant', 'string', '"hello"'])

d2 = self.get_decl_init('char* s = "hello" " world";')
self.assertEqual(d2, ['Constant', 'string', '"hello world"'])
self.assertEqual(d2, ['Constant', 'string', '"hello"" world"'])
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, this makes no sense in the general case - it goes against language semantics (see the discussion on the issue)

Copy link
Author

@Llewyllen Llewyllen Jul 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly the general case is wrong in particular cases and cannot be extended to cover these. And keeping the double quotes only when required would be way too complicated (would need to check if the last character of the first string is an escaped hexa/octal character that might wrongly merge with the first character of the second string)


# the test case from issue 6
d3 = self.parse(r'''
Expand All @@ -1690,13 +1690,13 @@ def test_unified_string_literals(self):

self.assertEqual(
d3.ext[0].body.block_items[0].args.exprs[1].value,
r'"Wrong Params?\nUsage:\n%s <binary_file_path>\n"')
r'"Wrong Params?\n""Usage:\n""%s <binary_file_path>\n"')

d4 = self.get_decl_init('char* s = "" "foobar";')
self.assertEqual(d4, ['Constant', 'string', '"foobar"'])
self.assertEqual(d4, ['Constant', 'string', '"""foobar"'])

d5 = self.get_decl_init(r'char* s = "foo\"" "bar";')
self.assertEqual(d5, ['Constant', 'string', r'"foo\"bar"'])
self.assertEqual(d5, ['Constant', 'string', r'"foo\"""bar"'])

# This is not correct based on the the C spec, but testing it here to
# see the behavior in action. Will have to fix this
Expand All @@ -1706,7 +1706,10 @@ def test_unified_string_literals(self):
# into single members of the execution character set just prior to
# adjacent string literal concatenation".
d6 = self.get_decl_init(r'char* s = "\1" "23";')
self.assertEqual(d6, ['Constant', 'string', r'"\123"'])
self.assertEqual(d6, ['Constant', 'string', r'"\1""23"'])

d7 = self.get_decl_init(r'char* s = "\07" "7";')
self.assertEqual(d7, ['Constant', 'string', r'"\07""7"'])

def test_unified_wstring_literals(self):
d1 = self.get_decl_init('char* s = L"hello" L"world";')
Expand Down
Loading