From 333349eb7b33b83f51c8e122df04e848f89135d3 Mon Sep 17 00:00:00 2001 From: Faisal Shah Date: Wed, 26 Mar 2025 20:02:43 -0500 Subject: [PATCH] Sort items to guarantee the same output each time --- ctypeslib/codegen/codegenerator.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/ctypeslib/codegen/codegenerator.py b/ctypeslib/codegen/codegenerator.py index 7d9d9fa..28a4f95 100644 --- a/ctypeslib/codegen/codegenerator.py +++ b/ctypeslib/codegen/codegenerator.py @@ -1082,6 +1082,14 @@ def generate_code(self, output): self.filter_types() self.filter_symbols() self.filter_expressions() + + # clang's AST does not guarantee a fixed declaration order + # sort items by name or location. This results in a reproducible output. + self.filtered_items = sorted( + self.filtered_items, + key=lambda x: (x.location[0], x.location[1], x.name) if hasattr(x, 'location') else x.name + ) + log.debug("Left with %d items after filtering", len(self.filtered_items)) loops = self.generator.generate(self.parser, self.filtered_items) if self.cfg.verbose: