[clang] 0f1be87 - [Sema] Fix a -Warc-repeated-use-of-weak false-positive by only calling CheckPlaceholderExpr once
Erik Pilkington via cfe-commits
cfe-commits at lists.llvm.org
Thu Sep 3 13:57:40 PDT 2020
Author: Erik Pilkington
Date: 2020-09-03T16:56:35-04:00
New Revision: 0f1be87e294751a0941f1d9b7785ebf4d8072149
URL: https://github.com/llvm/llvm-project/commit/0f1be87e294751a0941f1d9b7785ebf4d8072149
DIFF: https://github.com/llvm/llvm-project/commit/0f1be87e294751a0941f1d9b7785ebf4d8072149.diff
LOG: [Sema] Fix a -Warc-repeated-use-of-weak false-positive by only calling CheckPlaceholderExpr once
Previously, this code discarded the result of CheckPlaceholderExpr for
non-matrix subexpressions. Not only is this wasteful, but it was creating a
Warc-repeated-use-of-weak false-positive on the attached testcase, since the
discarded expression was still registered as a use of the weak property.
rdar://66162246
Differential revision: https://reviews.llvm.org/D87102
Added:
Modified:
clang/lib/Sema/SemaExpr.cpp
clang/test/SemaObjC/arc-repeated-weak.mm
Removed:
################################################################################
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 450185788537..cd71ce70c70e 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -4595,8 +4595,8 @@ Sema::ActOnArraySubscriptExpr(Scope *S, Expr *base, SourceLocation lbLoc,
<< SourceRange(base->getBeginLoc(), rbLoc);
return ExprError();
}
- // If the base is either a MatrixSubscriptExpr or a matrix type, try to create
- // a new MatrixSubscriptExpr.
+ // If the base is a MatrixSubscriptExpr, try to create a new
+ // MatrixSubscriptExpr.
auto *matSubscriptE = dyn_cast<MatrixSubscriptExpr>(base);
if (matSubscriptE) {
if (CheckAndReportCommaError(idx))
@@ -4607,34 +4607,13 @@ Sema::ActOnArraySubscriptExpr(Scope *S, Expr *base, SourceLocation lbLoc,
return CreateBuiltinMatrixSubscriptExpr(
matSubscriptE->getBase(), matSubscriptE->getRowIdx(), idx, rbLoc);
}
- Expr *matrixBase = base;
- bool IsMSPropertySubscript = isMSPropertySubscriptExpr(*this, base);
- if (!IsMSPropertySubscript) {
- ExprResult result = CheckPlaceholderExpr(base);
- if (!result.isInvalid())
- matrixBase = result.get();
- }
- if (matrixBase->getType()->isMatrixType()) {
- if (CheckAndReportCommaError(idx))
- return ExprError();
-
- return CreateBuiltinMatrixSubscriptExpr(matrixBase, idx, nullptr, rbLoc);
- }
-
- // A comma-expression as the index is deprecated in C++2a onwards.
- if (getLangOpts().CPlusPlus20 &&
- ((isa<BinaryOperator>(idx) && cast<BinaryOperator>(idx)->isCommaOp()) ||
- (isa<CXXOperatorCallExpr>(idx) &&
- cast<CXXOperatorCallExpr>(idx)->getOperator() == OO_Comma))) {
- Diag(idx->getExprLoc(), diag::warn_deprecated_comma_subscript)
- << SourceRange(base->getBeginLoc(), rbLoc);
- }
// Handle any non-overload placeholder types in the base and index
// expressions. We can't handle overloads here because the other
// operand might be an overloadable type, in which case the overload
// resolution for the operator overload should get the first crack
// at the overload.
+ bool IsMSPropertySubscript = false;
if (base->getType()->isNonOverloadPlaceholderType()) {
IsMSPropertySubscript = isMSPropertySubscriptExpr(*this, base);
if (!IsMSPropertySubscript) {
@@ -4644,6 +4623,24 @@ Sema::ActOnArraySubscriptExpr(Scope *S, Expr *base, SourceLocation lbLoc,
base = result.get();
}
}
+
+ // If the base is a matrix type, try to create a new MatrixSubscriptExpr.
+ if (base->getType()->isMatrixType()) {
+ if (CheckAndReportCommaError(idx))
+ return ExprError();
+
+ return CreateBuiltinMatrixSubscriptExpr(base, idx, nullptr, rbLoc);
+ }
+
+ // A comma-expression as the index is deprecated in C++2a onwards.
+ if (getLangOpts().CPlusPlus20 &&
+ ((isa<BinaryOperator>(idx) && cast<BinaryOperator>(idx)->isCommaOp()) ||
+ (isa<CXXOperatorCallExpr>(idx) &&
+ cast<CXXOperatorCallExpr>(idx)->getOperator() == OO_Comma))) {
+ Diag(idx->getExprLoc(), diag::warn_deprecated_comma_subscript)
+ << SourceRange(base->getBeginLoc(), rbLoc);
+ }
+
if (idx->getType()->isNonOverloadPlaceholderType()) {
ExprResult result = CheckPlaceholderExpr(idx);
if (result.isInvalid()) return ExprError();
diff --git a/clang/test/SemaObjC/arc-repeated-weak.mm b/clang/test/SemaObjC/arc-repeated-weak.mm
index 4eec4d2fe69c..90388598c7b8 100644
--- a/clang/test/SemaObjC/arc-repeated-weak.mm
+++ b/clang/test/SemaObjC/arc-repeated-weak.mm
@@ -485,3 +485,17 @@ void foo1() {
@class NSString;
static NSString* const kGlobal = @"";
+
+ at interface NSDictionary
+- (id)objectForKeyedSubscript:(id)key;
+ at end
+
+ at interface WeakProp
+ at property (weak) NSDictionary *nd;
+ at end
+
+ at implementation WeakProp
+-(void)m {
+ (void)self.nd[@""]; // no warning
+}
+ at end
More information about the cfe-commits
mailing list