[clang] b99e2c5 - [clang][AVR] Redefine [u]int16_t to be compatible with avr-gcc

Ben Shi via cfe-commits cfe-commits at lists.llvm.org
Mon May 17 16:06:23 PDT 2021


Author: Ben Shi
Date: 2021-05-18T07:06:12+08:00
New Revision: b99e2c56166a3a74ac9c49512c584d5b158ac9c6

URL: https://github.com/llvm/llvm-project/commit/b99e2c56166a3a74ac9c49512c584d5b158ac9c6
DIFF: https://github.com/llvm/llvm-project/commit/b99e2c56166a3a74ac9c49512c584d5b158ac9c6.diff

LOG: [clang][AVR] Redefine [u]int16_t to be compatible with avr-gcc

Reviewed By: efriedma

Differential Revision: https://reviews.llvm.org/D102547

Added: 
    

Modified: 
    clang/include/clang/Basic/TargetInfo.h
    clang/lib/Basic/TargetInfo.cpp
    clang/lib/Basic/Targets/AVR.cpp
    clang/lib/Basic/Targets/AVR.h
    clang/lib/Frontend/InitPreprocessor.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Basic/TargetInfo.h b/clang/include/clang/Basic/TargetInfo.h
index 532ff4554656c..b55a99051cfbd 100644
--- a/clang/include/clang/Basic/TargetInfo.h
+++ b/clang/include/clang/Basic/TargetInfo.h
@@ -129,9 +129,9 @@ struct TransferrableTargetInfo {
     Float128
   };
 protected:
-  IntType SizeType, IntMaxType, PtrDiffType, IntPtrType, WCharType,
-          WIntType, Char16Type, Char32Type, Int64Type, SigAtomicType,
-          ProcessIDType;
+  IntType SizeType, IntMaxType, PtrDiffType, IntPtrType, WCharType, WIntType,
+      Char16Type, Char32Type, Int64Type, Int16Type, SigAtomicType,
+      ProcessIDType;
 
   /// Whether Objective-C's built-in boolean type should be signed char.
   ///
@@ -351,6 +351,10 @@ class TargetInfo : public virtual TransferrableTargetInfo,
   IntType getUInt64Type() const {
     return getCorrespondingUnsignedType(Int64Type);
   }
+  IntType getInt16Type() const { return Int16Type; }
+  IntType getUInt16Type() const {
+    return getCorrespondingUnsignedType(Int16Type);
+  }
   IntType getSigAtomicType() const { return SigAtomicType; }
   IntType getProcessIDType() const { return ProcessIDType; }
 

diff  --git a/clang/lib/Basic/TargetInfo.cpp b/clang/lib/Basic/TargetInfo.cpp
index 2f200a9bb3e8a..e73b4a3a40c74 100644
--- a/clang/lib/Basic/TargetInfo.cpp
+++ b/clang/lib/Basic/TargetInfo.cpp
@@ -98,6 +98,7 @@ TargetInfo::TargetInfo(const llvm::Triple &T) : TargetOpts(), Triple(T) {
   Char16Type = UnsignedShort;
   Char32Type = UnsignedInt;
   Int64Type = SignedLongLong;
+  Int16Type = SignedShort;
   SigAtomicType = SignedInt;
   ProcessIDType = SignedInt;
   UseSignedCharForObjCBool = true;

diff  --git a/clang/lib/Basic/Targets/AVR.cpp b/clang/lib/Basic/Targets/AVR.cpp
index 86fda19869a86..e87b7338c4d6e 100644
--- a/clang/lib/Basic/Targets/AVR.cpp
+++ b/clang/lib/Basic/Targets/AVR.cpp
@@ -309,8 +309,6 @@ void AVRTargetInfo::getTargetDefines(const LangOptions &Opts,
   Builder.defineMacro("__AVR__");
   Builder.defineMacro("__ELF__");
   Builder.defineMacro("__flash", "__attribute__((address_space(1)))");
-  Builder.defineMacro("__UINT16_TYPE__", "unsigned int");
-  Builder.defineMacro("__INT16_TYPE__", "int");
 
   if (!this->CPU.empty()) {
     auto It = llvm::find_if(

diff  --git a/clang/lib/Basic/Targets/AVR.h b/clang/lib/Basic/Targets/AVR.h
index 94f006ee1b8a6..89a80ca6a39a3 100644
--- a/clang/lib/Basic/Targets/AVR.h
+++ b/clang/lib/Basic/Targets/AVR.h
@@ -52,6 +52,7 @@ class LLVM_LIBRARY_VISIBILITY AVRTargetInfo : public TargetInfo {
     IntPtrType = SignedInt;
     Char16Type = UnsignedInt;
     WIntType = SignedInt;
+    Int16Type = SignedInt;
     Char32Type = UnsignedLong;
     SigAtomicType = SignedChar;
     resetDataLayout("e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8");

diff  --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp
index 41b08ed56134a..163c77b04c139 100644
--- a/clang/lib/Frontend/InitPreprocessor.cpp
+++ b/clang/lib/Frontend/InitPreprocessor.cpp
@@ -216,6 +216,11 @@ static void DefineExactWidthIntType(TargetInfo::IntType Ty,
   if (TypeWidth == 64)
     Ty = IsSigned ? TI.getInt64Type() : TI.getUInt64Type();
 
+  // Use the target specified int16 type when appropriate. Some MCU targets
+  // (such as AVR) have definition of [u]int16_t to [un]signed int.
+  if (TypeWidth == 16)
+    Ty = IsSigned ? TI.getInt16Type() : TI.getUInt16Type();
+
   const char *Prefix = IsSigned ? "__INT" : "__UINT";
 
   DefineType(Prefix + Twine(TypeWidth) + "_TYPE__", Ty, Builder);


        


More information about the cfe-commits mailing list