MS/Borland portability and 2-phase lookup

We have a portability problem compiling old code for MS/Borland with
clang: 2-phase lookup. Those compilers don't do it, so they accept code
that clang rejects. For example:

template<class X>
struct S {
  int i;
};

template<class Y>
struct T {
  T() {
    i = 0; // ERROR, should be T::i or this->i
  }
};

Clang correctly diagnoses this problem, which presents a problem for old
broken code that compiles OK with MS and Borland. This problem appears
to be quite widespread.

Would there be any interest in a patch that turns off 2-phase lookup
when in MS/Borland compatibility mode
(-fms-extensions/-fborland-extensions)? If so, I can see about providing
one.

I think François Pichet is working on an MS-compatible template mechanism.
It's more complicated than just turning off 2-phase lookup.

John.

It would also be nice to have a finer grained control than enabling ALL bugs for particular compiler. In your example it might be better to actually fix the code, rather than have the compiler accept everything.

Bo Persson

We have a portability problem compiling old code for MS/Borland with
clang: 2-phase lookup. Those compilers don't do it, so they accept code
that clang rejects. For example:

template<class X>
struct S {
int i;
};

template<class Y>
struct T {
T() {
   i = 0; // ERROR, should be T::i or this->i
}
};

Presumably, you meant to inherit T from Y, or S<Y>, or something dependent.

Clang correctly diagnoses this problem, which presents a problem for old
broken code that compiles OK with MS and Borland. This problem appears
to be quite widespread.

Would there be any interest in a patch that turns off 2-phase lookup
when in MS/Borland compatibility mode
(-fms-extensions/-fborland-extensions)? If so, I can see about providing
one.

The *ideal* way to handle this would be to improve Clang's error recovery to the point where we could just warn that there is a two-phase name lookup issue here (with Fix-Its, naturally). Then, we don't have any special magic for MS/Borland compatibility modes.

  - Doug

Hi, I am very close to submit a patch that will dramatically improve
clang -fms-extensions mode handling of microsoft template.

What the patch does (suggested by Doug Gregor on the IRC channel) is
to simply skip parsing of template function code, lex and save the
tokens and then at the end of the translation unit do the actual
parsing with all the information of the translation unit available
(similar to the way inline functions parsing is postponed to the end
of the class definition)

This solves the vast majority of errors when parsing the Microsoft
template headers with clang.
Actually this evening, I was even able to self-host clang (on darwin)
with this mechanism.

Awesome! This sounds great, Francois. I encourage you to continue ...
this is valuable work.