From 5a11a91f68c482f2ba72e3fbaa8c1f1f66aa1a2e Mon Sep 17 00:00:00 2001 From: Alan Mackenzie Date: Sat, 28 Dec 2024 17:16:04 +0000 Subject: [PATCH] CC Mode: Minor corrections and tidy ups for cache invalidation These cache variables are c-lit-pos-cache-limit, c-semi-near-cache-limit, c-full-near-cache-limit, and c-state-cache-invalid-pos. * lisp/progmodes/cc-awk.el (c-awk-set-string-regexp-syntax-table-properties): Invalidate the caches after changing the 'syntax-table property. * lisp/progmodes/cc-defs.el (c-put-string-fence-trim-caches): New macro. (c-unmark-<-or->-as-paren): New name for c-unmark-<->-as-paren. (Also renamed throughout CC Mode) * lisp/progmodes/cc-engine.el (c-propertize-ml-string-opener): Add missing cache invalidation. * lisp/progmodes/cc-mode.el (c-neutralize-CPP-line): Remove syntax-table properties from < and > as needed. (c-put-syn-tab): Add missing cache invalidation. (cherry picked from commit 0334b0743f83a3e69d1d3a426efbba1d2a5f2cc6) --- lisp/progmodes/cc-awk.el | 12 ++++++------ lisp/progmodes/cc-defs.el | 12 ++++++++++-- lisp/progmodes/cc-engine.el | 26 ++++++++++++-------------- lisp/progmodes/cc-mode.el | 18 +++++++++++------- 4 files changed, 39 insertions(+), 29 deletions(-) diff --git a/lisp/progmodes/cc-awk.el b/lisp/progmodes/cc-awk.el index a40999e6334..8ebd2168969 100644 --- a/lisp/progmodes/cc-awk.el +++ b/lisp/progmodes/cc-awk.el @@ -754,15 +754,15 @@ (if (eq (char-after beg) ?_) (setq beg (1+ beg))) ;; First put the properties on the delimiters. - (cond ((eq end (point-max)) ; string/regexp terminated by EOB - (c-put-string-fence beg)) + (cond ((eq end (point-max)) ; string/regexp terminated by EOB + (c-put-string-fence-trim-caches beg)) ((/= (char-after beg) (char-after end)) ; missing end delimiter - (c-put-string-fence beg) + (c-put-string-fence-trim-caches beg) (c-put-string-fence end)) ((eq (char-after beg) ?/) ; Properly bracketed regexp - (c-put-char-property beg 'syntax-table '(7)) ; (7) = "string" - (c-put-syntax-table-trim-caches end '(7))) - (t)) ; Properly bracketed string: Nothing to do. + (c-put-syntax-table-trim-caches beg '(7)) ; (7) = "string" + (c-put-char-property end 'syntax-table '(7))) + (t)) ; Properly bracketed string: Nothing to do. ;; Now change the properties of any escaped "s in the string to punctuation. (save-excursion (goto-char (1+ beg)) diff --git a/lisp/progmodes/cc-defs.el b/lisp/progmodes/cc-defs.el index 55f268980e2..55b9f8bdff4 100644 --- a/lisp/progmodes/cc-defs.el +++ b/lisp/progmodes/cc-defs.el @@ -1267,6 +1267,14 @@ MODE is either a mode symbol or a list of mode symbols." (c-clear-char-property -pos- 'c-is-sws) (c-clear-char-property -pos- 'c-in-sws))) +(defmacro c-put-string-fence-trim-caches (pos) + ;; Put the string-fence syntax-table text property at POS, and invalidate + ;; the four caches from position POS. + (declare (debug t)) + `(let ((-pos- ,pos)) + (c-put-string-fence -pos-) + (c-truncate-lit-pos/state-cache -pos-))) + (eval-and-compile ;; Constant to decide at compilation time whether to use category ;; properties. Currently (2010-03) they're available only on GNU @@ -1862,8 +1870,8 @@ from the first position, if any, where a property was put." `(c-put-char-property ,pos 'category 'c->-as-paren-syntax) `(c-put-char-property ,pos 'syntax-table c->-as-paren-syntax))) -(defmacro c-unmark-<->-as-paren (pos) - ;; Unmark the "<" or "<" character at POS as an sexp list opener using the +(defmacro c-unmark-<-or->-as-paren (pos) + ;; Unmark the "<" or ">" character at POS as an sexp list opener using the ;; `syntax-table' property either directly or indirectly through a ;; `category' text property. ;; diff --git a/lisp/progmodes/cc-engine.el b/lisp/progmodes/cc-engine.el index 77ee8f9e6e6..17b18f6e7e4 100644 --- a/lisp/progmodes/cc-engine.el +++ b/lisp/progmodes/cc-engine.el @@ -7026,9 +7026,9 @@ comment at the start of cc-engine.el for more info." (c-go-list-forward)) (when (equal (c-get-char-property (1- (point)) 'syntax-table) c->-as-paren-syntax) ; should always be true. - (c-unmark-<->-as-paren (1- (point))) + (c-unmark-<-or->-as-paren (1- (point))) (c-truncate-lit-pos/state-cache (1- (point)))) - (c-unmark-<->-as-paren pos) + (c-unmark-<-or->-as-paren pos) (c-truncate-lit-pos/state-cache pos)))) (defun c-clear->-pair-props (&optional pos) @@ -7045,9 +7045,9 @@ comment at the start of cc-engine.el for more info." (c-go-up-list-backward)) (when (equal (c-get-char-property (point) 'syntax-table) c-<-as-paren-syntax) ; should always be true. - (c-unmark-<->-as-paren (point)) + (c-unmark-<-or->-as-paren (point)) (c-truncate-lit-pos/state-cache (point))) - (c-unmark-<->-as-paren pos) + (c-unmark-<-or->-as-paren pos) (c-truncate-lit-pos/state-cache pos)))) (defun c-clear-<>-pair-props (&optional pos) @@ -7080,10 +7080,10 @@ comment at the start of cc-engine.el for more info." (when (and (>= (point) lim) (equal (c-get-char-property (1- (point)) 'syntax-table) c->-as-paren-syntax)) ; should always be true. - (c-unmark-<->-as-paren (1- (point))) - (c-unmark-<->-as-paren pos) + (c-unmark-<-or->-as-paren (1- (point))) + (c-unmark-<-or->-as-paren pos) (c-truncate-lit-pos/state-cache pos) - (point))))) + (point))))) (defun c-clear->-pair-props-if-match-before (lim &optional pos) ;; POS (default point) is at a > character. If it is both marked @@ -7102,9 +7102,9 @@ comment at the start of cc-engine.el for more info." (when (and (<= (point) lim) (equal (c-get-char-property (point) 'syntax-table) c-<-as-paren-syntax)) ; should always be true. - (c-unmark-<->-as-paren (point)) + (c-unmark-<-or->-as-paren (point)) (c-truncate-lit-pos/state-cache (point)) - (c-unmark-<->-as-paren pos) + (c-unmark-<-or->-as-paren pos) (point))))) ;; Set by c-common-init in cc-mode.el. @@ -8205,7 +8205,7 @@ multi-line strings (but not C++, for example)." (goto-char (cadr end-delim)) t) (c-put-syntax-table-trim-caches (cddr delim) '(1)) - (c-put-string-fence (1- (cadr delim))) + (c-put-string-fence-trim-caches (1- (cadr delim))) (when bound ;; In a CPP construct, we try to apply a generic-string ;; `syntax-table' text property to the last possible character in @@ -8233,9 +8233,7 @@ multi-line strings (but not C++, for example)." "\\(\\\\\n\\)*\\=")) ; 11 (cadr delim) t)) (if (match-beginning 10) - (progn - (c-put-string-fence (match-beginning 10)) - (c-truncate-lit-pos/state-cache (match-beginning 10))) + (c-put-string-fence-trim-caches (match-beginning 10)) (c-put-syntax-table-trim-caches (match-beginning 5) '(1)) (c-put-string-fence (1+ (match-beginning 5))))) (goto-char bound)) @@ -8938,7 +8936,7 @@ multi-line strings (but not C++, for example)." (and (c-go-list-backward) (eq (char-after) ?<) (c-truncate-lit-pos/state-cache (point)) - (c-unmark-<->-as-paren (point))))) + (c-unmark-<-or->-as-paren (point))))) (c-mark-<-as-paren start) (c-mark->-as-paren (1- (point))) (c-truncate-lit-pos/state-cache start)) diff --git a/lisp/progmodes/cc-mode.el b/lisp/progmodes/cc-mode.el index 7f1396df520..4daca559212 100644 --- a/lisp/progmodes/cc-mode.el +++ b/lisp/progmodes/cc-mode.el @@ -1113,16 +1113,20 @@ Note that the style variables are always made local to the buffer." (progn (setq s (parse-partial-sexp beg end -1)) (cond - ((< (nth 0 s) 0) ; found an unmated ),},] - (c-put-syntax-table-trim-caches (1- (point)) '(1)) + ((< (nth 0 s) 0) ; found an unmated ),},],> + (if (eq (char-before) ?>) + (c-clear->-pair-props (1- (point))) + (c-put-syntax-table-trim-caches (1- (point)) '(1))) t) ;; Unbalanced strings are now handled by ;; `c-before-change-check-unbalanced-strings', etc. ;; ((nth 3 s) ; In a string ;; (c-put-char-property (nth 8 s) 'syntax-table '(1)) ;; t) - ((> (nth 0 s) 0) ; In a (,{,[ - (c-put-syntax-table-trim-caches (nth 1 s) '(1)) + ((> (nth 0 s) 0) ; In a (,{,[,< + (if (eq (char-after (nth 1 s)) ?<) + (c-clear-<-pair-props (nth 1 s)) + (c-put-syntax-table-trim-caches (nth 1 s) '(1))) t) (t nil))))))) @@ -1271,7 +1275,7 @@ Note that the style variables are always made local to the buffer." ;; `(let ((-pos- ,pos) ;; (-value- ,value)) (if (equal value '(15)) - (c-put-string-fence pos) + (c-put-string-fence-trim-caches pos) (c-put-syntax-table-trim-caches pos value)) (c-put-char-property pos 'c-fl-syn-tab value) (cond @@ -2023,11 +2027,11 @@ This function is used solely as a member of (looking-at "\\s(") (looking-at "\\(<\\)[^>\n\r]*\\(>\\)?") (not (cdr (c-semi-pp-to-literal hash-pos)))) - (c-unmark-<->-as-paren (match-beginning 1)) + (c-unmark-<-or->-as-paren (match-beginning 1)) (when (< hash-pos c-new-BEG) (setq c-new-BEG hash-pos)) (when (match-beginning 2) - (c-unmark-<->-as-paren (match-beginning 2)) + (c-unmark-<-or->-as-paren (match-beginning 2)) (when (> (match-end 2) c-new-END) (setq c-new-END (match-end 2)))))))) -- 2.39.5