Skip to content

Commit 85500dc

Browse files
committed
Merge pull request cppcheck-opensource#453 from simartin/ticket_5625
Ticket cppcheck-opensource#5625: Simplify constant ternary operator in template parameters.
2 parents cddb4af + 9ddf857 commit 85500dc

3 files changed

Lines changed: 18 additions & 3 deletions

File tree

lib/templatesimplifier.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,8 @@ unsigned int TemplateSimplifier::templateParameters(const Token *tok)
246246
continue;
247247
}
248248

249-
// Skip '='
250-
if (tok && tok->str() == "=")
249+
// Skip '=', '?', ':'
250+
if (tok && Token::Match(tok, "=|?|:"))
251251
tok = tok->next();
252252
if (!tok)
253253
return 0;

lib/tokenize.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4862,7 +4862,7 @@ bool Tokenizer::simplifyConstTernaryOp()
48624862

48634863
else if (endTok->str() == "?")
48644864
++ternaryOplevel;
4865-
else if (Token::Match(endTok, ")|}|]|;|,|:")) {
4865+
else if (Token::Match(endTok, ")|}|]|;|,|:|>")) {
48664866
if (endTok->str() == ":" && ternaryOplevel)
48674867
--ternaryOplevel;
48684868
else {

test/testtokenize.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ class TestTokenizer : public TestFixture {
254254
TEST_CASE(simplify_constants3);
255255
TEST_CASE(simplify_constants4);
256256
TEST_CASE(simplify_constants5);
257+
TEST_CASE(simplify_constants6); // Ticket #5625: Ternary operator as template parameter
257258
TEST_CASE(simplify_null);
258259
TEST_CASE(simplifyMulAndParens); // Ticket #2784 + #3184
259260

@@ -3591,6 +3592,20 @@ class TestTokenizer : public TestFixture {
35913592
ASSERT_EQUALS("int buffer [ 10 ] ;\n\n\nx = 10 ;\ny = 10 ;", tokenizeAndStringify(code,true));
35923593
}
35933594

3595+
void simplify_constants6() { // Ticket #5625
3596+
const char code[] = "template < class T > struct foo ;\n"
3597+
"void bar ( ) {\n"
3598+
"foo < 1 ? 0 ? 1 : 6 : 2 > x ;\n"
3599+
"foo < 1 ? 0 : 2 > y ;\n"
3600+
"}";
3601+
const char exp [] = "template < class T > struct foo ;\n"
3602+
"void bar ( ) {\n"
3603+
"foo < 6 > x ;\n"
3604+
"foo < 0 > y ;\n"
3605+
"}";
3606+
ASSERT_EQUALS(exp, tokenizeAndStringify(code, true));
3607+
}
3608+
35943609
void simplify_null() {
35953610
{
35963611
const char code[] =

0 commit comments

Comments
 (0)