Split up Cppcheck attribute. low and high values can be specified separately and they can be used for variables also.

This commit is contained in:
Daniel Marjamäki 2019-07-24 15:08:04 +02:00
parent 0041733bd6
commit 2da75d5af4
4 changed files with 24 additions and 13 deletions

View File

@ -108,7 +108,7 @@
<define name="_Ret_writes_maybenull_z_(s)" value=""/> <define name="_Ret_writes_maybenull_z_(s)" value=""/>
<define name="_Ret_writes_bytes_to_maybenull_(s, c)" value=""/> <define name="_Ret_writes_bytes_to_maybenull_(s, c)" value=""/>
<!-- Other Common Annotations --> <!-- Other Common Annotations -->
<define name="_In_range_(low, hi)" value="__cppcheck_in_range__(low, hi)"/> <define name="_In_range_(low, hi)" value="__cppcheck_low__(low) __cppcheck_high__(hi)"/>
<define name="_Out_range_(low, hi)" value=""/> <define name="_Out_range_(low, hi)" value=""/>
<define name="_Ret_range_(low, hi)" value=""/> <define name="_Ret_range_(low, hi)" value=""/>
<define name="_Deref_in_range_(low, hi)" value=""/> <define name="_Deref_in_range_(low, hi)" value=""/>

View File

@ -9815,20 +9815,29 @@ void Tokenizer::simplifyCppcheckAttribute()
if (attr.compare(attr.size()-2, 2, "__") != 0) // TODO: ends_with("__") if (attr.compare(attr.size()-2, 2, "__") != 0) // TODO: ends_with("__")
continue; continue;
if (attr == "__cppcheck_in_range__") { Token *vartok = tok->link();
Token *vartok = tok->link(); while (Token::Match(vartok->next(), "%name%|*|&|::")) {
while (Token::Match(vartok->next(), "%name%|*|&|::")) vartok = vartok->next();
vartok = vartok->next(); if (Token::Match(vartok, "%name% (") && vartok->str().compare(0,11,"__cppcheck_") == 0)
if (vartok->isName() && Token::Match(tok, "( %num% , %num% )")) { vartok = vartok->linkAt(1);
}
if (vartok->isName()) {
if (Token::Match(tok->previous(), "__cppcheck_low__ ( %num% )"))
vartok->setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::LOW, MathLib::toLongNumber(tok->next()->str())); vartok->setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::LOW, MathLib::toLongNumber(tok->next()->str()));
vartok->setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::HIGH, MathLib::toLongNumber(tok->strAt(3))); else if (Token::Match(tok->previous(), "__cppcheck_high__ ( %num% )"))
} vartok->setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::HIGH, MathLib::toLongNumber(tok->next()->str()));
} }
// Delete cppcheck attribute.. // Delete cppcheck attribute..
tok = tok->previous(); if (tok->tokAt(-2)) {
Token::eraseTokens(tok, tok->linkAt(1)->next()); tok = tok->tokAt(-2);
tok->deleteThis(); Token::eraseTokens(tok, tok->linkAt(2)->next());
} else {
tok = tok->previous();
Token::eraseTokens(tok, tok->linkAt(1)->next());
tok->str(";");
}
} }
} }

View File

@ -63,7 +63,9 @@ inline static int caseInsensitiveStringCompare(const std::string &lhs, const std
#define UNUSED(x) (void)(x) #define UNUSED(x) (void)(x)
// Use the nonneg macro when you want to assert that a variable/argument is not negative // Use the nonneg macro when you want to assert that a variable/argument is not negative
#ifdef NONNEG #ifdef __CPPCHECK__
#define nonneg __cppcheck_low__(0)
#elif defined(NONNEG)
// Enable non-negative values checking // Enable non-negative values checking
// TODO : investigate using annotations/contracts for stronger value checking // TODO : investigate using annotations/contracts for stronger value checking
#define nonneg unsigned #define nonneg unsigned

View File

@ -3937,7 +3937,7 @@ private:
ASSERT(values.front().floatValue < -1E20); ASSERT(values.front().floatValue < -1E20);
ASSERT(values.back().floatValue > 1E20); ASSERT(values.back().floatValue > 1E20);
code = "short f(__cppcheck_in_range__(0,100) short x) {\n" code = "short f(__cppcheck_low__(0) __cppcheck_high__(100) short x) {\n"
" return x + 0;\n" " return x + 0;\n"
"}"; "}";
values = tokenValues(code, "+", &s); values = tokenValues(code, "+", &s);