Skip to content

Commit

Permalink
CC Mode: correct the calculation and handling of c-use-category.
Browse files Browse the repository at this point in the history
This fixes bug #56629.  The use of c-use-category was inconsistent, with the
result that it would be nil at compilation time, but t at run time.  This
resulted in wrongly writing syntax-table text properties to <s and >s, yet
testing for category properties on them.

* lisp/progmodes/cc-defs.el (c-use-category): Move to after the definition of
c-<-as-paren-syntax and c->-as-paren-syntax so as correctly to be able to use
their values.  Put an eval-when-compile around the calculation of its value,
to reduce the chances of future failure.
(c-mark-<-as-paren, c-mark->-as-paren, c-unmark-<->-as-paren, c-sc-scan-lists)
(c-sc-parse-partial-sexp): Wrap c-use-category in (eval-when-compile ...) as
an optimization, preventing the XEmacs code also being generated.
  • Loading branch information
Alan Mackenzie committed Jul 26, 2022
1 parent 058d3c0 commit 004ee6b
Showing 1 changed file with 33 additions and 26 deletions.
59 changes: 33 additions & 26 deletions lisp/progmodes/cc-defs.el
Original file line number Diff line number Diff line change
Expand Up @@ -425,23 +425,6 @@ to it is returned. This function does not modify the point or the mark."

(defvar lookup-syntax-properties) ;XEmacs.

(eval-and-compile
;; Constant to decide at compilation time whether to use category
;; properties. Currently (2010-03) they're available only on GNU Emacs.
(defconst c-use-category
(with-temp-buffer
(let ((parse-sexp-lookup-properties t)
(lookup-syntax-properties t))
(set-syntax-table (make-syntax-table))
(insert "<()>")
(put-text-property (point-min) (1+ (point-min))
'category 'c-<-as-paren-syntax)
(put-text-property (+ 3 (point-min)) (+ 4 (point-min))
'category 'c->-as-paren-syntax)
(goto-char (point-min))
(forward-sexp)
(= (point) (+ 4 (point-min)))))))

(defmacro c-is-escaped (pos)
;; Are there an odd number of backslashes before POS?
(declare (debug t))
Expand Down Expand Up @@ -1147,11 +1130,13 @@ MODE is either a mode symbol or a list of mode symbols."
(cc-bytecomp-fboundp 'delete-extent)
(cc-bytecomp-fboundp 'map-extents))))

(defconst c-<-as-paren-syntax '(4 . ?>))
(put 'c-<-as-paren-syntax 'syntax-table c-<-as-paren-syntax)
(eval-and-compile
(defconst c-<-as-paren-syntax '(4 . ?>))
(put 'c-<-as-paren-syntax 'syntax-table c-<-as-paren-syntax))

(defconst c->-as-paren-syntax '(5 . ?<))
(put 'c->-as-paren-syntax 'syntax-table c->-as-paren-syntax)
(eval-and-compile
(defconst c->-as-paren-syntax '(5 . ?<))
(put 'c->-as-paren-syntax 'syntax-table c->-as-paren-syntax))

;; `c-put-char-property' is complex enough in XEmacs and Emacs < 21 to
;; make it a function.
Expand Down Expand Up @@ -1210,6 +1195,26 @@ MODE is either a mode symbol or a list of mode symbols."
`((setq c-syntax-table-hwm (min c-syntax-table-hwm -pos-))))
(put-text-property -pos- (1+ -pos-) ',property ,value))))

(eval-and-compile
;; Constant to decide at compilation time whether to use category
;; properties. Currently (2010-03) they're available only on GNU
;; Emacs. This defconst must follow the declarations of
;; `c-<-as-paren-syntax' and `c->-as-paren-syntax'.
(defconst c-use-category
(eval-when-compile
(with-temp-buffer
(let ((parse-sexp-lookup-properties t)
(lookup-syntax-properties t))
(set-syntax-table (make-syntax-table))
(insert "<()>")
(put-text-property (point-min) (1+ (point-min))
'category 'c-<-as-paren-syntax)
(put-text-property (+ 3 (point-min)) (+ 4 (point-min))
'category 'c->-as-paren-syntax)
(goto-char (point-min))
(forward-sexp)
(= (point) (+ 4 (point-min))))))))

(defmacro c-get-char-property (pos property)
;; Get the value of the given property on the character at POS if
;; it's been put there by `c-put-char-property'. PROPERTY is
Expand Down Expand Up @@ -1646,7 +1651,7 @@ with value CHAR in the region [FROM to)."
;; toggle the property in all template brackets simultaneously and
;; cheaply. We use this, for instance, in `c-parse-state'.
(declare (debug t))
(if c-use-category
(if (eval-when-compile c-use-category)
`(c-put-char-property ,pos 'category 'c-<-as-paren-syntax)
`(c-put-char-property ,pos 'syntax-table c-<-as-paren-syntax)))

Expand All @@ -1661,7 +1666,7 @@ with value CHAR in the region [FROM to)."
;; toggle the property in all template brackets simultaneously and
;; cheaply. We use this, for instance, in `c-parse-state'.
(declare (debug t))
(if c-use-category
(if (eval-when-compile c-use-category)
`(c-put-char-property ,pos 'category 'c->-as-paren-syntax)
`(c-put-char-property ,pos 'syntax-table c->-as-paren-syntax)))

Expand All @@ -1675,7 +1680,9 @@ with value CHAR in the region [FROM to)."
;; toggle the property in all template brackets simultaneously and
;; cheaply. We use this, for instance, in `c-parse-state'.
(declare (debug t))
`(c-clear-char-property ,pos ,(if c-use-category ''category ''syntax-table)))
`(c-clear-char-property ,pos ,(if (eval-when-compile c-use-category)
''category
''syntax-table)))

(defsubst c-suppress-<->-as-parens ()
;; Suppress the syntactic effect of all marked < and > as parens. Note
Expand Down Expand Up @@ -1755,7 +1762,7 @@ with value CHAR in the region [FROM to)."

(defmacro c-sc-scan-lists (from count depth)
(declare (debug t))
(if c-use-category
(if (eval-when-compile c-use-category)
`(scan-lists ,from ,count ,depth)
(cond
((and (eq count 1) (eq depth 1))
Expand Down Expand Up @@ -1803,7 +1810,7 @@ with value CHAR in the region [FROM to)."
(defmacro c-sc-parse-partial-sexp (from to &optional targetdepth stopbefore
oldstate)
(declare (debug t))
(if c-use-category
(if (eval-when-compile c-use-category)
`(parse-partial-sexp ,from ,to ,targetdepth ,stopbefore ,oldstate)
`(c-sc-parse-partial-sexp-no-category ,from ,to ,targetdepth ,stopbefore
,oldstate)))
Expand Down

0 comments on commit 004ee6b

Please # to comment.