Skip to content

Commit c2fea4c

Browse files
authored
[C++] Fix parsing of _Alignas in local declarations (#81915)
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.
1 parent 2e585c4 commit c2fea4c

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,9 @@ Bug Fixes to C++ Support
259259
some of (`#80510 <https://github.com/llvm/llvm-project/issues/80510>`).
260260
- Clang now ignores top-level cv-qualifiers on function parameters in template partial orderings.
261261
(`#75404 <https://github.com/llvm/llvm-project/issues/75404>`_)
262+
- No longer reject valid use of the ``_Alignas`` specifier when declaring a
263+
local variable, which is supported as a C11 extension in C++. Previously, it
264+
was only accepted at namespace scope but not at local function scope.
262265

263266
Bug Fixes to AST Handling
264267
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Parse/ParseTentative.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1842,6 +1842,9 @@ Parser::isCXXDeclarationSpecifier(ImplicitTypenameContext AllowImplicitTypename,
18421842
#include "clang/Basic/TransformTypeTraits.def"
18431843
return TPResult::True;
18441844

1845+
// C11 _Alignas
1846+
case tok::kw__Alignas:
1847+
return TPResult::True;
18451848
// C11 _Atomic
18461849
case tok::kw__Atomic:
18471850
return TPResult::True;

clang/test/SemaCXX/_Alignas.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: %clang_cc1 %s -fsyntax-only -verify=expected,cpp
2+
// RUN: %clang_cc1 -x c %s -fsyntax-only -verify=expected,c
3+
4+
// Ensure that we correctly parse _Alignas as an extension in C++.
5+
_Alignas(64) int i1;
6+
_Alignas(long long) int i2;
7+
int volatile _Alignas(64) i3; // Test strange ordering
8+
9+
void foo(void) {
10+
// We previously rejected these valid declarations.
11+
_Alignas(8) char i4;
12+
_Alignas(char) char i5;
13+
14+
(void)(int _Alignas(64))0; // expected-warning {{'_Alignas' attribute ignored when parsing type}}
15+
// FIXME: C and C++ should both diagnose the same way, as being ignored.
16+
(void)(_Alignas(64) int)0; // c-error {{expected expression}} \
17+
cpp-warning {{'_Alignas' attribute ignored when parsing type}}
18+
}
19+
20+
struct S {
21+
_Alignas(int) int i;
22+
_Alignas(64) int j;
23+
};
24+
25+
void bar(_Alignas(8) char c1, char _Alignas(char) c2); // expected-error 2 {{'_Alignas' attribute cannot be applied to a function parameter}}

0 commit comments

Comments
 (0)