================
@@ -6726,17 +6726,63 @@ QualType ASTContext::getTagDeclType(const TagDecl 
*Decl) const {
   return getTypeDeclType(const_cast<TagDecl*>(Decl));
 }
 
+// Inject __size_t, __signed_size_t, and __ptrdiff_t to provide portable hints
+// and diagnostics. In C and C++, expressions of type size_t can be obtained 
via
+// the sizeof operator, expressions of type ptrdiff_t via pointer subtraction,
+// and expressions of type signed size_t via the z literal suffix (since 
C++23).
+// However, no core language mechanism directly produces an expression of type
+// unsigned ptrdiff_t. The unsigned ptrdiff_t type is solely required by format
+// specifiers for printf and scanf. Consequently, no expression's type needs to
+// be displayed as unsigned ptrdiff_t. Verification of whether a type is
+// unsigned ptrdiff_t is also unnecessary, as no corresponding typedefs exist.
+// Therefore, injecting a typedef for signed ptrdiff_t is not required.
+
 /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
 /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
 /// needs to agree with the definition in <stddef.h>.
-CanQualType ASTContext::getSizeType() const {
-  return getFromTargetType(Target->getSizeType());
+QualType ASTContext::getSizeType() const {
+  if (SizeType.isNull()) {
+    if (auto const &LO = getLangOpts(); !LO.HLSL && (LO.C99 || LO.CPlusPlus))
+      SizeType = getTypedefType(buildImplicitTypedef(
+          getFromTargetType(Target->getSizeType()), "__size_t"));
+    else
+      SizeType = getFromTargetType(Target->getSizeType());
+  }
+  return SizeType;
 }
 
 /// Return the unique signed counterpart of the integer type
 /// corresponding to size_t.
-CanQualType ASTContext::getSignedSizeType() const {
-  return getFromTargetType(Target->getSignedSizeType());
+QualType ASTContext::getSignedSizeType() const {
+  if (SignedSizeType.isNull()) {
+    if (auto const &LO = getLangOpts(); !LO.HLSL && (LO.C99 || LO.CPlusPlus))
+      SignedSizeType = getTypedefType(buildImplicitTypedef(
+          getFromTargetType(Target->getSignedSizeType()), "__signed_size_t"));
+    else
+      SignedSizeType = getFromTargetType(Target->getSignedSizeType());
+  }
+  return SignedSizeType;
+}
+
+/// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17)
+/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
+QualType ASTContext::getPointerDiffType() const {
+  if (PtrdiffType.isNull()) {
+    if (auto const &LO = getLangOpts(); !LO.HLSL && (LO.C99 || LO.CPlusPlus))
+      PtrdiffType = getTypedefType(buildImplicitTypedef(
+          getFromTargetType(Target->getPtrDiffType(LangAS::Default)),
+          "__ptrdiff_t"));
----------------
mizvekov wrote:

Pre-existing problem, but creating all these implicit typedefs is pretty 
annoying and inefficient.

It would be quite simple to create a new sugar type node to represent these, 
they can be similar to a typedef, but instead of pointing to a decl, they would 
point to some kind of enum.

https://github.com/llvm/llvm-project/pull/143653
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to