[clang] 1a42255 - [C2y] Modify diagnostics for complex increment/decrement

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 2 06:08:58 PDT 2024


Author: Aaron Ballman
Date: 2024-07-02T09:08:44-04:00
New Revision: 1a422553f9ed05ee57463ed5554cfd7eeea46800

URL: https://github.com/llvm/llvm-project/commit/1a422553f9ed05ee57463ed5554cfd7eeea46800
DIFF: https://github.com/llvm/llvm-project/commit/1a422553f9ed05ee57463ed5554cfd7eeea46800.diff

LOG: [C2y] Modify diagnostics for complex increment/decrement

Clang implemented WG14 N3259 as an extension, now it's a feature of C2y.

Added: 
    clang/test/C/C2y/n3259.c

Modified: 
    clang/include/clang/Basic/DiagnosticSemaKinds.td
    clang/lib/Sema/SemaExpr.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index c852f1893fe14..3d3e98c7b751b 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -7664,8 +7664,12 @@ def ext_gnu_ptr_func_arith : Extension<
   InGroup<GNUPointerArith>;
 def err_readonly_message_assignment : Error<
   "assigning to 'readonly' return result of an Objective-C message not allowed">;
-def ext_increment_complex : Extension<
-  "'%select{--|++}0' on an object of complex type is a Clang extension">;
+def ext_c2y_increment_complex : Extension<
+  "'%select{--|++}0' on an object of complex type is a C2y extension">,
+  InGroup<C2y>;
+def warn_c2y_compat_increment_complex : Warning<
+  "'%select{--|++}0' on an object of complex type is incompatible with C "
+  "standards before C2y">, InGroup<CPre2yCompat>, DefaultIgnore;
 def ext_integer_complement_complex : Extension<
   "ISO C does not support '~' for complex conjugation of %0">;
 def err_nosetter_property_assignment : Error<

diff  --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index cc811a40185ce..1437fa08d2e7a 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -13764,8 +13764,9 @@ static QualType CheckIncrementDecrementOperand(Sema &S, Expr *Op,
       return QualType();
   } else if (ResType->isAnyComplexType()) {
     // C99 does not support ++/-- on complex types, we allow as an extension.
-    S.Diag(OpLoc, diag::ext_increment_complex)
-      << IsInc << Op->getSourceRange();
+    S.Diag(OpLoc, S.getLangOpts().C2y ? diag::warn_c2y_compat_increment_complex
+                                      : diag::ext_c2y_increment_complex)
+        << IsInc << Op->getSourceRange();
   } else if (ResType->isPlaceholderType()) {
     ExprResult PR = S.CheckPlaceholderExpr(Op);
     if (PR.isInvalid()) return QualType();

diff  --git a/clang/test/C/C2y/n3259.c b/clang/test/C/C2y/n3259.c
new file mode 100644
index 0000000000000..84118a76660c9
--- /dev/null
+++ b/clang/test/C/C2y/n3259.c
@@ -0,0 +1,66 @@
+// RUN: %clang_cc1 -std=c2y -Wall -pedantic -Wno-unused -Wpre-c2y-compat -verify=pre-c2y %s -emit-llvm -o - | FileCheck %s
+// RUN: %clang_cc1 -std=c23 -Wall -pedantic -Wno-unused %s -verify -emit-llvm -o - | FileCheck %s
+
+/* WG14 N3259: Yes
+ * Support ++ and -- on complex values
+ */
+
+// CHECK-LABEL: define {{.*}} void @test()
+void test() {
+  // CHECK: %[[F:.+]] = alloca { float, float }
+  // CHECK: store float 1
+  // CHECK: store float 0
+  _Complex float f = __builtin_complex(1.0f, 0.0f);
+
+  // CHECK:      %[[F_REALP1:.+]] = getelementptr inbounds { float, float }, ptr %[[F]], i32 0, i32 0
+  // CHECK-NEXT: %[[F_REAL:.+]] = load float, ptr %[[F_REALP1]]
+  // CHECK-NEXT: %[[F_IMAGP2:.+]] = getelementptr inbounds { float, float }, ptr %[[F]], i32 0, i32 1
+  // CHECK-NEXT: %[[F_IMAG:.+]] = load float, ptr %[[F_IMAGP2]]
+  // CHECK-NEXT: %[[INC:.+]] = fadd float %[[F_REAL]], 1.000000e+00
+  // CHECK-NEXT: %[[F_REALP3:.+]] = getelementptr inbounds { float, float }, ptr %[[F]], i32 0, i32 0
+  // CHECK-NEXT: %[[F_IMAGP4:.+]] = getelementptr inbounds { float, float }, ptr %[[F]], i32 0, i32 1
+  // CHECK-NEXT: store float %[[INC]], ptr %[[F_REALP3]]
+  // CHECK-NEXT: store float %[[F_IMAG]], ptr %[[F_IMAGP4]]
+  f++; /* expected-warning {{'++' on an object of complex type is a C2y extension}}
+          pre-c2y-warning {{'++' on an object of complex type is incompatible with C standards before C2y}}
+        */
+
+  // CHECK:      %[[F_REALP5:.+]] = getelementptr inbounds { float, float }, ptr %[[F]], i32 0, i32 0
+  // CHECK-NEXT: %[[F_REAL6:.+]] = load float, ptr %[[F_REALP5]]
+  // CHECK-NEXT: %[[F_IMAGP7:.+]] = getelementptr inbounds { float, float }, ptr %[[F]], i32 0, i32 1
+  // CHECK-NEXT: %[[F_IMAG8:.+]] = load float, ptr %[[F_IMAGP7]]
+  // CHECK-NEXT: %[[INC9:.+]] = fadd float %[[F_REAL6]], 1.000000e+00
+  // CHECK-NEXT: %[[F_REALP10:.+]] = getelementptr inbounds { float, float }, ptr %[[F]], i32 0, i32 0
+  // CHECK-NEXT: %[[F_IMAGP11:.+]] = getelementptr inbounds { float, float }, ptr %[[F]], i32 0, i32 1
+  // CHECK-NEXT: store float %[[INC9]], ptr %[[F_REALP10]]
+  // CHECK-NEXT: store float %[[F_IMAG8]], ptr %[[F_IMAGP11]]
+  ++f; /* expected-warning {{'++' on an object of complex type is a C2y extension}}
+          pre-c2y-warning {{'++' on an object of complex type is incompatible with C standards before C2y}}
+        */
+
+  // CHECK:      %[[F_REALP12:.+]] = getelementptr inbounds { float, float }, ptr %[[F]], i32 0, i32 0
+  // CHECK-NEXT: %[[F_REAL13:.+]] = load float, ptr %[[F_REALP12]]
+  // CHECK-NEXT: %[[F_IMAGP14:.+]] = getelementptr inbounds { float, float }, ptr %[[F]], i32 0, i32 1
+  // CHECK-NEXT: %[[F_IMAG15:.+]] = load float, ptr %[[F_IMAGP14]]
+  // CHECK-NEXT: %[[DEC:.+]] = fadd float %[[F_REAL13]], -1.000000e+00
+  // CHECK-NEXT: %[[F_REALP16:.+]] = getelementptr inbounds { float, float }, ptr %[[F]], i32 0, i32 0
+  // CHECK-NEXT: %[[F_IMAGP17:.+]] = getelementptr inbounds { float, float }, ptr %[[F]], i32 0, i32 1
+  // CHECK-NEXT: store float %[[DEC]], ptr %[[F_REALP16]]
+  // CHECK-NEXT: store float %[[F_IMAG15]], ptr %[[F_IMAGP17]]
+  f--; /* expected-warning {{'--' on an object of complex type is a C2y extension}}
+          pre-c2y-warning {{'--' on an object of complex type is incompatible with C standards before C2y}}
+        */
+
+  // CHECK:      %[[F_REALP18:.+]] = getelementptr inbounds { float, float }, ptr %[[F]], i32 0, i32 0
+  // CHECK-NEXT: %[[F_REAL19:.+]] = load float, ptr %[[F_REALP18]]
+  // CHECK-NEXT: %[[F_IMAGP20:.+]] = getelementptr inbounds { float, float }, ptr %[[F]], i32 0, i32 1
+  // CHECK-NEXT: %[[F_IMAG21:.+]] = load float, ptr %[[F_IMAGP20]]
+  // CHECK-NEXT: %[[DEC22:.+]] = fadd float %[[F_REAL19]], -1.000000e+00
+  // CHECK-NEXT: %[[F_REALP23:.+]] = getelementptr inbounds { float, float }, ptr %[[F]], i32 0, i32 0
+  // CHECK-NEXT: %[[F_IMAGP24:.+]] = getelementptr inbounds { float, float }, ptr %[[F]], i32 0, i32 1
+  // CHECK-NEXT: store float %[[DEC22]], ptr %[[F_REALP23]]
+  // CHECK-NEXT: store float %[[F_IMAG21]], ptr %[[F_IMAGP24]]
+  --f; /* expected-warning {{'--' on an object of complex type is a C2y extension}}
+          pre-c2y-warning {{'--' on an object of complex type is incompatible with C standards before C2y}}
+        */
+}


        


More information about the cfe-commits mailing list