[PATCH] D140860: [Diagnostics][NFC] Fix -Wlogical-op-parentheses warning inconsistency for const and constexpr values

2023-01-02 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet created this revision.
hazohelet added a project: clang.
Herald added a project: All.
hazohelet requested review of this revision.

When using the `&&` operator within a `||` operator, both Clang and GCC produce 
a warning for potentially confusing operator precedence. However, Clang avoids 
this warning for certain patterns, such as `a && b || 0` or `a || b && 1`, 
where the operator precedence of `&&` and `||` does not change the result.

However, this behavior appears inconsistent when using the `const` or 
`constexpr` qualifiers. For example:

  bool t = true;
  bool tt = true || false && t; // Warning: '&&' within '||' 
[-Wlogical-op-parentheses]

  const bool t = true;
  bool tt = true || false && t; // No warning

  const bool t = false;
  bool tt = true || false && t; // Warning: '&&' within '||' 
[-Wlogical-op-parentheses]

The second example does not produce a warning because `true || false && t` 
matches the `a || b && 1` pattern, while the third one does not match any of 
them.

This behavior can lead to the lack of warnings for complicated `constexpr` 
expressions. Clang should only suppress this warning when literal values are 
placed in the place of `t` in the examples above.

This patch adds the literal-or-not check to fix the inconsistent warnings for 
`&&` within `||` when using const or constexpr.


https://reviews.llvm.org/D140860

Files:
  clang/lib/AST/ExprConstant.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/test/Sema/logical-op-parentheses.c
  clang/test/Sema/logical-op-parentheses.cpp

Index: clang/test/Sema/logical-op-parentheses.cpp
===
--- /dev/null
+++ clang/test/Sema/logical-op-parentheses.cpp
@@ -0,0 +1,66 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s -DSILENCE
+// RUN: %clang_cc1 -fsyntax-only -verify %s -Wlogical-op-parentheses
+// RUN: %clang_cc1 -fsyntax-only -verify %s -Wparentheses
+// RUN: %clang_cc1 -fsyntax-only -fdiagnostics-parseable-fixits %s -Wlogical-op-parentheses 2>&1 | FileCheck %s
+
+#ifdef SILENCE
+// expected-no-diagnostics
+#endif
+
+void logical_op_parentheses(unsigned i) {
+	constexpr bool t = 1;
+  (void)(i ||
+ i && i);
+#ifndef SILENCE
+  // expected-warning@-2 {{'&&' within '||'}}
+  // expected-note@-3 {{place parentheses around the '&&' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:20-[[@LINE-6]]:20}:")"
+
+  static_assert(t ||
+ t && t);
+#ifndef SILENCE
+  // expected-warning@-2 {{'&&' within '||'}}
+  // expected-note@-3 {{place parentheses around the '&&' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:20-[[@LINE-6]]:20}:")"
+
+  static_assert(t && 
+ t || t);
+#ifndef SILENCE
+  // expected-warning@-3 {{'&&' within '||'}}
+  // expected-note@-4 {{place parentheses around the '&&' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:17-[[@LINE-6]]:17}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:15-[[@LINE-6]]:15}:")"
+	
+	(void)(i || i && "w00t");
+  (void)("w00t" && i || i);
+  (void)("w00t" && t || t);
+  static_assert(t && t || 0);
+  static_assert(1 && t || t);
+  static_assert(0 || t && t);
+  static_assert(t || t && 1);
+
+  (void)(i || i && "w00t" || i);
+#ifndef SILENCE
+  // expected-warning@-2 {{'&&' within '||'}}
+  // expected-note@-3 {{place parentheses around the '&&' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:26-[[@LINE-6]]:26}:")"
+
+  (void)(i || "w00t" && i || i);
+#ifndef SILENCE
+  // expected-warning@-2 {{'&&' within '||'}}
+  // expected-note@-3 {{place parentheses around the '&&' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:26-[[@LINE-6]]:26}:")"
+
+  (void)(i && i || 0);
+  (void)(0 || i && i);
+}
+
Index: clang/test/Sema/logical-op-parentheses.c
===
--- clang/test/Sema/logical-op-parentheses.c
+++ clang/test/Sema/logical-op-parentheses.c
@@ -8,6 +8,7 @@
 #endif
 
 void logical_op_parentheses(unsigned i) {
+	const unsigned t = 1;
   (void)(i ||
  i && i);
 #ifndef SILENCE
@@ -17,8 +18,31 @@
   // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
   // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:20-[[@LINE-6]]:20}:")"
 
-  (void)(i || i && "w00t");
+  (void)(t ||
+ t && t);
+#ifndef SILENCE
+  // expected-warning@-2 {{'&&' within '||'}}
+  // expected-note@-3 {{place parentheses around the '&&' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:20-[[@LINE-6]]:20}:")"
+
+  (v

[PATCH] D140860: [Diagnostics][NFC] Fix -Wlogical-op-parentheses warning inconsistency for const and constexpr values

2023-01-05 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

As you point out, enhancement may be more accurate than bug fix.
There are rare cases where enabling a warning for missing parentheses in 
`constexpr` logical expressions can be helpful, I think. For example, consider 
the following code:

  constexpr A = ...;
  constexpr B = ...;
  constexpr C = ...;
  
  static_assert(!(A && B || C));

In this case, the static assertion will only be successful if `(A && B || C)` 
evaluates to `false`, which is equivalent to the following combinations of 
values for `A`, `B`, and `C`:

  (A, B, C) = (T, F, F) (F, T, F) (F, F, F)

Note that `T` means `true` and `F` means `false`. Here, `C` is always `false`, 
so `A && B || C` matches the case of `a && b || 0`. Thus, the warning is not 
issued before this patch.
If the programmer is not careful and assumes that `(A && B || C)` is equivalent 
to `(A && (B || C))`, then they expect the values of `A`, `B`, and `C` to also 
include the following combinations:

  (A, B, C) = (F, T, T) (F, F, T)

This would require the programmer to consider additional, unnecessary 
combinations after the successful static assertion.

Enabling a warning for missing parentheses in this scenario could help prevent 
the programmer from making this mistake.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D140860/new/

https://reviews.llvm.org/D140860

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D140860: [Diagnostics][NFC] Fix -Wlogical-op-parentheses warning inconsistency for const and constexpr values

2023-01-06 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet updated this revision to Diff 486776.
hazohelet added a comment.

This update limits the warning suppression case to string literals only, and 
delete no longer necessary functions.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D140860/new/

https://reviews.llvm.org/D140860

Files:
  clang/lib/AST/ExprConstant.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/test/Sema/logical-op-parentheses.c
  clang/test/Sema/logical-op-parentheses.cpp

Index: clang/test/Sema/logical-op-parentheses.cpp
===
--- clang/test/Sema/logical-op-parentheses.cpp
+++ /dev/null
@@ -1,66 +0,0 @@
-// RUN: %clang_cc1 -fsyntax-only -verify %s -DSILENCE
-// RUN: %clang_cc1 -fsyntax-only -verify %s -Wlogical-op-parentheses
-// RUN: %clang_cc1 -fsyntax-only -verify %s -Wparentheses
-// RUN: %clang_cc1 -fsyntax-only -fdiagnostics-parseable-fixits %s -Wlogical-op-parentheses 2>&1 | FileCheck %s
-
-#ifdef SILENCE
-// expected-no-diagnostics
-#endif
-
-void logical_op_parentheses(unsigned i) {
-	constexpr bool t = 1;
-  (void)(i ||
- i && i);
-#ifndef SILENCE
-  // expected-warning@-2 {{'&&' within '||'}}
-  // expected-note@-3 {{place parentheses around the '&&' expression to silence this warning}}
-#endif
-  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
-  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:20-[[@LINE-6]]:20}:")"
-
-  static_assert(t ||
- t && t);
-#ifndef SILENCE
-  // expected-warning@-2 {{'&&' within '||'}}
-  // expected-note@-3 {{place parentheses around the '&&' expression to silence this warning}}
-#endif
-  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
-  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:20-[[@LINE-6]]:20}:")"
-
-  static_assert(t && 
- t || t);
-#ifndef SILENCE
-  // expected-warning@-3 {{'&&' within '||'}}
-  // expected-note@-4 {{place parentheses around the '&&' expression to silence this warning}}
-#endif
-  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:17-[[@LINE-6]]:17}:"("
-  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:15-[[@LINE-6]]:15}:")"
-	
-	(void)(i || i && "w00t");
-  (void)("w00t" && i || i);
-  (void)("w00t" && t || t);
-  static_assert(t && t || 0);
-  static_assert(1 && t || t);
-  static_assert(0 || t && t);
-  static_assert(t || t && 1);
-
-  (void)(i || i && "w00t" || i);
-#ifndef SILENCE
-  // expected-warning@-2 {{'&&' within '||'}}
-  // expected-note@-3 {{place parentheses around the '&&' expression to silence this warning}}
-#endif
-  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("
-  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:26-[[@LINE-6]]:26}:")"
-
-  (void)(i || "w00t" && i || i);
-#ifndef SILENCE
-  // expected-warning@-2 {{'&&' within '||'}}
-  // expected-note@-3 {{place parentheses around the '&&' expression to silence this warning}}
-#endif
-  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("
-  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:26-[[@LINE-6]]:26}:")"
-
-  (void)(i && i || 0);
-  (void)(0 || i && i);
-}
-
Index: clang/test/Sema/logical-op-parentheses.c
===
--- clang/test/Sema/logical-op-parentheses.c
+++ clang/test/Sema/logical-op-parentheses.c
@@ -40,9 +40,33 @@
   (void)("w00t" && i || i);
   (void)("w00t" && t || t);
   (void)(t && t || 0);
+#ifndef SILENCE
+  // expected-warning@-2 {{'&&' within '||'}}
+  // expected-note@-3 {{place parentheses around the '&&' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:10-[[@LINE-5]]:10}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:16-[[@LINE-6]]:16}:")"
   (void)(1 && t || t);
+#ifndef SILENCE
+  // expected-warning@-2 {{'&&' within '||'}}
+  // expected-note@-3 {{place parentheses around the '&&' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:10-[[@LINE-5]]:10}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:16-[[@LINE-6]]:16}:")"
   (void)(0 || t && t);
+#ifndef SILENCE
+  // expected-warning@-2 {{'&&' within '||'}}
+  // expected-note@-3 {{place parentheses around the '&&' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:21-[[@LINE-6]]:21}:")"
   (void)(t || t && 1);
+#ifndef SILENCE
+  // expected-warning@-2 {{'&&' within '||'}}
+  // expected-note@-3 {{place parentheses around the '&&' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:21-[[@LINE-6]]:21}:")"
 
   (void)(i || i && "w00t" || i);
 #ifndef SILENCE
@@ -61,5 +85,17 @@
   // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:26-[[@LINE-6]]:26}:")"
 
   (void)(i && i || 0);
+#ifndef SILENCE
+  // expected-warning@-2 {{'&&' within '||'}}
+  // expected-note@-3 {{place parentheses around the '&&' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:10-[[@LINE-5]]:10}:"("

[PATCH] D140860: [Diagnostics][NFC] Fix -Wlogical-op-parentheses warning inconsistency for const and constexpr values

2023-01-06 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet updated this revision to Diff 486793.
hazohelet added a comment.

add up the former 2 commits into 1


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D140860/new/

https://reviews.llvm.org/D140860

Files:
  clang/lib/Sema/SemaExpr.cpp
  clang/test/Sema/logical-op-parentheses.c

Index: clang/test/Sema/logical-op-parentheses.c
===
--- clang/test/Sema/logical-op-parentheses.c
+++ clang/test/Sema/logical-op-parentheses.c
@@ -8,6 +8,7 @@
 #endif
 
 void logical_op_parentheses(unsigned i) {
+	const unsigned t = 1;
   (void)(i ||
  i && i);
 #ifndef SILENCE
@@ -17,8 +18,55 @@
   // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
   // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:20-[[@LINE-6]]:20}:")"
 
-  (void)(i || i && "w00t");
+  (void)(t ||
+ t && t);
+#ifndef SILENCE
+  // expected-warning@-2 {{'&&' within '||'}}
+  // expected-note@-3 {{place parentheses around the '&&' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:20-[[@LINE-6]]:20}:")"
+
+  (void)(t && 
+ t || t);
+#ifndef SILENCE
+  // expected-warning@-3 {{'&&' within '||'}}
+  // expected-note@-4 {{place parentheses around the '&&' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:10-[[@LINE-6]]:10}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:15-[[@LINE-6]]:15}:")"
+	
+	(void)(i || i && "w00t");
   (void)("w00t" && i || i);
+  (void)("w00t" && t || t);
+  (void)(t && t || 0);
+#ifndef SILENCE
+  // expected-warning@-2 {{'&&' within '||'}}
+  // expected-note@-3 {{place parentheses around the '&&' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:10-[[@LINE-5]]:10}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:16-[[@LINE-6]]:16}:")"
+  (void)(1 && t || t);
+#ifndef SILENCE
+  // expected-warning@-2 {{'&&' within '||'}}
+  // expected-note@-3 {{place parentheses around the '&&' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:10-[[@LINE-5]]:10}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:16-[[@LINE-6]]:16}:")"
+  (void)(0 || t && t);
+#ifndef SILENCE
+  // expected-warning@-2 {{'&&' within '||'}}
+  // expected-note@-3 {{place parentheses around the '&&' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:21-[[@LINE-6]]:21}:")"
+  (void)(t || t && 1);
+#ifndef SILENCE
+  // expected-warning@-2 {{'&&' within '||'}}
+  // expected-note@-3 {{place parentheses around the '&&' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:21-[[@LINE-6]]:21}:")"
 
   (void)(i || i && "w00t" || i);
 #ifndef SILENCE
@@ -37,5 +85,17 @@
   // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:26-[[@LINE-6]]:26}:")"
 
   (void)(i && i || 0);
+#ifndef SILENCE
+  // expected-warning@-2 {{'&&' within '||'}}
+  // expected-note@-3 {{place parentheses around the '&&' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:10-[[@LINE-5]]:10}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:16-[[@LINE-6]]:16}:")"
   (void)(0 || i && i);
+#ifndef SILENCE
+  // expected-warning@-2 {{'&&' within '||'}}
+  // expected-note@-3 {{place parentheses around the '&&' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:21-[[@LINE-6]]:21}:")"
 }
Index: clang/lib/Sema/SemaExpr.cpp
===
--- clang/lib/Sema/SemaExpr.cpp
+++ clang/lib/Sema/SemaExpr.cpp
@@ -15206,38 +15206,21 @@
 Bop->getSourceRange());
 }
 
-/// Returns true if the given expression can be evaluated as a constant
-/// 'true'.
-static bool EvaluatesAsTrue(Sema &S, Expr *E) {
-  bool Res;
-  return !E->isValueDependent() &&
- E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && Res;
-}
-
-/// Returns true if the given expression can be evaluated as a constant
-/// 'false'.
-static bool EvaluatesAsFalse(Sema &S, Expr *E) {
-  bool Res;
-  return !E->isValueDependent() &&
- E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && !Res;
-}
-
 /// Look for '&&' in the left hand of a '||' expr.
 static void DiagnoseLogicalAndInLogicalOrLHS(Sema &S, SourceLocation OpLoc,
  Expr *LHSExpr, Expr *RHSExpr) {
   if (BinaryOperator *Bop = dyn_cast(LHSExpr)) {
 if (Bop->getOpcode() == BO_LAnd) {
-  // If it's "a && b || 0" don't warn since the precedence doesn't matter.
-  if (EvaluatesAsFalse(S, RHSExpr))
-return;
-  // If it's "1 && a || b" don't warn since the precedence doesn't matter.
-  if (!EvaluatesAsTrue(S, Bop->getLHS()))
+  // If it's 

[PATCH] D140860: [Diagnostics][NFC] Fix -Wlogical-op-parentheses warning inconsistency for const and constexpr values

2023-01-06 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

> So, so long as a string literal still does the suppression, it's probably 
> fine.

I agree with you. I now also think that integer literals _should not_ do the 
warning suppression because programmers are sometimes unsure of the expansion 
result of predefined macros in the compiled environment.
I updated the differential. I am new to Phabricator and made some unnecessary 
operations. Sorry for bothering you.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D140860/new/

https://reviews.llvm.org/D140860

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D140860: [Diagnostics][NFC] Fix -Wlogical-op-parentheses warning inconsistency for const and constexpr values

2023-01-07 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

I have yet to do thorough checks using this patched clang to build significant 
code bases.
It will likely take quite a bit of time as I am not used to editing build tool 
files.

Instead, I used `grep` to find potentially newly-warned codes.
The `grep` command is shown below. It is intended to match C/C++ codes with 
both `&&` and `||` within which 0, 1, or 2 matching parentheses exist in a 
single line.
`grep -r --include=\*.cpp --include=\*.cc  --include=\*.c -e "&&[^()]*||" -e 
"||[^()]*&&" -e "&&[^()]*([^()]*)[^()]*||" -e "||[^()]*([^()]*)[^()]*&&" -e 
"&&[^()]*([^()]*([^()]*)[^()]*)[^()]*||" -e 
"||[^()]*([^()]*([^()]*)[^()]*)[^()]*&&"`

I applied this `grep` command to the following popular GitHub repositories 
`NVIDIA/TensorRT`, `bulletphysics/bullet3`, `apple/foundationdb`, `grpc/grpc`, 
`microsoft/lightgbm`, `rui314/mold`, `oneapi-src/oneTBB`, 
`SerenityOS/serenity`, `tensorflow/tensorflow`.
I found the 7 examples of missing parentheses at `&&` within `||` in the 
matched strings (many of the matchings exist in preprocessor conditionals, 
which is out of the scope of this patch)
They are listed at the end of this comment.
The last case in tensorflow is an `assert(a || b && "reason for the assert")` 
known idiom and is handled correctly.
Because the newly-warned constants are compile-time constants not dependent on 
function arguments, the other 6 cases will also get warnings from the clang 
before this patch.

It suggests that this patch does not generate extensive new warnings in 
existent codebases.

oneTBB:
https://github.com/oneapi-src/oneTBB/blob/e6e493f96ec8b7e2e2b4d048ed49356eb54ec2a0/examples/common/gui/xvideo.cpp#L359
https://github.com/oneapi-src/oneTBB/blob/e6e493f96ec8b7e2e2b4d048ed49356eb54ec2a0/src/tbbmalloc/frontend.cpp#L1266
tensorflow:
https://github.com/tensorflow/tensorflow/blob/fb2c3b1e3140af73f949981d8428379cbb28228b/tensorflow/compiler/tf2tensorrt/convert/convert_nodes_test.cc#L4130
https://github.com/tensorflow/tensorflow/blob/fb2c3b1e3140af73f949981d8428379cbb28228b/tensorflow/compiler/tf2tensorrt/convert/convert_nodes_test.cc#L4074
https://github.com/tensorflow/tensorflow/blob/fb2c3b1e3140af73f949981d8428379cbb28228b/tensorflow/compiler/tf2tensorrt/convert/convert_nodes_test.cc#L9456
https://github.com/tensorflow/tensorflow/blob/fb2c3b1e3140af73f949981d8428379cbb28228b/tensorflow/compiler/jit/deadness_analysis.cc#L1423
https://github.com/tensorflow/tensorflow/blob/fb2c3b1e3140af73f949981d8428379cbb28228b/tensorflow/core/ir/tf_op_wrapper.cc#L25


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D140860/new/

https://reviews.llvm.org/D140860

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D140860: [Diagnostics][NFC] Fix -Wlogical-op-parentheses warning inconsistency for const and constexpr values

2023-01-10 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

> FWIW a lot of build systems support setting CXXFLAGS/CFLAGS before invoking 
> the build system/build generator (cmake, for instance) and respects those - 
> so might be relatively easy to add a new warning flag to the build (& CXX/CC 
> to set the compiler to point to your local build with the patch/changes)

Thanks for your advice! I'll give it a try. It might be a nice opportunity for 
me to learn some compiler development methods.

> So, based on best guess at least from grepping - you don't find any new 
> instances of the warning in these code bases?

No, I did not find any.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D140860/new/

https://reviews.llvm.org/D140860

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D142800: [Clang][Diagnostic] Add `-Wcomparison-op-parentheses` to warn on chained comparisons

2023-01-27 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet created this revision.
hazohelet added reviewers: dblaikie, aaron.ballman, erichkeane.
Herald added a project: All.
hazohelet requested review of this revision.
Herald added a reviewer: jdoerfert.
Herald added a subscriber: sstefan1.
Herald added a project: clang.

This patch introduces a new warning flag `-Wcomparison-op-parentheses` in 
`-Wparentheses` to issue warnings with its fixit hint for comparison operators 
within another comparison operator.

This fixes https://github.com/llvm/llvm-project/issues/60256


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D142800

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Basic/DiagnosticGroups.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Sema/SemaExpr.cpp
  clang/test/Misc/warning-wall.c
  clang/test/Sema/comparison-op-parentheses.c

Index: clang/test/Sema/comparison-op-parentheses.c
===
--- /dev/null
+++ clang/test/Sema/comparison-op-parentheses.c
@@ -0,0 +1,96 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s -DSILENCE
+// RUN: %clang_cc1 -fsyntax-only -verify %s -Wcomparison-op-parentheses
+// RUN: %clang_cc1 -fsyntax-only -verify %s -Wparentheses
+// RUN: %clang_cc1 -fsyntax-only -fdiagnostics-parseable-fixits %s -Wcomparison-op-parentheses 2>&1 | FileCheck %s
+
+#ifdef SILENCE
+// expected-no-diagnostics
+#endif
+
+void comparison_op_parentheses(int a, int b, int c) {
+  (void)(a ==
+ b > c);
+#ifndef SILENCE
+  // expected-warning@-2 {{'>' within '=='}}
+  // expected-note@-3 {{place parentheses around the '>' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:19-[[@LINE-6]]:19}:")"
+
+  (void)(a ==b == c);
+#ifndef SILENCE
+  // expected-warning@-2 {{'==' within '=='}}
+  // expected-note@-3 {{place parentheses around the '==' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:10-[[@LINE-5]]:10}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:15-[[@LINE-6]]:15}:")"
+
+  (void)(a !=b == c);
+#ifndef SILENCE
+  // expected-warning@-2 {{'!=' within '=='}}
+  // expected-note@-3 {{place parentheses around the '!=' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:10-[[@LINE-5]]:10}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:15-[[@LINE-6]]:15}:")"
+
+  (void)(a !=
+ b < c);
+#ifndef SILENCE
+  // expected-warning@-2 {{'<' within '!='}}
+  // expected-note@-3 {{place parentheses around the '<' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:19-[[@LINE-6]]:19}:")"
+
+  (void)(a>=b >= c);
+#ifndef SILENCE
+  // expected-warning@-2 {{'>=' within '>='}}
+  // expected-note@-3 {{place parentheses around the '>=' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:10-[[@LINE-5]]:10}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:14-[[@LINE-6]]:14}:")"
+
+  (void)(a >b >= c);
+#ifndef SILENCE
+  // expected-warning@-2 {{'>' within '>='}}
+  // expected-note@-3 {{place parentheses around the '>' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:10-[[@LINE-5]]:10}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:14-[[@LINE-6]]:14}:")"
+
+  (void)(a >b > c);
+#ifndef SILENCE
+  // expected-warning@-2 {{'>' within '>'}}
+  // expected-note@-3 {{place parentheses around the '>' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:10-[[@LINE-5]]:10}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:14-[[@LINE-6]]:14}:")"
+
+
+
+  (void)(a c);
+#ifndef SILENCE
+  // expected-warning@-2 {{'<' within '>'}}
+  // expected-note@-3 {{place parentheses around the '<' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:10-[[@LINE-5]]:10}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:13-[[@LINE-6]]:13}:")"
+
+  (void)(a != (b > c));
+  (void)(a == (b > c));
+  (void)((a>b) >= c);
+  (void)((a c);
+  (void)(a != b && a > c);
+  (void)((a c);
+  (void)((a (c > a));
+
+}
Index: clang/test/Misc/warning-wall.c
===
--- clang/test/Misc/warning-wall.c
+++ clang/test/Misc/warning-wall.c
@@ -92,6 +92,7 @@
 CHECK-NEXT:-Wlogical-not-parentheses
 CHECK-NEXT:-Wbitwise-conditional-parentheses
 CHECK-NEXT:-Wbitwise-op-parentheses
+CHECK-NEXT:-Wcomparison-op-parentheses
 CHECK-NEXT:-Wshift-op-parentheses
 CHECK-NEXT:-Woverloaded-shift-op-parentheses
 CHECK-NEXT:-Wparentheses-equality
Index: clang/lib/Sema/SemaExpr.cpp
===
--- clang/lib/Sema/SemaExpr.cpp
+++ clang/lib/Sema/SemaExpr.cpp
@@ -15534,6 +15534,37 @@
   SourceRange(OCE->getArg(1)->getBeginLoc(), RHSExpr->getEndLoc()));
 }
 
+/// It acc

[PATCH] D142800: [Clang][Diagnostic] Add `-Wcomparison-op-parentheses` to warn on chained comparisons

2023-01-28 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet updated this revision to Diff 492986.
hazohelet added a comment.

Fix the new warning issued against libcxx test code.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D142800/new/

https://reviews.llvm.org/D142800

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Basic/DiagnosticGroups.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Sema/SemaExpr.cpp
  clang/test/Misc/warning-wall.c
  clang/test/Sema/comparison-op-parentheses.c
  libcxx/test/support/test_comparisons.h

Index: libcxx/test/support/test_comparisons.h
===
--- libcxx/test/support/test_comparisons.h
+++ libcxx/test/support/test_comparisons.h
@@ -162,7 +162,7 @@
 bool less= order == Order::less;
 bool greater = order == Order::greater;
 
-return (t1 <=> t2 == order) && testComparisonsComplete(t1, t2, equal, less, greater);
+return ((t1 <=> t2) == order) && testComparisonsComplete(t1, t2, equal, less, greater);
 }
 
 template 
Index: clang/test/Sema/comparison-op-parentheses.c
===
--- /dev/null
+++ clang/test/Sema/comparison-op-parentheses.c
@@ -0,0 +1,94 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s -DSILENCE
+// RUN: %clang_cc1 -fsyntax-only -verify %s -Wcomparison-op-parentheses
+// RUN: %clang_cc1 -fsyntax-only -verify %s -Wparentheses
+// RUN: %clang_cc1 -fsyntax-only -fdiagnostics-parseable-fixits %s -Wcomparison-op-parentheses 2>&1 | FileCheck %s
+
+#ifdef SILENCE
+// expected-no-diagnostics
+#endif
+
+void comparison_op_parentheses(int a, int b, int c) {
+  (void)(a ==
+ b > c);
+#ifndef SILENCE
+  // expected-warning@-2 {{'>' within '=='}}
+  // expected-note@-3 {{place parentheses around the '>' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:19-[[@LINE-6]]:19}:")"
+
+  (void)(a ==b == c);
+#ifndef SILENCE
+  // expected-warning@-2 {{'==' within '=='}}
+  // expected-note@-3 {{place parentheses around the '==' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:10-[[@LINE-5]]:10}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:15-[[@LINE-6]]:15}:")"
+
+  (void)(a !=b == c);
+#ifndef SILENCE
+  // expected-warning@-2 {{'!=' within '=='}}
+  // expected-note@-3 {{place parentheses around the '!=' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:10-[[@LINE-5]]:10}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:15-[[@LINE-6]]:15}:")"
+
+  (void)(a !=
+ b < c);
+#ifndef SILENCE
+  // expected-warning@-2 {{'<' within '!='}}
+  // expected-note@-3 {{place parentheses around the '<' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:19-[[@LINE-6]]:19}:")"
+
+  (void)(a>=b >= c);
+#ifndef SILENCE
+  // expected-warning@-2 {{'>=' within '>='}}
+  // expected-note@-3 {{place parentheses around the '>=' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:10-[[@LINE-5]]:10}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:14-[[@LINE-6]]:14}:")"
+
+  (void)(a >b >= c);
+#ifndef SILENCE
+  // expected-warning@-2 {{'>' within '>='}}
+  // expected-note@-3 {{place parentheses around the '>' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:10-[[@LINE-5]]:10}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:14-[[@LINE-6]]:14}:")"
+
+  (void)(a >b > c);
+#ifndef SILENCE
+  // expected-warning@-2 {{'>' within '>'}}
+  // expected-note@-3 {{place parentheses around the '>' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:10-[[@LINE-5]]:10}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:14-[[@LINE-6]]:14}:")"
+
+
+
+  (void)(a c);
+#ifndef SILENCE
+  // expected-warning@-2 {{'<' within '>'}}
+  // expected-note@-3 {{place parentheses around the '<' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:10-[[@LINE-5]]:10}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:13-[[@LINE-6]]:13}:")"
+
+  (void)(a != (b > c));
+  (void)(a == (b > c));
+  (void)((a>b) >= c);
+  (void)((a c);
+  (void)(a != b && a > c);
+  (void)((a c);
+}
Index: clang/test/Misc/warning-wall.c
===
--- clang/test/Misc/warning-wall.c
+++ clang/test/Misc/warning-wall.c
@@ -92,6 +92,7 @@
 CHECK-NEXT:-Wlogical-not-parentheses
 CHECK-NEXT:-Wbitwise-conditional-parentheses
 CHECK-NEXT:-Wbitwise-op-parentheses
+CHECK-NEXT:-Wcomparison-op-parentheses
 CHECK-NEXT:-Wshift-op-parentheses
 CHECK-NEXT:-Woverloaded-shift-op-parentheses
 CHECK-NEXT:-Wparentheses-equality
Index: clang/lib/Sema/SemaExpr.cpp
===
--- clang/lib/Sema/SemaExp

[PATCH] D142800: [Clang][Diagnostic] Add `-Wcomparison-op-parentheses` to warn on chained comparisons

2023-01-31 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added inline comments.



Comment at: clang/lib/Sema/SemaExpr.cpp:15548
+
+  SuggestParentheses(Self, Bop->getOperatorLoc(),
+ Self.PDiag(diag::note_precedence_silence)

erichkeane wrote:
> I find myself wondering if we could provide a better 'suggested fix' here.  
> Aaron is better with the diagnostics than I am, but I would think that 
> someone doing:
> 
> `a < b < c` PROBABLY doesn't mean that, they probably mean: `a < b && b < c`.
> 
> Also, is the mistake the 'same' when they do something like `a > b != c` ?  
> It would seem to me that mixing operators might make it something either more 
> intentional/meaningful.  ADDITIONALLY, in the case where they are booleans, 
> these end up being overly noisy.  The pattern of the  == c (where 'c' is 
> bool, or convertible to bool) is probably intentional.
> 
> I think the logic here needs to be more complicated than just "Comparison 
> within Comparison", however I don't have a fully formed idea of when to 
> diagnose.
> 
> @tahonermann : Do you perhaps have a good idea?
> I find myself wondering if we could provide a better 'suggested fix' here.  
> Aaron is better with the diagnostics than I am, but I would think that 
> someone doing:
> 
> `a < b < c` PROBABLY doesn't mean that, they probably mean: `a < b && b < c`.

Yes. We could provide a better fix-it hint.
My idea:
- In the case of chained relational operators (`<`, `>`, `<=`, `>=`), clang 
suggests adding `&&`.
- In other cases, clang suggests parentheses.
How about doing it this way? It's similar to how Rust handles chained 
comparisons.

> Also, is the mistake the 'same' when they do something like `a > b != c` ?  
> It would seem to me that mixing operators might make it something either more 
> intentional/meaningful.  ADDITIONALLY, in the case where they are booleans, 
> these end up being overly noisy.  The pattern of the  == c (where 'c' is 
> bool, or convertible to bool) is probably intentional.
> 
> I think the logic here needs to be more complicated than just "Comparison 
> within Comparison", however I don't have a fully formed idea of when to 
> diagnose.

I have a differing perspective on suppressing the warning for boolean and 
boolean-convertible values.
There are two possible programmer mistakes in chained comparisons.
1. `a > b != c` misunderstood as `a > b && b != c`
2. `a > b != c` misunderstood as `a > (b != c)`
While the latter is considered rare in this scenario, the former could be 
likely to happen due to other languages, such as Python handling chained 
comparisons in the former manner.




CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D142800/new/

https://reviews.llvm.org/D142800

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D142800: [Clang][Diagnostic] Add `-Wcomparison-op-parentheses` to warn on chained comparisons

2023-02-03 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet updated this revision to Diff 494635.
hazohelet added a comment.

Address comments from erichkeane:

- Fix comment wording
- Avoid using macro in test file

Implement my proposal for fix-it hint:

- In the case of chained relational operators (`<`, `>`, `<=`, `>=`), suggest 
adding `&&`.
- In other cases, suggest parentheses.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D142800/new/

https://reviews.llvm.org/D142800

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Basic/DiagnosticGroups.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Sema/SemaExpr.cpp
  clang/test/Misc/warning-wall.c
  clang/test/Sema/comparison-op-parentheses.c
  libcxx/test/support/test_comparisons.h

Index: libcxx/test/support/test_comparisons.h
===
--- libcxx/test/support/test_comparisons.h
+++ libcxx/test/support/test_comparisons.h
@@ -162,7 +162,7 @@
 bool less= order == Order::less;
 bool greater = order == Order::greater;
 
-return (t1 <=> t2 == order) && testComparisonsComplete(t1, t2, equal, less, greater);
+return ((t1 <=> t2) == order) && testComparisonsComplete(t1, t2, equal, less, greater);
 }
 
 template 
Index: clang/test/Sema/comparison-op-parentheses.c
===
--- /dev/null
+++ clang/test/Sema/comparison-op-parentheses.c
@@ -0,0 +1,73 @@
+// RUN: %clang_cc1 -fsyntax-only -verify=off %s
+// RUN: %clang_cc1 -fsyntax-only -verify %s -Wcomparison-op-parentheses
+// RUN: %clang_cc1 -fsyntax-only -verify %s -Wparentheses
+// RUN: %clang_cc1 -fsyntax-only -fdiagnostics-parseable-fixits %s -Wcomparison-op-parentheses 2>&1 | FileCheck %s
+
+// off-no-diagnostics
+
+void comparison_op_parentheses(int a, int b, int c) {
+  (void)(a ==
+ b > c);
+  // expected-warning@-1 {{'>' within '=='}}
+  // expected-note@-2 {{place parentheses around the '>' expression to silence this warning}}
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:14-[[@LINE-3]]:14}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:19-[[@LINE-4]]:19}:")"
+
+  (void)(a ==b == c);
+  // expected-warning@-1 {{'==' within '=='}}
+  // expected-note@-2 {{place parentheses around the '==' expression to silence this warning}}
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:15-[[@LINE-4]]:15}:")"
+
+  (void)(a !=b == c);
+  // expected-warning@-1 {{'!=' within '=='}}
+  // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}}
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:15-[[@LINE-4]]:15}:")"
+
+  (void)(a !=
+ b < c);
+  // expected-warning@-1 {{'<' within '!='}}
+  // expected-note@-2 {{place parentheses around the '<' expression to silence this warning}}
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:14-[[@LINE-3]]:14}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:19-[[@LINE-4]]:19}:")"
+
+  (void)(a>=b >= c);
+  // expected-warning@-1 {{'>=' within '>='; did you mean 'a >= b && b >= c'?}}
+  // expected-note@-2 {{split the comparison into two}}
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:14-[[@LINE-3]]:14}:"&& b"
+
+  (void)(a >b >= c);
+  // expected-warning@-1 {{'>' within '>='; did you mean 'a > b && b >= c'?}}
+  // expected-note@-2 {{split the comparison into two}}
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:14-[[@LINE-3]]:14}:"&& b"
+
+  (void)(a >b > c);
+  // expected-warning@-1 {{'>' within '>'; did you mean 'a > b && b > c'?}}
+  // expected-note@-2 {{split the comparison into two}}
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:14-[[@LINE-3]]:14}:"&& b"
+
+  (void)(b + c <= c + a < a + b);
+  // expected-warning@-1 {{'<=' within '<'; did you mean 'b + c <= c + a && c + a < a + b'?}}
+  // expected-note@-2 {{split the comparison into two}}
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:24-[[@LINE-3]]:24}:"&& c + a"
+
+
+  (void)(a c);
+  // expected-warning@-1 {{'<' within '>'; did you mean 'a < b && b > c'?}}
+  // expected-note@-2 {{split the comparison into two}}
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:13-[[@LINE-3]]:13}:"&& b"
+
+  (void)(a != (b > c));
+  (void)(a == (b > c));
+  (void)((a>b) >= c);
+  (void)((a c);
+  (void)(a != b && a > c);
+  (void)((a c);
+}
Index: clang/test/Misc/warning-wall.c
===
--- clang/test/Misc/warning-wall.c
+++ clang/test/Misc/warning-wall.c
@@ -92,6 +92,7 @@
 CHECK-NEXT:-Wlogical-not-parentheses
 CHECK-NEXT:-Wbitwise-conditional-parentheses
 CHECK-NEXT:-Wbitwise-op-parentheses
+CHECK-NEXT:-Wcomparison-op-parentheses
 CHECK-NEXT:-Wshift-op-parentheses
 CHECK-NEXT:-Woverloaded-shift-op-parentheses
 CHECK-NEXT:-Wparentheses-equality
Index: clang/lib/Sema/SemaExpr.cpp
===
--- clang/lib/Sema/SemaExpr.cpp
+++ clang/lib/Sema/SemaExpr.cpp

[PATCH] D142800: [Clang][Diagnostic] Add `-Wcomparison-op-parentheses` to warn on chained comparisons

2023-02-04 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

> Also, why are these diagnostics off by default? Do we have some idea as to 
> the false positive rate?

As for the false positive rate, I have checked for instances of this warning in 
the codebases for 'oneapi-src/oneTBB', 'rui314/mold', and 'microsoft/lightgbm', 
but did not find any new cases.
I also ran a test on  'tensorflow/tensorflow' using gcc '-Wparentheses' and 
found six lines of code that trigger the new diagnostic. They all relate to 
checking whether `x` and `y` have the same sign using `x > 0 == y > 0` and 
alike. I tried to build with tensorflow using clang, but I stumbled upon some 
errors (for my poor knowledge of bazel configuration), so here I am using gcc.

I set the diagnostic disabled by default for compatibility with gcc. 
Considering the test against tensorflow above, it would be too noisy if we 
turned on the suggest-parentheses diagnostic by default (From my best guess, it 
would generate at least 18 new instances of warning on the tensorflow build for 
the six lines).
However, in my opinion, it is reasonable enough to have the diagnostic on 
chained relational operators enabled by default. The following is an excerpt 
from the 'Existing Code in C++' section of the proposal document of the 
introduction of chaining comparison for C++17.

> Overall, what we found was:
>
> - Zero instances of chained arithmetic comparisons that are correct today. 
> That is, intentionally using the current standard behavior.
> - Four instances of currently-erroneous arithmetic chaining, of the assert(0 
> <= ratio <= 1.0); variety. These are bugs that compile today but don’t do 
> what the programmer intended, but with this proposal would change in meaning 
> to become correct.
> - Many instances of using successive comparison operators in DSLs that 
> overloaded these operators to give meaning unrelated to comparisons.

Note that the 'chaining comparisons' in the document are

> - all `==`, such as `a == b == c == d`;
> - all `{<, <=}`, such as `a < b <= c < d`; and
> - all `{>, >=}` (e.g., `a >= b > c > d`).

URL: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0893r0.html

Although this paper is five years old now, I think we can conclude chaining 
relational operators are bug-prone and should be diagnosed by default.
Also, reading the document above, I think it would be reasonable to suggest 
adding '&&' in `a == b == c` case, too.

tensorflow/tensorflow newly-warned lines:

- 
https://github.com/tensorflow/tensorflow/blob/12eef948f12e47ab9ce736d8967c72fc4436db79/tensorflow/compiler/xla/overflow_util.h#L67
- 
https://github.com/tensorflow/tensorflow/blob/78034c6ee8f20932d7c43498326ea190966c5a19/tensorflow/core/kernels/cwise_ops.h#L366
- 
https://github.com/tensorflow/tensorflow/blob/78034c6ee8f20932d7c43498326ea190966c5a19/tensorflow/core/kernels/cwise_ops.h#L436
- 
https://github.com/tensorflow/tensorflow/blob/78034c6ee8f20932d7c43498326ea190966c5a19/tensorflow/core/kernels/cwise_ops.h#L457
- 
https://github.com/tensorflow/tensorflow/blob/9edf0d9eb621f18217c2a0cc5d9b67f53b40901e/tensorflow/compiler/xla/service/while_loop_analysis.cc#L364
- 
https://github.com/tensorflow/tensorflow/blob/9edf0d9eb621f18217c2a0cc5d9b67f53b40901e/tensorflow/compiler/xla/service/while_loop_analysis.cc#L377


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D142800/new/

https://reviews.llvm.org/D142800

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D140860: [Diagnostics][NFC] Fix -Wlogical-op-parentheses warning inconsistency for const and constexpr values

2023-01-12 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

In D140860#4047534 , @aaron.ballman 
wrote:

> In D140860#4045224 , @dblaikie 
> wrote:
>
>> In D140860#4044937 , 
>> @aaron.ballman wrote:
>>
>>> In D140860#4031872 , @dblaikie 
>>> wrote:
>>>
 The risk now is that this might significantly regress/add new findings for 
 this warning that may not be sufficiently bug-finding to be worth 
 immediate cleanup, causing users to have to choose between extensive 
 lower-value cleanup and disabling the warning entirely.

 Have you/could you run this over a significant codebase to see what sort 
 of new findings the modified warning finds, to see if they're high quality 
 bug finding, or mostly noise/check for whether this starts to detect 
 certain idioms we want to handle differently?

 It might be hard to find a candidate codebase that isn't already 
 warning-clean with GCC (at least Clang/LLVM wouldn't be a good candidate 
 because of this) & maybe that's sufficient justification to not worry too 
 much about this outcome...

 @aaron.ballman curious what your take on this might be
>>>
>>> Thank you for the ping (and the patience waiting on my response)!
>>>
>>> I think there's a design here that could make sense to me.
>>>
>>> Issuing the diagnostic when there is a literal is silly because the literal 
>>> value is never going to change. However, with a constant expression, the 
>>> value could change depending on configuration. This begs the question of: 
>>> what do we do with literals that are expanded from a macro? It looks like 
>>> we elide the diagnostic in that case, but macros also imply potential 
>>> configurability. So I think the design that would make sense to me is to 
>>> treat macro expansions and constant expressions the same way (diagnose) and 
>>> only elide the diagnostic when there's a (possibly string) literal. WDYT?
>>
>> Yeah, I'm OK with that - though I also wouldn't feel strongly about ensuring 
>> we warn on the macro case too - if the incremental improvement to do 
>> constexpr values is enough for now and a note is left to let someone know 
>> they could expand it to handle macros.
>>
>> But equally it's probably not super difficult to check if the literal is 
>> from a macro source location that differs from the source location of either 
>> of the operators, I guess? (I guess that check would be needed, so it 
>> doesn't warn when the macro is literally 'x && y || true' or the like.
>
> I mostly don't want to insist on dealing with macros in this patch, but it 
> does leave the diagnostic behavior somewhat inconsistent to my mind. I think 
> I can live without the macro functionality though, as this is still forward 
> progress. And yes, you'd need to check the macro location against the 
> operator location, I believe. Testing for a macro expansion is done with 
> `SourceLocation::isMacroID()`, in case @hazohelet wants to try to implement 
> that functionality as well.

I ran the diagnostic over `microsoft/lightgbm`, `oneapi-src/oneTBB`, and 
`rui314/mold` builds. As a result, I found no new warnings from this patch.

To my surprise, both unpatched/patched clang does not issue the 
`-Wlogical-op-parentheses` warning for the following code I mentioned in the 
previous comment.

> https://github.com/oneapi-src/oneTBB/blob/e6e493f96ec8b7e2e2b4d048ed49356eb54ec2a0/src/tbbmalloc/frontend.cpp#L1266

It is because clang does not issue warnings on `x || y && z` and `x && y || z` 
in the result of macro expansions as of now.
I found an issue on GitHub:
https://github.com/llvm/llvm-project/issues/19345

> And yes, you'd need to check the macro location against the operator 
> location, I believe. Testing for a macro expansion is done with 
> `SourceLocation::isMacroID()`, in case @hazohelet wants to try to implement 
> that functionality as well.

Thanks for your help. I think testing macro location against the operator is 
already handled in `DiagnoseBinOpPrecedence`, and is somewhat relevant to the 
issue above.

Anyway, I confirm no new instances of parentheses warning in the three 
repositories above.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D140860/new/

https://reviews.llvm.org/D140860

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D140860: [Diagnostics][NFC] Fix -Wlogical-op-parentheses warning inconsistency for const and constexpr values

2023-01-12 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet updated this revision to Diff 488863.
hazohelet added a comment.

I added the release note.

> Also, do you need someone to commit on your behalf? If so, what name and 
> email address would you like used for patch attribution?

I don't have commit access. Would you please land this patch for me? Please use 
"Takuya Shimizu " for the patch attribution.
Thank you.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D140860/new/

https://reviews.llvm.org/D140860

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/Sema/SemaExpr.cpp
  clang/test/Sema/logical-op-parentheses.c

Index: clang/test/Sema/logical-op-parentheses.c
===
--- clang/test/Sema/logical-op-parentheses.c
+++ clang/test/Sema/logical-op-parentheses.c
@@ -8,6 +8,7 @@
 #endif
 
 void logical_op_parentheses(unsigned i) {
+	const unsigned t = 1;
   (void)(i ||
  i && i);
 #ifndef SILENCE
@@ -17,8 +18,55 @@
   // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
   // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:20-[[@LINE-6]]:20}:")"
 
-  (void)(i || i && "w00t");
+  (void)(t ||
+ t && t);
+#ifndef SILENCE
+  // expected-warning@-2 {{'&&' within '||'}}
+  // expected-note@-3 {{place parentheses around the '&&' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:20-[[@LINE-6]]:20}:")"
+
+  (void)(t && 
+ t || t);
+#ifndef SILENCE
+  // expected-warning@-3 {{'&&' within '||'}}
+  // expected-note@-4 {{place parentheses around the '&&' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:10-[[@LINE-6]]:10}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:15-[[@LINE-6]]:15}:")"
+	
+	(void)(i || i && "w00t");
   (void)("w00t" && i || i);
+  (void)("w00t" && t || t);
+  (void)(t && t || 0);
+#ifndef SILENCE
+  // expected-warning@-2 {{'&&' within '||'}}
+  // expected-note@-3 {{place parentheses around the '&&' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:10-[[@LINE-5]]:10}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:16-[[@LINE-6]]:16}:")"
+  (void)(1 && t || t);
+#ifndef SILENCE
+  // expected-warning@-2 {{'&&' within '||'}}
+  // expected-note@-3 {{place parentheses around the '&&' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:10-[[@LINE-5]]:10}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:16-[[@LINE-6]]:16}:")"
+  (void)(0 || t && t);
+#ifndef SILENCE
+  // expected-warning@-2 {{'&&' within '||'}}
+  // expected-note@-3 {{place parentheses around the '&&' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:21-[[@LINE-6]]:21}:")"
+  (void)(t || t && 1);
+#ifndef SILENCE
+  // expected-warning@-2 {{'&&' within '||'}}
+  // expected-note@-3 {{place parentheses around the '&&' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:21-[[@LINE-6]]:21}:")"
 
   (void)(i || i && "w00t" || i);
 #ifndef SILENCE
@@ -37,5 +85,17 @@
   // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:26-[[@LINE-6]]:26}:")"
 
   (void)(i && i || 0);
+#ifndef SILENCE
+  // expected-warning@-2 {{'&&' within '||'}}
+  // expected-note@-3 {{place parentheses around the '&&' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:10-[[@LINE-5]]:10}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:16-[[@LINE-6]]:16}:")"
   (void)(0 || i && i);
+#ifndef SILENCE
+  // expected-warning@-2 {{'&&' within '||'}}
+  // expected-note@-3 {{place parentheses around the '&&' expression to silence this warning}}
+#endif
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:21-[[@LINE-6]]:21}:")"
 }
Index: clang/lib/Sema/SemaExpr.cpp
===
--- clang/lib/Sema/SemaExpr.cpp
+++ clang/lib/Sema/SemaExpr.cpp
@@ -15434,38 +15434,21 @@
 Bop->getSourceRange());
 }
 
-/// Returns true if the given expression can be evaluated as a constant
-/// 'true'.
-static bool EvaluatesAsTrue(Sema &S, Expr *E) {
-  bool Res;
-  return !E->isValueDependent() &&
- E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && Res;
-}
-
-/// Returns true if the given expression can be evaluated as a constant
-/// 'false'.
-static bool EvaluatesAsFalse(Sema &S, Expr *E) {
-  bool Res;
-  return !E->isValueDependent() &&
- E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && !Res;
-}
-
 /// Look for '&&' in the left hand of a '||' expr.
 static void DiagnoseLogicalAndInLogicalOrLHS(Sema &S, SourceLocation OpLoc,
  Expr *LHSExpr, Expr *RHSExpr) {
   if (BinaryOperator *Bop = dyn_cast(LHSExpr)) {
 if (Bop->getOpcode() ==

[PATCH] D155064: [clang][SemaCXX] Diagnose tautological uses of consteval if and is_constant_evaluated

2023-09-26 Thread Takuya Shimizu via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG491b2810fb7f: [clang][SemaCXX] Diagnose tautological uses of 
consteval if and… (authored by hazohelet).

Changed prior to commit:
  https://reviews.llvm.org/D155064?vs=556946&id=557380#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155064/new/

https://reviews.llvm.org/D155064

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Basic/DiagnosticASTKinds.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/include/clang/Parse/Parser.h
  clang/include/clang/Sema/Sema.h
  clang/lib/AST/ExprConstant.cpp
  clang/lib/Parse/ParseDecl.cpp
  clang/lib/Parse/ParseDeclCXX.cpp
  clang/lib/Parse/ParseExpr.cpp
  clang/lib/Parse/ParseExprCXX.cpp
  clang/lib/Parse/ParseStmt.cpp
  clang/lib/Sema/SemaChecking.cpp
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/lib/Sema/SemaLambda.cpp
  clang/lib/Sema/SemaStmt.cpp
  clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
  clang/test/AST/Interp/builtins.cpp
  clang/test/AST/Interp/if.cpp
  clang/test/AST/Interp/literals.cpp
  clang/test/CXX/expr/expr.const/p2-0x.cpp
  clang/test/CXX/expr/expr.const/p6-2a.cpp
  clang/test/CXX/expr/expr.prim/expr.prim.lambda/p3.cpp
  clang/test/CXX/stmt.stmt/stmt.select/stmt.if/p4.cpp
  clang/test/Parser/pragma-fenv_access.c
  clang/test/SemaCXX/constant-conversion.cpp
  clang/test/SemaCXX/constant-expression-cxx11.cpp
  clang/test/SemaCXX/cxx2a-consteval.cpp
  clang/test/SemaCXX/cxx2b-consteval-if.cpp
  clang/test/SemaCXX/cxx2b-consteval-propagate.cpp
  clang/test/SemaCXX/fixit-tautological-meta-constant.cpp
  clang/test/SemaCXX/vartemplate-lambda.cpp
  clang/test/SemaCXX/warn-constant-evaluated-constexpr.cpp
  clang/test/SemaCXX/warn-tautological-meta-constant.cpp
  clang/test/SemaTemplate/concepts.cpp
  clang/unittests/Support/TimeProfilerTest.cpp
  libcxx/include/__type_traits/is_constant_evaluated.h
  
libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
  
libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp

Index: libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp
===
--- libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp
+++ libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp
@@ -24,7 +24,7 @@
 #else
   // expected-error-re@+1 (static_assert|static assertion)}} failed}}
   static_assert(!std::is_constant_evaluated(), "");
-  // expected-warning@-1 0-1 {{'std::is_constant_evaluated' will always evaluate to 'true' in a manifestly constant-evaluated expression}}
+  // expected-warning-re@-1 0-1 {{'std::is_constant_evaluated' will always evaluate to {{('true' in a manifestly constant-evaluated expression|true in this context)
 #endif
   return 0;
 }
Index: libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
===
--- libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
+++ libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
@@ -93,12 +93,10 @@
 }
 
 int main(int, char**) {
-  if (!std::is_constant_evaluated()) {
-test_containers, std::deque>();
-test_containers, std::vector>();
-test_containers, std::deque>();
-test_containers, std::vector>();
-  }
+  test_containers, std::deque>();
+  test_containers, std::vector>();
+  test_containers, std::deque>();
+  test_containers, std::vector>();
 
   types::for_each(types::forward_iterator_list{}, [] {
 test_join_view();
Index: libcxx/include/__type_traits/is_constant_evaluated.h
===
--- libcxx/include/__type_traits/is_constant_evaluated.h
+++ libcxx/include/__type_traits/is_constant_evaluated.h
@@ -24,7 +24,14 @@
 #endif
 
 _LIBCPP_HIDE_FROM_ABI inline _LIBCPP_CONSTEXPR bool __libcpp_is_constant_evaluated() _NOEXCEPT {
+// __builtin_is_constant_evaluated() in this function always evaluates to false in pre-C++11 mode
+// because this function is not constexpr-qualified.
+// The following macro use clarifies this and avoids warnings from compilers.
+#ifndef _LIBCPP_CXX03_LANG
   return __builtin_is_constant_evaluated();
+#else
+  return false;
+#endif
 }
 
 _LIBCPP_END_NAMESPACE_STD
Index: clang/unittests/Support/TimeProfilerTest.cpp
===
--- clang/unittests/Support/TimeProfilerTest.cpp
+++ clang/unittests/Support/TimeProfilerTest.cpp
@@ -188,7 +188,6 @@
 | EvaluateAsBooleanCondition ()
 | | EvaluateAsRValue ()
 | EvaluateAsInitializer (slow_value)
-| EvaluateAsConstantExpr ()
 | EvaluateAsConstantEx

[PATCH] D155064: [clang][SemaCXX] Diagnose tautological uses of consteval if and is_constant_evaluated

2023-09-27 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

In D155064#4651306 , @hans wrote:

> We saw a new warning in Chromium after this, and I wasn't sure if that's 
> intentional:
>
>   #include 
>   
>   template  float foo(T input) {
> if (sizeof(T) > 2) {
>   return 42;
> } else {
>   constexpr float inverseMax = 1.0f / std::numeric_limits::max();
>   return input * inverseMax;
> }
>   }
>   
>   float f() {
> return foo(1);
>   }
>   $ build/bin/clang -c /tmp/a.cc
>   /tmp/a.cc:7:41: warning: implicit conversion from 'int' to 'float' changes 
> value from 2147483647 to 2147483648 [-Wimplicit-const-int-float-conversion]
>   7 | constexpr float inverseMax = 1.0f / 
> std::numeric_limits::max();
> |   ~ 
> ^
>   /tmp/a.cc:13:10: note: in instantiation of function template specialization 
> 'foo' requested here
>  13 |   return foo(1);
> |  ^
>   1 warning generated.
>
> Note that the branch with the conversion can be determined to not be taken 
> based on the template argument.
>
> Using `if constexpr` suppresses the warning, but I'm not sure if that should 
> really be necessary?
>
> (Our tracking bug is https://crbug.com/1487142)

It's unintended, but the cause is that I started using `Sema::Diag` instead of 
`Sema::DiagRuntimeBehavior`, in narrowing check for constexpr variable 
initializers, and thus lost the reachability analysis. It will be a simple fix, 
but would involve some adaptations to `Sema::DiagIfReachable`. I'll fix it 
after other concerns are resolved.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155064/new/

https://reviews.llvm.org/D155064

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155064: [clang][SemaCXX] Diagnose tautological uses of consteval if and is_constant_evaluated

2023-09-27 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

Thanks for the report about the errors in lambdas. The culprit is 
`clang/lib/Sema/SemaTemplateInstantiateDecl.cpp`. I am pushing 
constant-evaluated context if the instantiated variable is constexpr variable, 
and somehow it causes those errors. I'll take deeper look into them tomorrow.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155064/new/

https://reviews.llvm.org/D155064

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155064: [clang][SemaCXX] Diagnose tautological uses of consteval if and is_constant_evaluated

2023-09-28 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

Thank you for the revert!

This looks like a general issue of clang on template deduction in 
constant-evaluated context (https://godbolt.org/z/zTro7Ycfa). Pushing 
constant-evaluated context against initializer of instantiated constexpr 
variables made this bug appear in broader scenarios.
I can circumvent this problem by not pushing constant-evaluated context on 
instantiated constexpr variable initializers, but that in turn would force me 
to do tricky workaround in warnings on narrowing conversions in global 
constexpr variable templates.

@cor3ntin
The constant-evaluated-context miscompilation bisected to 
https://reviews.llvm.org/D140554
Is this the intended result of that patch?
I'm at lost how to proceed further on this patch. The only way looks like some 
workarounds in narrowing conversion warning mechanism, but is it acceptable?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155064/new/

https://reviews.llvm.org/D155064

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155610: [Clang][Sema] Fix display of characters on static assertion failure

2023-09-30 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet abandoned this revision.
hazohelet added a comment.

Thank you so much to everyone for guiding this patch onto the right track! I'll 
submit GitHub PR after making suggested changes.
Since Phabricator is going to be shutdown, I mark this differential as 
abandoned.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155610/new/

https://reviews.llvm.org/D155610

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155610: [Clang][Sema] Fix display of characters on static assertion failure

2023-10-01 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

In D155610#4652198 , @cor3ntin wrote:

> @hazohelet Please keep this patch on Phab. It's not going to be shutdown. The 
> current consensus is that we will reconsider shutting down phab on November 15
> https://discourse.llvm.org/t/update-on-github-pull-requests/71540/125

Oh I failed to notice that post, thanks for letting me know.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155610/new/

https://reviews.llvm.org/D155610

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155610: [Clang][Sema] Fix display of characters on static assertion failure

2023-10-01 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet updated this revision to Diff 557524.
hazohelet added a comment.

Address comments from Corentin


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155610/new/

https://reviews.llvm.org/D155610

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Basic/Diagnostic.h
  clang/lib/Basic/Diagnostic.cpp
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/test/Lexer/cxx1z-trigraphs.cpp
  clang/test/SemaCXX/static-assert-cxx26.cpp
  clang/test/SemaCXX/static-assert.cpp

Index: clang/test/SemaCXX/static-assert.cpp
===
--- clang/test/SemaCXX/static-assert.cpp
+++ clang/test/SemaCXX/static-assert.cpp
@@ -268,7 +268,31 @@
 return 'c';
   }
   static_assert(getChar() == 'a', ""); // expected-error {{failed}} \
-   // expected-note {{evaluates to ''c' == 'a''}}
+   // expected-note {{evaluates to ''c' (0x63, 99) == 'a' (0x61, 97)'}}
+  static_assert((char)9 == '\x61', ""); // expected-error {{failed}} \
+// expected-note {{evaluates to ''\t' (0x09, 9) == 'a' (0x61, 97)'}}
+  static_assert((char)10 == '\0', ""); // expected-error {{failed}} \
+   // expected-note {{n' (0x0A, 10) == '' (0x00, 0)'}}
+  // The note above is intended to match "evaluates to '\n' (0x0A, 10) == '' (0x00, 0)'", but if we write it as it is,
+  // the "\n" cannot be consumed by the diagnostic consumer.
+  static_assert((signed char)10 == (char)-123, ""); // expected-error {{failed}} \
+// expected-note {{evaluates to '10 == '<85>' (0x85, -123)'}}
+  static_assert((char)-4 == (unsigned char)-8, ""); // expected-error {{failed}} \
+// expected-note {{evaluates to ''' (0xFC, -4) == 248'}}
+  static_assert((char)-128 == (char)-123, ""); // expected-error {{failed}} \
+   // expected-note {{evaluates to ''<80>' (0x80, -128) == '<85>' (0x85, -123)'}}
+  static_assert('\xA0' == (char)'\x20', ""); // expected-error {{failed}} \
+ // expected-note {{evaluates to ''' (0xA0, -96) == ' ' (0x20, 32)'}}
+  static_assert((char16_t)L'ゆ' == L"C̵̭̯̠̎͌ͅť̺"[1], ""); // expected-error {{failed}} \
+  // expected-note {{evaluates to 'u'ゆ' (0x3086, 12422) == L'̵' (0x335, 821)'}}
+  static_assert(L"\/"[1] == u'\xFFFD', ""); // expected-error {{failed}} \
+  // expected-note {{evaluates to 'L'/' (0xFF0F, 65295) == u'�' (0xFFFD, 65533)'}}
+  static_assert(L"⚾"[0] == U'🌍', ""); // expected-error {{failed}} \
+ // expected-note {{evaluates to 'L'⚾' (0x26BE, 9918) == U'🌍' (0x1F30D, 127757)'}}
+  static_assert(U"\a"[0] == (wchar_t)9, ""); // expected-error {{failed}} \
+ // expected-note {{evaluates to 'U'\a' (0x07, 7) == L'\t' (0x09, 9)'}}
+  static_assert(L"§"[0] == U'Ö', ""); // expected-error {{failed}} \
+  // expected-note {{evaluates to 'L'§' (0xA7, 167) == U'Ö' (0xD6, 214)'}}
 
   /// Bools are printed as bools.
   constexpr bool invert(bool b) {
Index: clang/test/SemaCXX/static-assert-cxx26.cpp
===
--- clang/test/SemaCXX/static-assert-cxx26.cpp
+++ clang/test/SemaCXX/static-assert-cxx26.cpp
@@ -298,3 +298,12 @@
 Bad b; // expected-note {{in instantiation}}
 
 }
+
+namespace EscapeInDiagnostic {
+static_assert('\u{9}' == (char)1, ""); // expected-error {{failed}} \
+   // expected-note {{evaluates to ''\t' (0x09, 9) == '' (0x01, 1)'}}
+static_assert((char8_t)-128 == (char8_t)-123, ""); // expected-error {{failed}} \
+   // expected-note {{evaluates to 'u8'<80>' (0x80, 128) == u8'<85>' (0x85, 133)'}}
+static_assert((char16_t)0xFEFF == (char16_t)0xDB93, ""); // expected-error {{failed}} \
+ // expected-note {{evaluates to 'u'' (0xFEFF, 65279) == u'\xDB93' (0xDB93, 56211)'}}
+}
Index: clang/test/Lexer/cxx1z-trigraphs.cpp
===
--- clang/test/Lexer/cxx1z-trigraphs.cpp
+++ clang/test/Lexer/cxx1z-trigraphs.cpp
@@ -21,7 +21,7 @@
 
 #if !ENABLED_TRIGRAPHS
 // expected-error@11 {{}} expected-warning@11 {{trigraph ignored}}
-// expected-error@13 {{failed}} expected-warning@13 {{trigraph ignored}} expected-note@13 {{evaluates to ''?' == '#''}}
+// expected-error@13 {{failed}} expected-warning@13 {{trigraph ignored}} expected-note@13 {{evaluates to ''?' (0x3F, 63) == '#' (0x23, 35)'}}
 // expected-error@16 {{}}
 // expected-error@20 {{}}
 #else
Index: clang/lib/S

[PATCH] D155610: [Clang][Sema] Fix display of characters on static assertion failure

2023-10-03 Thread Takuya Shimizu via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG2176c5e510e3: [Clang][Sema] Fix display of characters on 
static assertion failure (authored by hazohelet).

Changed prior to commit:
  https://reviews.llvm.org/D155610?vs=557524&id=557581#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155610/new/

https://reviews.llvm.org/D155610

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Basic/Diagnostic.h
  clang/lib/Basic/Diagnostic.cpp
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/test/Lexer/cxx1z-trigraphs.cpp
  clang/test/SemaCXX/static-assert-cxx26.cpp
  clang/test/SemaCXX/static-assert.cpp

Index: clang/test/SemaCXX/static-assert.cpp
===
--- clang/test/SemaCXX/static-assert.cpp
+++ clang/test/SemaCXX/static-assert.cpp
@@ -268,7 +268,31 @@
 return 'c';
   }
   static_assert(getChar() == 'a', ""); // expected-error {{failed}} \
-   // expected-note {{evaluates to ''c' == 'a''}}
+   // expected-note {{evaluates to ''c' (0x63, 99) == 'a' (0x61, 97)'}}
+  static_assert((char)9 == '\x61', ""); // expected-error {{failed}} \
+// expected-note {{evaluates to ''\t' (0x09, 9) == 'a' (0x61, 97)'}}
+  static_assert((char)10 == '\0', ""); // expected-error {{failed}} \
+   // expected-note {{n' (0x0A, 10) == '' (0x00, 0)'}}
+  // The note above is intended to match "evaluates to '\n' (0x0A, 10) == '' (0x00, 0)'", but if we write it as it is,
+  // the "\n" cannot be consumed by the diagnostic consumer.
+  static_assert((signed char)10 == (char)-123, ""); // expected-error {{failed}} \
+// expected-note {{evaluates to '10 == '<85>' (0x85, -123)'}}
+  static_assert((char)-4 == (unsigned char)-8, ""); // expected-error {{failed}} \
+// expected-note {{evaluates to ''' (0xFC, -4) == 248'}}
+  static_assert((char)-128 == (char)-123, ""); // expected-error {{failed}} \
+   // expected-note {{evaluates to ''<80>' (0x80, -128) == '<85>' (0x85, -123)'}}
+  static_assert('\xA0' == (char)'\x20', ""); // expected-error {{failed}} \
+ // expected-note {{evaluates to ''' (0xA0, -96) == ' ' (0x20, 32)'}}
+  static_assert((char16_t)L'ゆ' == L"C̵̭̯̠̎͌ͅť̺"[1], ""); // expected-error {{failed}} \
+  // expected-note {{evaluates to 'u'ゆ' (0x3086, 12422) == L'̵' (0x335, 821)'}}
+  static_assert(L"\/"[1] == u'\xFFFD', ""); // expected-error {{failed}} \
+  // expected-note {{evaluates to 'L'/' (0xFF0F, 65295) == u'�' (0xFFFD, 65533)'}}
+  static_assert(L"⚾"[0] == U'🌍', ""); // expected-error {{failed}} \
+ // expected-note {{evaluates to 'L'⚾' (0x26BE, 9918) == U'🌍' (0x1F30D, 127757)'}}
+  static_assert(U"\a"[0] == (wchar_t)9, ""); // expected-error {{failed}} \
+ // expected-note {{evaluates to 'U'\a' (0x07, 7) == L'\t' (0x09, 9)'}}
+  static_assert(L"§"[0] == U'Ö', ""); // expected-error {{failed}} \
+  // expected-note {{evaluates to 'L'§' (0xA7, 167) == U'Ö' (0xD6, 214)'}}
 
   /// Bools are printed as bools.
   constexpr bool invert(bool b) {
Index: clang/test/SemaCXX/static-assert-cxx26.cpp
===
--- clang/test/SemaCXX/static-assert-cxx26.cpp
+++ clang/test/SemaCXX/static-assert-cxx26.cpp
@@ -298,3 +298,12 @@
 Bad b; // expected-note {{in instantiation}}
 
 }
+
+namespace EscapeInDiagnostic {
+static_assert('\u{9}' == (char)1, ""); // expected-error {{failed}} \
+   // expected-note {{evaluates to ''\t' (0x09, 9) == '' (0x01, 1)'}}
+static_assert((char8_t)-128 == (char8_t)-123, ""); // expected-error {{failed}} \
+   // expected-note {{evaluates to 'u8'<80>' (0x80, 128) == u8'<85>' (0x85, 133)'}}
+static_assert((char16_t)0xFEFF == (char16_t)0xDB93, ""); // expected-error {{failed}} \
+ // expected-note {{evaluates to 'u'' (0xFEFF, 65279) == u'\xDB93' (0xDB93, 56211)'}}
+}
Index: clang/test/Lexer/cxx1z-trigraphs.cpp
===
--- clang/test/Lexer/cxx1z-trigraphs.cpp
+++ clang/test/Lexer/cxx1z-trigraphs.cpp
@@ -21,7 +21,7 @@
 
 #if !ENABLED_TRIGRAPHS
 // expected-error@11 {{}} expected-warning@11 {{trigraph ignored}}
-// expected-error@13 {{failed}} expected-warning@13 {{trigraph ignored}} expected-note@13 {{evaluates to ''?' == '#''}}
+// e

[PATCH] D155064: [clang][SemaCXX] Diagnose tautological uses of consteval if and is_constant_evaluated

2023-07-18 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet updated this revision to Diff 541528.
hazohelet marked 8 inline comments as done.
hazohelet edited the summary of this revision.
hazohelet added a comment.

Address review comments

- Rename tablegen name of the diagnostics to follow other tautological warnings
- Remove redundant modification of context kind to `PotentiallyEvaluated`
- Use regex for libcxx test part (tentative)


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155064/new/

https://reviews.llvm.org/D155064

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Basic/DiagnosticASTKinds.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/include/clang/Sema/Sema.h
  clang/lib/AST/ExprConstant.cpp
  clang/lib/Parse/ParseDecl.cpp
  clang/lib/Parse/ParseDeclCXX.cpp
  clang/lib/Parse/ParseStmt.cpp
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/lib/Sema/SemaLambda.cpp
  clang/lib/Sema/SemaStmt.cpp
  clang/test/AST/Interp/builtins.cpp
  clang/test/AST/Interp/if.cpp
  clang/test/AST/Interp/literals.cpp
  clang/test/CXX/expr/expr.const/p2-0x.cpp
  clang/test/CXX/expr/expr.const/p6-2a.cpp
  clang/test/CXX/expr/expr.prim/expr.prim.lambda/p3.cpp
  clang/test/CXX/stmt.stmt/stmt.select/stmt.if/p4.cpp
  clang/test/Parser/pragma-fenv_access.c
  clang/test/SemaCXX/constant-expression-cxx11.cpp
  clang/test/SemaCXX/cxx2a-consteval.cpp
  clang/test/SemaCXX/cxx2b-consteval-if.cpp
  clang/test/SemaCXX/ext-int.cpp
  clang/test/SemaCXX/vartemplate-lambda.cpp
  clang/test/SemaCXX/warn-constant-evaluated-constexpr.cpp
  clang/test/SemaCXX/warn-tautological-meta-constant.cpp
  clang/test/SemaTemplate/concepts.cpp
  clang/unittests/Support/TimeProfilerTest.cpp
  
libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
  
libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp

Index: libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp
===
--- libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp
+++ libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp
@@ -24,7 +24,7 @@
 #else
   // expected-error-re@+1 (static_assert|static assertion)}} failed}}
   static_assert(!std::is_constant_evaluated(), "");
-  // expected-warning@-1 0-1 {{'std::is_constant_evaluated' will always evaluate to 'true' in a manifestly constant-evaluated expression}}
+  // expected-warning-re@-1 0-1 {{'std::is_constant_evaluated' will always evaluate to {{('true' in a manifestly constant-evaluated expression|true in this context)
 #endif
   return 0;
 }
Index: libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
===
--- libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
+++ libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
@@ -93,12 +93,10 @@
 }
 
 int main(int, char**) {
-  if (!std::is_constant_evaluated()) {
-test_containers, std::deque>();
-test_containers, std::vector>();
-test_containers, std::deque>();
-test_containers, std::vector>();
-  }
+  test_containers, std::deque>();
+  test_containers, std::vector>();
+  test_containers, std::deque>();
+  test_containers, std::vector>();
 
   types::for_each(types::forward_iterator_list{}, [] {
 test_join_view();
Index: clang/unittests/Support/TimeProfilerTest.cpp
===
--- clang/unittests/Support/TimeProfilerTest.cpp
+++ clang/unittests/Support/TimeProfilerTest.cpp
@@ -188,7 +188,6 @@
 | EvaluateAsBooleanCondition ()
 | | EvaluateAsRValue ()
 | EvaluateAsInitializer (slow_value)
-| EvaluateAsConstantExpr ()
 | EvaluateAsConstantExpr ()
 | EvaluateAsRValue ()
 | EvaluateAsInitializer (slow_init_list)
Index: clang/test/SemaTemplate/concepts.cpp
===
--- clang/test/SemaTemplate/concepts.cpp
+++ clang/test/SemaTemplate/concepts.cpp
@@ -135,21 +135,21 @@
 
 namespace BuiltinIsConstantEvaluated {
   // Check that we do all satisfaction and diagnostic checks in a constant context.
-  template concept C = __builtin_is_constant_evaluated(); // expected-warning {{always}}
+  template concept C = __builtin_is_constant_evaluated(); // expected-warning {{always evaluate to true}}
   static_assert(C);
 
-  template concept D = __builtin_is_constant_evaluated() == true; // expected-warning {{always}}
+  template concept D = __builtin_is_constant_evaluated() == true; // expected-warning {{always evaluate to true}}
   static_assert(D);
 
-  template concept E = __builtin_is_constant_evaluated() == true && // expected-warning {{always}}
+  template concept E = __builtin_is_constant_evaluated() == true && // expected-warning {{always evaluate to true}}

[PATCH] D155610: [Clang][ExprConstant] Print integer instead of character on static assertion failure

2023-07-18 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet created this revision.
hazohelet added reviewers: aaron.ballman, tbaeder, cjdb.
Herald added a project: All.
hazohelet requested review of this revision.
Herald added a project: clang.

BEFORE this patch, the values printed in `static_assert` were printed after 
ignoring the implicit type casts on them. As a result, clang is printing 
character type values that are visually hard to recognize such as 
`static_assert((char)9 == 0)` (Live demo: https://godbolt.org/z/6WKP3jjoE)

This patch makes clang print integers instead of the character representantions 
in these cases so as not to output nonprintable characters. (In the case above, 
`9 == 0`)
When the user-provided expression is of bool type, this patch does not print 
them as `0` and `1` because `false` and `true` would be easier to understand.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D155610

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/test/Lexer/cxx1z-trigraphs.cpp
  clang/test/SemaCXX/static-assert.cpp


Index: clang/test/SemaCXX/static-assert.cpp
===
--- clang/test/SemaCXX/static-assert.cpp
+++ clang/test/SemaCXX/static-assert.cpp
@@ -262,7 +262,15 @@
 return 'c';
   }
   static_assert(getChar() == 'a', ""); // expected-error {{failed}} \
-   // expected-note {{evaluates to ''c' == 
'a''}}
+   // expected-note {{evaluates to '99 == 
97'}}
+  static_assert((char)9 == 'a', ""); // expected-error {{failed}} \
+ // expected-note {{evaluates to '9 == 
97'}}
+  static_assert((unsigned char)10 == 'a', ""); // expected-error {{failed}} \
+   // expected-note {{evaluates to 
'10 == 97'}}
+  static_assert((char)-123 == 'a', ""); // expected-error {{failed}} \
+// expected-note {{evaluates to '-123 
== 97'}}
+  static_assert((unsigned char)-4 == 'a', ""); // expected-error {{failed}} \
+   // expected-note {{evaluates to 
'252 == 97'}}
 
   /// Bools are printed as bools.
   constexpr bool invert(bool b) {
Index: clang/test/Lexer/cxx1z-trigraphs.cpp
===
--- clang/test/Lexer/cxx1z-trigraphs.cpp
+++ clang/test/Lexer/cxx1z-trigraphs.cpp
@@ -21,7 +21,7 @@
 
 #if !ENABLED_TRIGRAPHS
 // expected-error@11 {{}} expected-warning@11 {{trigraph ignored}}
-// expected-error@13 {{failed}} expected-warning@13 {{trigraph ignored}} 
expected-note@13 {{evaluates to ''?' == '#''}}
+// expected-error@13 {{failed}} expected-warning@13 {{trigraph ignored}} 
expected-note@13 {{evaluates to '63 == 35'}}
 // expected-error@16 {{}}
 // expected-error@20 {{}}
 #else
Index: clang/lib/Sema/SemaDeclCXX.cpp
===
--- clang/lib/Sema/SemaDeclCXX.cpp
+++ clang/lib/Sema/SemaDeclCXX.cpp
@@ -16866,8 +16866,14 @@
   Expr::EvalResult Result;
   SmallString<12> ValueString;
   bool Print;
-} DiagSide[2] = {{LHS, Expr::EvalResult(), {}, false},
- {RHS, Expr::EvalResult(), {}, false}};
+} DiagSide[2] = {{LHS->getType()->isBooleanType() ? LHS : Op->getLHS(),
+  Expr::EvalResult(),
+  {},
+  false},
+ {RHS->getType()->isBooleanType() ? RHS : Op->getRHS(),
+  Expr::EvalResult(),
+  {},
+  false}};
 for (unsigned I = 0; I < 2; I++) {
   const Expr *Side = DiagSide[I].Cond;
 
Index: clang/docs/ReleaseNotes.rst
===
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -425,6 +425,9 @@
 - ``-Wformat`` will no longer suggest a no-op fix-it for fixing scoped enum 
format
   warnings. Instead, it will suggest casting the enum object to the type 
specified
   in the format string.
+- When describing the failure of static assertion, clang prints the integer 
representation
+  of the value instead of its character representation even when the 
user-provided value
+  is of character type so as not to output non-printable characters.
 
 Bug Fixes in This Version
 -


Index: clang/test/SemaCXX/static-assert.cpp
===
--- clang/test/SemaCXX/static-assert.cpp
+++ clang/test/SemaCXX/static-assert.cpp
@@ -262,7 +262,15 @@
 return 'c';
   }
   static_assert(getChar() == 'a', ""); // expected-error {{failed}} \
-   // expected-note {{evaluates to ''c' == 'a''}}
+   // expected-note {{evaluates to '99 == 97'}}
+  static_assert((char)9 == 'a', ""); // expected-error {{failed}} \
+ 

[PATCH] D155064: [clang][SemaCXX] Diagnose tautological uses of consteval if and is_constant_evaluated

2023-07-19 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

Regarding the clang CI failure, libcxx's `__libcpp_is_constant_evaluated` 
always returns false under C++03 or earlier, where constexpr isn't available.
(Code: 
https://github.com/llvm/llvm-project/blob/036a1b2202cb71aacfa72ef15145a88dc50a02cf/libcxx/include/__type_traits/is_constant_evaluated.h#L26-L28)
`_LIBCPP_CONSTEXPR` expands to nothing under C++03 mode, and thus this returns 
false every time as clang now says.
(Live demo: https://godbolt.org/z/oszebM5o5)
I'm not sure how I should fix it, so I would like to ask for help from libcxx 
folks.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155064/new/

https://reviews.llvm.org/D155064

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155610: [Clang][ExprConstant] Print integer instead of character on static assertion failure

2023-07-19 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

In D155610#4511547 , @tbaeder wrote:

> What if you switch from `IgnoreParenImpCasts` at the top of 
> `DiagnoseStaticAssertDetails()` to just `IgnoreParens()`? That improve cases 
> where we simply compare a character literal to an integer, since the literal 
> should be implicitly cast to an integer.

It results in printing everything including `char` and `bool` as integer.
I wanted to print `true` and `false` instead of `1` and `0`, so ignored the 
implicit casts when the user-provided expression is bool type.

I think it would look nicer to print the character representation as well when 
it is not non-printable


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155610/new/

https://reviews.llvm.org/D155610

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155064: [clang][SemaCXX] Diagnose tautological uses of consteval if and is_constant_evaluated

2023-07-21 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

In D155064#4514893 , @cor3ntin wrote:

> This looks good to me modulo nitpicks. 
> When you land that, please make an issue on github for the missing narrowing 
> warning, it seems important.
>
> I'll wait before approving in case @aaron.ballman spot things i missed

I've already prepared another patch to fix the narrowing issue. Should I add 
the diff to this patch or open another revision?

FWIW, I believe `const bool c = std::is_constant_evaluated();` this particular 
case should also be diagnosed because it could be a common mistake as was 
mentioned in 
https://github.com/llvm/llvm-project/issues/43760#issuecomment-981022686.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155064/new/

https://reviews.llvm.org/D155064

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155064: [clang][SemaCXX] Diagnose tautological uses of consteval if and is_constant_evaluated

2023-07-22 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet updated this revision to Diff 543189.
hazohelet marked 3 inline comments as done.
hazohelet edited the summary of this revision.
hazohelet added a comment.

Address comments from Corentin

- NFC stylistic changes
- Bring back narrowing warning on constexpr variables to avoid regression
- Newly diagnose narrowing conversions on constant-evaluated initializers in 
general (e..g immediate function context, and constexpr variable templates)


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155064/new/

https://reviews.llvm.org/D155064

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Basic/DiagnosticASTKinds.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/include/clang/Sema/Sema.h
  clang/lib/AST/ExprConstant.cpp
  clang/lib/Parse/ParseDecl.cpp
  clang/lib/Parse/ParseDeclCXX.cpp
  clang/lib/Parse/ParseStmt.cpp
  clang/lib/Sema/SemaChecking.cpp
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/lib/Sema/SemaLambda.cpp
  clang/lib/Sema/SemaStmt.cpp
  clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
  clang/test/AST/Interp/builtins.cpp
  clang/test/AST/Interp/if.cpp
  clang/test/AST/Interp/literals.cpp
  clang/test/CXX/expr/expr.const/p2-0x.cpp
  clang/test/CXX/expr/expr.const/p6-2a.cpp
  clang/test/CXX/expr/expr.prim/expr.prim.lambda/p3.cpp
  clang/test/CXX/stmt.stmt/stmt.select/stmt.if/p4.cpp
  clang/test/Parser/pragma-fenv_access.c
  clang/test/SemaCXX/constant-conversion.cpp
  clang/test/SemaCXX/constant-expression-cxx11.cpp
  clang/test/SemaCXX/cxx2a-consteval.cpp
  clang/test/SemaCXX/cxx2b-consteval-if.cpp
  clang/test/SemaCXX/vartemplate-lambda.cpp
  clang/test/SemaCXX/warn-constant-evaluated-constexpr.cpp
  clang/test/SemaCXX/warn-tautological-meta-constant.cpp
  clang/test/SemaTemplate/concepts.cpp
  clang/unittests/Support/TimeProfilerTest.cpp
  
libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
  
libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp

Index: libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp
===
--- libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp
+++ libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp
@@ -24,7 +24,7 @@
 #else
   // expected-error-re@+1 (static_assert|static assertion)}} failed}}
   static_assert(!std::is_constant_evaluated(), "");
-  // expected-warning@-1 0-1 {{'std::is_constant_evaluated' will always evaluate to 'true' in a manifestly constant-evaluated expression}}
+  // expected-warning-re@-1 0-1 {{'std::is_constant_evaluated' will always evaluate to {{('true' in a manifestly constant-evaluated expression|true in this context)
 #endif
   return 0;
 }
Index: libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
===
--- libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
+++ libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
@@ -93,12 +93,10 @@
 }
 
 int main(int, char**) {
-  if (!std::is_constant_evaluated()) {
-test_containers, std::deque>();
-test_containers, std::vector>();
-test_containers, std::deque>();
-test_containers, std::vector>();
-  }
+  test_containers, std::deque>();
+  test_containers, std::vector>();
+  test_containers, std::deque>();
+  test_containers, std::vector>();
 
   types::for_each(types::forward_iterator_list{}, [] {
 test_join_view();
Index: clang/unittests/Support/TimeProfilerTest.cpp
===
--- clang/unittests/Support/TimeProfilerTest.cpp
+++ clang/unittests/Support/TimeProfilerTest.cpp
@@ -188,7 +188,6 @@
 | EvaluateAsBooleanCondition ()
 | | EvaluateAsRValue ()
 | EvaluateAsInitializer (slow_value)
-| EvaluateAsConstantExpr ()
 | EvaluateAsConstantExpr ()
 | EvaluateAsRValue ()
 | EvaluateAsInitializer (slow_init_list)
Index: clang/test/SemaTemplate/concepts.cpp
===
--- clang/test/SemaTemplate/concepts.cpp
+++ clang/test/SemaTemplate/concepts.cpp
@@ -135,21 +135,21 @@
 
 namespace BuiltinIsConstantEvaluated {
   // Check that we do all satisfaction and diagnostic checks in a constant context.
-  template concept C = __builtin_is_constant_evaluated(); // expected-warning {{always}}
+  template concept C = __builtin_is_constant_evaluated(); // expected-warning {{always evaluate to true}}
   static_assert(C);
 
-  template concept D = __builtin_is_constant_evaluated() == true; // expected-warning {{always}}
+  template concept D = __builtin_is_constant_evaluated() == true; // expected-warning {{always evaluate to true}}
   static_assert(D);
 
-  template concept E = __builtin_is_constant_evaluated() == t

[PATCH] D155064: [clang][SemaCXX] Diagnose tautological uses of consteval if and is_constant_evaluated

2023-07-23 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added inline comments.



Comment at: clang/test/SemaCXX/vartemplate-lambda.cpp:17
+// 
expected-note{{cannot be used in a constant expression}} \
+// expected-error 2{{a 
lambda expression may not appear inside of a constant expression}}
 };

cor3ntin wrote:
> This also looks like a regression.
> 
> The current error is much clearer, can you investigate?
> ```
> :3:22: error: constexpr variable 't' must be initialized by a 
> constant expression
> 3 |   static constexpr T t = [](int f = T(7)){return f;}();
>   |  ^   ~
> :6:12: note: in instantiation of static data member 'S::t' 
> requested here
> 6 | int a = S::t;
>   |^
> :3:26: note: non-literal type 'S::(lambda at :3:26)' cannot 
> be used in a constant expression
> 3 |   static constexpr T t = [](int f = T(7)){return f;}();
>   |  ^
> ```
> 
> Why do we emt 2 errors instead of a single note? Here the error is that the 
> initializer is not a constant expression, everything else should be notes.
"lambda cannot be in constant expression" error is emitted from Sema against 
lambda expressions in constant-evaluated contexts in C++14 mode, and the note 
is emitted from constexpr evaluator.
The Sema-side error is emitted twice because it is emitted both before/after 
instantiation.
We can suppress one of them by ignoring it when sema is instantiating variable 
template initializer.
Or we can completely suppress this Sema error against initializers to avoid 
duplicate errors from Sema and constexpr evaluator.
I think "lambda cannot be in constant expression" Sema error is more 
understandable than the constexpr evaluator note "non-literal type cannot be in 
constant expression", so I think it is ok to keep one Sema error here.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155064/new/

https://reviews.llvm.org/D155064

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D153969: [clang][ExprConstant] Fix crash on uninitialized base class subobject

2023-07-24 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

Ping


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D153969/new/

https://reviews.llvm.org/D153969

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155064: [clang][SemaCXX] Diagnose tautological uses of consteval if and is_constant_evaluated

2023-07-24 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet updated this revision to Diff 543510.
hazohelet marked 4 inline comments as done.
hazohelet added a comment.

Address comments from Corentin

- When emitting "lambda expression may not appear inside of a constant 
expression" error from `Sema::PopExpressionEvaluationContext`, mark the 
variable declaration as invalid if the popped context is initializer evaluation 
context.
- This stops constexpr evaluator from evaluating the initializer and emitting 
duplicate errors.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155064/new/

https://reviews.llvm.org/D155064

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Basic/DiagnosticASTKinds.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/include/clang/Sema/Sema.h
  clang/lib/AST/ExprConstant.cpp
  clang/lib/Parse/ParseDecl.cpp
  clang/lib/Parse/ParseDeclCXX.cpp
  clang/lib/Parse/ParseStmt.cpp
  clang/lib/Sema/SemaChecking.cpp
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/lib/Sema/SemaLambda.cpp
  clang/lib/Sema/SemaStmt.cpp
  clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
  clang/test/AST/Interp/builtins.cpp
  clang/test/AST/Interp/if.cpp
  clang/test/AST/Interp/literals.cpp
  clang/test/CXX/expr/expr.const/p2-0x.cpp
  clang/test/CXX/expr/expr.const/p6-2a.cpp
  clang/test/CXX/expr/expr.prim/expr.prim.lambda/p3.cpp
  clang/test/CXX/stmt.stmt/stmt.select/stmt.if/p4.cpp
  clang/test/Parser/pragma-fenv_access.c
  clang/test/SemaCXX/constant-conversion.cpp
  clang/test/SemaCXX/constant-expression-cxx11.cpp
  clang/test/SemaCXX/cxx2a-consteval.cpp
  clang/test/SemaCXX/cxx2b-consteval-if.cpp
  clang/test/SemaCXX/vartemplate-lambda.cpp
  clang/test/SemaCXX/warn-constant-evaluated-constexpr.cpp
  clang/test/SemaCXX/warn-tautological-meta-constant.cpp
  clang/test/SemaTemplate/concepts.cpp
  clang/unittests/Support/TimeProfilerTest.cpp
  
libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
  
libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp

Index: libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp
===
--- libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp
+++ libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp
@@ -24,7 +24,7 @@
 #else
   // expected-error-re@+1 (static_assert|static assertion)}} failed}}
   static_assert(!std::is_constant_evaluated(), "");
-  // expected-warning@-1 0-1 {{'std::is_constant_evaluated' will always evaluate to 'true' in a manifestly constant-evaluated expression}}
+  // expected-warning-re@-1 0-1 {{'std::is_constant_evaluated' will always evaluate to {{('true' in a manifestly constant-evaluated expression|true in this context)
 #endif
   return 0;
 }
Index: libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
===
--- libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
+++ libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
@@ -93,12 +93,10 @@
 }
 
 int main(int, char**) {
-  if (!std::is_constant_evaluated()) {
-test_containers, std::deque>();
-test_containers, std::vector>();
-test_containers, std::deque>();
-test_containers, std::vector>();
-  }
+  test_containers, std::deque>();
+  test_containers, std::vector>();
+  test_containers, std::deque>();
+  test_containers, std::vector>();
 
   types::for_each(types::forward_iterator_list{}, [] {
 test_join_view();
Index: clang/unittests/Support/TimeProfilerTest.cpp
===
--- clang/unittests/Support/TimeProfilerTest.cpp
+++ clang/unittests/Support/TimeProfilerTest.cpp
@@ -188,7 +188,6 @@
 | EvaluateAsBooleanCondition ()
 | | EvaluateAsRValue ()
 | EvaluateAsInitializer (slow_value)
-| EvaluateAsConstantExpr ()
 | EvaluateAsConstantExpr ()
 | EvaluateAsRValue ()
 | EvaluateAsInitializer (slow_init_list)
Index: clang/test/SemaTemplate/concepts.cpp
===
--- clang/test/SemaTemplate/concepts.cpp
+++ clang/test/SemaTemplate/concepts.cpp
@@ -135,21 +135,21 @@
 
 namespace BuiltinIsConstantEvaluated {
   // Check that we do all satisfaction and diagnostic checks in a constant context.
-  template concept C = __builtin_is_constant_evaluated(); // expected-warning {{always}}
+  template concept C = __builtin_is_constant_evaluated(); // expected-warning {{always evaluate to true}}
   static_assert(C);
 
-  template concept D = __builtin_is_constant_evaluated() == true; // expected-warning {{always}}
+  template concept D = __builtin_is_constant_evaluated() == true; // expected-warning {{always evaluate to true}}
   static_assert(D);
 
-  template concept E = __bui

[PATCH] D155064: [clang][SemaCXX] Diagnose tautological uses of consteval if and is_constant_evaluated

2023-07-24 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added inline comments.



Comment at: clang/test/SemaCXX/vartemplate-lambda.cpp:17
+// 
expected-note{{cannot be used in a constant expression}} \
+// expected-error 2{{a 
lambda expression may not appear inside of a constant expression}}
 };

cor3ntin wrote:
> hazohelet wrote:
> > cor3ntin wrote:
> > > This also looks like a regression.
> > > 
> > > The current error is much clearer, can you investigate?
> > > ```
> > > :3:22: error: constexpr variable 't' must be initialized by 
> > > a constant expression
> > > 3 |   static constexpr T t = [](int f = T(7)){return f;}();
> > >   |  ^   ~
> > > :6:12: note: in instantiation of static data member 'S::t' 
> > > requested here
> > > 6 | int a = S::t;
> > >   |^
> > > :3:26: note: non-literal type 'S::(lambda at :3:26)' 
> > > cannot be used in a constant expression
> > > 3 |   static constexpr T t = [](int f = T(7)){return f;}();
> > >   |  ^
> > > ```
> > > 
> > > Why do we emt 2 errors instead of a single note? Here the error is that 
> > > the initializer is not a constant expression, everything else should be 
> > > notes.
> > "lambda cannot be in constant expression" error is emitted from Sema 
> > against lambda expressions in constant-evaluated contexts in C++14 mode, 
> > and the note is emitted from constexpr evaluator.
> > The Sema-side error is emitted twice because it is emitted both 
> > before/after instantiation.
> > We can suppress one of them by ignoring it when sema is instantiating 
> > variable template initializer.
> > Or we can completely suppress this Sema error against initializers to avoid 
> > duplicate errors from Sema and constexpr evaluator.
> > I think "lambda cannot be in constant expression" Sema error is more 
> > understandable than the constexpr evaluator note "non-literal type cannot 
> > be in constant expression", so I think it is ok to keep one Sema error here.
> So maybe the issue is that we are not making the declaration invalid in sema 
> when we get this error? Can you look into it?
> any opinion @aaron.ballman 
I updated the patch to keep a single sema error here and stop constant 
interpreter from evaluating the initializer by marking declaration invalid. I 
like having one sema error here.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155064/new/

https://reviews.llvm.org/D155064

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D154366: [clang][ExprConstant] Print template arguments when describing stack frame

2023-07-25 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

In D154366#4530536 , @dblaikie wrote:

> @hazohelet can you commit this yourself, or do you need someone to commit it 
> on your behalf? (& if so, what name/email address would you like to be 
> credited)

Thanks for the review.
I have commit access now, so I'll land this myself.
I'll add some missing tests on method function templates before landing this.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D154366/new/

https://reviews.llvm.org/D154366

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D152093: [clang][Analysis] Handle && and || against variable and its negation as tautology

2023-07-27 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

Ping


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D152093/new/

https://reviews.llvm.org/D152093

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155064: [clang][SemaCXX] Diagnose tautological uses of consteval if and is_constant_evaluated

2023-08-10 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet updated this revision to Diff 549203.
hazohelet added a comment.

Resolved the constexpr-if init-statement expression evaluation context problem.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155064/new/

https://reviews.llvm.org/D155064

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Basic/DiagnosticASTKinds.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/include/clang/Sema/Sema.h
  clang/lib/AST/ExprConstant.cpp
  clang/lib/Parse/ParseDecl.cpp
  clang/lib/Parse/ParseDeclCXX.cpp
  clang/lib/Parse/ParseExpr.cpp
  clang/lib/Parse/ParseExprCXX.cpp
  clang/lib/Parse/ParseStmt.cpp
  clang/lib/Sema/SemaChecking.cpp
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/lib/Sema/SemaLambda.cpp
  clang/lib/Sema/SemaStmt.cpp
  clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
  clang/test/AST/Interp/builtins.cpp
  clang/test/AST/Interp/if.cpp
  clang/test/AST/Interp/literals.cpp
  clang/test/CXX/expr/expr.const/p2-0x.cpp
  clang/test/CXX/expr/expr.const/p6-2a.cpp
  clang/test/CXX/expr/expr.prim/expr.prim.lambda/p3.cpp
  clang/test/CXX/stmt.stmt/stmt.select/stmt.if/p4.cpp
  clang/test/Parser/pragma-fenv_access.c
  clang/test/SemaCXX/constant-conversion.cpp
  clang/test/SemaCXX/constant-expression-cxx11.cpp
  clang/test/SemaCXX/cxx2a-consteval.cpp
  clang/test/SemaCXX/cxx2b-consteval-if.cpp
  clang/test/SemaCXX/cxx2b-consteval-propagate.cpp
  clang/test/SemaCXX/vartemplate-lambda.cpp
  clang/test/SemaCXX/warn-constant-evaluated-constexpr.cpp
  clang/test/SemaCXX/warn-tautological-meta-constant.cpp
  clang/test/SemaTemplate/concepts.cpp
  clang/unittests/Support/TimeProfilerTest.cpp
  libcxx/include/__type_traits/is_constant_evaluated.h
  
libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
  
libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp

Index: libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp
===
--- libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp
+++ libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp
@@ -24,7 +24,7 @@
 #else
   // expected-error-re@+1 (static_assert|static assertion)}} failed}}
   static_assert(!std::is_constant_evaluated(), "");
-  // expected-warning@-1 0-1 {{'std::is_constant_evaluated' will always evaluate to 'true' in a manifestly constant-evaluated expression}}
+  // expected-warning-re@-1 0-1 {{'std::is_constant_evaluated' will always evaluate to {{('true' in a manifestly constant-evaluated expression|true in this context)
 #endif
   return 0;
 }
Index: libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
===
--- libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
+++ libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
@@ -93,12 +93,10 @@
 }
 
 int main(int, char**) {
-  if (!std::is_constant_evaluated()) {
-test_containers, std::deque>();
-test_containers, std::vector>();
-test_containers, std::deque>();
-test_containers, std::vector>();
-  }
+  test_containers, std::deque>();
+  test_containers, std::vector>();
+  test_containers, std::deque>();
+  test_containers, std::vector>();
 
   types::for_each(types::forward_iterator_list{}, [] {
 test_join_view();
Index: libcxx/include/__type_traits/is_constant_evaluated.h
===
--- libcxx/include/__type_traits/is_constant_evaluated.h
+++ libcxx/include/__type_traits/is_constant_evaluated.h
@@ -24,7 +24,11 @@
 #endif
 
 _LIBCPP_HIDE_FROM_ABI inline _LIBCPP_CONSTEXPR bool __libcpp_is_constant_evaluated() _NOEXCEPT {
+#ifndef _LIBCPP_CXX03_LANG
   return __builtin_is_constant_evaluated();
+#else
+  return false;
+#endif
 }
 
 _LIBCPP_END_NAMESPACE_STD
Index: clang/unittests/Support/TimeProfilerTest.cpp
===
--- clang/unittests/Support/TimeProfilerTest.cpp
+++ clang/unittests/Support/TimeProfilerTest.cpp
@@ -188,7 +188,6 @@
 | EvaluateAsBooleanCondition ()
 | | EvaluateAsRValue ()
 | EvaluateAsInitializer (slow_value)
-| EvaluateAsConstantExpr ()
 | EvaluateAsConstantExpr ()
 | EvaluateAsRValue ()
 | EvaluateAsInitializer (slow_init_list)
Index: clang/test/SemaTemplate/concepts.cpp
===
--- clang/test/SemaTemplate/concepts.cpp
+++ clang/test/SemaTemplate/concepts.cpp
@@ -135,21 +135,21 @@
 
 namespace BuiltinIsConstantEvaluated {
   // Check that we do all satisfaction and diagnostic checks in a constant context.
-  template concept C = __builtin_is_constant_evaluated(); // expected-warning {{always}}
+  template concept C = __builtin_is_constan

[PATCH] D157855: [clang][ExprConstant] Improve error message of compound assignment against uninitialized object

2023-08-14 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet created this revision.
hazohelet added reviewers: aaron.ballman, tbaeder, cjdb.
Herald added a project: All.
hazohelet requested review of this revision.
Herald added a project: clang.

BEFORE this patch, compound assignment operator against uninitialized object 
such as `uninit += 1` was diagnosed as `subexpression not valid`
This patch clarifies the reason for the error by saying that `uninit` is an 
uninitialized object.

Fixes https://github.com/llvm/llvm-project/issues/51536


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D157855

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/AST/ExprConstant.cpp
  clang/test/SemaCXX/constant-expression-cxx2a.cpp


Index: clang/test/SemaCXX/constant-expression-cxx2a.cpp
===
--- clang/test/SemaCXX/constant-expression-cxx2a.cpp
+++ clang/test/SemaCXX/constant-expression-cxx2a.cpp
@@ -1492,3 +1492,33 @@
 class D : B{}; // expected-error {{deleted function '~D' cannot override a 
non-deleted function}}
 // expected-note@-1 {{destructor of 'D' is implicitly deleted because base 
class 'B' has an inaccessible destructor}}
 }
+
+namespace UninitCompoundAssign {
+constexpr int scalar(int a) {
+  int sum;
+  sum += a; // expected-note {{read of uninitialized object}};
+  return 0;
+}
+static_assert(scalar(3)); // expected-error {{constant expression}} \
+  // expected-note {{in call to 'scalar(3)'}}
+
+constexpr int array(int a) {
+  int arr[3];
+  arr[1] += a; // expected-note {{read of uninitialized object}};
+  return 0;
+}
+static_assert(array(3)); // expected-error {{constant expression}} \
+ // expected-note {{in call to 'array(3)'}}
+
+struct Foo {
+  int val;
+  constexpr Foo() {}
+};
+constexpr int field(int a) {
+  Foo f;
+  f.val += a; // expected-note {{read of uninitialized object}};
+  return 0;
+}
+static_assert(field(3)); // expected-error {{constant expression}} \
+ // expected-note {{in call to 'field(3)'}}
+}
Index: clang/lib/AST/ExprConstant.cpp
===
--- clang/lib/AST/ExprConstant.cpp
+++ clang/lib/AST/ExprConstant.cpp
@@ -4442,6 +4442,10 @@
   return foundPointer(Subobj, SubobjType);
 case APValue::Vector:
   return foundVector(Subobj, SubobjType);
+case APValue::Indeterminate:
+  Info.FFDiag(E, diag::note_constexpr_access_uninit)
+  << /*read of*/ 0 << /*uninitialized object*/ 1;
+  return false;
 default:
   // FIXME: can this happen?
   Info.FFDiag(E);
Index: clang/docs/ReleaseNotes.rst
===
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -129,6 +129,9 @@
   of a base class is not called in the constructor of its derived class.
 - Clang no longer emits ``-Wmissing-variable-declarations`` for variables 
declared
   with the ``register`` storage class.
+- Clang constexpr evaluator now diagnoses compound assignment operators against
+  uninitialized variables as a read of uninitialized object.
+  (`#51536 _`)
 
 Bug Fixes in This Version
 -


Index: clang/test/SemaCXX/constant-expression-cxx2a.cpp
===
--- clang/test/SemaCXX/constant-expression-cxx2a.cpp
+++ clang/test/SemaCXX/constant-expression-cxx2a.cpp
@@ -1492,3 +1492,33 @@
 class D : B{}; // expected-error {{deleted function '~D' cannot override a non-deleted function}}
 // expected-note@-1 {{destructor of 'D' is implicitly deleted because base class 'B' has an inaccessible destructor}}
 }
+
+namespace UninitCompoundAssign {
+constexpr int scalar(int a) {
+  int sum;
+  sum += a; // expected-note {{read of uninitialized object}};
+  return 0;
+}
+static_assert(scalar(3)); // expected-error {{constant expression}} \
+  // expected-note {{in call to 'scalar(3)'}}
+
+constexpr int array(int a) {
+  int arr[3];
+  arr[1] += a; // expected-note {{read of uninitialized object}};
+  return 0;
+}
+static_assert(array(3)); // expected-error {{constant expression}} \
+ // expected-note {{in call to 'array(3)'}}
+
+struct Foo {
+  int val;
+  constexpr Foo() {}
+};
+constexpr int field(int a) {
+  Foo f;
+  f.val += a; // expected-note {{read of uninitialized object}};
+  return 0;
+}
+static_assert(field(3)); // expected-error {{constant expression}} \
+ // expected-note {{in call to 'field(3)'}}
+}
Index: clang/lib/AST/ExprConstant.cpp
===
--- clang/lib/AST/ExprConstant.cpp
+++ clang/lib/AST/ExprConstant.cpp
@@ -4442,6 +4442,10 @@
   return foundPointer(Subobj, SubobjType);
 case APValue::Vector:
   return foundVector(Subobj, SubobjType);
+case APValue::Indeterminate:
+  

[PATCH] D157855: [clang][ExprConstant] Improve error message of compound assignment against uninitialized object

2023-08-15 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet updated this revision to Diff 550216.
hazohelet marked an inline comment as done.
hazohelet added a comment.

Address comments from Timm

- Moved test to C++14,20,23 test file from C++20-only one
- NFC stylistic changes


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157855/new/

https://reviews.llvm.org/D157855

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/AST/ExprConstant.cpp
  clang/test/SemaCXX/constant-expression-cxx14.cpp


Index: clang/test/SemaCXX/constant-expression-cxx14.cpp
===
--- clang/test/SemaCXX/constant-expression-cxx14.cpp
+++ clang/test/SemaCXX/constant-expression-cxx14.cpp
@@ -1275,3 +1275,33 @@
 (dbt2.wp = nullptr, 0)
   };
 }
+
+namespace UninitCompoundAssign {
+constexpr int scalar(int a) {
+  int sum; // cxx14-warning {{uninitialized variable in a constexpr function 
is a C++20 extension}}
+  sum += a; // expected-note {{read of uninitialized object}};
+  return 0;
+}
+static_assert(scalar(3), ""); // expected-error {{constant expression}} \
+  // expected-note {{in call to 'scalar(3)'}}
+
+constexpr int array(int a) {
+  int arr[3]; // cxx14-warning {{uninitialized variable in a constexpr 
function is a C++20 extension}}
+  arr[1] += a; // expected-note {{read of uninitialized object}};
+  return 0;
+}
+static_assert(array(3), ""); // expected-error {{constant expression}} \
+ // expected-note {{in call to 'array(3)'}}
+
+struct Foo {
+  int val; // cxx14-note{{member not initialized by constructor}}
+  constexpr Foo() {} // cxx14-warning {{constexpr constructor that does not 
initialize all members is a C++20 extension}}
+};
+constexpr int field(int a) {
+  Foo f;
+  f.val += a; // expected-note {{read of uninitialized object}};
+  return 0;
+}
+static_assert(field(3), ""); // expected-error {{constant expression}} \
+ // expected-note {{in call to 'field(3)'}}
+}
Index: clang/lib/AST/ExprConstant.cpp
===
--- clang/lib/AST/ExprConstant.cpp
+++ clang/lib/AST/ExprConstant.cpp
@@ -4442,6 +4442,10 @@
   return foundPointer(Subobj, SubobjType);
 case APValue::Vector:
   return foundVector(Subobj, SubobjType);
+case APValue::Indeterminate:
+  Info.FFDiag(E, diag::note_constexpr_access_uninit)
+  << /*read of=*/0 << /*uninitialized object=*/1;
+  return false;
 default:
   // FIXME: can this happen?
   Info.FFDiag(E);
Index: clang/docs/ReleaseNotes.rst
===
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -132,6 +132,9 @@
 - Clang now warns on unused variables declared and initialized in condition
   expressions.
   (`#61681: `_)
+- Clang constexpr evaluator now diagnoses compound assignment operators against
+  uninitialized variables as a read of uninitialized object.
+  (`#51536 _`)
 
 Bug Fixes in This Version
 -


Index: clang/test/SemaCXX/constant-expression-cxx14.cpp
===
--- clang/test/SemaCXX/constant-expression-cxx14.cpp
+++ clang/test/SemaCXX/constant-expression-cxx14.cpp
@@ -1275,3 +1275,33 @@
 (dbt2.wp = nullptr, 0)
   };
 }
+
+namespace UninitCompoundAssign {
+constexpr int scalar(int a) {
+  int sum; // cxx14-warning {{uninitialized variable in a constexpr function is a C++20 extension}}
+  sum += a; // expected-note {{read of uninitialized object}};
+  return 0;
+}
+static_assert(scalar(3), ""); // expected-error {{constant expression}} \
+  // expected-note {{in call to 'scalar(3)'}}
+
+constexpr int array(int a) {
+  int arr[3]; // cxx14-warning {{uninitialized variable in a constexpr function is a C++20 extension}}
+  arr[1] += a; // expected-note {{read of uninitialized object}};
+  return 0;
+}
+static_assert(array(3), ""); // expected-error {{constant expression}} \
+ // expected-note {{in call to 'array(3)'}}
+
+struct Foo {
+  int val; // cxx14-note{{member not initialized by constructor}}
+  constexpr Foo() {} // cxx14-warning {{constexpr constructor that does not initialize all members is a C++20 extension}}
+};
+constexpr int field(int a) {
+  Foo f;
+  f.val += a; // expected-note {{read of uninitialized object}};
+  return 0;
+}
+static_assert(field(3), ""); // expected-error {{constant expression}} \
+ // expected-note {{in call to 'field(3)'}}
+}
Index: clang/lib/AST/ExprConstant.cpp
===
--- clang/lib/AST/ExprConstant.cpp
+++ clang/lib/AST/ExprConstant.cpp
@@ -4442,6 +4442,10 @@
   return foundPointer(Subobj, SubobjType);
 case APValue::Vector:
   return 

[PATCH] D152495: [Clang][SemaCXX] Add unused warning for variables declared in condition expressions

2023-08-15 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

In D152495#4587804 , @chapuni wrote:

> Would this cause many warnings in Clang/LLVM tree?
> https://lab.llvm.org/buildbot/#/builders/36/builds/36560
>
> I hope you to fix possible warnings at first.

Thanks. I'll revert this change.
I was thinking of pushing speculative fixes upstream, but the lines that need 
fixing are not only a few lines, so I now think it needs a review.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D152495/new/

https://reviews.llvm.org/D152495

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D157383: [clang][Diagnostics] Provide source range to integer-overflow warnings

2023-08-15 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

Ping


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157383/new/

https://reviews.llvm.org/D157383

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155610: [Clang][Sema] Fix display of characters on static assertion failure

2023-08-15 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet updated this revision to Diff 550403.
hazohelet marked 16 inline comments as done.
hazohelet added a comment.

Address some review comments

- Renamed `ConvertCharToString` to `WriteCharValueForDiagnostic`
- Made the function static
- Fixed the printing for unicode 0x80 ~ 0xFF
- Added decimal value next to the hex code


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155610/new/

https://reviews.llvm.org/D155610

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/test/Lexer/cxx1z-trigraphs.cpp
  clang/test/SemaCXX/static-assert-cxx26.cpp
  clang/test/SemaCXX/static-assert.cpp

Index: clang/test/SemaCXX/static-assert.cpp
===
--- clang/test/SemaCXX/static-assert.cpp
+++ clang/test/SemaCXX/static-assert.cpp
@@ -268,7 +268,31 @@
 return 'c';
   }
   static_assert(getChar() == 'a', ""); // expected-error {{failed}} \
-   // expected-note {{evaluates to ''c' == 'a''}}
+   // expected-note {{evaluates to ''c' (0x63, 99) == 'a' (0x61, 97)'}}
+  static_assert((char)9 == '\x61', ""); // expected-error {{failed}} \
+// expected-note {{evaluates to ''\t' (0x09, 9) == 'a' (0x61, 97)'}}
+  static_assert((char)10 == '\0', ""); // expected-error {{failed}} \
+   // expected-note {{n' (0x0A, 10) == '' (0x00, 0)'}}
+  // The note above is intended to match "evaluates to '\n' (0x0A, 10) == '' (0x00, 0)'", but if we write it as it is,
+  // the "\n" cannot be consumed by the diagnostic consumer.
+  static_assert((signed char)10 == (char)-123, ""); // expected-error {{failed}} \
+// expected-note {{evaluates to '10 == '<85>' (0x85, -123)'}}
+  static_assert((char)-4 == (unsigned char)-8, ""); // expected-error {{failed}} \
+// expected-note {{evaluates to ''' (0xFC, -4) == 248'}}
+  static_assert((char)-128 == (char)-123, ""); // expected-error {{failed}} \
+   // expected-note {{evaluates to ''<80>' (0x80, -128) == '<85>' (0x85, -123)'}}
+  static_assert('\xA0' == (char)'\x20', ""); // expected-error {{failed}} \
+ // expected-note {{evaluates to ''' (0xA0, -96) == ' ' (0x20, 32)'}}
+  static_assert((char16_t)L'ゆ' == L"C̵̭̯̠̎͌ͅť̺"[1], ""); // expected-error {{failed}} \
+  // expected-note {{evaluates to ''ゆ' (0x3086, 12422) == '̵' (0x335, 821)'}}
+  static_assert(L"\/"[1] == u'\xFFFD', ""); // expected-error {{failed}} \
+  // expected-note {{evaluates to ''/' (0xFF0F, 65295) == '�' (0xFFFD, 65533)'}}
+  static_assert(L"⚾"[0] == U'🌍', ""); // expected-error {{failed}} \
+ // expected-note {{evaluates to ''⚾' (0x26BE, 9918) == '🌍' (0x1F30D, 127757)'}}
+  static_assert(U"\a"[0] == (wchar_t)9, ""); // expected-error {{failed}} \
+ // expected-note {{evaluates to ''\a' (0x07, 7) == '\t' (0x09, 9)'}}
+  static_assert(L"§"[0] == U'Ö', ""); // expected-error {{failed}} \
+  // expected-note {{evaluates to ''§' (0xA7, 167) == 'Ö' (0xD6, 214)'}}
 
   /// Bools are printed as bools.
   constexpr bool invert(bool b) {
Index: clang/test/SemaCXX/static-assert-cxx26.cpp
===
--- clang/test/SemaCXX/static-assert-cxx26.cpp
+++ clang/test/SemaCXX/static-assert-cxx26.cpp
@@ -298,3 +298,12 @@
 Bad b; // expected-note {{in instantiation}}
 
 }
+
+namespace EscapeInDiagnostic {
+static_assert('\u{9}' == (char)1, ""); // expected-error {{failed}} \
+   // expected-note {{evaluates to ''\t' (0x09, 9) == '' (0x01, 1)'}}
+static_assert((char8_t)-128 == (char8_t)-123, ""); // expected-error {{failed}} \
+   // expected-note {{evaluates to ''<80>' (0x80, 128) == '<85>' (0x85, 133)'}}
+static_assert((char16_t)0xFEFF == (char16_t)0xDB93, ""); // expected-error {{failed}} \
+ // expected-note {{evaluates to ''' (0xFEFF, 65279) == '\xDB93' (0xDB93, 56211)'}}
+}
Index: clang/test/Lexer/cxx1z-trigraphs.cpp
===
--- clang/test/Lexer/cxx1z-trigraphs.cpp
+++ clang/test/Lexer/cxx1z-trigraphs.cpp
@@ -21,7 +21,7 @@
 
 #if !ENABLED_TRIGRAPHS
 // expected-error@11 {{}} expected-warning@11 {{trigraph ignored}}
-// expected-error@13 {{failed}} expected-warning@13 {{trigraph ignored}} expected-note@13 {{evaluates to ''?' == '#''}}
+// expected-error@13 {{failed}} expected-warning@13 {{trigraph ignored}} expected-note@

[PATCH] D152495: [Clang][SemaCXX] Add unused warning for variables declared in condition expressions

2023-08-15 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet updated this revision to Diff 550433.
hazohelet added a comment.
Herald added subscribers: llvm-commits, wangpc, hoy, wlei, steakhal, abrachet, 
ormris, martong, MaskRay, hiraditya.
Herald added a reviewer: NoQ.
Herald added projects: LLVM, lld-macho.
Herald added a reviewer: lld-macho.

Fixed newly-issued warnings when trying 
`check-clang,llvm,lld,compiler-rt,cxx,cxxabi` with `-Werror=unused-variable` 
locally using local build of clang


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D152495/new/

https://reviews.llvm.org/D152495

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/ASTMatchers/ASTMatchers.h
  clang/lib/AST/Interp/ByteCodeStmtGen.cpp
  clang/lib/CodeGen/BackendUtil.cpp
  clang/lib/CodeGen/CGExprConstant.cpp
  clang/lib/CodeGen/CGOpenMPRuntime.cpp
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/lib/Driver/ToolChains/Cuda.cpp
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaExprCXX.cpp
  clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
  clang/lib/StaticAnalyzer/Checkers/FuchsiaHandleChecker.cpp
  clang/lib/StaticAnalyzer/Checkers/Yaml.h
  clang/test/SemaCXX/warn-unused-variables.cpp
  clang/tools/clang-scan-deps/ClangScanDeps.cpp
  lld/MachO/Driver.cpp
  llvm/lib/Bitcode/Reader/MetadataLoader.cpp
  llvm/lib/DebugInfo/DWARF/DWARFContext.cpp
  llvm/lib/DebugInfo/DWARF/DWARFUnit.cpp
  llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp
  llvm/lib/IR/PrintPasses.cpp
  llvm/lib/Passes/StandardInstrumentations.cpp
  llvm/lib/Support/VirtualFileSystem.cpp
  llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
  llvm/tools/llvm-profgen/ProfiledBinary.cpp

Index: llvm/tools/llvm-profgen/ProfiledBinary.cpp
===
--- llvm/tools/llvm-profgen/ProfiledBinary.cpp
+++ llvm/tools/llvm-profgen/ProfiledBinary.cpp
@@ -812,7 +812,7 @@
   // Handles DWO sections that can either be in .o, .dwo or .dwp files.
   for (const auto &CompilationUnit : DebugContext->compile_units()) {
 DWARFUnit *const DwarfUnit = CompilationUnit.get();
-if (std::optional DWOId = DwarfUnit->getDWOId()) {
+if (DwarfUnit->getDWOId()) {
   DWARFUnit *DWOCU = DwarfUnit->getNonSkeletonUnitDIE(false).getDwarfUnit();
   if (!DWOCU->isDWOUnit()) {
 std::string DWOName = dwarf::toString(
Index: llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
===
--- llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -9178,7 +9178,7 @@
 VecOp = FMulRecipe;
   } else {
 if (RecurrenceDescriptor::isMinMaxRecurrenceKind(Kind)) {
-  if (auto *Cmp = dyn_cast(CurrentLink)) {
+  if (isa(CurrentLink)) {
 assert(isa(CurrentLinkI) &&
"need to have the compare of the select");
 continue;
Index: llvm/lib/Support/VirtualFileSystem.cpp
===
--- llvm/lib/Support/VirtualFileSystem.cpp
+++ llvm/lib/Support/VirtualFileSystem.cpp
@@ -1337,7 +1337,7 @@
   SmallString<256> Path;
   Path_.toVector(Path);
 
-  if (std::error_code EC = makeCanonical(Path))
+  if (makeCanonical(Path))
 return {};
 
   return ExternalFS->isLocal(Path, Result);
Index: llvm/lib/Passes/StandardInstrumentations.cpp
===
--- llvm/lib/Passes/StandardInstrumentations.cpp
+++ llvm/lib/Passes/StandardInstrumentations.cpp
@@ -501,7 +501,7 @@
   static SmallVector FD{-1};
   SmallVector SR{S};
   static SmallVector FileName{""};
-  if (auto Err = prepareTempFiles(FD, SR, FileName)) {
+  if (prepareTempFiles(FD, SR, FileName)) {
 dbgs() << "Unable to create temporary file.";
 return;
   }
@@ -518,7 +518,7 @@
 return;
   }
 
-  if (auto Err = cleanUpTempFiles(FileName))
+  if (cleanUpTempFiles(FileName))
 dbgs() << "Unable to remove temporary file.";
 }
 
Index: llvm/lib/IR/PrintPasses.cpp
===
--- llvm/lib/IR/PrintPasses.cpp
+++ llvm/lib/IR/PrintPasses.cpp
@@ -212,7 +212,7 @@
   static SmallVector FD{-1, -1, -1};
   SmallVector SR{Before, After};
   static SmallVector FileName{"", "", ""};
-  if (auto Err = prepareTempFiles(FD, SR, FileName))
+  if (prepareTempFiles(FD, SR, FileName))
 return "Unable to create temporary file.";
 
   static ErrorOr DiffExe = sys::findProgramByName(DiffBinary);
@@ -238,7 +238,7 @@
   else
 return "Unable to read result.";
 
-  if (auto Err = cleanUpTempFiles(FileName))
+  if (cleanUpTempFiles(FileName))
 return "Unable to remove temporary file.";
 
   return Diff;
Index: llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp
===
--- llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp
+++ llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp
@@ -462,7 +462,7 @@
   size_t NumBefore = Gsym.getNumFunctionInfos();
   auto getDie = [&](DWARFUnit 

[PATCH] D158016: [NFC] Remove unused variables declared in conditions

2023-08-15 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet created this revision.
hazohelet added reviewers: nikic, aaron.ballman, MaskRay, tbaeder.
Herald added subscribers: hoy, wlei, steakhal, abrachet, ormris, StephenFan, 
martong, hiraditya.
Herald added a reviewer: NoQ.
Herald added projects: lld-macho, All.
Herald added a reviewer: lld-macho.
hazohelet requested review of this revision.
Herald added subscribers: llvm-commits, cfe-commits, wangpc.
Herald added projects: clang, LLVM.

D152495  makes clang warn on unused variables 
that are declared in conditions like `if (int var = init) {}`
This patch is an NFC fix to suppress the new warning in llvm,clang,lld builds 
to pass CI in the above patch.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D158016

Files:
  clang/include/clang/ASTMatchers/ASTMatchers.h
  clang/lib/AST/Interp/ByteCodeStmtGen.cpp
  clang/lib/CodeGen/BackendUtil.cpp
  clang/lib/CodeGen/CGExprConstant.cpp
  clang/lib/CodeGen/CGOpenMPRuntime.cpp
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/lib/Driver/ToolChains/Cuda.cpp
  clang/lib/StaticAnalyzer/Checkers/FuchsiaHandleChecker.cpp
  clang/lib/StaticAnalyzer/Checkers/Yaml.h
  clang/tools/clang-scan-deps/ClangScanDeps.cpp
  lld/MachO/Driver.cpp
  llvm/lib/Bitcode/Reader/MetadataLoader.cpp
  llvm/lib/DebugInfo/DWARF/DWARFContext.cpp
  llvm/lib/DebugInfo/DWARF/DWARFUnit.cpp
  llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp
  llvm/lib/IR/PrintPasses.cpp
  llvm/lib/Passes/StandardInstrumentations.cpp
  llvm/lib/Support/VirtualFileSystem.cpp
  llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
  llvm/tools/llvm-profgen/ProfiledBinary.cpp

Index: llvm/tools/llvm-profgen/ProfiledBinary.cpp
===
--- llvm/tools/llvm-profgen/ProfiledBinary.cpp
+++ llvm/tools/llvm-profgen/ProfiledBinary.cpp
@@ -812,7 +812,7 @@
   // Handles DWO sections that can either be in .o, .dwo or .dwp files.
   for (const auto &CompilationUnit : DebugContext->compile_units()) {
 DWARFUnit *const DwarfUnit = CompilationUnit.get();
-if (std::optional DWOId = DwarfUnit->getDWOId()) {
+if (DwarfUnit->getDWOId()) {
   DWARFUnit *DWOCU = DwarfUnit->getNonSkeletonUnitDIE(false).getDwarfUnit();
   if (!DWOCU->isDWOUnit()) {
 std::string DWOName = dwarf::toString(
Index: llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
===
--- llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -9178,7 +9178,7 @@
 VecOp = FMulRecipe;
   } else {
 if (RecurrenceDescriptor::isMinMaxRecurrenceKind(Kind)) {
-  if (auto *Cmp = dyn_cast(CurrentLink)) {
+  if (isa(CurrentLink)) {
 assert(isa(CurrentLinkI) &&
"need to have the compare of the select");
 continue;
Index: llvm/lib/Support/VirtualFileSystem.cpp
===
--- llvm/lib/Support/VirtualFileSystem.cpp
+++ llvm/lib/Support/VirtualFileSystem.cpp
@@ -1337,7 +1337,7 @@
   SmallString<256> Path;
   Path_.toVector(Path);
 
-  if (std::error_code EC = makeCanonical(Path))
+  if (makeCanonical(Path))
 return {};
 
   return ExternalFS->isLocal(Path, Result);
Index: llvm/lib/Passes/StandardInstrumentations.cpp
===
--- llvm/lib/Passes/StandardInstrumentations.cpp
+++ llvm/lib/Passes/StandardInstrumentations.cpp
@@ -501,7 +501,7 @@
   static SmallVector FD{-1};
   SmallVector SR{S};
   static SmallVector FileName{""};
-  if (auto Err = prepareTempFiles(FD, SR, FileName)) {
+  if (prepareTempFiles(FD, SR, FileName)) {
 dbgs() << "Unable to create temporary file.";
 return;
   }
@@ -518,7 +518,7 @@
 return;
   }
 
-  if (auto Err = cleanUpTempFiles(FileName))
+  if (cleanUpTempFiles(FileName))
 dbgs() << "Unable to remove temporary file.";
 }
 
Index: llvm/lib/IR/PrintPasses.cpp
===
--- llvm/lib/IR/PrintPasses.cpp
+++ llvm/lib/IR/PrintPasses.cpp
@@ -212,7 +212,7 @@
   static SmallVector FD{-1, -1, -1};
   SmallVector SR{Before, After};
   static SmallVector FileName{"", "", ""};
-  if (auto Err = prepareTempFiles(FD, SR, FileName))
+  if (prepareTempFiles(FD, SR, FileName))
 return "Unable to create temporary file.";
 
   static ErrorOr DiffExe = sys::findProgramByName(DiffBinary);
@@ -238,7 +238,7 @@
   else
 return "Unable to read result.";
 
-  if (auto Err = cleanUpTempFiles(FileName))
+  if (cleanUpTempFiles(FileName))
 return "Unable to remove temporary file.";
 
   return Diff;
Index: llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp
===
--- llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp
+++ llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp
@@ -462,7 +462,7 @@
   size_t NumBefore = Gsym.getNumFunctionInfos();
  

[PATCH] D152495: [Clang][SemaCXX] Add unused warning for variables declared in condition expressions

2023-08-15 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet updated this revision to Diff 550443.
hazohelet added a comment.

Removed warning fixes that are now in D158016 


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D152495/new/

https://reviews.llvm.org/D152495

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaExprCXX.cpp
  clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
  clang/test/SemaCXX/warn-unused-variables.cpp

Index: clang/test/SemaCXX/warn-unused-variables.cpp
===
--- clang/test/SemaCXX/warn-unused-variables.cpp
+++ clang/test/SemaCXX/warn-unused-variables.cpp
@@ -1,5 +1,5 @@
 // RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -Wno-c++1y-extensions -verify %s
-// RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -Wno-c++1y-extensions -verify -std=c++11 %s
+// RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -Wno-c++14-extensions -Wno-c++17-extensions -verify -std=c++11 %s
 template void f() {
   T t;
   t = 17;
@@ -294,3 +294,115 @@
 }
 
 } // namespace gh54489
+
+namespace inside_condition {
+  void ifs() {
+if (int hoge = 0) // expected-warning {{unused variable 'hoge'}}
+  return;
+if (const int const_hoge = 0) // expected-warning {{unused variable 'const_hoge'}}
+  return;
+else if (int fuga = 0)
+  (void)fuga;
+else if (int used = 1; int catched = used) // expected-warning {{unused variable 'catched'}}
+  return;
+else if (int refed = 1; int used = refed)
+  (void)used;
+else if (int unused1 = 2; int unused2 = 3) // expected-warning {{unused variable 'unused1'}} \
+   // expected-warning {{unused variable 'unused2'}}
+  return;
+else if (int unused = 4; int used = 5) // expected-warning {{unused variable 'unused'}}
+  (void)used;
+else if (int used = 6; int unused = 7) // expected-warning {{unused variable 'unused'}}
+  (void)used;
+else if (int used1 = 8; int used2 = 9)
+  (void)(used1 + used2);
+else if (auto [a, b] = (int[2]){ 1, 2 }; 1) // expected-warning {{unused variable '[a, b]'}}
+  return;
+else if (auto [a, b] = (int[2]){ 1, 2 }; a)
+  return;
+  }
+
+  void fors() {
+for (int i = 0;int unused = 0;); // expected-warning {{unused variable 'i'}} \
+ // expected-warning {{unused variable 'unused'}}
+for (int i = 0;int used = 0;) // expected-warning {{unused variable 'i'}}
+  (void)used;
+  while(int var = 1) // expected-warning {{unused variable 'var'}}
+return;
+  }
+
+  void whiles() {
+while(int unused = 1) // expected-warning {{unused variable 'unused'}}
+  return;
+while(int used = 1)
+  (void)used;
+  }
+
+
+  void switches() {
+switch(int unused = 1) { // expected-warning {{unused variable 'unused'}}
+  case 1: return;
+}
+switch(constexpr int used = 3; int unused = 4) { // expected-warning {{unused variable 'unused'}}
+  case used: return;
+}
+switch(int used = 3; int unused = 4) { // expected-warning {{unused variable 'unused'}}
+  case 3: (void)used;
+}
+switch(constexpr int used1 = 0; constexpr int used2 = 6) {
+  case (used1+used2): return;
+}
+switch(auto [a, b] = (int[2]){ 1, 2 }; 1) { // expected-warning {{unused variable '[a, b]'}}
+  case 1: return;
+}
+switch(auto [a, b] = (int[2]){ 1, 2 }; b) {
+  case 1: return;
+}
+switch(auto [a, b] = (int[2]){ 1, 2 }; 1) {
+  case 1: (void)a;
+}
+  }
+  template 
+  struct Vector {
+void doIt() {
+  for (auto& e : elements){} // expected-warning {{unused variable 'e'}}
+}
+T elements[10];
+  };
+  void ranged_for() {
+Vectorvector;
+vector.doIt(); // expected-note {{here}}
+  }
+
+
+  struct RAII {
+int &x;
+RAII(int &ref) : x(ref) {}
+~RAII() { x = 0;}
+operator int() const { return 1; }
+  };
+  void aggregate() {
+int x = 10;
+int y = 10;
+if (RAII var = x) {}
+for(RAII var = x; RAII var2 = y;) {}
+while (RAII var = x) {}
+switch (RAII var = x) {}
+  }
+
+  struct TrivialDtor{
+int &x;
+TrivialDtor(int &ref) : x(ref) { ref = 32; }
+operator int() const { return 1; }
+  };
+  void trivial_dtor() {
+int x = 10;
+int y = 10;
+if (TrivialDtor var = x) {} // expected-warning {{unused variable 'var'}}
+for(TrivialDtor var = x; TrivialDtor var2 = y;) {} // expected-warning {{unused variable 'var'}} \
+ // expected-warning {{unused variable 'var2'}}
+while (TrivialDtor var = x) {} // expected-warning {{unused variable 'var'}}
+switch (TrivialDtor var = x) {} // expected-warning {{unused variable 'var'}}
+  }
+
+} // namespace inside_condition
Index: clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
===
--- c

[PATCH] D155064: [clang][SemaCXX] Diagnose tautological uses of consteval if and is_constant_evaluated

2023-08-15 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

Thanks for the feedback.




Comment at: clang/test/SemaCXX/warn-constant-evaluated-constexpr.cpp:38
 constexpr int fn5() {
-  if constexpr (__builtin_is_constant_evaluated()) // expected-warning 
{{'__builtin_is_constant_evaluated' will always evaluate to 'true' in a 
manifestly constant-evaluated expression}}
+  if constexpr (__builtin_is_constant_evaluated()) // expected-warning 
{{'__builtin_is_constant_evaluated' will always evaluate to true in this 
context}}
 return 0;

cjdb wrote:
> This should also generate a fix-it hint, since we can automate the fix.
I think it's reasonable to show fix-it hint to remove `constexpr` here.
For that we need to store the source range of `constexpr` in evaluation context 
record if it's constexpr-if condition.



Comment at: clang/test/SemaCXX/warn-tautological-meta-constant.cpp:18
+  } else {
+if constexpr (std::is_constant_evaluated()) { // expected-warning {{always 
evaluate to true}}
+  return 0;

cjdb wrote:
> I'm not a fan of this diagnostic text: it doesn't offer insight into what's 
> gone wrong or provide actionable feedback on how to fix the code.
I agree that the current wording (`... will always evaluate to true in this 
context`) is not great, but I'm not sure how to improve it because the reason 
to see this diagnostic would mostly be a misunderstanding of the C++ const 
semantics.
At least, if the appearance of `is_constant_evaluated` is in the condition of 
constexpr-if, probably we can say they should remove `constexpr`.
CC @aaron.ballman 


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155064/new/

https://reviews.llvm.org/D155064

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155610: [Clang][Sema] Fix display of characters on static assertion failure

2023-08-16 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet updated this revision to Diff 550707.
hazohelet marked 3 inline comments as done.
hazohelet added a comment.

Address comments from Hubert

- Bring back type prefix
- NFC stylistic changes


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155610/new/

https://reviews.llvm.org/D155610

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/test/Lexer/cxx1z-trigraphs.cpp
  clang/test/SemaCXX/static-assert-cxx26.cpp
  clang/test/SemaCXX/static-assert.cpp

Index: clang/test/SemaCXX/static-assert.cpp
===
--- clang/test/SemaCXX/static-assert.cpp
+++ clang/test/SemaCXX/static-assert.cpp
@@ -268,7 +268,31 @@
 return 'c';
   }
   static_assert(getChar() == 'a', ""); // expected-error {{failed}} \
-   // expected-note {{evaluates to ''c' == 'a''}}
+   // expected-note {{evaluates to ''c' (0x63, 99) == 'a' (0x61, 97)'}}
+  static_assert((char)9 == '\x61', ""); // expected-error {{failed}} \
+// expected-note {{evaluates to ''\t' (0x09, 9) == 'a' (0x61, 97)'}}
+  static_assert((char)10 == '\0', ""); // expected-error {{failed}} \
+   // expected-note {{n' (0x0A, 10) == '' (0x00, 0)'}}
+  // The note above is intended to match "evaluates to '\n' (0x0A, 10) == '' (0x00, 0)'", but if we write it as it is,
+  // the "\n" cannot be consumed by the diagnostic consumer.
+  static_assert((signed char)10 == (char)-123, ""); // expected-error {{failed}} \
+// expected-note {{evaluates to '10 == '<85>' (0x85, -123)'}}
+  static_assert((char)-4 == (unsigned char)-8, ""); // expected-error {{failed}} \
+// expected-note {{evaluates to ''' (0xFC, -4) == 248'}}
+  static_assert((char)-128 == (char)-123, ""); // expected-error {{failed}} \
+   // expected-note {{evaluates to ''<80>' (0x80, -128) == '<85>' (0x85, -123)'}}
+  static_assert('\xA0' == (char)'\x20', ""); // expected-error {{failed}} \
+ // expected-note {{evaluates to ''' (0xA0, -96) == ' ' (0x20, 32)'}}
+  static_assert((char16_t)L'ゆ' == L"C̵̭̯̠̎͌ͅť̺"[1], ""); // expected-error {{failed}} \
+  // expected-note {{evaluates to 'u'ゆ' (0x3086, 12422) == L'̵' (0x335, 821)'}}
+  static_assert(L"\/"[1] == u'\xFFFD', ""); // expected-error {{failed}} \
+  // expected-note {{evaluates to 'L'/' (0xFF0F, 65295) == u'�' (0xFFFD, 65533)'}}
+  static_assert(L"⚾"[0] == U'🌍', ""); // expected-error {{failed}} \
+ // expected-note {{evaluates to 'L'⚾' (0x26BE, 9918) == U'🌍' (0x1F30D, 127757)'}}
+  static_assert(U"\a"[0] == (wchar_t)9, ""); // expected-error {{failed}} \
+ // expected-note {{evaluates to 'U'\a' (0x07, 7) == L'\t' (0x09, 9)'}}
+  static_assert(L"§"[0] == U'Ö', ""); // expected-error {{failed}} \
+  // expected-note {{evaluates to 'L'§' (0xA7, 167) == U'Ö' (0xD6, 214)'}}
 
   /// Bools are printed as bools.
   constexpr bool invert(bool b) {
Index: clang/test/SemaCXX/static-assert-cxx26.cpp
===
--- clang/test/SemaCXX/static-assert-cxx26.cpp
+++ clang/test/SemaCXX/static-assert-cxx26.cpp
@@ -298,3 +298,12 @@
 Bad b; // expected-note {{in instantiation}}
 
 }
+
+namespace EscapeInDiagnostic {
+static_assert('\u{9}' == (char)1, ""); // expected-error {{failed}} \
+   // expected-note {{evaluates to ''\t' (0x09, 9) == '' (0x01, 1)'}}
+static_assert((char8_t)-128 == (char8_t)-123, ""); // expected-error {{failed}} \
+   // expected-note {{evaluates to 'u8'<80>' (0x80, 128) == u8'<85>' (0x85, 133)'}}
+static_assert((char16_t)0xFEFF == (char16_t)0xDB93, ""); // expected-error {{failed}} \
+ // expected-note {{evaluates to 'u'' (0xFEFF, 65279) == u'\xDB93' (0xDB93, 56211)'}}
+}
Index: clang/test/Lexer/cxx1z-trigraphs.cpp
===
--- clang/test/Lexer/cxx1z-trigraphs.cpp
+++ clang/test/Lexer/cxx1z-trigraphs.cpp
@@ -21,7 +21,7 @@
 
 #if !ENABLED_TRIGRAPHS
 // expected-error@11 {{}} expected-warning@11 {{trigraph ignored}}
-// expected-error@13 {{failed}} expected-warning@13 {{trigraph ignored}} expected-note@13 {{evaluates to ''?' == '#''}}
+// expected-error@13 {{failed}} expected-warning@13 {{trigraph ignored}} expected-note@13 {{evaluates to ''?' (0x3F, 63) == '#' (0x23, 35)'}}
 // expected-error@16 {{}}
 // expected-error@20 {{}}
 #else

[PATCH] D155064: [clang][SemaCXX] Diagnose tautological uses of consteval if and is_constant_evaluated

2023-08-16 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet updated this revision to Diff 550881.
hazohelet marked an inline comment as done.
hazohelet added a comment.

Address comments from Chris

- Generate fix-it hint to remove `constexpr` in constexpr-if

Added `SourceLocation` field to Sema that saves the location of `constexpr` in 
constexpr-if.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155064/new/

https://reviews.llvm.org/D155064

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Basic/DiagnosticASTKinds.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/include/clang/Parse/Parser.h
  clang/include/clang/Sema/Sema.h
  clang/lib/AST/ExprConstant.cpp
  clang/lib/Parse/ParseDecl.cpp
  clang/lib/Parse/ParseDeclCXX.cpp
  clang/lib/Parse/ParseExpr.cpp
  clang/lib/Parse/ParseExprCXX.cpp
  clang/lib/Parse/ParseStmt.cpp
  clang/lib/Sema/SemaChecking.cpp
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/lib/Sema/SemaLambda.cpp
  clang/lib/Sema/SemaStmt.cpp
  clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
  clang/test/AST/Interp/builtins.cpp
  clang/test/AST/Interp/if.cpp
  clang/test/AST/Interp/literals.cpp
  clang/test/CXX/expr/expr.const/p2-0x.cpp
  clang/test/CXX/expr/expr.const/p6-2a.cpp
  clang/test/CXX/expr/expr.prim/expr.prim.lambda/p3.cpp
  clang/test/CXX/stmt.stmt/stmt.select/stmt.if/p4.cpp
  clang/test/Parser/pragma-fenv_access.c
  clang/test/SemaCXX/constant-conversion.cpp
  clang/test/SemaCXX/constant-expression-cxx11.cpp
  clang/test/SemaCXX/cxx2a-consteval.cpp
  clang/test/SemaCXX/cxx2b-consteval-if.cpp
  clang/test/SemaCXX/cxx2b-consteval-propagate.cpp
  clang/test/SemaCXX/fixit-tautological-meta-constant.cpp
  clang/test/SemaCXX/vartemplate-lambda.cpp
  clang/test/SemaCXX/warn-constant-evaluated-constexpr.cpp
  clang/test/SemaCXX/warn-tautological-meta-constant.cpp
  clang/test/SemaTemplate/concepts.cpp
  clang/unittests/Support/TimeProfilerTest.cpp
  libcxx/include/__type_traits/is_constant_evaluated.h
  
libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
  
libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp

Index: libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp
===
--- libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp
+++ libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp
@@ -24,7 +24,7 @@
 #else
   // expected-error-re@+1 (static_assert|static assertion)}} failed}}
   static_assert(!std::is_constant_evaluated(), "");
-  // expected-warning@-1 0-1 {{'std::is_constant_evaluated' will always evaluate to 'true' in a manifestly constant-evaluated expression}}
+  // expected-warning-re@-1 0-1 {{'std::is_constant_evaluated' will always evaluate to {{('true' in a manifestly constant-evaluated expression|true in this context)
 #endif
   return 0;
 }
Index: libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
===
--- libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
+++ libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
@@ -93,12 +93,10 @@
 }
 
 int main(int, char**) {
-  if (!std::is_constant_evaluated()) {
-test_containers, std::deque>();
-test_containers, std::vector>();
-test_containers, std::deque>();
-test_containers, std::vector>();
-  }
+  test_containers, std::deque>();
+  test_containers, std::vector>();
+  test_containers, std::deque>();
+  test_containers, std::vector>();
 
   types::for_each(types::forward_iterator_list{}, [] {
 test_join_view();
Index: libcxx/include/__type_traits/is_constant_evaluated.h
===
--- libcxx/include/__type_traits/is_constant_evaluated.h
+++ libcxx/include/__type_traits/is_constant_evaluated.h
@@ -24,7 +24,11 @@
 #endif
 
 _LIBCPP_HIDE_FROM_ABI inline _LIBCPP_CONSTEXPR bool __libcpp_is_constant_evaluated() _NOEXCEPT {
+#ifndef _LIBCPP_CXX03_LANG
   return __builtin_is_constant_evaluated();
+#else
+  return false;
+#endif
 }
 
 _LIBCPP_END_NAMESPACE_STD
Index: clang/unittests/Support/TimeProfilerTest.cpp
===
--- clang/unittests/Support/TimeProfilerTest.cpp
+++ clang/unittests/Support/TimeProfilerTest.cpp
@@ -188,7 +188,6 @@
 | EvaluateAsBooleanCondition ()
 | | EvaluateAsRValue ()
 | EvaluateAsInitializer (slow_value)
-| EvaluateAsConstantExpr ()
 | EvaluateAsConstantExpr ()
 | EvaluateAsRValue ()
 | EvaluateAsInitializer (slow_init_list)
Index: clang/test/SemaTemplate/concepts.cpp
===
--- clang/test/SemaTemplate/concepts.cpp
+++ clang/test/SemaTemplate/concepts.cpp
@@ -135,21 +135,21 @@
 
 namespace Buil

[PATCH] D155064: [clang][SemaCXX] Diagnose tautological uses of consteval if and is_constant_evaluated

2023-08-16 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet updated this revision to Diff 550899.
hazohelet added a comment.

rebase to upstream to run ci


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155064/new/

https://reviews.llvm.org/D155064

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Basic/DiagnosticASTKinds.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/include/clang/Parse/Parser.h
  clang/include/clang/Sema/Sema.h
  clang/lib/AST/ExprConstant.cpp
  clang/lib/Parse/ParseDecl.cpp
  clang/lib/Parse/ParseDeclCXX.cpp
  clang/lib/Parse/ParseExpr.cpp
  clang/lib/Parse/ParseExprCXX.cpp
  clang/lib/Parse/ParseStmt.cpp
  clang/lib/Sema/SemaChecking.cpp
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/lib/Sema/SemaLambda.cpp
  clang/lib/Sema/SemaStmt.cpp
  clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
  clang/test/AST/Interp/builtins.cpp
  clang/test/AST/Interp/if.cpp
  clang/test/AST/Interp/literals.cpp
  clang/test/CXX/expr/expr.const/p2-0x.cpp
  clang/test/CXX/expr/expr.const/p6-2a.cpp
  clang/test/CXX/expr/expr.prim/expr.prim.lambda/p3.cpp
  clang/test/CXX/stmt.stmt/stmt.select/stmt.if/p4.cpp
  clang/test/Parser/pragma-fenv_access.c
  clang/test/SemaCXX/constant-conversion.cpp
  clang/test/SemaCXX/constant-expression-cxx11.cpp
  clang/test/SemaCXX/cxx2a-consteval.cpp
  clang/test/SemaCXX/cxx2b-consteval-if.cpp
  clang/test/SemaCXX/cxx2b-consteval-propagate.cpp
  clang/test/SemaCXX/fixit-tautological-meta-constant.cpp
  clang/test/SemaCXX/vartemplate-lambda.cpp
  clang/test/SemaCXX/warn-constant-evaluated-constexpr.cpp
  clang/test/SemaCXX/warn-tautological-meta-constant.cpp
  clang/test/SemaTemplate/concepts.cpp
  clang/unittests/Support/TimeProfilerTest.cpp
  libcxx/include/__type_traits/is_constant_evaluated.h
  
libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
  
libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp

Index: libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp
===
--- libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp
+++ libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp
@@ -24,7 +24,7 @@
 #else
   // expected-error-re@+1 (static_assert|static assertion)}} failed}}
   static_assert(!std::is_constant_evaluated(), "");
-  // expected-warning@-1 0-1 {{'std::is_constant_evaluated' will always evaluate to 'true' in a manifestly constant-evaluated expression}}
+  // expected-warning-re@-1 0-1 {{'std::is_constant_evaluated' will always evaluate to {{('true' in a manifestly constant-evaluated expression|true in this context)
 #endif
   return 0;
 }
Index: libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
===
--- libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
+++ libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
@@ -93,12 +93,10 @@
 }
 
 int main(int, char**) {
-  if (!std::is_constant_evaluated()) {
-test_containers, std::deque>();
-test_containers, std::vector>();
-test_containers, std::deque>();
-test_containers, std::vector>();
-  }
+  test_containers, std::deque>();
+  test_containers, std::vector>();
+  test_containers, std::deque>();
+  test_containers, std::vector>();
 
   types::for_each(types::forward_iterator_list{}, [] {
 test_join_view();
Index: libcxx/include/__type_traits/is_constant_evaluated.h
===
--- libcxx/include/__type_traits/is_constant_evaluated.h
+++ libcxx/include/__type_traits/is_constant_evaluated.h
@@ -24,7 +24,11 @@
 #endif
 
 _LIBCPP_HIDE_FROM_ABI inline _LIBCPP_CONSTEXPR bool __libcpp_is_constant_evaluated() _NOEXCEPT {
+#ifndef _LIBCPP_CXX03_LANG
   return __builtin_is_constant_evaluated();
+#else
+  return false;
+#endif
 }
 
 _LIBCPP_END_NAMESPACE_STD
Index: clang/unittests/Support/TimeProfilerTest.cpp
===
--- clang/unittests/Support/TimeProfilerTest.cpp
+++ clang/unittests/Support/TimeProfilerTest.cpp
@@ -188,7 +188,6 @@
 | EvaluateAsBooleanCondition ()
 | | EvaluateAsRValue ()
 | EvaluateAsInitializer (slow_value)
-| EvaluateAsConstantExpr ()
 | EvaluateAsConstantExpr ()
 | EvaluateAsRValue ()
 | EvaluateAsInitializer (slow_init_list)
Index: clang/test/SemaTemplate/concepts.cpp
===
--- clang/test/SemaTemplate/concepts.cpp
+++ clang/test/SemaTemplate/concepts.cpp
@@ -135,21 +135,21 @@
 
 namespace BuiltinIsConstantEvaluated {
   // Check that we do all satisfaction and diagnostic checks in a constant context.
-  template concept C = __builtin_is_constant_evaluated(); // expected-warning {{always}}

[PATCH] D152093: [clang][Analysis] Handle && and || against variable and its negation as tautology

2023-08-17 Thread Takuya Shimizu via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
hazohelet marked an inline comment as done.
Closed by commit rGb3469ce6f80b: [clang][Analysis] Handle && and || 
against variable and its negation as… (authored by hazohelet).

Changed prior to commit:
  https://reviews.llvm.org/D152093?vs=545213&id=551047#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D152093/new/

https://reviews.llvm.org/D152093

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Analysis/CFG.h
  clang/include/clang/Basic/DiagnosticGroups.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Analysis/CFG.cpp
  clang/lib/Sema/AnalysisBasedWarnings.cpp
  clang/test/Analysis/temp-obj-dtors-cfg-output.cpp
  clang/test/Misc/warning-wall.c
  clang/test/SemaCXX/tautological-negation-compare.cpp
  clang/test/SemaCXX/warn-infinite-recursion.cpp

Index: clang/test/SemaCXX/warn-infinite-recursion.cpp
===
--- clang/test/SemaCXX/warn-infinite-recursion.cpp
+++ clang/test/SemaCXX/warn-infinite-recursion.cpp
@@ -203,3 +203,13 @@
   (void)typeid(unevaluated_recursive_function());
   return 0;
 }
+
+void func1(int i) { // expected-warning {{call itself}}
+  if (i || !i)
+func1(i);
+}
+void func2(int i) { // expected-warning {{call itself}}
+  if (!i && i) {}
+  else
+func2(i);
+}
Index: clang/test/SemaCXX/tautological-negation-compare.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/tautological-negation-compare.cpp
@@ -0,0 +1,41 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wtautological-negation-compare -Wno-constant-logical-operand %s
+// RUN: %clang_cc1 -fsyntax-only -verify -Wtautological-compare -Wno-constant-logical-operand %s
+// RUN: %clang_cc1 -fsyntax-only -verify -Wall -Wno-unused -Wno-loop-analysis -Wno-constant-logical-operand %s
+
+#define COPY(x) x
+
+void test_int(int x) {
+  if (x || !x) {} // expected-warning {{'||' of a value and its negation always evaluates to true}}
+  if (!x || x) {} // expected-warning {{'||' of a value and its negation always evaluates to true}}
+  if (x && !x) {} // expected-warning {{'&&' of a value and its negation always evaluates to false}}
+  if (!x && x) {} // expected-warning {{'&&' of a value and its negation always evaluates to false}}
+
+  // parentheses are ignored
+  if (x || (!x)) {} // expected-warning {{'||' of a value and its negation always evaluates to true}}
+  if (!(x) || x) {} // expected-warning {{'||' of a value and its negation always evaluates to true}}
+
+  // don't warn on macros
+  if (COPY(x) || !x) {}
+  if (!x || COPY(x)) {}
+  if (x && COPY(!x)) {}
+  if (COPY(!x && x)) {}
+
+  // dont' warn on literals
+  if (1 || !1) {}
+  if (!42 && 42) {}
+
+
+  // don't warn on overloads
+  struct Foo{
+int val;
+Foo operator!() const { return Foo{!val}; }
+bool operator||(const Foo other) const { return val || other.val; }
+bool operator&&(const Foo other) const { return val && other.val; }
+  };
+
+  Foo f{3};
+  if (f || !f) {}
+  if (!f || f) {}
+  if (f.val || !f.val) {} // expected-warning {{'||' of a value and its negation always evaluates to true}}
+  if (!f.val && f.val) {} // expected-warning {{'&&' of a value and its negation always evaluates to false}}
+}
Index: clang/test/Misc/warning-wall.c
===
--- clang/test/Misc/warning-wall.c
+++ clang/test/Misc/warning-wall.c
@@ -55,6 +55,7 @@
 CHECK-NEXT:  -Wtautological-bitwise-compare
 CHECK-NEXT:  -Wtautological-undefined-compare
 CHECK-NEXT:  -Wtautological-objc-bool-compare
+CHECK-NEXT:  -Wtautological-negation-compare
 CHECK-NEXT:-Wtrigraphs
 CHECK-NEXT:-Wuninitialized
 CHECK-NEXT:  -Wsometimes-uninitialized
Index: clang/test/Analysis/temp-obj-dtors-cfg-output.cpp
===
--- clang/test/Analysis/temp-obj-dtors-cfg-output.cpp
+++ clang/test/Analysis/temp-obj-dtors-cfg-output.cpp
@@ -1393,13 +1393,13 @@
 // CHECK: Succs (2): B2 B1
 // CHECK:   [B4 (NORETURN)]
 // CHECK: 1: ~NoReturn() (Temporary object destructor)
-// CHECK: Preds (1): B5
+// CHECK: Preds (1): B5(Unreachable)
 // CHECK: Succs (1): B0
 // CHECK:   [B5]
 // CHECK: 1: [B8.3] || [B7.2] || [B6.7]
 // CHECK: T: (Temp Dtor) [B6.4]
 // CHECK: Preds (3): B6 B7 B8
-// CHECK: Succs (2): B4 B3
+// CHECK: Succs (2): B4(Unreachable) B3
 // CHECK:   [B6]
 // CHECK: 1: check
 // CHECK: 2: [B6.1] (ImplicitCastExpr, FunctionToPointerDecay, _Bool (*)(const NoReturn &))
Index: clang/lib/Sema/AnalysisBasedWarnings.cpp
===
--- clang/lib/Sema/AnalysisBasedWarnings.cpp
+++ clang/lib/Sema/AnalysisBasedWarnings.cpp
@@ -158,6 +158,17 @@
 re

[PATCH] D157526: [clang][Sema] Remove irrelevant diagnostics from constraint satisfaction failure

2023-08-17 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

Ping


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157526/new/

https://reviews.llvm.org/D157526

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155064: [clang][SemaCXX] Diagnose tautological uses of consteval if and is_constant_evaluated

2023-08-19 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added inline comments.



Comment at: libcxx/include/__type_traits/is_constant_evaluated.h:31
+  return false;
+#endif
 }

Mordante wrote:
> Why is this needed? Does this mean the builtin will fail in C++03 mode?
`__libcpp_is_constant_evaluated` always returns false under C++03 or earlier, 
where constexpr isn't available. This is a fix to run libc++ tests without 
seeing warnings from clang with this patch.
(Live demo: https://godbolt.org/z/oszebM5o5)


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155064/new/

https://reviews.llvm.org/D155064

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155064: [clang][SemaCXX] Diagnose tautological uses of consteval if and is_constant_evaluated

2023-08-19 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet updated this revision to Diff 551744.
hazohelet marked 2 inline comments as done.
hazohelet added a comment.

Address comments from Mark

- Added comment about the need of macro use in libc++ include file.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155064/new/

https://reviews.llvm.org/D155064

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Basic/DiagnosticASTKinds.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/include/clang/Parse/Parser.h
  clang/include/clang/Sema/Sema.h
  clang/lib/AST/ExprConstant.cpp
  clang/lib/Parse/ParseDecl.cpp
  clang/lib/Parse/ParseDeclCXX.cpp
  clang/lib/Parse/ParseExpr.cpp
  clang/lib/Parse/ParseExprCXX.cpp
  clang/lib/Parse/ParseStmt.cpp
  clang/lib/Sema/SemaChecking.cpp
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/lib/Sema/SemaLambda.cpp
  clang/lib/Sema/SemaStmt.cpp
  clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
  clang/test/AST/Interp/builtins.cpp
  clang/test/AST/Interp/if.cpp
  clang/test/AST/Interp/literals.cpp
  clang/test/CXX/expr/expr.const/p2-0x.cpp
  clang/test/CXX/expr/expr.const/p6-2a.cpp
  clang/test/CXX/expr/expr.prim/expr.prim.lambda/p3.cpp
  clang/test/CXX/stmt.stmt/stmt.select/stmt.if/p4.cpp
  clang/test/Parser/pragma-fenv_access.c
  clang/test/SemaCXX/constant-conversion.cpp
  clang/test/SemaCXX/constant-expression-cxx11.cpp
  clang/test/SemaCXX/cxx2a-consteval.cpp
  clang/test/SemaCXX/cxx2b-consteval-if.cpp
  clang/test/SemaCXX/cxx2b-consteval-propagate.cpp
  clang/test/SemaCXX/fixit-tautological-meta-constant.cpp
  clang/test/SemaCXX/vartemplate-lambda.cpp
  clang/test/SemaCXX/warn-constant-evaluated-constexpr.cpp
  clang/test/SemaCXX/warn-tautological-meta-constant.cpp
  clang/test/SemaTemplate/concepts.cpp
  clang/unittests/Support/TimeProfilerTest.cpp
  libcxx/include/__type_traits/is_constant_evaluated.h
  
libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
  
libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp

Index: libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp
===
--- libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp
+++ libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp
@@ -24,7 +24,7 @@
 #else
   // expected-error-re@+1 (static_assert|static assertion)}} failed}}
   static_assert(!std::is_constant_evaluated(), "");
-  // expected-warning@-1 0-1 {{'std::is_constant_evaluated' will always evaluate to 'true' in a manifestly constant-evaluated expression}}
+  // expected-warning-re@-1 0-1 {{'std::is_constant_evaluated' will always evaluate to {{('true' in a manifestly constant-evaluated expression|true in this context)
 #endif
   return 0;
 }
Index: libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
===
--- libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
+++ libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
@@ -93,12 +93,10 @@
 }
 
 int main(int, char**) {
-  if (!std::is_constant_evaluated()) {
-test_containers, std::deque>();
-test_containers, std::vector>();
-test_containers, std::deque>();
-test_containers, std::vector>();
-  }
+  test_containers, std::deque>();
+  test_containers, std::vector>();
+  test_containers, std::deque>();
+  test_containers, std::vector>();
 
   types::for_each(types::forward_iterator_list{}, [] {
 test_join_view();
Index: libcxx/include/__type_traits/is_constant_evaluated.h
===
--- libcxx/include/__type_traits/is_constant_evaluated.h
+++ libcxx/include/__type_traits/is_constant_evaluated.h
@@ -24,7 +24,14 @@
 #endif
 
 _LIBCPP_HIDE_FROM_ABI inline _LIBCPP_CONSTEXPR bool __libcpp_is_constant_evaluated() _NOEXCEPT {
+// __builtin_is_constant_evaluated() in this function always evaluates to false in pre-C++11 mode
+// because this function is not constexpr-qualified.
+// The following macro use clarifies this and avoids warnings from compilers.
+#ifndef _LIBCPP_CXX03_LANG
   return __builtin_is_constant_evaluated();
+#else
+  return false;
+#endif
 }
 
 _LIBCPP_END_NAMESPACE_STD
Index: clang/unittests/Support/TimeProfilerTest.cpp
===
--- clang/unittests/Support/TimeProfilerTest.cpp
+++ clang/unittests/Support/TimeProfilerTest.cpp
@@ -188,7 +188,6 @@
 | EvaluateAsBooleanCondition ()
 | | EvaluateAsRValue ()
 | EvaluateAsInitializer (slow_value)
-| EvaluateAsConstantExpr ()
 | EvaluateAsConstantExpr ()
 | EvaluateAsRValue ()
 | EvaluateAsInitializer (slow_init_list)
Index: clang/test/SemaTemplate/concepts.cpp
==

[PATCH] D157383: [clang][Diagnostics] Provide source range to integer-overflow warnings

2023-08-19 Thread Takuya Shimizu via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG985a72b6b3e7: [clang][Diagnostics] Provide source range to 
integer-overflow warnings (authored by hazohelet).

Changed prior to commit:
  https://reviews.llvm.org/D157383?vs=548149&id=551746#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157383/new/

https://reviews.llvm.org/D157383

Files:
  clang/lib/AST/ExprConstant.cpp
  clang/lib/AST/Interp/Interp.h
  clang/test/Misc/constexpr-source-ranges.cpp


Index: clang/test/Misc/constexpr-source-ranges.cpp
===
--- clang/test/Misc/constexpr-source-ranges.cpp
+++ clang/test/Misc/constexpr-source-ranges.cpp
@@ -34,3 +34,10 @@
 }
 static_assert(ints(1, div(true, false), 2, div(false, true)) == 1, "");
 // CHECK: constexpr-source-ranges.cpp:35:23:{35:23-35:39}
+
+namespace overflow {
+// CHECK:  :{[[@LINE+1]]:9-[[@LINE+1]]:29}:
+int x = -1 + __INT_MAX__ + 2 + 3;
+// CHECK:  :{[[@LINE+1]]:9-[[@LINE+1]]:19}:
+int a = -(1 << 31) + 1;
+}
Index: clang/lib/AST/Interp/Interp.h
===
--- clang/lib/AST/Interp/Interp.h
+++ clang/lib/AST/Interp/Interp.h
@@ -271,7 +271,8 @@
 SmallString<32> Trunc;
 Value.trunc(Result.bitWidth()).toString(Trunc, 10);
 auto Loc = E->getExprLoc();
-S.report(Loc, diag::warn_integer_constant_overflow) << Trunc << Type;
+S.report(Loc, diag::warn_integer_constant_overflow)
+<< Trunc << Type << E->getSourceRange();
 return true;
   } else {
 S.CCEDiag(E, diag::note_constexpr_overflow) << Value << Type;
@@ -478,7 +479,8 @@
 SmallString<32> Trunc;
 NegatedValue.trunc(Result.bitWidth()).toString(Trunc, 10);
 auto Loc = E->getExprLoc();
-S.report(Loc, diag::warn_integer_constant_overflow) << Trunc << Type;
+S.report(Loc, diag::warn_integer_constant_overflow)
+<< Trunc << Type << E->getSourceRange();
 return true;
   }
 
@@ -531,7 +533,8 @@
 SmallString<32> Trunc;
 APResult.trunc(Result.bitWidth()).toString(Trunc, 10);
 auto Loc = E->getExprLoc();
-S.report(Loc, diag::warn_integer_constant_overflow) << Trunc << Type;
+S.report(Loc, diag::warn_integer_constant_overflow)
+<< Trunc << Type << E->getSourceRange();
 return true;
   }
 
Index: clang/lib/AST/ExprConstant.cpp
===
--- clang/lib/AST/ExprConstant.cpp
+++ clang/lib/AST/ExprConstant.cpp
@@ -2798,7 +2798,7 @@
 if (Info.checkingForUndefinedBehavior())
   Info.Ctx.getDiagnostics().Report(E->getExprLoc(),
diag::warn_integer_constant_overflow)
-  << toString(Result, 10) << E->getType();
+  << toString(Result, 10) << E->getType() << E->getSourceRange();
 return HandleOverflow(Info, E, Value, E->getType());
   }
   return true;
@@ -13643,7 +13643,7 @@
   if (Info.checkingForUndefinedBehavior())
 Info.Ctx.getDiagnostics().Report(E->getExprLoc(),
  diag::warn_integer_constant_overflow)
-<< toString(Value, 10) << E->getType();
+<< toString(Value, 10) << E->getType() << E->getSourceRange();
 
   if (!HandleOverflow(Info, E, -Value.extend(Value.getBitWidth() + 1),
   E->getType()))


Index: clang/test/Misc/constexpr-source-ranges.cpp
===
--- clang/test/Misc/constexpr-source-ranges.cpp
+++ clang/test/Misc/constexpr-source-ranges.cpp
@@ -34,3 +34,10 @@
 }
 static_assert(ints(1, div(true, false), 2, div(false, true)) == 1, "");
 // CHECK: constexpr-source-ranges.cpp:35:23:{35:23-35:39}
+
+namespace overflow {
+// CHECK:  :{[[@LINE+1]]:9-[[@LINE+1]]:29}:
+int x = -1 + __INT_MAX__ + 2 + 3;
+// CHECK:  :{[[@LINE+1]]:9-[[@LINE+1]]:19}:
+int a = -(1 << 31) + 1;
+}
Index: clang/lib/AST/Interp/Interp.h
===
--- clang/lib/AST/Interp/Interp.h
+++ clang/lib/AST/Interp/Interp.h
@@ -271,7 +271,8 @@
 SmallString<32> Trunc;
 Value.trunc(Result.bitWidth()).toString(Trunc, 10);
 auto Loc = E->getExprLoc();
-S.report(Loc, diag::warn_integer_constant_overflow) << Trunc << Type;
+S.report(Loc, diag::warn_integer_constant_overflow)
+<< Trunc << Type << E->getSourceRange();
 return true;
   } else {
 S.CCEDiag(E, diag::note_constexpr_overflow) << Value << Type;
@@ -478,7 +479,8 @@
 SmallString<32> Trunc;
 NegatedValue.trunc(Result.bitWidth()).toString(Trunc, 10);
 auto Loc = E->getExprLoc();
-S.report(Loc, diag::warn_integer_constant_overflow) << Trunc << Type;
+S.report(Loc, diag::warn_integer_constant_overflow)
+<< Trunc << Type << E->getSourceRange();
 return true;
   }
 
@@ -531,7 +533,8 @@
 SmallString<32> Trunc;
 APResult.

[PATCH] D155064: [clang][SemaCXX] Diagnose tautological uses of consteval if and is_constant_evaluated

2023-08-19 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet marked an inline comment as done.
hazohelet added inline comments.



Comment at: libcxx/include/__type_traits/is_constant_evaluated.h:31
+  return false;
+#endif
 }

philnik wrote:
> Mordante wrote:
> > hazohelet wrote:
> > > Mordante wrote:
> > > > Why is this needed? Does this mean the builtin will fail in C++03 mode?
> > > `__libcpp_is_constant_evaluated` always returns false under C++03 or 
> > > earlier, where constexpr isn't available. This is a fix to run libc++ 
> > > tests without seeing warnings from clang with this patch.
> > > (Live demo: https://godbolt.org/z/oszebM5o5)
> > I see can you add a comment in that regard? To me it looks like the builtin 
> > fails in C++03, where returning false indeed is always the right answer.
> https://godbolt.org/z/bafeeY7b7 shows that 
> `__builtin_is_constant_evaluated()` doesn't always return false in C++03, so 
> this change seems wrong to me.
This is NFC.
The problem is that `__libcpp_is_constant_evaluated()` has different semantics 
from `__builtin_is_constant_evaluated()` in C++03.
The cause of this tautological-falsity here is whether 
`__libcpp_is_constant_evaluated()` is `constexpr`-qualified or not.
`_LIBCPP_CONSTEXPR` macro expands to `constexpr` since C++11, but in pre-C++11 
mode, it expands to nothing.
So in C++03 mode this function is something like `bool 
__libcpp_is_constant_evaluated() { return __builtin_is_constant_evaluated(); 
}`, where it is tautologically-false because it is not constexpr-qualified.

As you mentioned, `__builtin_is_constant_evaluated` does not change its 
semantics depending on C++ versions.
However, `__libcpp_is_constant_evaluated()` always returns false in C++03 as in 
https://godbolt.org/z/oszebM5o5


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155064/new/

https://reviews.llvm.org/D155064

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155610: [Clang][Sema] Fix display of characters on static assertion failure

2023-08-21 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet updated this revision to Diff 552167.
hazohelet marked an inline comment as done.

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155610/new/

https://reviews.llvm.org/D155610

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/test/Lexer/cxx1z-trigraphs.cpp
  clang/test/SemaCXX/static-assert-cxx26.cpp
  clang/test/SemaCXX/static-assert.cpp

Index: clang/test/SemaCXX/static-assert.cpp
===
--- clang/test/SemaCXX/static-assert.cpp
+++ clang/test/SemaCXX/static-assert.cpp
@@ -268,7 +268,31 @@
 return 'c';
   }
   static_assert(getChar() == 'a', ""); // expected-error {{failed}} \
-   // expected-note {{evaluates to ''c' == 'a''}}
+   // expected-note {{evaluates to ''c' (0x63, 99) == 'a' (0x61, 97)'}}
+  static_assert((char)9 == '\x61', ""); // expected-error {{failed}} \
+// expected-note {{evaluates to ''\t' (0x09, 9) == 'a' (0x61, 97)'}}
+  static_assert((char)10 == '\0', ""); // expected-error {{failed}} \
+   // expected-note {{n' (0x0A, 10) == '' (0x00, 0)'}}
+  // The note above is intended to match "evaluates to '\n' (0x0A, 10) == '' (0x00, 0)'", but if we write it as it is,
+  // the "\n" cannot be consumed by the diagnostic consumer.
+  static_assert((signed char)10 == (char)-123, ""); // expected-error {{failed}} \
+// expected-note {{evaluates to '10 == '<85>' (0x85, -123)'}}
+  static_assert((char)-4 == (unsigned char)-8, ""); // expected-error {{failed}} \
+// expected-note {{evaluates to ''' (0xFC, -4) == 248'}}
+  static_assert((char)-128 == (char)-123, ""); // expected-error {{failed}} \
+   // expected-note {{evaluates to ''<80>' (0x80, -128) == '<85>' (0x85, -123)'}}
+  static_assert('\xA0' == (char)'\x20', ""); // expected-error {{failed}} \
+ // expected-note {{evaluates to ''' (0xA0, -96) == ' ' (0x20, 32)'}}
+  static_assert((char16_t)L'ゆ' == L"C̵̭̯̠̎͌ͅť̺"[1], ""); // expected-error {{failed}} \
+  // expected-note {{evaluates to 'u'ゆ' (0x3086, 12422) == L'̵' (0x335, 821)'}}
+  static_assert(L"\/"[1] == u'\xFFFD', ""); // expected-error {{failed}} \
+  // expected-note {{evaluates to 'L'/' (0xFF0F, 65295) == u'�' (0xFFFD, 65533)'}}
+  static_assert(L"⚾"[0] == U'🌍', ""); // expected-error {{failed}} \
+ // expected-note {{evaluates to 'L'⚾' (0x26BE, 9918) == U'🌍' (0x1F30D, 127757)'}}
+  static_assert(U"\a"[0] == (wchar_t)9, ""); // expected-error {{failed}} \
+ // expected-note {{evaluates to 'U'\a' (0x07, 7) == L'\t' (0x09, 9)'}}
+  static_assert(L"§"[0] == U'Ö', ""); // expected-error {{failed}} \
+  // expected-note {{evaluates to 'L'§' (0xA7, 167) == U'Ö' (0xD6, 214)'}}
 
   /// Bools are printed as bools.
   constexpr bool invert(bool b) {
Index: clang/test/SemaCXX/static-assert-cxx26.cpp
===
--- clang/test/SemaCXX/static-assert-cxx26.cpp
+++ clang/test/SemaCXX/static-assert-cxx26.cpp
@@ -298,3 +298,12 @@
 Bad b; // expected-note {{in instantiation}}
 
 }
+
+namespace EscapeInDiagnostic {
+static_assert('\u{9}' == (char)1, ""); // expected-error {{failed}} \
+   // expected-note {{evaluates to ''\t' (0x09, 9) == '' (0x01, 1)'}}
+static_assert((char8_t)-128 == (char8_t)-123, ""); // expected-error {{failed}} \
+   // expected-note {{evaluates to 'u8'<80>' (0x80, 128) == u8'<85>' (0x85, 133)'}}
+static_assert((char16_t)0xFEFF == (char16_t)0xDB93, ""); // expected-error {{failed}} \
+ // expected-note {{evaluates to 'u'' (0xFEFF, 65279) == u'\xDB93' (0xDB93, 56211)'}}
+}
Index: clang/test/Lexer/cxx1z-trigraphs.cpp
===
--- clang/test/Lexer/cxx1z-trigraphs.cpp
+++ clang/test/Lexer/cxx1z-trigraphs.cpp
@@ -21,7 +21,7 @@
 
 #if !ENABLED_TRIGRAPHS
 // expected-error@11 {{}} expected-warning@11 {{trigraph ignored}}
-// expected-error@13 {{failed}} expected-warning@13 {{trigraph ignored}} expected-note@13 {{evaluates to ''?' == '#''}}
+// expected-error@13 {{failed}} expected-warning@13 {{trigraph ignored}} expected-note@13 {{evaluates to ''?' (0x3F, 63) == '#' (0x23, 35)'}}
 // expected-error@16 {{}}
 // expected-error@20 {{}}
 #else
Index: clang/lib/Sema/SemaDeclCXX.cpp
===
--

[PATCH] D158472: [clang][Diagnostics] Emit fix-it hint separately on overload resolution failure

2023-08-21 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet created this revision.
hazohelet added reviewers: aaron.ballman, cjdb, tbaeder, shafik.
Herald added a subscriber: arphaman.
Herald added a project: All.
hazohelet requested review of this revision.
Herald added a project: clang.

D153359  addressed the distant source ranges 
in overload resolution failure, but still clang generates distant source 
locations when emitting fix-it hint about the addition/deletion of `*` and `&` 
operators.
This patch separates the fix-it emission to another note to resolve this issue.
Code Example:

  void f(int n);
  
  
  void g(int *p) { f(p); }

Before:

  source:4:18: error: no matching function for call to 'f'
  4 | void g(int *p) { f(p); }
|  ^
  source:1:6: note: candidate function not viable: no known conversion from 
'int *' to 'int' for 1st argument; dereference the argument with *
  1 | void f(int n);
|  ^ ~
  2 |
  3 |
  4 | void g(int *p) { f(p); }
|
|*

After:

  source:4:18: error: no matching function for call to 'f'
  4 | void g(int *p) { f(p); }
|  ^
  source:1:6: note: candidate function not viable: no known conversion from 
'int *' to 'int' for 1st argument
  1 | void f(int n);
|  ^ ~
  source:4:20: note: dereference the argument with *
  4 | void g(int *p) { f(p); }
|^
|*


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D158472

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Sema/SemaOverload.cpp
  clang/test/FixIt/fixit-function-call.cpp
  clang/test/Index/fixit-sysheader-test.cpp
  clang/test/Sema/overloadable.c
  clang/test/SemaCXX/MicrosoftExtensions.cpp
  clang/test/SemaCXX/builtin-operator-new-delete.cpp
  clang/test/SemaCXX/crashes.cpp
  clang/test/SemaCXX/for-range-dereference.cpp
  clang/test/SemaCXX/overload-member-call.cpp
  clang/test/SemaOpenCL/fdeclare-opencl-builtins.cl
  clang/test/SemaTemplate/instantiate-expr-4.cpp

Index: clang/test/SemaTemplate/instantiate-expr-4.cpp
===
--- clang/test/SemaTemplate/instantiate-expr-4.cpp
+++ clang/test/SemaTemplate/instantiate-expr-4.cpp
@@ -94,7 +94,8 @@
 template
 struct New2 {
   T* f(bool x, Arg1 a1, Arg2 a2) {
-return new T(a1, a2); // expected-error{{no matching}}
+return new T(a1, a2); // expected-error{{no matching}} \
+  // expected-note{{dereference the argument with *}}
   }
 };
 
Index: clang/test/SemaOpenCL/fdeclare-opencl-builtins.cl
===
--- clang/test/SemaOpenCL/fdeclare-opencl-builtins.cl
+++ clang/test/SemaOpenCL/fdeclare-opencl-builtins.cl
@@ -175,10 +175,12 @@
   // expected-error@-1{{no matching function for call to 'atomic_init'}}
 #if defined(NO_FP64)
   // Expecting 5 candidates: int, uint, long, ulong, float
-  // expected-note@-4 5 {{candidate function not viable: no known conversion}}
+  // expected-note@-4 5 {{candidate function not viable: no known conversion}} \
+  // expected-note@-4 5{{dereference the argument with *}}
 #else
   // Expecting 6 candidates: int, uint, long, ulong, float, double
-  // expected-note@-7 6 {{candidate function not viable: no known conversion}}
+  // expected-note@-8 6 {{candidate function not viable: no known conversion}} \
+  // expected-note@-8 6{{dereference the argument with *}}
 #endif
 }
 
Index: clang/test/SemaCXX/overload-member-call.cpp
===
--- clang/test/SemaCXX/overload-member-call.cpp
+++ clang/test/SemaCXX/overload-member-call.cpp
@@ -124,6 +124,7 @@
 void member_call_op_template(int *p) {
   // Ensure that we don't get confused about relative parameter / argument
   // indexing here.
-  [](int, int, auto...){}(p, p); // expected-error {{no matching function}} expected-note {{no known conversion}}
+  [](int, int, auto...){}(p, p); // expected-error {{no matching function}} expected-note {{no known conversion}} \
+ // expected-note {{dereference the argument with *}}
 }
 
Index: clang/test/SemaCXX/for-range-dereference.cpp
===
--- clang/test/SemaCXX/for-range-dereference.cpp
+++ clang/test/SemaCXX/for-range-dereference.cpp
@@ -56,7 +56,8 @@
   ADLNoEnd ANE;
   for (auto i : ANE) { } // expected-error{{invalid range expression of type 'ADLNoEnd'; no viable 'end' function available}}
   ADLNoEnd *pANE;
-  for (auto i : pANE) { } // expected-error{{invalid range expression of type 'ADLNoEnd *'; no viable 'begin' function available}}
+  for (auto i : pANE) { } // expected-error{{invalid range expression of type 'ADLNoEnd *'; no viable 'begin' function available}} \
+  // expected-note 

[PATCH] D158472: [clang][Diagnostics] Emit fix-it hint separately on overload resolution failure

2023-08-21 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

This breaks the one-note-for-one-overload-candidate rule of overload resolution 
failure diagnostics 
(https://github.com/llvm/llvm-project/blob/ff08c8e57e39d7970b65637595cdc221901f4ed1/clang/lib/Sema/SemaOverload.cpp#L11517-L11526),
 but in most cases this change would produce less lines of diagnostics.
If we don't like this additional note, we could instead suppress the fix-it 
diagnostics when the fix-it location and the diagnosed candidate location are 
not close (for example, more than 2 lines away).


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D158472/new/

https://reviews.llvm.org/D158472

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D158472: [clang][Diagnostics] Emit fix-it hint separately on overload resolution failure

2023-08-22 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet updated this revision to Diff 552417.
hazohelet added a comment.
Herald added a subscriber: kadircet.
Herald added a project: clang-tools-extra.

Fixed clangd test


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D158472/new/

https://reviews.llvm.org/D158472

Files:
  clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Sema/SemaOverload.cpp
  clang/test/FixIt/fixit-function-call.cpp
  clang/test/Index/fixit-sysheader-test.cpp
  clang/test/Sema/overloadable.c
  clang/test/SemaCXX/MicrosoftExtensions.cpp
  clang/test/SemaCXX/builtin-operator-new-delete.cpp
  clang/test/SemaCXX/crashes.cpp
  clang/test/SemaCXX/for-range-dereference.cpp
  clang/test/SemaCXX/overload-member-call.cpp
  clang/test/SemaOpenCL/fdeclare-opencl-builtins.cl
  clang/test/SemaTemplate/instantiate-expr-4.cpp

Index: clang/test/SemaTemplate/instantiate-expr-4.cpp
===
--- clang/test/SemaTemplate/instantiate-expr-4.cpp
+++ clang/test/SemaTemplate/instantiate-expr-4.cpp
@@ -94,7 +94,8 @@
 template
 struct New2 {
   T* f(bool x, Arg1 a1, Arg2 a2) {
-return new T(a1, a2); // expected-error{{no matching}}
+return new T(a1, a2); // expected-error{{no matching}} \
+  // expected-note{{dereference the argument with *}}
   }
 };
 
Index: clang/test/SemaOpenCL/fdeclare-opencl-builtins.cl
===
--- clang/test/SemaOpenCL/fdeclare-opencl-builtins.cl
+++ clang/test/SemaOpenCL/fdeclare-opencl-builtins.cl
@@ -175,10 +175,12 @@
   // expected-error@-1{{no matching function for call to 'atomic_init'}}
 #if defined(NO_FP64)
   // Expecting 5 candidates: int, uint, long, ulong, float
-  // expected-note@-4 5 {{candidate function not viable: no known conversion}}
+  // expected-note@-4 5 {{candidate function not viable: no known conversion}} \
+  // expected-note@-4 5{{dereference the argument with *}}
 #else
   // Expecting 6 candidates: int, uint, long, ulong, float, double
-  // expected-note@-7 6 {{candidate function not viable: no known conversion}}
+  // expected-note@-8 6 {{candidate function not viable: no known conversion}} \
+  // expected-note@-8 6{{dereference the argument with *}}
 #endif
 }
 
Index: clang/test/SemaCXX/overload-member-call.cpp
===
--- clang/test/SemaCXX/overload-member-call.cpp
+++ clang/test/SemaCXX/overload-member-call.cpp
@@ -124,6 +124,7 @@
 void member_call_op_template(int *p) {
   // Ensure that we don't get confused about relative parameter / argument
   // indexing here.
-  [](int, int, auto...){}(p, p); // expected-error {{no matching function}} expected-note {{no known conversion}}
+  [](int, int, auto...){}(p, p); // expected-error {{no matching function}} expected-note {{no known conversion}} \
+ // expected-note {{dereference the argument with *}}
 }
 
Index: clang/test/SemaCXX/for-range-dereference.cpp
===
--- clang/test/SemaCXX/for-range-dereference.cpp
+++ clang/test/SemaCXX/for-range-dereference.cpp
@@ -56,7 +56,8 @@
   ADLNoEnd ANE;
   for (auto i : ANE) { } // expected-error{{invalid range expression of type 'ADLNoEnd'; no viable 'end' function available}}
   ADLNoEnd *pANE;
-  for (auto i : pANE) { } // expected-error{{invalid range expression of type 'ADLNoEnd *'; no viable 'begin' function available}}
+  for (auto i : pANE) { } // expected-error{{invalid range expression of type 'ADLNoEnd *'; no viable 'begin' function available}} \
+  // expected-note {{dereference the argument with *}}
 
   DeletedEnd DE;
   for (auto i : DE) { } // expected-error{{attempt to use a deleted function}} \
Index: clang/test/SemaCXX/crashes.cpp
===
--- clang/test/SemaCXX/crashes.cpp
+++ clang/test/SemaCXX/crashes.cpp
@@ -44,7 +44,8 @@
 struct {
   Y obj;
 } objs[] = {
-  new Y // expected-error{{no viable conversion}}
+  new Y // expected-error{{no viable conversion}} \
+// expected-note{{dereference the argument with *}}
 };
 }
 
Index: clang/test/SemaCXX/builtin-operator-new-delete.cpp
===
--- clang/test/SemaCXX/builtin-operator-new-delete.cpp
+++ clang/test/SemaCXX/builtin-operator-new-delete.cpp
@@ -138,8 +138,10 @@
 
 void test_no_matching_fn() {
   Tag<1> tag;
-  __builtin_operator_new(42, tag);// expected-error {{no matching function for call to 'operator new'}}
-  __builtin_operator_delete(NP, tag); // expected-error {{no matching function for call to 'operator delete'}}
+  __builtin_operator_new(42, tag);// expected-error {{no matching function for call to 'operator new'}} \
+  // expected-note {

[PATCH] D158562: [clang][Sema] Add truncation warning on fortified snprintf

2023-08-22 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet created this revision.
hazohelet added reviewers: aaron.ballman, serge-sans-paille, nickdesaulniers, 
tbaeder.
Herald added a project: All.
hazohelet requested review of this revision.
Herald added a project: clang.

This patch warns on `snprintf` calls whose `n` argument is known to be smaller 
than the size of the formatted string like

  char buf[5];
  snprintf(buf, 5, "Hello");

This is a counterpart of gcc's `Wformat-truncation`
Fixes https://github.com/llvm/llvm-project/issues/64871


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D158562

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Sema/SemaChecking.cpp
  clang/test/Analysis/taint-generic.c
  clang/test/Sema/format-strings.c
  clang/test/Sema/warn-fortify-source.c

Index: clang/test/Sema/warn-fortify-source.c
===
--- clang/test/Sema/warn-fortify-source.c
+++ clang/test/Sema/warn-fortify-source.c
@@ -87,6 +87,10 @@
   char buf[10];
   __builtin_snprintf(buf, 10, "merp");
   __builtin_snprintf(buf, 11, "merp"); // expected-warning {{'snprintf' size argument is too large; destination buffer has size 10, but size argument is 11}}
+  __builtin_snprintf(buf, 0, "merp");
+  __builtin_snprintf(buf, 3, "merp"); // expected-warning {{'snprintf' will always be truncated; specified size is 3, but format string expands to at least 5}}
+  __builtin_snprintf(buf, 4, "merp"); // expected-warning {{'snprintf' will always be truncated; specified size is 4, but format string expands to at least 5}}
+  __builtin_snprintf(buf, 5, "merp");
 }
 
 void call_vsnprintf(void) {
@@ -94,6 +98,10 @@
   __builtin_va_list list;
   __builtin_vsnprintf(buf, 10, "merp", list);
   __builtin_vsnprintf(buf, 11, "merp", list); // expected-warning {{'vsnprintf' size argument is too large; destination buffer has size 10, but size argument is 11}}
+  __builtin_vsnprintf(buf, 0, "merp", list);
+  __builtin_vsnprintf(buf, 3, "merp", list); // expected-warning {{'vsnprintf' will always be truncated; specified size is 3, but format string expands to at least 5}}
+  __builtin_vsnprintf(buf, 4, "merp", list); // expected-warning {{'vsnprintf' will always be truncated; specified size is 4, but format string expands to at least 5}}
+  __builtin_vsnprintf(buf, 5, "merp", list);
 }
 
 void call_sprintf_chk(char *buf) {
Index: clang/test/Sema/format-strings.c
===
--- clang/test/Sema/format-strings.c
+++ clang/test/Sema/format-strings.c
@@ -202,7 +202,8 @@
   printf("%s%lv%d","unix",10,20); // expected-warning {{invalid conversion specifier 'v'}} expected-warning {{data argument not used by format string}}
   fprintf(fp,"%%%l"); // expected-warning {{incomplete format specifier}}
   sprintf(buf,"%ld%d%d", 1, 2, 3); // expected-warning{{format specifies type 'long' but the argument has type 'int'}}
-  snprintf(buf, 2, "%ld%;%d", 1, 2, 3); // expected-warning{{format specifies type 'long' but the argument has type 'int'}} expected-warning {{invalid conversion specifier ';'}} expected-warning {{data argument not used by format string}}
+  snprintf(buf, 2, "%ld%;%d", 1, 2, 3); // expected-warning{{format specifies type 'long' but the argument has type 'int'}} expected-warning {{invalid conversion specifier ';'}} expected-warning {{data argument not used by format string}} \
+// expected-warning{{'snprintf' will always be truncated; specified size is 2, but format string expands to at least 7}}
 }
 
 void check_null_char_string(char* b)
Index: clang/test/Analysis/taint-generic.c
===
--- clang/test/Analysis/taint-generic.c
+++ clang/test/Analysis/taint-generic.c
@@ -229,6 +229,7 @@
   char addr[128];
   scanf("%s", addr);
   __builtin_snprintf(buffern, 10, "/bin/mail %s < /tmp/email", addr);
+  // expected-warning@-1 {{'snprintf' will always be truncated; specified size is 10, but format string expands to at least 24}}
   system(buffern); // expected-warning {{Untrusted data is passed to a system call}}
 }
 
Index: clang/lib/Sema/SemaChecking.cpp
===
--- clang/lib/Sema/SemaChecking.cpp
+++ clang/lib/Sema/SemaChecking.cpp
@@ -1131,6 +1131,24 @@
 // Add 1 for null byte.
 return llvm::APSInt::getUnsigned(Result + 1).extOrTrunc(SizeTypeWidth);
   };
+  auto ProcessFormatStringLiteral =
+  [&](const Expr *FormatExpr, StringRef &FormatStrRef, size_t &StrLen) {
+if (auto *Format = dyn_cast(FormatExpr)) {
+  if (!Format->isOrdinary() && !Format->isUTF8())
+return false;
+
+  FormatStrRef = Format->getString();
+  const ConstantArrayType *T =
+  Context.getAsConstantArrayType(Format->getType());
+  assert(T && "String literal not of constant array type!")

[PATCH] D152093: [clang][Analysis] Handle && and || against variable and its negation as tautology

2023-07-28 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet updated this revision to Diff 545213.
hazohelet added a comment.

Address comments from Aaron

- Reworded diagnostic message


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D152093/new/

https://reviews.llvm.org/D152093

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Analysis/CFG.h
  clang/include/clang/Basic/DiagnosticGroups.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Analysis/CFG.cpp
  clang/lib/Sema/AnalysisBasedWarnings.cpp
  clang/test/Analysis/temp-obj-dtors-cfg-output.cpp
  clang/test/Misc/warning-wall.c
  clang/test/SemaCXX/tautological-negation-compare.cpp
  clang/test/SemaCXX/warn-infinite-recursion.cpp

Index: clang/test/SemaCXX/warn-infinite-recursion.cpp
===
--- clang/test/SemaCXX/warn-infinite-recursion.cpp
+++ clang/test/SemaCXX/warn-infinite-recursion.cpp
@@ -203,3 +203,13 @@
   (void)typeid(unevaluated_recursive_function());
   return 0;
 }
+
+void func1(int i) { // expected-warning {{call itself}}
+  if (i || !i)
+func1(i);
+}
+void func2(int i) { // expected-warning {{call itself}}
+  if (!i && i) {}
+  else
+func2(i);
+}
Index: clang/test/SemaCXX/tautological-negation-compare.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/tautological-negation-compare.cpp
@@ -0,0 +1,41 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wtautological-negation-compare -Wno-constant-logical-operand %s
+// RUN: %clang_cc1 -fsyntax-only -verify -Wtautological-compare -Wno-constant-logical-operand %s
+// RUN: %clang_cc1 -fsyntax-only -verify -Wall -Wno-unused -Wno-loop-analysis -Wno-constant-logical-operand %s
+
+#define COPY(x) x
+
+void test_int(int x) {
+  if (x || !x) {} // expected-warning {{'||' of a value and its negation always evaluates to true}}
+  if (!x || x) {} // expected-warning {{'||' of a value and its negation always evaluates to true}}
+  if (x && !x) {} // expected-warning {{'&&' of a value and its negation always evaluates to false}}
+  if (!x && x) {} // expected-warning {{'&&' of a value and its negation always evaluates to false}}
+
+  // parentheses are ignored
+  if (x || (!x)) {} // expected-warning {{'||' of a value and its negation always evaluates to true}}
+  if (!(x) || x) {} // expected-warning {{'||' of a value and its negation always evaluates to true}}
+
+  // don't warn on macros
+  if (COPY(x) || !x) {}
+  if (!x || COPY(x)) {}
+  if (x && COPY(!x)) {}
+  if (COPY(!x && x)) {}
+
+  // dont' warn on literals
+  if (1 || !1) {}
+  if (!42 && 42) {}
+
+
+  // don't warn on overloads
+  struct Foo{
+int val;
+Foo operator!() const { return Foo{!val}; }
+bool operator||(const Foo other) const { return val || other.val; }
+bool operator&&(const Foo other) const { return val && other.val; }
+  };
+
+  Foo f{3};
+  if (f || !f) {}
+  if (!f || f) {}
+  if (f.val || !f.val) {} // expected-warning {{'||' of a value and its negation always evaluates to true}}
+  if (!f.val && f.val) {} // expected-warning {{'&&' of a value and its negation always evaluates to false}}
+}
Index: clang/test/Misc/warning-wall.c
===
--- clang/test/Misc/warning-wall.c
+++ clang/test/Misc/warning-wall.c
@@ -55,6 +55,7 @@
 CHECK-NEXT:  -Wtautological-bitwise-compare
 CHECK-NEXT:  -Wtautological-undefined-compare
 CHECK-NEXT:  -Wtautological-objc-bool-compare
+CHECK-NEXT:  -Wtautological-negation-compare
 CHECK-NEXT:-Wtrigraphs
 CHECK-NEXT:-Wuninitialized
 CHECK-NEXT:  -Wsometimes-uninitialized
Index: clang/test/Analysis/temp-obj-dtors-cfg-output.cpp
===
--- clang/test/Analysis/temp-obj-dtors-cfg-output.cpp
+++ clang/test/Analysis/temp-obj-dtors-cfg-output.cpp
@@ -1393,13 +1393,13 @@
 // CHECK: Succs (2): B2 B1
 // CHECK:   [B4 (NORETURN)]
 // CHECK: 1: ~NoReturn() (Temporary object destructor)
-// CHECK: Preds (1): B5
+// CHECK: Preds (1): B5(Unreachable)
 // CHECK: Succs (1): B0
 // CHECK:   [B5]
 // CHECK: 1: [B8.3] || [B7.2] || [B6.7]
 // CHECK: T: (Temp Dtor) [B6.4]
 // CHECK: Preds (3): B6 B7 B8
-// CHECK: Succs (2): B4 B3
+// CHECK: Succs (2): B4(Unreachable) B3
 // CHECK:   [B6]
 // CHECK: 1: check
 // CHECK: 2: [B6.1] (ImplicitCastExpr, FunctionToPointerDecay, _Bool (*)(const NoReturn &))
Index: clang/lib/Sema/AnalysisBasedWarnings.cpp
===
--- clang/lib/Sema/AnalysisBasedWarnings.cpp
+++ clang/lib/Sema/AnalysisBasedWarnings.cpp
@@ -158,6 +158,17 @@
 return false;
   }
 
+  void logicAlwaysTrue(const BinaryOperator *B, bool isAlwaysTrue) override {
+if (HasMacroID(B))
+  return;
+
+unsigned DiagID = isAlwaysTrue
+  ? diag::warn_tautological_negation_or_compare
+  : diag::warn_tautol

[PATCH] D152093: [clang][Analysis] Handle && and || against variable and its negation as tautology

2023-07-28 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet marked 2 inline comments as done.
hazohelet added a comment.

Thanks for the review




Comment at: clang/include/clang/Basic/DiagnosticSemaKinds.td:9743-9748
+def warn_tautological_negation_and_compare: Warning<
+  "'&&' against a variable and its negation always evaluates to false">,
+  InGroup, DefaultIgnore;
+def warn_tautological_negation_or_compare: Warning<
+  "'||' against a variable and its negation always evaluates to true">,
+  InGroup, DefaultIgnore;

aaron.ballman wrote:
> Let's combine these if we can. I'm not 100% certain that `%select` is going 
> to like `&&|||` though, so if there's an issue, separate is also fine.
`%select(&&|||)0` doesn't seem to work as we want it to, so I kept it separated


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D152093/new/

https://reviews.llvm.org/D152093

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D154366: [clang][ExprConstant] Print template arguments when describing stack frame

2023-07-28 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet updated this revision to Diff 545216.
hazohelet added a comment.

Added missing tests for method function templates before landing this


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D154366/new/

https://reviews.llvm.org/D154366

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/AST/ExprConstant.cpp
  clang/test/AST/Interp/literals.cpp
  clang/test/SemaCXX/builtin-align-cxx.cpp
  clang/test/SemaCXX/constant-expression-cxx11.cpp
  clang/test/SemaCXX/constant-expression-cxx14.cpp
  clang/test/SemaCXX/constexpr-builtin-bit-cast.cpp
  clang/test/SemaCXX/constexpr-frame-describe.cpp
  clang/test/SemaCXX/cxx2a-constexpr-dynalloc-limits.cpp
  clang/test/SemaCXX/cxx2b-consteval-propagate.cpp

Index: clang/test/SemaCXX/cxx2b-consteval-propagate.cpp
===
--- clang/test/SemaCXX/cxx2b-consteval-propagate.cpp
+++ clang/test/SemaCXX/cxx2b-consteval-propagate.cpp
@@ -45,7 +45,7 @@
 }
 
 int i = hh(); // expected-error {{call to immediate function 'examples::hh' is not a constant expression}} \
-   // expected-note {{in call to 'hh()'}}
+   // expected-note {{in call to 'hh()'}}
 
 struct A {
   int x;
@@ -180,7 +180,7 @@
 
 void test_runtime() {
 (void)immediate(0); // expected-error {{call to immediate function 'Aggregate::immediate' is not a constant expression}} \
-// expected-note {{in call to 'immediate(0)'}}
+// expected-note {{in call to 'immediate(0)'}}
 }
 consteval int f(int i) {
 return i;
Index: clang/test/SemaCXX/cxx2a-constexpr-dynalloc-limits.cpp
===
--- clang/test/SemaCXX/cxx2a-constexpr-dynalloc-limits.cpp
+++ clang/test/SemaCXX/cxx2a-constexpr-dynalloc-limits.cpp
@@ -88,11 +88,11 @@
 void ohno() {
   int bar[stack_array<1024>()];
   int foo[stack_array<1025>()]; // expected-warning {{variable length arrays are a C99 feature}} \
-// expected-note {{in call to 'stack_array()'}}
+// expected-note {{in call to 'stack_array<1025>()'}}
 
   constexpr int foo[stack_array<1025>()]; // expected-warning {{variable length arrays are a C99 feature}} \
   // expected-error {{constexpr variable cannot have non-literal type 'const int[stack_array<1025>()]'}} \
-  // expected-note {{in call to 'stack_array()'}}
+  // expected-note {{in call to 'stack_array<1025>()'}}
 }
 
 }
Index: clang/test/SemaCXX/constexpr-frame-describe.cpp
===
--- clang/test/SemaCXX/constexpr-frame-describe.cpp
+++ clang/test/SemaCXX/constexpr-frame-describe.cpp
@@ -58,3 +58,24 @@
 constexpr D d{};
 static_assert(d.c->b.a->foo() == 1); // expected-error {{constant expression}} \
 expected-note {{in call to 'd.c->b.a->foo()'}}
+
+template 
+struct Bar {
+  template 
+  constexpr int fail1() const { return 1 / 0; } // expected-warning {{division by zero}} \
+// expected-note {{division by zero}}
+  template 
+  constexpr int fail2() const { return 1 / 0; } // expected-warning {{division by zero}} \
+// expected-note {{division by zero}}
+  template 
+  constexpr int fail3(Args... args) const { return 1 / 0; } // expected-warning {{division by zero}} \
+// expected-note {{division by zero}}
+};
+
+constexpr Bar bar;
+static_assert(bar.fail1()); // expected-error {{constant expression}} \
+ // expected-note {{in call to 'bar.fail1()'}}
+static_assert(bar.fail2()); // expected-error {{constant expression}} \
+  // expected-note {{in call to 'bar.fail2()'}}
+static_assert(bar.fail3(3, 4UL, bar, &bar)); // expected-error {{constant expression}} \
+ // expected-note {{in call to 'bar.fail3, const Bar *>(3, 4, {}, &bar)'}}
Index: clang/test/SemaCXX/constexpr-builtin-bit-cast.cpp
===
--- clang/test/SemaCXX/constexpr-builtin-bit-cast.cpp
+++ clang/test/SemaCXX/constexpr-builtin-bit-cast.cpp
@@ -117,11 +117,11 @@
 
   constexpr pad pir{4, 4};
   // expected-error@+2 {{constexpr variable 'piw' must be initialized by a constant expression}}
-  // expected-note@+1 {{in call to 'bit_cast(pir)'}}
+  // expected-note@+1 {{in call to 'bit_cast(pir)'}}
   constexpr int piw = bit_cast(pir).x;
 
   // expected-error@+2 {{constexpr variable 'bad' must be initialized by a constant expression}}
-  // expected-note@+1 {{in call to 'bit_cast(pir)'}}
+  // expected-note@+1 {{in call to 'bit_cast(pir)'}}
   constexpr no_pad bad = bit_cast(pir);
 
   

[PATCH] D154366: [clang][ExprConstant] Print template arguments when describing stack frame

2023-07-31 Thread Takuya Shimizu via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGe90f4fc6acaf: [clang][ExprConstant] Print template arguments 
when describing stack frame (authored by hazohelet).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D154366/new/

https://reviews.llvm.org/D154366

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/AST/ExprConstant.cpp
  clang/test/AST/Interp/literals.cpp
  clang/test/SemaCXX/builtin-align-cxx.cpp
  clang/test/SemaCXX/constant-expression-cxx11.cpp
  clang/test/SemaCXX/constant-expression-cxx14.cpp
  clang/test/SemaCXX/constexpr-builtin-bit-cast.cpp
  clang/test/SemaCXX/constexpr-frame-describe.cpp
  clang/test/SemaCXX/cxx2a-constexpr-dynalloc-limits.cpp
  clang/test/SemaCXX/cxx2b-consteval-propagate.cpp

Index: clang/test/SemaCXX/cxx2b-consteval-propagate.cpp
===
--- clang/test/SemaCXX/cxx2b-consteval-propagate.cpp
+++ clang/test/SemaCXX/cxx2b-consteval-propagate.cpp
@@ -45,7 +45,7 @@
 }
 
 int i = hh(); // expected-error {{call to immediate function 'examples::hh' is not a constant expression}} \
-   // expected-note {{in call to 'hh()'}}
+   // expected-note {{in call to 'hh()'}}
 
 struct A {
   int x;
@@ -180,7 +180,7 @@
 
 void test_runtime() {
 (void)immediate(0); // expected-error {{call to immediate function 'Aggregate::immediate' is not a constant expression}} \
-// expected-note {{in call to 'immediate(0)'}}
+// expected-note {{in call to 'immediate(0)'}}
 }
 consteval int f(int i) {
 return i;
Index: clang/test/SemaCXX/cxx2a-constexpr-dynalloc-limits.cpp
===
--- clang/test/SemaCXX/cxx2a-constexpr-dynalloc-limits.cpp
+++ clang/test/SemaCXX/cxx2a-constexpr-dynalloc-limits.cpp
@@ -88,11 +88,11 @@
 void ohno() {
   int bar[stack_array<1024>()];
   int foo[stack_array<1025>()]; // expected-warning {{variable length arrays are a C99 feature}} \
-// expected-note {{in call to 'stack_array()'}}
+// expected-note {{in call to 'stack_array<1025>()'}}
 
   constexpr int foo[stack_array<1025>()]; // expected-warning {{variable length arrays are a C99 feature}} \
   // expected-error {{constexpr variable cannot have non-literal type 'const int[stack_array<1025>()]'}} \
-  // expected-note {{in call to 'stack_array()'}}
+  // expected-note {{in call to 'stack_array<1025>()'}}
 }
 
 }
Index: clang/test/SemaCXX/constexpr-frame-describe.cpp
===
--- clang/test/SemaCXX/constexpr-frame-describe.cpp
+++ clang/test/SemaCXX/constexpr-frame-describe.cpp
@@ -58,3 +58,24 @@
 constexpr D d{};
 static_assert(d.c->b.a->foo() == 1); // expected-error {{constant expression}} \
 expected-note {{in call to 'd.c->b.a->foo()'}}
+
+template 
+struct Bar {
+  template 
+  constexpr int fail1() const { return 1 / 0; } // expected-warning {{division by zero}} \
+// expected-note {{division by zero}}
+  template 
+  constexpr int fail2() const { return 1 / 0; } // expected-warning {{division by zero}} \
+// expected-note {{division by zero}}
+  template 
+  constexpr int fail3(Args... args) const { return 1 / 0; } // expected-warning {{division by zero}} \
+// expected-note {{division by zero}}
+};
+
+constexpr Bar bar;
+static_assert(bar.fail1()); // expected-error {{constant expression}} \
+ // expected-note {{in call to 'bar.fail1()'}}
+static_assert(bar.fail2()); // expected-error {{constant expression}} \
+  // expected-note {{in call to 'bar.fail2()'}}
+static_assert(bar.fail3(3, 4UL, bar, &bar)); // expected-error {{constant expression}} \
+ // expected-note {{in call to 'bar.fail3, const Bar *>(3, 4, {}, &bar)'}}
Index: clang/test/SemaCXX/constexpr-builtin-bit-cast.cpp
===
--- clang/test/SemaCXX/constexpr-builtin-bit-cast.cpp
+++ clang/test/SemaCXX/constexpr-builtin-bit-cast.cpp
@@ -117,11 +117,11 @@
 
   constexpr pad pir{4, 4};
   // expected-error@+2 {{constexpr variable 'piw' must be initialized by a constant expression}}
-  // expected-note@+1 {{in call to 'bit_cast(pir)'}}
+  // expected-note@+1 {{in call to 'bit_cast(pir)'}}
   constexpr int piw = bit_cast(pir).x;
 
   // expected-error@+2 {{constexpr variable 'bad' must be initialized by a constant expression}

[PATCH] D155610: [Clang][ExprConstant] Print integer instead of character on static assertion failure

2023-07-31 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

Thanks everyone for the comments!




Comment at: clang/test/Lexer/cxx1z-trigraphs.cpp:24
 // expected-error@11 {{}} expected-warning@11 {{trigraph ignored}}
-// expected-error@13 {{failed}} expected-warning@13 {{trigraph ignored}} 
expected-note@13 {{evaluates to ''?' == '#''}}
+// expected-error@13 {{failed}} expected-warning@13 {{trigraph ignored}} 
expected-note@13 {{evaluates to '63 == 35'}}
 // expected-error@16 {{}}

cor3ntin wrote:
> tahonermann wrote:
> > aaron.ballman wrote:
> > > I think the original diagnostic was actually more understandable as it 
> > > relates more closely to what's written in the static assertion. I could 
> > > imagine something like `evaluates to '?' (63) == '#' (35)` would also be 
> > > reasonable.
> > I agree. I would also be ok with printing the integer value as primary with 
> > the character as secondary:
> >   evaluates to 63 ('?') == 35 ('#')
> > 
> > There are two kinds of non-printable characters:
> >   # Control characters (including new-line)
> >   # character values that don't correspond to a character (e.g., lone 
> > trailing characters or invalid code unit values).
> > For the first case, I would support printing them as either C escapes or 
> > universal-character-names. e.g.,
> >   evaluates to 0 ('\0') == 1 (\u0001)
> > For the second case, I would support printing them as C hex escapes. e.g, 
> >   evaluates to -128 ('\x80') == -123 ('\x85')
> > 
> > 
> > For the first case, I would support printing them as either C escapes or 
> > universal-character-names. e.g.,
> 
> As mentioned before, we should be consistent with what we do for diagnostics 
> messages in general - (`ie `pushEscapedString`).
> I check and we already do that. https://godbolt.org/z/doah9YGMT
> 
> Question is, why do we sometimes don't?
> Note that in general i don't have an opinion about displaying the value of 
> characters literal _in addition_ of the character itself, it seems like a 
> good thing)
The character escape in the current error message is handled in 
`CharacterLiteral::print` 
(https://github.com/llvm/llvm-project/blob/fcb6a9c07cf7a2bc63d364e3b7f60aaadadd57cc/clang/lib/AST/Expr.cpp#L1064-L1083),
 and the reason for `'\0'` being escaped to `\x00` and `'\u{9}'` escaped to 
`\t` is that `escapeCStyle` does not escape null character 
(https://github.com/llvm/llvm-project/blob/c1c86f9eae73786bcdacddaab248817c4f176935/clang/include/clang/Basic/CharInfo.h#L174-L200).
`pushEscapedString` does not escape whitespace characters, so we should use 
`escapeCStyle` if we are to use c-style escape for them.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155610/new/

https://reviews.llvm.org/D155610

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155610: [Clang][ExprConstant] Print integer instead of character on static assertion failure

2023-07-31 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet updated this revision to Diff 545582.
hazohelet added a comment.

Address review comments

- Print the character representation only when the type of the expressions is 
`char` or `char8_t`
- Use `pushEscapedString` in the printing so that we can reuse its escaping 
logic
- Use `escapeCStyle` to escape whitespace characters
- `wchar_t` and `charN_t` are not handled yet


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155610/new/

https://reviews.llvm.org/D155610

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Basic/Diagnostic.h
  clang/lib/Basic/Diagnostic.cpp
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/test/Lexer/cxx1z-trigraphs.cpp
  clang/test/SemaCXX/static-assert-cxx26.cpp
  clang/test/SemaCXX/static-assert.cpp

Index: clang/test/SemaCXX/static-assert.cpp
===
--- clang/test/SemaCXX/static-assert.cpp
+++ clang/test/SemaCXX/static-assert.cpp
@@ -262,7 +262,19 @@
 return 'c';
   }
   static_assert(getChar() == 'a', ""); // expected-error {{failed}} \
-   // expected-note {{evaluates to ''c' == 'a''}}
+   // expected-note {{evaluates to ''c' (99) == 'a' (97)'}}
+  static_assert((char)9 == '\x61', ""); // expected-error {{failed}} \
+// expected-note {{evaluates to ''\t' (9) == 'a' (97)'}}
+  static_assert((char)10 == '\0', ""); // expected-error {{failed}} \
+   // expected-note {{n' (10) == '\u' (0)'}}
+  // The note above is intended to match "evaluates to '\n' (10) == '\u' (0)'", but if we write it as it is,
+  // the "\n" cannot be consumed by the diagnostic consumer.
+  static_assert((signed char)10 == (char)-123, ""); // expected-error {{failed}} \
+// expected-note {{evaluates to '10 == '\x85' (-123)'}}
+  static_assert((char)-4 == (unsigned char)-8, ""); // expected-error {{failed}} \
+// expected-note {{evaluates to ''\xfc' (-4) == 248'}}
+  static_assert((char)-128 == (char)-123, ""); // expected-error {{failed}} \
+   // expected-note {{evaluates to ''\x80' (-128) == '\x85' (-123)'}}
 
   /// Bools are printed as bools.
   constexpr bool invert(bool b) {
Index: clang/test/SemaCXX/static-assert-cxx26.cpp
===
--- clang/test/SemaCXX/static-assert-cxx26.cpp
+++ clang/test/SemaCXX/static-assert-cxx26.cpp
@@ -289,3 +289,10 @@
 Bad b; // expected-note {{in instantiation}}
 
 }
+
+namespace EscapeInDiagnostic {
+static_assert('\u{9}' == (char)1, ""); // expected-error {{failed}} \
+   // expected-note {{evaluates to ''\t' (9) == '\u0001' (1)'}}
+static_assert((char8_t)-128 == (char8_t)-123, ""); // expected-error {{failed}} \
+   // expected-note {{evaluates to ''\x80' (128) == '\x85' (133)'}}
+}
Index: clang/test/Lexer/cxx1z-trigraphs.cpp
===
--- clang/test/Lexer/cxx1z-trigraphs.cpp
+++ clang/test/Lexer/cxx1z-trigraphs.cpp
@@ -21,7 +21,7 @@
 
 #if !ENABLED_TRIGRAPHS
 // expected-error@11 {{}} expected-warning@11 {{trigraph ignored}}
-// expected-error@13 {{failed}} expected-warning@13 {{trigraph ignored}} expected-note@13 {{evaluates to ''?' == '#''}}
+// expected-error@13 {{failed}} expected-warning@13 {{trigraph ignored}} expected-note@13 {{evaluates to ''?' (63) == '#' (35)'}}
 // expected-error@16 {{}}
 // expected-error@20 {{}}
 #else
Index: clang/lib/Sema/SemaDeclCXX.cpp
===
--- clang/lib/Sema/SemaDeclCXX.cpp
+++ clang/lib/Sema/SemaDeclCXX.cpp
@@ -16817,13 +16817,31 @@
  "Bool type, but value is not 0 or 1");
   llvm::raw_svector_ostream OS(Str);
   OS << (BoolValue ? "true" : "false");
-} else if (T->isCharType()) {
+} else {
   // Same is true for chars.
-  Str.push_back('\'');
-  Str.push_back(V.getInt().getExtValue());
-  Str.push_back('\'');
-} else
-  V.getInt().toString(Str);
+  // We want to print the character representation for `char` type
+  // and need to escape it if it is not printable.
+  const auto *BTy = T->getAs();
+  if (BTy && (BTy->getKind() == BuiltinType::Char_S ||
+  BTy->getKind() == BuiltinType::Char_U ||
+  BTy->getKind() == BuiltinType::Char8)) {
+llvm::raw_svector_ostream OS(Str);
+int64_t CharVal = V.getInt().getExtValue();
+Str.push_back('\'');
+StringRef Escaped = escapeCStyle(CharVal);
+if (!Escaped.empty()) {
+  OS << Escaped;
+} else {
+  const char CharArr[] = {static_cast(CharVal)};
+  pushEscapedString(StringRef(CharArr, sizeof(

[PATCH] D155064: [clang][SemaCXX] Diagnose tautological uses of consteval if and is_constant_evaluated

2023-07-31 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

@ldionne @philnik @Mordante
Is this way of fixing libc++ tests OK from libc++ side? If there's better way 
to fix them I am happy to follow it.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155064/new/

https://reviews.llvm.org/D155064

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155610: [Clang][ExprConstant] Print integer instead of character on static assertion failure

2023-07-31 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added inline comments.



Comment at: clang/lib/Basic/Diagnostic.cpp:838-858
+  if (UseUCN)
+OutStream << "\\u"
+  << llvm::format_hex_no_prefix(CodepointValue, /*Width=*/4,
+/*Upper=*/false);
+  else
+OutStream << " The use UCN addition is probably not justified. we should consistent in how 
> we print the value of non-printable code points.
My motivation here is to print a valid character literal. I think it justifies 
this change somewhat. I'd like to see what others think about this.



Comment at: clang/lib/Sema/SemaDeclCXX.cpp:16836-16837
+  const char CharArr[] = {static_cast(CharVal)};
+  pushEscapedString(StringRef(CharArr, sizeof(CharArr)), Str,
+/*UseUCN=*/true);
+}

cor3ntin wrote:
> A  different way to do that would be to have a 'Escape Whitespaces' parameter 
> on pushEscapedString that would also escape \t, \n, etc (I don't think we 
> want to escape SPACE)
If you mean `\u0020` by SPACE, it won't be escaped by this code.



CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155610/new/

https://reviews.llvm.org/D155610

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D156604: [clang][ExprConst] Use call source range for 'in call to' diags

2023-08-01 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

In D156604#4549524 , @tbaeder wrote:

> @hazohelet I changed this to just return `CallExpr->getSourceRange()` and to 
> not save a source range separately. Can you give this a look please?

I was thinking about removing `CallLoc` some time ago, and concluded that we 
shouldn't.
Destructor calls are usually not explicitly written by the user, so `CallExpr` 
is set to null. `CallLoc` is often set to the declaration location 
(https://github.com/llvm/llvm-project/blob/97cddb78502eee583b5f4ee02c59b7156398587f/clang/lib/AST/ExprConstant.cpp#L703-L708).
So we'll lose this information if we are to remove `CallLoc` from the stack 
frame, and it seems to be the reason for the test failures.

About getting source range from `CallExpr->getSourceRange`, I don't see any 
obvious problems. All explicitly-written function calls should have its AST in 
`CallExpr` field.
So using the `CallLoc` for the diagnostics location and 
`CallExpr->getSourceRange` for the source range would do the trick.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156604/new/

https://reviews.llvm.org/D156604

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D153969: [clang][ExprConstant] Fix crash on uninitialized base class subobject

2023-08-01 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

Ping


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D153969/new/

https://reviews.llvm.org/D153969

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D153969: [clang][ExprConstant] Fix crash on uninitialized base class subobject

2023-08-01 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added inline comments.



Comment at: clang/lib/AST/ExprConstant.cpp:2422
+  << BS.getType();
+  Info.Note(BS.getBeginLoc(), 
diag::note_constexpr_base_inherited_here);
+  return false;

aaron.ballman wrote:
> aaron.ballman wrote:
> > aaron.ballman wrote:
> > > hazohelet wrote:
> > > > aaron.ballman wrote:
> > > > > hazohelet wrote:
> > > > > > hazohelet wrote:
> > > > > > > tbaeder wrote:
> > > > > > > > Can you pass `<< BS.getSourceRange()` here? Does that improve 
> > > > > > > > things?
> > > > > > > Currently, `DiagLoc` points to the variable declaration and the 
> > > > > > > `BS.getSourceRange` covers the line where the base class is 
> > > > > > > inherited. This causes distant source range and thus 
> > > > > > > unnecessarily many lines of snippet printing.
> > > > > > > e.g.
> > > > > > > ```
> > > > > > > struct Base {
> > > > > > >   Base() = delete;
> > > > > > > };
> > > > > > > struct Derived : Base {
> > > > > > >   constexpr Derived() {}
> > > > > > > };
> > > > > > > constexpr Derived dd;
> > > > > > > ```
> > > > > > > Output:
> > > > > > > ```
> > > > > > > source.cpp:7:19: error: constexpr variable 'dd' must be 
> > > > > > > initialized by a constant expression
> > > > > > > 7 | constexpr Derived dd;
> > > > > > >   |   ^~
> > > > > > > source.cpp:7:19: note: constructor of base class 'Base' is not 
> > > > > > > called
> > > > > > > 7 | struct Derived : Base {
> > > > > > >   |  
> > > > > > > 8 |   constexpr Derived() {}
> > > > > > > 9 | };
> > > > > > >10 | constexpr Derived dd;
> > > > > > >   |   ^
> > > > > > > source.cpp:4:18: note: base class inherited here
> > > > > > > 4 | struct Derived : Base {
> > > > > > >   |  ^
> > > > > > > ```
> > > > > > > (The line numbers seem incorrect but is already reported in 
> > > > > > > https://github.com/llvm/llvm-project/issues/63524)
> > > > > > > 
> > > > > > > I think we don't need two notes here because the error is already 
> > > > > > > pointing to the variable declaration. Having something like the 
> > > > > > > following would be succint.
> > > > > > > ```
> > > > > > > source.cpp:7:19: error: constexpr variable 'dd' must be 
> > > > > > > initialized by a constant expression
> > > > > > > 7 | constexpr Derived dd;
> > > > > > >   |   ^~
> > > > > > > source.cpp:4:18: note: constructor of base class 'Base' is not 
> > > > > > > called
> > > > > > > 4 | struct Derived : Base {
> > > > > > >   |  ^~~~
> > > > > > > ```
> > > > > > > Providing source range would be beneficial because the inherited 
> > > > > > > class often spans in a few lines (the code in the crashing 
> > > > > > > report, for example)
> > > > > > Sorry, I was looking at the line above. The distant source range 
> > > > > > problem doesn't occur.
> > > > > > 
> > > > > > I tested another input
> > > > > > ```
> > > > > > struct Base {
> > > > > >   Base() = delete;
> > > > > >   constexpr Base(int){}
> > > > > > };
> > > > > > 
> > > > > > struct Derived : Base {
> > > > > >   constexpr Derived() {}
> > > > > >   constexpr Derived(int n): Base(n) {}
> > > > > > };
> > > > > > 
> > > > > > constexpr Derived darr[3] = {1, Derived(), 3};
> > > > > > ```
> > > > > > expecting that the `DiagLoc` points to the second initializer 
> > > > > > `Derived()`, but it pointed to the same location as the error, so 
> > > > > > I'm still in favor of the idea of having a single note here.
> > > > > Erich's suggestion in 
> > > > > https://github.com/llvm/llvm-project/issues/63496#issuecomment-1607415201
> > > > >  was to continue to evaluate the constructor because there may be 
> > > > > further follow-on diagnostics that are relevant and not related to 
> > > > > the base class subobject. I tend to agree -- is there a reason why 
> > > > > you're not doing that here?
> > > > My question 
> > > > (https://github.com/llvm/llvm-project/issues/63496#issuecomment-1607177233)
> > > >  was whether or not we should utilize constant evaluator even if the 
> > > > evaluated expression is a semantically-invalid constructor like the 
> > > > crashing case.
> > > > So in my understanding, Erich's suggestion was that we should continue 
> > > > utilizing the constant evaluator in these cases, and stopping the 
> > > > evaluator here at uninitialized base class subobject is something else.
> > > Our usual strategy is to continue compilation to try to find follow-on 
> > > issues. For example: https://godbolt.org/z/qrMchvh1f -- even though the 
> > > constructor declaration is not valid, we still go on to diagnose issues 
> > > within the constructor body.
> > This is the only outstanding discussion that I see left in the review, and 
> > it may be due to a misunderstanding. Am I correct that your changes cause 
> > us to not diagnose further issues in the constructor

[PATCH] D155610: [Clang][ExprConstant] Print integer instead of character on static assertion failure

2023-08-02 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

In D155610#4550346 , @cor3ntin wrote:

> I've been thinking about it and I think I have a cleaner design for the 
> printing of characters:
>
> We need a `CharToString(unsigned, Qualtype) -> SmallString` method that takes 
> a value and the type.
> for `char` and `char8_t` we can just return the value.
> For `wchar_t`, `char32_t` and `char16_t`, we can use something like 
> `ConvertCodePointToUTF8` to convert to UTF-8. If that fails we can escape 
> with `\x`
> If we pass the result of that to the diagnostic engine, escaping  of non 
> printing character would happen automatically.
>
> That way we have a nice separation between converting an APValue to string 
> and printing it, which should avoid code duplication quite a bit, and 
> generally make the design nicer.
> Then maybe we need to consider whether we want to modify 
> `CharacterLiteral::print` to be aligned with all of that. I don;t know if 
> that's used, for example, for mangling.
>
> Given there are a bunch of different issues here, i would not mind separate 
> PRs - having the numerical value showed in paren seems valuable on its own.

Thanks for the suggestion. Printing of multibyte character is a bit out of the 
scope of the original goal of this patch, but I'll give it a try.




Comment at: clang/lib/Basic/Diagnostic.cpp:838-858
+  if (UseUCN)
+OutStream << "\\u"
+  << llvm::format_hex_no_prefix(CodepointValue, /*Width=*/4,
+/*Upper=*/false);
+  else
+OutStream << " hazohelet wrote:
> > cor3ntin wrote:
> > > The use UCN addition is probably not justified. we should consistent in 
> > > how we print the value of non-printable code points.
> > My motivation here is to print a valid character literal. I think it 
> > justifies this change somewhat. I'd like to see what others think about 
> > this.
> Why? there is no expectation that diagnostics messages reproduce C++. 
> Consistency between string literals and characters literals is a lot more 
> important
The diagnostic meesage here looks like `expression evaluates to 'VALUE1 == 
VALUE2'`
I tend to expect that `VALUE1 == VALUE2` is a syntactically valid expression 
because of the syntactical element `==`. But if others do not feel the same 
way, I am okay with something like `'' == ''`


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155610/new/

https://reviews.llvm.org/D155610

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155610: [Clang][ExprConstant] Print integer instead of character on static assertion failure

2023-08-03 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet updated this revision to Diff 546782.
hazohelet added a comment.

Address comments from Corentin

- Use default `pushEscapedString` escaping (``) instead of UCN 
representation `\u0001`
- Convert multi-byte characters (`wchar_t`, `char16_t`, `char32_t`) to UTF-8 
and prints them.
- Added `CharToString` utility function


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155610/new/

https://reviews.llvm.org/D155610

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/test/Lexer/cxx1z-trigraphs.cpp
  clang/test/SemaCXX/static-assert-cxx26.cpp
  clang/test/SemaCXX/static-assert.cpp

Index: clang/test/SemaCXX/static-assert.cpp
===
--- clang/test/SemaCXX/static-assert.cpp
+++ clang/test/SemaCXX/static-assert.cpp
@@ -262,7 +262,29 @@
 return 'c';
   }
   static_assert(getChar() == 'a', ""); // expected-error {{failed}} \
-   // expected-note {{evaluates to ''c' == 'a''}}
+   // expected-note {{evaluates to ''c' (99) == 'a' (97)'}}
+  static_assert((char)9 == '\x61', ""); // expected-error {{failed}} \
+// expected-note {{evaluates to ''\t' (9) == 'a' (97)'}}
+  static_assert((char)10 == '\0', ""); // expected-error {{failed}} \
+   // expected-note {{n' (10) == '' (0)'}}
+  // The note above is intended to match "evaluates to '\n' (10) == '' (0)'", but if we write it as it is,
+  // the "\n" cannot be consumed by the diagnostic consumer.
+  static_assert((signed char)10 == (char)-123, ""); // expected-error {{failed}} \
+// expected-note {{evaluates to '10 == '<85>' (-123)'}}
+  static_assert((char)-4 == (unsigned char)-8, ""); // expected-error {{failed}} \
+// expected-note {{evaluates to ''' (-4) == 248'}}
+  static_assert((char)-128 == (char)-123, ""); // expected-error {{failed}} \
+   // expected-note {{evaluates to ''<80>' (-128) == '<85>' (-123)'}}
+  static_assert('\xA0' == (char)'\x20', ""); // expected-error {{failed}} \
+ // expected-note {{evaluates to ''' (-96) == ' ' (32)'}}
+static_assert((char16_t)L'ゆ' == L"C̵̭̯̠̎͌ͅť̺"[1], ""); // expected-error {{failed}} \
+// expected-note {{evaluates to 'u'ゆ' (12422) == L'̵' (821)'}}
+static_assert(L"\/"[1] == u'\xFFFD', ""); // expected-error {{failed}} \
+   // expected-note {{evaluates to 'L'/' (65295) == u'�' (65533)'}}
+static_assert(L"⚾"[0] == U'🌍', ""); // expected-error {{failed}} \
+   // expected-note {{evaluates to 'L'⚾' (9918) == U'🌍' (127757)'}}
+static_assert(U"\a"[0] == (wchar_t)9, ""); // expected-error {{failed}} \
+   // expected-note {{evaluates to 'U'\a' (7) == L'\t' (9)'}}
 
   /// Bools are printed as bools.
   constexpr bool invert(bool b) {
Index: clang/test/SemaCXX/static-assert-cxx26.cpp
===
--- clang/test/SemaCXX/static-assert-cxx26.cpp
+++ clang/test/SemaCXX/static-assert-cxx26.cpp
@@ -298,3 +298,12 @@
 Bad b; // expected-note {{in instantiation}}
 
 }
+
+namespace EscapeInDiagnostic {
+static_assert('\u{9}' == (char)1, ""); // expected-error {{failed}} \
+   // expected-note {{evaluates to ''\t' (9) == '' (1)'}}
+static_assert((char8_t)-128 == (char8_t)-123, ""); // expected-error {{failed}} \
+   // expected-note {{evaluates to 'u8'<80>' (128) == u8'<85>' (133)'}}
+static_assert((char16_t)0xFEFF == (char16_t)0xDB93, ""); // expected-error {{failed}} \
+ // expected-note {{evaluates to 'u'' (65279) == u'\xDB93' (56211)'}}
+}
Index: clang/test/Lexer/cxx1z-trigraphs.cpp
===
--- clang/test/Lexer/cxx1z-trigraphs.cpp
+++ clang/test/Lexer/cxx1z-trigraphs.cpp
@@ -21,7 +21,7 @@
 
 #if !ENABLED_TRIGRAPHS
 // expected-error@11 {{}} expected-warning@11 {{trigraph ignored}}
-// expected-error@13 {{failed}} expected-warning@13 {{trigraph ignored}} expected-note@13 {{evaluates to ''?' == '#''}}
+// expected-error@13 {{failed}} expected-warning@13 {{trigraph ignored}} expected-note@13 {{evaluates to ''?' (63) == '#' (35)'}}
 // expected-error@16 {{}}
 // expected-error@20 {{}}
 #else
Index: clang/lib/Sema/SemaDeclCXX.cpp
===
--- clang/lib/Sema/SemaDeclCXX.cpp
+++ clang/lib/Sema/SemaDeclCXX.cpp
@@ -46,6 +46,7 @@
 #include "llvm/ADT/ScopeExit.h"
 #include "llvm/ADT/SmallString.h"
 #inc

[PATCH] D155610: [Clang][Sema] Fix display of characters on static assertion failure

2023-08-03 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet updated this revision to Diff 546832.
hazohelet marked 2 inline comments as done.
hazohelet retitled this revision from "[Clang][ExprConstant] Print integer 
instead of character on static assertion failure" to "[Clang][Sema] Fix display 
of characters on static assertion failure".
hazohelet edited the summary of this revision.
hazohelet added a comment.

Address comments from Corentin

- Remove printing of character type prefix
- Added code example in release note
- Removed unnecessary static_cast


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155610/new/

https://reviews.llvm.org/D155610

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/test/Lexer/cxx1z-trigraphs.cpp
  clang/test/SemaCXX/static-assert-cxx26.cpp
  clang/test/SemaCXX/static-assert.cpp

Index: clang/test/SemaCXX/static-assert.cpp
===
--- clang/test/SemaCXX/static-assert.cpp
+++ clang/test/SemaCXX/static-assert.cpp
@@ -262,7 +262,29 @@
 return 'c';
   }
   static_assert(getChar() == 'a', ""); // expected-error {{failed}} \
-   // expected-note {{evaluates to ''c' == 'a''}}
+   // expected-note {{evaluates to ''c' (99) == 'a' (97)'}}
+  static_assert((char)9 == '\x61', ""); // expected-error {{failed}} \
+// expected-note {{evaluates to ''\t' (9) == 'a' (97)'}}
+  static_assert((char)10 == '\0', ""); // expected-error {{failed}} \
+   // expected-note {{n' (10) == '' (0)'}}
+  // The note above is intended to match "evaluates to '\n' (10) == '' (0)'", but if we write it as it is,
+  // the "\n" cannot be consumed by the diagnostic consumer.
+  static_assert((signed char)10 == (char)-123, ""); // expected-error {{failed}} \
+// expected-note {{evaluates to '10 == '<85>' (-123)'}}
+  static_assert((char)-4 == (unsigned char)-8, ""); // expected-error {{failed}} \
+// expected-note {{evaluates to ''' (-4) == 248'}}
+  static_assert((char)-128 == (char)-123, ""); // expected-error {{failed}} \
+   // expected-note {{evaluates to ''<80>' (-128) == '<85>' (-123)'}}
+  static_assert('\xA0' == (char)'\x20', ""); // expected-error {{failed}} \
+ // expected-note {{evaluates to ''' (-96) == ' ' (32)'}}
+static_assert((char16_t)L'ゆ' == L"C̵̭̯̠̎͌ͅť̺"[1], ""); // expected-error {{failed}} \
+// expected-note {{evaluates to ''ゆ' (12422) == '̵' (821)'}}
+static_assert(L"\/"[1] == u'\xFFFD', ""); // expected-error {{failed}} \
+   // expected-note {{evaluates to ''/' (65295) == '�' (65533)'}}
+static_assert(L"⚾"[0] == U'🌍', ""); // expected-error {{failed}} \
+   // expected-note {{evaluates to ''⚾' (9918) == '🌍' (127757)'}}
+static_assert(U"\a"[0] == (wchar_t)9, ""); // expected-error {{failed}} \
+   // expected-note {{evaluates to ''\a' (7) == '\t' (9)'}}
 
   /// Bools are printed as bools.
   constexpr bool invert(bool b) {
Index: clang/test/SemaCXX/static-assert-cxx26.cpp
===
--- clang/test/SemaCXX/static-assert-cxx26.cpp
+++ clang/test/SemaCXX/static-assert-cxx26.cpp
@@ -298,3 +298,12 @@
 Bad b; // expected-note {{in instantiation}}
 
 }
+
+namespace EscapeInDiagnostic {
+static_assert('\u{9}' == (char)1, ""); // expected-error {{failed}} \
+   // expected-note {{evaluates to ''\t' (9) == '' (1)'}}
+static_assert((char8_t)-128 == (char8_t)-123, ""); // expected-error {{failed}} \
+   // expected-note {{evaluates to ''<80>' (128) == '<85>' (133)'}}
+static_assert((char16_t)0xFEFF == (char16_t)0xDB93, ""); // expected-error {{failed}} \
+ // expected-note {{evaluates to ''' (65279) == '\xDB93' (56211)'}}
+}
Index: clang/test/Lexer/cxx1z-trigraphs.cpp
===
--- clang/test/Lexer/cxx1z-trigraphs.cpp
+++ clang/test/Lexer/cxx1z-trigraphs.cpp
@@ -21,7 +21,7 @@
 
 #if !ENABLED_TRIGRAPHS
 // expected-error@11 {{}} expected-warning@11 {{trigraph ignored}}
-// expected-error@13 {{failed}} expected-warning@13 {{trigraph ignored}} expected-note@13 {{evaluates to ''?' == '#''}}
+// expected-error@13 {{failed}} expected-warning@13 {{trigraph ignored}} expected-note@13 {{evaluates to ''?' (63) == '#' (35)'}}
 // expected-error@16 {{}}
 // expected-error@20 {{}}
 #else
Index: clang/lib/Sema/SemaDeclCXX.cpp

[PATCH] D155610: [Clang][Sema] Fix display of characters on static assertion failure

2023-08-03 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

One concern from my side is that some unicode characters like `U+FEFF` (I added 
in test) are invisible, but it may not be a big concern because we also display 
integer representation in parens.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155610/new/

https://reviews.llvm.org/D155610

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155610: [Clang][Sema] Fix display of characters on static assertion failure

2023-08-04 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added inline comments.



Comment at: clang/docs/ReleaseNotes.rst:103-137
+- When describing the failure of static assertion of `==` expression, clang 
prints the integer
+  representation of the value as well as its character representation when
+  the user-provided expression is of character type. If the character is
+  non-printable, clang now shows the escpaed character.
+  Clang also prints multi-byte characters if the user-provided expression
+  is of multi-byte character type.
+

aaron.ballman wrote:
> cor3ntin wrote:
> > aaron.ballman wrote:
> > > cor3ntin wrote:
> > > > @aaron.ballman One one hand this is nice, on the other hand maybe too 
> > > > detailed. What do you think?
> > > I'm happy with it -- better too much detail than too little, but this 
> > > really helps users see what's been improved and why it matters.
> > > 
> > > That said, I think `0x0A` and `0x1F30D` would arguably be better than 
> > > printing the values in decimal. For `\n`, perhaps folks remember that 
> > > it's decimal value 10, but nobody is going to know what `127757` means 
> > > compared to the hex representation (esp because the value is specified in 
> > > hex with the prefix printed in the error message). WDYT?
> > For `wchar_t`, `charN_t` I think that makes sense.
> > for `char`... hard to know, I think this is mostly useful for people who 
> > treat char as some kind of integer. I could go either way. using hex 
> > consistently seems reasonable
> I don't insist on using hex, but I have a slight preference for using it 
> consistently everywhere. CC @cjdb for more opinions since this relates to 
> user experience of diagnostics.
I generally agree that hex code would be better for characters.
I think we still have some arguable points.
1. Should we print the unsigned code point or the (possibly signed) integer? 
(e.g. `0xFF` vs `-0x01` for `(char)-1`, on targets where `char` is signed)
2. Should we print the hex code when the other subexpression of the `==` 
expression is not a textual type? (e.g. `0x11` vs `17` for LHS of `(char)17 == 
11`)

For 1, I think we should always print unsigned code point for all textual types 
for consistency. Also we don't want to print `-0x3` for `L'\xFFFD'` on targets 
where `wchar_t` is signed and 16-bit width (I haven't checked whether that 
target exists, though).
For 2, I want to see decimal (possibly signed) integer if the other side of the 
expression is not textual type.
Displaying `expression evaluates to ''' (0xFF) == 255'` for the following 
code would be highly confusing.
```
static_assert((char)-1 == (unsigned char)-1);
```
WDYT?


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155610/new/

https://reviews.llvm.org/D155610

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D152495: [Clang][SemaCXX] Add unused warning for variables declared in condition expressions

2023-08-06 Thread Takuya Shimizu via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGbd0ed0abc31f: [Clang][SemaCXX] Add unused warning for 
variables declared in condition… (authored by hazohelet).

Changed prior to commit:
  https://reviews.llvm.org/D152495?vs=540479&id=547525#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D152495/new/

https://reviews.llvm.org/D152495

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaExprCXX.cpp
  clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
  clang/test/SemaCXX/warn-unused-variables.cpp

Index: clang/test/SemaCXX/warn-unused-variables.cpp
===
--- clang/test/SemaCXX/warn-unused-variables.cpp
+++ clang/test/SemaCXX/warn-unused-variables.cpp
@@ -1,5 +1,5 @@
 // RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -Wno-c++1y-extensions -verify %s
-// RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -Wno-c++1y-extensions -verify -std=c++11 %s
+// RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -Wno-c++14-extensions -Wno-c++17-extensions -verify -std=c++11 %s
 template void f() {
   T t;
   t = 17;
@@ -294,3 +294,115 @@
 }
 
 } // namespace gh54489
+
+namespace inside_condition {
+  void ifs() {
+if (int hoge = 0) // expected-warning {{unused variable 'hoge'}}
+  return;
+if (const int const_hoge = 0) // expected-warning {{unused variable 'const_hoge'}}
+  return;
+else if (int fuga = 0)
+  (void)fuga;
+else if (int used = 1; int catched = used) // expected-warning {{unused variable 'catched'}}
+  return;
+else if (int refed = 1; int used = refed)
+  (void)used;
+else if (int unused1 = 2; int unused2 = 3) // expected-warning {{unused variable 'unused1'}} \
+   // expected-warning {{unused variable 'unused2'}}
+  return;
+else if (int unused = 4; int used = 5) // expected-warning {{unused variable 'unused'}}
+  (void)used;
+else if (int used = 6; int unused = 7) // expected-warning {{unused variable 'unused'}}
+  (void)used;
+else if (int used1 = 8; int used2 = 9)
+  (void)(used1 + used2);
+else if (auto [a, b] = (int[2]){ 1, 2 }; 1) // expected-warning {{unused variable '[a, b]'}}
+  return;
+else if (auto [a, b] = (int[2]){ 1, 2 }; a)
+  return;
+  }
+
+  void fors() {
+for (int i = 0;int unused = 0;); // expected-warning {{unused variable 'i'}} \
+ // expected-warning {{unused variable 'unused'}}
+for (int i = 0;int used = 0;) // expected-warning {{unused variable 'i'}}
+  (void)used;
+  while(int var = 1) // expected-warning {{unused variable 'var'}}
+return;
+  }
+
+  void whiles() {
+while(int unused = 1) // expected-warning {{unused variable 'unused'}}
+  return;
+while(int used = 1)
+  (void)used;
+  }
+
+
+  void switches() {
+switch(int unused = 1) { // expected-warning {{unused variable 'unused'}}
+  case 1: return;
+}
+switch(constexpr int used = 3; int unused = 4) { // expected-warning {{unused variable 'unused'}}
+  case used: return;
+}
+switch(int used = 3; int unused = 4) { // expected-warning {{unused variable 'unused'}}
+  case 3: (void)used;
+}
+switch(constexpr int used1 = 0; constexpr int used2 = 6) {
+  case (used1+used2): return;
+}
+switch(auto [a, b] = (int[2]){ 1, 2 }; 1) { // expected-warning {{unused variable '[a, b]'}}
+  case 1: return;
+}
+switch(auto [a, b] = (int[2]){ 1, 2 }; b) {
+  case 1: return;
+}
+switch(auto [a, b] = (int[2]){ 1, 2 }; 1) {
+  case 1: (void)a;
+}
+  }
+  template 
+  struct Vector {
+void doIt() {
+  for (auto& e : elements){} // expected-warning {{unused variable 'e'}}
+}
+T elements[10];
+  };
+  void ranged_for() {
+Vectorvector;
+vector.doIt(); // expected-note {{here}}
+  }
+
+
+  struct RAII {
+int &x;
+RAII(int &ref) : x(ref) {}
+~RAII() { x = 0;}
+operator int() const { return 1; }
+  };
+  void aggregate() {
+int x = 10;
+int y = 10;
+if (RAII var = x) {}
+for(RAII var = x; RAII var2 = y;) {}
+while (RAII var = x) {}
+switch (RAII var = x) {}
+  }
+
+  struct TrivialDtor{
+int &x;
+TrivialDtor(int &ref) : x(ref) { ref = 32; }
+operator int() const { return 1; }
+  };
+  void trivial_dtor() {
+int x = 10;
+int y = 10;
+if (TrivialDtor var = x) {} // expected-warning {{unused variable 'var'}}
+for(TrivialDtor var = x; TrivialDtor var2 = y;) {} // expected-warning {{unused variable 'var'}} \
+ // expected-warning {{unused variable 'var2'}}
+while (TrivialDtor var = x) {} // expected-warning {{unused variable 'var'}}
+switch (TrivialDt

[PATCH] D152495: [Clang][SemaCXX] Add unused warning for variables declared in condition expressions

2023-08-06 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

I reverted this change because it broke sanitizer buildbots.
https://lab.llvm.org/buildbot/#/builders/19/builds/18369
The cause of the errors here are relatively clear and I can speculatively 
replace `if (std::error_code EC = makeCanonical(Path))` with `if 
(makeCanonical(Path))`

https://lab.llvm.org/buildbot/#/builders/94/builds/15865
https://lab.llvm.org/buildbot/#/builders/240/builds/12947
These 2 are not clear for me, so I'll take time to find the cause.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D152495/new/

https://reviews.llvm.org/D152495

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155064: [clang][SemaCXX] Diagnose tautological uses of consteval if and is_constant_evaluated

2023-08-07 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

I've noticed several shortcoming/limitations of this patch.

1. Function arguments: When we parse the arguments to a function call, the 
callee isn't still resolved, so we don't know whether it's consteval. If the 
callee is consteval, its argument is known to be constant-evaluated regardless 
of its surrounding evaluation context.

e.g.

  consteval int ce(int n){ return n;};
  int f() {
int a = ce(__builtin_is_constant_evaluated()); // tautologically true
  }

2. init-statement of constexpr-if:

The condition is required to be a constant expression, but the init-stmt before 
it isn't.
e.g. Given `if constexpr (InitStmt; Cond) {}`, `InitStmt` is evaluated in the 
surrounding evaluation context.
If the init-statement is a declaration we can push initializer evaluation 
context like we do in other variables. However, if the init-statement is an 
expression, when clang parses the expression, clang doesn't know whether it is 
parsing the init-statement or the condition. 
https://github.com/llvm/llvm-project/blob/e3c57fdd8439ba82c67347629a3c66f293e1f3d0/clang/lib/Parse/ParseExprCXX.cpp#L2102

If we are to handle these cases with perfection, we need to defer warning 
emissions, but I'm skeptical about the value we obtain from this effort.

For the first problem, we can make `IsRuntimeEvaluated` false while parsing 
arguments to avoid emitting incorrect diagnostics although it makes false 
negatives on tautologically-false uses in arguments.
The second problem is difficult because we cannot avoid incorrect diagnostics 
in `InitStmt` expression of constexpr-if with a simple fix. But I think it's 
acceptable for the time being because it would be a pretty rare case to use 
`is_constant_evaluated` there.

@cor3ntin 
BTW, can I separate the initializer evaluation context bug fix part to make 
another PR? Another patch I'm working on locally also suffers from the 
evaluation context bug, and I want to land that part relatively soon.
Also, it might be nice to separate the NFC libc++ test modifications I recently 
uploaded because it's too much despite being NFC. Or we can turn off this 
tautology warning against macros. Do you think it's reasonable?


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155064/new/

https://reviews.llvm.org/D155064

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D153969: [clang][ExprConstant] Fix crash on uninitialized base class subobject

2023-08-07 Thread Takuya Shimizu via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG24c91d443222: [clang][ExprConstant] Fix crash on 
uninitialized base class subobject (authored by hazohelet).

Changed prior to commit:
  https://reviews.llvm.org/D153969?vs=538754&id=548079#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D153969/new/

https://reviews.llvm.org/D153969

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Basic/DiagnosticASTKinds.td
  clang/lib/AST/ExprConstant.cpp
  clang/test/Misc/constexpr-subobj-init-source-ranges.cpp
  clang/test/SemaCXX/constexpr-subobj-initialization.cpp

Index: clang/test/SemaCXX/constexpr-subobj-initialization.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/constexpr-subobj-initialization.cpp
@@ -0,0 +1,58 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+namespace baseclass_uninit {
+struct DelBase {
+  constexpr DelBase() = delete; // expected-note {{'DelBase' has been explicitly marked deleted here}}
+};
+
+struct Foo : DelBase {  // expected-note 2{{constructor of base class 'DelBase' is not called}}
+  constexpr Foo() {}; // expected-error {{call to deleted constructor of 'DelBase'}}
+};
+constexpr Foo f; // expected-error {{must be initialized by a constant expression}}
+struct Bar : Foo {
+  constexpr Bar() {};
+};
+constexpr Bar bar; // expected-error {{must be initialized by a constant expression}}
+
+struct Base {};
+struct A : Base { // expected-note {{constructor of base class 'Base' is not called}}
+  constexpr A() : value() {} // expected-error {{member initializer 'value' does not name a non-static data member or base class}}
+};
+
+constexpr A a; // expected-error {{must be initialized by a constant expression}}
+
+struct B : Base { // expected-note {{constructor of base class 'Base' is not called}}
+  constexpr B() : {} // expected-error {{expected class member or base class name}}
+};
+
+constexpr B b; // expected-error {{must be initialized by a constant expression}}
+} // namespace baseclass_uninit
+
+
+struct Foo {
+  constexpr Foo(); // expected-note 2{{declared here}}
+};
+
+constexpr Foo ff; // expected-error {{must be initialized by a constant expression}} \
+  // expected-note {{undefined constructor 'Foo' cannot be used in a constant expression}}
+
+struct Bar : protected Foo {
+  int i;
+  constexpr Bar() : i(12) {} // expected-note {{undefined constructor 'Foo' cannot be used in a constant expression}}
+};
+
+constexpr Bar bb; // expected-error {{must be initialized by a constant expression}} \
+  // expected-note {{in call to 'Bar()'}}
+
+template 
+struct Baz {
+  constexpr Baz(); // expected-note {{declared here}}
+};
+
+struct Quux : Baz, private Bar {
+  int i;
+  constexpr Quux() : i(12) {} // expected-note {{undefined constructor 'Baz' cannot be used in a constant expression}}
+};
+
+constexpr Quux qx; // expected-error {{must be initialized by a constant expression}} \
+   // expected-note {{in call to 'Quux()'}}
Index: clang/test/Misc/constexpr-subobj-init-source-ranges.cpp
===
--- /dev/null
+++ clang/test/Misc/constexpr-subobj-init-source-ranges.cpp
@@ -0,0 +1,11 @@
+// RUN: not %clang_cc1 -fsyntax-only -fdiagnostics-print-source-range-info %s 2>&1 | FileCheck %s --strict-whitespace
+
+struct DelBase {
+  constexpr DelBase() = delete;
+};
+
+// CHECK:  :{[[@LINE+1]]:21-[[@LINE+1]]:28}
+struct Foo : public DelBase {
+  constexpr Foo() {};
+};
+constexpr Foo f;
Index: clang/lib/AST/ExprConstant.cpp
===
--- clang/lib/AST/ExprConstant.cpp
+++ clang/lib/AST/ExprConstant.cpp
@@ -2450,9 +2450,16 @@
 if (const CXXRecordDecl *CD = dyn_cast(RD)) {
   unsigned BaseIndex = 0;
   for (const CXXBaseSpecifier &BS : CD->bases()) {
-if (!CheckEvaluationResult(CERK, Info, DiagLoc, BS.getType(),
-   Value.getStructBase(BaseIndex), Kind,
-   /*SubobjectDecl=*/nullptr, CheckedTemps))
+const APValue &BaseValue = Value.getStructBase(BaseIndex);
+if (!BaseValue.hasValue()) {
+  SourceLocation TypeBeginLoc = BS.getBaseTypeLoc();
+  Info.FFDiag(TypeBeginLoc, diag::note_constexpr_uninitialized_base)
+  << BS.getType() << SourceRange(TypeBeginLoc, BS.getEndLoc());
+  return false;
+}
+if (!CheckEvaluationResult(CERK, Info, DiagLoc, BS.getType(), BaseValue,
+   Kind, /*SubobjectDecl=*/nullptr,
+   CheckedTemps))
   return false;
 ++BaseIndex;
   }
Index: clang/include/clang/Basic/DiagnosticASTKinds.td
===
-

[PATCH] D152495: [Clang][SemaCXX] Add unused warning for variables declared in condition expressions

2023-08-08 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

In D152495#4566634 , @aaron.ballman 
wrote:

> In D152495#4563520 , @hazohelet 
> wrote:
>
>> I reverted this change because it broke sanitizer buildbots.
>> https://lab.llvm.org/buildbot/#/builders/19/builds/18369
>> The cause of the errors here are relatively clear and I can speculatively 
>> replace `if (std::error_code EC = makeCanonical(Path))` with `if 
>> (makeCanonical(Path))`
>
> That changes makes sense to me.
>
>> https://lab.llvm.org/buildbot/#/builders/94/builds/15865
>> https://lab.llvm.org/buildbot/#/builders/240/builds/12947
>> These 2 are not clear for me, so I'll take time to find the cause.
>
> Neither of these are problems with your patch -- they're flaky tests in this 
> case.

Thanks! I'll reland this patch with the unused `std::error_code` fix. I'll 
revert again if the same CI keeps failing for a few hours.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D152495/new/

https://reviews.llvm.org/D152495

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D157383: [clang][Diagnostics] Provide source range to integer-overflow warnings

2023-08-08 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet created this revision.
hazohelet added reviewers: aaron.ballman, tbaeder, cjdb.
Herald added a project: All.
hazohelet requested review of this revision.
Herald added a project: clang.

BEFORE:

  overflow.cpp:1:21: warning: overflow in expression; result is -2147483648 
with type 'int' [-Winteger-overflow]
  1 | int x = __INT_MAX__ + 1 + 3;
| ^
  overflow.cpp:2:9: warning: overflow in expression; result is -2147483648 with 
type 'int' [-Winteger-overflow]
  2 | int a = -(1 << 31) + 1;
| ^

AFTER:

  overflow.cpp:1:21: warning: overflow in expression; result is -2147483648 
with type 'int' [-Winteger-overflow]
  1 | int x = __INT_MAX__ + 1 + 3;
| ^~~
  overflow.cpp:2:9: warning: overflow in expression; result is -2147483648 with 
type 'int' [-Winteger-overflow]
  2 | int a = -(1 << 31) + 1;
| ^~


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D157383

Files:
  clang/lib/AST/ExprConstant.cpp
  clang/lib/AST/Interp/Interp.h
  clang/test/Misc/constexpr-source-ranges.cpp


Index: clang/test/Misc/constexpr-source-ranges.cpp
===
--- clang/test/Misc/constexpr-source-ranges.cpp
+++ clang/test/Misc/constexpr-source-ranges.cpp
@@ -13,3 +13,10 @@
 constexpr const int *P = &I;
 constexpr long L = (long)P;
 // CHECK: constexpr-source-ranges.cpp:14:20:{14:20-14:27}
+
+namespace overflow {
+// CHECK:  :{[[@LINE+1]]:9-[[@LINE+1]]:29}: warning: overflow
+int x = -1 + __INT_MAX__ + 2 + 3;
+// CHECK:  :{[[@LINE+1]]:9-[[@LINE+1]]:19}: warning: overflow
+int a = -(1 << 31) + 1;
+}
Index: clang/lib/AST/Interp/Interp.h
===
--- clang/lib/AST/Interp/Interp.h
+++ clang/lib/AST/Interp/Interp.h
@@ -269,7 +269,8 @@
 SmallString<32> Trunc;
 Value.trunc(Result.bitWidth()).toString(Trunc, 10);
 auto Loc = E->getExprLoc();
-S.report(Loc, diag::warn_integer_constant_overflow) << Trunc << Type;
+S.report(Loc, diag::warn_integer_constant_overflow)
+<< Trunc << Type << E->getSourceRange();
 return true;
   } else {
 S.CCEDiag(E, diag::note_constexpr_overflow) << Value << Type;
@@ -476,7 +477,8 @@
 SmallString<32> Trunc;
 NegatedValue.trunc(Result.bitWidth()).toString(Trunc, 10);
 auto Loc = E->getExprLoc();
-S.report(Loc, diag::warn_integer_constant_overflow) << Trunc << Type;
+S.report(Loc, diag::warn_integer_constant_overflow)
+<< Trunc << Type << E->getSourceRange();
 return true;
   }
 
@@ -529,7 +531,8 @@
 SmallString<32> Trunc;
 APResult.trunc(Result.bitWidth()).toString(Trunc, 10);
 auto Loc = E->getExprLoc();
-S.report(Loc, diag::warn_integer_constant_overflow) << Trunc << Type;
+S.report(Loc, diag::warn_integer_constant_overflow)
+<< Trunc << Type << E->getSourceRange();
 return true;
   }
 
Index: clang/lib/AST/ExprConstant.cpp
===
--- clang/lib/AST/ExprConstant.cpp
+++ clang/lib/AST/ExprConstant.cpp
@@ -2798,7 +2798,7 @@
 if (Info.checkingForUndefinedBehavior())
   Info.Ctx.getDiagnostics().Report(E->getExprLoc(),
diag::warn_integer_constant_overflow)
-  << toString(Result, 10) << E->getType();
+  << toString(Result, 10) << E->getType() << E->getSourceRange();
 return HandleOverflow(Info, E, Value, E->getType());
   }
   return true;
@@ -13625,7 +13625,7 @@
   if (Info.checkingForUndefinedBehavior())
 Info.Ctx.getDiagnostics().Report(E->getExprLoc(),
  diag::warn_integer_constant_overflow)
-<< toString(Value, 10) << E->getType();
+<< toString(Value, 10) << E->getType() << E->getSourceRange();
 
   if (!HandleOverflow(Info, E, -Value.extend(Value.getBitWidth() + 1),
   E->getType()))


Index: clang/test/Misc/constexpr-source-ranges.cpp
===
--- clang/test/Misc/constexpr-source-ranges.cpp
+++ clang/test/Misc/constexpr-source-ranges.cpp
@@ -13,3 +13,10 @@
 constexpr const int *P = &I;
 constexpr long L = (long)P;
 // CHECK: constexpr-source-ranges.cpp:14:20:{14:20-14:27}
+
+namespace overflow {
+// CHECK:  :{[[@LINE+1]]:9-[[@LINE+1]]:29}: warning: overflow
+int x = -1 + __INT_MAX__ + 2 + 3;
+// CHECK:  :{[[@LINE+1]]:9-[[@LINE+1]]:19}: warning: overflow
+int a = -(1 << 31) + 1;
+}
Index: clang/lib/AST/Interp/Interp.h
===
--- clang/lib/AST/Interp/Interp.h
+++ clang/lib/AST/Interp/Interp.h
@@ -269,7 +269,8 @@
 SmallString<32> Trunc;
 Value.trunc(Result.bitWidth()).toString(Trunc, 10);
 auto Loc = E->getExprLoc();
-S.report(Loc, diag::warn_integer_constant_overflow) << Trunc << Type;
+S

[PATCH] D157383: [clang][Diagnostics] Provide source range to integer-overflow warnings

2023-08-08 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added inline comments.



Comment at: clang/lib/AST/Interp/Interp.h:530-535
   if (S.checkingForUndefinedBehavior()) {
 SmallString<32> Trunc;
 APResult.trunc(Result.bitWidth()).toString(Trunc, 10);
 auto Loc = E->getExprLoc();
-S.report(Loc, diag::warn_integer_constant_overflow) << Trunc << Type;
+S.report(Loc, diag::warn_integer_constant_overflow)
+<< Trunc << Type << E->getSourceRange();

I'm not sure whether this branch takes effect.
I could not find codes that takes this block, so I haven't added tests for this.

FWIW, the old interpreter does not have the corresponding 
`warn_integer_constant_overflow` generated against overflowing increments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157383/new/

https://reviews.llvm.org/D157383

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D157383: [clang][Diagnostics] Provide source range to integer-overflow warnings

2023-08-08 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added inline comments.



Comment at: clang/lib/AST/Interp/Interp.h:530-535
   if (S.checkingForUndefinedBehavior()) {
 SmallString<32> Trunc;
 APResult.trunc(Result.bitWidth()).toString(Trunc, 10);
 auto Loc = E->getExprLoc();
-S.report(Loc, diag::warn_integer_constant_overflow) << Trunc << Type;
+S.report(Loc, diag::warn_integer_constant_overflow)
+<< Trunc << Type << E->getSourceRange();

tbaeder wrote:
> hazohelet wrote:
> > I'm not sure whether this branch takes effect.
> > I could not find codes that takes this block, so I haven't added tests for 
> > this.
> > 
> > FWIW, the old interpreter does not have the corresponding 
> > `warn_integer_constant_overflow` generated against overflowing increments.
> Is is not this: https://godbolt.org/z/eqn4Gs13q?
That note is emitted from `S.CCEDiag` at L539.
This warning looks like it is intended to be emitted when the function is not 
constexpr, but it does not appear.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157383/new/

https://reviews.llvm.org/D157383

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155064: [clang][SemaCXX] Diagnose tautological uses of consteval if and is_constant_evaluated

2023-08-08 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet updated this revision to Diff 548197.
hazohelet added a comment.

- Tentatively disable the warning on macros so as not to make a fuss in libc++ 
tests.
- Fixed incorrect output in arguments.
- Fixed incorrect output in declaration of init-statement of constexpr-if 
condition.
- Added tests with FIXMEs about the limitation I mentioned before.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155064/new/

https://reviews.llvm.org/D155064

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Basic/DiagnosticASTKinds.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/include/clang/Sema/Sema.h
  clang/lib/AST/ExprConstant.cpp
  clang/lib/Parse/ParseDecl.cpp
  clang/lib/Parse/ParseDeclCXX.cpp
  clang/lib/Parse/ParseExpr.cpp
  clang/lib/Parse/ParseExprCXX.cpp
  clang/lib/Parse/ParseStmt.cpp
  clang/lib/Sema/SemaChecking.cpp
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/lib/Sema/SemaLambda.cpp
  clang/lib/Sema/SemaStmt.cpp
  clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
  clang/test/AST/Interp/builtins.cpp
  clang/test/AST/Interp/if.cpp
  clang/test/AST/Interp/literals.cpp
  clang/test/CXX/expr/expr.const/p2-0x.cpp
  clang/test/CXX/expr/expr.const/p6-2a.cpp
  clang/test/CXX/expr/expr.prim/expr.prim.lambda/p3.cpp
  clang/test/CXX/stmt.stmt/stmt.select/stmt.if/p4.cpp
  clang/test/Parser/pragma-fenv_access.c
  clang/test/SemaCXX/constant-conversion.cpp
  clang/test/SemaCXX/constant-expression-cxx11.cpp
  clang/test/SemaCXX/cxx2a-consteval.cpp
  clang/test/SemaCXX/cxx2b-consteval-if.cpp
  clang/test/SemaCXX/cxx2b-consteval-propagate.cpp
  clang/test/SemaCXX/vartemplate-lambda.cpp
  clang/test/SemaCXX/warn-constant-evaluated-constexpr.cpp
  clang/test/SemaCXX/warn-tautological-meta-constant.cpp
  clang/test/SemaTemplate/concepts.cpp
  clang/unittests/Support/TimeProfilerTest.cpp
  libcxx/include/__type_traits/is_constant_evaluated.h
  
libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
  
libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp

Index: libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp
===
--- libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp
+++ libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp
@@ -24,7 +24,7 @@
 #else
   // expected-error-re@+1 (static_assert|static assertion)}} failed}}
   static_assert(!std::is_constant_evaluated(), "");
-  // expected-warning@-1 0-1 {{'std::is_constant_evaluated' will always evaluate to 'true' in a manifestly constant-evaluated expression}}
+  // expected-warning-re@-1 0-1 {{'std::is_constant_evaluated' will always evaluate to {{('true' in a manifestly constant-evaluated expression|true in this context)
 #endif
   return 0;
 }
Index: libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
===
--- libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
+++ libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
@@ -93,12 +93,10 @@
 }
 
 int main(int, char**) {
-  if (!std::is_constant_evaluated()) {
-test_containers, std::deque>();
-test_containers, std::vector>();
-test_containers, std::deque>();
-test_containers, std::vector>();
-  }
+  test_containers, std::deque>();
+  test_containers, std::vector>();
+  test_containers, std::deque>();
+  test_containers, std::vector>();
 
   types::for_each(types::forward_iterator_list{}, [] {
 test_join_view();
Index: libcxx/include/__type_traits/is_constant_evaluated.h
===
--- libcxx/include/__type_traits/is_constant_evaluated.h
+++ libcxx/include/__type_traits/is_constant_evaluated.h
@@ -24,7 +24,11 @@
 #endif
 
 _LIBCPP_HIDE_FROM_ABI inline _LIBCPP_CONSTEXPR bool __libcpp_is_constant_evaluated() _NOEXCEPT {
+#ifndef _LIBCPP_CXX03_LANG
   return __builtin_is_constant_evaluated();
+#else
+  return false;
+#endif
 }
 
 _LIBCPP_END_NAMESPACE_STD
Index: clang/unittests/Support/TimeProfilerTest.cpp
===
--- clang/unittests/Support/TimeProfilerTest.cpp
+++ clang/unittests/Support/TimeProfilerTest.cpp
@@ -188,7 +188,6 @@
 | EvaluateAsBooleanCondition ()
 | | EvaluateAsRValue ()
 | EvaluateAsInitializer (slow_value)
-| EvaluateAsConstantExpr ()
 | EvaluateAsConstantExpr ()
 | EvaluateAsRValue ()
 | EvaluateAsInitializer (slow_init_list)
Index: clang/test/SemaTemplate/concepts.cpp
===
--- clang/test/SemaTemplate/concepts.cpp
+++ clang/test/SemaTemplate/concepts.cpp
@@ -135,21 +135,21 @@
 
 namespace BuiltinIsConstantEvaluated {
   // Check that we 

[PATCH] D156604: [clang][ExprConst] Use call source range for 'in call to' diags

2023-08-08 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

We need some tests for dtors because they are handled differently from other 
functions.
I think the current ExprConstant part would not cover the explicitly-called 
dtors because the `HandleDestructorImpl` only has access to `CallLoc` and not 
source range.
Also new interpreter seems to point to the wrong source location on 
implicitly-called dtor.
Link: https://godbolt.org/z/1a6GW47eG
I think it's good to have a fix for it in this patch if it's not too 
complicated.

My previous comment "All explicitly-written function calls should have its AST 
in CallExpr field." was wrong for destructors. Sorry for the confusion.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156604/new/

https://reviews.llvm.org/D156604

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D156604: [clang][ExprConst] Use call source range for 'in call to' diags

2023-08-09 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet accepted this revision.
hazohelet added a comment.

I think it would be helpful to point to the subobject source range when 
diagnosing errors in subobject dtors, so I left some suggestions.
Otherwise this LGTM. Thanks!




Comment at: clang/lib/AST/ExprConstant.cpp:6663
 APValue *SubobjectValue = &Value.getStructField(FD->getFieldIndex());
-if (!HandleDestructionImpl(Info, CallLoc, Subobject, *SubobjectValue,
+if (!HandleDestructionImpl(Info, CallRange, Subobject, *SubobjectValue,
FD->getType()))





Comment at: clang/lib/AST/ExprConstant.cpp:6682
 APValue *SubobjectValue = &Value.getStructBase(BasesLeft);
-if (!HandleDestructionImpl(Info, CallLoc, Subobject, *SubobjectValue,
+if (!HandleDestructionImpl(Info, CallRange, Subobject, *SubobjectValue,
BaseType))

It's not the problem of this patch, but it might be nice to have 
`CXXBaseSpecifier::getTypeSourceRange` or alike because this is an interesting 
source range.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156604/new/

https://reviews.llvm.org/D156604

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D157526: [clang][Sema] Remove irrelevant diagnostics from constraint satisfaction failure

2023-08-09 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet created this revision.
hazohelet added reviewers: aaron.ballman, erichkeane, tbaeder, shafik.
Herald added a project: All.
hazohelet requested review of this revision.
Herald added a project: clang.

BEFORE this patch, when clang handles constraints like `C1 || C2` where `C1` 
evaluates to false and `C2` evaluates to true, it emitted irrelevant 
diagnostics about the falsity of `C1`.
This patch removes the irrelevant diagnostic information generated during the 
evaluation of `C1` if `C2` evaluates to true.

Fixes https://github.com/llvm/llvm-project/issues/54678


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D157526

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/Sema/SemaConcept.cpp
  clang/test/SemaTemplate/concepts.cpp


Index: clang/test/SemaTemplate/concepts.cpp
===
--- clang/test/SemaTemplate/concepts.cpp
+++ clang/test/SemaTemplate/concepts.cpp
@@ -994,3 +994,40 @@
 }
 
 }
+
+namespace GH54678 {
+template
+concept True = true;
+
+template
+concept False = false; // expected-note 9 {{'false' evaluated to false}}
+
+template
+concept Irrelevant = false;
+
+template 
+concept ErrorRequires = requires(ErrorRequires auto x) { x; }; // 
expected-error {{unknown type name 'ErrorRequires'}}
+
+template void aaa(T t) // expected-note {{candidate template ignored: 
constraints not satisfied}}
+requires (False || False) || False {} // expected-note 3 {{'int' does 
not satisfy 'False'}}
+template void bbb(T t) // expected-note {{candidate template ignored: 
constraints not satisfied}}
+requires (False || False) && True {} // expected-note 2 {{'long' does 
not satisfy 'False'}}
+template void ccc(T t) // expected-note {{candidate template ignored: 
constraints not satisfied}}
+requires (True || Irrelevant) && False {} // expected-note 
{{'unsigned long' does not satisfy 'False'}}
+template void ddd(T t) // expected-note {{candidate template ignored: 
constraints not satisfied}}
+requires (Irrelevant || True) && False {} // expected-note {{'int' 
does not satisfy 'False'}}
+template void eee(T t) // expected-note {{candidate template ignored: 
constraints not satisfied}}
+requires (Irrelevant || Irrelevant || True) && False {} // 
expected-note {{'long' does not satisfy 'False'}}
+
+template void fff(T t) // expected-note {{candidate template ignored: 
constraints not satisfied}}
+requires((ErrorRequires || False || True) && False) {} // 
expected-note {{'unsigned long' does not satisfy 'False'}}
+
+void test() {
+aaa(42); // expected-error {{no matching function}}
+bbb(42L); // expected-error{{no matching function}}
+ccc(42UL); // expected-error {{no matching function}}
+ddd(42); // expected-error {{no matching function}}
+eee(42L); // expected-error {{no matching function}}
+fff(42UL); // expected-error {{no matching function}}
+}
+}
Index: clang/lib/Sema/SemaConcept.cpp
===
--- clang/lib/Sema/SemaConcept.cpp
+++ clang/lib/Sema/SemaConcept.cpp
@@ -183,6 +183,7 @@
   ConstraintExpr = ConstraintExpr->IgnoreParenImpCasts();
 
   if (LogicalBinOp BO = ConstraintExpr) {
+auto EffectiveDetailEnd = Satisfaction.Details.end();
 ExprResult LHSRes = calculateConstraintSatisfaction(
 S, BO.getLHS(), Satisfaction, Evaluator);
 
@@ -216,6 +217,11 @@
 if (RHSRes.isInvalid())
   return ExprError();
 
+bool IsRHSSatisfied = Satisfaction.IsSatisfied;
+if (BO.isOr() && IsRHSSatisfied)
+  Satisfaction.Details.erase(EffectiveDetailEnd,
+ Satisfaction.Details.end());
+
 return BO.recreateBinOp(S, LHSRes, RHSRes);
   }
 
Index: clang/docs/ReleaseNotes.rst
===
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -125,6 +125,8 @@
   of a base class is not called in the constructor of its derived class.
 - Clang no longer emits ``-Wmissing-variable-declarations`` for variables 
declared
   with the ``register`` storage class.
+- Clang no longer emits irrelevant notes about unsatisfied constraint 
expressions
+  on the left-hand side of ``||`` when the right-hand side constraint is 
satisfied.
 
 Bug Fixes in This Version
 -


Index: clang/test/SemaTemplate/concepts.cpp
===
--- clang/test/SemaTemplate/concepts.cpp
+++ clang/test/SemaTemplate/concepts.cpp
@@ -994,3 +994,40 @@
 }
 
 }
+
+namespace GH54678 {
+template
+concept True = true;
+
+template
+concept False = false; // expected-note 9 {{'false' evaluated to false}}
+
+template
+concept Irrelevant = false;
+
+template 
+concept ErrorRequires = requires(ErrorRequires auto x) { x; }; // expected-error {{unknown type name 'ErrorRequires'}}
+
+template void aaa(T t) // expected-note {{candidate template ignored: constraints not satisfied}}
+requires (False || False) || Fal

[PATCH] D156604: [clang][ExprConst] Use call source range for 'in call to' diags

2023-08-10 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

In D156604#4572994 , @tbaeder wrote:

> While both of those suggestions are probably improvements (I haven't 
> checked), they also seem out of scope for this patch. This is just adding 
> source ranges. We can improve them later (and add better tests for them at 
> that point).

That makes sense to me.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156604/new/

https://reviews.llvm.org/D156604

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155610: [Clang][Sema] Fix display of characters on static assertion failure

2023-08-10 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet updated this revision to Diff 548948.
hazohelet marked 2 inline comments as done.
hazohelet added a comment.

Address comments from Aaron

- Use hex code for integer representation of textual types
- NFC stylistic changes


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155610/new/

https://reviews.llvm.org/D155610

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/test/Lexer/cxx1z-trigraphs.cpp
  clang/test/SemaCXX/static-assert-cxx26.cpp
  clang/test/SemaCXX/static-assert.cpp

Index: clang/test/SemaCXX/static-assert.cpp
===
--- clang/test/SemaCXX/static-assert.cpp
+++ clang/test/SemaCXX/static-assert.cpp
@@ -268,7 +268,29 @@
 return 'c';
   }
   static_assert(getChar() == 'a', ""); // expected-error {{failed}} \
-   // expected-note {{evaluates to ''c' == 'a''}}
+   // expected-note {{evaluates to ''c' (0x63) == 'a' (0x61)'}}
+  static_assert((char)9 == '\x61', ""); // expected-error {{failed}} \
+// expected-note {{evaluates to ''\t' (0x09) == 'a' (0x61)'}}
+  static_assert((char)10 == '\0', ""); // expected-error {{failed}} \
+   // expected-note {{n' (0x0A) == '' (0x00)'}}
+  // The note above is intended to match "evaluates to '\n' (0x0A) == '' (0x00)'", but if we write it as it is,
+  // the "\n" cannot be consumed by the diagnostic consumer.
+  static_assert((signed char)10 == (char)-123, ""); // expected-error {{failed}} \
+// expected-note {{evaluates to '10 == '<85>' (0x85)'}}
+  static_assert((char)-4 == (unsigned char)-8, ""); // expected-error {{failed}} \
+// expected-note {{evaluates to ''' (0xFC) == 248'}}
+  static_assert((char)-128 == (char)-123, ""); // expected-error {{failed}} \
+   // expected-note {{evaluates to ''<80>' (0x80) == '<85>' (0x85)'}}
+  static_assert('\xA0' == (char)'\x20', ""); // expected-error {{failed}} \
+ // expected-note {{evaluates to ''' (0xA0) == ' ' (0x20)'}}
+  static_assert((char16_t)L'ゆ' == L"C̵̭̯̠̎͌ͅť̺"[1], ""); // expected-error {{failed}} \
+  // expected-note {{evaluates to ''ゆ' (0x3086) == '̵' (0x335)'}}
+  static_assert(L"\/"[1] == u'\xFFFD', ""); // expected-error {{failed}} \
+  // expected-note {{evaluates to ''/' (0xFF0F) == '�' (0xFFFD)'}}
+  static_assert(L"⚾"[0] == U'🌍', ""); // expected-error {{failed}} \
+ // expected-note {{evaluates to ''⚾' (0x26BE) == '🌍' (0x1F30D)'}}
+  static_assert(U"\a"[0] == (wchar_t)9, ""); // expected-error {{failed}} \
+ // expected-note {{evaluates to ''\a' (0x07) == '\t' (0x09)'}}
 
   /// Bools are printed as bools.
   constexpr bool invert(bool b) {
Index: clang/test/SemaCXX/static-assert-cxx26.cpp
===
--- clang/test/SemaCXX/static-assert-cxx26.cpp
+++ clang/test/SemaCXX/static-assert-cxx26.cpp
@@ -298,3 +298,12 @@
 Bad b; // expected-note {{in instantiation}}
 
 }
+
+namespace EscapeInDiagnostic {
+static_assert('\u{9}' == (char)1, ""); // expected-error {{failed}} \
+   // expected-note {{evaluates to ''\t' (0x09) == '' (0x01)'}}
+static_assert((char8_t)-128 == (char8_t)-123, ""); // expected-error {{failed}} \
+   // expected-note {{evaluates to ''<80>' (0x80) == '<85>' (0x85)'}}
+static_assert((char16_t)0xFEFF == (char16_t)0xDB93, ""); // expected-error {{failed}} \
+ // expected-note {{evaluates to ''' (0xFEFF) == '\xDB93' (0xDB93)'}}
+}
Index: clang/test/Lexer/cxx1z-trigraphs.cpp
===
--- clang/test/Lexer/cxx1z-trigraphs.cpp
+++ clang/test/Lexer/cxx1z-trigraphs.cpp
@@ -21,7 +21,7 @@
 
 #if !ENABLED_TRIGRAPHS
 // expected-error@11 {{}} expected-warning@11 {{trigraph ignored}}
-// expected-error@13 {{failed}} expected-warning@13 {{trigraph ignored}} expected-note@13 {{evaluates to ''?' == '#''}}
+// expected-error@13 {{failed}} expected-warning@13 {{trigraph ignored}} expected-note@13 {{evaluates to ''?' (0x3F) == '#' (0x23)'}}
 // expected-error@16 {{}}
 // expected-error@20 {{}}
 #else
Index: clang/lib/Sema/SemaDeclCXX.cpp
===
--- clang/lib/Sema/SemaDeclCXX.cpp
+++ clang/lib/Sema/SemaDeclCXX.cpp
@@ -46,6 +46,7 @@
 #include "llvm/ADT/ScopeExit.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringExtras.h"
+#include "ll

[PATCH] D157526: [clang][Sema] Remove irrelevant diagnostics from constraint satisfaction failure

2023-09-07 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet updated this revision to Diff 556227.
hazohelet marked an inline comment as done.
hazohelet added a comment.

Added comment and FIXME


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157526/new/

https://reviews.llvm.org/D157526

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/Sema/SemaConcept.cpp
  clang/test/SemaTemplate/concepts.cpp


Index: clang/test/SemaTemplate/concepts.cpp
===
--- clang/test/SemaTemplate/concepts.cpp
+++ clang/test/SemaTemplate/concepts.cpp
@@ -994,3 +994,40 @@
 }
 
 }
+
+namespace GH54678 {
+template
+concept True = true;
+
+template
+concept False = false; // expected-note 9 {{'false' evaluated to false}}
+
+template
+concept Irrelevant = false;
+
+template 
+concept ErrorRequires = requires(ErrorRequires auto x) { x; }; // 
expected-error {{unknown type name 'ErrorRequires'}}
+
+template void aaa(T t) // expected-note {{candidate template ignored: 
constraints not satisfied}}
+requires (False || False) || False {} // expected-note 3 {{'int' does 
not satisfy 'False'}}
+template void bbb(T t) // expected-note {{candidate template ignored: 
constraints not satisfied}}
+requires (False || False) && True {} // expected-note 2 {{'long' does 
not satisfy 'False'}}
+template void ccc(T t) // expected-note {{candidate template ignored: 
constraints not satisfied}}
+requires (True || Irrelevant) && False {} // expected-note 
{{'unsigned long' does not satisfy 'False'}}
+template void ddd(T t) // expected-note {{candidate template ignored: 
constraints not satisfied}}
+requires (Irrelevant || True) && False {} // expected-note {{'int' 
does not satisfy 'False'}}
+template void eee(T t) // expected-note {{candidate template ignored: 
constraints not satisfied}}
+requires (Irrelevant || Irrelevant || True) && False {} // 
expected-note {{'long' does not satisfy 'False'}}
+
+template void fff(T t) // expected-note {{candidate template ignored: 
constraints not satisfied}}
+requires((ErrorRequires || False || True) && False) {} // 
expected-note {{'unsigned long' does not satisfy 'False'}}
+
+void test() {
+aaa(42); // expected-error {{no matching function}}
+bbb(42L); // expected-error{{no matching function}}
+ccc(42UL); // expected-error {{no matching function}}
+ddd(42); // expected-error {{no matching function}}
+eee(42L); // expected-error {{no matching function}}
+fff(42UL); // expected-error {{no matching function}}
+}
+}
Index: clang/lib/Sema/SemaConcept.cpp
===
--- clang/lib/Sema/SemaConcept.cpp
+++ clang/lib/Sema/SemaConcept.cpp
@@ -185,6 +185,7 @@
   ConstraintExpr = ConstraintExpr->IgnoreParenImpCasts();
 
   if (LogicalBinOp BO = ConstraintExpr) {
+auto EffectiveDetailEnd = Satisfaction.Details.end();
 ExprResult LHSRes = calculateConstraintSatisfaction(
 S, BO.getLHS(), Satisfaction, Evaluator);
 
@@ -218,6 +219,19 @@
 if (RHSRes.isInvalid())
   return ExprError();
 
+bool IsRHSSatisfied = Satisfaction.IsSatisfied;
+// Current implementation adds diagnostic information about the falsity
+// of each false atomic constraint expression when it evaluates them.
+// When the evaluation results to `false || true`, the information
+// generated during the evaluation of left-hand side is meaningless
+// because the whole expression evaluates to true.
+// The following code removes the irrelevant diagnostic information.
+// FIXME: We should probably delay the addition of diagnostic information
+// until we know the entire expression is false.
+if (BO.isOr() && IsRHSSatisfied)
+  Satisfaction.Details.erase(EffectiveDetailEnd,
+ Satisfaction.Details.end());
+
 return BO.recreateBinOp(S, LHSRes, RHSRes);
   }
 
Index: clang/docs/ReleaseNotes.rst
===
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -166,6 +166,9 @@
   Also clang no longer emits false positive warnings about the output length of
   ``%g`` format specifier.
 - Clang now emits ``-Wcast-qual`` for functional-style cast expressions.
+- Clang no longer emits irrelevant notes about unsatisfied constraint 
expressions
+  on the left-hand side of ``||`` when the right-hand side constraint is 
satisfied.
+  (`#54678: `_).
 
 Bug Fixes in This Version
 -


Index: clang/test/SemaTemplate/concepts.cpp
===
--- clang/test/SemaTemplate/concepts.cpp
+++ clang/test/SemaTemplate/concepts.cpp
@@ -994,3 +994,40 @@
 }
 
 }
+
+namespace GH54678 {
+template
+concept True = true;
+
+template
+concept False = false; // expected-note 9 {{'false' evaluated to false}}
+
+template
+concept Irrelevant = false;
+
+template 
+concept ErrorRequires = requires(ErrorRequires 

[PATCH] D159138: [clang][Sema] Fix format size estimator's handling of %o, %x, %X with alternative form

2023-09-08 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

Ping


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D159138/new/

https://reviews.llvm.org/D159138

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D156045: [clang][Interp] Enable existing source_location tests

2023-09-08 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

I was rebasing D155064  locally and saw test 
failures for the new interpreter in this file.
The cause seems to me that the new interpreter is not handling some cases of 
`SourceLocExpr` correctly when they appear inside constant-evaluated or 
immediate-function context.
e.g.

  struct A {
    int n = __builtin_LINE();
  };
  struct B {
    A a = {};
  };
  #line 100
  consteval void f() {
    constexpr B c = {};
    static_assert(c.a.n == 101, "");
  }

Live demo: https://godbolt.org/z/9Y7bzj56G

D155064  pushes constant-evaluated context 
against initializers of `constexpr` variables, so this bug appears in the test 
file.
I'm not in any hurry to push D155064  
upstream, but if it might take several weeks for this bug to be fixed, I'd like 
this change to be reverted until it gets fixed.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156045/new/

https://reviews.llvm.org/D156045

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D156045: [clang][Interp] Enable existing source_location tests

2023-09-09 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

@tbaeder Now it's working correctly. Thanks!


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156045/new/

https://reviews.llvm.org/D156045

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D159138: [clang][Sema] Fix format size estimator's handling of %o, %x, %X with alternative form

2023-09-09 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet marked 4 inline comments as done.
hazohelet added a comment.

Thanks for the review!




Comment at: clang/docs/ReleaseNotes.rst:125
 * ``-Woverriding-t-option`` is renamed to ``-Woverriding-option``.
 * ``-Winterrupt-service-routine`` is renamed to ``-Wexcessive-regsave`` as a 
generalization
 

serge-sans-paille wrote:
> ??
I'm not seeing any diff around here, so perhaps you were seeing some 
Phabricator bug?



Comment at: clang/test/Sema/warn-fortify-source.c:100-102
+  __builtin_snprintf(buf, 2, "%#x", n);
+  __builtin_snprintf(buf, 2, "%#X", n);
+  __builtin_snprintf(buf, 2, "%#o", n);

nickdesaulniers wrote:
> hazohelet wrote:
> > nickdesaulniers wrote:
> > > Note that GCC -Wformat-truncation can warn on some of these.
> > > 
> > > https://godbolt.org/z/jE3axWe1W
> > > 
> > > Looks like the diagnostic keeps an up and lower bounds on the estimated 
> > > format string expansion.
> > > 
> > > Trunk for Clang also warns for these, so is this change a regression? Or 
> > > are both GCC and Clang (trunk) incorrect?
> > Clang trunk is saying something factually incorrect because it says the 
> > output `will always be truncated`, when in fact `__builtin_snprintf(buf, 2, 
> > "%#x", n);` doesn't trigger truncation if `n` is zero.
> > 
> > GCC is correct but is more conservative than clang's `ALWAYS be truncated` 
> > diagnostics.
> > GCC's warning message is `... directive output MAY BE truncated` .
> > GCC doesn't warn on it when `n` is known to be zero. 
> > (https://godbolt.org/z/E51a3Pfhr)
> > 
> > GCC's behavior makes sense here because the truncation does happen whenever 
> > `n` is not zero. If the user knows `n` is zero then they have no reason to 
> > use `%#x` specifier.
> > So, I think it makes sense to assume `n` is not zero and emit diagnostics, 
> > but it definitely needs diagnostics rewording like `is likely to be 
> > truncated`.
> I see; thanks for the explanation. Sorry for the code review delay; I missed 
> this comment in my inbox.
As a future plan, I think it's generally good to follow GCC's behavior 
regarding this format warning considering GCC's high smartness on it.
I want to emit some warning here like GCC does because users are highly likely 
to expect non-zero value to be printed, but I think it's reasonable to defer it 
for now because it's not ideal to add a new warning flag when we are 
considering changing this warning's flag from `Wfortify-source` to 
`Wformat-truncation`


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D159138/new/

https://reviews.llvm.org/D159138

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D157526: [clang][Sema] Remove irrelevant diagnostics from constraint satisfaction failure

2023-09-09 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

When I first ran `git clang-format` locally, it somehow forced 4-space indent.
After starting to use clang-format from the trunk, I haven't seen the 
suspicious behavior locally, but the CI format check failure might be caused by 
that.
The format seems okay as-is, so I'll push this change without addressing the CI 
clang-format failure.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157526/new/

https://reviews.llvm.org/D157526

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D159138: [clang][Sema] Fix format size estimator's handling of %o, %x, %X with alternative form

2023-09-11 Thread Takuya Shimizu via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
hazohelet marked an inline comment as done.
Closed by commit rG72f6abb9bca6: [clang][Sema] Fix format size estimator's 
handling of %o, %x, %X with… (authored by hazohelet).

Changed prior to commit:
  https://reviews.llvm.org/D159138?vs=555043&id=556516#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D159138/new/

https://reviews.llvm.org/D159138

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/Sema/SemaChecking.cpp
  clang/test/Sema/warn-fortify-source.c

Index: clang/test/Sema/warn-fortify-source.c
===
--- clang/test/Sema/warn-fortify-source.c
+++ clang/test/Sema/warn-fortify-source.c
@@ -85,7 +85,7 @@
   __builtin_memset(buf, 0xff, 11); // expected-warning {{'memset' will always overflow; destination buffer has size 10, but size argument is 11}}
 }
 
-void call_snprintf(double d) {
+void call_snprintf(double d, int n) {
   char buf[10];
   __builtin_snprintf(buf, 10, "merp");
   __builtin_snprintf(buf, 11, "merp"); // expected-warning {{'snprintf' size argument is too large; destination buffer has size 10, but size argument is 11}}
@@ -96,6 +96,13 @@
   __builtin_snprintf(buf, 1, "%.1000g", d); // expected-warning {{'snprintf' will always be truncated; specified size is 1, but format string expands to at least 2}}
   __builtin_snprintf(buf, 5, "%.1000g", d);
   __builtin_snprintf(buf, 5, "%.1000G", d);
+  __builtin_snprintf(buf, 10, " %#08x", n);
+  __builtin_snprintf(buf, 2, "%#x", n);
+  __builtin_snprintf(buf, 2, "%#X", n);
+  __builtin_snprintf(buf, 2, "%#o", n);
+  __builtin_snprintf(buf, 1, "%#x", n); // expected-warning {{'snprintf' will always be truncated; specified size is 1, but format string expands to at least 2}}
+  __builtin_snprintf(buf, 1, "%#X", n); // expected-warning {{'snprintf' will always be truncated; specified size is 1, but format string expands to at least 2}}
+  __builtin_snprintf(buf, 1, "%#o", n); // expected-warning {{'snprintf' will always be truncated; specified size is 1, but format string expands to at least 2}}
 }
 
 void call_vsnprintf(void) {
@@ -110,6 +117,13 @@
   __builtin_vsnprintf(buf, 1, "%.1000g", list); // expected-warning {{'vsnprintf' will always be truncated; specified size is 1, but format string expands to at least 2}}
   __builtin_vsnprintf(buf, 5, "%.1000g", list);
   __builtin_vsnprintf(buf, 5, "%.1000G", list);
+  __builtin_vsnprintf(buf, 10, " %#08x", list);
+  __builtin_vsnprintf(buf, 2, "%#x", list);
+  __builtin_vsnprintf(buf, 2, "%#X", list);
+  __builtin_vsnprintf(buf, 2, "%#o", list);
+  __builtin_vsnprintf(buf, 1, "%#x", list); // expected-warning {{'vsnprintf' will always be truncated; specified size is 1, but format string expands to at least 2}}
+  __builtin_vsnprintf(buf, 1, "%#X", list); // expected-warning {{'vsnprintf' will always be truncated; specified size is 1, but format string expands to at least 2}}
+  __builtin_vsnprintf(buf, 1, "%#o", list); // expected-warning {{'vsnprintf' will always be truncated; specified size is 1, but format string expands to at least 2}}
 }
 
 void call_sprintf_chk(char *buf) {
@@ -147,7 +161,7 @@
   __builtin___sprintf_chk(buf, 1, 2, "%%");
   __builtin___sprintf_chk(buf, 1, 1, "%%"); // expected-warning {{'sprintf' will always overflow; destination buffer has size 1, but format string expands to at least 2}}
   __builtin___sprintf_chk(buf, 1, 4, "%#x", 9);
-  __builtin___sprintf_chk(buf, 1, 3, "%#x", 9); // expected-warning {{'sprintf' will always overflow; destination buffer has size 3, but format string expands to at least 4}}
+  __builtin___sprintf_chk(buf, 1, 3, "%#x", 9);
   __builtin___sprintf_chk(buf, 1, 4, "%p", (void *)9);
   __builtin___sprintf_chk(buf, 1, 3, "%p", (void *)9); // expected-warning {{'sprintf' will always overflow; destination buffer has size 3, but format string expands to at least 4}}
   __builtin___sprintf_chk(buf, 1, 3, "%+d", 9);
@@ -184,7 +198,7 @@
   sprintf(buf, "1234%lld", 9ll);
   sprintf(buf, "12345%lld", 9ll); // expected-warning {{'sprintf' will always overflow; destination buffer has size 6, but format string expands to at least 7}}
   sprintf(buf, "12%#x", 9);
-  sprintf(buf, "123%#x", 9); // expected-warning {{'sprintf' will always overflow; destination buffer has size 6, but format string expands to at least 7}}
+  sprintf(buf, "123%#x", 9);
   sprintf(buf, "12%p", (void *)9);
   sprintf(buf, "123%p", (void *)9); // expected-warning {{'sprintf' will always overflow; destination buffer has size 6, but format string expands to at least 7}}
   sprintf(buf, "123%+d", 9);
Index: clang/lib/Sema/SemaChecking.cpp
===
--- clang/lib/Sema/SemaChecking.cpp
+++ clang/lib/Sema/SemaChecking.cpp
@@ -890,6 +890,8 @@
 // %g style conversion switches between %f or %e st

[PATCH] D158472: [clang][Diagnostics] Emit fix-it hint separately on overload resolution failure

2023-09-13 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

Ping


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D158472/new/

https://reviews.llvm.org/D158472

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D158472: [clang][Diagnostics] Emit fix-it hint separately on overload resolution failure

2023-09-18 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet abandoned this revision.
hazohelet added a comment.

In D158472#4645378 , @aaron.ballman 
wrote:

> In D158472#4605228 , @hazohelet 
> wrote:
>
>> This breaks the one-note-for-one-overload-candidate rule of overload 
>> resolution failure diagnostics 
>> (https://github.com/llvm/llvm-project/blob/ff08c8e57e39d7970b65637595cdc221901f4ed1/clang/lib/Sema/SemaOverload.cpp#L11517-L11526),
>>  but in most cases this change would produce less lines of diagnostics.
>> If we don't like this additional note, we could instead suppress the fix-it 
>> diagnostics when the fix-it location and the diagnosed candidate location 
>> are not close (for example, more than 2 lines away).
>
> I'm uncomfortable with starting down the slippery slope of multiple notes per 
> overload resolution failure; that leads to an explosion of extra diagnostics 
> which makes it harder to spot the issue in the first place. My inclination is 
> to try to resolve this by fixing the way we emit fix-it diagnostics so that 
> we remove the extra whitespace from there because this seems like it's a 
> general problem with fix-its. Or am I wrong about this being a general issue? 
> CC @cjdb for opinions as well

I agree that the ideal fix would be a systematic change in how we display 
fix-it hints, or for that matter, distant source ranges in general.
We probably want output that looks like this:

  :1:6: note: candidate function not viable: no known conversion from 
'int' to 'int *' for 1st argument; take the address of the argument with &
  1 | void ptr(int *p);
|  ^   ~~
  ---
  5 |   ptr(n);
|   
|   &

In any case, suppressing fix-it hints would be disastrous for LSP things like 
clangd that use clang diagnostic output. So, this is a problem of how clang 
diagnostic engine display fix-it hints.




Comment at: clang/test/Sema/overloadable.c:31
+  accept_funcptr(f2); // expected-error{{no matching function for call to 
'accept_funcptr'}} \
+  // expected-note 2{{take the address of the argument 
with &}}
 }

aaron.ballman wrote:
> Something odd is going on -- why is the note emitted twice?
There are two candidates, both of which would become viable if we take the 
address of them. (https://godbolt.org/z/6onn9x89a)


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D158472/new/

https://reviews.llvm.org/D158472

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D157526: [clang][Sema] Remove irrelevant diagnostics from constraint satisfaction failure

2023-09-18 Thread Takuya Shimizu via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGb2cd9db58933: [clang][Sema] Remove irrelevant diagnostics 
from constraint satisfaction failure (authored by hazohelet).

Changed prior to commit:
  https://reviews.llvm.org/D157526?vs=556227&id=556936#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157526/new/

https://reviews.llvm.org/D157526

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/Sema/SemaConcept.cpp
  clang/test/SemaTemplate/concepts.cpp


Index: clang/test/SemaTemplate/concepts.cpp
===
--- clang/test/SemaTemplate/concepts.cpp
+++ clang/test/SemaTemplate/concepts.cpp
@@ -994,3 +994,40 @@
 }
 
 }
+
+namespace GH54678 {
+template
+concept True = true;
+
+template
+concept False = false; // expected-note 9 {{'false' evaluated to false}}
+
+template
+concept Irrelevant = false;
+
+template 
+concept ErrorRequires = requires(ErrorRequires auto x) { x; }; // 
expected-error {{unknown type name 'ErrorRequires'}}
+
+template void aaa(T t) // expected-note {{candidate template ignored: 
constraints not satisfied}}
+requires (False || False) || False {} // expected-note 3 {{'int' does 
not satisfy 'False'}}
+template void bbb(T t) // expected-note {{candidate template ignored: 
constraints not satisfied}}
+requires (False || False) && True {} // expected-note 2 {{'long' does 
not satisfy 'False'}}
+template void ccc(T t) // expected-note {{candidate template ignored: 
constraints not satisfied}}
+requires (True || Irrelevant) && False {} // expected-note 
{{'unsigned long' does not satisfy 'False'}}
+template void ddd(T t) // expected-note {{candidate template ignored: 
constraints not satisfied}}
+requires (Irrelevant || True) && False {} // expected-note {{'int' 
does not satisfy 'False'}}
+template void eee(T t) // expected-note {{candidate template ignored: 
constraints not satisfied}}
+requires (Irrelevant || Irrelevant || True) && False {} // 
expected-note {{'long' does not satisfy 'False'}}
+
+template void fff(T t) // expected-note {{candidate template ignored: 
constraints not satisfied}}
+requires((ErrorRequires || False || True) && False) {} // 
expected-note {{'unsigned long' does not satisfy 'False'}}
+
+void test() {
+aaa(42); // expected-error {{no matching function}}
+bbb(42L); // expected-error{{no matching function}}
+ccc(42UL); // expected-error {{no matching function}}
+ddd(42); // expected-error {{no matching function}}
+eee(42L); // expected-error {{no matching function}}
+fff(42UL); // expected-error {{no matching function}}
+}
+}
Index: clang/lib/Sema/SemaConcept.cpp
===
--- clang/lib/Sema/SemaConcept.cpp
+++ clang/lib/Sema/SemaConcept.cpp
@@ -185,6 +185,7 @@
   ConstraintExpr = ConstraintExpr->IgnoreParenImpCasts();
 
   if (LogicalBinOp BO = ConstraintExpr) {
+auto EffectiveDetailEnd = Satisfaction.Details.end();
 ExprResult LHSRes = calculateConstraintSatisfaction(
 S, BO.getLHS(), Satisfaction, Evaluator);
 
@@ -218,6 +219,19 @@
 if (RHSRes.isInvalid())
   return ExprError();
 
+bool IsRHSSatisfied = Satisfaction.IsSatisfied;
+// Current implementation adds diagnostic information about the falsity
+// of each false atomic constraint expression when it evaluates them.
+// When the evaluation results to `false || true`, the information
+// generated during the evaluation of left-hand side is meaningless
+// because the whole expression evaluates to true.
+// The following code removes the irrelevant diagnostic information.
+// FIXME: We should probably delay the addition of diagnostic information
+// until we know the entire expression is false.
+if (BO.isOr() && IsRHSSatisfied)
+  Satisfaction.Details.erase(EffectiveDetailEnd,
+ Satisfaction.Details.end());
+
 return BO.recreateBinOp(S, LHSRes, RHSRes);
   }
 
Index: clang/docs/ReleaseNotes.rst
===
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -169,6 +169,9 @@
   Also clang no longer emits false positive warnings about the output length of
   ``%g`` format specifier and about ``%o, %x, %X`` with ``#`` flag.
 - Clang now emits ``-Wcast-qual`` for functional-style cast expressions.
+- Clang no longer emits irrelevant notes about unsatisfied constraint 
expressions
+  on the left-hand side of ``||`` when the right-hand side constraint is 
satisfied.
+  (`#54678: `_).
 
 Bug Fixes in This Version
 -


Index: clang/test/SemaTemplate/concepts.cpp
===
--- clang/test/SemaTemplate/concepts.cpp
+++ clang/test/SemaTemp

[PATCH] D155064: [clang][SemaCXX] Diagnose tautological uses of consteval if and is_constant_evaluated

2023-09-18 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet updated this revision to Diff 556946.
hazohelet added a comment.

After discussion in https://github.com/llvm/llvm-project/pull/66222, I noticed 
I had misunderstood the recursiveness of constant-evaluated context.
I was pushing constant-evaluating on the lambda body when it appears inside 
constant-evaluated context. This was causing false positive in

  constexpr auto cexpr_lambda = []() {
    return __builtin_is_constant_evaluated();
  }

I fixed this by stopping pushing constant-evaluated context (See 
`SemaLambda.cpp`). Instead, I am pushing immediate-function context when the 
outer evaluation context is immediate function context as well as when it's 
consteval lambda so that we can warn on

  consteval void f() {
auto lam = []() { return __builtin_is_constant_evaluated(); };
  }

@cor3ntin
Should this patch wait for https://github.com/llvm/llvm-project/pull/66222?


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155064/new/

https://reviews.llvm.org/D155064

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Basic/DiagnosticASTKinds.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/include/clang/Parse/Parser.h
  clang/include/clang/Sema/Sema.h
  clang/lib/AST/ExprConstant.cpp
  clang/lib/Parse/ParseDecl.cpp
  clang/lib/Parse/ParseDeclCXX.cpp
  clang/lib/Parse/ParseExpr.cpp
  clang/lib/Parse/ParseExprCXX.cpp
  clang/lib/Parse/ParseStmt.cpp
  clang/lib/Sema/SemaChecking.cpp
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/lib/Sema/SemaLambda.cpp
  clang/lib/Sema/SemaStmt.cpp
  clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
  clang/test/AST/Interp/builtins.cpp
  clang/test/AST/Interp/if.cpp
  clang/test/AST/Interp/literals.cpp
  clang/test/CXX/expr/expr.const/p2-0x.cpp
  clang/test/CXX/expr/expr.const/p6-2a.cpp
  clang/test/CXX/expr/expr.prim/expr.prim.lambda/p3.cpp
  clang/test/CXX/stmt.stmt/stmt.select/stmt.if/p4.cpp
  clang/test/Parser/pragma-fenv_access.c
  clang/test/SemaCXX/constant-conversion.cpp
  clang/test/SemaCXX/constant-expression-cxx11.cpp
  clang/test/SemaCXX/cxx2a-consteval.cpp
  clang/test/SemaCXX/cxx2b-consteval-if.cpp
  clang/test/SemaCXX/cxx2b-consteval-propagate.cpp
  clang/test/SemaCXX/fixit-tautological-meta-constant.cpp
  clang/test/SemaCXX/vartemplate-lambda.cpp
  clang/test/SemaCXX/warn-constant-evaluated-constexpr.cpp
  clang/test/SemaCXX/warn-tautological-meta-constant.cpp
  clang/test/SemaTemplate/concepts.cpp
  clang/unittests/Support/TimeProfilerTest.cpp
  libcxx/include/__type_traits/is_constant_evaluated.h
  
libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
  
libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp

Index: libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp
===
--- libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp
+++ libcxx/test/std/utilities/meta/meta.const.eval/is_constant_evaluated.verify.cpp
@@ -24,7 +24,7 @@
 #else
   // expected-error-re@+1 (static_assert|static assertion)}} failed}}
   static_assert(!std::is_constant_evaluated(), "");
-  // expected-warning@-1 0-1 {{'std::is_constant_evaluated' will always evaluate to 'true' in a manifestly constant-evaluated expression}}
+  // expected-warning-re@-1 0-1 {{'std::is_constant_evaluated' will always evaluate to {{('true' in a manifestly constant-evaluated expression|true in this context)
 #endif
   return 0;
 }
Index: libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
===
--- libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
+++ libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.segmented.pass.cpp
@@ -93,12 +93,10 @@
 }
 
 int main(int, char**) {
-  if (!std::is_constant_evaluated()) {
-test_containers, std::deque>();
-test_containers, std::vector>();
-test_containers, std::deque>();
-test_containers, std::vector>();
-  }
+  test_containers, std::deque>();
+  test_containers, std::vector>();
+  test_containers, std::deque>();
+  test_containers, std::vector>();
 
   types::for_each(types::forward_iterator_list{}, [] {
 test_join_view();
Index: libcxx/include/__type_traits/is_constant_evaluated.h
===
--- libcxx/include/__type_traits/is_constant_evaluated.h
+++ libcxx/include/__type_traits/is_constant_evaluated.h
@@ -24,7 +24,14 @@
 #endif
 
 _LIBCPP_HIDE_FROM_ABI inline _LIBCPP_CONSTEXPR bool __libcpp_is_constant_evaluated() _NOEXCEPT {
+// __builtin_is_constant_evaluated() in this function always evaluates to false in pre-C++11 mode
+// because this function is not constexpr-qualified.
+// The following macro use clarifies this and avoids warnings from compiler

[PATCH] D152093: [clang][Analysis] Handle && and || against variable and its negation as tautology

2023-07-03 Thread Takuya Shimizu via Phabricator via cfe-commits
hazohelet added a comment.

Ping


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D152093/new/

https://reviews.llvm.org/D152093

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


  1   2   3   >