================
@@ -408,41 +420,53 @@ static void emitDiagnostic(const Expr *MovingCall, const
DeclRefExpr *MoveArg,
const SourceLocation UseLoc = Use.DeclRef->getExprLoc();
const SourceLocation MoveLoc = MovingCall->getExprLoc();
- const bool IsMove = (Type == MoveType::Move);
-
- Check->diag(UseLoc, "'%0' used after it was %select{forwarded|moved}1")
- << MoveArg->getDecl()->getName() << IsMove;
- Check->diag(MoveLoc, "%select{forward|move}0 occurred here",
+ Check->diag(UseLoc,
+ "'%0' used after it was %select{forwarded|moved|invalidated}1")
+ << MoveArg->getDecl()->getName() << Type;
+ Check->diag(MoveLoc, "%select{forward|move|invalidation}0 occurred here",
DiagnosticIDs::Note)
- << IsMove;
+ << Type;
if (Use.EvaluationOrderUndefined) {
Check->diag(
UseLoc,
- "the use and %select{forward|move}0 are unsequenced, i.e. "
+ "the use and %select{forward|move|invalidation}0 are unsequenced, i.e.
"
"there is no guarantee about the order in which they are evaluated",
DiagnosticIDs::Note)
- << IsMove;
+ << Type;
} else if (Use.UseHappensInLaterLoopIteration) {
Check->diag(UseLoc,
"the use happens in a later loop iteration than the "
- "%select{forward|move}0",
+ "%select{forward|move|invalidation}0",
DiagnosticIDs::Note)
- << IsMove;
+ << Type;
}
}
+UseAfterMoveCheck::UseAfterMoveCheck(StringRef Name, ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context),
+ InvalidationFunctions(utils::options::parseStringList(
+ Options.get("InvalidationFunctions", ""))) {}
+
+void UseAfterMoveCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
+ Options.store(Opts, "InvalidationFunctions",
+ utils::options::serializeStringList(InvalidationFunctions));
+}
+
void UseAfterMoveCheck::registerMatchers(MatchFinder *Finder) {
// try_emplace is a common maybe-moving function that returns a
// bool to tell callers whether it moved. Ignore std::move inside
// try_emplace to avoid false positives as we don't track uses of
// the bool.
auto TryEmplaceMatcher =
cxxMemberCallExpr(callee(cxxMethodDecl(hasName("try_emplace"))));
+ auto Arg = declRefExpr().bind("arg");
+ auto IsMemberCallee = callee(functionDecl(unless(isStaticStorageClass())));
auto CallMoveMatcher =
- callExpr(argumentCountIs(1),
- callee(functionDecl(hasAnyName("::std::move", "::std::forward"))
+ callExpr(callee(functionDecl(getNameMatcher(InvalidationFunctions))
.bind("move-decl")),
- hasArgument(0, declRefExpr().bind("arg")),
+ anyOf(cxxMemberCallExpr(IsMemberCallee, on(Arg)),
+ callExpr(unless(cxxMemberCallExpr(IsMemberCallee)),
+ hasArgument(0, Arg))),
----------------
higher-performance wrote:
Yup, I deliberately didn't try to generalize in this PR, since as you mentioned
I expect that to be much less common, and I don't have a use case for it yet.
There are other cases to consider too (like emplace), but if people want it
later we can always extend it further.
https://github.com/llvm/llvm-project/pull/170346
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits