Author: rtrieu Date: Fri Oct 19 19:15:58 2018 New Revision: 344828 URL: http://llvm.org/viewvc/llvm-project?rev=344828&view=rev Log: Make -Wfor-loop-analysis work with C++17
For now, disable the "variable in loop condition not modified" warning to not be emitted when there is a structured binding variable in the loop condition. https://bugs.llvm.org/show_bug.cgi?id=39285 Modified: cfe/trunk/lib/Sema/SemaStmt.cpp cfe/trunk/test/SemaCXX/warn-loop-analysis.cpp Modified: cfe/trunk/lib/Sema/SemaStmt.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaStmt.cpp?rev=344828&r1=344827&r2=344828&view=diff ============================================================================== --- cfe/trunk/lib/Sema/SemaStmt.cpp (original) +++ cfe/trunk/lib/Sema/SemaStmt.cpp Fri Oct 19 19:15:58 2018 @@ -1409,7 +1409,11 @@ namespace { void VisitDeclRefExpr(DeclRefExpr *E) { VarDecl *VD = dyn_cast<VarDecl>(E->getDecl()); - if (!VD) return; + if (!VD) { + // Don't allow unhandled Decl types. + Simple = false; + return; + } Ranges.push_back(E->getSourceRange()); Modified: cfe/trunk/test/SemaCXX/warn-loop-analysis.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-loop-analysis.cpp?rev=344828&r1=344827&r2=344828&view=diff ============================================================================== --- cfe/trunk/test/SemaCXX/warn-loop-analysis.cpp (original) +++ cfe/trunk/test/SemaCXX/warn-loop-analysis.cpp Fri Oct 19 19:15:58 2018 @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -fsyntax-only -Wloop-analysis -verify %s +// RUN: %clang_cc1 -fsyntax-only -Wloop-analysis -verify -std=c++17 %s struct S { bool stop() { return false; } @@ -278,3 +278,24 @@ void test9() { // Don't warn when variable is defined by the loop condition. for (int i = 0; int x = f(i); ++i) {} } + +// Don't warn when decomposition variables are in the loop condition. +// TODO: BindingDecl's which make a copy should warn. +void test10() { + int arr[] = {1, 2, 3}; + for (auto[i, j, k] = arr;;) { } + for (auto[i, j, k] = arr; i < j; ++i, ++j) { } + + for (auto[i, j, k] = arr; i;) { } + for (auto[i, j, k] = arr; i < j;) { } + for (auto[i, j, k] = arr; i < j; ++arr[0]) { } + + int a = 1, b = 2; + for (auto[i, j, k] = arr; a < b;) { } // expected-warning{{variables 'a' and 'b' used in loop condition not modified in loop body}} + for (auto[i, j, k] = arr; a < b; ++a) { } + + for (auto [i, j, k] = arr; i < a;) { } + for (auto[i, j, k] = arr; i < a; ++a) { } + for (auto[i, j, k] = arr; i < a; ++i) { } + for (auto[i, j, k] = arr; i < a; ++arr[0]) { } +}; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits