From 68e6a440ff0dbc1ac25d9368d82e270d515d49c6 Mon Sep 17 00:00:00 2001 From: Denis <18046412+gshadows@users.noreply.github.com> Date: Mon, 15 Jul 2019 10:29:31 +0300 Subject: [PATCH] Fix adding unescaped slash token when splitting gcc case range. (#1987) * Fix adding unescaped slash token when splitting gcc case range. Construction like case '!'...'~' converted to a list of separate case tokens. When slas '\' symbol appears as a part of this list it was added "as is", but it should be escaped like '\\' to be valid c++ code. * Add test for switch-case range with slash --- lib/tokenize.cpp | 6 +++++- test/testtokenize.cpp | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 50a60c67d..eaf7b8add 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2821,7 +2821,11 @@ void Tokenizer::simplifyCaseRange() tok->next()->str("case"); for (char i = end - 1; i > start; i--) { tok->insertToken(":"); - tok->insertToken(std::string(1, '\'') + i + '\''); + if (i == '\\') { + tok->insertToken(std::string("\'\\") + i + '\''); + } else { + tok->insertToken(std::string(1, '\'') + i + '\''); + } tok->insertToken("case"); } } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index b702a008e..209094064 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -7114,6 +7114,8 @@ private: ASSERT_EQUALS("void f ( ) { switch ( x ) { case 'a' : case 'b' : case 'c' : ; } }", tokenizeAndStringify("void f() { switch(x) { case 'a' ... 'c': } }")); ASSERT_EQUALS("void f ( ) { switch ( x ) { case 'c' . . . 'a' : ; } }", tokenizeAndStringify("void f() { switch(x) { case 'c' ... 'a': } }")); + + ASSERT_EQUALS("void f ( ) { switch ( x ) { case '[' : case '\\\\' : case ']' : ; } }", tokenizeAndStringify("void f() { switch(x) { case '[' ... ']': } }")); } void prepareTernaryOpForAST() {