From dddc9db805bce87ddd0f68749640228ffe703921 Mon Sep 17 00:00:00 2001 From: guwirth Date: Thu, 29 Aug 2024 16:53:23 +0200 Subject: [PATCH] C++23 extend init-statement to allow alias-declaration - Extend init-statement (of for loop) to allow alias-declaration [P2360R0](https://wg21.link/P2360R0) - linked #2536 --- cxx-squid/dox/diff-cpp20-cpp23_grammar.txt | 5 ----- .../src/main/java/org/sonar/cxx/parser/CxxGrammarImpl.java | 3 ++- .../src/test/java/org/sonar/cxx/parser/StatementTest.java | 7 +++++-- .../src/test/resources/parser/own/C++23/init-statement.cc | 4 ++++ 4 files changed, 11 insertions(+), 8 deletions(-) create mode 100644 cxx-squid/src/test/resources/parser/own/C++23/init-statement.cc diff --git a/cxx-squid/dox/diff-cpp20-cpp23_grammar.txt b/cxx-squid/dox/diff-cpp20-cpp23_grammar.txt index ae20df630..a5ae8a2fa 100644 --- a/cxx-squid/dox/diff-cpp20-cpp23_grammar.txt +++ b/cxx-squid/dox/diff-cpp20-cpp23_grammar.txt @@ -184,11 +184,6 @@ postfix-expression: **A.6 Statements [gram.stmt]** -init-statement: - expression-statement - simple-declaration - alias-declaration - label: attribute-specifier-seqopt identifier : attribute-specifier-seqopt case constant-expression : diff --git a/cxx-squid/src/main/java/org/sonar/cxx/parser/CxxGrammarImpl.java b/cxx-squid/src/main/java/org/sonar/cxx/parser/CxxGrammarImpl.java index 6ba2fc746..b84c6f397 100644 --- a/cxx-squid/src/main/java/org/sonar/cxx/parser/CxxGrammarImpl.java +++ b/cxx-squid/src/main/java/org/sonar/cxx/parser/CxxGrammarImpl.java @@ -928,7 +928,8 @@ private static void statements(LexerfulGrammarBuilder b) { b.rule(initStatement).is( b.firstOf( expressionStatement, // C++ - simpleDeclaration // C++ + simpleDeclaration, // C++ + aliasDeclaration // C++23 ) ); diff --git a/cxx-squid/src/test/java/org/sonar/cxx/parser/StatementTest.java b/cxx-squid/src/test/java/org/sonar/cxx/parser/StatementTest.java index a4194c825..326c34f27 100644 --- a/cxx-squid/src/test/java/org/sonar/cxx/parser/StatementTest.java +++ b/cxx-squid/src/test/java/org/sonar/cxx/parser/StatementTest.java @@ -251,7 +251,9 @@ void iterationStatement_reallife() { // CLI extension .matches("for each(String^% s in arr) { s = i++.ToString(); }") // C++17 structered bindings - .matches("for (const auto&[key, val] : mymap) { std::cout << key << \": \" << val << std::endl; }"); + .matches("for (const auto&[key, val] : mymap) { std::cout << key << \": \" << val << std::endl; }") + // C++23 + .matches("for (using T = int; T e : v) ;"); } @Test @@ -259,7 +261,8 @@ void forInitStatement_reallife() { setRootRule(CxxGrammarImpl.initStatement); assertThatParser() - .matches("int i=1;"); + .matches("int i=1;") + .matches("using T = int;"); // C++23 } @Test diff --git a/cxx-squid/src/test/resources/parser/own/C++23/init-statement.cc b/cxx-squid/src/test/resources/parser/own/C++23/init-statement.cc new file mode 100644 index 000000000..1c6511fc0 --- /dev/null +++ b/cxx-squid/src/test/resources/parser/own/C++23/init-statement.cc @@ -0,0 +1,4 @@ +void sample() { + for (using T = int; T e : v) + /* something */; +}