git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Jeff King <peff@peff.net>
To: Zach Riggle <zachriggle@gmail.com>
Cc: git@vger.kernel.org
Subject: Re: git grep --show-function treats GOTO labels as function names
Date: Wed, 27 May 2020 19:16:28 -0400	[thread overview]
Message-ID: <20200527231628.GC546534@coredump.intra.peff.net> (raw)
In-Reply-To: <CAMP9c5m65hBXKgP76iUCGe79c_s5p106K6iwzJyPmm7fCsc7LA@mail.gmail.com>

On Wed, May 27, 2020 at 06:04:21PM -0500, Zach Riggle wrote:

> It looks like that does the trick for "goto" labels, but there are
> also some issue on function name parsing with attributes when they are
> split onto a second line.
> 
> $ cat attr.cpp
> int main() __attribute__ ( (no_sanitize("alignment")) )
> {
>     FOO
> }
> $ git grep --no-index --show-function -e FOO attr.cpp
> attr.cpp=2=__attribute__ ( (no_sanitize("alignment")) )
> attr.cpp:4:    FOO

From your output, I assume the problematic input actually splits the
attribute onto the second line?

I agree that's not ideal. The baked-in regex we use for matching C
function lines is:

  $ git grep -nA4 cpp userdiff.c
  userdiff.c:173:PATTERNS("cpp",
  userdiff.c-174-  /* Jump targets or access declarations */
  userdiff.c-175-  "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n"
  userdiff.c-176-  /* functions/methods, variables, and compounds at top level */
  userdiff.c-177-  "^((::[[:space:]]*)?[A-Za-z_].*)$",

so we mistake it for a function name. I'm not sure how easy it is to do
better, though. We can add a line like:

diff --git a/userdiff.c b/userdiff.c
index 1df884ef0b..de8e1a3d72 100644
--- a/userdiff.c
+++ b/userdiff.c
@@ -173,6 +173,8 @@ PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
 PATTERNS("cpp",
 	 /* Jump targets or access declarations */
 	 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n"
+	 /* skip over attribute declarations are on their own lines */
+	 "!((::[[:space:]]*))?__attribute__\n"
 	 /* functions/methods, variables, and compounds at top level */
 	 "^((::[[:space:]]*)?[A-Za-z_].*)$",
 	 /* -- */

which works for your case, but would regress:

  __attribute__((whatever) int main()
  {
  FOO
  }

Handling both means skipping past the attribute, not counting it as a
function, and then checking for a plausible function afterwards. That's
a much trickier regex. But if you come up with something that works, I
think we'd be happy to take a patch. :)

You can also just override this regex via config for your personal use.
If you know you'd never use that style, then the problem becomes much
easier.

-Peff

  parent reply	other threads:[~2020-05-27 23:16 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-27 22:29 git grep --show-function treats GOTO labels as function names Zach Riggle
2020-05-27 22:48 ` Jeff King
2020-05-27 22:54   ` Zach Riggle
2020-05-27 23:04     ` Zach Riggle
2020-05-27 23:05       ` Zach Riggle
2020-05-27 23:16       ` Jeff King [this message]
2020-05-28 19:01         ` Johannes Sixt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200527231628.GC546534@coredump.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=zachriggle@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.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).