Skip to content
Open
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
3 changes: 3 additions & 0 deletions lib/checkbufferoverrun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,9 @@ void CheckBufferOverrunImpl::objectIndex()
const Token *idx = tok->astOperand2();
if (!idx || !obj)
continue;
// An object such as a view can refer to a variable but indexing it calls operator[]
if (obj->valueType() && obj->valueType()->pointer == 0)
continue;
if (const ValueFlow::Value* v = idx->getKnownValue(ValueFlow::Value::ValueType::INT)) {
if (v->intvalue == 0)
continue;
Expand Down
33 changes: 33 additions & 0 deletions lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,30 @@ void SymbolDatabase::createSymbolDatabaseClassInfo()
}


namespace {
struct LifetimeBoundParam {
nonneg int index;
Token::LifetimeAttrMode mode;
};
}

static std::vector<LifetimeBoundParam> findLifetimeBoundParams(const Token* argStart)
{
std::vector<LifetimeBoundParam> result;
if (!Token::simpleMatch(argStart, "("))
return result;
nonneg int index = 0;
for (const Token* tok = argStart->next(); tok && tok != argStart->link(); tok = tok->next()) {
if (tok->link() && Token::Match(tok, "(|[|{|<"))
tok = tok->link();
if (tok->str() == ",")
index++;
else if (tok->isAttributeLifetimeBound() && (result.empty() || result.back().index != index))
result.push_back({index, tok->getAttributeLifetimeBound()});
}
return result;
}

void SymbolDatabase::createSymbolDatabaseVariableInfo()
{
// fill in variable info
Expand All @@ -901,6 +925,14 @@ void SymbolDatabase::createSymbolDatabaseVariableInfo()
for (auto func = scope.functionList.begin(); func != scope.functionList.end(); ++func) {
// add arguments
func->addArguments(&scope);

// the attribute can be on the declaration, the definition or both
for (const Token* argStart : {func->argDef, func->arg}) {
for (const LifetimeBoundParam& arg : findLifetimeBoundParams(argStart)) {
if (arg.index < func->argumentList.size())
std::next(func->argumentList.begin(), arg.index)->mLifetimeBound = arg.mode;
}
}
}
}
}
Expand Down Expand Up @@ -2502,6 +2534,7 @@ Variable& Variable::operator=(const Variable &var) &
mIndex = var.mIndex;
mAccess = var.mAccess;
mFlags = var.mFlags;
mLifetimeBound = var.mLifetimeBound;
mType = var.mType;
mScope = var.mScope;
mDimensions = var.mDimensions;
Expand Down
15 changes: 15 additions & 0 deletions lib/symboldatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,14 @@ class CPPCHECKLIB Variable {
return getFlag(fIsMaybeUnused);
}

/**
* Is this a function argument marked [[clang::lifetimebound]]?
* The return value of the function (or the constructed object) may refer to it.
*/
bool isLifetimeBound() const {
return mLifetimeBound != Token::LifetimeAttrMode::NONE;
}

const ValueType *valueType() const {
return mValueType;
}
Expand Down Expand Up @@ -690,6 +698,9 @@ class CPPCHECKLIB Variable {
/** @brief flags */
unsigned int mFlags;

/** @brief function argument marked [[clang::lifetimebound]] */
Token::LifetimeAttrMode mLifetimeBound = Token::LifetimeAttrMode::NONE;

/** @brief pointer to user defined type info (for known types) */
const Type *mType;

Expand Down Expand Up @@ -831,6 +842,10 @@ class CPPCHECKLIB Function {
bool isAttributeMaybeUnused() const {
return tokenDef->isAttributeMaybeUnused();
}
/** The implicit object parameter is marked [[clang::lifetimebound]] */
bool isAttributeLifetimeBound() const {
return tokenDef->isAttributeLifetimeBound() || (token && token->isAttributeLifetimeBound());
}

bool hasBody() const {
return getFlag(fHasBody);
Expand Down
18 changes: 18 additions & 0 deletions lib/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ class CPPCHECKLIB Token {
public:
enum CppcheckAttributesType : std::uint8_t { LOW, HIGH };
enum class Cpp11init : std::uint8_t { UNKNOWN, CPP11INIT, NOINIT };
/** What a lifetime attribute such as [[clang::lifetimebound]] applies to. OBJECT is the object that is passed. */
enum class LifetimeAttrMode : std::uint8_t { NONE, OBJECT };

private:
struct Impl {
Expand Down Expand Up @@ -164,6 +166,9 @@ class CPPCHECKLIB Token {

std::string mAttributeCleanup;

// [[clang::lifetimebound]], __attribute__((lifetimebound))
LifetimeAttrMode mAttributeLifetimeBound{LifetimeAttrMode::NONE};

// For memoization, to speed up parsing of huge arrays #8897
Cpp11init mCpp11init{Cpp11init::UNKNOWN};

Expand Down Expand Up @@ -586,6 +591,19 @@ class CPPCHECKLIB Token {
void isAttributeFallthrough(const bool value) {
setFlag(fIsAttributeFallthrough, value);
}
bool isAttributeLifetimeBound() const {
return mImpl->mAttributeLifetimeBound != LifetimeAttrMode::NONE;
}
LifetimeAttrMode getAttributeLifetimeBound() const {
return mImpl->mAttributeLifetimeBound;
}
void setAttributeLifetimeBound(LifetimeAttrMode mode) {
mImpl->mAttributeLifetimeBound = mode;
}
/** Copy the lifetime attributes. They are not a part of flags(). */
void copyLifetimeAttributes(const Token& tok) {
mImpl->mAttributeLifetimeBound = tok.mImpl->mAttributeLifetimeBound;
}
std::vector<std::string> getAttributeAlignas() const {
return mImpl->mAttributeAlignas ? *mImpl->mAttributeAlignas : std::vector<std::string>();
}
Expand Down
52 changes: 52 additions & 0 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9737,6 +9737,46 @@ static Token* getVariableTokenAfterAttributes(Token* tok) {
return vartok;
}

// An attribute after the function declarator applies to the implicit object parameter, so the function
// name is returned. Otherwise a token of the parameter declaration is returned.
static Token* getLifetimeBoundTok(Token* attr, Token* after)
{
Token* const prev = attr->previous();
Token* declEnd = prev;
while (declEnd) {
if (Token::Match(declEnd, "const|volatile|&|&&|noexcept"))
declEnd = declEnd->previous();
else if (Token::simpleMatch(declEnd, ")") && Token::simpleMatch(declEnd->link()->previous(), "noexcept"))
declEnd = declEnd->link()->tokAt(-2);
else
break;
}
if (Token::simpleMatch(declEnd, ")")) {
Token* const ftok = declEnd->link()->previous();
if (Token::simpleMatch(ftok, ")") && Token::simpleMatch(ftok->tokAt(-2), "operator ("))
return ftok->tokAt(-2);
for (Token* tok = ftok; tok && !Token::Match(tok, "[;{}()]"); tok = tok->previous()) {
if (tok->str() == "operator")
return tok;
}
return Token::Match(ftok, "%name%") ? ftok : nullptr;
}
if (Token::Match(prev, "(|,")) {
Token* end = after;
while (end && !Token::Match(end, "[,)=;]")) {
if (end->link() && Token::Match(end, "(|[|{"))
end = end->link();
else if (end->str() == "<" && end->findClosingBracket())
end = end->findClosingBracket();
end = end->next();
}
if (!Token::Match(end, "[,)=]") || end == after)
return nullptr;
return end->previous();
}
return Token::Match(prev, "%name%|]|*|&|&&|>") ? prev : nullptr;
}

Token* Tokenizer::getAttributeFuncTok(Token* tok, bool gccattr) const {
if (!Token::Match(tok, "%name% ("))
return nullptr;
Expand Down Expand Up @@ -9860,6 +9900,11 @@ void Tokenizer::simplifyAttribute()
else if (Token::Match(attr, "[(,] packed [,)]") && Token::simpleMatch(tok->previous(), "}"))
tok->previous()->isAttributePacked(true);

else if (Token::Match(attr, "[(,] lifetimebound|__lifetimebound__ [,)]")) {
if (Token* lifetimeBoundTok = getLifetimeBoundTok(tok, getTokenAfterAttributes(tok, true)))
lifetimeBoundTok->setAttributeLifetimeBound(Token::LifetimeAttrMode::OBJECT);
}

else if (functok && Token::simpleMatch(attr, "( __visibility__ ( \"default\" ) )"))
functok->isAttributeExport(true);

Expand Down Expand Up @@ -9940,6 +9985,13 @@ void Tokenizer::simplifyCPPAttribute()
tok = skipCPPOrAlignAttribute(tok)->next();
continue;
}
if (Token::findmatch(tok->tokAt(2), "lifetimebound|__lifetimebound__", tok->link())) {
Token* head = skipCPPOrAlignAttribute(tok)->next();
while (isCPPAttribute(head) || isAlignAttribute(head))
head = skipCPPOrAlignAttribute(head)->next();
if (Token* lifetimeBoundTok = getLifetimeBoundTok(tok, head))
lifetimeBoundTok->setAttributeLifetimeBound(Token::LifetimeAttrMode::OBJECT);
}
if (Token::findsimplematch(tok->tokAt(2), "noreturn", tok->link())) {
Token * head = skipCPPOrAlignAttribute(tok)->next();
while (isCPPAttribute(head) || isAlignAttribute(head))
Expand Down
6 changes: 6 additions & 0 deletions lib/tokenlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ void TokenList::addtoken(const Token * tok, const nonneg int lineno, const nonne
mTokensFrontBack->back->column(column);
mTokensFrontBack->back->fileIndex(fileno);
mTokensFrontBack->back->flags(tok->flags());
mTokensFrontBack->back->copyLifetimeAttributes(*tok);
mTokensFrontBack->back->tokType(tok->tokType());
}

Expand All @@ -217,6 +218,7 @@ void TokenList::addtoken(const Token *tok, const Token *locationTok)
}

mTokensFrontBack->back->flags(tok->flags());
mTokensFrontBack->back->copyLifetimeAttributes(*tok);
mTokensFrontBack->back->linenr(locationTok->linenr());
mTokensFrontBack->back->column(locationTok->column());
mTokensFrontBack->back->fileIndex(locationTok->fileIndex());
Expand All @@ -241,6 +243,7 @@ void TokenList::addtoken(const Token *tok)
}

mTokensFrontBack->back->flags(tok->flags());
mTokensFrontBack->back->copyLifetimeAttributes(*tok);
mTokensFrontBack->back->linenr(tok->linenr());
mTokensFrontBack->back->column(tok->column());
mTokensFrontBack->back->fileIndex(tok->fileIndex());
Expand All @@ -265,6 +268,7 @@ Token *TokenList::copyTokens(Token *dest, const Token *first, const Token *last,
tok2->linenr(linenr);
tok2->tokType(tok->tokType());
tok2->flags(tok->flags());
tok2->copyLifetimeAttributes(*tok);
tok2->varId(tok->varId());
tok2->setTokenDebug(tok->getTokenDebug());

Expand Down Expand Up @@ -317,6 +321,7 @@ void TokenList::insertTokens(Token *dest, const Token *src, nonneg int n)
dest->varId(src->varId());
dest->tokType(src->tokType());
dest->flags(src->flags());
dest->copyLifetimeAttributes(*src);
if (!src->getMacroName().empty())
dest->setMacroName(src->getMacroName());
src = src->next();
Expand Down Expand Up @@ -424,6 +429,7 @@ std::size_t TokenList::calculateHash() const
std::string hashData;
for (const Token* tok = front(); tok; tok = tok->next()) {
hashData += std::to_string(tok->flags());
hashData += std::to_string(static_cast<int>(tok->getAttributeLifetimeBound()));
hashData += std::to_string(tok->varId());
hashData += std::to_string(tok->tokType());
hashData += tok->str();
Expand Down
Loading
Loading