[clang] Refactor ASTContext::getDeclAlign() (NFC) (PR #72977)
Eli Friedman via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 21 14:18:14 PST 2023
================
@@ -1627,28 +1627,20 @@ const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
CharUnits ASTContext::getDeclAlign(const Decl *D, bool ForAlignof) const {
unsigned Align = Target->getCharWidth();
- bool UseAlignAttrOnly = false;
- if (unsigned AlignFromAttr = D->getMaxAlignment()) {
+ const unsigned AlignFromAttr = D->getMaxAlignment();
+ if (AlignFromAttr)
Align = AlignFromAttr;
- // __attribute__((aligned)) can increase or decrease alignment
- // *except* on a struct or struct member, where it only increases
- // alignment unless 'packed' is also specified.
- //
- // It is an error for alignas to decrease alignment, so we can
- // ignore that possibility; Sema should diagnose it.
- if (isa<FieldDecl>(D)) {
- UseAlignAttrOnly = D->hasAttr<PackedAttr>() ||
- cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>();
- } else {
- UseAlignAttrOnly = true;
- }
- }
- else if (isa<FieldDecl>(D))
- UseAlignAttrOnly =
- D->hasAttr<PackedAttr>() ||
- cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>();
-
+ // __attribute__((aligned)) can increase or decrease alignment
+ // *except* on a struct or struct member, where it only increases
+ // alignment unless 'packed' is also specified.
+ //
+ // It is an error for alignas to decrease alignment, so we can
+ // ignore that possibility; Sema should diagnose it.
+ bool IsPackedField = isa<FieldDecl>(D) &&
+ (D->hasAttr<PackedAttr>() ||
+ cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>());
+ bool UseAlignAttrOnly = isa<FieldDecl>(D) ? IsPackedField : AlignFromAttr;
----------------
efriedma-quic wrote:
That's a lot of `isa<>` and `cast<>`. I think I'd prefer something more like:
```
bool UseAlignAttrOnly;
if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
UseAlignAttrOnly = FD->hasAttr<PackedAttr>() ||
FD->getParent()->hasAttr<PackedAttr>();
} else {
UseAlignAttrOnly = AlignFromAttr != 0;
}
```
https://github.com/llvm/llvm-project/pull/72977
More information about the cfe-commits
mailing list