From 75b0430ba5ff0887de845f43b79bbfb51ad7e175 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 7 Jun 2015 11:25:33 +0200 Subject: [PATCH] Token::strValue: fixed handling of backslash --- lib/token.cpp | 38 ++++++++++++++++++++++++++++--------- lib/valueflow.cpp | 1 + test/testsimplifytokens.cpp | 2 +- test/testtoken.cpp | 17 +++++++++++++++++ 4 files changed, 48 insertions(+), 10 deletions(-) diff --git a/lib/token.cpp b/lib/token.cpp index c0ddb9952..18a18f600 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -150,7 +150,23 @@ void Token::concatStr(std::string const& b) std::string Token::strValue() const { assert(_type == eString); - return _str.substr(1, _str.length() - 2); + std::string ret(_str.substr(1, _str.length() - 2)); + std::string::size_type pos = 0U; + while ((pos = ret.find("\\",pos)) != std::string::npos) { + ret.erase(pos,1U); + if (ret[pos] >= 'a') { + if (ret[pos] == 'n') + ret[pos] = '\n'; + else if (ret[pos] == 'r') + ret[pos] = '\r'; + else if (ret[pos] == 't') + ret[pos] = '\t'; + } + if (ret[pos] == '0') + return ret.substr(0,pos); + pos++; + } + return ret; } void Token::deleteNext(unsigned long index) @@ -665,21 +681,25 @@ bool Token::Match(const Token *tok, const char pattern[], unsigned int varid) std::size_t Token::getStrLength(const Token *tok) { assert(tok != nullptr); + assert(tok->_type == eString); std::size_t len = 0; - const std::string strValue(tok->strValue()); - const char *str = strValue.c_str(); + std::string::const_iterator it = tok->str().begin() + 1U; + const std::string::const_iterator end = tok->str().end() - 1U; - while (*str) { - if (*str == '\\') { - ++str; + while (it != end) { + if (*it == '\\') { + ++it; // string ends at '\0' - if (*str == '0') - break; + if (*it == '0') + return len; } - ++str; + if (*it == '\0') + return len; + + ++it; ++len; } diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 00c2e03f9..ccf070f17 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -1548,6 +1548,7 @@ static bool valueFlowForLoop2(const Token *tok, std::map *memory2, std::map *memoryAfter) { + // for ( firstExpression ; secondExpression ; thirdExpression ) const Token *firstExpression = tok->next()->astOperand2()->astOperand1(); const Token *secondExpression = tok->next()->astOperand2()->astOperand2()->astOperand1(); const Token *thirdExpression = tok->next()->astOperand2()->astOperand2()->astOperand2(); diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index b55aa696f..cdbd55853 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -4263,7 +4263,7 @@ private: ASSERT_EQUALS("'\\0' ;", tok("\"hello\"[5] ;")); ASSERT_EQUALS("'\\0' ;", tok("\"\"[0] ;")); ASSERT_EQUALS("'\\0' ;", tok("\"\\0\"[0] ;")); - ASSERT_EQUALS("'\\n' ;", tok("\"hello\\nworld\"[5] ;")); + ASSERT_EQUALS("'\n' ;", tok("\"hello\\nworld\"[5] ;")); ASSERT_EQUALS("'w' ;", tok("\"hello\nworld\"[6] ;")); ASSERT_EQUALS("\"hello\" [ 7 ] ;", tok("\"hello\"[7] ;")); ASSERT_EQUALS("\"hello\" [ -1 ] ;", tok("\"hello\"[-1] ;")); diff --git a/test/testtoken.cpp b/test/testtoken.cpp index f0a693e5f..7444fbc3b 100644 --- a/test/testtoken.cpp +++ b/test/testtoken.cpp @@ -282,11 +282,28 @@ private: void strValue() const { Token tok(0); + tok.str("\"\""); ASSERT_EQUALS("", tok.strValue()); tok.str("\"0\""); ASSERT_EQUALS("0", tok.strValue()); + + tok.str("\"a\\n\""); + ASSERT_EQUALS("a\n", tok.strValue()); + + tok.str("\"a\\r\""); + ASSERT_EQUALS("a\r", tok.strValue()); + + tok.str("\"a\\t\""); + ASSERT_EQUALS("a\t", tok.strValue()); + + tok.str("\"\\\\\""); + ASSERT_EQUALS("\\", tok.strValue()); + + tok.str("\"a\\0\""); + ASSERT_EQUALS("a", tok.strValue()); + }