From 7ffc25df4d433a50626a602fa69f881c83667798 Mon Sep 17 00:00:00 2001 From: Llewyllen <61664783+Llewyllen@users.noreply.github.com> Date: Wed, 3 Jul 2024 16:18:31 +0200 Subject: [PATCH 1/3] Update c_parser.py Do not remove consecutive double quotes, as it might result in a different string in case of an escaped octal or hexadecimal value at the end of the first string AND a numeric character at the beginning of the second string --- pycparser/c_parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pycparser/c_parser.py b/pycparser/c_parser.py index fa56c389..a31915d8 100644 --- a/pycparser/c_parser.py +++ b/pycparser/c_parser.py @@ -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): From 33b9c499b362c6767e27af1989542b9c7a53b2b2 Mon Sep 17 00:00:00 2001 From: Llewyllen <61664783+Llewyllen@users.noreply.github.com> Date: Wed, 3 Jul 2024 16:42:34 +0200 Subject: [PATCH 2/3] Update test_c_parser.py --- tests/test_c_parser.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tests/test_c_parser.py b/tests/test_c_parser.py index 13529852..23ea466b 100755 --- a/tests/test_c_parser.py +++ b/tests/test_c_parser.py @@ -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"']) # the test case from issue 6 d3 = self.parse(r''' @@ -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 \n"') + r'"Wrong Params?\n""Usage:\n""%s \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 @@ -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.assertNotEqual(d7, ['Constant', 'string', r'"\07""7"']) def test_unified_wstring_literals(self): d1 = self.get_decl_init('char* s = L"hello" L"world";') From aa2c84f4bcefa08afcbe778f1f7707f5b6e561a4 Mon Sep 17 00:00:00 2001 From: Llewyllen <61664783+Llewyllen@users.noreply.github.com> Date: Wed, 3 Jul 2024 16:44:44 +0200 Subject: [PATCH 3/3] Update test_c_parser.py --- tests/test_c_parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_c_parser.py b/tests/test_c_parser.py index 23ea466b..5d4f7777 100755 --- a/tests/test_c_parser.py +++ b/tests/test_c_parser.py @@ -1709,7 +1709,7 @@ def test_unified_string_literals(self): self.assertEqual(d6, ['Constant', 'string', r'"\1""23"']) d7 = self.get_decl_init(r'char* s = "\07" "7";') - self.assertNotEqual(d7, ['Constant', 'string', r'"\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";')