Add handling of a simple C++ contract

This commit is contained in:
Daniel Marjamäki 2019-07-12 16:05:08 +02:00
parent f47fc84825
commit 8ad3e43f92
2 changed files with 30 additions and 0 deletions

View File

@ -9839,6 +9839,28 @@ void Tokenizer::simplifyCPPAttribute()
else
head->previous()->isAttributeNodiscard(true);
}
} else if (Token::Match(tok->previous(), ") [ [ expects|ensures|assert default|audit|axiom| : %name% <|<=|>|>= %num% ] ]")) {
const Token *vartok = tok->tokAt(4);
if (vartok->str() == ":")
vartok = vartok->next();
Token *argtok = tok->tokAt(-2);
while (argtok && argtok->str() != "(") {
if (argtok->str() == vartok->str())
break;
if (argtok->str() == ")")
argtok = argtok->link();
argtok = argtok->previous();
}
if (argtok && argtok->str() == vartok->str()) {
if (vartok->next()->str() == ">=")
argtok->setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::LOW, MathLib::toLongNumber(vartok->strAt(2)));
else if (vartok->next()->str() == ">")
argtok->setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::LOW, MathLib::toLongNumber(vartok->strAt(2))+1);
else if (vartok->next()->str() == "<=")
argtok->setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::HIGH, MathLib::toLongNumber(vartok->strAt(2)));
else if (vartok->next()->str() == "<")
argtok->setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::HIGH, MathLib::toLongNumber(vartok->strAt(2))-1);
}
}
Token::eraseTokens(tok, tok->link()->next());
tok->deleteThis();

View File

@ -3927,6 +3927,14 @@ private:
ASSERT_EQUALS(2, values.size());
ASSERT_EQUALS(0, values.front().intvalue);
ASSERT_EQUALS(100, values.back().intvalue);
code = "void f(unsigned short x) [[expects: x <= 100]] {\n"
" return x + 0;\n"
"}";
values = tokenValues(code, "+", &s);
ASSERT_EQUALS(2, values.size());
ASSERT_EQUALS(0, values.front().intvalue);
ASSERT_EQUALS(100, values.back().intvalue);
}