Skip to content

Commit 39acaff

Browse files
author
Alan Mackenzie
committed
CC Mode: Fix multiline block comments in macros.
In particulr, handle multiline block comments whose newlines are not escaped. There is an example of this in #define EXTRA_CONTEXT_FIELDS in editfns.c. * lisp/progmodes/cc-engine.el (c-beginning-of-macro, c-end-of-macro): Enclose the loops scanning escaped newlines with outer loops which check heuristically for, respectively, a block comment ender and a block comment starter on the lines we end up on. (A rigorous syntactic check would be too slow, here.) * lisp/progmodes/cc-langs.el (c-last-c-comment-end-on-line-re) (c-last-open-c-comment-start-on-line-re): New language constants/variables.
1 parent 3fa9c9f commit 39acaff

File tree

2 files changed

+60
-9
lines changed

2 files changed

+60
-9
lines changed

lisp/progmodes/cc-engine.el

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,8 @@ otherwise return nil and leave point unchanged.
287287

288288
Note that this function might do hidden buffer changes. See the
289289
comment at the start of cc-engine.el for more info."
290-
(let ((here (point)))
290+
(let ((here (point))
291+
(pause (c-point 'eol)))
291292
(when c-opt-cpp-prefix
292293
(if (and (car c-macro-cache)
293294
(>= (point) (car c-macro-cache))
@@ -307,8 +308,23 @@ comment at the start of cc-engine.el for more info."
307308
(save-restriction
308309
(if lim (narrow-to-region lim (point-max)))
309310
(beginning-of-line)
310-
(while (eq (char-before (1- (point))) ?\\)
311-
(forward-line -1))
311+
(when (or (null lim)
312+
(>= here lim))
313+
(while
314+
(progn
315+
(while (eq (char-before (1- (point))) ?\\)
316+
(forward-line -1))
317+
(when (and c-last-c-comment-end-on-line-re
318+
(re-search-forward
319+
c-last-c-comment-end-on-line-re pause t))
320+
(goto-char (match-end 1))
321+
(if (c-backward-single-comment)
322+
(progn
323+
(beginning-of-line)
324+
(setq pause (point)))
325+
(goto-char pause)
326+
nil)))))
327+
312328
(back-to-indentation)
313329
(if (and (<= (point) here)
314330
(save-match-data (looking-at c-opt-cpp-start))
@@ -345,12 +361,23 @@ comment at the start of cc-engine.el for more info."
345361
c-macro-cache-start-pos nil
346362
c-macro-cache-syntactic nil
347363
c-macro-cache-no-comment nil))
348-
(while (progn
349-
(end-of-line)
350-
(when (and (eq (char-before) ?\\)
351-
(not (eobp)))
352-
(forward-char)
353-
t)))
364+
(while
365+
(progn
366+
(while (progn
367+
(end-of-line)
368+
(when (and (eq (char-before) ?\\)
369+
(not (eobp)))
370+
(forward-char)
371+
t)))
372+
(if (and c-last-open-c-comment-start-on-line-re
373+
(re-search-backward
374+
c-last-open-c-comment-start-on-line-re
375+
(c-point 'bol) t))
376+
(progn
377+
(goto-char (match-beginning 1))
378+
(c-forward-single-comment))
379+
nil)))
380+
354381
(when (and (car c-macro-cache)
355382
(bolp)
356383
(not (eq (char-before (1- (point))) ?\\)))
@@ -2007,6 +2034,10 @@ comment at the start of cc-engine.el for more info."
20072034
;; Take elaborate precautions to detect an open block comment at
20082035
;; the end of a macro. If we find one, we set `safe-start' to nil
20092036
;; and break off any further scanning of comments.
2037+
;;
2038+
;; (2019-05-02): `c-end-of-macro' now moves completely over block
2039+
;; comments, even multiline ones lacking \s at their EOLs. So a
2040+
;; lot of the following is probably redundant now.
20102041
(let ((com-begin (point)) com-end in-macro)
20112042
(when (and (c-forward-single-comment)
20122043
(setq com-end (point))

lisp/progmodes/cc-langs.el

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,6 +1589,26 @@ properly."
15891589
(c-lang-defvar c-line-comment-start-regexp
15901590
(c-lang-const c-line-comment-start-regexp))
15911591

1592+
(c-lang-defconst c-last-c-comment-end-on-line-re
1593+
"Regexp which matches the last block comment ender on the
1594+
current line, if any, or nil in those languages without block
1595+
comments. When a match is found, submatch 1 contains the comment
1596+
ender."
1597+
t "\\(\\*/\\)\\([^*]\\|\\*[^/]\\)*$"
1598+
awk nil)
1599+
(c-lang-defvar c-last-c-comment-end-on-line-re
1600+
(c-lang-const c-last-c-comment-end-on-line-re))
1601+
1602+
(c-lang-defconst c-last-open-c-comment-start-on-line-re
1603+
"Regexp which matches the last block comment start on the
1604+
current ine, if any, or nil in those languages without block
1605+
comments. When a match is found, submatch 1 contains the comment
1606+
starter."
1607+
t "\\(/\\*\\)\\([^*]\\|\\*[^/]\\)*$"
1608+
awk nil)
1609+
(c-lang-defvar c-last-open-c-comment-start-on-line-re
1610+
(c-lang-const c-last-open-c-comment-start-on-line-re))
1611+
15921612
(c-lang-defconst c-literal-start-regexp
15931613
;; Regexp to match the start of comments and string literals.
15941614
t (concat (c-lang-const c-comment-start-regexp)

0 commit comments

Comments
 (0)