Skip to content

Commit 69fb96b

Browse files
authored
[clang][bytecode] __builtin_is_aligned needs a declaration (#188011)
Reject pointers to expressions. Also remove the bytecode version of this test.
1 parent 621fc87 commit 69fb96b

File tree

3 files changed

+20
-249
lines changed

3 files changed

+20
-249
lines changed

clang/lib/AST/ByteCode/InterpBuiltin.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,12 +1221,23 @@ static bool interp__builtin_is_aligned_up_down(InterpState &S, CodePtr OpPC,
12211221
if (!Ptr.isBlockPointer())
12221222
return false;
12231223

1224+
const ValueDecl *PtrDecl = Ptr.getDeclDesc()->asValueDecl();
1225+
// We need a pointer for a declaration here.
1226+
if (!PtrDecl) {
1227+
if (BuiltinOp == Builtin::BI__builtin_is_aligned)
1228+
S.FFDiag(Call->getArg(0), diag::note_constexpr_alignment_compute)
1229+
<< Alignment;
1230+
else
1231+
S.FFDiag(Call->getArg(0), diag::note_constexpr_alignment_adjust)
1232+
<< Alignment;
1233+
return false;
1234+
}
1235+
12241236
// For one-past-end pointers, we can't call getIndex() since it asserts.
12251237
// Use getNumElems() instead which gives the correct index for past-end.
12261238
unsigned PtrOffset =
12271239
Ptr.isElementPastEnd() ? Ptr.getNumElems() : Ptr.getIndex();
1228-
CharUnits BaseAlignment =
1229-
S.getASTContext().getDeclAlign(Ptr.getDeclDesc()->asValueDecl());
1240+
CharUnits BaseAlignment = S.getASTContext().getDeclAlign(PtrDecl);
12301241
CharUnits PtrAlign =
12311242
BaseAlignment.alignmentAtOffset(CharUnits::fromQuantity(PtrOffset));
12321243

clang/test/AST/ByteCode/builtin-align-cxx.cpp

Lines changed: 0 additions & 247 deletions
This file was deleted.

clang/test/SemaCXX/builtin-align-cxx.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// C++-specific checks for the alignment builtins
22
// RUN: %clang_cc1 -triple=x86_64-unknown-unknown -std=c++11 -o - %s -fsyntax-only -verify
3+
// RUN: %clang_cc1 -triple=x86_64-unknown-unknown -std=c++11 -o - %s -fsyntax-only -verify -fexperimental-new-constant-interpreter
34

45
// Check that we don't crash when using dependent types in __builtin_align:
56
template <typename a, a b>
@@ -239,5 +240,11 @@ static_assert(!__builtin_is_aligned(static_cast<unsigned long>(7), static_cast<s
239240
static_assert(!__builtin_is_aligned(static_cast<signed long>(7), static_cast<unsigned short>(4)), "");
240241
static_assert(!__builtin_is_aligned(static_cast<unsigned short>(7), static_cast<signed long>(4)), "");
241242

243+
// Check that one-past-end pointers work correctly (GH#178647).
244+
static_assert(__builtin_align_up(&align32array[128], 4) == align32array + 128, "");
245+
242246
// Check the diagnostic message
243247
_Alignas(void) char align_void_array[1]; // expected-error {{invalid application of '_Alignas' to an incomplete type 'void'}}
248+
249+
static_assert(!__builtin_is_aligned(&"", 4), ""); // expected-error {{not an integral constant expression}} \
250+
// expected-note {{cannot constant evaluate whether run-time alignment is at least 4}}

0 commit comments

Comments
 (0)