Fixed #6321: Implemented function Token::swapWithNext().

This commit is contained in:
Robert Reif 2014-12-27 10:53:26 +01:00 committed by PKEuS
parent 14f13afa0a
commit 9e60f584d9
6 changed files with 67 additions and 11 deletions

View File

@ -166,6 +166,49 @@ void Token::deleteNext(unsigned long index)
*tokensBack = this; *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() void Token::deleteThis()
{ {
if (_next) { // Copy next to this and delete next if (_next) { // Copy next to this and delete next

View File

@ -92,6 +92,11 @@ public:
*/ */
void deleteNext(unsigned long index = 1); 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. * @return token in given index, related to this token.
* For example index 1 would return next token, and 2 * For example index 1 would return next token, and 2

View File

@ -8946,18 +8946,15 @@ void Tokenizer::simplifyConst()
{ {
for (Token *tok = list.front(); tok; tok = tok->next()) { for (Token *tok = list.front(); tok; tok = tok->next()) {
if (tok->isStandardType() && tok->strAt(1) == "const") { if (tok->isStandardType() && tok->strAt(1) == "const") {
tok->next()->str(tok->str()); tok->swapWithNext();
tok->str("const");
} else if (Token::Match(tok, "struct %type% const")) { } else if (Token::Match(tok, "struct %type% const")) {
tok->tokAt(2)->str(tok->next()->str()); tok->next()->swapWithNext();
tok->str("const"); tok->swapWithNext();
tok->next()->str("struct");
} else if (Token::Match(tok, "%type% const") && } else if (Token::Match(tok, "%type% const") &&
(!tok->previous() || Token::Match(tok->previous(), "[;{}(,]")) && (!tok->previous() || Token::Match(tok->previous(), "[;{}(,]")) &&
tok->str().find(":") == std::string::npos && tok->str().find(":") == std::string::npos &&
tok->str() != "operator") { tok->str() != "operator") {
tok->next()->str(tok->str()); tok->swapWithNext();
tok->str("const");
} }
} }
} }

View File

@ -60,6 +60,7 @@ private:
TEST_CASE(testMicrosoftSecureScanfArgument); TEST_CASE(testMicrosoftSecureScanfArgument);
TEST_CASE(testTernary); // ticket #6182 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) { 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()); 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) REGISTER_TEST(TestIO)

View File

@ -1245,10 +1245,8 @@ private:
" delete [] (double*)f;\n" " delete [] (double*)f;\n"
" delete [] (long double const*)(new float[10]);\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" 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:4]: (portability) Casting between float* and long double* which have an incompatible binary data representation.\n", errout.str());
"[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());
checkInvalidPointerCast("void test(const float* f) {\n" checkInvalidPointerCast("void test(const float* f) {\n"
" double *d = (double*)f;\n" " double *d = (double*)f;\n"

View File

@ -4963,6 +4963,9 @@ private:
ASSERT_EQUALS("void foo ( ) { { } const long x ; }", ASSERT_EQUALS("void foo ( ) { { } const long x ; }",
tokenizeAndStringify("void foo(){ {} long const 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 ; }", ASSERT_EQUALS("void foo ( ) { bar ( ) ; const char x ; }",
tokenizeAndStringify("void foo(){ bar(); char const 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 foo ( ) ;", tokenizeAndStringify("int const foo ();"));
ASSERT_EQUALS("const int x ;", tokenizeAndStringify("int const x;")); 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;")); ASSERT_EQUALS("const struct X x ;", tokenizeAndStringify("struct X const x;"));
} }