Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3869,3 +3869,20 @@ void CheckOther::overlappingWriteFunction(const Token *tok)
const std::string &funcname = tok ? tok->str() : emptyString;
reportError(tok, Severity::error, "overlappingWriteFunction", "Overlapping read/write in " + funcname + "() is undefined behavior");
}

// see https://gcc.gnu.org/onlinedocs/gcc/Conditionals.html
void CheckOther::checkTernaryOmittedOperand()
{
if (!mSettings->severity.isEnabled(Severity::portability))
return;

for (const Token* tok = mTokenizer->tokens(); tok; tok = tok->next()) {
if (tok->str() == "?" && Token::simpleMatch(tok->astOperand2(), ":") && !tok->astOperand2()->astOperand1()) {
ternaryOmittedOperandError(tok->astOperand2());
}
}
}

void CheckOther::ternaryOmittedOperandError(const Token* tok) {
reportError(tok, Severity::portability, "ternaryOmittedOperand", "The ternary operator without a second operand is a GNU C extension.");
}
5 changes: 5 additions & 0 deletions lib/checkother.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class CPPCHECKLIB CheckOther : public Check {
checkOther.checkAccessOfMovedVariable();
checkOther.checkModuloOfOne();
checkOther.checkOverlappingWrite();
checkOther.checkTernaryOmittedOperand();
}

/** Is expression a comparison that checks if a nonzero (unsigned/pointer) expression is less than zero? */
Expand Down Expand Up @@ -226,6 +227,8 @@ class CPPCHECKLIB CheckOther : public Check {
void overlappingWriteUnion(const Token *tok);
void overlappingWriteFunction(const Token *tok);

void checkTernaryOmittedOperand();

private:
// Error messages..
void checkComparisonFunctionIsAlwaysTrueOrFalseError(const Token* tok, const std::string &functionName, const std::string &varName, const bool result);
Expand Down Expand Up @@ -282,6 +285,7 @@ class CPPCHECKLIB CheckOther : public Check {
void knownArgumentError(const Token *tok, const Token *ftok, const ValueFlow::Value *value, const std::string &varexpr, bool isVariableExpressionHidden);
void comparePointersError(const Token *tok, const ValueFlow::Value *v1, const ValueFlow::Value *v2);
void checkModuloOfOneError(const Token *tok);
void ternaryOmittedOperandError(const Token *tok);

void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override {
CheckOther c(nullptr, settings, errorLogger);
Expand Down Expand Up @@ -353,6 +357,7 @@ class CPPCHECKLIB CheckOther : public Check {
c.comparePointersError(nullptr, nullptr, nullptr);
c.redundantAssignmentError(nullptr, nullptr, "var", false);
c.redundantInitializationError(nullptr, nullptr, "var", false);
c.ternaryOmittedOperandError(nullptr);

const std::vector<const Token *> nullvec;
c.funcArgOrderDifferent("function", nullptr, nullptr, nullvec, nullvec);
Expand Down
10 changes: 10 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ class TestOther : public TestFixture {
TEST_CASE(checkOverlappingWrite);

TEST_CASE(constVariableArrayMember); // #10371

TEST_CASE(ternaryOmittedOperand);
}

#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
Expand Down Expand Up @@ -10731,6 +10733,14 @@ class TestOther : public TestFixture {
"};\n");
ASSERT_EQUALS("", errout.str());
}

void ternaryOmittedOperand() {
check("void f(int x, int y) {\n"
" g(x ? x : y);\n"
" h(x ? : y);\n"
"};\n");
ASSERT_EQUALS("[test.cpp:3]: (portability) The ternary operator without a second operand is a GNU C extension.\n", errout.str());
}
};

REGISTER_TEST(TestOther)