-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[Clang] Implement CWG2813: Class member access with prvalues #95112
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
Closed
MitalAshok
wants to merge
13
commits into
llvm:main
from
MitalAshok:prvalue-explicit-object-member-function-call
Closed
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e53dfbc
[Clang] Implement CWG2813
MitalAshok edac756
fix clangd AST test
MitalAshok a353728
Run [[nodiscard]] test in C++23
MitalAshok cc2c637
Change __declspec(property) handling; Refactor code; Tests
MitalAshok 3fe511a
Merge branch 'main' into prvalue-explicit-object-member-function-call
MitalAshok 9108fd0
anonymous namespace -> static
MitalAshok 1ff7db1
Merge branch 'main' into prvalue-explicit-object-member-function-call
MitalAshok 087a9f0
Update cwg28xx.cpp
MitalAshok e839e95
Merge branch 'main' into prvalue-explicit-object-member-function-call
MitalAshok 8e72680
Fix test failure
MitalAshok ef71fb7
Merge branch 'main' into prvalue-explicit-object-member-function-call
MitalAshok 95440d6
Add example from #100341
MitalAshok 78a3115
Retarget clang 20
MitalAshok File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -222,18 +222,13 @@ static bool DiagnoseNoDiscard(Sema &S, const WarnUnusedResultAttr *A, | |
| return S.Diag(Loc, diag::warn_unused_result_msg) << A << Msg << R1 << R2; | ||
| } | ||
|
|
||
| void Sema::DiagnoseUnusedExprResult(const Stmt *S, unsigned DiagID) { | ||
| const unsigned OrigDiagID = DiagID; | ||
| if (const LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S)) | ||
| return DiagnoseUnusedExprResult(Label->getSubStmt(), DiagID); | ||
|
|
||
| const Expr *E = dyn_cast_or_null<Expr>(S); | ||
| if (!E) | ||
| return; | ||
| namespace { | ||
MitalAshok marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| void DiagnoseUnused(Sema &S, const Expr *E, std::optional<unsigned> DiagID) { | ||
| bool NoDiscardOnly = !DiagID.has_value(); | ||
|
||
|
|
||
| // If we are in an unevaluated expression context, then there can be no unused | ||
| // results because the results aren't expected to be used in the first place. | ||
| if (isUnevaluatedContext()) | ||
| if (S.isUnevaluatedContext()) | ||
| return; | ||
|
|
||
| SourceLocation ExprLoc = E->IgnoreParenImpCasts()->getExprLoc(); | ||
|
|
@@ -242,30 +237,31 @@ void Sema::DiagnoseUnusedExprResult(const Stmt *S, unsigned DiagID) { | |
| // expression is a call to a function with the warn_unused_result attribute, | ||
| // we warn no matter the location. Because of the order in which the various | ||
| // checks need to happen, we factor out the macro-related test here. | ||
| bool ShouldSuppress = | ||
| SourceMgr.isMacroBodyExpansion(ExprLoc) || | ||
| SourceMgr.isInSystemMacro(ExprLoc); | ||
| bool ShouldSuppress = S.SourceMgr.isMacroBodyExpansion(ExprLoc) || | ||
| S.SourceMgr.isInSystemMacro(ExprLoc); | ||
|
|
||
| const Expr *WarnExpr; | ||
| SourceLocation Loc; | ||
| SourceRange R1, R2; | ||
| if (!E->isUnusedResultAWarning(WarnExpr, Loc, R1, R2, Context)) | ||
| if (!E->isUnusedResultAWarning(WarnExpr, Loc, R1, R2, S.Context)) | ||
| return; | ||
|
|
||
| // If this is a GNU statement expression expanded from a macro, it is probably | ||
| // unused because it is a function-like macro that can be used as either an | ||
| // expression or statement. Don't warn, because it is almost certainly a | ||
| // false positive. | ||
| if (isa<StmtExpr>(E) && Loc.isMacroID()) | ||
| return; | ||
|
|
||
| // Check if this is the UNREFERENCED_PARAMETER from the Microsoft headers. | ||
| // That macro is frequently used to suppress "unused parameter" warnings, | ||
| // but its implementation makes clang's -Wunused-value fire. Prevent this. | ||
| if (isa<ParenExpr>(E->IgnoreImpCasts()) && Loc.isMacroID()) { | ||
| SourceLocation SpellLoc = Loc; | ||
| if (findMacroSpelling(SpellLoc, "UNREFERENCED_PARAMETER")) | ||
| if (!NoDiscardOnly) { | ||
| // If this is a GNU statement expression expanded from a macro, it is | ||
| // probably unused because it is a function-like macro that can be used as | ||
| // either an expression or statement. Don't warn, because it is almost | ||
| // certainly a false positive. | ||
| if (isa<StmtExpr>(E) && Loc.isMacroID()) | ||
| return; | ||
|
|
||
| // Check if this is the UNREFERENCED_PARAMETER from the Microsoft headers. | ||
| // That macro is frequently used to suppress "unused parameter" warnings, | ||
| // but its implementation makes clang's -Wunused-value fire. Prevent this. | ||
| if (isa<ParenExpr>(E->IgnoreImpCasts()) && Loc.isMacroID()) { | ||
| SourceLocation SpellLoc = Loc; | ||
| if (S.findMacroSpelling(SpellLoc, "UNREFERENCED_PARAMETER")) | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| // Okay, we have an unused result. Depending on what the base expression is, | ||
|
|
@@ -276,7 +272,7 @@ void Sema::DiagnoseUnusedExprResult(const Stmt *S, unsigned DiagID) { | |
| if (const CXXBindTemporaryExpr *TempExpr = dyn_cast<CXXBindTemporaryExpr>(E)) | ||
| E = TempExpr->getSubExpr(); | ||
|
|
||
| if (DiagnoseUnusedComparison(*this, E)) | ||
| if (DiagnoseUnusedComparison(S, E)) | ||
| return; | ||
|
|
||
| E = WarnExpr; | ||
|
|
@@ -289,8 +285,9 @@ void Sema::DiagnoseUnusedExprResult(const Stmt *S, unsigned DiagID) { | |
| if (E->getType()->isVoidType()) | ||
| return; | ||
|
|
||
| if (DiagnoseNoDiscard(*this, cast_or_null<WarnUnusedResultAttr>( | ||
| CE->getUnusedResultAttr(Context)), | ||
| if (DiagnoseNoDiscard(S, | ||
| cast_if_present<WarnUnusedResultAttr>( | ||
| CE->getUnusedResultAttr(S.Context)), | ||
| Loc, R1, R2, /*isCtor=*/false)) | ||
| return; | ||
|
|
||
|
|
@@ -302,50 +299,50 @@ void Sema::DiagnoseUnusedExprResult(const Stmt *S, unsigned DiagID) { | |
| if (ShouldSuppress) | ||
| return; | ||
| if (FD->hasAttr<PureAttr>()) { | ||
| Diag(Loc, diag::warn_unused_call) << R1 << R2 << "pure"; | ||
| S.Diag(Loc, diag::warn_unused_call) << R1 << R2 << "pure"; | ||
| return; | ||
| } | ||
| if (FD->hasAttr<ConstAttr>()) { | ||
| Diag(Loc, diag::warn_unused_call) << R1 << R2 << "const"; | ||
| S.Diag(Loc, diag::warn_unused_call) << R1 << R2 << "const"; | ||
| return; | ||
| } | ||
| } | ||
| } else if (const auto *CE = dyn_cast<CXXConstructExpr>(E)) { | ||
| if (const CXXConstructorDecl *Ctor = CE->getConstructor()) { | ||
| const auto *A = Ctor->getAttr<WarnUnusedResultAttr>(); | ||
| A = A ? A : Ctor->getParent()->getAttr<WarnUnusedResultAttr>(); | ||
| if (DiagnoseNoDiscard(*this, A, Loc, R1, R2, /*isCtor=*/true)) | ||
| if (DiagnoseNoDiscard(S, A, Loc, R1, R2, /*isCtor=*/true)) | ||
| return; | ||
| } | ||
| } else if (const auto *ILE = dyn_cast<InitListExpr>(E)) { | ||
| if (const TagDecl *TD = ILE->getType()->getAsTagDecl()) { | ||
|
|
||
| if (DiagnoseNoDiscard(*this, TD->getAttr<WarnUnusedResultAttr>(), Loc, R1, | ||
| R2, /*isCtor=*/false)) | ||
| if (DiagnoseNoDiscard(S, TD->getAttr<WarnUnusedResultAttr>(), Loc, R1, R2, | ||
| /*isCtor=*/false)) | ||
| return; | ||
| } | ||
| } else if (ShouldSuppress) | ||
| return; | ||
|
|
||
| E = WarnExpr; | ||
| if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E)) { | ||
| if (getLangOpts().ObjCAutoRefCount && ME->isDelegateInitCall()) { | ||
| Diag(Loc, diag::err_arc_unused_init_message) << R1; | ||
| if (S.getLangOpts().ObjCAutoRefCount && ME->isDelegateInitCall()) { | ||
| S.Diag(Loc, diag::err_arc_unused_init_message) << R1; | ||
| return; | ||
| } | ||
| const ObjCMethodDecl *MD = ME->getMethodDecl(); | ||
| if (MD) { | ||
| if (DiagnoseNoDiscard(*this, MD->getAttr<WarnUnusedResultAttr>(), Loc, R1, | ||
| R2, /*isCtor=*/false)) | ||
| if (DiagnoseNoDiscard(S, MD->getAttr<WarnUnusedResultAttr>(), Loc, R1, R2, | ||
| /*isCtor=*/false)) | ||
| return; | ||
| } | ||
| } else if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) { | ||
| const Expr *Source = POE->getSyntacticForm(); | ||
| // Handle the actually selected call of an OpenMP specialized call. | ||
| if (LangOpts.OpenMP && isa<CallExpr>(Source) && | ||
| if (S.LangOpts.OpenMP && isa<CallExpr>(Source) && | ||
| POE->getNumSemanticExprs() == 1 && | ||
| isa<CallExpr>(POE->getSemanticExpr(0))) | ||
| return DiagnoseUnusedExprResult(POE->getSemanticExpr(0), DiagID); | ||
| return DiagnoseUnused(S, POE->getSemanticExpr(0), DiagID); | ||
| if (isa<ObjCSubscriptRefExpr>(Source)) | ||
| DiagID = diag::warn_unused_container_subscript_expr; | ||
| else if (isa<ObjCPropertyRefExpr>(Source)) | ||
|
|
@@ -362,17 +359,21 @@ void Sema::DiagnoseUnusedExprResult(const Stmt *S, unsigned DiagID) { | |
| if (!RD->getAttr<WarnUnusedAttr>()) | ||
| return; | ||
| } | ||
|
|
||
| if (NoDiscardOnly) | ||
| return; | ||
|
|
||
| // Diagnose "(void*) blah" as a typo for "(void) blah". | ||
| else if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(E)) { | ||
| if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(E)) { | ||
| TypeSourceInfo *TI = CE->getTypeInfoAsWritten(); | ||
| QualType T = TI->getType(); | ||
|
|
||
| // We really do want to use the non-canonical type here. | ||
| if (T == Context.VoidPtrTy) { | ||
| if (T == S.Context.VoidPtrTy) { | ||
| PointerTypeLoc TL = TI->getTypeLoc().castAs<PointerTypeLoc>(); | ||
|
|
||
| Diag(Loc, diag::warn_unused_voidptr) | ||
| << FixItHint::CreateRemoval(TL.getStarLoc()); | ||
| S.Diag(Loc, diag::warn_unused_voidptr) | ||
| << FixItHint::CreateRemoval(TL.getStarLoc()); | ||
| return; | ||
| } | ||
| } | ||
|
|
@@ -381,23 +382,34 @@ void Sema::DiagnoseUnusedExprResult(const Stmt *S, unsigned DiagID) { | |
| // isn't an array. | ||
| if (E->isGLValue() && E->getType().isVolatileQualified() && | ||
| !E->getType()->isArrayType()) { | ||
| Diag(Loc, diag::warn_unused_volatile) << R1 << R2; | ||
| S.Diag(Loc, diag::warn_unused_volatile) << R1 << R2; | ||
| return; | ||
| } | ||
|
|
||
| // Do not diagnose use of a comma operator in a SFINAE context because the | ||
| // type of the left operand could be used for SFINAE, so technically it is | ||
| // *used*. | ||
| if (DiagID == diag::warn_unused_comma_left_operand && isSFINAEContext()) | ||
| if (DiagID == diag::warn_unused_comma_left_operand && S.isSFINAEContext()) | ||
| return; | ||
|
|
||
| // Don't diagnose discarded left of dot in static class member access | ||
| // because its type is "used" to determine the class to access | ||
| if (OrigDiagID == diag::warn_discarded_class_member_access) | ||
| S.DiagIfReachable(Loc, llvm::ArrayRef<const Stmt *>(E), | ||
| S.PDiag(*DiagID) << R1 << R2); | ||
| } | ||
| } // namespace | ||
|
|
||
| void Sema::DiagnoseDiscardedNodiscard(const Expr *E) { | ||
| DiagnoseUnused(*this, E, std::nullopt); | ||
| } | ||
|
|
||
| void Sema::DiagnoseUnusedExprResult(const Stmt *S, unsigned DiagID) { | ||
| if (const LabelStmt *Label = dyn_cast_if_present<LabelStmt>(S)) | ||
| S = Label->getSubStmt(); | ||
|
|
||
| const Expr *E = dyn_cast_if_present<Expr>(S); | ||
| if (!E) | ||
| return; | ||
|
|
||
| DiagIfReachable(Loc, S ? llvm::ArrayRef(S) : std::nullopt, | ||
| PDiag(DiagID) << R1 << R2); | ||
| DiagnoseUnused(*this, E, DiagID); | ||
| } | ||
|
|
||
| void Sema::ActOnStartOfCompoundStmt(bool IsStmtExpr) { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we find a better name for that function?