Fixed #7103 (isReturnScope: handling 'return (bstr){0};')
This commit is contained in:
parent
c676f626b9
commit
d656e3a056
|
@ -275,6 +275,8 @@ bool isReturnScope(const Token * const endToken)
|
|||
return false;
|
||||
|
||||
const Token *prev = endToken->previous();
|
||||
while (prev && Token::simpleMatch(prev->previous(), "; ;"))
|
||||
prev = prev->previous();
|
||||
if (prev && Token::simpleMatch(prev->previous(), "} ;"))
|
||||
prev = prev->previous();
|
||||
|
||||
|
@ -286,6 +288,10 @@ bool isReturnScope(const Token * const endToken)
|
|||
!Token::findsimplematch(prev->link(), "break", prev)) {
|
||||
return true;
|
||||
}
|
||||
if (Token::simpleMatch(prev->link()->previous(), ") {") &&
|
||||
Token::simpleMatch(prev->link()->linkAt(-1)->previous(), "return (")) {
|
||||
return true;
|
||||
}
|
||||
} else if (Token::simpleMatch(prev, ";")) {
|
||||
// noreturn function
|
||||
if (Token::simpleMatch(prev->previous(), ") ;") && Token::Match(prev->linkAt(-1)->tokAt(-2), "[;{}] %name% ("))
|
||||
|
|
|
@ -34,17 +34,21 @@ private:
|
|||
TEST_CASE(isReturnScope);
|
||||
}
|
||||
|
||||
bool isReturnScope(const char code[], const char filename[]="test.cpp") {
|
||||
bool isReturnScope(const char code[], int offset) {
|
||||
Settings settings;
|
||||
Tokenizer tokenizer(&settings, this);
|
||||
std::istringstream istr(code);
|
||||
tokenizer.tokenize(istr, filename);
|
||||
return ::isReturnScope(Token::findsimplematch(tokenizer.tokens(),"}"));
|
||||
tokenizer.tokenize(istr, "test.cpp");
|
||||
const Token * const tok = (offset < 0)
|
||||
? tokenizer.list.back()->tokAt(1+offset)
|
||||
: tokenizer.tokens()->tokAt(offset);
|
||||
return ::isReturnScope(tok);
|
||||
}
|
||||
|
||||
void isReturnScope() {
|
||||
ASSERT_EQUALS(true, isReturnScope("void f() { if (a) { return; } }"));
|
||||
|
||||
ASSERT_EQUALS(true, isReturnScope("void f() { if (a) { return; } }", -2));
|
||||
ASSERT_EQUALS(true, isReturnScope("void f() { if (a) { return (ab){0}; } }", -2)); // #7103
|
||||
ASSERT_EQUALS(false, isReturnScope("void f() { if (a) { return (ab){0}; } }", -4)); // #7103
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue