[PATCH] D141625: [DeclContext] Sort the Decls before adding into DeclContext

Steven Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 12 11:37:39 PST 2023


steven_wu created this revision.
steven_wu added reviewers: jansvoboda11, akyrtzi, benlangmuir, vsapsai, rnk, dblaikie.
Herald added subscribers: ributzka, mgrang.
Herald added a project: All.
steven_wu requested review of this revision.
Herald added a project: clang.

Fix a non-deterministic issue in clang module generation, which the
anonymous declaration number from a function context is not
deterministic. This is due to the unstable iteration order for decls in
scope so the order after moving the decls into function decl context is
not deterministic.

>From https://reviews.llvm.org/D135118, we can't use a set that preserves
the order without the performance penalty. Fix the issue by sorting the
decls based on raw encoding of their source location.

rdar://104097976


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D141625

Files:
  clang/lib/Parse/ParseDecl.cpp


Index: clang/lib/Parse/ParseDecl.cpp
===================================================================
--- clang/lib/Parse/ParseDecl.cpp
+++ clang/lib/Parse/ParseDecl.cpp
@@ -6986,6 +6986,13 @@
         continue;
       DeclsInPrototype.push_back(ND);
     }
+    // Sort DeclsInPrototype based on raw encoding of the source location. This
+    // provides a stable iterating order in DeclContext, which is important for
+    // tasks like ASTWriter for deterministic output.
+    llvm::sort(DeclsInPrototype, [](Decl *D1, Decl *D2) {
+      return D1->getLocation().getRawEncoding() <
+             D2->getLocation().getRawEncoding();
+    });
   }
 
   // Remember that we parsed a function type, and remember the attributes.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D141625.488728.patch
Type: text/x-patch
Size: 730 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230112/75afcef3/attachment.bin>


More information about the cfe-commits mailing list