[cfe-commits] r77303 - in /cfe/trunk: include/clang/AST/Type.h include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaType.cpp test/Sema/address_spaces.c

John McCall rjmccall at apple.com
Mon Jul 27 23:52:18 PDT 2009


Author: rjmccall
Date: Tue Jul 28 01:52:18 2009
New Revision: 77303

URL: http://llvm.org/viewvc/llvm-project?rev=77303&view=rev
Log:
Bounds checking for address spaces.


Modified:
    cfe/trunk/include/clang/AST/Type.h
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaType.cpp
    cfe/trunk/test/Sema/address_spaces.c

Modified: cfe/trunk/include/clang/AST/Type.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Type.h?rev=77303&r1=77302&r2=77303&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Type.h (original)
+++ cfe/trunk/include/clang/AST/Type.h Tue Jul 28 01:52:18 2009
@@ -97,6 +97,9 @@
     Weak,
     Strong
   };
+
+  // 24 bits should be enough for anyone.
+  static const unsigned MaxAddressSpace = 0xffffffu;
   
   QualType() {}
   
@@ -569,6 +572,10 @@
 
 
 /// QualifierSet - This class is used to collect qualifiers.
+/// Clang supports five independent qualifiers:
+/// * C99: const, volatile, and restrict
+/// * Embedded C (TR18037): address spaces
+/// * Objective C: the GC attributes (none, weak, or strong)
 class QualifierSet {
 public:
   QualifierSet() : Mask(0) {}
@@ -653,7 +660,7 @@
   static const uint32_t GCAttrShift = 3;
   static const uint32_t AddressSpaceMask = ~(CVRMask | GCAttrMask);
   static const uint32_t AddressSpaceShift = 5;
-  static const unsigned MaxAddressSpace = ~0u >> AddressSpaceShift;
+  static const unsigned MaxAddressSpace = QualType::MaxAddressSpace;
 };
 
 

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=77303&r1=77302&r2=77303&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue Jul 28 01:52:18 2009
@@ -512,6 +512,10 @@
   "illegal vector component name '%0'">;
 def err_attribute_address_space_not_int : Error<
   "address space attribute requires an integer constant">;
+def err_attribute_address_space_negative : Error<
+  "address space is negative">;
+def err_attribute_address_space_too_high : Error<
+  "address space is larger than the maximum supported (%0)">;
 def err_attribute_address_multiple_qualifiers : Error<
   "multiple address spaces specified for type">;
 def err_implicit_pointer_address_space_cast : Error<

Modified: cfe/trunk/lib/Sema/SemaType.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaType.cpp?rev=77303&r1=77302&r2=77303&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaType.cpp (original)
+++ cfe/trunk/lib/Sema/SemaType.cpp Tue Jul 28 01:52:18 2009
@@ -1469,6 +1469,23 @@
     return;
   }
 
+  // Bounds checking.
+  if (addrSpace.isSigned()) {
+    if (addrSpace.isNegative()) {
+      S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
+        << ASArgExpr->getSourceRange();
+      return;
+    }
+    addrSpace.setIsSigned(false);
+  }
+  llvm::APSInt max(addrSpace.getBitWidth());
+  max = QualType::MaxAddressSpace;
+  if (addrSpace > max) {
+    S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
+      << QualType::MaxAddressSpace << ASArgExpr->getSourceRange();
+    return;
+  }
+
   unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue()); 
   Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
 }

Modified: cfe/trunk/test/Sema/address_spaces.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/address_spaces.c?rev=77303&r1=77302&r2=77303&view=diff

==============================================================================
--- cfe/trunk/test/Sema/address_spaces.c (original)
+++ cfe/trunk/test/Sema/address_spaces.c Tue Jul 28 01:52:18 2009
@@ -15,6 +15,12 @@
   _AS1 int array[5];  // expected-error {{automatic variable qualified with an address space}}
   _AS1 int arrarr[5][5]; // expected-error {{automatic variable qualified with an address space}}
 
+  __attribute__((address_space(-1))) int *_boundsA; // expected-error {{address space is negative}}
+  __attribute__((address_space(0xFFFFFF))) int *_boundsB;
+  __attribute__((address_space(0x1000000))) int *_boundsC; // expected-error {{address space is larger than the maximum supported}}
+  // chosen specifically to overflow 32 bits and come out reasonable
+  __attribute__((address_space(4294967500))) int *_boundsD; // expected-error {{address space is larger than the maximum supported}}
+
   *a = 5.0f;
 }
 





More information about the cfe-commits mailing list