Skip to content

Commit a4a6f3e

Browse files
committed
Tokenizer: Removed Tokenizer::simplifyConditionOperator(). Using the AST and ValueFlow, it should be much easier to parse ?: than before.
1 parent 4daf775 commit a4a6f3e

4 files changed

Lines changed: 1 addition & 162 deletions

File tree

lib/tokenize.cpp

Lines changed: 0 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -3436,9 +3436,6 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
34363436
// syntax is corrected.
34373437
TemplateSimplifier::cleanupAfterSimplify(list.front());
34383438

3439-
// Simplify the operator "?:"
3440-
simplifyConditionOperator();
3441-
34423439
// Collapse operator name tokens into single token
34433440
// operator = => operator=
34443441
simplifyOperatorName();
@@ -3670,8 +3667,6 @@ bool Tokenizer::simplifyTokenList2()
36703667
modified |= simplifyCalculations();
36713668
}
36723669

3673-
simplifyConditionOperator();
3674-
36753670
// simplify redundant for
36763671
removeRedundantFor();
36773672

@@ -4469,105 +4464,6 @@ void Tokenizer::simplifyCompoundAssignment()
44694464
}
44704465
}
44714466

4472-
void Tokenizer::simplifyConditionOperator()
4473-
{
4474-
for (Token *tok = list.front(); tok; tok = tok->next()) {
4475-
if (tok->str() == "(" || tok->str() == "[" ||
4476-
(tok->str() == "{" && tok->previous() && tok->previous()->str() == "="))
4477-
tok = tok->link();
4478-
4479-
if (Token::Match(tok, "[{};] *| %var% = %any% ? %any% : %any% ;") ||
4480-
Token::Match(tok, "[{};] return %any% ? %any% : %any% ;")) {
4481-
4482-
// backup varids so they can be set properly
4483-
std::map<std::string, unsigned int> varid;
4484-
for (const Token *tok2 = tok->next(); tok2->str() != ";"; tok2 = tok2->next()) {
4485-
if (tok2->varId())
4486-
varid[tok2->str()] = tok2->varId();
4487-
}
4488-
4489-
std::string var(tok->next()->str());
4490-
bool isPointer = false;
4491-
bool isReturn = false;
4492-
if (tok->next()->str() == "*") {
4493-
tok = tok->next();
4494-
var = tok->next()->str();
4495-
isPointer = true;
4496-
} else if (tok->next()->str() == "return") {
4497-
isReturn = true;
4498-
}
4499-
4500-
Token *tok2 = tok->tokAt(3 - (isReturn ? 1 : 0));
4501-
if (!tok2->isName() && !tok2->isNumber() && tok2->str()[0] != '\"')
4502-
continue;
4503-
const std::string condition(tok2->str());
4504-
tok2 = tok2->tokAt(2);
4505-
if (!tok2->isName() && !tok2->isNumber() && tok2->str()[0] != '\"')
4506-
continue;
4507-
const std::string value1(tok2->str());
4508-
tok2 = tok2->tokAt(2);
4509-
if (!tok2->isName() && !tok2->isNumber() && tok2->str()[0] != '\"')
4510-
continue;
4511-
const std::string value2(tok2->str());
4512-
4513-
if (isPointer) {
4514-
tok = tok->previous();
4515-
tok->deleteNext(9);
4516-
} else if (isReturn)
4517-
tok->deleteNext(6);
4518-
else
4519-
tok->deleteNext(8);
4520-
4521-
Token *starttok = nullptr;
4522-
4523-
std::string str;
4524-
if (isReturn)
4525-
str = "if ( condition ) { return value1 ; } return value2 ;";
4526-
else
4527-
str = "if ( condition ) { * var = value1 ; } else { * var = value2 ; }";
4528-
4529-
std::string::size_type pos1 = 0;
4530-
while (pos1 != std::string::npos) {
4531-
if (str[pos1] == '*') {
4532-
pos1 += 2;
4533-
if (isPointer) {
4534-
tok->insertToken("*");
4535-
tok = tok->next();
4536-
}
4537-
}
4538-
std::string::size_type pos2 = str.find(" ", pos1);
4539-
if (pos2 == std::string::npos) {
4540-
tok->insertToken(str.substr(pos1));
4541-
pos1 = pos2;
4542-
} else {
4543-
tok->insertToken(str.substr(pos1, pos2 - pos1));
4544-
pos1 = pos2 + 1;
4545-
}
4546-
tok = tok->next();
4547-
4548-
// set links.
4549-
if (tok->str() == "(" || tok->str() == "{")
4550-
starttok = tok;
4551-
else if (starttok && (tok->str() == ")" || tok->str() == "}")) {
4552-
Token::createMutualLinks(starttok, tok);
4553-
starttok = 0;
4554-
} else if (tok->str() == "condition")
4555-
tok->str(condition);
4556-
else if (tok->str() == "var")
4557-
tok->str(var);
4558-
else if (tok->str() == "value1")
4559-
tok->str(value1);
4560-
else if (tok->str() == "value2")
4561-
tok->str(value2);
4562-
4563-
// set varid.
4564-
if (varid.find(tok->str()) != varid.end())
4565-
tok->varId(varid[tok->str()]);
4566-
}
4567-
}
4568-
}
4569-
}
4570-
45714467
bool Tokenizer::simplifyConditions()
45724468
{
45734469
bool ret = false;

lib/tokenize.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -386,11 +386,6 @@ class CPPCHECKLIB Tokenizer {
386386
/** Simplify "if else" */
387387
void elseif();
388388

389-
/**
390-
* Simplify the operator "?:"
391-
*/
392-
void simplifyConditionOperator();
393-
394389
/** Simplify conditions
395390
* @return true if something is modified
396391
* false if nothing is done.

test/testsimplifytokens.cpp

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3001,11 +3001,6 @@ class TestSimplifyTokens : public TestFixture {
30013001
}
30023002

30033003
void simplifyConditionOperator() {
3004-
{
3005-
const char code[] = "; x = a ? b : c;";
3006-
ASSERT_EQUALS("; if ( a ) { x = b ; } else { x = c ; }", tok(code));
3007-
}
3008-
30093004
{
30103005
const char code[] = "(0?(false?1:2):3)";
30113006
ASSERT_EQUALS("( 3 )", tok(code));
@@ -3081,51 +3076,9 @@ class TestSimplifyTokens : public TestFixture {
30813076
ASSERT_EQUALS("; a = 1 ; b = 2 ;", tok(code));
30823077
}
30833078

3084-
{
3085-
const char code[] = "int f(int b, int d)\n"
3086-
"{\n"
3087-
" d = b ? b : 10;\n"
3088-
" return d;\n"
3089-
"}\n";
3090-
ASSERT_EQUALS("int f ( int b , int d ) { if ( b ) { d = b ; } else { d = 10 ; } return d ; }", tok(code));
3091-
}
3092-
3093-
{
3094-
const char code[] = "int f(int b, int *d)\n"
3095-
"{\n"
3096-
" *d = b ? b : 10;\n"
3097-
" return *d;\n"
3098-
"}\n";
3099-
ASSERT_EQUALS("int f ( int b , int * d ) { if ( b ) { * d = b ; } else { * d = 10 ; } return * d ; }", tok(code));
3100-
}
3101-
3102-
{
3103-
const char code[] = "int f(int b, int *d)\n"
3104-
"{\n"
3105-
" if(b) {b++;}"
3106-
" *d = b ? b : 10;\n"
3107-
" return *d;\n"
3108-
"}\n";
3109-
ASSERT_EQUALS("int f ( int b , int * d ) { if ( b ) { b ++ ; } if ( b ) { * d = b ; } else { * d = 10 ; } return * d ; }", tok(code));
3110-
}
3111-
3112-
{
3113-
// Ticket #2885
3114-
const char code[] = "; const char *cx16 = has_cmpxchg16b ? \" -mcx16\" : \" -mno-cx16\";";
3115-
const char expected[] = "; const char * cx16 ; if ( has_cmpxchg16b ) { cx16 = \" -mcx16\" ; } else { cx16 = \" -mno-cx16\" ; }";
3116-
ASSERT_EQUALS(expected, tok(code));
3117-
}
31183079
// Ticket #3572 (segmentation fault)
31193080
ASSERT_EQUALS("0 ; x = { ? y : z ; }", tok("0; x = { ? y : z; }"));
31203081

3121-
{
3122-
// #4019 - varid
3123-
const char code[] = "; char *p; *p = a ? 1 : 0;";
3124-
const char expected[] = "\n\n##file 0\n"
3125-
"1: ; char * p@1 ; if ( a ) { * p@1 = 1 ; } else { * p@1 = 0 ; }\n";
3126-
ASSERT_EQUALS(expected, tokenizeDebugListing(code, true));
3127-
}
3128-
31293082
{
31303083
// #3922 - (true)
31313084
ASSERT_EQUALS("; x = 2 ;", tok("; x = (true)?2:4;"));
@@ -3134,11 +3087,6 @@ class TestSimplifyTokens : public TestFixture {
31343087
ASSERT_EQUALS("; x = * b ;", tok("; x = (false)?*a:*b;"));
31353088
ASSERT_EQUALS("void f ( ) { return 1 ; }", tok("void f() { char *p=0; return (p==0)?1:2; }"));
31363089
}
3137-
3138-
// 4225 - varid gets lost
3139-
ASSERT_EQUALS("\n\n##file 0\n"
3140-
"1: int a@1 ; int b@2 ; int c@3 ; int d@4 ; if ( b@2 ) { a@1 = c@3 ; } else { a@1 = d@4 ; }\n",
3141-
tokenizeDebugListing("int a, b, c, d; a = b ? (int *)c : d;", true));
31423090
}
31433091

31443092
void calculations() {

test/testuninitvar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1603,7 +1603,7 @@ class TestUninitVar : public TestFixture {
16031603
" int y;\n"
16041604
" return x ? 1 : y;\n"
16051605
"}");
1606-
ASSERT_EQUALS("[test.cpp:2]: (error) Uninitialized variable: y\n", errout.str());
1606+
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: y\n", errout.str());
16071607

16081608
// Ticket #3106 - False positive
16091609
{

0 commit comments

Comments
 (0)