Skip to content

[clang][NFC] Use range-based for loops #96831

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 3 commits into from
Jul 1, 2024

Conversation

MagentaTreehouse
Copy link
Contributor

Use range-based for loops. In addition, extracted a loop from CXXRecordDecl::completeDefinition to eliminate the Done flag, and only construct MyFinalOverriders when FinalOverriders is null.

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

llvmbot commented Jun 26, 2024

@llvm/pr-subscribers-clang

Author: None (MagentaTreehouse)

Changes

Use range-based for loops. In addition, extracted a loop from CXXRecordDecl::completeDefinition to eliminate the Done flag, and only construct MyFinalOverriders when FinalOverriders is null.


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

1 Files Affected:

  • (modified) clang/lib/AST/DeclCXX.cpp (+32-32)
diff --git a/clang/lib/AST/DeclCXX.cpp b/clang/lib/AST/DeclCXX.cpp
index 7f2c786547b9b..18f2b76d9e781 100644
--- a/clang/lib/AST/DeclCXX.cpp
+++ b/clang/lib/AST/DeclCXX.cpp
@@ -1521,11 +1521,11 @@ void CXXRecordDecl::setCaptures(ASTContext &Context,
   auto *ToCapture = (LambdaCapture *)Context.Allocate(sizeof(LambdaCapture) *
                                                       Captures.size());
   Data.AddCaptureList(Context, ToCapture);
-  for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
-    if (Captures[I].isExplicit())
+  for (const LambdaCapture &C : Captures) {
+    if (C.isExplicit())
       ++Data.NumExplicitCaptures;
 
-    new (ToCapture) LambdaCapture(Captures[I]);
+    new (ToCapture) LambdaCapture(C);
     ToCapture++;
   }
 
@@ -2056,40 +2056,40 @@ void CXXRecordDecl::completeDefinition() {
   completeDefinition(nullptr);
 }
 
+static bool hasPureVirtualFinalOverrider(
+    const CXXRecordDecl &RD, const CXXFinalOverriderMap *FinalOverriders) {
+  auto ExistsIn = [](const CXXFinalOverriderMap &FinalOverriders) {
+    for (const auto &[_, M] : FinalOverriders) {
+      for (const auto &[_, SO] : M) {
+        assert(SO.size() > 0 &&
+              "All virtual functions have overriding virtual functions");
+
+        if (SO.front().Method->isPureVirtual())
+          return true;
+      }
+    }
+    return false;
+  };
+
+  if (FinalOverriders)
+    return ExistsIn(*FinalOverriders);
+  CXXFinalOverriderMap MyFinalOverriders;
+  RD.getFinalOverriders(MyFinalOverriders);
+  return ExistsIn(MyFinalOverriders);
+}
+
 void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
   RecordDecl::completeDefinition();
 
   // If the class may be abstract (but hasn't been marked as such), check for
   // any pure final overriders.
-  if (mayBeAbstract()) {
-    CXXFinalOverriderMap MyFinalOverriders;
-    if (!FinalOverriders) {
-      getFinalOverriders(MyFinalOverriders);
-      FinalOverriders = &MyFinalOverriders;
-    }
-
-    bool Done = false;
-    for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
-                                     MEnd = FinalOverriders->end();
-         M != MEnd && !Done; ++M) {
-      for (OverridingMethods::iterator SO = M->second.begin(),
-                                    SOEnd = M->second.end();
-           SO != SOEnd && !Done; ++SO) {
-        assert(SO->second.size() > 0 &&
-               "All virtual functions have overriding virtual functions");
-
-        // C++ [class.abstract]p4:
-        //   A class is abstract if it contains or inherits at least one
-        //   pure virtual function for which the final overrider is pure
-        //   virtual.
-        if (SO->second.front().Method->isPureVirtual()) {
-          data().Abstract = true;
-          Done = true;
-          break;
-        }
-      }
-    }
-  }
+  //
+  // C++ [class.abstract]p4:
+  //   A class is abstract if it contains or inherits at least one
+  //   pure virtual function for which the final overrider is pure
+  //   virtual.
+  if (mayBeAbstract() && hasPureVirtualFinalOverrider(*this, FinalOverriders))
+    markAbstract();
 
   // Set access bits correctly on the directly-declared conversions.
   for (conversion_iterator I = conversion_begin(), E = conversion_end();

@tbaederr tbaederr requested review from cor3ntin and Endilll June 27, 2024 04:40
Copy link
Contributor

@Endilll Endilll left a comment

Choose a reason for hiding this comment

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

This looks good overall, but I have minor suggestions.

Copy link
Contributor

@MitalAshok MitalAshok left a comment

Choose a reason for hiding this comment

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

LGTM

@Sirraide Sirraide merged commit a662750 into llvm:main Jul 1, 2024
7 checks passed
@MagentaTreehouse MagentaTreehouse deleted the use-range-for branch July 1, 2024 19:34
lravenclaw pushed a commit to lravenclaw/llvm-project that referenced this pull request Jul 3, 2024
Use range-based for loops. In addition, extracted a loop from
`CXXRecordDecl::completeDefinition` to eliminate the `Done` flag, and
only construct `MyFinalOverriders` when `FinalOverriders` is null.
kbluck pushed a commit to kbluck/llvm-project that referenced this pull request Jul 6, 2024
Use range-based for loops. In addition, extracted a loop from
`CXXRecordDecl::completeDefinition` to eliminate the `Done` flag, and
only construct `MyFinalOverriders` when `FinalOverriders` is null.
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.

5 participants