Subject: Re: bug#48670: 27.2; cc-mode and constexpr if

From: Alan Mackenzie <[email-masked]>
Date: Sun, 30 May 2021 16:29:17 +0000

Hello, John.

On Wed, May 26, 2021 at 05:08:03 +0200, John Eivind Helset wrote:

> cc-mode has problems with constexpr-if-statements whose true-statement
> is not a compound statement.

> >From 'emacs -Q' open a buffer in cc-mode and compare the formatting of:

> void f(){
>   if(a == 4)
>     return 4;
>   else if(a == 3)
>     return 3;
>   else
>     return 2;
> }

> void f_constexpr(){
>   if constexpr(a == 4)
>                 return 4;
>   else if constexpr(a == 3)
>          return 3;
>   else
>     return 2;
> }

Thanks for taking the trouble to report this bug, and thanks even more
for creating a minimal, easy to work with, test case.

The bug has in fact already been fixed in the development version of
Emacs, and should be getting released with Emacs 28.1, whenever that
will happen.

In the mean time, please apply the following patch to
..../emacs/lisp/progmodes/cc-engine.el, and then byte compile that file.
Should you want any help with the patching or byte compilation, feel
free to send me private email.

--- cc-engine.el.orig	2021-05-30 16:08:48.855885223 +0000
+++ cc-engine.el	2021-05-30 16:16:47.785903083 +0000
@@ -1181,6 +1181,15 @@
 			  ;; suitable error.
 			  (setq pre-stmt-found t)
 			  (throw 'loop nil))
+			;; Handle C++'s `constexpr', etc.
+			(if (save-excursion
+			      (and (looking-at c-block-stmt-hangon-key)
+				   (progn
+				     (c-backward-syntactic-ws lim)
+				     (c-safe (c-backward-sexp) t))
+				   (looking-at c-block-stmt-2-key)
+				   (setq pos (point))))
+			    (goto-char pos))
 			(cond
 			 ;; Have we moved into a macro?
 			 ((and (not macro-start)

> In GNU Emacs 27.2 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.27, cairo version 1.17.4)
>  of 2021-03-26 built on juergen
> Windowing system distributor 'The X.Org Foundation', version 11.0.12011000
> System Description: Arch Linux

[ .... ]

> -- 
> John Eivind Helset

-- 
Alan Mackenzie (Nuremberg, Germany).