[PATCH] D103938: Diagnose -Wunused-value in constant evaluation context

Yuanfang Chen via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 8 17:51:00 PDT 2021


ychen created this revision.
ychen added reviewers: aaron.ballman, rsmith.
ychen requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

GCC/MSVC diagnoses in such cases, probably it makes sense to do the
same for Clang.
https://godbolt.org/z/7zxb8Tx96

It actually generated the warning internally, however since
`Sema::DiagnoseUnusedExprResult` called `Sema::DiagRuntimeBehavior` which
claims that "Relevant diagnostics should be produced by constant
evaluation.". So directly diagnose in `Sema::DiagnoseUnusedExprResult`
for constant evaluation context cases.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D103938

Files:
  clang/lib/Sema/SemaStmt.cpp
  clang/test/SemaCXX/warn-unused-value.cpp


Index: clang/test/SemaCXX/warn-unused-value.cpp
===================================================================
--- clang/test/SemaCXX/warn-unused-value.cpp
+++ clang/test/SemaCXX/warn-unused-value.cpp
@@ -138,3 +138,10 @@
   (void)arr3;
   (void)arr4;
 }
+
+#if __cplusplus >= 201103L // C++11 or later
+namespace test5 {
+int v[(5, 6)];                   // expected-warning {{expression result unused}}
+auto p1 = new double[4][(5, 6)]; // expected-warning {{expression result unused}}
+} // namespace test5
+#endif
Index: clang/lib/Sema/SemaStmt.cpp
===================================================================
--- clang/lib/Sema/SemaStmt.cpp
+++ clang/lib/Sema/SemaStmt.cpp
@@ -379,6 +379,12 @@
     return;
   }
 
+  if (ExprEvalContexts.back().Context ==
+      ExpressionEvaluationContext::ConstantEvaluated) {
+    Diag(Loc, DiagID) << R1 << R2;
+    return;
+  }
+
   DiagRuntimeBehavior(Loc, nullptr, PDiag(DiagID) << R1 << R2);
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D103938.350752.patch
Type: text/x-patch
Size: 956 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210609/34d15451/attachment.bin>


More information about the cfe-commits mailing list