]> granicus.if.org Git - clang/commitdiff
[Sema] Warn when alignof is used with __builtin_alloca_with_align
authorDavid Majnemer <david.majnemer@gmail.com>
Mon, 31 Oct 2016 18:07:57 +0000 (18:07 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Mon, 31 Oct 2016 18:07:57 +0000 (18:07 +0000)
The second argument to __builtin_alloca_with_align is supposed to be in
bits, not bytes.  Using alignof there would be indicative of a bug.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@285609 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaChecking.cpp
test/Sema/builtin-alloca-with-align.c

index 085008753f9acff33e9c0d7cc77286bd694e9d45..9d7cc015a4775fc34193fcb03de35b91f96a026f 100644 (file)
@@ -2440,6 +2440,9 @@ def err_no_accessor_for_property : Error<
 def error_cannot_find_suitable_accessor : Error<
   "cannot find suitable %select{getter|setter}0 for property %1">;
 
+def warn_alloca_align_alignof : Warning<
+  "second argument to __builtin_alloca_with_align is supposed to be in bits">;
+
 def err_alignment_too_small : Error<
   "requested alignment must be %0 or greater">;
 def err_alignment_too_big : Error<
index a4e3c5b8b24cd9094c0cfce342eed5fbb34ab116..4d9ad78189b3c6ba34cd17f268ab1e363254d2bf 100644 (file)
@@ -3907,7 +3907,7 @@ bool Sema::SemaBuiltinAssume(CallExpr *TheCall) {
   return false;
 }
 
-/// Handle __builtin_assume_aligned. This is declared
+/// Handle __builtin_alloca_with_align. This is declared
 /// as (size_t, size_t) where the second size_t must be a power of 2 greater
 /// than 8.
 bool Sema::SemaBuiltinAllocaWithAlign(CallExpr *TheCall) {
@@ -3916,6 +3916,12 @@ bool Sema::SemaBuiltinAllocaWithAlign(CallExpr *TheCall) {
 
   // We can't check the value of a dependent argument.
   if (!Arg->isTypeDependent() && !Arg->isValueDependent()) {
+    if (const auto *UE =
+            dyn_cast<UnaryExprOrTypeTraitExpr>(Arg->IgnoreParenImpCasts()))
+      if (UE->getKind() == UETT_AlignOf)
+        Diag(TheCall->getLocStart(), diag::warn_alloca_align_alignof)
+          << Arg->getSourceRange();
+
     llvm::APSInt Result = Arg->EvaluateKnownConstInt(Context);
 
     if (!Result.isPowerOf2())
index 67dc09d58ba073b42d88835b7d3718741150ab4f..16d71da60080c6f248291858c4855405e3c82924 100644 (file)
@@ -27,3 +27,7 @@ void test6(int a, int j) {
 void test7(int a) {
   __builtin_alloca_with_align(a, 2); // expected-error {{requested alignment must be 8 or greater}}
 }
+
+void test8() {
+  __builtin_alloca_with_align(sizeof(__INT64_TYPE__), __alignof__(__INT64_TYPE__)); // expected-warning {{second argument to __builtin_alloca_with_align is supposed to be in bits}}
+}