Subject: bug#48264: [PATCH v3 15/15] Add and use BVAR_FIELD macros

From: Spencer Baugh <[email-masked]>
Date: Sat, 08 May 2021 15:03:46 -0400

Eli Zaretskii <[email-masked]> writes:
>> From: Spencer Baugh <[email-masked]>
>> Cc: [email-masked]
>> Date: Sat, 08 May 2021 09:35:31 -0400
>> If you think such a conditionally-compiled runtime check would be
>> acceptable for applying these changes, I can go ahead and write that.
>
> Yes, I think so.  But if Lars or Stefan think differently, I might
> reconsider.

In the process of implementing the runtime check, I, of course, came up
with a better compile-time check. How about this?

---
 src/buffer.h | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/src/buffer.h b/src/buffer.h
index f78046a9a8..a8a662b1ed 100644
--- a/src/buffer.h
+++ b/src/buffer.h
@@ -280,13 +280,25 @@ struct buffer_text
     bool_bf redisplay : 1;
   };
 
+/* This structure is used purely as a list of field names that are
+   permitted for use with BVAR; it's never actually instantiated.  */
+struct bvar_permanent_locals {
+  char name, filename, directory, backed_up, save_length, auto_save_file_name,
+    read_only, mark, local_var_alist, major_mode, local_minor_modes, mode_name,
+    keymap, downcase_table, upcase_table, case_canon_table, case_eqv_table,
+    mark_active, enable_multibyte_characters, file_format,
+    auto_save_file_format, width_table, pt_marker, begv_marker, zv_marker,
+    point_before_scroll, file_truename, invisibility_spec, last_selected_window,
+    display_count, display_time, undo_list;
+};
 /* Most code should use this macro to access Lisp fields in struct buffer.  */
 
-#define BVAR(buf, field) ((buf)->field ## _)
+#define BVAR(buf, field) \
+  *((void) offsetof (struct bvar_permanent_locals, field), &(buf)->field ## _)
 
-#define BVAR_OR_DEFAULT(buf, field) (EQ (BVAR ((buf), field), Qunbound) \
-				     ? BVAR (&buffer_defaults, field) \
-				     : BVAR ((buf), field))
+#define BVAR_OR_DEFAULT(buf, field) (EQ ((buf)->field ## _, Qunbound)	\
+				     ? buffer_defaults.field ## _	\
+				     : (buf)->field ## _)
 
 /* Max number of builtin per-buffer variables.  */
 enum { MAX_PER_BUFFER_VARS = 50 };
-- 
2.31.1