unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Spencer Baugh <sbaugh@catern.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: Spencer Baugh <sbaugh@catern.com>,
	monnier@iro.umontreal.ca, 48264@debbugs.gnu.org
Subject: bug#48264: [PATCH 2/2] Add compile-time check that BVAR is used correctly
Date: Sun,  9 May 2021 13:09:33 -0400	[thread overview]
Message-ID: <20210509170933.27338-3-sbaugh@catern.com> (raw)
In-Reply-To: <20210509170933.27338-1-sbaugh@catern.com>

BVAR should only be used for permanent-local buffer variables, so
we'll make a list of such variables and check that the field passed to
BVAR is in that list.  The added offsetof call will fail at compile
time if the passed field is not present in bvar_permanent_locals,
while not incurring any overhead at runtime.

We could add a similar check for BVAR_OR_DEFAULT, but it's not as
important since BVAR_OR_DEFAULT will work fine for any Lisp field.

* src/buffer.h (BVAR): Add compile-time check that the passed field is
permanently buffer-local.
(BVAR_OR_DEFAULT): Stop using BVAR, since this can be called on fields
which aren't permanently buffer-local.
(BVAR_DEFAULT): Add.
* src/buffer.c (init_buffer_once):
* src/category.h (Vstandard_category_table):
* src/syntax.h (Vstandard_syntax_table): Use BVAR_DEFAULT instead of
BVAR to access buffer_defaults fields that aren't permanently
buffer-local.
---
 src/buffer.c   | 14 +++++++-------
 src/buffer.h   | 28 +++++++++++++++++++++++-----
 src/category.h |  2 +-
 src/syntax.h   |  2 +-
 4 files changed, 32 insertions(+), 14 deletions(-)

diff --git a/src/buffer.c b/src/buffer.c
index c0d72ef5c5..1ee804bd9d 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -5112,10 +5112,10 @@ init_buffer_once (void)
   bset_abbrev_table (&buffer_defaults, Qnil);
   bset_display_table (&buffer_defaults, Qnil);
   /* Later further initialized by init_{syntax,category}_once.  */
-  BVAR (&buffer_defaults, syntax_table) = Qnil;
-  BVAR (&buffer_defaults, category_table) = Qnil;
+  BVAR_DEFAULT (syntax_table) = Qnil;
+  BVAR_DEFAULT (category_table) = Qnil;
 
-  XSETFASTINT (BVAR (&buffer_defaults, tab_width), 8);
+  XSETFASTINT (BVAR_DEFAULT (tab_width), 8);
   bset_truncate_lines (&buffer_defaults, Qnil);
   bset_word_wrap (&buffer_defaults, Qnil);
   bset_ctl_arrow (&buffer_defaults, Qt);
@@ -5128,11 +5128,11 @@ init_buffer_once (void)
   bset_cursor_in_non_selected_windows (&buffer_defaults, Qt);
 
   bset_buffer_file_coding_system (&buffer_defaults, Qnil);
-  XSETFASTINT (BVAR (&buffer_defaults, fill_column), 70);
-  XSETFASTINT (BVAR (&buffer_defaults, left_margin), 0);
+  XSETFASTINT (BVAR_DEFAULT (fill_column), 70);
+  XSETFASTINT (BVAR_DEFAULT (left_margin), 0);
   bset_cache_long_scans (&buffer_defaults, Qt);
-  XSETFASTINT (BVAR (&buffer_defaults, left_margin_cols), 0);
-  XSETFASTINT (BVAR (&buffer_defaults, right_margin_cols), 0);
+  XSETFASTINT (BVAR_DEFAULT (left_margin_cols), 0);
+  XSETFASTINT (BVAR_DEFAULT (right_margin_cols), 0);
   bset_left_fringe_width (&buffer_defaults, Qnil);
   bset_right_fringe_width (&buffer_defaults, Qnil);
   bset_fringes_outside_margins (&buffer_defaults, Qnil);
diff --git a/src/buffer.h b/src/buffer.h
index f78046a9a8..3e8198bb70 100644
--- a/src/buffer.h
+++ b/src/buffer.h
@@ -280,13 +280,31 @@ struct buffer_text
     bool_bf redisplay : 1;
   };
 
-/* Most code should use this macro to access Lisp fields in struct buffer.  */
+/* 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 BVAR or BVAR_OR_DEFAULT to access Lisp fields
+   in struct buffer.  BVAR should be used for fields that are
+   permanently buffer-local, and BVAR_OR_DEFAULT should be used for
+   fields that may not have a buffer-local binding and should use the
+   default in that case.  */
+#define BVAR(buf, field) \
+  *((void) offsetof (struct bvar_permanent_locals, field), &(buf)->field ## _)
 
-#define BVAR(buf, field) ((buf)->field ## _)
+#define BVAR_OR_DEFAULT(buf, field) (EQ ((buf)->field ## _, Qunbound)	\
+				     ? BVAR_DEFAULT(field)		\
+				     : (buf)->field ## _)
 
-#define BVAR_OR_DEFAULT(buf, field) (EQ (BVAR ((buf), field), Qunbound) \
-				     ? BVAR (&buffer_defaults, field) \
-				     : BVAR ((buf), field))
+#define BVAR_DEFAULT(field) buffer_defaults.field ## _
 
 /* Max number of builtin per-buffer variables.  */
 enum { MAX_PER_BUFFER_VARS = 50 };
diff --git a/src/category.h b/src/category.h
index cc32990478..85e872a940 100644
--- a/src/category.h
+++ b/src/category.h
@@ -94,7 +94,7 @@ CHAR_HAS_CATEGORY (int ch, int category)
 
 /* The standard category table is stored where it will automatically
    be used in all new buffers.  */
-#define Vstandard_category_table BVAR (&buffer_defaults, category_table)
+#define Vstandard_category_table BVAR_DEFAULT (category_table)
 
 /* Return the doc string of CATEGORY in category table TABLE.  */
 #define CATEGORY_DOCSTRING(table, category)				\
diff --git a/src/syntax.h b/src/syntax.h
index 187946c899..c85743808c 100644
--- a/src/syntax.h
+++ b/src/syntax.h
@@ -31,7 +31,7 @@ extern void update_syntax_table_forward (ptrdiff_t, bool, Lisp_Object);
 
 /* The standard syntax table is stored where it will automatically
    be used in all new buffers.  */
-#define Vstandard_syntax_table BVAR (&buffer_defaults, syntax_table)
+#define Vstandard_syntax_table BVAR_DEFAULT (syntax_table)
 
 /* A syntax table is a chartable whose elements are cons cells
    (CODE+FLAGS . MATCHING-CHAR).  MATCHING-CHAR can be nil if the char
-- 
2.31.1






  parent reply	other threads:[~2021-05-09 17:09 UTC|newest]

Thread overview: 84+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-06 20:24 bug#48264: 28.0.50; Changing the default for DEFVAR_PER_BUFFER variables takes O(#buffers) time Spencer Baugh
2021-05-06 21:33 ` bug#48264: [PATCH v3 00/15] Speeding up setting the default for DEFVAR_PER_BUFFER vars Spencer Baugh
2021-05-07 11:05   ` Eli Zaretskii
2021-05-08  2:08   ` bug#48264: [PATCH v4 " Spencer Baugh
2021-05-08  2:08     ` Spencer Baugh
2021-05-08  2:08     ` bug#48264: [PATCH v4 01/14] Stop checking the constant default for enable_multibyte_characters Spencer Baugh
2021-05-08  2:08     ` bug#48264: [PATCH v4 02/14] Take offset not idx in PER_BUFFER_VALUE_P Spencer Baugh
2021-05-08  2:08     ` bug#48264: [PATCH v4 03/14] Add and use BVAR_HAS_DEFAULT_VALUE_P Spencer Baugh
2021-05-08  2:08     ` bug#48264: [PATCH v4 04/14] Combine unnecessarily separate loops in buffer.c Spencer Baugh
2021-05-08  2:08     ` bug#48264: [PATCH v4 05/14] Add and use KILL_PER_BUFFER_VALUE Spencer Baugh
2021-05-08  2:08     ` bug#48264: [PATCH v4 06/14] Rearrange set_internal for buffer forwarded symbols Spencer Baugh
2021-05-08  2:08     ` bug#48264: [PATCH v4 07/14] Use BVAR_OR_DEFAULT for per-buffer vars with defaults Spencer Baugh
2021-05-08  2:08     ` bug#48264: [PATCH v4 08/14] Remove unnecessary Qunbound check Spencer Baugh
2021-05-08  2:09     ` bug#48264: [PATCH v4 09/14] Get rid of buffer_permanent_local_flags array Spencer Baugh
2021-05-08  2:09     ` bug#48264: [PATCH v4 10/14] Delete SET_PER_BUFFER_VALUE_P and buffer local_flags field Spencer Baugh
2021-05-08  2:09     ` bug#48264: [PATCH v4 11/14] Set buffer_defaults fields without a default to Qunbound Spencer Baugh
2021-05-08  2:09     ` bug#48264: [PATCH v4 12/14] Assert that PER_BUFFER_IDX for Lisp variables is not 0 Spencer Baugh
2021-05-08  2:09     ` bug#48264: [PATCH v4 13/14] Remove PER_BUFFER_IDX and buffer_local_flags Spencer Baugh
2021-05-08  2:09     ` bug#48264: [PATCH v4 14/14] Add and use BVAR_FIELD macros Spencer Baugh
2021-05-08 18:06     ` bug#48264: [PATCH v4 00/15] Speeding up setting the default for DEFVAR_PER_BUFFER vars Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-05-06 21:33 ` bug#48264: [PATCH v3 01/15] Stop checking the constant default for enable_multibyte_characters Spencer Baugh
2021-05-06 21:33 ` bug#48264: [PATCH v3 02/15] Take offset not idx in PER_BUFFER_VALUE_P Spencer Baugh
2021-05-07  7:27   ` Eli Zaretskii
2021-05-07 12:45     ` Spencer Baugh
2021-05-07 12:54       ` Eli Zaretskii
2021-05-06 21:33 ` bug#48264: [PATCH v3 03/15] Add and use BUFFER_DEFAULT_VALUE_P Spencer Baugh
2021-05-07  7:29   ` Eli Zaretskii
2021-05-07 12:49     ` Spencer Baugh
2021-05-07 12:58       ` Eli Zaretskii
2021-05-07 13:38         ` Spencer Baugh
2021-05-07 13:42           ` Eli Zaretskii
2021-05-07 14:30             ` Spencer Baugh
2021-05-07 14:39               ` Eli Zaretskii
2021-05-06 21:33 ` bug#48264: [PATCH v3 04/15] Combine unnecessarily separate loops in buffer.c Spencer Baugh
2021-05-06 21:33 ` bug#48264: [PATCH v3 05/15] Add and use KILL_PER_BUFFER_VALUE Spencer Baugh
2021-05-06 21:33 ` bug#48264: [PATCH v3 06/15] Rearrange set_internal for buffer forwarded symbols Spencer Baugh
2021-05-07 10:45   ` Eli Zaretskii
2021-05-07 14:26     ` Spencer Baugh
2021-05-06 21:33 ` bug#48264: [PATCH v3 07/15] Add BVAR_OR_DEFAULT macro as a stub Spencer Baugh
2021-05-07 10:54   ` Eli Zaretskii
2021-05-07 13:05     ` Spencer Baugh
2021-05-07 13:12       ` Eli Zaretskii
2021-05-07 13:24         ` Spencer Baugh
2021-05-07 13:32           ` Eli Zaretskii
2021-05-06 21:33 ` bug#48264: [PATCH v3 08/15] Set non-buffer-local BVARs to Qunbound Spencer Baugh
2021-05-07 10:37   ` Eli Zaretskii
2021-05-07 12:54     ` Spencer Baugh
2021-05-07 13:00       ` Eli Zaretskii
2021-05-06 21:33 ` bug#48264: [PATCH v3 09/15] Remove unnecessary Qunbound check Spencer Baugh
2021-05-06 21:33 ` bug#48264: [PATCH v3 10/15] Get rid of buffer_permanent_local_flags array Spencer Baugh
2021-05-06 21:33 ` bug#48264: [PATCH v3 11/15] Delete SET_PER_BUFFER_VALUE_P and buffer local_flags field Spencer Baugh
2021-05-06 21:33 ` bug#48264: [PATCH v3 12/15] Set buffer_defaults fields without a default to Qunbound Spencer Baugh
2021-05-07 10:42   ` Eli Zaretskii
2021-05-07 13:20     ` Spencer Baugh
2021-05-07 13:29       ` Eli Zaretskii
2021-05-07 14:15         ` Spencer Baugh
2021-05-07 14:30           ` Eli Zaretskii
2021-05-07 21:35             ` Spencer Baugh
2021-05-08  6:40               ` Eli Zaretskii
2021-05-08 13:22                 ` Spencer Baugh
2021-05-06 21:33 ` bug#48264: [PATCH v3 13/15] Assert that PER_BUFFER_IDX for Lisp variables is not 0 Spencer Baugh
2021-05-06 21:33 ` bug#48264: [PATCH v3 14/15] Remove PER_BUFFER_IDX and buffer_local_flags Spencer Baugh
2021-05-06 21:33 ` bug#48264: [PATCH v3 15/15] Add and use BVAR_FIELD macros Spencer Baugh
2021-05-07 11:03   ` Eli Zaretskii
2021-05-07 12:59     ` Spencer Baugh
2021-05-07 13:08       ` Eli Zaretskii
2021-05-07 21:43         ` Spencer Baugh
2021-05-08  6:55           ` Eli Zaretskii
2021-05-08 13:35             ` Spencer Baugh
2021-05-08 13:58               ` Eli Zaretskii
2021-05-08 17:13                 ` Spencer Baugh
2021-05-08 19:03                 ` Spencer Baugh
2021-05-09  8:10                   ` Eli Zaretskii
2021-05-09 17:09                     ` Spencer Baugh
2021-05-09 17:09                       ` bug#48264: [PATCH 1/2] Take buffer field name in DEFVAR_PER_BUFFER Spencer Baugh
2021-05-09 17:09                       ` Spencer Baugh [this message]
2021-05-09 18:09                         ` bug#48264: [PATCH 2/2] Add compile-time check that BVAR is used correctly Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-05-09 18:29                           ` Eli Zaretskii
2021-05-10 14:09                             ` Spencer Baugh
2022-07-01 12:18                               ` bug#48264: 28.0.50; Changing the default for DEFVAR_PER_BUFFER variables takes O(#buffers) time Lars Ingebrigtsen
2022-07-01 18:49                                 ` Spencer Baugh
2022-07-02 12:00                                   ` Lars Ingebrigtsen
2022-08-02 11:11                                     ` Lars Ingebrigtsen
2021-05-09 10:08                 ` bug#48264: [PATCH v3 15/15] Add and use BVAR_FIELD macros Lars Ingebrigtsen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210509170933.27338-3-sbaugh@catern.com \
    --to=sbaugh@catern.com \
    --cc=48264@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).