Skip to content

C front-end: fix processing of alignment and packing attributes#8454

Open
tautschnig wants to merge 1 commit intodiffblue:developfrom
tautschnig:fix-8443-attributes
Open

C front-end: fix processing of alignment and packing attributes#8454
tautschnig wants to merge 1 commit intodiffblue:developfrom
tautschnig:fix-8443-attributes

Conversation

@tautschnig
Copy link
Copy Markdown
Collaborator

Packing needs to be evaluated as part of the definition of the type while alignment applies also when using a type. The position of the attribute also requires extra care as some positions are accepted by GCC (and warned about by Clang) while actually being ignored.

Fixes: #8443

  • Each commit message has a non-empty body, explaining why the change was made.
  • Methods or procedures I have added are documented, following the guidelines provided in CODING_STANDARD.md.
  • n/a The feature or user visible behaviour I have added or modified has been documented in the User Guide in doc/cprover-manual/
  • Regression or unit tests are included, or existing tests cover the modified code (in this case I have detailed which ones those are in the commit message).
  • n/a My commit message includes data points confirming performance improvements (if claimed).
  • My PR is restricted to a single feature or bugfix.
  • n/a White-space or formatting changes outside the feature-related changed lines are in commits of their own.

@codecov
Copy link
Copy Markdown

codecov Bot commented Sep 13, 2024

Codecov Report

❌ Patch coverage is 92.68293% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.02%. Comparing base (bacf91c) to head (200002c).
⚠️ Report is 54 commits behind head on develop.

Files with missing lines Patch % Lines
src/ansi-c/c_typecheck_type.cpp 91.30% 2 Missing ⚠️
src/ansi-c/parser.y 94.44% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #8454      +/-   ##
===========================================
+ Coverage    80.01%   80.02%   +0.01%     
===========================================
  Files         1700     1700              
  Lines       188344   188382      +38     
  Branches        73       78       +5     
===========================================
+ Hits        150701   150754      +53     
+ Misses       37643    37628      -15     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

power(2, align_int->floorPow2()) != *align_int)
{
throw errort().with_location(type.source_location())
<< "alignment is not a positive power of 2";
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that a requirement in the standard?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C11 6.2.8 "Alignment of objects" includes the following sentence: "Every valid alignment value shall be a nonnegative integral power of two."

@tautschnig tautschnig force-pushed the fix-8443-attributes branch from 5eea045 to 0367e2c Compare May 28, 2025 11:16
Copilot AI review requested due to automatic review settings March 9, 2026 16:14
@tautschnig tautschnig force-pushed the fix-8443-attributes branch from 0367e2c to 42011e8 Compare March 9, 2026 16:14
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes C front-end handling of GCC packed and aligned attributes so packing is applied during type definition while alignment also affects later uses, and ignores attribute placements that GCC accepts but ignores.

Changes:

  • Update grammar action for sue_declaration_specifier to drop ignored packed/aligned attributes before merging specifiers.
  • Add alignment validation (power-of-two) and introduce align_to to combine alignments across redefinitions / typedef uses.
  • Add a regression test covering many attribute placements and interactions.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 7 comments.

File Description
src/ansi-c/parser.y Filters out ignored packed/aligned attributes in a specific specifier context to match GCC behavior.
src/ansi-c/c_typecheck_type.cpp Validates alignment values and combines alignments when reusing/redefining types.
regression/ansi-c/gcc_attributes16/test.desc Adds expected-output configuration for the new regression test.
regression/ansi-c/gcc_attributes16/main.c Adds C source covering many packed/aligned placements and expected sizeof results.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/ansi-c/c_typecheck_type.cpp
Comment thread src/ansi-c/c_typecheck_type.cpp
Comment thread src/ansi-c/c_typecheck_type.cpp
Comment thread src/ansi-c/c_typecheck_type.cpp
Comment thread src/ansi-c/c_typecheck_type.cpp
Comment thread src/ansi-c/c_typecheck_type.cpp
Comment thread src/ansi-c/c_typecheck_type.cpp Outdated
Packing needs to be evaluated as part of the definition of the type
while alignment applies also when using a type. The position of the
attribute also requires extra care as some positions are accepted by GCC
(and warned about by Clang) while actually being ignored.

Fixes: diffblue#8443
Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
@tautschnig tautschnig force-pushed the fix-8443-attributes branch from 42011e8 to 200002c Compare March 9, 2026 21:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

__attribute__((packed)) incorrectly handled when used in member of a struct

3 participants