]> granicus.if.org Git - clang/commitdiff
Restrict vector component access (using "." and "[]") to variables.
authorSteve Naroff <snaroff@apple.com>
Fri, 3 Aug 2007 22:40:33 +0000 (22:40 +0000)
committerSteve Naroff <snaroff@apple.com>
Fri, 3 Aug 2007 22:40:33 +0000 (22:40 +0000)
Chris suggested this, since it simplifies the code generator.
If this features is needed (and we don't think it is), we can revisit.

The following test case now produces an error.
[dylan:~/llvm/tools/clang] admin% cat t.c

typedef __attribute__(( ocu_vector_type(4) )) float float4;

static void test() {
    float4 vec4;

    vec4.rg.g;
    vec4.rg[1];
}
[dylan:~/llvm/tools/clang] admin% ../../Debug/bin/clang t.c
t.c:8:12: error: vector component access limited to variables
    vec4.rg.g;
           ^~
t.c:9:12: error: vector component access limited to variables
    vec4.rg[1];
           ^~~
2 diagnostics generated.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@40795 91177308-0d34-0410-b5e6-96231b3b80d8

Sema/SemaExpr.cpp
include/clang/Basic/DiagnosticKinds.def
test/Parser/ocu_vector_components.c

index 7a4200632ce7bc9a87f3328c69a0cc6e8e540643..9dfcd7d5eab08f760f71402a6c25b0368e9b1fb0 100644 (file)
@@ -317,6 +317,11 @@ ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
   } else if (const VectorType *VTy = LHSTy->getAsVectorType()) {
     BaseExpr = LHSExp;    // vectors: V[123]
     IndexExpr = RHSExp;
+    
+    // Component access limited to variables (reject vec4.rg[1]).
+    if (!isa<DeclRefExpr>(BaseExpr)) 
+      return Diag(LLoc, diag::err_ocuvector_component_access, 
+                  SourceRange(LLoc, RLoc));
     // FIXME: need to deal with const...
     ResultType = VTy->getElementType();
   } else {
@@ -435,6 +440,10 @@ ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
                   SourceRange(MemberLoc));
     return new MemberExpr(BaseExpr, OpKind==tok::arrow, MemberDecl, MemberLoc);
   } else if (BaseType->isOCUVectorType() && OpKind == tok::period) {
+    // Component access limited to variables (reject vec4.rg.g).
+    if (!isa<DeclRefExpr>(BaseExpr)) 
+      return Diag(OpLoc, diag::err_ocuvector_component_access, 
+                  SourceRange(MemberLoc));
     QualType ret = CheckOCUVectorComponent(BaseType, OpLoc, Member, MemberLoc);
     if (ret.isNull())
       return true;
index 1ede4fd81548882496cd3747dab89e5d39f0c0da..b6958540be9cb5e7d69bfaa32d751146ddf4e06e 100644 (file)
@@ -449,6 +449,8 @@ DIAG(err_ocuvector_component_exceeds_length, ERROR,
      "vector component access exceeds type '%0'")
 DIAG(err_ocuvector_component_name_illegal, ERROR,
      "illegal vector component name '%0'")
+DIAG(err_ocuvector_component_access, ERROR,
+     "vector component access limited to variables")
 
 // Function Parameter Semantic Analysis.
 DIAG(err_param_with_void_type, ERROR,
index fc989329bafba05a8a37830dba6656d2d695408a..26627b85e859bf7aad1ca7143facbc7080524f36 100644 (file)
@@ -24,4 +24,6 @@ static void test() {
     vec2.xx = vec2_2.xy; // expected-error {{vector is not assignable (contains duplicate components)}}
     vec2.yx = vec2_2.xy;
     vec4 = (float4){ 1,2,3,4 };
+    vec4.rg.g; // expected-error {{vector component access limited to variables}}
+    vec4.rg[1]; // expected-error {{vector component access limited to variables}}
 }