[PATCH] D97831: [Clang][Sema] Implement GCC -Wcast-function-type

Yuanfang Chen via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 23 18:18:20 PDT 2021


ychen added inline comments.


================
Comment at: clang/lib/Sema/SemaCast.cpp:1044-1048
+  // Allow integral type mismatch if their size are equal.
+  if (SrcType->isIntegralType(Context) && DestType->isIntegralType(Context))
+    if (Context.getTypeInfoInChars(SrcType).Width ==
+        Context.getTypeInfoInChars(DestType).Width)
+      return true;
----------------
rsmith wrote:
> In addition to allowing the cases where the sizes are the same width, should we also allow cases where the promoted types are the same width (eg, `int` versus `short`)? What does GCC do?
GCC does exactly that. I didn't find a way to do that by looking at TargetInfo. Maybe there is a way for clang?


================
Comment at: clang/lib/Sema/SemaCast.cpp:1050-1055
+  llvm::DenseSet<std::pair<Decl *, Decl *>> NonEquivalentDecls;
+  StructuralEquivalenceContext Ctx(
+      Context, Context, NonEquivalentDecls, StructuralEquivalenceKind::Default,
+      false /*StrictTypeSpelling*/, false /*Complain*/,
+      false /*ErrorOnTagTypeMismatch*/);
+  return Ctx.IsEquivalent(SrcType, DestType);
----------------
rsmith wrote:
> I think a "same type" check (`Context.hasSameUnqualifiedType(SrcType, DestType)`) would be more appropriate here than a structural equivalence check.
Agreed.


================
Comment at: clang/lib/Sema/SemaCast.cpp:1082-1083
+                 ->castAs<FunctionType>();
+  } else {
+    return true;
+  }
----------------
rsmith wrote:
> We should also handle the case where the source is of function type and the destination is a reference-to-function type.
> 
> Maybe also block pointer types?
Yep. Code/tests added for `function -> function pointer` and `block pointer -> function pointer`.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D97831/new/

https://reviews.llvm.org/D97831



More information about the cfe-commits mailing list