[clang] [clang] Add support for new loop attribute [[clang::code_align()]] (PR #70762)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 2 10:56:22 PDT 2023


================
@@ -0,0 +1,127 @@
+// RUN: %clang_cc1 -fsyntax-only -verify=expected,c-local -x c %s
+// RUN: %clang_cc1 -fsyntax-only -verify=expected,cpp-local -pedantic -x c++ -std=c++11 %s
+
+void foo() {
+  int i;
+  int a[10], b[10];
+
+  [[clang::code_align(8)]]
+  for (i = 0; i < 10; ++i) {  // this is OK
+    a[i] = b[i] = 0;
+  }
+  // expected-error at +1 {{'code_align' attribute only applies to 'for', 'while', and 'do' statements}}
+  [[clang::code_align(4)]]
+  i = 7;
+  for (i = 0; i < 10; ++i) {
+    a[i] = b[i] = 0;
+  }
+
+  // expected-error at +1{{'code_align' attribute cannot be applied to a declaration}}
+  [[clang::code_align(12)]] int n[10];
+}
+
+void bar(int);
+#if __cplusplus >= 201103L
+// cpp-local-note at +2 {{declared here}}
+#endif
+void foo1(int A)
+{
+  // expected-error at +1 {{'code_align' attribute must be a constant power of two between 1 and 4096 inclusive}}
+  [[clang::code_align(0)]]
+  for(int I=0; I<128; ++I) { bar(I); }
+
+  // expected-error at +1{{'code_align' attribute must be a constant power of two between 1 and 4096 inclusive}}
+  [[clang::code_align(-4)]]
+  for(int I=0; I<128; ++I) { bar(I); }
+
+#if __cplusplus >= 201103L
+    // cpp-local-error at +4 {{integral constant expression must have integral or unscoped enumeration type, not 'double'}}
+#else
+    // c-local-error at +2 {{integer constant expression must have integer type, not 'double'}}
+#endif
----------------
AaronBallman wrote:

Rather than use `#if` to surround the comments, it's easier to understand if you use a custom `-verify=` prefix that's specific to C++11 and later.

https://github.com/llvm/llvm-project/pull/70762


More information about the cfe-commits mailing list