emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

cc-mode uniform initialization support


From: Daniel Colascione
Subject: cc-mode uniform initialization support
Date: Wed, 19 Aug 2015 11:36:11 -0700
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.8.0

Recent versions of C++ support using {} to indicate an initialization
value. For example,

  1 int foo{5};
  2 int bar = {5};
  3 mumble_t mumble{
  4  foo,
  5    bar
  6 };
  7 return {mumble, foo}; // Type inferred from function declaration

Working with code written in this style is a nightmare right now because
cc-mode recognizes line 4 as defun-block-intro, leading to line 5 being
indented spuriously, since cc-mode thinks it's a statement continuation.
In reality, the construct starting at line 3 is a brace list, but
there's no syntactic clue to tell us that. (We can, of course, look for
an "=", but an "=" is no longer required for a brace list.)

I don't see a way around needing to use awful heuristics to distinguish
the cases. Does anyone have a better idea? Keep in mind that code like
this is common too:

  foo({
    abc,
    def
  });

And plenty of people define macros that accept blocks:

  MY_FOREACH {
    statement1;
    statement2;
  }

diff --git a/lisp/progmodes/cc-engine.el b/lisp/progmodes/cc-engine.el
index 06b03a2..84f3ad7 100644
--- a/lisp/progmodes/cc-engine.el
+++ b/lisp/progmodes/cc-engine.el
@@ -8725,6 +8725,34 @@ comment at the start of cc-engine.el for more info."
    (save-excursion
      (goto-char containing-sexp)
      (c-backward-over-enum-header))
+   ;; Detect C++11 uniform initialization.  This bit is necessarily
+   ;; heuristic, since there are few syntactic clues to go
+   ;; on. Initializer lists can now stand in for any value at all when
+   ;; the compiler can infer the type to build from the
+   ;; initializer list.
+   (save-excursion
+     (goto-char containing-sexp)
+     (and
+      (eq (char-after) ?\{)
+      (progn (c-backward-sws)
+            (or
+             ;; After a comma means universal initialization
+             (eq (char-before) ?\,)
+             ;; Er, we use GCC statement expressions most often in
+             ;; macros, so if we see ({ outside of one, think of it
+             ;; as uniform initialization.
+             (and (eq (char-before) ?\()
+                  (save-excursion
+                    (not (c-beginning-of-macro))))
+             ;; foo{} versus foo {. Yuck.
+             (and (not (bobp))
+                  (prog1
+                      (backward-char)
+                    (looking-at "\\(\\w\\|\\s_\\){")
+                    (forward-char)))
+             ;; Special case for return {...}
+             (looking-back "\\_<return\\=" (point-at-bol))))
+      containing-sexp))
    ;; this will pick up array/aggregate init lists, even if they are
nested.
    (save-excursion
      (let ((class-key

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]