https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120706

            Bug ID: 120706
           Summary: Incorrect underlining for -Wformat when the type has a
                    user-defined copy constructor
           Product: gcc
           Version: 14.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libgdiagnostics
          Assignee: dmalcolm at gcc dot gnu.org
          Reporter: abbeyj+gcc at gmail dot com
  Target Milestone: ---

Compile the following with `g++ -c -Wall`

```
void my_printf(const char *fmt, ...) __attribute__((format(__printf__, 1, 2)));

class String {
public:
    String(const String&);
};

void print(const String& name) {
    my_printf("name: %s\n", name);
}
```


I would expect a diagnostic like 

```
warning: format '%s' expects argument of type 'char*', but argument 2 has type
'const String' [-Wformat=]
    9 |     my_printf("name: %s\n", name);
      |                      ~^     ~~~~
      |                       |     |
      |                       char* const String
```


However you actually get a diagnostic more like

```
warning: format '%s' expects argument of type 'char*', but argument 2 has type
'const String' [-Wformat=]
    9 |     my_printf("name: %s\n", name);
      |     ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~
      |              |        |
      |              |        char*
      |              const String
```


The underlining for `const String` encompasses the entire function call instead
of just the argument `name`.  Since the two underlines overlap it is a bit
confusing.  (When displayed in color it is less confusing than shown here
because the two underlines are drawn in different colors.)

If you remove the copy constructor or change the copy constructor to `=
default;` then the message changes to the first version above.

Godbolt link: https://godbolt.org/z/cGT6MMq7P

This looks like it started in GCC 14.1.  Other similarly odd behavior shows up
in earlier versions like GCC 7.1.  In that version, the underline shows up in
the correct place when the copy constructor is absent and is not drawn at all
when the copy constructor is present.

Possibly related to Bug 87757.

Reply via email to