From 1729ea6f0f24fca66e10171ee23ef83b6bbc22d4 Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Wed, 24 Sep 2014 16:45:06 +0200 Subject: [PATCH] Fixed #6182 (Format specifier check confused by ternary operator) --- lib/checkio.cpp | 4 ++-- test/testio.cpp | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/checkio.cpp b/lib/checkio.cpp index 0db5b4d81..508f47314 100644 --- a/lib/checkio.cpp +++ b/lib/checkio.cpp @@ -1427,8 +1427,8 @@ CheckIO::ArgumentInfo::ArgumentInfo(const Token * tok, const Settings *settings) tok1 = tok1->link(); // check for some common well known functions - else if ((Token::Match(tok1->previous(), "%var% . size|empty|c_str ( )") && isStdContainer(tok1->previous())) || - (Token::Match(tok1->previous(), "] . size|empty|c_str ( )") && Token::Match(tok1->previous()->link()->previous(), "%var%") && isStdContainer(tok1->previous()->link()->previous()))) { + else if ((Token::Match(tok1->previous(), "%var% . size|empty|c_str ( ) [,)]") && isStdContainer(tok1->previous())) || + (Token::Match(tok1->previous(), "] . size|empty|c_str ( ) [,)]") && Token::Match(tok1->previous()->link()->previous(), "%var%") && isStdContainer(tok1->previous()->link()->previous()))) { tempToken = new Token(0); tempToken->fileIndex(tok1->fileIndex()); tempToken->linenr(tok1->linenr()); diff --git a/test/testio.cpp b/test/testio.cpp index 97a36492c..7249529bc 100644 --- a/test/testio.cpp +++ b/test/testio.cpp @@ -59,6 +59,8 @@ private: TEST_CASE(testMicrosoftCStringFormatArguments); // ticket #4920 TEST_CASE(testMicrosoftSecurePrintfArgument); TEST_CASE(testMicrosoftSecureScanfArgument); + + TEST_CASE(testTernary); // ticket #6182 } void check(const char code[], bool inconclusive = false, bool portability = false, Settings::PlatformType platform = Settings::Unspecified) { @@ -3639,6 +3641,14 @@ private: "}\n", false, false, Settings::Win32W); ASSERT_EQUALS("", errout.str()); } + + void testTernary() { // ticket #6182 + check("void test(const std::string &val) {\n" + " printf(\"%s\n\", val.empty() ? \"I like to eat bananas\" : val.c_str());\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + }; REGISTER_TEST(TestIO)