From: Daniel Dunbar Date: Mon, 16 Feb 2009 23:37:57 +0000 (+0000) Subject: Diagnose non-power-of-2 arguments to attribute aligned. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=396b2a22788b0134018760d6a476de1e20f81334;p=clang Diagnose non-power-of-2 arguments to attribute aligned. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@64700 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.def b/include/clang/Basic/DiagnosticSemaKinds.def index af4c7f5aa4..8514b92214 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.def +++ b/include/clang/Basic/DiagnosticSemaKinds.def @@ -356,6 +356,8 @@ DIAG(err_as_qualified_auto_decl, ERROR, "automatic variable qualified with an address space") DIAG(err_attribute_annotate_no_string, ERROR, "argument to annotate attribute was not a string literal") +DIAG(err_attribute_aligned_not_power_of_two, ERROR, + "requested alignment is not a power of 2") DIAG(warn_redeclaration_without_attribute_prev_attribute_ignored, WARNING, "'%0' redeclared without %1 attribute: previous %1 ignored") DIAG(warn_attribute_ignored, WARNING, diff --git a/lib/Sema/SemaDeclAttr.cpp b/lib/Sema/SemaDeclAttr.cpp index a28d0a6d6d..1d83605316 100644 --- a/lib/Sema/SemaDeclAttr.cpp +++ b/lib/Sema/SemaDeclAttr.cpp @@ -1218,6 +1218,12 @@ static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) { << "aligned" << alignmentExpr->getSourceRange(); return; } + if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) { + S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two) + << alignmentExpr->getSourceRange(); + return; + } + d->addAttr(new AlignedAttr(Alignment.getZExtValue() * 8)); } diff --git a/test/Sema/attr-aligned.c b/test/Sema/attr-aligned.c new file mode 100644 index 0000000000..390e6d9df4 --- /dev/null +++ b/test/Sema/attr-aligned.c @@ -0,0 +1,3 @@ +// RUN: clang -fsyntax-only -verify %s + +int x __attribute__((aligned(3))); // expected-error {{requested alignment is not a power of 2}}