Avoid calling mergeTypes in C++.  I think these are the correct C++ 
alternatives, but please correct me if I'm wrong.

I eventually plan to assert in mergeTypes that we aren't in C++ mode 
because composite types are fundamentally not a part of C++. The 
remaining callers for code in the regression tests are 
Sema::WarnConflictingTypedMethods and CodeGenFunction::EmitFunctionProlog;
I'm not quite sure what the correct approach is for those callers.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@71946 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp
index 4145be6..77902d5 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/lib/Sema/SemaExpr.cpp
@@ -3727,14 +3727,24 @@
                                      rex->getType()))
         return QualType();
 
-      // Pointee types must be compatible.
-      if (!Context.typesAreCompatible(
-              Context.getCanonicalType(lpointee).getUnqualifiedType(),
-              Context.getCanonicalType(rpointee).getUnqualifiedType())) {
-        Diag(Loc, diag::err_typecheck_sub_ptr_compatible)
-          << lex->getType() << rex->getType()
-          << lex->getSourceRange() << rex->getSourceRange();
-        return QualType();
+      if (getLangOptions().CPlusPlus) {
+        // Pointee types must be the same: C++ [expr.add]
+        if (!Context.hasSameUnqualifiedType(lpointee, rpointee)) {
+          Diag(Loc, diag::err_typecheck_sub_ptr_compatible)
+            << lex->getType() << rex->getType()
+            << lex->getSourceRange() << rex->getSourceRange();
+          return QualType();
+        }
+      } else {
+        // Pointee types must be compatible C99 6.5.6p3
+        if (!Context.typesAreCompatible(
+                Context.getCanonicalType(lpointee).getUnqualifiedType(),
+                Context.getCanonicalType(rpointee).getUnqualifiedType())) {
+          Diag(Loc, diag::err_typecheck_sub_ptr_compatible)
+            << lex->getType() << rex->getType()
+            << lex->getSourceRange() << rex->getSourceRange();
+          return QualType();
+        }
       }
 
       if (ComplainAboutVoid)