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
Zach Riggle <zachriggle@gmail.com>
Date
May 27, 2020, 23:05 UTC
Message-ID
<CAMP9c5nROh5DxbfwgKL3vNWsr=RYYgkEHCeaEMVN3gq1xkPkLA@mail.gmail.com>
In-Reply-To
<CAMP9c5m65hBXKgP76iUCGe79c_s5p106K6iwzJyPmm7fCsc7LA@mail.gmail.com>

I made a mistake copy-pasting; things work correctly when the attribute is on the same line, but not when it is split:

$ cat attr.cpp
int main()
__attribute__ ( (no_sanitize("alignment")) )
{
    FOO
}
Zach Riggle
On Wed, May 27, 2020 at 6:04 PM Zach Riggle <zachriggle@gmail.com> wrote:
Show 50 quoted lines
>
> 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
>
> Zach Riggle
>
> On Wed, May 27, 2020 at 5:54 PM Zach Riggle <zachriggle@gmail.com> wrote:
> >
> > Awesome, thanks!
> >
> >
> > Zach Riggle
> >
> > On Wed, May 27, 2020 at 5:48 PM Jeff King <peff@peff.net> wrote:
> > >
> > > On Wed, May 27, 2020 at 05:29:08PM -0500, Zach Riggle wrote:
> > >
> > > > It looks like there is an issue with how the parser handles "goto"
> > > > labels, as it treats them the same as a function name.
> > >
> > > By default, the function-finding isn't aware of the specific content in
> > > the file. But you can associate extensions with particular types, like:
> > >
> > >   $ echo '*.cpp diff=cpp' >~/.gitattributes
> > >   $ git config --global core.attributesFile ~/.gitattributes
> > >   $ git grep --no-index --show-function -e FOO test2.cpp
> > >   test2.cpp=int main() {
> > >   test2.cpp:    FOO
> > >   test2.cpp:    FOO
> > >
> > > Usually this is done in-repo, but since your example used --no-index, I
> > > showed how to set up a per-user attribute file. The "diff" attribute
> > > covers both diff and grep (for diff, the hunk headers will also show the
> > > function).
> > >
> > > The "cpp" diff regexes are built-in to the git binary. We just don't
> > > associate any filenames by default. You can also add your own; see the
> > > section "Defining a custom hunk-header" from "git help attributes".
> > >
> > > -Peff
Previous: Zach RiggleNext: Jeff King
Message 5 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.