Skip to content

Commit 7a7e1e4

Browse files
committed
Refactor integer 0 handling in ternary operator typing
1 parent 46e4f05 commit 7a7e1e4

File tree

2 files changed

+8
-16
lines changed

2 files changed

+8
-16
lines changed

src/frontc/cabs2cil.ml

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,29 +1911,19 @@ let conditionalConversion (t2: typ) (t3: typ) (e2: exp option) (e3:exp) : typ =
19111911
arithmeticConversion t2 t3
19121912
| TComp (comp2,_), TComp (comp3,_), _
19131913
when comp2.ckey = comp3.ckey -> t2
1914-
| TPtr(b2, _), TPtr(TVoid a3, _), _ ->
1915-
if isNullPtrConstant e3 then
1916-
t2
1917-
else (
1914+
| TPtr(_, _), _, _ when isNullPtrConstant e3 -> t2
1915+
| _, TPtr(_, _), Some e2' when isNullPtrConstant e2' -> t3
1916+
| TPtr(b2, _), TPtr(TVoid _ as b3, _), _
1917+
| TPtr(TVoid _ as b2, _), TPtr(b3, _), _ ->
19181918
let a2 = typeAttrsOuter b2 in
1919-
let (q2, _) = partitionQualifierAttributes a2 in
1920-
let (q3, _) = partitionQualifierAttributes a3 in
1921-
let q = cabsAddAttributes q2 q3 in
1922-
TPtr (TVoid q, [])
1923-
)
1924-
| TPtr(TVoid a2, _), TPtr(b3, _), Some e2' ->
1925-
if isNullPtrConstant e2' then
1926-
t3
1927-
else (
19281919
let a3 = typeAttrsOuter b3 in
19291920
let (q2, _) = partitionQualifierAttributes a2 in
19301921
let (q3, _) = partitionQualifierAttributes a3 in
19311922
let q = cabsAddAttributes q2 q3 in
19321923
TPtr (TVoid q, [])
1933-
)
19341924
| TPtr _, TPtr _, _ when Util.equals (typeSig t2) (typeSig t3) -> t2
1935-
| TPtr _, TInt _, _ -> t2 (* most likely comparison with int constant 0, if it isn't it would not be valid C *)
1936-
| TInt _, TPtr _, _ -> t3 (* most likely comparison with int constant 0, if it isn't it would not be valid C *)
1925+
| TPtr _, TInt _, _ -> t2 (* not "null pointer constant", not allowed by standard, works in gcc/clang with warning *)
1926+
| TInt _, TPtr _, _ -> t3 (* not "null pointer constant", not allowed by standard, works in gcc/clang with warning *)
19371927

19381928
(* When we compare two pointers of different type, we combine them
19391929
* using the same algorithm when combining multiple declarations of

test/small1/clang-c11-generic-2.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@ int main() {
88

99
// added here
1010
_Generic(0 ? (const int*)0 : (volatile int*)0, const volatile int*: (void)0);
11+
_Generic(0 ? (int const *)0 : 0, int const *: (void)0);
12+
_Generic(0 ? (int const *)0 : 1, int const *: (void)0); // not allowed by standard, works in gcc/clang with warning
1113
return 0;
1214
}

0 commit comments

Comments
 (0)