Skip to content
Merged
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
173 changes: 173 additions & 0 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,178 @@ static void valueFlowString(TokenList& tokenlist, const Settings& settings)
}
}

static const Token* findTypeEnd(const Token* tok)
{
while (Token::Match(tok, "%name%|::|<|(|*|&|&&") && !tok->isVariable()) {
if (tok->link())
tok = tok->link();
tok = tok->next();
}
return tok;
}

static std::vector<const Token*> evaluateType(const Token* start, const Token* end)
{
std::vector<const Token*> result;
if (!start)
return result;
if (!end)
return result;
for (const Token* tok = start; tok != end; tok = tok->next()) {
if (!Token::Match(tok, "%name%|::|<|(|*|&|&&"))
return {};
if (Token::simpleMatch(tok, "decltype (")) {
if (Token::Match(tok->next(), "( %name% )")) {
const Token* vartok = tok->tokAt(2);
if (vartok->function() && !vartok->variable()) {
result.push_back(vartok);
} else {
auto t = Token::typeDecl(vartok);
if (!t.first || !t.second)
return {};
auto inner = evaluateType(t.first, t.second);
if (inner.empty())
return {};
result.insert(result.end(), inner.begin(), inner.end());
}
} else if (Token::Match(tok->next(), "( %name% (|{") && Token::Match(tok->linkAt(3), "}|) )")) {
const Token* ftok = tok->tokAt(3);
auto t = Token::typeDecl(ftok);
if (!t.first || !t.second)
return {};
auto inner = evaluateType(t.first, t.second);
if (inner.empty())
return {};
result.insert(result.end(), inner.begin(), inner.end());
} else {
// We cant evaluate the decltype so bail
return {};
}
tok = tok->linkAt(1);

} else {
if (tok->link()) {
auto inner = evaluateType(tok->next(), tok->link());
if (inner.empty())
return {};
result.push_back(tok);
result.insert(result.end(), inner.begin(), inner.end());
tok = tok->link();
}
result.push_back(tok);
}
}
return result;
}

static bool hasUnknownType(const std::vector<const Token*>& toks)
{
if (toks.empty())
return true;
return std::any_of(toks.begin(), toks.end(), [&](const Token* tok) {
if (!tok)
return true;
if (Token::Match(tok, "const|volatile|&|*|&&|%type%|::"))
return false;
if (Token::Match(tok, "%name% :: %name%"))
return false;
if (tok->type())
return false;
return true;
});
}

static void stripCV(std::vector<const Token*>& toks)
{
auto it =
std::find_if(toks.begin(), toks.end(), [&](const Token* tok) {
return !Token::Match(tok, "const|volatile");
});
if (it == toks.begin())
return;
toks.erase(toks.begin(), it);
}

static std::vector<std::vector<const Token*>> evaluateTemplateArgs(const Token* tok)
{
std::vector<std::vector<const Token*>> result;
while (tok) {
const Token* next = tok->nextTemplateArgument();
const Token* end = next ? next->previous() : findTypeEnd(tok);
result.push_back(evaluateType(tok, end));
tok = next;
}
return result;
}

static void valueFlowTypeTraits(TokenList& tokenlist, const Settings& settings)
{
std::unordered_map<std::string, std::function<ValueFlow::Value(std::vector<std::vector<const Token*>> args)>> eval;
eval["is_void"] = [&](std::vector<std::vector<const Token*>> args) {
if (args.size() != 1)
return ValueFlow::Value::unknown();
stripCV(args[0]);
if (args[0].size() == 1 && args[0][0]->str() == "void")
return ValueFlow::Value(1);
if (!hasUnknownType(args[0]))
return ValueFlow::Value(0);
return ValueFlow::Value::unknown();
};
eval["is_lvalue_reference"] = [&](const std::vector<std::vector<const Token*>>& args) {
if (args.size() != 1)
return ValueFlow::Value::unknown();
if (args[0].back()->str() == "&")
return ValueFlow::Value(1);
if (!hasUnknownType(args[0]))
return ValueFlow::Value(0);
return ValueFlow::Value::unknown();
};
eval["is_rvalue_reference"] = [&](const std::vector<std::vector<const Token*>>& args) {
if (args.size() != 1)
return ValueFlow::Value::unknown();
if (args[0].back()->str() == "&&")
return ValueFlow::Value(1);
if (!hasUnknownType(args[0]))
return ValueFlow::Value(0);
return ValueFlow::Value::unknown();
};
eval["is_reference"] = [&](const std::vector<std::vector<const Token*>>& args) {
if (args.size() != 1)
return ValueFlow::Value::unknown();
ValueFlow::Value isRValue = eval["is_rvalue_reference"](args);
if (isRValue.isUninitValue() || isRValue.intvalue == 1)
return isRValue;
return eval["is_lvalue_reference"](args);
};
for (Token* tok = tokenlist.front(); tok; tok = tok->next()) {

if (!Token::Match(tok, "std :: %name% <"))
continue;
Token* templateTok = tok->tokAt(3);
Token* updateTok = nullptr;
std::string traitName = tok->strAt(2);
if (endsWith(traitName, "_v")) {
traitName.pop_back();
traitName.pop_back();
updateTok = tok->next();
} else if (Token::simpleMatch(templateTok->link(), "> { }") ||
Token::simpleMatch(templateTok->link(), "> :: value")) {
updateTok = templateTok->link()->next();
}
if (!updateTok)
continue;
if (updateTok->hasKnownIntValue())
continue;
if (eval.count(traitName) == 0)
continue;
ValueFlow::Value value = eval[traitName](evaluateTemplateArgs(templateTok->next()));
if (value.isUninitValue())
continue;
value.setKnown();
ValueFlow::setTokenValue(updateTok, std::move(value), settings);
}
}

static void valueFlowArray(TokenList& tokenlist, const Settings& settings)
{
std::map<nonneg int, const Token*> constantArrays;
Expand Down Expand Up @@ -7254,6 +7426,7 @@ void ValueFlow::setValues(TokenList& tokenlist,
VFA(valueFlowEnumValue(symboldatabase, settings)),
VFA(valueFlowNumber(tokenlist, settings)),
VFA(valueFlowString(tokenlist, settings)),
VFA(valueFlowTypeTraits(tokenlist, settings)),
VFA(valueFlowArray(tokenlist, settings)),
VFA(valueFlowUnknownFunctionReturn(tokenlist, settings)),
VFA(valueFlowGlobalConstVar(tokenlist, settings)),
Expand Down
146 changes: 146 additions & 0 deletions test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class TestValueFlow : public TestFixture {

TEST_CASE(valueFlowNumber);
TEST_CASE(valueFlowString);
TEST_CASE(valueFlowTypeTraits);
TEST_CASE(valueFlowPointerAlias);
TEST_CASE(valueFlowLifetime);
TEST_CASE(valueFlowArrayElement);
Expand Down Expand Up @@ -532,6 +533,21 @@ class TestValueFlow : public TestFixture {
return values.size() == 1U && !values.front().isTokValue() ? values.front() : ValueFlow::Value();
}

#define testKnownValueOfTok(...) testKnownValueOfTok_(__FILE__, __LINE__, __VA_ARGS__)
bool testKnownValueOfTok_(const char* file,
int line,
const char code[],
const char tokstr[],
int value,
const Settings* s = nullptr,
bool cpp = true)
{
std::list<ValueFlow::Value> values = removeImpossible(tokenValues_(file, line, code, tokstr, s, cpp));
return std::any_of(values.begin(), values.end(), [&](const ValueFlow::Value& v) {
return v.isKnown() && v.isIntValue() && v.intvalue == value;
});
}

static std::list<ValueFlow::Value> removeSymbolicTok(std::list<ValueFlow::Value> values)
{
values.remove_if([](const ValueFlow::Value& v) {
Expand Down Expand Up @@ -596,6 +612,136 @@ class TestValueFlow : public TestFixture {
ASSERT_EQUALS(true, testValueOfX(code, 2, "\"abc\"", ValueFlow::Value::ValueType::TOK));
}

void valueFlowTypeTraits()
{
ASSERT_EQUALS(true, testKnownValueOfTok("std::is_void<void>{};", "{", 1));
ASSERT_EQUALS(true, testKnownValueOfTok("std::is_void<void>::value;", ":: value", 1));
ASSERT_EQUALS(true, testKnownValueOfTok("std::is_void_v<void>;", "::", 1));

// is_void
ASSERT_EQUALS(true, testKnownValueOfTok("std::is_void<int>{};", "{", 0));
ASSERT_EQUALS(true, testKnownValueOfTok("std::is_void<void*>{};", "{", 0));
ASSERT_EQUALS(true, testKnownValueOfTok("std::is_void<const void>{};", "{", 1));
ASSERT_EQUALS(true, testKnownValueOfTok("std::is_void<volatile void>{};", "{", 1));

// is_lvalue_reference
ASSERT_EQUALS(true, testKnownValueOfTok("std::is_lvalue_reference<int>{};", "{", 0));
ASSERT_EQUALS(true, testKnownValueOfTok("std::is_lvalue_reference<int&>{};", "{", 1));
ASSERT_EQUALS(true, testKnownValueOfTok("std::is_lvalue_reference<int&&>{};", "{", 0));

// is_rvalue_reference
ASSERT_EQUALS(true, testKnownValueOfTok("std::is_rvalue_reference<int>{};", "{", 0));
ASSERT_EQUALS(true, testKnownValueOfTok("std::is_rvalue_reference<int&>{};", "{", 0));
ASSERT_EQUALS(true, testKnownValueOfTok("std::is_rvalue_reference<int&&>{};", "{", 1));

// is_reference
ASSERT_EQUALS(true, testKnownValueOfTok("std::is_reference<int>{};", "{", 0));
ASSERT_EQUALS(true, testKnownValueOfTok("std::is_reference<int&>{};", "{", 1));
ASSERT_EQUALS(true, testKnownValueOfTok("std::is_reference<int&&>{};", "{", 1));

{
const char* code;
code = "void bar();\n"
"void foo() { std::is_void<decltype(bar())>::value; }";
ASSERT_EQUALS(true, testKnownValueOfTok(code, ":: value", 1));

code = "int bar();\n"
"void foo() { std::is_void<decltype(bar())>::value; }";
ASSERT_EQUALS(true, testKnownValueOfTok(code, ":: value", 0));

code = "void bar();\n"
"void foo() { std::is_void<decltype(bar)>::value; }";
ASSERT_EQUALS(true, testKnownValueOfTok(code, ":: value", 0));

code = "class A;\n"
"void foo() { std::is_lvalue_reference<A>::value; }";
ASSERT_EQUALS(true, testKnownValueOfTok(code, ":: value", 0));

code = "class A;\n"
"void foo() { std::is_lvalue_reference<A&>::value; }";
ASSERT_EQUALS(true, testKnownValueOfTok(code, ":: value", 1));

code = "class A;\n"
"void foo() { std::is_lvalue_reference<A&&>::value; }";
ASSERT_EQUALS(true, testKnownValueOfTok(code, ":: value", 0));

code = "class A;\n"
"void foo() { std::is_rvalue_reference<A>::value; }";
ASSERT_EQUALS(true, testKnownValueOfTok(code, ":: value", 0));

code = "class A;\n"
"void foo() { std::is_rvalue_reference<A&>::value; }";
ASSERT_EQUALS(true, testKnownValueOfTok(code, ":: value", 0));

code = "class A;\n"
"void foo() { std::is_rvalue_reference<A&&>::value; }";
ASSERT_EQUALS(true, testKnownValueOfTok(code, ":: value", 1));

code = "class A;\n"
"void foo() { std::is_reference<A>::value; }";
ASSERT_EQUALS(true, testKnownValueOfTok(code, ":: value", 0));

code = "class A;\n"
"void foo() { std::is_reference<A&>::value; }";
ASSERT_EQUALS(true, testKnownValueOfTok(code, ":: value", 1));

code = "class A;\n"
"void foo() { std::is_reference<A&&>::value; }";
ASSERT_EQUALS(true, testKnownValueOfTok(code, ":: value", 1));

code = "void foo() {\n"
" int bar;\n"
" std::is_void<decltype(bar)>::value;\n"
"}\n";
ASSERT_EQUALS(true, testKnownValueOfTok(code, ":: value", 0));

code = "void foo(int bar) {\n"
" std::is_lvalue_reference<decltype(bar)>::value;\n"
"}\n";
ASSERT_EQUALS(true, testKnownValueOfTok(code, ":: value", 0));

code = "void foo(int& bar) {\n"
" std::is_lvalue_reference<decltype(bar)>::value;\n"
"}\n";
ASSERT_EQUALS(true, testKnownValueOfTok(code, ":: value", 1));

code = "void foo(int&& bar) {\n"
" std::is_lvalue_reference<decltype(bar)>::value;\n"
"}\n";
ASSERT_EQUALS(true, testKnownValueOfTok(code, ":: value", 0));

code = "void foo(int bar) {\n"
" std::is_rvalue_reference<decltype(bar)>::value;\n"
"}\n";
ASSERT_EQUALS(true, testKnownValueOfTok(code, ":: value", 0));

code = "void foo(int& bar) {\n"
" std::is_rvalue_reference<decltype(bar)>::value;\n"
"}\n";
ASSERT_EQUALS(true, testKnownValueOfTok(code, ":: value", 0));

code = "void foo(int&& bar) {\n"
" std::is_rvalue_reference<decltype(bar)>::value;\n"
"}\n";
ASSERT_EQUALS(true, testKnownValueOfTok(code, ":: value", 1));

code = "void foo(int bar) {\n"
" std::is_reference<decltype(bar)>::value;\n"
"}\n";
ASSERT_EQUALS(true, testKnownValueOfTok(code, ":: value", 0));

code = "void foo(int& bar) {\n"
" std::is_reference<decltype(bar)>::value;\n"
"}\n";
ASSERT_EQUALS(true, testKnownValueOfTok(code, ":: value", 1));

code = "void foo(int&& bar) {\n"
" std::is_reference<decltype(bar)>::value;\n"
"}\n";
ASSERT_EQUALS(true, testKnownValueOfTok(code, ":: value", 1));
}
}

void valueFlowPointerAlias() {
const char *code;
std::list<ValueFlow::Value> values;
Expand Down