Go to file
David Malcolm 0d48e8779c Support string locations for C++ in -Wformat (PR c++/56856)
-Wformat in the C++ FE doesn't work as well as it could:
(a) it doesn't report precise locations within the string literal, and
(b) it doesn't underline arguments for those arguments !CAN_HAVE_LOCATION_P,
despite having location wrapper nodes.

For example:

  Wformat-ranges.C:32:10: warning: format '%s' expects argument of type 'char*', but argument 2 has type 'int' [-Wformat=]
  32 |   printf("hello %s", 42);
     |          ^~~~~~~~~~

(a) is due to not wiring up the langhook for extracting substring
    locations.

    This patch uses the one in c-family; it also fixes string literal
    parsing so that it records string concatenations (needed for
    extracting substring locations from concatenated strings).

(b) is due to the call to maybe_constant_value here:
       fargs[j] = maybe_constant_value (argarray[j]);
    within build_over_call.

    The patch fixes this by building a vec of location_t values when
    calling check_function_arguments.
    I attempted to eliminate the maybe_constant_value call here, but
    it's needed by e.g. check_function_sentinel for detecting NULL,
    and that code is in "c-family", so it can't simply call into
    maybe_constant_value (which is in "cp").

With this patch, the output for the above example is improved to:

  Wformat-ranges.C:32:18: warning: format '%s' expects argument of type 'char*', but argument 2 has type 'int' [-Wformat=]
  32 |   printf("hello %s", 42);
     |                 ~^   ~~
     |                  |   |
     |                  |   int
     |                  char*
     |                 %d

gcc/cp/ChangeLog:
	PR c++/56856
	* call.c (build_over_call): Build a vec of locations of the
	arguments before the call to maybe_constant_value, and pass to
	check_function_arguments.
	* cp-lang.c (LANG_HOOKS_GET_SUBSTRING_LOCATION): Define as
	c_get_substring_location.
	* parser.c (cp_parser_string_literal): Capture string
	concatenation locations.

gcc/ChangeLog:
	PR c++/56856
	* input.c (expand_location_to_spelling_point): Add param "aspect"
	and use rather than hardcoding LOCATION_ASPECT_CARET.
	(get_substring_ranges_for_loc): Handle the case of a single token
	within a macro expansion.
	* input.h (expand_location_to_spelling_point): Add "aspect" param,
	defaulting to LOCATION_ASPECT_CARET.

gcc/testsuite/ChangeLog:
	PR c++/56856
	* g++.dg/ext/builtin4.C: Set expected location for warning to the
	correct location within the format string.
	* g++.dg/plugin/plugin.exp (plugin_test_list): Add the plugin and
	files for testing locations within string literal locations from
	the C frontend.
	* g++.dg/warn/Wformat-method.C: New test.
	* g++.dg/warn/Wformat-pr71863.C: New test.
	* g++.dg/warn/Wformat-ranges-c++11.C: New test.
	* g++.dg/warn/Wformat-ranges.C: New test, based on
	gcc.dg/format/diagnostic-ranges.c.
	* gcc.dg/plugin/diagnostic-test-string-literals-1.c
	(test_multitoken_macro): Generalize expected output to work with
	both C and C++.
	* gcc.dg/plugin/diagnostic-test-string-literals-2.c
	(test_stringified_token_1): Likewise.
	(test_stringified_token_3): Likewise.

From-SVN: r264887
2018-10-05 19:02:17 +00:00
config
contrib contrib: unused_functions.py: Handle archives 2018-10-05 00:28:18 +02:00
fixincludes fixincludes: vxworks: remove unnecessary parentheses in ioctl wrapper macro 2018-09-03 13:26:45 +00:00
gcc Support string locations for C++ in -Wformat (PR c++/56856) 2018-10-05 19:02:17 +00:00
gnattools
gotools Ignore chmod errors during mostlyclean-local, check-go-tool 2018-09-26 12:56:09 +00:00
include
INSTALL
intl libgnuintl.h (_INTL_MAY_RETURN_STRING_ARG, [...]): Backport changes from upstream gettext. 2018-09-18 22:08:49 -06:00
libada
libatomic
libbacktrace re PR libbacktrace/87529 (libbacktrace API forces users to have memory leaks) 2018-10-05 14:09:07 +00:00
libcc1
libcpp Add range_idx param to range_label::get_text 2018-09-17 23:32:12 +00:00
libdecnumber
libffi
libgcc Fix divergence in indirect profiling (PR gcov-profile/84107). 2018-10-04 12:41:14 +00:00
libgfortran * io/close.c [!HAVE_UNLINK_OPEN_FILE]: Include <string.h>. 2018-10-02 20:02:03 +00:00
libgo libgo: use inline assembly in favor of call to _xgetbv() 2018-10-05 17:51:57 +00:00
libgomp [nvptx] Remove use of CUDA unified memory in libgomp 2018-09-18 08:41:54 -07:00
libhsail-rt
libiberty [libiberty] Use pipe inside pex_run 2018-10-01 18:46:51 +00:00
libitm
libobjc
liboffloadmic
libquadmath
libsanitizer
libssp
libstdc++-v3 2018-10-05 François Dumont <fdumont@gcc.gnu.org> 2018-10-05 16:01:39 +00:00
libvtv
lto-plugin
maintainer-scripts
zlib
.dir-locals.el
.gitattributes
.gitignore
ABOUT-NLS
ChangeLog List myself as "libgomp (OpenACC)" and "OpenACC" maintainer 2018-10-04 17:50:34 +02:00
ChangeLog.jit
ChangeLog.tree-ssa
compile
config-ml.in
config.guess
config.rpath
config.sub
configure
configure.ac
COPYING
COPYING3
COPYING3.LIB
COPYING.LIB
COPYING.RUNTIME
depcomp
install-sh
libtool-ldflags
libtool.m4
lt~obsolete.m4
ltgcc.m4
ltmain.sh
ltoptions.m4
ltsugar.m4
ltversion.m4
MAINTAINERS List myself as "libgomp (OpenACC)" and "OpenACC" maintainer 2018-10-04 17:50:34 +02:00
Makefile.def
Makefile.in
Makefile.tpl
missing
mkdep
mkinstalldirs
move-if-change
README
symlink-tree
ylwrap

This directory contains the GNU Compiler Collection (GCC).

The GNU Compiler Collection is free software.  See the files whose
names start with COPYING for copying permission.  The manuals, and
some of the runtime libraries, are under different terms; see the
individual source files for details.

The directory INSTALL contains copies of the installation information
as HTML and plain text.  The source of this information is
gcc/doc/install.texi.  The installation information includes details
of what is included in the GCC sources and what files GCC installs.

See the file gcc/doc/gcc.texi (together with other files that it
includes) for usage and porting information.  An online readable
version of the manual is in the files gcc/doc/gcc.info*.

See http://gcc.gnu.org/bugs/ for how to report bugs usefully.

Copyright years on GCC source files may be listed using range
notation, e.g., 1987-2012, indicating that every year in the range,
inclusive, is a copyrightable year that could otherwise be listed
individually.