Skip to content

[clang-tidy] fix fp when modifying variant by operator[] with template in parameters #128407

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

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
3 changes: 2 additions & 1 deletion clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ Changes in existing checks
- Improved :doc:`misc-const-correctness
<clang-tidy/checks/misc/const-correctness>` check by adding the option
`AllowedTypes`, that excludes specified types from const-correctness
checking.
checking and fixing false positives when modifying variant by ``operator[]``
with template in parameters.

- Improved :doc:`misc-redundant-expression
<clang-tidy/checks/misc/redundant-expression>` check by providing additional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -998,3 +998,11 @@ void member_pointer_const(Value &x, PointerToConstMemberFunction m) {
// CHECK-MESSAGES:[[@LINE-1]]:3: warning: variable 'member_pointer_tmp' of type 'Value &' can be declared 'const'
(member_pointer_tmp.*m)();
}

namespace gh127776_false_positive {
template <class T> struct vector { T &operator[](int t); };
template <typename T> void f() {
vector<int> x;
x[T{}] = 3;
}
} // namespace gh127776_false_positive
18 changes: 15 additions & 3 deletions clang/lib/Analysis/ExprMutationAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ static bool canExprResolveTo(const Expr *Source, const Expr *Target) {

namespace {

// `ArraySubscriptExpr` can switch base and idx, e.g. `a[4]` is the same as
// `4[a]`. When type is dependent, we conservatively assume both sides are base.
AST_MATCHER_P(ArraySubscriptExpr, hasBaseConservative,
ast_matchers::internal::Matcher<Expr>, InnerMatcher) {
if (Node.isTypeDependent()) {
return InnerMatcher.matches(*Node.getLHS(), Finder, Builder) ||
InnerMatcher.matches(*Node.getRHS(), Finder, Builder);
}
return InnerMatcher.matches(*Node.getBase(), Finder, Builder);
}

AST_MATCHER(Type, isDependentType) { return Node.isDependentType(); }

AST_MATCHER_P(LambdaExpr, hasCaptureInit, const Expr *, E) {
Expand Down Expand Up @@ -513,8 +524,8 @@ ExprMutationAnalyzer::Analyzer::findArrayElementMutation(const Expr *Exp) {
// Check whether any element of an array is mutated.
const auto SubscriptExprs = match(
findAll(arraySubscriptExpr(
anyOf(hasBase(canResolveToExpr(Exp)),
hasBase(implicitCastExpr(allOf(
anyOf(hasBaseConservative(canResolveToExpr(Exp)),
hasBaseConservative(implicitCastExpr(allOf(
hasCastKind(CK_ArrayToPointerDecay),
hasSourceExpression(canResolveToExpr(Exp)))))))
.bind(NodeID<Expr>::value)),
Expand Down Expand Up @@ -716,7 +727,8 @@ ExprMutationAnalyzer::Analyzer::findPointeeValueMutation(const Expr *Exp) {
unaryOperator(hasOperatorName("*"),
hasUnaryOperand(canResolveToExprPointee(Exp))),
// deref by []
arraySubscriptExpr(hasBase(canResolveToExprPointee(Exp)))))
arraySubscriptExpr(
hasBaseConservative(canResolveToExprPointee(Exp)))))
.bind(NodeID<Expr>::value))),
Stm, Context);
return findExprMutation(Matches);
Expand Down
13 changes: 13 additions & 0 deletions clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,19 @@ TEST(ExprMutationAnalyzerTest, TemplateWithArrayToPointerDecay) {
EXPECT_THAT(mutatedBy(ResultsY, AST.get()), ElementsAre("y"));
}

TEST(ExprMutationAnalyzerTest, T1) {
const auto AST = buildASTFromCodeWithArgs(
"template <class T> struct vector { T &operator[](int t); };"
"template <typename T> void func() {"
" vector<int> x;"
" x[T{}] = 3;"
"}",
{"-fno-delayed-template-parsing"});
const auto Results =
match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
EXPECT_TRUE(isMutated(Results, AST.get()));
}

// section: special case: all created references are non-mutating themself
// and therefore all become 'const'/the value is not modified!

Expand Down