Skip to content

Commit 8f77621

Browse files
authored
[clang] Convert second arg of __builtin_assume_aligned to ConstantExpr (llvm#161314)
Since the second argument must be a constant integer, we can as well convert it to a `ConstantExpr` in Sema. Fixes llvm#161272
1 parent cd0f560 commit 8f77621

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ Non-comprehensive list of changes in this release
246246

247247
- ``__builtin_assume_dereferenceable`` now accepts non-constant size operands.
248248

249+
- Fixed a crash when the second argument to ``__builtin_assume_aligned`` was not constant (#GH161314)
250+
249251
New Compiler Flags
250252
------------------
251253
- New option ``-fno-sanitize-debug-trap-reasons`` added to disable emitting trap reasons into the debug info when compiling with trapping UBSan (e.g. ``-fsanitize-trap=undefined``).

clang/lib/Sema/SemaChecking.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5954,6 +5954,9 @@ bool Sema::BuiltinAssumeAligned(CallExpr *TheCall) {
59545954
if (Result > Sema::MaximumAlignment)
59555955
Diag(TheCall->getBeginLoc(), diag::warn_assume_aligned_too_great)
59565956
<< SecondArg->getSourceRange() << Sema::MaximumAlignment;
5957+
5958+
TheCall->setArg(1,
5959+
ConstantExpr::Create(Context, SecondArg, APValue(Result)));
59575960
}
59585961

59595962
if (NumArgs > 2) {

clang/test/SemaCXX/builtin-assume-aligned.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,9 @@ constexpr void *s1 = __builtin_assume_aligned(x, 32);
4747
constexpr void *s2 = __builtin_assume_aligned(x, 32, 5);
4848
constexpr void *s3 = __builtin_assume_aligned(x, 32, -1);
4949

50+
51+
constexpr int add(int a, int b) {
52+
return a+b;
53+
}
54+
constexpr void *c1 = __builtin_assume_aligned(p, add(1,1));
55+
constexpr void *c2 = __builtin_assume_aligned(p, add(2,1)); // expected-error {{not a power of 2}}

0 commit comments

Comments
 (0)