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
2 changes: 0 additions & 2 deletions lib/checkfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,6 @@ void CheckFunctions::checkMissingReturn()
continue;
if (Token::Match(function->retDef, "%name% (") && function->retDef->isUpperCaseName())
continue;
if (mTokenizer->isCPP() && Token::Match(function->retDef, "std :: enable_if|enable_if_t"))
continue;
if (Function::returnsVoid(function, true))
continue;
const Token *errorToken = checkMissingReturnScope(scope->bodyEnd, mSettings->library);
Expand Down
111 changes: 71 additions & 40 deletions lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2490,10 +2490,13 @@ const Token *Function::setFlags(const Token *tok1, const Scope *scope)
tok1 = tok1->link()->previous();
}

// Function template
else if (tok1->link() && tok1->str() == ">" && Token::simpleMatch(tok1->link()->previous(), "template <")) {
templateDef = tok1->link()->previous();
break;
else if (tok1->link() && tok1->str() == ">") {
// Function template
if (Token::simpleMatch(tok1->link()->previous(), "template <")) {
templateDef = tok1->link()->previous();
break;
}
tok1 = tok1->link();
}
}
return tok1;
Expand Down Expand Up @@ -2853,58 +2856,86 @@ static bool isUnknownType(const Token* start, const Token* end)
return false;
}

bool Function::returnsConst(const Function* function, bool unknown)
static const Token* getEnableIfReturnType(const Token* start)
{
if (!start)
return nullptr;
for (const Token* tok = start->next(); precedes(tok, start->link()); tok = tok->next()) {
if (tok->link() && Token::Match(tok, "(|[|{|<")) {
tok = tok->link();
continue;
}
if (Token::simpleMatch(tok, ","))
return tok->next();
}
return nullptr;
}

template<class Predicate>
static bool checkReturns(const Function* function, bool unknown, bool emptyEnableIf, Predicate pred)
{
if (!function)
return false;
if (function->type != Function::eFunction)
if (function->type != Function::eFunction && function->type != Function::eOperatorEqual)
return false;
const Token* defStart = function->retDef;
const Token* defEnd = function->returnDefEnd();
if (Token::findsimplematch(function->retDef, "const", defEnd))
if (!defStart)
return unknown;
if (!defEnd)
return unknown;
if (defEnd == defStart)
return unknown;
if (pred(defStart, defEnd))
return true;
if (Token::Match(defEnd->tokAt(-1), "*|&|&&"))
return false;
// void STDCALL foo()
while (defEnd->previous() != defStart && Token::Match(defEnd->tokAt(-2), "%name%|> %name%") &&
!Token::Match(defEnd->tokAt(-2), "const|volatile"))
defEnd = defEnd->previous();
// enable_if
const Token* enableIfEnd = nullptr;
if (Token::simpleMatch(defEnd->previous(), ">"))
enableIfEnd = defEnd->previous();
else if (Token::simpleMatch(defEnd->tokAt(-3), "> :: type"))
enableIfEnd = defEnd->tokAt(-3);
if (enableIfEnd && enableIfEnd->link() &&
Token::Match(enableIfEnd->link()->previous(), "enable_if|enable_if_t|EnableIf")) {
if (const Token* start = getEnableIfReturnType(enableIfEnd->link())) {
defStart = start;
defEnd = enableIfEnd;
} else {
return emptyEnableIf;
}
}
assert(defEnd != defStart);
if (pred(defStart, defEnd))
return true;
// Check for unknown types, which could be a const
if (isUnknownType(function->retDef, defEnd))
if (isUnknownType(defStart, defEnd))
return unknown;
return false;
}

bool Function::returnsConst(const Function* function, bool unknown)
{
return checkReturns(function, unknown, false, [](const Token* defStart, const Token* defEnd) {
return Token::findsimplematch(defStart, "const", defEnd);
});
}

bool Function::returnsReference(const Function* function, bool unknown)
{
if (!function)
return false;
if (function->type != Function::eFunction)
return false;
const Token* defEnd = function->returnDefEnd();
if (defEnd->strAt(-1) == "&")
return true;
// Check for unknown types, which could be a reference
if (isUnknownType(function->retDef, defEnd))
return unknown;
return false;
return checkReturns(function, unknown, false, [](UNUSED const Token* defStart, const Token* defEnd) {
return Token::simpleMatch(defEnd->previous(), "&");
});
}

bool Function::returnsVoid(const Function* function, bool unknown)
{
if (!function)
return false;
if (function->type != Function::eFunction && function->type != Function::eOperatorEqual)
return false;
const Token* defEnd = function->returnDefEnd();
if (defEnd->strAt(-1) == "void")
return true;
// Check for unknown types, which could be a void type
if (isUnknownType(function->retDef, defEnd))
return unknown;
if (unknown) {
// void STDCALL foo()
const Token *def;
bool isVoid = false;
for (def = function->retDef; def && def->isName(); def = def->next())
isVoid |= (def->str() == "void");
if (isVoid && def && !Token::Match(def, "*|&|&&"))
return true;
}
return false;
return checkReturns(function, unknown, true, [](UNUSED const Token* defStart, const Token* defEnd) {
return Token::simpleMatch(defEnd->previous(), "void");
});
}

std::vector<const Token*> Function::findReturns(const Function* f)
Expand Down
32 changes: 31 additions & 1 deletion test/testfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1726,7 +1726,37 @@ class TestFunctions : public TestFixture {
"[test.cpp:10]: (error) Found a exit path from function with non-void return type that has missing return statement\n",
errout.str());

check("std::enable_if_t<sizeof(uint64_t) == 8> f() {}"); // #11171
// #11171
check("std::enable_if_t<sizeof(uint64_t) == 8> f() {}");
ASSERT_EQUALS("", errout.str());

check("std::enable_if_t<sizeof(uint64_t) == 8, int> f() {}");
ASSERT_EQUALS(
"[test.cpp:1]: (error) Found a exit path from function with non-void return type that has missing return statement\n",
errout.str());

check("template<class T> std::enable_if_t<std::is_same<T, int>{}, int> f(T) {}");
ASSERT_EQUALS(
"[test.cpp:1]: (error) Found a exit path from function with non-void return type that has missing return statement\n",
errout.str());

check("template<class T> std::enable_if_t<std::is_same<T, int>{}> f(T) {}");
ASSERT_EQUALS("", errout.str());

check("typename std::enable_if<sizeof(uint64_t) == 8>::type f() {}");
ASSERT_EQUALS("", errout.str());

check("typename std::enable_if<sizeof(uint64_t) == 8, int>::type f() {}");
ASSERT_EQUALS(
"[test.cpp:1]: (error) Found a exit path from function with non-void return type that has missing return statement\n",
errout.str());

check("template<class T> typename std::enable_if<std::is_same<T, int>{}, int>::type f(T) {}");
ASSERT_EQUALS(
"[test.cpp:1]: (error) Found a exit path from function with non-void return type that has missing return statement\n",
errout.str());

check("template<class T> typename std::enable_if<std::is_same<T, int>{}>::type f(T) {}");
ASSERT_EQUALS("", errout.str());
}

Expand Down