Skip to content

Commit 9e60f58

Browse files
IOBYTEPKEuS
authored andcommitted
Fixed danmar#6321: Implemented function Token::swapWithNext().
1 parent 14f13af commit 9e60f58

6 files changed

Lines changed: 67 additions & 11 deletions

File tree

lib/token.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,49 @@ void Token::deleteNext(unsigned long index)
166166
*tokensBack = this;
167167
}
168168

169+
void Token::swapWithNext()
170+
{
171+
if (_next) {
172+
Token temp(0);
173+
174+
temp._str = _next->_str;
175+
temp._type = _next->_type;
176+
temp._flags = _next->_flags;
177+
temp._varId = _next->_varId;
178+
temp._fileIndex = _next->_fileIndex;
179+
temp._link = _next->_link;
180+
temp._scope = _next->_scope;
181+
temp._function = _next->_function;
182+
temp._originalName = _next->_originalName;
183+
temp.values = _next->values;
184+
temp._progressValue = _next->_progressValue;
185+
186+
_next->_str = _str;
187+
_next->_type = _type;
188+
_next->_flags = _flags;
189+
_next->_varId = _varId;
190+
_next->_fileIndex = _fileIndex;
191+
_next->_link = _link;
192+
_next->_scope = _scope;
193+
_next->_function = _function;
194+
_next->_originalName = _originalName;
195+
_next->values = values;
196+
_next->_progressValue = _progressValue;
197+
198+
_str = temp._str;
199+
_type = temp._type;
200+
_flags = temp._flags;
201+
_varId = temp._varId;
202+
_fileIndex = temp._fileIndex;
203+
_link = temp._link;
204+
_scope = temp._scope;
205+
_function = temp._function;
206+
_originalName = temp._originalName;
207+
values = temp.values;
208+
_progressValue = temp._progressValue;
209+
}
210+
}
211+
169212
void Token::deleteThis()
170213
{
171214
if (_next) { // Copy next to this and delete next

lib/token.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ class CPPCHECKLIB Token {
9292
*/
9393
void deleteNext(unsigned long index = 1);
9494

95+
/**
96+
* Swap the contents of this token with the next token.
97+
*/
98+
void swapWithNext();
99+
95100
/**
96101
* @return token in given index, related to this token.
97102
* For example index 1 would return next token, and 2

lib/tokenize.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8946,18 +8946,15 @@ void Tokenizer::simplifyConst()
89468946
{
89478947
for (Token *tok = list.front(); tok; tok = tok->next()) {
89488948
if (tok->isStandardType() && tok->strAt(1) == "const") {
8949-
tok->next()->str(tok->str());
8950-
tok->str("const");
8949+
tok->swapWithNext();
89518950
} else if (Token::Match(tok, "struct %type% const")) {
8952-
tok->tokAt(2)->str(tok->next()->str());
8953-
tok->str("const");
8954-
tok->next()->str("struct");
8951+
tok->next()->swapWithNext();
8952+
tok->swapWithNext();
89558953
} else if (Token::Match(tok, "%type% const") &&
89568954
(!tok->previous() || Token::Match(tok->previous(), "[;{}(,]")) &&
89578955
tok->str().find(":") == std::string::npos &&
89588956
tok->str() != "operator") {
8959-
tok->next()->str(tok->str());
8960-
tok->str("const");
8957+
tok->swapWithNext();
89618958
}
89628959
}
89638960
}

test/testio.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class TestIO : public TestFixture {
6060
TEST_CASE(testMicrosoftSecureScanfArgument);
6161

6262
TEST_CASE(testTernary); // ticket #6182
63+
TEST_CASE(testUnsignedConst); // ticket #6132
6364
}
6465

6566
void check(const char code[], bool inconclusive = false, bool portability = false, Settings::PlatformType platform = Settings::Unspecified) {
@@ -3673,6 +3674,14 @@ class TestIO : public TestFixture {
36733674
ASSERT_EQUALS("", errout.str());
36743675
}
36753676

3677+
void testUnsignedConst() { // ticket #6321
3678+
check("void test() {\n"
3679+
" unsigned const x = 5;\n"
3680+
" printf(\"%u\", x);\n"
3681+
"}\n");
3682+
ASSERT_EQUALS("", errout.str());
3683+
}
3684+
36763685
};
36773686

36783687
REGISTER_TEST(TestIO)

test/testother.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,10 +1245,8 @@ class TestOther : public TestFixture {
12451245
" delete [] (double*)f;\n"
12461246
" delete [] (long double const*)(new float[10]);\n"
12471247
"}");
1248-
TODO_ASSERT_EQUALS("[test.cpp:3]: (portability) Casting between float* and double* which have an incompatible binary data representation.\n"
1249-
"[test.cpp:4]: (portability) Casting between float* and long double* which have an incompatible binary data representation.\n",
1250-
"[test.cpp:3]: (portability) Casting between float* and double* which have an incompatible binary data representation.\n"
1251-
"[test.cpp:4]: (portability) Casting between float* and double* which have an incompatible binary data representation.\n", errout.str());
1248+
ASSERT_EQUALS("[test.cpp:3]: (portability) Casting between float* and double* which have an incompatible binary data representation.\n"
1249+
"[test.cpp:4]: (portability) Casting between float* and long double* which have an incompatible binary data representation.\n", errout.str());
12521250

12531251
checkInvalidPointerCast("void test(const float* f) {\n"
12541252
" double *d = (double*)f;\n"

test/testtokenize.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4963,6 +4963,9 @@ class TestTokenizer : public TestFixture {
49634963
ASSERT_EQUALS("void foo ( ) { { } const long x ; }",
49644964
tokenizeAndStringify("void foo(){ {} long const x;}"));
49654965

4966+
ASSERT_EQUALS("void foo ( int b , const unsigned int x ) { }",
4967+
tokenizeAndStringify("void foo(int b,unsigned const x){}"));
4968+
49664969
ASSERT_EQUALS("void foo ( ) { bar ( ) ; const char x ; }",
49674970
tokenizeAndStringify("void foo(){ bar(); char const x;}"));
49684971

@@ -4978,6 +4981,7 @@ class TestTokenizer : public TestFixture {
49784981
ASSERT_EQUALS("const int foo ( ) ;", tokenizeAndStringify("int const foo ();"));
49794982

49804983
ASSERT_EQUALS("const int x ;", tokenizeAndStringify("int const x;"));
4984+
ASSERT_EQUALS("const unsigned int x ;", tokenizeAndStringify("unsigned const x;"));
49814985
ASSERT_EQUALS("const struct X x ;", tokenizeAndStringify("struct X const x;"));
49824986
}
49834987

0 commit comments

Comments
 (0)