C11 alignas与clang -Wcast-align

han*_*ach 21 c clang memory-alignment c11

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

随着警告全部上升,clang正确地抱怨结构的对齐要求未得到满足.所以我alignas在缓冲区中添加了一个C11 说明符,以确保缓冲区已经充分对齐,但是没有关闭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;
}
Run Code Online (Sandbox Code Playgroud)

编译器选项和错误消息:

$ 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;
                      ^~~~~~~~~~~~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)

编译器版本:

$ 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
Run Code Online (Sandbox Code Playgroud)

更新: 当我将缓冲区及其对齐方式移动到结构中时,没有警告.我将此解释为暗示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;
}
Run Code Online (Sandbox Code Playgroud)

ben*_*ank 2

从 clang 源代码来看,在 SemaChecking.cpp:~7862 中,他们似乎只查看您提到的类型:

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

  // else warn...
Run Code Online (Sandbox Code Playgroud)

在我看来,clang 正在准备 c 风格的演员阵容,而这反过来又会检查演员阵容的对齐情况。

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

void checkCastAlign() {
  Self.CheckCastAlign(SrcExpr.get(), DestType, OpRange);
}
Run Code Online (Sandbox Code Playgroud)

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

/// 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;
}
Run Code Online (Sandbox Code Playgroud)