Skip to content

[clang-tidy] fix misc-const-correctnes false-positive for fold expressions #78320

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
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
5 changes: 3 additions & 2 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,8 @@ Changes in existing checks
using pointer to member function. Additionally, the check no longer emits
a diagnostic when a variable that is not type-dependent is an operand of a
type-dependent binary operator. Improved performance of the check through
optimizations.
optimizations. The check no longer emits a diagnostic for non-parameter-pack
variables in C++17 fold expressions.

- Improved :doc:`misc-include-cleaner
<clang-tidy/checks/misc/include-cleaner>` check by adding option
Expand Down Expand Up @@ -502,7 +503,7 @@ Changes in existing checks
<clang-tidy/checks/readability/implicit-bool-conversion>` check to take
do-while loops into account for the `AllowIntegerConditions` and
`AllowPointerConditions` options. It also now provides more consistent
suggestions when parentheses are added to the return value or expressions.
suggestions when parentheses are added to the return value or expressions.
It also ignores false-positives for comparison containing bool bitfield.

- Improved :doc:`readability-misleading-indentation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,31 @@ namespace gh57297{
struct Stream { };
template <typename T> void f() { T t; Stream x; x << t; }
} // namespace gh57297

namespace gh70323{
// A fold expression may contain the checked variable as it's initializer.
// We don't know if the operator modifies that variable because the
// operator is type dependent due to the parameter pack.

struct Stream {};
template <typename... Args>
void concatenate1(Args... args)
{
Stream stream;
(stream << ... << args);
}

template <typename... Args>
void concatenate2(Args... args)
{
Stream stream;
(args << ... << stream);
}

template <typename... Args>
void concatenate3(Args... args)
{
Stream stream;
(..., (stream << args));
}
} // namespace gh70323
4 changes: 4 additions & 0 deletions clang/lib/Analysis/ExprMutationAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,10 @@ const Stmt *ExprMutationAnalyzer::findDirectMutation(const Expr *Exp) {
// in different instantiations of the template.
binaryOperator(isTypeDependent(),
hasEitherOperand(ignoringImpCasts(canResolveToExpr(Exp)))),
// A fold expression may contain `Exp` as it's initializer.
// We don't know if the operator modifies `Exp` because the
// operator is type dependent due to the parameter pack.
cxxFoldExpr(hasFoldInit(ignoringImpCasts(canResolveToExpr(Exp)))),
// Within class templates and member functions the member expression might
// not be resolved. In that case, the `callExpr` is considered to be a
// modification.
Expand Down
31 changes: 31 additions & 0 deletions clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,37 @@ TEST(ExprMutationAnalyzerTest, DependentOperatorWithNonDependentOperand) {
EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x << t"));
}

TEST(ExprMutationAnalyzerTest, FoldExpression) {
// gh70323
// A fold expression may contain `Exp` as it's initializer.
// We don't know if the operator modifies `Exp` because the
// operator is type dependent due to the parameter pack.
auto AST = buildASTFromCodeWithArgs(
"struct Stream {};"
"template <typename... Args> void concatenate(Args... args) "
"{ Stream x; (x << ... << args); }",
{"-fno-delayed-template-parsing"});
auto Results =
match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("(x << ... << args)"));

AST = buildASTFromCodeWithArgs(
"struct Stream {};"
"template <typename... Args> void concatenate(Args... args) "
"{ Stream x; (args << ... << x); }",
{"-fno-delayed-template-parsing"});
Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("(args << ... << x)"));

AST = buildASTFromCodeWithArgs(
"struct Stream {};"
"template <typename... Args> void concatenate(Args... args) "
"{ Stream x; (..., (x << args)); }",
{"-fno-delayed-template-parsing"});
Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x << args"));
}

// Section: expression as call argument

TEST(ExprMutationAnalyzerTest, ByValueArgument) {
Expand Down