[PATCH] Fix error when initializing a variable with address_space without a cv-qualifier in C++ mode

Matt Arsenault whatmannerofburgeristhis at gmail.com
Tue Feb 19 15:44:11 PST 2013


Hi,

I encountered this error when trying to use address_space when compiling as C++, but not C.

// No error
__attribute__((address_space(42)))
const float withc = 1.0f;
 
// No error
__attribute__((address_space(42)))
volatile float withv = 1.0f;
 
// Error
__attribute__((address_space(42)))
float nocv = 1.0f;


This would produce the error:

as_initializer.cpp:12:7: error: cannot initialize a variable of type
      '__attribute__((address_space(42))) float' with an rvalue of type 'float'
float nocv = 1.0f;
      ^      ~~~~
1 error generated.

This patch adds a test for this case, and allows the conversion if the address spaces are different for non-pointer types. I'm not really familiar with the code so I'm not particularly confident that this is the correct fix. Please review!

http://llvm-reviews.chandlerc.com/D426

Files:
  lib/Sema/SemaOverload.cpp
  test/SemaCXX/address-space-initialize.cpp

Index: lib/Sema/SemaOverload.cpp
===================================================================
--- lib/Sema/SemaOverload.cpp
+++ lib/Sema/SemaOverload.cpp
@@ -1678,7 +1678,7 @@
         (CanonFrom.getLocalCVRQualifiers() != CanonTo.getLocalCVRQualifiers()
          || CanonFrom.getObjCGCAttr() != CanonTo.getObjCGCAttr()
          || CanonFrom.getObjCLifetime() != CanonTo.getObjCLifetime()
-         || (CanonFrom->isSamplerT() && 
+         || (!CanonFrom->isAnyPointerType() &&
            CanonFrom.getAddressSpace() != CanonTo.getAddressSpace()))) {
       FromType = ToType;
       CanonFrom = CanonTo;
Index: test/SemaCXX/address-space-initialize.cpp
===================================================================
--- /dev/null
+++ test/SemaCXX/address-space-initialize.cpp
@@ -0,0 +1,24 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+
+__attribute__((address_space(42)))
+const float withc = 1.0f;
+
+__attribute__((address_space(42)))
+volatile float withv = 1.0f;
+
+__attribute__((address_space(42)))
+float nocv = 1.0f;
+
+__attribute__((address_space(42)))
+float nocv_array[10] = { 1.0f };
+
+__attribute__((address_space(42)))
+int nocv_iarray[10] = { 4 };
+
+
+__attribute__((address_space(9999)))
+int* as_ptr = nocv_iarray; // expected-error{{cannot initialize a variable of type '__attribute__((address_space(9999))) int *' with an lvalue of type '__attribute__((address_space(42))) int [10]'}}
+
+
+
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D426.1.patch
Type: text/x-patch
Size: 1436 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130219/b7589c2f/attachment.bin>


More information about the cfe-commits mailing list