Tokenizer: Remove some functions from simplifyTokenList2

This commit is contained in:
Daniel Marjamäki 2022-06-16 14:06:07 +02:00
parent a2b50c1ccf
commit dbb12cb333
3 changed files with 0 additions and 261 deletions

View File

@ -5349,11 +5349,6 @@ bool Tokenizer::simplifyTokenList2()
tok->clearValueFlow();
}
// simplify references
simplifyReference();
simplifyStd();
if (Settings::terminated())
return false;
@ -5370,12 +5365,6 @@ bool Tokenizer::simplifyTokenList2()
if (Settings::terminated())
return false;
// Replace "*(ptr + num)" => "ptr[num]"
simplifyOffsetPointerDereference();
// Replace "&str[num]" => "(str + num)"
simplifyOffsetPointerReference();
simplifyRealloc();
// Change initialisation of variable to assignment
@ -8991,128 +8980,11 @@ void Tokenizer::simplifyTypeIntrinsics()
}
}
void Tokenizer::simplifyReference()
{
if (isC())
return;
for (Token *tok = list.front(); tok; tok = tok->next()) {
// starting executable scope..
Token *start = const_cast<Token *>(startOfExecutableScope(tok));
if (start) {
tok = start;
// replace references in this scope..
Token * const end = tok->link();
for (Token *tok2 = tok; tok2 && tok2 != end; tok2 = tok2->next()) {
// found a reference..
if (Token::Match(tok2, "[;{}] %type% & %name% (|= %name% )| ;")) {
const int refId = tok2->tokAt(3)->varId();
if (!refId)
continue;
// replace reference in the code..
for (Token *tok3 = tok2->tokAt(7); tok3 && tok3 != end; tok3 = tok3->next()) {
if (tok3->varId() == refId) {
tok3->str(tok2->strAt(5));
tok3->varId(tok2->tokAt(5)->varId());
}
}
tok2->deleteNext(6+(tok2->strAt(6)==")" ? 1 : 0));
}
}
tok = end;
}
}
}
bool Tokenizer::simplifyCalculations()
{
return mTemplateSimplifier->simplifyCalculations(nullptr, nullptr, false);
}
void Tokenizer::simplifyOffsetPointerDereference()
{
// Replace "*(str + num)" => "str[num]" and
// Replace "*(str - num)" => "str[-num]"
for (Token *tok = list.front(); tok; tok = tok->next()) {
if (!tok->isName() && !tok->isLiteral()
&& !Token::Match(tok, "]|)|++|--")
&& Token::Match(tok->next(), "* ( %name% +|- %num%|%name% )")) {
// remove '* ('
tok->deleteNext(2);
// '+'->'['
tok = tok->tokAt(2);
Token* const openBraceTok = tok;
const bool isNegativeIndex = (tok->str() == "-");
tok->str("[");
// Insert a "-" in front of the number or variable
if (isNegativeIndex) {
if (tok->next()->isName()) {
tok->insertToken("-");
tok = tok->next();
} else
tok->next()->str(std::string("-") + tok->next()->str());
}
tok = tok->tokAt(2);
tok->str("]");
Token::createMutualLinks(openBraceTok, tok);
}
}
}
void Tokenizer::simplifyOffsetPointerReference()
{
std::set<int> pod;
for (const Token *tok = list.front(); tok; tok = tok->next()) {
if (tok->isStandardType()) {
tok = tok->next();
while (tok && (tok->str() == "*" || tok->isName())) {
if (tok->varId() > 0) {
pod.insert(tok->varId());
break;
}
tok = tok->next();
}
if (!tok)
break;
}
}
for (Token *tok = list.front(); tok; tok = tok->next()) {
if (!Token::Match(tok, "%num%|%name%|]|)") &&
(Token::Match(tok->next(), "& %name% [ %num%|%name% ] !!["))) {
tok = tok->next();
if (tok->next()->varId()) {
if (pod.find(tok->next()->varId()) == pod.end()) {
tok = tok->tokAt(5);
if (!tok)
syntaxError(tok);
continue;
}
}
// '&' => '('
tok->str("(");
tok = tok->next();
// '[' => '+'
tok->deleteNext();
tok->insertToken("+");
tok = tok->tokAt(3);
//remove ']'
tok->str(")");
Token::createMutualLinks(tok->tokAt(-4), tok);
}
}
}
void Tokenizer::simplifyNestedStrcat()
{
for (Token *tok = list.front(); tok; tok = tok->next()) {
@ -9147,33 +9019,6 @@ void Tokenizer::simplifyNestedStrcat()
}
}
static const std::set<std::string> stdFunctionsPresentInC = {
"strcat",
"strcpy",
"strncat",
"strncpy",
"free",
"malloc",
"strdup"
};
void Tokenizer::simplifyStd()
{
if (isC())
return;
for (Token *tok = list.front(); tok; tok = tok->next()) {
if (tok->str() != "std")
continue;
if (Token::Match(tok->previous(), "[(,{};] std :: %name% (") &&
stdFunctionsPresentInC.find(tok->strAt(2)) != stdFunctionsPresentInC.end()) {
tok->deleteNext();
tok->deleteThis();
}
}
}
//---------------------------------------------------------------------------
// Helper functions for handling the tokens list
//---------------------------------------------------------------------------

View File

@ -202,19 +202,6 @@ public:
*/
bool simplifyCalculations();
/**
* Simplify dereferencing a pointer offset by a number:
* "*(ptr + num)" => "ptr[num]"
* "*(ptr - num)" => "ptr[-num]"
*/
void simplifyOffsetPointerDereference();
/**
* Simplify referencing a pointer offset:
* "Replace "&str[num]" => "(str + num)"
*/
void simplifyOffsetPointerReference();
/** Insert array size where it isn't given */
void arraySize();
@ -458,9 +445,6 @@ public:
*/
bool simplifyRedundantParentheses();
/** Simplify references */
void simplifyReference();
/**
* Simplify functions like "void f(x) int x; {"
* into "void f(int x) {"
@ -557,11 +541,6 @@ private:
*/
void simplifyFuncInWhile();
/**
* Remove "std::" before some function names
*/
void simplifyStd();
/** Simplify pointer to standard type (C only) */
void simplifyPointerToStandardType();

View File

@ -52,8 +52,6 @@ private:
settings_std.checkUnusedTemplates = true;
settings_windows.checkUnusedTemplates = true;
TEST_CASE(test1); // array access. replace "*(p+1)" => "p[1]"
TEST_CASE(cast);
TEST_CASE(iftruefalse);
@ -162,12 +160,6 @@ private:
// ticket #3140
TEST_CASE(while0for);
// remove "std::" on some standard functions
TEST_CASE(removestd);
// Tokenizer::simplifyReference
TEST_CASE(simplifyReference);
// x = realloc(y,0); => free(y);x=0;
TEST_CASE(simplifyRealloc);
@ -212,7 +204,6 @@ private:
TEST_CASE(undefinedSizeArray);
TEST_CASE(simplifyArrayAddress); // Replace "&str[num]" => "(str + num)"
TEST_CASE(simplifyOverride); // ticket #5069
TEST_CASE(simplifyNestedNamespace);
TEST_CASE(simplifyNamespaceAliases1);
@ -432,52 +423,6 @@ private:
}
void test1() {
// "&p[1]" => "p+1"
/*
ASSERT_EQUALS("; x = p + n ;", tok("; x = & p [ n ] ;"));
ASSERT_EQUALS("; x = ( p + n ) [ m ] ;", tok("; x = & p [ n ] [ m ] ;"));
ASSERT_EQUALS("; x = y & p [ n ] ;", tok("; x = y & p [ n ] ;"));
ASSERT_EQUALS("; x = 10 & p [ n ] ;", tok("; x = 10 & p [ n ] ;"));
ASSERT_EQUALS("; x = y [ 10 ] & p [ n ] ;", tok("; x = y [ 10 ] & p [ n ] ;"));
ASSERT_EQUALS("; x = ( a + m ) & p [ n ] ;", tok("; x = ( a + m ) & p [ n ] ;"));
*/
// "*(p+1)" => "p[1]"
ASSERT_EQUALS("; x = p [ 1 ] ;", tok("; x = * ( p + 1 ) ;"));
ASSERT_EQUALS("; x = p [ 0xA ] ;", tok("; x = * ( p + 0xA ) ;"));
ASSERT_EQUALS("; x = p [ n ] ;", tok("; x = * ( p + n ) ;"));
ASSERT_EQUALS("; x = y * ( p + n ) ;", tok("; x = y * ( p + n ) ;"));
ASSERT_EQUALS("; x = 10 * ( p + n ) ;", tok("; x = 10 * ( p + n ) ;"));
ASSERT_EQUALS("; x = y [ 10 ] * ( p + n ) ;", tok("; x = y [ 10 ] * ( p + n ) ;"));
ASSERT_EQUALS("; x = ( a + m ) * ( p + n ) ;", tok("; x = ( a + m ) * ( p + n ) ;"));
// "*(p-1)" => "p[-1]" and "*(p-n)" => "p[-n]"
ASSERT_EQUALS("; x = p [ -1 ] ;", tok("; x = *(p - 1);"));
ASSERT_EQUALS("; x = p [ -0xA ] ;", tok("; x = *(p - 0xA);"));
ASSERT_EQUALS("; x = p [ - n ] ;", tok("; x = *(p - n);"));
ASSERT_EQUALS("; x = y * ( p - 1 ) ;", tok("; x = y * (p - 1);"));
ASSERT_EQUALS("; x = 10 * ( p - 1 ) ;", tok("; x = 10 * (p - 1);"));
ASSERT_EQUALS("; x = y [ 10 ] * ( p - 1 ) ;", tok("; x = y[10] * (p - 1);"));
ASSERT_EQUALS("; x = ( a - m ) * ( p - n ) ;", tok("; x = (a - m) * (p - n);"));
// Test that the array-index simplification is not applied when there's no dereference:
// "(x-y)" => "(x-y)" and "(x+y)" => "(x+y)"
ASSERT_EQUALS("; a = b * ( x - y ) ;", tok("; a = b * (x - y);"));
ASSERT_EQUALS("; a = b * x [ - y ] ;", tok("; a = b * *(x - y);"));
ASSERT_EQUALS("; a *= ( x - y ) ;", tok("; a *= (x - y);"));
ASSERT_EQUALS("; z = a ++ * ( x - y ) ;", tok("; z = a++ * (x - y);"));
ASSERT_EQUALS("; z = a ++ * ( x + y ) ;", tok("; z = a++ * (x + y);"));
ASSERT_EQUALS("; z = a -- * ( x - y ) ;", tok("; z = a-- * (x - y);"));
ASSERT_EQUALS("; z = a -- * ( x + y ) ;", tok("; z = a-- * (x + y);"));
ASSERT_EQUALS("; z = 'a' * ( x - y ) ;", tok("; z = 'a' * (x - y);"));
ASSERT_EQUALS("; z = \"a\" * ( x - y ) ;", tok("; z = \"a\" * (x - y);"));
ASSERT_EQUALS("; z = 'a' * ( x + y ) ;", tok("; z = 'a' * (x + y);"));
ASSERT_EQUALS("; z = \"a\" * ( x + y ) ;", tok("; z = \"a\" * (x + y);"));
ASSERT_EQUALS("; z = foo ( ) * ( x + y ) ;", tok("; z = foo() * (x + y);"));
}
void simplifyMathFunctions_erfc() {
// verify erfc(), erfcf(), erfcl() - simplifcation
const char code_erfc[] ="void f(int x) {\n"
@ -4194,25 +4139,6 @@ private:
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; }"));
}
void removestd() {
ASSERT_EQUALS("; strcpy ( a , b ) ;", tok("; std::strcpy(a,b);"));
ASSERT_EQUALS("; strcat ( a , b ) ;", tok("; std::strcat(a,b);"));
ASSERT_EQUALS("; strncpy ( a , b , 10 ) ;", tok("; std::strncpy(a,b,10);"));
ASSERT_EQUALS("; strncat ( a , b , 10 ) ;", tok("; std::strncat(a,b,10);"));
ASSERT_EQUALS("; free ( p ) ;", tok("; std::free(p);"));
ASSERT_EQUALS("; malloc ( 10 ) ;", tok("; std::malloc(10);"));
}
void simplifyReference() {
ASSERT_EQUALS("void f ( ) { int a ; a ++ ; }",
tok("void f() { int a; int &b(a); b++; }"));
ASSERT_EQUALS("void f ( ) { int a ; a ++ ; }",
tok("void f() { int a; int &b = a; b++; }"));
ASSERT_EQUALS("void test ( ) { c . f ( 7 ) ; }",
tok("void test() { c.f(7); T3 &t3 = c; }")); // #6133
}
void simplifyRealloc() {
ASSERT_EQUALS("; free ( p ) ; p = 0 ;", tok("; p = realloc(p, 0);"));
ASSERT_EQUALS("; p = malloc ( 100 ) ;", tok("; p = realloc(0, 100);"));
@ -4838,17 +4764,6 @@ private:
ASSERT_EQUALS("int x [ 13 ] = { [ 11 ] = 2 , [ 12 ] = 3 } ;", tok("int x[] = {[11]=2, [12]=3};"));
}
void simplifyArrayAddress() { // ticket #3304
const char code[] = "void foo() {\n"
" int a[10];\n"
" memset(&a[4], 0, 20*sizeof(int));\n"
"}";
ASSERT_EQUALS("void foo ( ) {"
" int a [ 10 ] ;"
" memset ( a + 4 , 0 , 80 ) ;"
" }", tok(code, true));
}
void test_4881() {
const char code[] = "int evallex() {\n"
" int c, t;\n"