From 1e898d911cbd6f464ac6d4c2a9f63bd6c4abdb80 Mon Sep 17 00:00:00 2001 From: Alexander Shaposhnikov Date: Fri, 14 Jul 2017 17:30:14 +0000 Subject: [PATCH] [clang] Add getSignedSizeType method C11 standard refers to the signed counterpart of the type size_t in the paragraph 7.21.6.1 where it defines d, i, o, u, x, or x conversion specifiers (in printf format string). In Clang there is a FIXME (in lib/Analysis/PrintfFormatString.cpp) for this case (which is not handled correctly at the moment). This diff adds getSignedSizeType method to TargetInfo and exposes it in ASTContext similarly to how it is done for getSizeType. lib/Analysis/PrintfFormatString.cpp will be changed in a separate commit. Differential revision: https://reviews.llvm.org/D35378 Test plan: make check-all llvm-svn: 308037 --- clang/include/clang/AST/ASTContext.h | 4 ++++ clang/include/clang/Basic/TargetInfo.h | 14 ++++++++++++++ clang/lib/AST/ASTContext.cpp | 6 ++++++ 3 files changed, 24 insertions(+) diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h index 3b46d31458cee..703f588c56635 100644 --- a/clang/include/clang/AST/ASTContext.h +++ b/clang/include/clang/AST/ASTContext.h @@ -1441,6 +1441,10 @@ class ASTContext : public RefCountedBase { /// The sizeof operator requires this (C99 6.5.3.4p4). CanQualType getSizeType() const; + /// \brief Return the unique signed counterpart of + /// the integer type corresponding to size_t. + CanQualType getSignedSizeType() const; + /// \brief Return the unique type for "intmax_t" (C99 7.18.1.5), defined in /// . CanQualType getIntMaxType() const; diff --git a/clang/include/clang/Basic/TargetInfo.h b/clang/include/clang/Basic/TargetInfo.h index 5885532b91db6..d1a9ea85dbe96 100644 --- a/clang/include/clang/Basic/TargetInfo.h +++ b/clang/include/clang/Basic/TargetInfo.h @@ -226,6 +226,20 @@ class TargetInfo : public RefCountedBase { public: IntType getSizeType() const { return SizeType; } + IntType getSignedSizeType() const { + switch (SizeType) { + case UnsignedShort: + return SignedShort; + case UnsignedInt: + return SignedInt; + case UnsignedLong: + return SignedLong; + case UnsignedLongLong: + return SignedLongLong; + default: + llvm_unreachable("Invalid SizeType"); + } + } IntType getIntMaxType() const { return IntMaxType; } IntType getUIntMaxType() const { return getCorrespondingUnsignedType(IntMaxType); diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp index fd9723298fca9..c60373c5a90a4 100644 --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/lib/AST/ASTContext.cpp @@ -4525,6 +4525,12 @@ CanQualType ASTContext::getSizeType() const { return getFromTargetType(Target->getSizeType()); } +/// Return the unique signed counterpart of the integer type +/// corresponding to size_t. +CanQualType ASTContext::getSignedSizeType() const { + return getFromTargetType(Target->getSignedSizeType()); +} + /// getIntMaxType - Return the unique type for "intmax_t" (C99 7.18.1.5). CanQualType ASTContext::getIntMaxType() const { return getFromTargetType(Target->getIntMaxType());