Fix FP in lifetime anlaysis: Dont decay std array

This will fix FP with:

```cpp
std::array<char, 1> f() {
    std::array<char, 1> x;
    return x;
}
```
This commit is contained in:
Paul Fultz II 2018-12-01 19:09:19 +01:00 committed by Daniel Marjamäki
parent 229c45e7f8
commit 67dd822910
2 changed files with 24 additions and 17 deletions

View File

@ -2840,7 +2840,7 @@ static void valueFlowLifetime(TokenList *tokenlist, SymbolDatabase*, ErrorLogger
const Variable * var = getLifetimeVariable(tok, errorPath); const Variable * var = getLifetimeVariable(tok, errorPath);
if (!var) if (!var)
continue; continue;
if (var->isArray() && !var->isArgument() && tok->astParent() && if (var->isArray() && !var->isStlType() && !var->isArgument() && tok->astParent() &&
(astIsPointer(tok->astParent()) || Token::Match(tok->astParent(), "%assign%|return"))) { (astIsPointer(tok->astParent()) || Token::Match(tok->astParent(), "%assign%|return"))) {
errorPath.emplace_back(tok, "Array decayed to pointer here."); errorPath.emplace_back(tok, "Array decayed to pointer here.");

View File

@ -1510,29 +1510,36 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
<<<<<<< HEAD
check("void f() {\n" check("void f() {\n"
" struct b {\n" " struct b {\n"
" uint32_t f[6];\n" " uint32_t f[6];\n"
" } d;\n" " } d;\n"
" uint32_t *a = d.f;\n" " uint32_t *a = d.f;\n"
"}\n"); =======
ASSERT_EQUALS("", errout.str()); // Dont decay std::array
check("std::array<char, 1> f() {\n"
" std::array<char, 1> x;\n"
" return x;\n"
>>>>>>> 52f00b8c88aecaa064f1acbebbe59331f08f09fc
"}\n");
ASSERT_EQUALS("", errout.str());
// Make sure we dont hang // Make sure we dont hang
check("struct A;\n" check("struct A;\n"
"void f() {\n" "void f() {\n"
" using T = A[3];\n" " using T = A[3];\n"
" A &&a = T{1, 2, 3}[1];\n" " A &&a = T{1, 2, 3}[1];\n"
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// Make sure we dont hang // Make sure we dont hang
check("struct A;\n" check("struct A;\n"
"void f() {\n" "void f() {\n"
" using T = A[3];\n" " using T = A[3];\n"
" A &&a = T{1, 2, 3}[1]();\n" " A &&a = T{1, 2, 3}[1]();\n"
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void danglingLifetimeFunction() { void danglingLifetimeFunction() {