C11 alignas 与 clang -Wcast-align

C11 alignas vs. clang -Wcast-align

所以我有以下最小化的 C11 代码,它定义了一个包含 uint16_t 的结构(这意味着它应该对齐到 2 个字节的结构)并且我想将一个 char 缓冲区转换为指向它的指针结构。

随着警告全部出现,clang 正确地抱怨不满足结构的对齐要求。所以我向缓冲区添加了一个 C11 alignas 说明符以确保缓冲区充分对齐,但这并没有关闭 clang。

我的问题是:我在 alignas 上做错了什么吗?或者只是 -Wcast-align 诊断只查看参数的类型而不是手动指定的对齐方式? (我知道我可以强制转换为 void* 来停止诊断,但是由于这段代码应该是可移植的,所以我不想回避诊断,除非我确定它是误报.)

#include <stdint.h>
#include <stdalign.h>

struct foo {
    uint16_t field1;
};


int main(void) {
    alignas(struct foo) char buffer[122] = {0};
    struct foo *foo = (struct foo*)buffer;
    return foo->field1;
}

编译器选项和错误信息:

$ clang -ggdb -O3 foo.c -Weverything -Werror -Wno-c++98-compat -Wno-c11-extensions
foo.c:11:23: error: cast from 'char *' to 'struct foo *' increases required alignment from 1 to 2 [-Werror,-Wcast-align]
    struct foo *foo = (struct foo*)buffer;
                      ^~~~~~~~~~~~~~~~~~~~~~~~~

编译器版本:

$ clang -v
clang version 3.5.1 (tags/RELEASE_351/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
Selected GCC installation: /usr/lib/gcc/x86_64-pc-linux-gnu/4.8.4

更新: 当我将缓冲区及其对齐方式移动到结构中时,没有任何警告。我将此解释为 clang 确实只查看此警告的类型的提示。

#include <stdint.h>
#include <stdalign.h>

struct foo {
    uint16_t field1;
};

struct bar {
    alignas(struct foo) char buffer[122];
};


int main(void) {
    struct bar bar = {{0}};
    struct foo *foo = (struct foo*)&bar;
    return foo->field1;
}

来自 clang 来源,在 SemaChecking.cpp:~7862 中,他们似乎只关注你提到的类型:

  CharUnits SrcAlign = Context.getTypeAlignInChars(SrcPointee);
  if (SrcAlign >= DestAlign) return;

  // else warn...

在我看来,clang 正在准备一个 c 风格的转换,而后者又会检查转换对齐。

void CastOperation::CheckCStyleCast()
    -> Kind = CastKind Sema::PrepareScalarCast(...);
        -> if (Kind == CK_BitCast)
               checkCastAlign();

void checkCastAlign() {
  Self.CheckCastAlign(SrcExpr.get(), DestType, OpRange);
}

这里是有更多上下文的方法:

/// CheckCastAlign - Implements -Wcast-align, which warns when a
/// pointer cast increases the alignment requirements.
void Sema::CheckCastAlign(Expr *Op, QualType T, SourceRange TRange) {
  // This is actually a lot of work to potentially be doing on every
  // cast; don't do it if we're ignoring -Wcast_align (as is the default).
  if (getDiagnostics().isIgnored(diag::warn_cast_align, TRange.getBegin()))
    return;

  // Ignore dependent types.
  if (T->isDependentType() || Op->getType()->isDependentType())
    return;

  // Require that the destination be a pointer type.
  const PointerType *DestPtr = T->getAs<PointerType>();
  if (!DestPtr) return;

  // If the destination has alignment 1, we're done.
  QualType DestPointee = DestPtr->getPointeeType();
  if (DestPointee->isIncompleteType()) return;
  CharUnits DestAlign = Context.getTypeAlignInChars(DestPointee);
  if (DestAlign.isOne()) return;

  // Require that the source be a pointer type.
  const PointerType *SrcPtr = Op->getType()->getAs<PointerType>();
  if (!SrcPtr) return;
  QualType SrcPointee = SrcPtr->getPointeeType();

  // Whitelist casts from cv void*.  We already implicitly
  // whitelisted casts to cv void*, since they have alignment 1.
  // Also whitelist casts involving incomplete types, which implicitly
  // includes 'void'.
  if (SrcPointee->isIncompleteType()) return;

  CharUnits SrcAlign = Context.getTypeAlignInChars(SrcPointee);
  if (SrcAlign >= DestAlign) return;

  Diag(TRange.getBegin(), diag::warn_cast_align)
    << Op->getType() << T
    << static_cast<unsigned>(SrcAlign.getQuantity())
    << static_cast<unsigned>(DestAlign.getQuantity())
    << TRange << Op->getSourceRange();
}

static const Type* getElementType(const Expr *BaseExpr) {
  const Type* EltType = BaseExpr->getType().getTypePtr();
  if (EltType->isAnyPointerType())
    return EltType->getPointeeType().getTypePtr();
  else if (EltType->isArrayType())
    return EltType->getBaseElementTypeUnsafe();
  return EltType;
}