Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions clang/lib/StaticAnalyzer/Checkers/PointerAlignmentChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,20 @@ int getTrailingZerosCount(const MemRegion *R, ProgramStateRef State,
return -1;
unsigned NaturalAlign = ASTCtx.getTypeAlignInChars(PT).getQuantity();

if (const FieldRegion *FR = R->getAs<FieldRegion>()) {
// If this is the first field of a larger struct, we can use the alignment
// of the containing struct try to increase the assumed alignment.
const RegionOffset &Offset = FR->getAsOffset();
if (!Offset.hasSymbolicOffset() && Offset.getOffset() == 0) {
if (const auto *Base =
FR->getSuperRegion()->getAs<TypedValueRegion>()) {
auto BaseTy = Base->getValueType();
unsigned BaseAlign = ASTCtx.getTypeAlignInChars(BaseTy).getQuantity();
NaturalAlign = std::max(NaturalAlign, BaseAlign);
}
}
}

if (const ElementRegion *ER = R->getAs<ElementRegion>()) {
int ElTyTZ = APSInt::getUnsigned(NaturalAlign).countTrailingZeros();

Expand Down
12 changes: 12 additions & 0 deletions clang/test/Analysis/pointer-alignment.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,15 @@ void cast_and_assign(void) {
// no duplicate assign warn
*i = 42;
}

// ----
struct __attribute__((aligned(8))) AlignedParentStruct {
int a;
int b;
};

void write_to_first_member(struct AlignedParentStruct *chunk) {
// No warning because alignment of is inferred from the parent struct.
*(long long*)(&chunk->a) = 0;
}