From 9e60f584d9f2a56de8a7d8c12d2c1f8029224e16 Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Sat, 27 Dec 2014 10:53:26 +0100 Subject: [PATCH] Fixed #6321: Implemented function Token::swapWithNext(). --- lib/token.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ lib/token.h | 5 +++++ lib/tokenize.cpp | 11 ++++------- test/testio.cpp | 9 +++++++++ test/testother.cpp | 6 ++---- test/testtokenize.cpp | 4 ++++ 6 files changed, 67 insertions(+), 11 deletions(-) diff --git a/lib/token.cpp b/lib/token.cpp index 32bb60e44..6075f0302 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -166,6 +166,49 @@ void Token::deleteNext(unsigned long index) *tokensBack = this; } +void Token::swapWithNext() +{ + if (_next) { + Token temp(0); + + temp._str = _next->_str; + temp._type = _next->_type; + temp._flags = _next->_flags; + temp._varId = _next->_varId; + temp._fileIndex = _next->_fileIndex; + temp._link = _next->_link; + temp._scope = _next->_scope; + temp._function = _next->_function; + temp._originalName = _next->_originalName; + temp.values = _next->values; + temp._progressValue = _next->_progressValue; + + _next->_str = _str; + _next->_type = _type; + _next->_flags = _flags; + _next->_varId = _varId; + _next->_fileIndex = _fileIndex; + _next->_link = _link; + _next->_scope = _scope; + _next->_function = _function; + _next->_originalName = _originalName; + _next->values = values; + _next->_progressValue = _progressValue; + + _str = temp._str; + _type = temp._type; + _flags = temp._flags; + _varId = temp._varId; + _fileIndex = temp._fileIndex; + _link = temp._link; + _scope = temp._scope; + _function = temp._function; + _originalName = temp._originalName; + values = temp.values; + _progressValue = temp._progressValue; + } +} + void Token::deleteThis() { if (_next) { // Copy next to this and delete next diff --git a/lib/token.h b/lib/token.h index e4a512aaa..7c8cd64e5 100644 --- a/lib/token.h +++ b/lib/token.h @@ -92,6 +92,11 @@ public: */ void deleteNext(unsigned long index = 1); + /** + * Swap the contents of this token with the next token. + */ + void swapWithNext(); + /** * @return token in given index, related to this token. * For example index 1 would return next token, and 2 diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 2e0f957b8..1b5ef5449 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -8946,18 +8946,15 @@ void Tokenizer::simplifyConst() { for (Token *tok = list.front(); tok; tok = tok->next()) { if (tok->isStandardType() && tok->strAt(1) == "const") { - tok->next()->str(tok->str()); - tok->str("const"); + tok->swapWithNext(); } else if (Token::Match(tok, "struct %type% const")) { - tok->tokAt(2)->str(tok->next()->str()); - tok->str("const"); - tok->next()->str("struct"); + tok->next()->swapWithNext(); + tok->swapWithNext(); } else if (Token::Match(tok, "%type% const") && (!tok->previous() || Token::Match(tok->previous(), "[;{}(,]")) && tok->str().find(":") == std::string::npos && tok->str() != "operator") { - tok->next()->str(tok->str()); - tok->str("const"); + tok->swapWithNext(); } } } diff --git a/test/testio.cpp b/test/testio.cpp index 1b2b767bb..9b72d72c2 100644 --- a/test/testio.cpp +++ b/test/testio.cpp @@ -60,6 +60,7 @@ private: TEST_CASE(testMicrosoftSecureScanfArgument); TEST_CASE(testTernary); // ticket #6182 + TEST_CASE(testUnsignedConst); // ticket #6132 } void check(const char code[], bool inconclusive = false, bool portability = false, Settings::PlatformType platform = Settings::Unspecified) { @@ -3673,6 +3674,14 @@ private: ASSERT_EQUALS("", errout.str()); } + void testUnsignedConst() { // ticket #6321 + check("void test() {\n" + " unsigned const x = 5;\n" + " printf(\"%u\", x);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + }; REGISTER_TEST(TestIO) diff --git a/test/testother.cpp b/test/testother.cpp index 7c2605442..4db3fe5b6 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -1245,10 +1245,8 @@ private: " delete [] (double*)f;\n" " delete [] (long double const*)(new float[10]);\n" "}"); - TODO_ASSERT_EQUALS("[test.cpp:3]: (portability) Casting between float* and double* which have an incompatible binary data representation.\n" - "[test.cpp:4]: (portability) Casting between float* and long double* which have an incompatible binary data representation.\n", - "[test.cpp:3]: (portability) Casting between float* and double* which have an incompatible binary data representation.\n" - "[test.cpp:4]: (portability) Casting between float* and double* which have an incompatible binary data representation.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (portability) Casting between float* and double* which have an incompatible binary data representation.\n" + "[test.cpp:4]: (portability) Casting between float* and long double* which have an incompatible binary data representation.\n", errout.str()); checkInvalidPointerCast("void test(const float* f) {\n" " double *d = (double*)f;\n" diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index a5d6653d1..3dc00bf40 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -4963,6 +4963,9 @@ private: ASSERT_EQUALS("void foo ( ) { { } const long x ; }", tokenizeAndStringify("void foo(){ {} long const x;}")); + ASSERT_EQUALS("void foo ( int b , const unsigned int x ) { }", + tokenizeAndStringify("void foo(int b,unsigned const x){}")); + ASSERT_EQUALS("void foo ( ) { bar ( ) ; const char x ; }", tokenizeAndStringify("void foo(){ bar(); char const x;}")); @@ -4978,6 +4981,7 @@ private: ASSERT_EQUALS("const int foo ( ) ;", tokenizeAndStringify("int const foo ();")); ASSERT_EQUALS("const int x ;", tokenizeAndStringify("int const x;")); + ASSERT_EQUALS("const unsigned int x ;", tokenizeAndStringify("unsigned const x;")); ASSERT_EQUALS("const struct X x ;", tokenizeAndStringify("struct X const x;")); }