pestctrl updated this revision to Diff 265898. pestctrl added a comment. Added warning to group c99-extensions, only enable warning when C99 or less
Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D79945/new/ https://reviews.llvm.org/D79945 Files: clang/include/clang/Basic/DiagnosticSemaKinds.td clang/lib/Sema/SemaExpr.cpp clang/test/Sema/c89.c Index: clang/test/Sema/c89.c =================================================================== --- clang/test/Sema/c89.c +++ clang/test/Sema/c89.c @@ -127,3 +127,11 @@ struct Test17 t1 = test17_aux(); /* this is allowed */ } +int incomplete[]; /* expected-warning {{tentative array definition assumed to have one element}} */ +int complete[5]; + +void test18() { + if (&incomplete < &complete) { /* expected-warning {{ordered comparison of complete and incomplete pointers}} */ + return; + } +} Index: clang/lib/Sema/SemaExpr.cpp =================================================================== --- clang/lib/Sema/SemaExpr.cpp +++ clang/lib/Sema/SemaExpr.cpp @@ -11422,11 +11422,23 @@ // 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, + getLangOpts().C11 + ? diag::ext_typecheck_compare_complete_incomplete_pointers + : diag::warn_typecheck_compare_complete_incomplete_pointers) + << 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())) { Index: clang/include/clang/Basic/DiagnosticSemaKinds.td =================================================================== --- clang/include/clang/Basic/DiagnosticSemaKinds.td +++ clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -6441,6 +6441,11 @@ "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_compare_complete_incomplete_pointers : Extension< + "ordered comparison of complete and incomplete pointers (%0 and %1)">; +def warn_typecheck_compare_complete_incomplete_pointers : ExtWarn< + "ordered comparison of complete and incomplete pointers (%0 and %1)">, + InGroup<C99Designator>; def ext_typecheck_ordered_comparison_of_function_pointers : ExtWarn< "ordered comparison of function pointers (%0 and %1)">, InGroup<DiagGroup<"ordered-compare-function-pointers">>;
Index: clang/test/Sema/c89.c =================================================================== --- clang/test/Sema/c89.c +++ clang/test/Sema/c89.c @@ -127,3 +127,11 @@ struct Test17 t1 = test17_aux(); /* this is allowed */ } +int incomplete[]; /* expected-warning {{tentative array definition assumed to have one element}} */ +int complete[5]; + +void test18() { + if (&incomplete < &complete) { /* expected-warning {{ordered comparison of complete and incomplete pointers}} */ + return; + } +} Index: clang/lib/Sema/SemaExpr.cpp =================================================================== --- clang/lib/Sema/SemaExpr.cpp +++ clang/lib/Sema/SemaExpr.cpp @@ -11422,11 +11422,23 @@ // 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, + getLangOpts().C11 + ? diag::ext_typecheck_compare_complete_incomplete_pointers + : diag::warn_typecheck_compare_complete_incomplete_pointers) + << 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())) { Index: clang/include/clang/Basic/DiagnosticSemaKinds.td =================================================================== --- clang/include/clang/Basic/DiagnosticSemaKinds.td +++ clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -6441,6 +6441,11 @@ "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_compare_complete_incomplete_pointers : Extension< + "ordered comparison of complete and incomplete pointers (%0 and %1)">; +def warn_typecheck_compare_complete_incomplete_pointers : ExtWarn< + "ordered comparison of complete and incomplete pointers (%0 and %1)">, + InGroup<C99Designator>; def ext_typecheck_ordered_comparison_of_function_pointers : ExtWarn< "ordered comparison of function pointers (%0 and %1)">, InGroup<DiagGroup<"ordered-compare-function-pointers">>;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits