Skip to content

Commit

Permalink
avoid using temporary objects in followAllReferences()
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Aug 14, 2024
1 parent f4c9f3a commit 39ec2d5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
24 changes: 12 additions & 12 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1250,27 +1250,27 @@ SmallVector<ReferenceToken> followAllReferences(const Token* tok,
return {};
if (depth < 0) {
SmallVector<ReferenceToken> refs_result;
refs_result.push_back({tok, std::move(errors)});
refs_result.emplace_back(tok, std::move(errors));
return refs_result;
}
const Variable *var = tok->variable();
if (var && var->declarationId() == tok->varId()) {
if (var->nameToken() == tok || isStructuredBindingVariable(var)) {
SmallVector<ReferenceToken> refs_result;
refs_result.push_back({tok, std::move(errors)});
refs_result.emplace_back(tok, std::move(errors));
return refs_result;
}
if (var->isReference() || var->isRValueReference()) {
const Token * const varDeclEndToken = var->declEndToken();
if (!varDeclEndToken) {
SmallVector<ReferenceToken> refs_result;
refs_result.push_back({tok, std::move(errors)});
refs_result.emplace_back(tok, std::move(errors));
return refs_result;
}
if (var->isArgument()) {
errors.emplace_back(varDeclEndToken, "Passed to reference.");
SmallVector<ReferenceToken> refs_result;
refs_result.push_back({tok, std::move(errors)});
refs_result.emplace_back(tok, std::move(errors));
return refs_result;
}
if (Token::simpleMatch(varDeclEndToken, "=")) {
Expand All @@ -1281,7 +1281,7 @@ SmallVector<ReferenceToken> followAllReferences(const Token* tok,
if (vartok == tok || (!temporary && isTemporary(vartok, nullptr, true) &&
(var->isConst() || var->isRValueReference()))) {
SmallVector<ReferenceToken> refs_result;
refs_result.push_back({tok, std::move(errors)});
refs_result.emplace_back(tok, std::move(errors));
return refs_result;
}
if (vartok)
Expand All @@ -1299,7 +1299,7 @@ SmallVector<ReferenceToken> followAllReferences(const Token* tok,

if (!inconclusive && result.size() != 1) {
SmallVector<ReferenceToken> refs_result;
refs_result.push_back({tok, std::move(errors)});
refs_result.emplace_back(tok, std::move(errors));
return refs_result;
}

Expand All @@ -1313,7 +1313,7 @@ SmallVector<ReferenceToken> followAllReferences(const Token* tok,
const Function *f = tok->previous()->function();
if (!Function::returnsReference(f)) {
SmallVector<ReferenceToken> refs_result;
refs_result.push_back({tok, std::move(errors)});
refs_result.emplace_back(tok, std::move(errors));
return refs_result;
}
std::set<ReferenceToken, ReferenceTokenLess> result;
Expand All @@ -1326,20 +1326,20 @@ SmallVector<ReferenceToken> followAllReferences(const Token* tok,
const Variable* argvar = rt.token->variable();
if (!argvar) {
SmallVector<ReferenceToken> refs_result;
refs_result.push_back({tok, std::move(errors)});
refs_result.emplace_back(tok, std::move(errors));
return refs_result;
}
if (argvar->isArgument() && (argvar->isReference() || argvar->isRValueReference())) {
const int n = getArgumentPos(argvar, f);
if (n < 0) {
SmallVector<ReferenceToken> refs_result;
refs_result.push_back({tok, std::move(errors)});
refs_result.emplace_back(tok, std::move(errors));
return refs_result;
}
std::vector<const Token*> args = getArguments(tok->previous());
if (n >= args.size()) {
SmallVector<ReferenceToken> refs_result;
refs_result.push_back({tok, std::move(errors)});
refs_result.emplace_back(tok, std::move(errors));
return refs_result;
}
const Token* argTok = args[n];
Expand All @@ -1351,7 +1351,7 @@ SmallVector<ReferenceToken> followAllReferences(const Token* tok,
result.insert(refs.cbegin(), refs.cend());
if (!inconclusive && result.size() > 1) {
SmallVector<ReferenceToken> refs_result;
refs_result.push_back({tok, std::move(errors)});
refs_result.emplace_back(tok, std::move(errors));
return refs_result;
}
}
Expand All @@ -1364,7 +1364,7 @@ SmallVector<ReferenceToken> followAllReferences(const Token* tok,
}
}
SmallVector<ReferenceToken> refs_result;
refs_result.push_back({tok, std::move(errors)});
refs_result.emplace_back(tok, std::move(errors));
return refs_result;
}

Expand Down
4 changes: 4 additions & 0 deletions lib/astutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ bool succeeds(const Token* tok1, const Token* tok2);
bool exprDependsOnThis(const Token* expr, bool onVar = true, nonneg int depth = 0);

struct ReferenceToken {
ReferenceToken(const Token* t, ErrorPath e)
: token(t)
, errors(std::move(e))
{}
const Token* token;
ErrorPath errors;
};
Expand Down

0 comments on commit 39ec2d5

Please sign in to comment.