aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlan Mackenzie2017-12-13 20:55:03 +0000
committerAlan Mackenzie2017-12-13 20:55:03 +0000
commit4cb8696e4754d815efd5fd5e26f2b6b2567a11fe (patch)
tree7272c661d7d1cd25d00deb9d1209a99507903dcb
parentce31e726adbb4d24557b3d1ff067cc4c04d94446 (diff)
downloademacs-4cb8696e4754d815efd5fd5e26f2b6b2567a11fe.tar.gz
emacs-4cb8696e4754d815efd5fd5e26f2b6b2567a11fe.zip
Don't misfontify "foo ()" inside C++ initialization parentheses as a type
Also recognize and handle function names introduced by "extern" inside a function. * lisp/progmodes/cc-engine.el (c-forward-decl-or-cast-1): Add a new element to the result list which is t when our declaration is, or is to be treated as, being at top level. * lisp/progmodes/cc-fonts.el (c-get-fontification-context): Detect being inside a C++ uniform initialization and return (not-decl nil) for this case. (c-font-lock-declarations): Use the new element 4 of the result of c-forward-decl-or-cast-1. * lisp/progmodes/cc-langs.el (c-make-top-level-kwds, c-make-top-level-key): New lang consts/vars.
-rw-r--r--lisp/progmodes/cc-engine.el25
-rw-r--r--lisp/progmodes/cc-fonts.el14
-rw-r--r--lisp/progmodes/cc-langs.el10
3 files changed, 40 insertions, 9 deletions
diff --git a/lisp/progmodes/cc-engine.el b/lisp/progmodes/cc-engine.el
index ab0204cb961..138a0e5da21 100644
--- a/lisp/progmodes/cc-engine.el
+++ b/lisp/progmodes/cc-engine.el
@@ -8167,9 +8167,9 @@ comment at the start of cc-engine.el for more info."
8167 ;; If a declaration is parsed: 8167 ;; If a declaration is parsed:
8168 ;; 8168 ;;
8169 ;; The point is left at the first token after the first complete 8169 ;; The point is left at the first token after the first complete
8170 ;; declarator, if there is one. The return value is a list of 4 elements, 8170 ;; declarator, if there is one. The return value is a list of 5 elements,
8171 ;; where the first is the position of the first token in the declarator. 8171 ;; where the first is the position of the first token in the declarator.
8172 ;; (See below for the other three.) 8172 ;; (See below for the other four.)
8173 ;; Some examples: 8173 ;; Some examples:
8174 ;; 8174 ;;
8175 ;; void foo (int a, char *b) stuff ... 8175 ;; void foo (int a, char *b) stuff ...
@@ -8210,7 +8210,9 @@ comment at the start of cc-engine.el for more info."
8210 ;; 8210 ;;
8211 ;; The third element of the return value is non-nil when the declaration 8211 ;; The third element of the return value is non-nil when the declaration
8212 ;; parsed might be an expression. The fourth element is the position of 8212 ;; parsed might be an expression. The fourth element is the position of
8213 ;; the start of the type identifier. 8213 ;; the start of the type identifier. The fifth element is t if either
8214 ;; CONTEXT was 'top, or the declaration is detected to be treated as top
8215 ;; level (e.g. with the keyword "extern").
8214 ;; 8216 ;;
8215 ;; If a cast is parsed: 8217 ;; If a cast is parsed:
8216 ;; 8218 ;;
@@ -8308,6 +8310,9 @@ comment at the start of cc-engine.el for more info."
8308 ;; Set when the symbol before `preceding-token-end' is known to 8310 ;; Set when the symbol before `preceding-token-end' is known to
8309 ;; terminate the previous construct, or when we're at point-min. 8311 ;; terminate the previous construct, or when we're at point-min.
8310 at-decl-start 8312 at-decl-start
8313 ;; Set when we have encountered a keyword (e.g. "extern") which
8314 ;; causes the following declaration to be treated as though top-level.
8315 make-top
8311 ;; Save `c-record-type-identifiers' and 8316 ;; Save `c-record-type-identifiers' and
8312 ;; `c-record-ref-identifiers' since ranges are recorded 8317 ;; `c-record-ref-identifiers' since ranges are recorded
8313 ;; speculatively and should be thrown away if it turns out 8318 ;; speculatively and should be thrown away if it turns out
@@ -8339,7 +8344,9 @@ comment at the start of cc-engine.el for more info."
8339 8344
8340 (cond 8345 (cond
8341 ;; Look for a specifier keyword clause. 8346 ;; Look for a specifier keyword clause.
8342 ((or (looking-at c-prefix-spec-kwds-re) 8347 ((or (and (looking-at c-make-top-level-key)
8348 (setq make-top t))
8349 (looking-at c-prefix-spec-kwds-re)
8343 (and (c-major-mode-is 'java-mode) 8350 (and (c-major-mode-is 'java-mode)
8344 (looking-at "@[A-Za-z0-9]+"))) 8351 (looking-at "@[A-Za-z0-9]+")))
8345 (save-match-data 8352 (save-match-data
@@ -8609,7 +8616,7 @@ comment at the start of cc-engine.el for more info."
8609 ;; construct here in C, since we want to recognize this as a 8616 ;; construct here in C, since we want to recognize this as a
8610 ;; typeless function declaration. 8617 ;; typeless function declaration.
8611 (not (and (c-major-mode-is 'c-mode) 8618 (not (and (c-major-mode-is 'c-mode)
8612 (eq context 'top) 8619 (or (eq context 'top) make-top)
8613 (eq (char-after) ?\))))) 8620 (eq (char-after) ?\)))))
8614 (if (eq (char-after) ?\)) 8621 (if (eq (char-after) ?\))
8615 (when (> paren-depth 0) 8622 (when (> paren-depth 0)
@@ -8657,7 +8664,7 @@ comment at the start of cc-engine.el for more info."
8657 ;; Recognize a top-level typeless 8664 ;; Recognize a top-level typeless
8658 ;; function declaration in C. 8665 ;; function declaration in C.
8659 (and (c-major-mode-is 'c-mode) 8666 (and (c-major-mode-is 'c-mode)
8660 (eq context 'top) 8667 (or (eq context 'top) make-top)
8661 (eq (char-after) ?\)))))))) 8668 (eq (char-after) ?\))))))))
8662 (setq pos (c-up-list-forward (point))) 8669 (setq pos (c-up-list-forward (point)))
8663 (eq (char-before pos) ?\))) 8670 (eq (char-before pos) ?\)))
@@ -8914,6 +8921,7 @@ comment at the start of cc-engine.el for more info."
8914 (when (and got-identifier 8921 (when (and got-identifier
8915 (looking-at c-after-suffixed-type-decl-key) 8922 (looking-at c-after-suffixed-type-decl-key)
8916 (or (eq context 'top) 8923 (or (eq context 'top)
8924 make-top
8917 (and (eq context nil) 8925 (and (eq context nil)
8918 (match-beginning 1))) 8926 (match-beginning 1)))
8919 (if (and got-parens 8927 (if (and got-parens
@@ -9080,7 +9088,7 @@ comment at the start of cc-engine.el for more info."
9080 ;; CASE 19 9088 ;; CASE 19
9081 (or (eq context 'decl) 9089 (or (eq context 'decl)
9082 (and (c-major-mode-is 'c-mode) 9090 (and (c-major-mode-is 'c-mode)
9083 (eq context 'top)))))) 9091 (or (eq context 'top) make-top))))))
9084 9092
9085 ;; The point is now after the type decl expression. 9093 ;; The point is now after the type decl expression.
9086 9094
@@ -9185,7 +9193,8 @@ comment at the start of cc-engine.el for more info."
9185 (and (or at-type-decl at-typedef) 9193 (and (or at-type-decl at-typedef)
9186 (cons at-type-decl at-typedef)) 9194 (cons at-type-decl at-typedef))
9187 maybe-expression 9195 maybe-expression
9188 type-start)) 9196 type-start
9197 (or (eq context 'top) make-top)))
9189 9198
9190 (t 9199 (t
9191 ;; False alarm. Restore the recorded ranges. 9200 ;; False alarm. Restore the recorded ranges.
diff --git a/lisp/progmodes/cc-fonts.el b/lisp/progmodes/cc-fonts.el
index d352e5b08c9..7b99c2f54e5 100644
--- a/lisp/progmodes/cc-fonts.el
+++ b/lisp/progmodes/cc-fonts.el
@@ -1251,6 +1251,17 @@ casts and declarations are fontified. Used on level 2 and higher."
1251 ;; Got a cached hit in some other type of arglist. 1251 ;; Got a cached hit in some other type of arglist.
1252 (type 1252 (type
1253 (cons 'arglist t)) 1253 (cons 'arglist t))
1254 ;; We're at a C++ uniform initialization.
1255 ((and (c-major-mode-is 'c++-mode)
1256 (eq (char-before match-pos) ?\()
1257 (save-excursion
1258 (goto-char match-pos)
1259 (and
1260 (zerop (c-backward-token-2 2))
1261 (looking-at c-identifier-start)
1262 (c-got-face-at (point)
1263 '(font-lock-variable-name-face)))))
1264 (cons 'not-decl nil))
1254 ((and not-front-decl 1265 ((and not-front-decl
1255 ;; The point is within the range of a previously 1266 ;; The point is within the range of a previously
1256 ;; encountered type decl expression, so the arglist 1267 ;; encountered type decl expression, so the arglist
@@ -1589,7 +1600,8 @@ casts and declarations are fontified. Used on level 2 and higher."
1589 (setq max-type-decl-end (point)))) 1600 (setq max-type-decl-end (point))))
1590 (goto-char start-pos) 1601 (goto-char start-pos)
1591 (c-font-lock-single-decl limit decl-or-cast match-pos 1602 (c-font-lock-single-decl limit decl-or-cast match-pos
1592 context toplev)) 1603 context
1604 (or toplev (nth 4 decl-or-cast))))
1593 1605
1594 (t t)))) 1606 (t t))))
1595 1607
diff --git a/lisp/progmodes/cc-langs.el b/lisp/progmodes/cc-langs.el
index 227b3e16485..869048bee31 100644
--- a/lisp/progmodes/cc-langs.el
+++ b/lisp/progmodes/cc-langs.el
@@ -2355,6 +2355,16 @@ construct it's part of continues."
2355 t nil 2355 t nil
2356 (c c++ objc) '("extern")) 2356 (c c++ objc) '("extern"))
2357 2357
2358(c-lang-defconst c-make-top-level-kwds
2359 "Keywords which make declarations they introduce be handled as top-level."
2360 t nil
2361 (c c++ objc) '("extern"))
2362
2363(c-lang-defconst c-make-top-level-key
2364 ;; A regexp which matches any `c-make-top-level-kwds' keyword.
2365 t (c-make-keywords-re t (c-lang-const c-make-top-level-kwds)))
2366(c-lang-defvar c-make-top-level-key (c-lang-const c-make-top-level-key))
2367
2358(c-lang-defconst c-type-list-kwds 2368(c-lang-defconst c-type-list-kwds
2359 "Keywords that may be followed by a comma separated list of type 2369 "Keywords that may be followed by a comma separated list of type
2360identifiers, where each optionally can be prefixed by keywords. (Can 2370identifiers, where each optionally can be prefixed by keywords. (Can