Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid using temporary objects in followAllReferences() #6697

Merged
merged 1 commit into from
Aug 15, 2024
Merged
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
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
Loading