[PATCH] D38270: [clang] Add getUnsignedPointerDiffType method

Alexander Shaposhnikov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 26 15:30:44 PDT 2017


alexshap created this revision.

C11 standard refers to the unsigned counterpart of the type ptrdiff_t 
in the paragraph 7.19.6 where it defines the format specifier %tu.
In Clang (in PrintfFormatString.cpp, lines 508-510) there is a FIXME for this case,
in particular, right now Clang doesn't diagnose %tu issues at all, i.e.
it doesn't emit any warnings on the code printf("%tu", 3.14).
In this diff we add a method getUnsignedPointerDiffType for getting the corresponding type
similarly to how it's already done in the other analogous cases (size_t, ssize_t, ptrdiff_t etc).
The code for printf diagnostics + new tests are supposed to be added by a separate diff.


Repository:
  rL LLVM

https://reviews.llvm.org/D38270

Files:
  include/clang/AST/ASTContext.h
  include/clang/Basic/TargetInfo.h
  lib/AST/ASTContext.cpp


Index: lib/AST/ASTContext.cpp
===================================================================
--- lib/AST/ASTContext.cpp
+++ lib/AST/ASTContext.cpp
@@ -4571,6 +4571,12 @@
   return getFromTargetType(Target->getPtrDiffType(0));
 }
 
+/// getUnsignedPointerDiffType - Return the unique unsigned counterpart of
+// "ptrdiff_t" integer type.
+QualType ASTContext::getUnsignedPointerDiffType() const {
+  return getFromTargetType(Target->getUnsignedPtrDiffType(0));
+}
+
 /// \brief Return the unique type for "pid_t" defined in
 /// <sys/types.h>. We need this to compute the correct type for vfork().
 QualType ASTContext::getProcessIDType() const {
Index: include/clang/Basic/TargetInfo.h
===================================================================
--- include/clang/Basic/TargetInfo.h
+++ include/clang/Basic/TargetInfo.h
@@ -248,6 +248,9 @@
   IntType getPtrDiffType(unsigned AddrSpace) const {
     return AddrSpace == 0 ? PtrDiffType : getPtrDiffTypeV(AddrSpace);
   }
+  IntType getUnsignedPtrDiffType(unsigned AddrSpace) const {
+    return getCorrespondingUnsignedType(getPtrDiffType(AddrSpace));
+  }
   IntType getIntPtrType() const { return IntPtrType; }
   IntType getUIntPtrType() const {
     return getCorrespondingUnsignedType(IntPtrType);
Index: include/clang/AST/ASTContext.h
===================================================================
--- include/clang/AST/ASTContext.h
+++ include/clang/AST/ASTContext.h
@@ -1489,6 +1489,10 @@
   /// <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
   QualType getPointerDiffType() const;
 
+  /// \brief Return the unique unsigned counterpart of 
+  /// "ptrdiff_t" integer type.
+  QualType getUnsignedPointerDiffType() const;
+
   /// \brief Return the unique type for "pid_t" defined in
   /// <sys/types.h>. We need this to compute the correct type for vfork().
   QualType getProcessIDType() const;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D38270.116633.patch
Type: text/x-patch
Size: 1886 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170926/5e865dc1/attachment-0001.bin>


More information about the cfe-commits mailing list