Skip to content

Commit dbb12cb

Browse files
committed
Tokenizer: Remove some functions from simplifyTokenList2
1 parent a2b50c1 commit dbb12cb

3 files changed

Lines changed: 0 additions & 261 deletions

File tree

lib/tokenize.cpp

Lines changed: 0 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -5349,11 +5349,6 @@ bool Tokenizer::simplifyTokenList2()
53495349
tok->clearValueFlow();
53505350
}
53515351

5352-
// simplify references
5353-
simplifyReference();
5354-
5355-
simplifyStd();
5356-
53575352
if (Settings::terminated())
53585353
return false;
53595354

@@ -5370,12 +5365,6 @@ bool Tokenizer::simplifyTokenList2()
53705365
if (Settings::terminated())
53715366
return false;
53725367

5373-
// Replace "*(ptr + num)" => "ptr[num]"
5374-
simplifyOffsetPointerDereference();
5375-
5376-
// Replace "&str[num]" => "(str + num)"
5377-
simplifyOffsetPointerReference();
5378-
53795368
simplifyRealloc();
53805369

53815370
// Change initialisation of variable to assignment
@@ -8991,128 +8980,11 @@ void Tokenizer::simplifyTypeIntrinsics()
89918980
}
89928981
}
89938982

8994-
void Tokenizer::simplifyReference()
8995-
{
8996-
if (isC())
8997-
return;
8998-
8999-
for (Token *tok = list.front(); tok; tok = tok->next()) {
9000-
// starting executable scope..
9001-
Token *start = const_cast<Token *>(startOfExecutableScope(tok));
9002-
if (start) {
9003-
tok = start;
9004-
// replace references in this scope..
9005-
Token * const end = tok->link();
9006-
for (Token *tok2 = tok; tok2 && tok2 != end; tok2 = tok2->next()) {
9007-
// found a reference..
9008-
if (Token::Match(tok2, "[;{}] %type% & %name% (|= %name% )| ;")) {
9009-
const int refId = tok2->tokAt(3)->varId();
9010-
if (!refId)
9011-
continue;
9012-
9013-
// replace reference in the code..
9014-
for (Token *tok3 = tok2->tokAt(7); tok3 && tok3 != end; tok3 = tok3->next()) {
9015-
if (tok3->varId() == refId) {
9016-
tok3->str(tok2->strAt(5));
9017-
tok3->varId(tok2->tokAt(5)->varId());
9018-
}
9019-
}
9020-
9021-
tok2->deleteNext(6+(tok2->strAt(6)==")" ? 1 : 0));
9022-
}
9023-
}
9024-
tok = end;
9025-
}
9026-
}
9027-
}
9028-
90298983
bool Tokenizer::simplifyCalculations()
90308984
{
90318985
return mTemplateSimplifier->simplifyCalculations(nullptr, nullptr, false);
90328986
}
90338987

9034-
void Tokenizer::simplifyOffsetPointerDereference()
9035-
{
9036-
// Replace "*(str + num)" => "str[num]" and
9037-
// Replace "*(str - num)" => "str[-num]"
9038-
for (Token *tok = list.front(); tok; tok = tok->next()) {
9039-
if (!tok->isName() && !tok->isLiteral()
9040-
&& !Token::Match(tok, "]|)|++|--")
9041-
&& Token::Match(tok->next(), "* ( %name% +|- %num%|%name% )")) {
9042-
9043-
// remove '* ('
9044-
tok->deleteNext(2);
9045-
9046-
// '+'->'['
9047-
tok = tok->tokAt(2);
9048-
Token* const openBraceTok = tok;
9049-
const bool isNegativeIndex = (tok->str() == "-");
9050-
tok->str("[");
9051-
9052-
// Insert a "-" in front of the number or variable
9053-
if (isNegativeIndex) {
9054-
if (tok->next()->isName()) {
9055-
tok->insertToken("-");
9056-
tok = tok->next();
9057-
} else
9058-
tok->next()->str(std::string("-") + tok->next()->str());
9059-
}
9060-
9061-
tok = tok->tokAt(2);
9062-
tok->str("]");
9063-
Token::createMutualLinks(openBraceTok, tok);
9064-
}
9065-
}
9066-
}
9067-
9068-
void Tokenizer::simplifyOffsetPointerReference()
9069-
{
9070-
std::set<int> pod;
9071-
for (const Token *tok = list.front(); tok; tok = tok->next()) {
9072-
if (tok->isStandardType()) {
9073-
tok = tok->next();
9074-
while (tok && (tok->str() == "*" || tok->isName())) {
9075-
if (tok->varId() > 0) {
9076-
pod.insert(tok->varId());
9077-
break;
9078-
}
9079-
tok = tok->next();
9080-
}
9081-
if (!tok)
9082-
break;
9083-
}
9084-
}
9085-
9086-
for (Token *tok = list.front(); tok; tok = tok->next()) {
9087-
if (!Token::Match(tok, "%num%|%name%|]|)") &&
9088-
(Token::Match(tok->next(), "& %name% [ %num%|%name% ] !!["))) {
9089-
tok = tok->next();
9090-
9091-
if (tok->next()->varId()) {
9092-
if (pod.find(tok->next()->varId()) == pod.end()) {
9093-
tok = tok->tokAt(5);
9094-
if (!tok)
9095-
syntaxError(tok);
9096-
continue;
9097-
}
9098-
}
9099-
9100-
// '&' => '('
9101-
tok->str("(");
9102-
9103-
tok = tok->next();
9104-
// '[' => '+'
9105-
tok->deleteNext();
9106-
tok->insertToken("+");
9107-
9108-
tok = tok->tokAt(3);
9109-
//remove ']'
9110-
tok->str(")");
9111-
Token::createMutualLinks(tok->tokAt(-4), tok);
9112-
}
9113-
}
9114-
}
9115-
91168988
void Tokenizer::simplifyNestedStrcat()
91178989
{
91188990
for (Token *tok = list.front(); tok; tok = tok->next()) {
@@ -9147,33 +9019,6 @@ void Tokenizer::simplifyNestedStrcat()
91479019
}
91489020
}
91499021

9150-
static const std::set<std::string> stdFunctionsPresentInC = {
9151-
"strcat",
9152-
"strcpy",
9153-
"strncat",
9154-
"strncpy",
9155-
"free",
9156-
"malloc",
9157-
"strdup"
9158-
};
9159-
9160-
void Tokenizer::simplifyStd()
9161-
{
9162-
if (isC())
9163-
return;
9164-
9165-
for (Token *tok = list.front(); tok; tok = tok->next()) {
9166-
if (tok->str() != "std")
9167-
continue;
9168-
9169-
if (Token::Match(tok->previous(), "[(,{};] std :: %name% (") &&
9170-
stdFunctionsPresentInC.find(tok->strAt(2)) != stdFunctionsPresentInC.end()) {
9171-
tok->deleteNext();
9172-
tok->deleteThis();
9173-
}
9174-
}
9175-
}
9176-
91779022
//---------------------------------------------------------------------------
91789023
// Helper functions for handling the tokens list
91799024
//---------------------------------------------------------------------------

lib/tokenize.h

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -202,19 +202,6 @@ class CPPCHECKLIB Tokenizer {
202202
*/
203203
bool simplifyCalculations();
204204

205-
/**
206-
* Simplify dereferencing a pointer offset by a number:
207-
* "*(ptr + num)" => "ptr[num]"
208-
* "*(ptr - num)" => "ptr[-num]"
209-
*/
210-
void simplifyOffsetPointerDereference();
211-
212-
/**
213-
* Simplify referencing a pointer offset:
214-
* "Replace "&str[num]" => "(str + num)"
215-
*/
216-
void simplifyOffsetPointerReference();
217-
218205
/** Insert array size where it isn't given */
219206
void arraySize();
220207

@@ -458,9 +445,6 @@ class CPPCHECKLIB Tokenizer {
458445
*/
459446
bool simplifyRedundantParentheses();
460447

461-
/** Simplify references */
462-
void simplifyReference();
463-
464448
/**
465449
* Simplify functions like "void f(x) int x; {"
466450
* into "void f(int x) {"
@@ -557,11 +541,6 @@ class CPPCHECKLIB Tokenizer {
557541
*/
558542
void simplifyFuncInWhile();
559543

560-
/**
561-
* Remove "std::" before some function names
562-
*/
563-
void simplifyStd();
564-
565544
/** Simplify pointer to standard type (C only) */
566545
void simplifyPointerToStandardType();
567546

test/testsimplifytokens.cpp

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ class TestSimplifyTokens : public TestFixture {
5252
settings_std.checkUnusedTemplates = true;
5353
settings_windows.checkUnusedTemplates = true;
5454

55-
TEST_CASE(test1); // array access. replace "*(p+1)" => "p[1]"
56-
5755
TEST_CASE(cast);
5856
TEST_CASE(iftruefalse);
5957

@@ -162,12 +160,6 @@ class TestSimplifyTokens : public TestFixture {
162160
// ticket #3140
163161
TEST_CASE(while0for);
164162

165-
// remove "std::" on some standard functions
166-
TEST_CASE(removestd);
167-
168-
// Tokenizer::simplifyReference
169-
TEST_CASE(simplifyReference);
170-
171163
// x = realloc(y,0); => free(y);x=0;
172164
TEST_CASE(simplifyRealloc);
173165

@@ -212,7 +204,6 @@ class TestSimplifyTokens : public TestFixture {
212204

213205
TEST_CASE(undefinedSizeArray);
214206

215-
TEST_CASE(simplifyArrayAddress); // Replace "&str[num]" => "(str + num)"
216207
TEST_CASE(simplifyOverride); // ticket #5069
217208
TEST_CASE(simplifyNestedNamespace);
218209
TEST_CASE(simplifyNamespaceAliases1);
@@ -432,52 +423,6 @@ class TestSimplifyTokens : public TestFixture {
432423
}
433424

434425

435-
void test1() {
436-
// "&p[1]" => "p+1"
437-
/*
438-
ASSERT_EQUALS("; x = p + n ;", tok("; x = & p [ n ] ;"));
439-
ASSERT_EQUALS("; x = ( p + n ) [ m ] ;", tok("; x = & p [ n ] [ m ] ;"));
440-
ASSERT_EQUALS("; x = y & p [ n ] ;", tok("; x = y & p [ n ] ;"));
441-
ASSERT_EQUALS("; x = 10 & p [ n ] ;", tok("; x = 10 & p [ n ] ;"));
442-
ASSERT_EQUALS("; x = y [ 10 ] & p [ n ] ;", tok("; x = y [ 10 ] & p [ n ] ;"));
443-
ASSERT_EQUALS("; x = ( a + m ) & p [ n ] ;", tok("; x = ( a + m ) & p [ n ] ;"));
444-
*/
445-
// "*(p+1)" => "p[1]"
446-
ASSERT_EQUALS("; x = p [ 1 ] ;", tok("; x = * ( p + 1 ) ;"));
447-
ASSERT_EQUALS("; x = p [ 0xA ] ;", tok("; x = * ( p + 0xA ) ;"));
448-
ASSERT_EQUALS("; x = p [ n ] ;", tok("; x = * ( p + n ) ;"));
449-
ASSERT_EQUALS("; x = y * ( p + n ) ;", tok("; x = y * ( p + n ) ;"));
450-
ASSERT_EQUALS("; x = 10 * ( p + n ) ;", tok("; x = 10 * ( p + n ) ;"));
451-
ASSERT_EQUALS("; x = y [ 10 ] * ( p + n ) ;", tok("; x = y [ 10 ] * ( p + n ) ;"));
452-
ASSERT_EQUALS("; x = ( a + m ) * ( p + n ) ;", tok("; x = ( a + m ) * ( p + n ) ;"));
453-
454-
// "*(p-1)" => "p[-1]" and "*(p-n)" => "p[-n]"
455-
ASSERT_EQUALS("; x = p [ -1 ] ;", tok("; x = *(p - 1);"));
456-
ASSERT_EQUALS("; x = p [ -0xA ] ;", tok("; x = *(p - 0xA);"));
457-
ASSERT_EQUALS("; x = p [ - n ] ;", tok("; x = *(p - n);"));
458-
ASSERT_EQUALS("; x = y * ( p - 1 ) ;", tok("; x = y * (p - 1);"));
459-
ASSERT_EQUALS("; x = 10 * ( p - 1 ) ;", tok("; x = 10 * (p - 1);"));
460-
ASSERT_EQUALS("; x = y [ 10 ] * ( p - 1 ) ;", tok("; x = y[10] * (p - 1);"));
461-
ASSERT_EQUALS("; x = ( a - m ) * ( p - n ) ;", tok("; x = (a - m) * (p - n);"));
462-
463-
// Test that the array-index simplification is not applied when there's no dereference:
464-
// "(x-y)" => "(x-y)" and "(x+y)" => "(x+y)"
465-
ASSERT_EQUALS("; a = b * ( x - y ) ;", tok("; a = b * (x - y);"));
466-
ASSERT_EQUALS("; a = b * x [ - y ] ;", tok("; a = b * *(x - y);"));
467-
ASSERT_EQUALS("; a *= ( x - y ) ;", tok("; a *= (x - y);"));
468-
ASSERT_EQUALS("; z = a ++ * ( x - y ) ;", tok("; z = a++ * (x - y);"));
469-
ASSERT_EQUALS("; z = a ++ * ( x + y ) ;", tok("; z = a++ * (x + y);"));
470-
ASSERT_EQUALS("; z = a -- * ( x - y ) ;", tok("; z = a-- * (x - y);"));
471-
ASSERT_EQUALS("; z = a -- * ( x + y ) ;", tok("; z = a-- * (x + y);"));
472-
ASSERT_EQUALS("; z = 'a' * ( x - y ) ;", tok("; z = 'a' * (x - y);"));
473-
ASSERT_EQUALS("; z = \"a\" * ( x - y ) ;", tok("; z = \"a\" * (x - y);"));
474-
ASSERT_EQUALS("; z = 'a' * ( x + y ) ;", tok("; z = 'a' * (x + y);"));
475-
ASSERT_EQUALS("; z = \"a\" * ( x + y ) ;", tok("; z = \"a\" * (x + y);"));
476-
ASSERT_EQUALS("; z = foo ( ) * ( x + y ) ;", tok("; z = foo() * (x + y);"));
477-
}
478-
479-
480-
481426
void simplifyMathFunctions_erfc() {
482427
// verify erfc(), erfcf(), erfcl() - simplifcation
483428
const char code_erfc[] ="void f(int x) {\n"
@@ -4194,25 +4139,6 @@ class TestSimplifyTokens : public TestFixture {
41944139
ASSERT_EQUALS("void f ( ) { int i ; for ( i = 0 ; i < 0 ; ++ i ) { } return i ; }", tok("void f() { int i; for (i=0;i<0;++i){ dostuff(); } return i; }"));
41954140
}
41964141

4197-
void removestd() {
4198-
ASSERT_EQUALS("; strcpy ( a , b ) ;", tok("; std::strcpy(a,b);"));
4199-
ASSERT_EQUALS("; strcat ( a , b ) ;", tok("; std::strcat(a,b);"));
4200-
ASSERT_EQUALS("; strncpy ( a , b , 10 ) ;", tok("; std::strncpy(a,b,10);"));
4201-
ASSERT_EQUALS("; strncat ( a , b , 10 ) ;", tok("; std::strncat(a,b,10);"));
4202-
ASSERT_EQUALS("; free ( p ) ;", tok("; std::free(p);"));
4203-
ASSERT_EQUALS("; malloc ( 10 ) ;", tok("; std::malloc(10);"));
4204-
}
4205-
4206-
void simplifyReference() {
4207-
ASSERT_EQUALS("void f ( ) { int a ; a ++ ; }",
4208-
tok("void f() { int a; int &b(a); b++; }"));
4209-
ASSERT_EQUALS("void f ( ) { int a ; a ++ ; }",
4210-
tok("void f() { int a; int &b = a; b++; }"));
4211-
4212-
ASSERT_EQUALS("void test ( ) { c . f ( 7 ) ; }",
4213-
tok("void test() { c.f(7); T3 &t3 = c; }")); // #6133
4214-
}
4215-
42164142
void simplifyRealloc() {
42174143
ASSERT_EQUALS("; free ( p ) ; p = 0 ;", tok("; p = realloc(p, 0);"));
42184144
ASSERT_EQUALS("; p = malloc ( 100 ) ;", tok("; p = realloc(0, 100);"));
@@ -4838,17 +4764,6 @@ class TestSimplifyTokens : public TestFixture {
48384764
ASSERT_EQUALS("int x [ 13 ] = { [ 11 ] = 2 , [ 12 ] = 3 } ;", tok("int x[] = {[11]=2, [12]=3};"));
48394765
}
48404766

4841-
void simplifyArrayAddress() { // ticket #3304
4842-
const char code[] = "void foo() {\n"
4843-
" int a[10];\n"
4844-
" memset(&a[4], 0, 20*sizeof(int));\n"
4845-
"}";
4846-
ASSERT_EQUALS("void foo ( ) {"
4847-
" int a [ 10 ] ;"
4848-
" memset ( a + 4 , 0 , 80 ) ;"
4849-
" }", tok(code, true));
4850-
}
4851-
48524767
void test_4881() {
48534768
const char code[] = "int evallex() {\n"
48544769
" int c, t;\n"

0 commit comments

Comments
 (0)