Analysis
我正在使用其他人编写的一些代码(准确地说,调试有关 SO 的另一个问题),并且我抑制了-Werror
获取警告的选项:
$ gcc -O3 -g -std=c11 -Wall -Wextra stl.c -o stl
stl.c: In function ‘printStudent’:
stl.c:84:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘List’ [-Wformat=]
printf("Name: %s ", student->first);
^
stl.c:84:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘List’ [-Wformat=]
stl.c:85:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
printf("Grade: %s\n", student->grade);
^
stl.c:85:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
$
并启用-Werror
获取错误消息:
$ gcc -O3 -g -std=c11 -Wall -Wextra -Werror stl.c -o stl
stl.c: In function ‘printStudent’:
stl.c:84:5: error: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘List’ [-Werror=format=]
printf("Name: %s ", student->first);
^
stl.c:84:5: error: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘List’ [-Werror=format=]
stl.c:85:5: error: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Werror=format=]
printf("Grade: %s\n", student->grade);
^
stl.c:85:5: error: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Werror=format=]
cc1: all warnings being treated as errors
$
您会收到一些“上下文消息”,例如In function
消息,但每个警告都以<source file>:<line>:<col>: warning:
,每个错误都以包含以下内容的行开头error
代替warning
。它继续到下一条上下文消息,或下一条警告或错误消息,或最后的摘要行(“所有警告都被视为错误”)。如果您有额外的信息正在生成(-H
, or -v
选项),那么生活就更复杂了,但是对于常规编译来说,这已经涵盖了。
在更广泛的 C++ 示例中,您有一条上下文消息和两个错误,并有多个注释来解释为什么会产生第一个错误。
合成
因此,可以使用合适的标记来分割消息。事实上,用一个简单的方法就可以很容易地完成sed
script:
#!/bin/sh
#
# Insert markers before GCC warning and error messages
sed -e '
/^.*:[1-9][0-9]*:[1-9][0-9]*: warning: /i\
\
-- WARNING --\
\
/^.*:[1-9][0-9]*:[1-9][0-9]*: error: /i\
\
-- ERROR --\
\
' "$@"
该脚本在诊断输出中发现了第二个错误,而我在第一眼浏览时就错过了该错误。
Example
这是您的示例和我的示例的一些复合输出(加上未删除的编译命令行,其他):
/usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/src/DataItemCollection.cpp: In member function ‘virtual bool xmodel::Core::DataItemCollection::tryAdditionalItemRegistration(std::shared_ptr<xmodel::Core::IDataItem>) const’:
-- ERROR --
/usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/src/DataItemCollection.cpp:414:66: error: no matching function for call to ‘bind(<unresolved overloaded function type>, const std::_Placeholder<1>&, const QUuid&)’
, std::bind(&IDataItemCollectionScope::findItemById, _1, id)
^
/usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/src/DataItemCollection.cpp:414:66: note: candidates are:
In file included from /gl-sync/compilers/gcc/4.8.2/sle11sp0/include/c++/4.8.2/bits/stl_algo.h:66:0,
from /gl-sync/compilers/gcc/4.8.2/sle11sp0/include/c++/4.8.2/algorithm:62,
from /gl-sync/thirdparty/Qt/4.8.4/lnx_x23.64/include/QtCore/qglobal.h:68,
from /gl-sync/thirdparty/Qt/4.8.4/lnx_x23.64/include/QtCore/qatomic.h:45,
from /gl-sync/thirdparty/Qt/4.8.4/lnx_x23.64/include/QtCore/qhash.h:45,
from /gl-sync/thirdparty/Qt/4.8.4/lnx_x23.64/include/QtCore/QHash:1,
from /usr2/viewstore_some/xy01/xy01_unix1/fubar/rappserv/code/include/convenience/stdhash_quuid.h:11,
from /usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/include/xmodel/core/ItemId.h:11,
from /usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/include/xmodel/core/IDataItem.h:11,
from /usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/include/xmodel/core/DataItemDescriptor.h:11,
from /usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/include/xmodel/core/IDataItemCollection.h:11,
from /usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/src/DataItemCollection.h:11,
from /usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/src/DataItemCollection.cpp:9:
/gl-sync/compilers/gcc/4.8.2/sle11sp0/include/c++/4.8.2/functional:1655:5: note: template<class _Func, class ... _BoundArgs> typename std::_Bind_helper<std::__or_<std::is_integral<typename std::decay<_Tp>::type>, std::is_enum<typename std::decay<_Tp>::type> >::value, _Func, _BoundArgs ...>::type std::bind(_Func&&, _BoundArgs&& ...)
bind(_Func&& __f, _BoundArgs&&... __args)
^
/gl-sync/compilers/gcc/4.8.2/sle11sp0/include/c++/4.8.2/functional:1655:5: note: template argument deduction/substitution failed:
/usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/src/DataItemCollection.cpp:414:66: note: couldn't deduce template parameter ‘_Func’
, std::bind(&IDataItemCollectionScope::findItemById, _1, id)
^
In file included from /gl-sync/compilers/gcc/4.8.2/sle11sp0/include/c++/4.8.2/bits/stl_algo.h:66:0,
from /gl-sync/compilers/gcc/4.8.2/sle11sp0/include/c++/4.8.2/algorithm:62,
from /gl-sync/thirdparty/Qt/4.8.4/lnx_x23.64/include/QtCore/qglobal.h:68,
from /gl-sync/thirdparty/Qt/4.8.4/lnx_x23.64/include/QtCore/qatomic.h:45,
from /gl-sync/thirdparty/Qt/4.8.4/lnx_x23.64/include/QtCore/qhash.h:45,
from /gl-sync/thirdparty/Qt/4.8.4/lnx_x23.64/include/QtCore/QHash:1,
from /usr2/viewstore_some/xy01/xy01_unix1/fubar/rappserv/code/include/convenience/stdhash_quuid.h:11,
from /usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/include/xmodel/core/ItemId.h:11,
from /usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/include/xmodel/core/IDataItem.h:11,
from /usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/include/xmodel/core/DataItemDescriptor.h:11,
from /usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/include/xmodel/core/IDataItemCollection.h:11,
from /usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/src/DataItemCollection.h:11,
from /usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/src/DataItemCollection.cpp:9:
/gl-sync/compilers/gcc/4.8.2/sle11sp0/include/c++/4.8.2/functional:1682:5: note: template<class _Result, class _Func, class ... _BoundArgs> typename std::_Bindres_helper<_Result, _Func, _BoundArgs>::type std::bind(_Func&&, _BoundArgs&& ...)
bind(_Func&& __f, _BoundArgs&&... __args)
^
/gl-sync/compilers/gcc/4.8.2/sle11sp0/include/c++/4.8.2/functional:1682:5: note: template argument deduction/substitution failed:
/usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/src/DataItemCollection.cpp:414:66: note: couldn't deduce template parameter ‘_Result’
, std::bind(&IDataItemCollectionScope::findItemById, _1, id)
^
-- ERROR --
/usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/src/DataItemCollection.cpp:417:1: error: control reaches end of non-void function [-Werror=return-type]
}
^
'stl' is up to date.
gcc -O3 -g -I/Users/jleffler/inc -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition stl.c -o stl -L/Users/jleffler/lib/64 -ljl
gcc -O3 -g -I/Users/jleffler/inc -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition stl.c -o stl -L/Users/jleffler/lib/64 -ljl
stl.c: In function ‘printStudent’:
-- WARNING --
stl.c:84:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘List’ [-Wformat=]
printf("Name: %s ", student->first);
^
-- WARNING --
stl.c:84:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘List’ [-Wformat=]
-- WARNING --
stl.c:85:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
printf("Grade: %s\n", student->grade);
^
-- WARNING --
stl.c:85:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
gcc -O3 -g -I/Users/jleffler/inc -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition -Werror stl.c -o stl -L/Users/jleffler/lib/64 -ljl
stl.c: In function ‘printStudent’:
-- ERROR --
stl.c:84:5: error: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘List’ [-Werror=format=]
printf("Name: %s ", student->first);
^
-- ERROR --
stl.c:84:5: error: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘List’ [-Werror=format=]
-- ERROR --
stl.c:85:5: error: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Werror=format=]
printf("Grade: %s\n", student->grade);
^
-- ERROR --
stl.c:85:5: error: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Werror=format=]
cc1: all warnings being treated as errors