This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Updated GCC vs Clang diagnostics [Was: Switching to C++ by default in 4.8]


On 12 April 2012 11:35, Richard Guenther wrote:
> And since yesterday GCC shows
>
> t.C:2:10: error: expected ';' after class definition
> ?class a {}
> ? ? ? ? ?^
> t.C:6:1: error: expected ';' after struct definition
> ?}
> ?^
>
> as we now enabled -fdiagnostics-show-caret by default.

Yep :-)

Because of that I plan to show results for both 4.7 and 4.8 on the
wiki page, but I only have the 4.8-20120408 snapshot to hand right now
so did those first tests with 4.7.0


Two more examples, then I'll save it for a wiki page instead of the
mailing list:

A missing "typename" keyword is a very common, very easy to make
mistake when using C++ templates.  It isn't usually obvious what the
problem is, so it's exactly where compilers could help, but both get
zero marks here:

$ cat missing-typename.cc
template<class T> void f(T::type) { }

struct A { };

void g()
{
    A a;
    f<A>(a);
}
$ g++-4.7 missing-typename.cc
missing-typename.cc:1:33: error: variable or field 'f' declared void
missing-typename.cc: In function 'void g()':
missing-typename.cc:8:5: error: 'f' was not declared in this scope
missing-typename.cc:8:8: error: expected primary-expression before '>' token

$ clang++-3.1 missing-typename.cc
missing-typename.cc:1:24: error: variable 'f' declared as a template
template<class T> void f(T::type) { }
~~~~~~~~~~~~~~~~~      ^
missing-typename.cc:1:34: error: expected ';' at end of declaration
template<class T> void f(T::type) { }
                                 ^
                                 ;
missing-typename.cc:1:35: error: expected unqualified-id
template<class T> void f(T::type) { }
                                  ^
missing-typename.cc:7:5: error: use of undeclared identifier 'A'
    A a;
    ^
missing-typename.cc:8:5: error: use of undeclared identifier 'f'
    f<A>(a);
    ^
missing-typename.cc:8:7: error: use of undeclared identifier 'A'
    f<A>(a);
      ^
missing-typename.cc:8:10: error: use of undeclared identifier 'a'
    f<A>(a);
         ^
7 errors generated.


Clang's carets and range highlighting don't help in the slightest, and
the poor error recovery that means "A" is reported as unidentified
doesn't help either.

If you add the missing typename then G++ does better:

$ cat deduce.cc
template<class T> void f(typename T::type) { }

struct A { };

void g()
{
    A a;
    f<A>(a);
}
$ g++-4.7 deduce.cc
deduce.cc: In function 'void g()':
deduce.cc:8:11: error: no matching function for call to 'f(A&)'
deduce.cc:8:11: note: candidate is:
deduce.cc:1:24: note: template<class T> void f(typename T::type)
deduce.cc:1:24: note:   template argument deduction/substitution failed:
deduce.cc: In substitution of 'template<class T> void f(typename
T::type) [with T = A]':
deduce.cc:8:11:   required from here
deduce.cc:1:24: error: no type named 'type' in 'struct A'

$ clang++-3.1 deduce.cc
deduce.cc:8:5: error: no matching function for call to 'f'
    f<A>(a);
    ^~~~
deduce.cc:1:24: note: candidate template ignored: substitution failure
[with T = A]
template<class T> void f(typename T::type) { }
                       ^
1 error generated.


Compare "no type named 'type' in 'struct A'" with "substitution failure"


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]