[clang] Refactor ASTContext::getDeclAlign() (NFC) (PR #72977)

Jonas Paulsson via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 21 03:31:33 PST 2023


https://github.com/JonPsson1 updated https://github.com/llvm/llvm-project/pull/72977

>From 90938183b35284cff65d100f3cb5284b816f28cc Mon Sep 17 00:00:00 2001
From: Jonas Paulsson <paulson1 at linux.ibm.com>
Date: Tue, 21 Nov 2023 12:10:03 +0100
Subject: [PATCH 1/2] Refactor ASTContext::getDeclAlign() (NFC)

---
 clang/lib/AST/ASTContext.cpp | 33 +++++++++++++--------------------
 1 file changed, 13 insertions(+), 20 deletions(-)

diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 1c893d008cb49f3..d08b9072c0e6298 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -1627,28 +1627,21 @@ 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;
   // If we're using the align attribute only, just ignore everything
   // else about the declaration and its type.
   if (UseAlignAttrOnly) {

>From 3a2b09bbdf533df9797f5d40ae1e027852400560 Mon Sep 17 00:00:00 2001
From: Jonas Paulsson <paulson1 at linux.ibm.com>
Date: Tue, 21 Nov 2023 12:31:16 +0100
Subject: [PATCH 2/2] Reformat

---
 clang/lib/AST/ASTContext.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index d08b9072c0e6298..50bb24631c118f4 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -1640,8 +1640,7 @@ CharUnits ASTContext::getDeclAlign(const Decl *D, bool ForAlignof) const {
   bool IsPackedField = isa<FieldDecl>(D) &&
                        (D->hasAttr<PackedAttr>() ||
                         cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>());
-  bool UseAlignAttrOnly =
-    isa<FieldDecl>(D) ? IsPackedField : AlignFromAttr;
+  bool UseAlignAttrOnly = isa<FieldDecl>(D) ? IsPackedField : AlignFromAttr;
   // If we're using the align attribute only, just ignore everything
   // else about the declaration and its type.
   if (UseAlignAttrOnly) {



More information about the cfe-commits mailing list