[clang] Add new flag -Wpointer-integer-ordered-compare (PR #88489)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 12 02:33:38 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Bhuminjay Soni (11happy)
<details>
<summary>Changes</summary>
**Overview:**
This pull request fixes #<!-- -->88090 where a false negative issue related to `-Wpointer-integer-compare` failing on comparison between a pointer and zero.
**Testing:**
- Tested the updated code.
- Verified that other functionalities remain unaffected.
**Dependencies:**
- No dependencies on other pull requests.
**CC:**
- @<!-- -->AaronBallman , @<!-- -->shafik
---
Full diff: https://github.com/llvm/llvm-project/pull/88489.diff
5 Files Affected:
- (modified) clang/docs/ReleaseNotes.rst (+6)
- (modified) clang/include/clang/Basic/DiagnosticGroups.td (+2-1)
- (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+5-3)
- (modified) clang/test/Misc/warning-flags.c (+2-3)
- (added) clang/test/Sema/compare_pointers.c (+30)
``````````diff
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 93318871fa9f6e..e205088c5cd440 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -245,6 +245,12 @@ Modified Compiler Flags
f3 *c = (f3 *)x;
}
+- Added a new diagnostic flag ``-Wpointer-integer-ordered-compare`` which is
+ grouped under ``-Wpointer-integer-compare`` and moved previously
+ ungrouped diagnostics ``ext_typecheck_ordered_comparison_of_pointer_integer``,
+ ``ext_typecheck_ordered_comparison_of_pointer_and_zero`` under
+ ``-Wpointer-integer-ordered-compare``. Fixes #GH88090
+
Removed Compiler Flags
-------------------------
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td b/clang/include/clang/Basic/DiagnosticGroups.td
index 47747d8704b6c8..2cf56574650839 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -714,10 +714,11 @@ def HeaderHygiene : DiagGroup<"header-hygiene">;
def DuplicateDeclSpecifier : DiagGroup<"duplicate-decl-specifier">;
def CompareDistinctPointerType : DiagGroup<"compare-distinct-pointer-types">;
def GNUUnionCast : DiagGroup<"gnu-union-cast">;
+def PointerIntegerOrderedCompare : DiagGroup<"pointer-integer-ordered-compare">;
+def PointerIntegerCompare : DiagGroup<"pointer-integer-compare",[PointerIntegerOrderedCompare]>;
def GNUVariableSizedTypeNotAtEnd : DiagGroup<"gnu-variable-sized-type-not-at-end">;
def Varargs : DiagGroup<"varargs">;
def XorUsedAsPow : DiagGroup<"xor-used-as-pow">;
-
def Unsequenced : DiagGroup<"unsequenced">;
// GCC name for -Wunsequenced
def : DiagGroup<"sequence-point", [Unsequenced]>;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 180e913155d67c..a5632a3f6f61da 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -7272,9 +7272,11 @@ def err_typecheck_sub_ptr_compatible : Error<
"%diff{$ and $ are not pointers to compatible types|"
"pointers to incompatible types}0,1">;
def ext_typecheck_ordered_comparison_of_pointer_integer : ExtWarn<
- "ordered comparison between pointer and integer (%0 and %1)">;
+ "ordered comparison between pointer and integer (%0 and %1)">,
+ InGroup<PointerIntegerOrderedCompare>;
def ext_typecheck_ordered_comparison_of_pointer_and_zero : Extension<
- "ordered comparison between pointer and zero (%0 and %1) is an extension">;
+ "ordered comparison between pointer and zero (%0 and %1) is an extension">,
+ InGroup<PointerIntegerOrderedCompare>;
def err_typecheck_ordered_comparison_of_pointer_and_zero : Error<
"ordered comparison between pointer and zero (%0 and %1)">;
def err_typecheck_three_way_comparison_of_pointer_and_zero : Error<
@@ -7299,7 +7301,7 @@ def err_typecheck_comparison_of_fptr_to_void : Error<
"equality comparison between function pointer and void pointer (%0 and %1)">;
def ext_typecheck_comparison_of_pointer_integer : ExtWarn<
"comparison between pointer and integer (%0 and %1)">,
- InGroup<DiagGroup<"pointer-integer-compare">>;
+ InGroup<PointerIntegerCompare>;
def err_typecheck_comparison_of_pointer_integer : Error<
"comparison between pointer and integer (%0 and %1)">;
def ext_typecheck_comparison_of_distinct_pointers : ExtWarn<
diff --git a/clang/test/Misc/warning-flags.c b/clang/test/Misc/warning-flags.c
index dd73331913c6f6..8c0e151613bd82 100644
--- a/clang/test/Misc/warning-flags.c
+++ b/clang/test/Misc/warning-flags.c
@@ -18,7 +18,7 @@ This test serves two purposes:
The list of warnings below should NEVER grow. It should gradually shrink to 0.
-CHECK: Warnings without flags (66):
+CHECK: Warnings without flags (65):
CHECK-NEXT: ext_expected_semi_decl_list
CHECK-NEXT: ext_explicit_specialization_storage_class
@@ -28,7 +28,6 @@ CHECK-NEXT: ext_plain_complex
CHECK-NEXT: ext_template_arg_extra_parens
CHECK-NEXT: ext_template_spec_extra_headers
CHECK-NEXT: ext_typecheck_cond_incompatible_operands
-CHECK-NEXT: ext_typecheck_ordered_comparison_of_pointer_integer
CHECK-NEXT: ext_using_undefined_std
CHECK-NEXT: pp_invalid_string_literal
CHECK-NEXT: pp_out_of_date_dependency
@@ -89,4 +88,4 @@ CHECK-NEXT: warn_weak_import
The list of warnings in -Wpedantic should NEVER grow.
-CHECK: Number in -Wpedantic (not covered by other -W flags): 26
+CHECK: Number in -Wpedantic (not covered by other -W flags): 25
diff --git a/clang/test/Sema/compare_pointers.c b/clang/test/Sema/compare_pointers.c
new file mode 100644
index 00000000000000..07592ea17a3193
--- /dev/null
+++ b/clang/test/Sema/compare_pointers.c
@@ -0,0 +1,30 @@
+// RUN: %clang_cc1 -Wno-pointer-integer-compare -Wpointer-integer-ordered-compare -fsyntax-only -verify=pointer-integer-ordered %s
+// RUN: %clang_cc1 -Wpointer-integer-compare -Wno-pointer-integer-ordered-compare -fsyntax-only -verify=pointer-integer %s
+
+void test1(int *a){
+ int b = 1;
+ short c = 1;
+ if(c<a) {}; // pointer-integer-ordered-warning{{ordered comparison between pointer and integer ('short' and 'int *')}}
+ if(a!=b) {}; // pointer-integer-warning{{comparison between pointer and integer ('int *' and 'int')}}
+ if(a == b) {}; // pointer-integer-warning{{comparison between pointer and integer ('int *' and 'int')}}
+}
+
+int test2(int *a){
+ return a>=0; // pointer-integer-ordered-warning{{ordered comparison between pointer and zero ('int *' and 'int') is an extension}}
+}
+
+int test3(int *a){
+ return a>=1; // pointer-integer-ordered-warning{{ordered comparison between pointer and integer ('int *' and 'int')}}
+}
+
+int test4(int *a){
+ return a>1; // pointer-integer-ordered-warning{{ordered comparison between pointer and integer ('int *' and 'int')}}
+}
+int test5(int *a){
+ int zero = 0;
+ return a>=zero; // pointer-integer-ordered-warning{{ordered comparison between pointer and integer ('int *' and 'int')}}
+}
+
+
+
+
``````````
</details>
https://github.com/llvm/llvm-project/pull/88489
More information about the cfe-commits
mailing list