* C99 compound literals in c-mode
@ 2017-08-15 8:04 Nikolai Weibull
2017-08-15 9:03 ` Stefan Monnier
0 siblings, 1 reply; 10+ messages in thread
From: Nikolai Weibull @ 2017-08-15 8:04 UTC (permalink / raw)
To: Emacs Developers
[-- Attachment #1: Type: text/plain, Size: 215 bytes --]
Hi!
C-mode doesn’t seem to understand C99’s compound literals, resulting in
rather broken indentation. Is this correct and, if so, how difficult would
it be to add support for it?
Thanks!
Nikolai
[-- Attachment #2: Type: text/html, Size: 285 bytes --]
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: C99 compound literals in c-mode
2017-08-15 8:04 C99 compound literals in c-mode Nikolai Weibull
@ 2017-08-15 9:03 ` Stefan Monnier
2017-08-15 9:56 ` Nikolai Weibull
` (2 more replies)
0 siblings, 3 replies; 10+ messages in thread
From: Stefan Monnier @ 2017-08-15 9:03 UTC (permalink / raw)
To: emacs-devel
> C-mode doesn’t seem to understand C99’s compound literals, resulting in
> rather broken indentation. Is this correct and, if so, how difficult would
> it be to add support for it?
Could you show some example problematic code?
Stefan
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: C99 compound literals in c-mode
2017-08-15 9:03 ` Stefan Monnier
@ 2017-08-15 9:56 ` Nikolai Weibull
2017-08-15 9:57 ` Helmut Eller
2017-08-15 9:57 ` Nikolai Weibull
2 siblings, 0 replies; 10+ messages in thread
From: Nikolai Weibull @ 2017-08-15 9:56 UTC (permalink / raw)
To: Stefan Monnier; +Cc: Emacs Developers
On Tue, Aug 15, 2017 at 11:03 AM, Stefan Monnier
<monnier@iro.umontreal.ca> wrote:
>
> > C-mode doesn’t seem to understand C99’s compound literals, resulting in
> > rather broken indentation. Is this correct and, if so, how difficult would
> > it be to add support for it?
>
> Could you show some example problematic code?
Yes:
I use the following
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: C99 compound literals in c-mode
2017-08-15 9:03 ` Stefan Monnier
2017-08-15 9:56 ` Nikolai Weibull
@ 2017-08-15 9:57 ` Helmut Eller
2017-08-15 9:57 ` Nikolai Weibull
2 siblings, 0 replies; 10+ messages in thread
From: Helmut Eller @ 2017-08-15 9:57 UTC (permalink / raw)
To: emacs-devel
On Tue, Aug 15 2017, Stefan Monnier wrote:
>> C-mode doesn’t seem to understand C99’s compound literals, resulting in
>> rather broken indentation. Is this correct and, if so, how difficult would
>> it be to add support for it?
>
> Could you show some example problematic code?
I think code like this:
int foo (void)
{
return (struct bar) { .f1 = 10,
.f2 = 20,
};
}
should be indented better. Maybe so:
return (struct bar) { .f1 = 10,
.f2 = 20,
};
There are also strange interactions with declarations, eg. adding a
a declaration before the return changes the indentation to:
int foo (void)
{
int i = 1;
return (struct bar) { .f1 = 10,
.f2 = 20,
};
}
Helmut
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: C99 compound literals in c-mode
2017-08-15 9:03 ` Stefan Monnier
2017-08-15 9:56 ` Nikolai Weibull
2017-08-15 9:57 ` Helmut Eller
@ 2017-08-15 9:57 ` Nikolai Weibull
2017-08-16 17:26 ` Alan Mackenzie
2017-08-20 20:40 ` Alan Mackenzie
2 siblings, 2 replies; 10+ messages in thread
From: Nikolai Weibull @ 2017-08-15 9:57 UTC (permalink / raw)
To: Stefan Monnier; +Cc: Emacs Developers
On Tue, Aug 15, 2017 at 11:03 AM, Stefan Monnier
<monnier@iro.umontreal.ca> wrote:
>> C-mode doesn’t seem to understand C99’s compound literals, resulting in
>> rather broken indentation. Is this correct and, if so, how difficult would
>> it be to add support for it?
>
> Could you show some example problematic code?
Yes:
struct a {
int b;
};
int
main(void)
{
return (struct a){
0
}.b;
}
I use the following style:
(c-add-style "now-c-style"
'("linux"
(c-hanging-braces-alist . ((brace-entry-open)
(brace-list-open after)
(brace-list-close before)
(class-close before)
(class-open after)
(substatement-open after)
(block-close before)))
(c-hanging-colons-alist . ((case-label after)
(label after)))))
Nikolai
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: C99 compound literals in c-mode
2017-08-15 9:57 ` Nikolai Weibull
@ 2017-08-16 17:26 ` Alan Mackenzie
2017-08-16 17:52 ` Nikolai Weibull
2017-08-20 20:40 ` Alan Mackenzie
1 sibling, 1 reply; 10+ messages in thread
From: Alan Mackenzie @ 2017-08-16 17:26 UTC (permalink / raw)
To: Nikolai Weibull; +Cc: Stefan Monnier, Emacs Developers
Hello, Nikolai.
On Tue, Aug 15, 2017 at 11:57:25 +0200, Nikolai Weibull wrote:
> On Tue, Aug 15, 2017 at 11:03 AM, Stefan Monnier
> <monnier@iro.umontreal.ca> wrote:
> >> C-mode doesn’t seem to understand C99’s compound literals, resulting in
> >> rather broken indentation. Is this correct and, if so, how difficult would
> >> it be to add support for it?
> >
> > Could you show some example problematic code?
> Yes:
> struct a {
> int b;
> };
> int
> main(void)
> {
> return (struct a){
> 0
> }.b;
> }
I'll take a look at it. It shouldn't be too difficult.
> I use the following style:
> (c-add-style "now-c-style"
> '("linux"
> (c-hanging-braces-alist . ((brace-entry-open)
> (brace-list-open after)
> (brace-list-close before)
> (class-close before)
> (class-open after)
> (substatement-open after)
> (block-close before)))
> (c-hanging-colons-alist . ((case-label after)
> (label after)))))
> Nikolai
--
Alan Mackenzie (Nuremberg, Germany).
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: C99 compound literals in c-mode
2017-08-16 17:26 ` Alan Mackenzie
@ 2017-08-16 17:52 ` Nikolai Weibull
0 siblings, 0 replies; 10+ messages in thread
From: Nikolai Weibull @ 2017-08-16 17:52 UTC (permalink / raw)
To: Alan Mackenzie; +Cc: Nikolai Weibull, Stefan Monnier, Emacs Developers
On Wed, Aug 16, 2017 at 7:26 PM, Alan Mackenzie <acm@muc.de> wrote:
> Hello, Nikolai.
>
> On Tue, Aug 15, 2017 at 11:57:25 +0200, Nikolai Weibull wrote:
>> On Tue, Aug 15, 2017 at 11:03 AM, Stefan Monnier
>> <monnier@iro.umontreal.ca> wrote:
>> >> C-mode doesn’t seem to understand C99’s compound literals, resulting in
>> >> rather broken indentation. Is this correct and, if so, how difficult would
>> >> it be to add support for it?
>> >
>> > Could you show some example problematic code?
>
>> Yes:
>
>> struct a {
>> int b;
>> };
>
>> int
>> main(void)
>> {
>> return (struct a){
>> 0
>> }.b;
>> }
>
> I'll take a look at it. It shouldn't be too difficult.
Thank you, I really appreciate it. (You may contact me on or off list
if you’d like more examples or other input.)
Nikolai
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: C99 compound literals in c-mode
2017-08-15 9:57 ` Nikolai Weibull
2017-08-16 17:26 ` Alan Mackenzie
@ 2017-08-20 20:40 ` Alan Mackenzie
2017-08-23 10:01 ` Nikolai Weibull
1 sibling, 1 reply; 10+ messages in thread
From: Alan Mackenzie @ 2017-08-20 20:40 UTC (permalink / raw)
To: Nikolai Weibull; +Cc: Stefan Monnier, Emacs Developers
Hello, Nikolai.
On Tue, Aug 15, 2017 at 11:57:25 +0200, Nikolai Weibull wrote:
> On Tue, Aug 15, 2017 at 11:03 AM, Stefan Monnier
> <monnier@iro.umontreal.ca> wrote:
> >> C-mode doesn’t seem to understand C99’s compound literals, resulting in
> >> rather broken indentation. Is this correct and, if so, how difficult would
> >> it be to add support for it?
> > Could you show some example problematic code?
> Yes:
> struct a {
> int b;
> };
This seems to be correctly analysed and indented by CC Mode. You can
see this by doing C-c C-s on any line to get the syntactic analysis. On
the middle line, this shows ((inclass 332) (topmost-intro 332)) (where
the "332" may vary, depending on the position in the file). The
"inclass" bit causes an indentation of c-basic-offset (here 8) columns.
What indentation do you want here?
> int
> main(void)
> {
> return (struct a){
> 0
> }.b;
> }
Here, the "0" line is being wrongly analysed as a statement-block-intro,
when it should be a brace-list-intro. The problem is that brace lists
are recognised only by their context in the source code, rather than
their internal structure. When a brace list can appear virtually
anywhere, this doesn't make sense.
The following patch causes brace lists to be recognised by their
internal structure too. Would you please apply it to CC Mode (in
directory .../lisp/progmodes), try it out, and let me know how well it
solves the problems with compound literals.
diff -r 9533dc4cbda3 cc-engine.el
--- a/cc-engine.el Thu Jul 27 17:37:02 2017 +0000
+++ b/cc-engine.el Sun Aug 20 20:00:49 2017 +0000
@@ -10658,26 +10658,35 @@
(defun c-looking-at-statement-block ()
;; Point is at an opening brace. If this is a statement block (i.e. the
- ;; elements in it are terminated by semicolons) return t. Otherwise, return
- ;; nil.
+ ;; elements in the block are terminated by semicolons, or the block is
+ ;; empty, or the block contains a keyword) return t. Otherwise, return nil.
(let ((here (point)))
(prog1
(if (c-go-list-forward)
(let ((there (point)))
(backward-char)
- (c-syntactic-skip-backward
- "^;," here t)
+ (c-syntactic-skip-backward "^;," here t)
(cond
((eq (char-before) ?\;) t)
((eq (char-before) ?,) nil)
- (t (goto-char here)
- (forward-char)
- (and (c-syntactic-re-search-forward "{" there t t)
- (progn (backward-char)
- (c-looking-at-statement-block))))))
+ (t ; We're at (1+ here).
+ (cond
+ ((progn (c-forward-syntactic-ws)
+ (eq (point) (1- there)))
+ t)
+ ((c-syntactic-re-search-forward c-keywords-regexp there t)
+ t)
+ ((c-syntactic-re-search-forward "{" there t t)
+ (backward-char)
+ (c-looking-at-statement-block))
+ (t nil)))))
(forward-char)
- (and (c-syntactic-re-search-forward "[;,]" nil t t)
- (eq (char-before) ?\;)))
+ (cond
+ ((c-syntactic-re-search-forward "[;,]" nil t t)
+ (eq (char-before) ?\;))
+ ((c-syntactic-re-search-forward c-keywords-regexp nil t t)
+ t)
+ (t nil)))
(goto-char here))))
(defun c-looking-at-inexpr-block (lim containing-sexp &optional check-at-end)
@@ -12467,7 +12476,11 @@
(save-excursion
(goto-char containing-sexp)
(c-looking-at-special-brace-list)))
- (c-inside-bracelist-p containing-sexp paren-state))))
+ (c-inside-bracelist-p containing-sexp paren-state)
+ (save-excursion
+ (goto-char containing-sexp)
+ (and (eq (char-after) ?{)
+ (not (c-looking-at-statement-block)))))))
(cond
;; CASE 9A: In the middle of a special brace list opener.
[ .... ]
> Nikolai
--
Alan Mackenzie (Nuremberg, Germany).
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: C99 compound literals in c-mode
2017-08-20 20:40 ` Alan Mackenzie
@ 2017-08-23 10:01 ` Nikolai Weibull
2017-08-24 16:37 ` Alan Mackenzie
0 siblings, 1 reply; 10+ messages in thread
From: Nikolai Weibull @ 2017-08-23 10:01 UTC (permalink / raw)
To: Alan Mackenzie; +Cc: Nikolai Weibull, Stefan Monnier, Emacs Developers
On Sun, Aug 20, 2017 at 10:40 PM, Alan Mackenzie <acm@muc.de> wrote:
> Hello, Nikolai.
>
> On Tue, Aug 15, 2017 at 11:57:25 +0200, Nikolai Weibull wrote:
>> On Tue, Aug 15, 2017 at 11:03 AM, Stefan Monnier
>> <monnier@iro.umontreal.ca> wrote:
>> >> C-mode doesn’t seem to understand C99’s compound literals, resulting in
>> >> rather broken indentation. Is this correct and, if so, how difficult would
>> >> it be to add support for it?
>
>> > Could you show some example problematic code?
>
>> Yes:
>
>> struct a {
>> int b;
>> };
>
> This seems to be correctly analysed and indented by CC Mode. You can
> see this by doing C-c C-s on any line to get the syntactic analysis. On
> the middle line, this shows ((inclass 332) (topmost-intro 332)) (where
> the "332" may vary, depending on the position in the file). The
> "inclass" bit causes an indentation of c-basic-offset (here 8) columns.
>
> What indentation do you want here?
Sorry, this was just there to give a complete example, it indents
correctly as is.
>> int
>> main(void)
>> {
>> return (struct a){
>> 0
>> }.b;
>> }
> Here, the "0" line is being wrongly analysed as a statement-block-intro,
> when it should be a brace-list-intro. The problem is that brace lists
> are recognised only by their context in the source code, rather than
> their internal structure. When a brace list can appear virtually
> anywhere, this doesn't make sense.
>
> The following patch causes brace lists to be recognised by their
> internal structure too. Would you please apply it to CC Mode (in
> directory .../lisp/progmodes), try it out, and let me know how well it
> solves the problems with compound literals.
This seems to be a patch for a newer version of CC Mode than that
installed with my Emacs (25.2.1 via MacPorts). The function
c-looking-at-statement-block isn’t even defined.
Nikolai
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: C99 compound literals in c-mode
2017-08-23 10:01 ` Nikolai Weibull
@ 2017-08-24 16:37 ` Alan Mackenzie
0 siblings, 0 replies; 10+ messages in thread
From: Alan Mackenzie @ 2017-08-24 16:37 UTC (permalink / raw)
To: Nikolai Weibull; +Cc: Emacs Developers
Hello, Nikolai.
On Wed, Aug 23, 2017 at 12:01:34 +0200, Nikolai Weibull wrote:
> On Sun, Aug 20, 2017 at 10:40 PM, Alan Mackenzie <acm@muc.de> wrote:
> > Hello, Nikolai.
> > On Tue, Aug 15, 2017 at 11:57:25 +0200, Nikolai Weibull wrote:
> >> struct a {
> >> int b;
> >> };
> > This seems to be correctly analysed and indented by CC Mode. You can
> > see this by doing C-c C-s on any line to get the syntactic analysis.
> > On the middle line, this shows ((inclass 332) (topmost-intro 332))
> > (where the "332" may vary, depending on the position in the file).
> > The "inclass" bit causes an indentation of c-basic-offset (here 8)
> > columns.
> > What indentation do you want here?
> Sorry, this was just there to give a complete example, it indents
> correctly as is.
OK.
> >> int
> >> main(void)
> >> {
> >> return (struct a){
> >> 0
> >> }.b;
> >> }
> > Here, the "0" line is being wrongly analysed as a
> > statement-block-intro, when it should be a brace-list-intro. The
> > problem is that brace lists are recognised only by their context in
> > the source code, rather than their internal structure. When a brace
> > list can appear virtually anywhere, this doesn't make sense.
> > The following patch causes brace lists to be recognised by their
> > internal structure too. Would you please apply it to CC Mode (in
> > directory .../lisp/progmodes), try it out, and let me know how well it
> > solves the problems with compound literals.
> This seems to be a patch for a newer version of CC Mode than that
> installed with my Emacs (25.2.1 via MacPorts). The function
> c-looking-at-statement-block isn’t even defined.
Apologies, I didn't read your original post properly. There have indeed
been quite a few changes to CC Mode since the code freeze for Emacs 25.2.
Would it be OK if I sent you a tarball (format .tar.gz) of the up to date
CC Mode sources (including this patch)? The total size is around 440 kB.
> Nikolai
--
Alan Mackenzie (Nuremberg, Germany).
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2017-08-24 16:37 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-08-15 8:04 C99 compound literals in c-mode Nikolai Weibull
2017-08-15 9:03 ` Stefan Monnier
2017-08-15 9:56 ` Nikolai Weibull
2017-08-15 9:57 ` Helmut Eller
2017-08-15 9:57 ` Nikolai Weibull
2017-08-16 17:26 ` Alan Mackenzie
2017-08-16 17:52 ` Nikolai Weibull
2017-08-20 20:40 ` Alan Mackenzie
2017-08-23 10:01 ` Nikolai Weibull
2017-08-24 16:37 ` Alan Mackenzie
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).