================
@@ -480,6 +485,54 @@ class LifetimeSafetySemaHelperImpl : public
LifetimeSafetySemaHelper {
return "";
}
+ const Expr *extractExpr(const Expr *E) {
+ // FIXME: Ideally, this should use IgnoreParenImpCasts().
+ // However, according to the comment on IgnoreParenImpCasts(),
+ // it is not fully equivalent to IgnoreImpCasts() + IgnoreParens().
+ // Once the FIXME in IgnoreParenImpCasts() is resolved,
+ // this can be switched to use IgnoreParenImpCasts().
+ const Expr *PureExpr = E->IgnoreImpCasts()->IgnoreParens();
+
+ if (const auto *UO = dyn_cast<UnaryOperator>(PureExpr))
+ return UO->getSubExpr();
+ // For a BinaryOperator, there is only one relevant case: assignment
+ // chains. Therefore, we only need to consider the LHS expression.
+ if (const auto *BO = dyn_cast<BinaryOperator>(PureExpr))
+ return BO->getLHS();
+
+ // TODO: Handle other expression types.
+ return PureExpr;
+ }
+
+ void reportAliasingChain(llvm::ArrayRef<const Expr *> OriginExprChain) {
+ std::string IssueStr;
+ const Expr *LastExpr = nullptr;
+ for (const Expr *CurrExpr : reverse(OriginExprChain)) {
+ if (IssueStr.empty()) {
+ IssueStr = getDiagSubjectDescription(CurrExpr);
+ LastExpr = CurrExpr;
+ continue;
+ }
+
+ const Expr *ExtractedExpr = extractExpr(CurrExpr);
+ if (LastExpr &&
+ ExtractedExpr->getSourceRange() == LastExpr->getSourceRange())
+ continue;
+
+ // FIXME: Because getDiagSubjectDescription and extractExpr is not fully
+ // implemented yet, some diagnostic that should have been issued are
+ // currently being skipped here.
+ std::string ExprName = getDiagSubjectDescription(ExtractedExpr);
+ if (ExprName.empty())
+ continue;
----------------
usx95 wrote:
Let us change `getDiagSubjectDescription` to return "expression" as the default
instead of "" empty string to print all the expression we ask it to print. Not
printing something should be decided by `shouldShowInAliasingChain`.
This reduces the code here to
```cpp
S.Diag(ExtractedExpr->getBeginLoc(),
diag::note_lifetime_safety_aliases_storage)
<< ExtractedExpr->getSourceRange() <<
getDiagSubjectDescription(ExtractedExpr) << IssueStr;
```
https://github.com/llvm/llvm-project/pull/199345
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits