aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2023-07-03 11:35:18 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2023-07-05 11:34:15 +0000
commit8b07f6bb887de53c6391f3bf9986508233c45777 (patch)
treebced55e5ea40b644368142e8dc44c571f0fe4e95
parent5241257f00f71b84a86de6a84e1b82d19daff87d (diff)
snippets_translate: Prevent the variable initialization code from triggering for functions
The code trying to change a constructor initialization: "Foo foo(2);" into "foo = Foo(2)" also triggered for member function definitions returning pointers "Foo *Foo:foo()" and many function declarations in headers. Restrict this by checking for a semicolon and non-presence of some function qualifiers. Task-number: PYSIDE-1106 Change-Id: I224ac3e7321e57f1c5beecdcdb568a273330a664 Reviewed-by: Shyamnath Premnadh <Shyamnath.Premnadh@qt.io> (cherry picked from commit 1f3f99bf62df78981c839c95ee98adad649a5ef4) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--tools/snippets_translate/converter.py9
-rw-r--r--tools/snippets_translate/tests/test_converter.py8
2 files changed, 13 insertions, 4 deletions
diff --git a/tools/snippets_translate/converter.py b/tools/snippets_translate/converter.py
index 784e4e45f..d45bf277f 100644
--- a/tools/snippets_translate/converter.py
+++ b/tools/snippets_translate/converter.py
@@ -45,6 +45,9 @@ QUALIFIERS = {"public:", "protected:", "private:", "public slots:",
"protected slots:", "private slots:", "signals:"}
+FUNCTION_QUALIFIERS = ["virtual ", " override", "inline ", " noexcept"]
+
+
switch_var = None
switch_branch = 0
@@ -60,7 +63,8 @@ def snippet_translate(x):
## General Rules
# Remove ';' at the end of the lines
- if x.endswith(";"):
+ has_semicolon = x.endswith(";")
+ if has_semicolon:
x = x[:-1]
# Remove lines with only '{' or '}'
@@ -260,7 +264,8 @@ def snippet_translate(x):
# At the end we skip methods with the form:
# QStringView Message::body()
# to threat them as methods.
- if (VAR1_PATTERN.search(xs)
+ if (has_semicolon and VAR1_PATTERN.search(xs)
+ and not ([f for f in FUNCTION_QUALIFIERS if f in x])
and xs.split()[0] not in ("def", "return", "and", "or")
and not VAR2_PATTERN.search(xs)
and ("{" not in x and "}" not in x)):
diff --git a/tools/snippets_translate/tests/test_converter.py b/tools/snippets_translate/tests/test_converter.py
index a1f01eae1..084cc8a6d 100644
--- a/tools/snippets_translate/tests/test_converter.py
+++ b/tools/snippets_translate/tests/test_converter.py
@@ -166,14 +166,14 @@ def test_cout_endl():
def test_variable_declaration():
assert st("QLabel label;") == "label = QLabel()"
- assert st('QLabel label("Hello")') == 'label = QLabel("Hello")'
+ assert st('QLabel label("Hello");') == 'label = QLabel("Hello")'
assert st("Widget w;") == "w = Widget()"
assert st('QLabel *label = new QLabel("Hello");') == 'label = QLabel("Hello")'
assert st('QLabel label = a_function("Hello");') == 'label = a_function("Hello")'
assert st('QString a = "something";') == 'a = "something"'
assert st("int var;") == "var = int()"
assert st("float v = 0.1;") == "v = 0.1"
- assert st("QSome<thing> var") == "var = QSome()"
+ assert st("QSome<thing> var;") == "var = QSome()"
assert st("QQueue<int> queue;") == "queue = QQueue()"
assert st("QVBoxLayout *layout = new QVBoxLayout;") == "layout = QVBoxLayout()"
assert st("QPointer<QLabel> label = new QLabel;") == "label = QLabel()"
@@ -181,6 +181,10 @@ def test_variable_declaration():
assert st("QList<QImage> collage =") == "collage ="
assert st("bool b = true;") == "b = True"
assert st("Q3DBars *m_graph = nullptr;") == "m_graph = None"
+ # Do not fall for member function definitions
+ assert st("Q3DBars *Graph::bars() const") == "Q3DBars Graph.bars()"
+ # Do not fall for member function declarations
+ assert st("virtual Q3DBars *bars();") == "virtual Q3DBars bars()"
def test_for():