diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2022-09-19 14:40:37 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2022-09-21 09:19:32 +0000 |
commit | c4f1f65208b072fbd06af42682681d1fcdc042ef (patch) | |
tree | 67447556819b18ac179636cde1d00f5b633224f5 | |
parent | 67b4ed2c16fecae7efbdcb9aadfbff8434333230 (diff) |
snippets_translate: Fix handling of operator new, take 2
Change e48dce39c1450e73f7cdef58cfeba29e1c3b8be3 introduced a bug
appending "()" to member initalizer lists "m_member(new X()),". Fix
that by using a regexp to loop through the expressions.
Change-Id: I6ce095749bcab74e92fb6dd630f25fb9fd517cc5
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
(cherry picked from commit 68883026da79c4f380411237731d761429760c23)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | tools/snippets_translate/converter.py | 5 | ||||
-rw-r--r-- | tools/snippets_translate/handlers.py | 18 | ||||
-rw-r--r-- | tools/snippets_translate/tests/test_converter.py | 4 |
3 files changed, 24 insertions, 3 deletions
diff --git a/tools/snippets_translate/converter.py b/tools/snippets_translate/converter.py index 1de3fd051..98de860b2 100644 --- a/tools/snippets_translate/converter.py +++ b/tools/snippets_translate/converter.py @@ -46,6 +46,7 @@ from handlers import (handle_array_declarations, handle_casts, handle_class, handle_inc_dec, handle_include, handle_keywords, handle_methods_return_type, handle_negate, handle_type_var_declaration, handle_useless_qt_classes, + handle_new, handle_void_functions, handle_qt_connects) from parse_utils import dstrip, get_indent, remove_ref @@ -129,9 +130,7 @@ def snippet_translate(x): # This contains an extra whitespace because of some variables # that include the string 'new' if "new " in x: - x = x.replace("new ", "") - if not x.endswith(")"): # "new Foo" -> "new Foo()" - x += "()" + x = handle_new(x) # Handle 'const' # Some variables/functions have the word 'const' so we explicitly diff --git a/tools/snippets_translate/handlers.py b/tools/snippets_translate/handlers.py index 642db24cb..8a074a040 100644 --- a/tools/snippets_translate/handlers.py +++ b/tools/snippets_translate/handlers.py @@ -75,6 +75,7 @@ COUT_ENDL_PATTERN = re.compile(r"cout *<<(.*)<< *.*endl") COUT1_PATTERN = re.compile(r" *<< *") COUT2_PATTERN = re.compile(r".*cout *<<") COUT_ENDL2_PATTERN = re.compile(r"<< +endl") +NEW_PATTERN = re.compile(r"new +([a-zA-Z][a-zA-Z0-9_]*)") def handle_condition(x, name): @@ -548,6 +549,23 @@ def handle_useless_qt_classes(x): return x +def handle_new(x): + """Parse operator new() and add parentheses were needed: + func(new Foo, new Bar(x))" -> "func(Foo(), Bar(x))""" + result = "" + last_pos = 0 + for match in NEW_PATTERN.finditer(x): + end = match.end(0) + parentheses_needed = end >= len(x) or x[end] != "(" + type_name = match.group(1) + result += x[last_pos:match.start(0)] + type_name + if parentheses_needed: + result += "()" + last_pos = end + result += x[last_pos:] + return result + + # The code below handles pairs of instance/pointer to member functions (PMF) # which appear in Qt in connect statements like: # "connect(fontButton, &QAbstractButton::clicked, this, &Dialog::setFont)". diff --git a/tools/snippets_translate/tests/test_converter.py b/tools/snippets_translate/tests/test_converter.py index 28436e846..813c07ac5 100644 --- a/tools/snippets_translate/tests/test_converter.py +++ b/tools/snippets_translate/tests/test_converter.py @@ -101,6 +101,10 @@ def test_else(): def test_new(): assert st("a = new Something(...);") == "a = Something(...)" assert st("a = new Something") == "a = Something()" + assert st("foo(new X, new Y(b), new Z)") == "foo(X(), Y(b), Z())" + # Class member initialization list + assert st("m_mem(new Something(p)),") == "m_mem(Something(p))," + assert st("m_mem(new Something),") == "m_mem(Something())," def test_semicolon(): |