Copilot commented on code in PR #3786:
URL: https://github.com/apache/thrift/pull/3786#discussion_r3951753454


##########
compiler/cpp/tests/cpp/t_cpp_parser_string_constant_tests.cc:
##########
@@ -0,0 +1,61 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "t_cpp_generator_test_utils.h"
+
+#include <memory>
+#include <string>
+#include <vector>
+
+struct yy_buffer_state;
+extern yy_buffer_state* yy_scan_string(const char*);
+extern int yylex_destroy();
+extern int yylex();
+
+using cpp_generator_test_utils::join_path;
+using cpp_generator_test_utils::parse_thrift_for_test;
+using cpp_generator_test_utils::source_dir;
+
+TEST_CASE("parser preserves supported string escapes", "[parser]")
+{
+    const std::string path = join_path(source_dir(), 
"test_string_escapes.thrift");
+    std::unique_ptr<t_program> program(new t_program(path, 
"test_string_escapes"));
+    parse_thrift_for_test(program.get());
+
+    const std::vector<t_const*>& consts = program->get_consts();
+    REQUIRE(consts.size() == 4);
+    REQUIRE(consts[0]->get_value()->get_string() == "Y-m-d\\TH:i:s.uP");
+    REQUIRE(consts[1]->get_value()->get_string() == "Y-m-d\\TH:i:s.uP");
+    REQUIRE(consts[2]->get_value()->get_string() == "\r\n\t\"'\\");
+    REQUIRE(consts[3]->get_value()->get_string() == "\r\n\t\"'\\");
+}
+
+TEST_CASE("lexer explains how to escape a literal backslash", "[parser]")
+{
+    for (const std::string& escape : {"T", "x41", "0", "u00e9"}) {
+        for (const char quote : {'"', '\''}) {
+            const std::string source = std::string(1, quote) + "\\" + escape + 
quote;
+            INFO(source);
+            // Reset all scanner state, including after an expected parser 
exception.
+            yylex_destroy();
+            auto cleanup = [](yy_buffer_state*) { yylex_destroy(); };
+            std::unique_ptr<yy_buffer_state, decltype(cleanup)> buffer(
+                yy_scan_string(source.c_str()), cleanup);

Review Comment:
   `yylex_destroy()` is called twice for each iteration (once explicitly and 
once via the `buffer` deleter). The explicit call is redundant given the RAII 
cleanup and can also end up calling `yylex_destroy()` when no scanner/buffer is 
active, which is unnecessary and may be brittle across Flex implementations.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to