Skip to content

Fixing the logic issue in TransformTypos::TransformDesignatedInitExpr… #127211

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

Merged
merged 1 commit into from
Feb 16, 2025

Conversation

GS-GOAT
Copy link
Contributor

@GS-GOAT GS-GOAT commented Feb 14, 2025

#126113

-Transforming Indices: For array designators, transform the index expression and update ExprChanged if it's modified.
-Correct Initializer Check: Compare the transformed initializer against the original to accurately track changes.
-Single Initializer Transformation: The initializer is processed once, not per designator, as each DesignatedInitExpr has one initializer.

Fixes #126113

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Feb 14, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 14, 2025

@llvm/pr-subscribers-clang

Author: None (GS-GOAT)

Changes

… #126113

-Transforming Indices: For array designators, transform the index expression and update ExprChanged if it's modified.
-Correct Initializer Check: Compare the transformed initializer against the original to accurately track changes.
-Single Initializer Transformation: The initializer is processed once, not per designator, as each DesignatedInitExpr has one initializer.

Fixes #126113


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

1 Files Affected:

  • (modified) clang/lib/Sema/TreeTransform.h (+13-11)
diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index fc1e3f7d58f4d..81388b2783fad 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -13634,12 +13634,12 @@ ExprResult
 TreeTransform<Derived>::TransformDesignatedInitExpr(DesignatedInitExpr *E) {
   Designation Desig;
 
-  // transform the initializer value
+  // Transform the initializer value once.
   ExprResult Init = getDerived().TransformExpr(E->getInit());
   if (Init.isInvalid())
     return ExprError();
 
-  // transform the designators.
+  // Transform the designators.
   SmallVector<Expr*, 4> ArrayExprs;
   bool ExprChanged = false;
   for (const DesignatedInitExpr::Designator &D : E->designators()) {
@@ -13649,7 +13649,7 @@ TreeTransform<Derived>::TransformDesignatedInitExpr(DesignatedInitExpr *E) {
             getDerived().TransformDecl(D.getFieldLoc(), D.getFieldDecl()));
         if (Field != D.getFieldDecl())
           // Rebuild the expression when the transformed FieldDecl is
-          // different to the already assigned FieldDecl.
+          // different from the already assigned FieldDecl.
           ExprChanged = true;
         if (Field->isAnonymousStructOrUnion())
           continue;
@@ -13665,21 +13665,22 @@ TreeTransform<Derived>::TransformDesignatedInitExpr(DesignatedInitExpr *E) {
     }
 
     if (D.isArrayDesignator()) {
-      ExprResult Index = getDerived().TransformExpr(E->getArrayIndex(D));
-      if (Index.isInvalid())
+      // Transform the array index expression.
+      ExprResult NewIndex = getDerived().TransformExpr(E->getArrayIndex(D));
+      if (NewIndex.isInvalid())
         return ExprError();
 
-      Desig.AddDesignator(
-          Designator::CreateArrayDesignator(Index.get(), D.getLBracketLoc()));
+      Desig.AddDesignator(Designator::CreateArrayDesignator(
+          NewIndex.get(), D.getLBracketLoc()));
 
-      ExprChanged = ExprChanged || Init.get() != E->getArrayIndex(D);
-      ArrayExprs.push_back(Index.get());
+      // Check if the transformed index is different from the original.
+      ExprChanged = ExprChanged || NewIndex.get() != E->getArrayIndex(D);
+      ArrayExprs.push_back(NewIndex.get());
       continue;
     }
 
     assert(D.isArrayRangeDesignator() && "New kind of designator?");
-    ExprResult Start
-      = getDerived().TransformExpr(E->getArrayRangeStart(D));
+    ExprResult Start = getDerived().TransformExpr(E->getArrayRangeStart(D));
     if (Start.isInvalid())
       return ExprError();
 
@@ -13697,6 +13698,7 @@ TreeTransform<Derived>::TransformDesignatedInitExpr(DesignatedInitExpr *E) {
     ArrayExprs.push_back(End.get());
   }
 
+  // Compare the transformed initializer against the original.
   if (!getDerived().AlwaysRebuild() &&
       Init.get() == E->getInit() &&
       !ExprChanged)

Copy link
Contributor

@zyn0217 zyn0217 left a comment

Choose a reason for hiding this comment

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

Thanks. Though I think we should just fix the problematic condition expression, and other changes seem a bit unnecessary since they are already clear enough.

@@ -13697,6 +13698,7 @@ TreeTransform<Derived>::TransformDesignatedInitExpr(DesignatedInitExpr *E) {
ArrayExprs.push_back(End.get());
}

// Compare the transformed initializer against the original.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the code is self-explanatory enough so the comment is unnecessary

continue;
}

assert(D.isArrayRangeDesignator() && "New kind of designator?");
ExprResult Start
= getDerived().TransformExpr(E->getArrayRangeStart(D));
ExprResult Start = getDerived().TransformExpr(E->getArrayRangeStart(D));
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you please leave out the unrelated changes?

@GS-GOAT
Copy link
Contributor Author

GS-GOAT commented Feb 14, 2025

@zyn0217 Please review.

@@ -13665,15 +13665,15 @@ TreeTransform<Derived>::TransformDesignatedInitExpr(DesignatedInitExpr *E) {
}

if (D.isArrayDesignator()) {
ExprResult Index = getDerived().TransformExpr(E->getArrayIndex(D));
if (Index.isInvalid())
ExprResult NewIndex = getDerived().TransformExpr(E->getArrayIndex(D));
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you avoid renaming the variable? Thanks

…lvm#126113

-Transforming Indices: For array designators, transform the index expression
and update ExprChanged if it's modified.
-Correct Initializer Check: Compare the transformed initializer against the
original to accurately track changes.
-Single Initializer Transformation: The initializer is processed once, not
per designator, as each DesignatedInitExpr has one initializer.

Fixes llvm#126113
@GS-GOAT
Copy link
Contributor Author

GS-GOAT commented Feb 15, 2025

@zyn0217 Made the suggested changes.

@zyn0217 zyn0217 merged commit 552e465 into llvm:main Feb 16, 2025
7 checks passed
Copy link

@GS-GOAT Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Feb 24, 2025
…127211)

It was clearly a typo regarding whether the array index expression has changed in its transforming.

This doesn't appear to be a functional change in practice, so no test case or release note provided.

Fixes llvm#126113
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.

[Clang FE] Possible logic issue in TransformTypos::TransformDesignatedInitExpr
3 participants