r285609 - [Sema] Warn when alignof is used with __builtin_alloca_with_align

David Majnemer via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 31 11:07:57 PDT 2016


Author: majnemer
Date: Mon Oct 31 13:07:57 2016
New Revision: 285609

URL: http://llvm.org/viewvc/llvm-project?rev=285609&view=rev
Log:
[Sema] Warn when alignof is used with __builtin_alloca_with_align

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.

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

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=285609&r1=285608&r2=285609&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Mon Oct 31 13:07:57 2016
@@ -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<

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=285609&r1=285608&r2=285609&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Mon Oct 31 13:07:57 2016
@@ -3907,7 +3907,7 @@ bool Sema::SemaBuiltinAssume(CallExpr *T
   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(Ca
 
   // 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())

Modified: cfe/trunk/test/Sema/builtin-alloca-with-align.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/builtin-alloca-with-align.c?rev=285609&r1=285608&r2=285609&view=diff
==============================================================================
--- cfe/trunk/test/Sema/builtin-alloca-with-align.c (original)
+++ cfe/trunk/test/Sema/builtin-alloca-with-align.c Mon Oct 31 13:07:57 2016
@@ -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}}
+}




More information about the cfe-commits mailing list