Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ASTContext::getDeclAlign() (NFC) #72977

Merged
merged 4 commits into from
Nov 30, 2023

Conversation

JonPsson1
Copy link
Contributor

@efriedma-quic
@rjmccall

While working on this (with the other patch: 72886), I found this refactoring at the top of the function which I like. What do you think?

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Nov 21, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Nov 21, 2023

@llvm/pr-subscribers-clang

Author: Jonas Paulsson (JonPsson1)

Changes

@efriedma-quic
@rjmccall

While working on this (with the other patch: 72886), I found this refactoring at the top of the function which I like. What do you think?


Full diff: https://github.com/llvm/llvm-project/pull/72977.diff

1 Files Affected:

  • (modified) clang/lib/AST/ASTContext.cpp (+13-20)
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) {

Copy link

github-actions bot commented Nov 21, 2023

✅ With the latest revision this PR passed the C/C++ code formatter.

bool IsPackedField = isa<FieldDecl>(D) &&
(D->hasAttr<PackedAttr>() ||
cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>());
bool UseAlignAttrOnly = isa<FieldDecl>(D) ? IsPackedField : AlignFromAttr;
Copy link
Collaborator

Choose a reason for hiding this comment

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

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;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That works for me as well: main problem to me with the original code was the duplicated handling of a FieldDecl.

I took your suggestion, but removed the braces, which seem superfluous? Or are they required by the coding standard?

@JonPsson1
Copy link
Contributor Author

Eli, are you supposed to press some button before I merge this?

Copy link
Collaborator

@efriedma-quic efriedma-quic left a comment

Choose a reason for hiding this comment

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

LGTM

See https://llvm.org/docs/CodingStandards.html#don-t-use-braces-on-simple-single-statement-bodies-of-if-else-loop-statements for the rules on use/don't use braces. A single multi-line statement is sort of on the edge; I tend to prefer braces in that case, but you can go either way.

@JonPsson1 JonPsson1 merged commit b04a419 into llvm:main Nov 30, 2023
3 checks passed
@JonPsson1 JonPsson1 deleted the Refactor_getDeclAlign branch November 30, 2023 10:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants