Is there a way to make gcc print offending lines when it emits an error?

I have a large code base on which I was instructed to port up to 64 bits. The code compiles, but it prints a very large number of incompatible pointer warnings (as expected.) Is there a way I can have gcc print the line on which the error occurs? Right now I'm just using gcc error messages to try and keep track of the assumptions that need to be changed, and having to find each one is not fun.

+3
source share
6 answers

I frankly stole Joseph Quincy's answer for this. The only difference is that I tried to make the code more understandable:

For bash, use make 2>&1 | show_gcc_linewith the show_gcc_linefollowing script:

#!/bin/bash
#  Read and echo each line only if it is an error or warning message
#  The lines printed will start something like "foobar:123:" so that
#  line 123 of file foobar will be printed.

while read input
do
    loc=$(echo "$input" | sed -n 's/^\([^ :]*\):\([0-9]*\):.*/\1 \2/p')
    len=${#loc}
    file=${loc% *}
    line=${loc#* }

    if [ $len -gt  0 ]
    then
        echo "$input"
        echo "$(sed -n ${line}p $file)"
        echo
    fi
done

This was partly because I did not like the formatting of the original. It only prints warnings / errors, followed by a line of code causing the problem, followed by an empty line. I also deleted the hyphen line.

+2
source

You may need a script to print the desired lines. If you are using csh (unlikely!), Use:

  make ... |& show_gcc_line

with the show_gcc_linefollowing script:

#!/bin/csh
# Read and echo each line. And, if it starts with "foobar:123:", print line 123
# of foobar, using find(1) to find it, prefaced by ---------------.

set input="$<"
while ( "$input" ) 
    echo "$input"
    set loc=`echo "$input" | sed -n 's/^\([^ :]*\):\([0-9]*\):.*/\1 \2/p'`
    if ( $#loc ) then
        find . -name $loc[1] | xargs sed -n $loc[2]s/^/---------------/p
    endif
    set input="$<"
end

And for bash use make ... 2>&1 | show_gcc_linewith:

#!/bin/bash
#  Read and echo each line. And, if it starts with "foobar:123:", print line 123
#  of foobar, using find(1) to find it, prefaced by ---------------.

while read input
do
    echo "$input"
    loc=$(echo "$input" | sed -n 's/^\([^ :]*\):\([0-9]*\):.*/\1 \2/p')
    if [ ${#loc} -gt  0 ] 
    then
        find . -name ${loc% *} | xargs sed -n ${loc#* }s/^/---------------/p
    fi
done
+2
source

-W , . .

:

gcc ... 1>/dev/nul
+1

, , ( C) - ​​ , , ... gcc , . , . (, , ).

0

This little script should work, but I can't check it right now. Sorry if you need to change.

LAST_ERROR_LINE=`(gcc ... 2>&1 >/dev/null ) | grep error | tail -n1`
FILE=`echo $LAST_ERROR_LINE | cut -f1 -d':'`
LINE=`echo $LAST_ERROR_LINE | cut -f2 -d':'`
sed -n "${LINE}p" $FILE
0
source

It’s hard for me to remember the error message redirection symbol.

So here is my version for printing gcc error messages:

$ee make

for error messages and warnings:

ee2 make

How to: add them to .bashrc

function ee() {
   $*  2>&1  | grep error
}

function ee2() {
    $* 2> ha
    echo "-----"
    echo "Error"
    echo "-----"
    grep error ha
    echo "-------"
    echo "Warning"
    echo "-------"
    grep warning ha
}
0
source

Source: https://habr.com/ru/post/1748573/


All Articles