481 Commits

Author SHA1 Message Date
Theo de Magalhaes
41892fc435
[CLANG-CL] ignores Wpadded (#134426)
[clang] add support for -Wpadded on Windows

Implements the -Wpadded warning for --target=x86_64-windows-msvc etc.

Fixes #61702 .
2025-04-14 14:17:49 -07:00
Alex Bradbury
2a9948f038
Revert "[CLANG-CL] ignores wpadded" (#134239)
Reverts llvm/llvm-project#130182

This is causing failures on RISC-V and ppc builders as mentioned on
https://github.com/llvm/llvm-project/pull/130182#issuecomment-2775516899

Reverting so the issue can be fixed by the path author without time pressure (as noted in that PR, it seems a value is uninitialised).
2025-04-03 13:47:59 +01:00
Theo de Magalhaes
76fa9530c9
[clang] add support for -Wpadded on Windows (#130182)
Implements the -Wpadded warning for --target=x86_64-windows-msvc etc.

Fixes #61702 .
2025-04-02 14:46:58 -07:00
Kazu Hirata
c6c394634c
[clang] Use *Set::insert_range (NFC) (#132507)
DenseSet, SmallPtrSet, SmallSet, SetVector, and StringSet recently
gained C++23-style insert_range.  This patch replaces:

  Dest.insert(Src.begin(), Src.end());

with:

  Dest.insert_range(Src);

This patch does not touch custom begin like succ_begin for now.
2025-03-22 08:06:38 -07:00
Oliver Stannard
02e8fd7a30
[ARM,AArch64] Fix ABI bugs with over-sized bitfields (#126774)
This fixes two bugs in the ABI for over-sized bitfields for ARM and
AArch64:

The container type picked for an over-sized bitfield already contributes
to the alignment of the structure, but it should also contribute to the
"unadjusted alignment" which is used by the ARM and AArch64 PCS.

AAPCS64 defines the bitfield layout algorithm for over-sized bitfields
as picking a container which is the fundamental integer data type with
the largest size less than or equal to the bit-field width. Since
AAPCS64 has a 128-bit integer fundamental data type, we need to consider
Int128 as a container type for AArch64.
2025-02-20 17:07:16 +00:00
Akira Hatanaka
f5c5bc5ed5
[CodeGen][ObjC] Invalidate cached ObjC class layout information after parsing ObjC class implementations if new ivars are added to the interface (#126591)
The layout and the size of an ObjC interface can change after its
corresponding implementation is parsed when synthesized ivars or ivars
declared in categories are added to the interface's list of ivars. This
can cause clang to mis-compile if the optimization that emits fixed
offsets for ivars (see 923ddf65f4e21ec67018cf56e823895de18d83bc) uses an
ObjC class layout that is outdated and no longer reflects the current
state of the class.

For example, when compiling `constant-non-fragile-ivar-offset.m`, clang
emits 20 instead of 24 as the offset for `IntermediateClass2Property` as
the class layout for `SuperClass2`, which is created when the
implementation of IntermediateClass3 is parsed, is outdated when the
implementation of `IntermediateClass2` is parsed.

This commit invalidates the stale layout information of the class and
its subclasses if new ivars are added to the interface.

With this change, we can also stop using ObjC implementation decls as
the key to retrieve ObjC class layouts information as the layout
retrieved using the ObjC interface as the key will always be up to date.

rdar://139531391
2025-02-17 11:50:44 -08:00
Timm Baeder
cfe26358e3
Reapply "[clang] Avoid re-evaluating field bitwidth" (#122289) 2025-01-11 07:12:37 +01:00
Reid Kleckner
26aa20a3dd
Use range-based for to iterate over fields in record layout, NFCI (#122029)
Move the common case of FieldDecl::getFieldIndex() inline to mitigate
the cost of removing the extra `FieldNo` induction variable.

Also rename isNoUniqueAddress parameter to isNonVirtualBaseType, which
appears to be more accurate. I think the current name is just a
consequence of autocomplete gone wrong.
2025-01-09 11:21:03 -08:00
Timm Bäder
59bdea24b0 Revert "[clang] Avoid re-evaluating field bitwidth (#117732)"
This reverts commit 81fc3add1e627c23b7270fe2739cdacc09063e54.

This breaks some LLDB tests, e.g.
SymbolFile/DWARF/x86/no_unique_address-with-bitfields.cpp:

lldb: ../llvm-project/clang/lib/AST/Decl.cpp:4604: unsigned int clang::FieldDecl::getBitWidthValue() const: Assertion `isa<ConstantExpr>(getBitWidth())' failed.
2025-01-08 15:09:52 +01:00
Timm Baeder
81fc3add1e
[clang] Avoid re-evaluating field bitwidth (#117732)
Save the bitwidth value as a `ConstantExpr` with the value set. Remove
the `ASTContext` parameter from `getBitWidthValue()`, so the latter
simply returns the value from the `ConstantExpr` instead of
constant-evaluating the bitwidth expression every time it is called.
2025-01-08 14:45:19 +01:00
Eric Wang
4cea3c3c27
[Clang] Fix typo in ItaniumRecordLayoutBuilder API (#118563) 2024-12-10 07:40:55 +01:00
Kazu Hirata
dec6324cb0
[AST] Remove unused includes (NFC) (#116549)
Identified with misc-include-cleaner.
2024-11-17 09:36:48 -08:00
Yaxun (Sam) Liu
748ef7eccc
[CUDA][HIP] Fix record layout on Windows (#87651)
On windows, record layout should be consistent with host side, otherwise
host code is not able to access fields of the record correctly.

Fixes: https://github.com/llvm/llvm-project/issues/51031

Fixes: SWDEV-446010
2024-04-17 21:44:12 -04:00
Vlad Serebrennikov
63b414e497 [clang][NFC] Annotate RecordLayoutBuilder.cpp with preferred_type
This helps debuggers to display values in bit-fields in a more helpful way.
2024-02-11 15:01:18 +03:00
cor3ntin
e90e43fb9c
[Clang][NFC] Rename CXXMethodDecl::isPure -> is VirtualPure (#78463)
To avoid any possible confusion with the notion of pure function and the
gnu::pure attribute.
2024-01-18 15:30:58 +01:00
Amy Huang
3745f47587
Fix to msvc::no_unique_address causing assert when used with __declspec(empty_bases) (#74776)
no_unique_address makes it possible for a class to be empty and have
non-zero virtual size, so just remove this assert.

Bug: https://github.com/llvm/llvm-project/issues/74442
2023-12-11 10:38:46 -08:00
Vlad Serebrennikov
edd690b02e
[clang][NFC] Refactor TagTypeKind (#71160)
This patch converts TagTypeKind into scoped enum. Among other benefits,
this allows us to forward-declare it where necessary.
2023-11-03 21:45:39 +04:00
Dan Klishch
43feb3eeb1
[clang] Separate bit-field padding diagnostics into -Wpadded-bitfield (#70978)
The `-Wpadded` diagnostics are usually very noisy and generally not
helpful. However, reporting padding that was introduced in bit-fields is
rather helpful. For example, yesterday in SerenityOS's discord we had
very unpleasant experience of debugging Windows portability issue, and
its root cause was that under `x86_64-pc-windows-msvc` a padding was
introduced for one of the bit-fields.

So, this PR separates bit-field-related padding diagnostics into a new
`-Wpadded-bitfield`. The diagnostic group is also enabled by `-Wpadded`
for compatibility reasons.
2023-11-02 11:52:57 -04:00
Amy Huang
0faee97a92
Recommit "Implement [[msvc::no_unique_address]] (#65675)" (#67199)
This implements the [[msvc::no_unique_address]] attribute.

There is not ABI compatibility in this patch because the attribute is
relatively new and there's still some uncertainty in the MSVC version.

The recommit changes the attribute definitions so that instead of making
two separate attributes for no_unique_address
and msvc::no_unique_address, it modifies the attributes tablegen emitter
to allow spellings to be target-specific.

This reverts commit 71f9e7695b87298f9855d8890f0e6a3b89381eb5.
2023-09-28 14:29:32 -07:00
Amy Huang
71f9e7695b
Revert "Implement [[msvc::no_unique_address]] (#65675)" (#67198)
This reverts commit 4a55d426967b9c70f5dea7b3a389e11393a4f4c4.

Reverting because this breaks sphinx documentation, and even with it
fixed the format of the attribute makes the no_unique_address
documentation show up twice.
2023-09-22 14:39:00 -07:00
Amy Huang
4a55d42696
Implement [[msvc::no_unique_address]] (#65675)
This implements the [[msvc::no_unique_address]] attribute.

There is not ABI compatibility in this patch because the attribute is
relatively new and there's still some uncertainty in the MSVC version.

Bug: https://github.com/llvm/llvm-project/issues/49358

Also see https://reviews.llvm.org/D157762.
2023-09-22 13:28:38 -07:00
Kazu Hirata
7a6806073d [AST] Modernize ExternalLayout (NFC) 2023-08-20 09:42:57 -07:00
Zequan Wu
879e886933 Reland "[Clang][MS] Remove assertion on BaseOffset can't be smaller than Size."
This reland 5d54213ee557a86fae82af0f75498adf02f24e82 with fixes.
2023-06-15 11:33:02 -04:00
Nico Weber
9c560350dd Revert "[Clang][MS] Remove assertion on BaseOffset can't be smaller than Size."
This reverts commit 5d54213ee557a86fae82af0f75498adf02f24e82.

Breaks check-clang on Windows, see https://reviews.llvm.org/D152472#4422913
2023-06-14 16:19:48 -07:00
Zequan Wu
5d54213ee5 [Clang][MS] Remove assertion on BaseOffset can't be smaller than Size.
This assertion triggered when we have two base classes sharing the same offset
and the first base is empty and the second class is non-empty.
Remove it for correctness.

I can't add a test case for this because -foverride-record-layout doesn't read
base class info at all. I can add that support later for testing if needed.

Reviewed By: rnk

Differential Revision: https://reviews.llvm.org/D152472
2023-06-14 16:48:26 -04:00
David Blaikie
a8b0c6fa28 Remove -Wpacked false positive for non-pod types where the layout isn't directly changed
The packed attribute can still be useful in this case if the struct is
then placed inside another packed struct - the non-pod element type's
packed attribute declares that it's OK to misalign this element inside
the packed structure. (otherwise the non-pod element is not packed/its
alignment is preserved, as per D117616/2771233)

Fixes PR62353

Differential Revision: https://reviews.llvm.org/D149182
2023-05-09 00:13:45 +00:00
Vladislav Dzhidzhoev
af6c0b6d8c [clang][CodeGen] Use base subobject type layout for potentially-overlapping fields
RecordLayoutBuilder assumes the size of a potentially-overlapping
class/struct field with non-zero size as the size of the base subobject
type corresponding to the field type.
Make CGRecordLayoutBuilder to acknowledge that in order to avoid incorrect
padding insertion.

Differential Revision: https://reviews.llvm.org/D139741
2023-02-17 15:11:42 +01:00
Nicole Rabjohn
0f5099cd94 Opting out of Clang 16 ABI Changes for AIX and z/OS
There is already a GCC compatibility gap on AIX, & GCC compatibility is
not a concern on z/OS. GCC compatibility is not sufficient motivation
for breaking ABI on AIX and z/OS. This opts out of changes introduced in
https://reviews.llvm.org/D119051. For AIX only, also opt out of D117616
(which z/OS had picked up at the time ABI stabilization occurred).

Differential Revision: https://reviews.llvm.org/D142358
2023-01-24 01:14:05 -05:00
Volodymyr Sapsai
f33b5b1bf7 [ODRHash] Detect mismatches in anonymous RecordDecl.
Allow completing a redeclaration check for anonymous structs/unions
inside `RecordDecl`, so we deserialize and compare anonymous entities
from different modules.

Completing the redeclaration chain for `RecordDecl` in
`ASTContext::getASTRecordLayout` mimics the behavior in
`CXXRecordDecl::dataPtr`. Instead of completing the redeclaration chain
every time we request a definition, do that right before we need a
complete definition in `ASTContext::getASTRecordLayout`.

Such code is required only for anonymous `RecordDecl` because we
deserialize named decls when we look them up by name. But it doesn't
work for anonymous decls as they don't have a name. That's why need to
force deserialization of anonymous decls in a different way.

rdar://81864186

Differential Revision: https://reviews.llvm.org/D140055
2023-01-19 15:58:31 -06:00
Vladislav Dzhidzhoev
19d300fb41 Revert "[clang][CodeGen] Use base subobject type layout for potentially-overlapping fields"
This reverts commit 731abdfdcc33d813e6c3b4b89eff307aa5c18083.

This commit breaks some tests in libcxx, e.g.
`std/utilities/expected/expected.expected/ctor/ctor.inplace.pass.cpp`
2022-12-15 18:12:26 +03:00
Vladislav Dzhidzhoev
731abdfdcc [clang][CodeGen] Use base subobject type layout for potentially-overlapping fields
RecordLayoutBuilder assumes the size of a potentially-overlapping field
with non-zero size as the size of the base subobject type corresponding
to the field type.
Make CGRecordLayoutBuilder to acknowledge that in order to avoid incorrect
padding insertion.

Differential Revision: https://reviews.llvm.org/D139741
2022-12-15 15:10:41 +03:00
Alex Richardson
a602f76a24 [clang][TargetInfo] Use LangAS for getPointer{Width,Align}()
Mixing LLVM and Clang address spaces can result in subtle bugs, and there
is no need for this hook to use the LLVM IR level address spaces.
Most of this change is just replacing zero with LangAS::Default,
but it also allows us to remove a few calls to getTargetAddressSpace().

This also removes a stale comment+workaround in
CGDebugInfo::CreatePointerLikeType(): ASTContext::getTypeSize() does
return the expected size for ReferenceType (and handles address spaces).

Differential Revision: https://reviews.llvm.org/D138295
2022-11-30 20:24:01 +00:00
Zequan Wu
29c9287917 [AST] Fix class layout when using external layout under MS ABI.
Reviewed By: rnk

Differential Revision: https://reviews.llvm.org/D137806
2022-11-16 11:17:50 -08:00
David Blaikie
ec273d3e3a Add a warning for not packing non-POD members in packed structs
Differential Revision: https://reviews.llvm.org/D118511
2022-10-26 22:18:51 +00:00
David Blaikie
037f856681 Itanium ABI: Pack non-pod members of packed types
Seems there's a narrow case - where a packed type doesn't pack its base
subobjects (only fields), but when packing a field of the derived type,
GCC does pack the resulting total object - effectively packing the base
subobject.

So ensure that this non-pod type (owing to it having a base class) that
is packed, gets packed when placed in /another/ type that is also
packed.

This is a (smallish?) ABI fix to a regression introduced by D117616 -
but that regression/ABI break hasn't been released in LLVM as-yet (it's
been reverted on the release branch from the last two LLVM releases - I
probably should've just reverted the whole patch while we hashed out
this and other issues) so this change isn't itself an ABI break, as far
as LLVM releases are concerned (for folks releasing their own copies of
LLVM from ToT/without the LLVM release branch, and didn't opt into the
clang-abi-compat 14 or below (soon to be 15 or below, I guess I should
say) then this would be an ABI break against clang from the last 9
months or so)

Differential Revision: https://reviews.llvm.org/D135916
2022-10-14 19:32:57 +00:00
David Blaikie
9363071303 Move GCC-compatible pod-packing change to v16/old behavior available at v15 and below
Change matches D126334/e59f648d698e since this change got punted from
v15 too.
2022-10-13 21:13:19 +00:00
Paul Robinson
0fe88f9679 [PS4/PS5] Don't inherit base class alignment 2022-06-10 13:15:17 -07:00
Paul Robinson
30b7ffe74e [PS5] Pack non-POD members in packed structs, matching PS4 ABI 2022-06-02 12:26:26 -07:00
Paul Robinson
bb7835e2a7 [PS5] Apply 'packed' attribute to base classes, matching PS4 ABI 2022-06-02 12:26:26 -07:00
David Blaikie
e59f648d69 Move GCC-compatible pod-packing change to v15/old behavior available at v14 and below
Since this didn't make it into the v14 release - anyone requesting the
v14 ABI shouldn't get this GCC-compatible change that isn't backwards
compatible with v14 Clang.

Differential Revision: https://reviews.llvm.org/D126334
2022-05-25 03:03:27 +00:00
Nathan James
cfb8169059
[clang] Add a raw_ostream operator<< overload for QualType
Under the hood this prints the same as `QualType::getAsString()` but cuts out the middle-man when that string is sent to another raw_ostream.

Also cleaned up all the call sites where this occurs.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D123926
2022-04-20 22:09:05 +01:00
David Blaikie
277123376c GCC ABI Compatibility: Preserve alignment of non-pod members in packed structs
This matches GCC: https://godbolt.org/z/sM5q95PGY

I realize this is an API break for clang+clang - so I'm totally open to
discussing how we should deal with that. If Apple wants to keep the
Clang layout indefinitely, if we want to put a flag on this so non-Apple
folks can opt out of this fix/new behavior.

Differential Revision: https://reviews.llvm.org/D117616
2022-01-28 11:04:20 -08:00
Mubashar Ahmad
35737df4dc [Clang][AArch64][ARM] Unaligned Access Warning Added
Added warning for potential cases of
unaligned access when option
-mno-unaligned-access has been specified

Differential Revision: https://reviews.llvm.org/D116221
2022-01-20 14:12:49 +00:00
Archibald Elliott
3aec4b3d34 Revert "Unaligned Access Warning Added"
This reverts commits:
- 2cd2600abaf3c604889b77ab408fdf89d7a21c48
- 11c67e5a4e99f51ec66c9781710f81955cfd5e24

Due to test failures on Windows.
2022-01-07 13:07:30 +00:00
Mubashar Ahmad
2cd2600aba Unaligned Access Warning Added
Added warning for potential cases of
unaligned access when option
-mno-unaligned-access has been specified
2022-01-07 09:54:20 +00:00
Kazu Hirata
4db2e4cebe Use {DenseSet,SetVector,SmallPtrSet}::contains (NFC) 2021-10-30 19:00:19 -07:00
Kazu Hirata
57b40b5f34 [AST, CodeGen, Driver] Use llvm::is_contained (NFC) 2021-10-12 09:19:49 -07:00
Volodymyr Sapsai
9fad9de5c0 [modules] Fix IRGen assertion on accessing ObjC ivar inside a method.
When have ObjCInterfaceDecl with the same name in 2 different modules,
hitting the assertion

> Assertion failed: (Index < RL->getFieldCount() && "Ivar is not inside record layout!"),
> function lookupFieldBitOffset, file llvm-project/clang/lib/AST/RecordLayoutBuilder.cpp, line 3434.

on accessing an ivar inside a method. The assertion happens because
ivar belongs to one module while its containing interface belongs to
another module and then we fail to find the ivar inside the containing
interface. We already keep a single ObjCInterfaceDecl definition in
redecleration chain and in this case containing interface was correct.
The issue is with ObjCIvarDecl. IVar decl for IRGen is taken from
ObjCIvarRefExpr that is created in `Sema::BuildIvarRefExpr` using ivar
decl returned from `Sema::LookupIvarInObjCMethod`. And ivar lookup
returns a wrong decl because basically we take the first ObjCIvarDecl
found in `ASTReader::FindExternalVisibleDeclsByName` (called by
`DeclContext::lookup`). And in `ASTReader.Lookups` lookup table for a
wrong module comes first because `ASTReader::finishPendingActions`
processes `PendingUpdateRecords` in reverse order and the first
encountered ObjCIvarDecl will end up the last in `ASTReader.Lookups`.

Fix by merging ObjCIvarDecl from different modules correctly and by
using a canonical one in IRGen.

rdar://82854574

Differential Revision: https://reviews.llvm.org/D110280
2021-10-07 17:09:31 -07:00
Nico Weber
60ab6861ed [clang] Fix a few more comment typos to cycle bots 2021-09-20 20:06:03 -04:00
Steven Wan
71b170ccf3 [AIX] "aligned" attribute does not decrease alignment
The "aligned" attribute can only increase the alignment of a struct, or struct member, unless it's used together with the "packed" attribute, or used as a part of a typedef, in which case, the "aligned" attribute can both increase and decrease alignment.

That said, we expect:
1. "aligned" attribute alone: does not interfere with the alignment upgrade instrumented by the AIX "power" alignment rule,
2. "aligned" attribute + typedef: overrides any computed alignment,
3. "aligned" attribute + "packed" attribute: overrides any computed alignment.
The old implementation achieved 2 and 3, but didn't get 1 right, in that any field marked attribute "aligned" would not go through the alignment upgrade.

Reviewed By: rjmccall

Differential Revision: https://reviews.llvm.org/D107394
2021-08-29 21:33:05 -04:00