Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C++] Fix parsing of _Alignas in local declarations #81915

Merged
merged 1 commit into from
Feb 16, 2024

Conversation

AaronBallman
Copy link
Collaborator

We support '_Alignas' from C11 as an extension in C++. However, we were not correctly parsing its use in local variable declarations. This patch addresses that issue.

We support '_Alignas' from C11 as an extension in C++. However, we were
not correctly parsing its use in local variable declarations. This
patch addresses that issue.
@AaronBallman AaronBallman added clang Clang issues not falling into any other category c++ clang:frontend Language frontend issues, e.g. anything involving "Sema" extension:clang labels Feb 15, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Feb 15, 2024

@llvm/pr-subscribers-clang

Author: Aaron Ballman (AaronBallman)

Changes

We support '_Alignas' from C11 as an extension in C++. However, we were not correctly parsing its use in local variable declarations. This patch addresses that issue.


Full diff: https://github.com/llvm/llvm-project/pull/81915.diff

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+3)
  • (modified) clang/lib/Parse/ParseTentative.cpp (+3)
  • (added) clang/test/SemaCXX/_Alignas.cpp (+25)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 608d855abf5d06..16f79a349c20c8 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -259,6 +259,9 @@ Bug Fixes to C++ Support
   some of (`#80510 <https://github.com/llvm/llvm-project/issues/80510>`).
 - Clang now ignores top-level cv-qualifiers on function parameters in template partial orderings.
   (`#75404 <https://github.com/llvm/llvm-project/issues/75404>`_)
+- No longer reject valid use of the ``_Alignas`` specifier when declaring a
+  local variable, which is supported as a C11 extension in C++. Previously, it
+  was only accepted at namespace scope but not at local function scope.
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Parse/ParseTentative.cpp b/clang/lib/Parse/ParseTentative.cpp
index 299e46f4be9a2c..ea17c3e3252ec2 100644
--- a/clang/lib/Parse/ParseTentative.cpp
+++ b/clang/lib/Parse/ParseTentative.cpp
@@ -1842,6 +1842,9 @@ Parser::isCXXDeclarationSpecifier(ImplicitTypenameContext AllowImplicitTypename,
 #include "clang/Basic/TransformTypeTraits.def"
     return TPResult::True;
 
+  // C11 _Alignas
+  case tok::kw__Alignas:
+    return TPResult::True;
   // C11 _Atomic
   case tok::kw__Atomic:
     return TPResult::True;
diff --git a/clang/test/SemaCXX/_Alignas.cpp b/clang/test/SemaCXX/_Alignas.cpp
new file mode 100644
index 00000000000000..4e892745580ed6
--- /dev/null
+++ b/clang/test/SemaCXX/_Alignas.cpp
@@ -0,0 +1,25 @@
+// RUN: %clang_cc1 %s -fsyntax-only -verify=expected,cpp
+// RUN: %clang_cc1 -x c %s -fsyntax-only -verify=expected,c
+
+// Ensure that we correctly parse _Alignas as an extension in C++.
+_Alignas(64) int i1;
+_Alignas(long long) int i2;
+int volatile _Alignas(64) i3; // Test strange ordering
+
+void foo(void) {
+  // We previously rejected these valid declarations.
+  _Alignas(8) char i4;
+  _Alignas(char) char i5;
+
+  (void)(int _Alignas(64))0; // expected-warning {{'_Alignas' attribute ignored when parsing type}}
+  // FIXME: C and C++ should both diagnose the same way, as being ignored.
+  (void)(_Alignas(64) int)0; // c-error {{expected expression}} \
+                                cpp-warning {{'_Alignas' attribute ignored when parsing type}}
+}
+
+struct S {
+  _Alignas(int) int i;
+  _Alignas(64) int j;
+};
+
+void bar(_Alignas(8) char c1, char _Alignas(char) c2); // expected-error 2 {{'_Alignas' attribute cannot be applied to a function parameter}}

_Alignas(char) char i5;

(void)(int _Alignas(64))0; // expected-warning {{'_Alignas' attribute ignored when parsing type}}
// FIXME: C and C++ should both diagnose the same way, as being ignored.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I plan to fix this in a follow-up

@AaronBallman
Copy link
Collaborator Author

CI came back green (failures are unrelated to this patch) and the changes are trivial, so landing as a code owner (I'll happily address any feedback post-commit).

@AaronBallman AaronBallman merged commit c2fea4c into llvm:main Feb 16, 2024
9 of 10 checks passed
@AaronBallman AaronBallman deleted the aballman-alignas-cpp-parsing branch February 16, 2024 12:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category extension:clang
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants