git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: git grep --show-function treats GOTO labels as function names

From
Johannes Sixt <j6t@kdbg.org>
Date
May 28, 2020, 19:01 UTC
Message-ID
<07f56d7a-45cc-2d28-ec48-51b95015550e@kdbg.org>
In-Reply-To
<20200527231628.GC546534@coredump.intra.peff.net>
Am 28.05.20 um 01:16 schrieb Jeff King:
Show 29 quoted lines
> 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:

C and C++ have a very versatile syntax and it turned out to be virtually impossible to capture actual function definitions with a regex. See 8a2e8da367f7 ("userdiff: have 'cpp' hunk header pattern catch more C++ anchor points", 2014-03-21) for details.

With the current pattern, we catch probably 95% of the coding patterns and coding styles. The style under discussion falls into the remaining 5% because continuation lines are not indented.

-- Hannes
Previous: Jeff King
Message 7 of 7 in “git grep --show-function treats GOTO labels as function names”
  1. Zach RiggleMay 27, 2020
  2. Jeff KingMay 27, 2020
  3. Zach RiggleMay 27, 2020
  4. Zach RiggleMay 27, 2020
  5. Zach RiggleMay 27, 2020
  6. Jeff KingMay 27, 2020
  7. Johannes SixtMay 28, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.