diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -6441,6 +6441,8 @@ "ordered comparison between pointer and zero (%0 and %1)">; def err_typecheck_three_way_comparison_of_pointer_and_zero : Error< "three-way comparison between pointer and zero">; +def ext_typecheck_comparison_of_complete_and_incomplete_types : ExtWarn< + "ordered comparison of complete and incomplete pointers (%0 and %1)">; def ext_typecheck_ordered_comparison_of_function_pointers : ExtWarn< "ordered comparison of function pointers (%0 and %1)">, InGroup>; diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -11422,11 +11422,21 @@ // C99 6.5.9p2 and C99 6.5.8p2 if (Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), RCanPointeeTy.getUnqualifiedType())) { - // Valid unless a relational comparison of function pointers - if (IsRelational && LCanPointeeTy->isFunctionType()) { - Diag(Loc, diag::ext_typecheck_ordered_comparison_of_function_pointers) - << LHSType << RHSType << LHS.get()->getSourceRange() - << RHS.get()->getSourceRange(); + if (IsRelational) { + // Pointers both need to point to complete or incomplete types + if (LCanPointeeTy->isIncompleteType() != + RCanPointeeTy->isIncompleteType()) { + Diag(Loc, + diag::ext_typecheck_comparison_of_complete_and_incomplete_types) + << LHSType << RHSType << LHS.get()->getSourceRange() + << RHS.get()->getSourceRange(); + } + if (LCanPointeeTy->isFunctionType()) { + // Valid unless a relational comparison of function pointers + Diag(Loc, diag::ext_typecheck_ordered_comparison_of_function_pointers) + << LHSType << RHSType << LHS.get()->getSourceRange() + << RHS.get()->getSourceRange(); + } } } else if (!IsRelational && (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { diff --git a/clang/test/Sema/compare.c b/clang/test/Sema/compare.c --- a/clang/test/Sema/compare.c +++ b/clang/test/Sema/compare.c @@ -405,3 +405,12 @@ if (x == y) x = y; // no warning if (y == x) y = x; // no warning } + +int incomplete[]; // expected-warning {{tentative array definition assumed to have one element}} +int complete[5]; + +void test13() { + if (&incomplete < &complete) { // expected-error {{ordered comparison of complete and incomplete pointers}} + return; + } +}