Subject: Re: bug#36678: 27.0.50; imenu not working in C++ (maybe because of namespace)

From: Alan Mackenzie <[email-masked]>
Date: Fri, 2 Aug 2019 19:33:15 +0000

Hello, Ergus.

On Wed, Jul 31, 2019 at 17:56:13 +0200, Ergus wrote:
> Hi Alan:

> Sorry for the long delay replying this.

No problem!

> I have been thinking on this for a while because this issue affects me
> constantly as I use most of the time C++ now.

I've actually spent some time on this.  What I now have is the ability of
imenu to find functions, even when they are not at column zero, and to
record the enclosing namespace/class/etc. of these functions.  On M-x
imenu <tab>, which lists all found functions (more or less), the
namespace/class bit only gets displayed as needed to distinguish from
other functions of the same name.

The above is a bit of an exaggeration: it is not yet clean or robust
code, and hasn't been tested much, but does sort of work on the files
I've tried it on.

What is still missing is a scanning of a function's parameters, needed to
distinguish, say, a constructor with two parameters from the same with
none.  This is still looking a bit tricky.

> Maybe this is a stupid suggestion, but I was thinking that if the issue
> is performance (for ii) we could add a special function to implement the
> expensive part for the functional search in C and use the C regexps.

At the moment, the scanning of these C++ files is taking a small number
of seconds on my (pretty fast) Ryzen machine.  This is slow for imenu.

I can't honestly see how we might use a C function to speed all this up.
Currently, my new code is just using (c-beginning-of-defun -1) to find
the next function, thus avoiding the need to write reams of syntactical
analysis code, but not being fast.

> Maybe it could be restrictive somehow, but if this solves a problem,
> could be good enough and important for C++-mode. Because namespace use
> is becoming more and more extended in C++ with the new standards. And
> the most common indentations are the 

> Probably:

> 1) For example it could do a first filtration for the buffer in a C
> function using C regexp (with fake positives) and then filter again in
> the Lisp side?

> 2) Or, we could just iterate the file by lines in C and then modify the
> regex for the next line conditionally (which is cheaper than in
> Lisp). This could create a list in C that we can then use in the Lisp
> side more efficiently.

All this might be workable, I just don't know.  But it would probably
make sense to write it in Lisp first, and only then optimise it with C if
it really is too slow.

> In case we figure out a nice method for that. Does it makes sense?

[ .... ]

-- 
Alan Mackenzie (Nuremberg, Germany).