Parameter check question

Hello,

I want to do a check on function parameters similar to the “err_arg_with_address_space” check that I added in ActOnParamDeclarator (r83165). However, this time, I only want to perform the check if the function has a specific attribute. Is ActOnParamDeclarator the right place? I’m not seeing a way to get the function attribute there. Or, any suggestion on where to add this?

Thanks,
Tanya

ActOnParamDeclarator won’t work, since that gets can get called by the parser before we’ve seen the function attributes. I suggest putting this check into ActOnFunctionDeclarator (e.g., where we check for parameters of “void” type).

  • Doug

Awesome, thanks! At this point in ActOnFunctionDeclarator, do parameters know about their attributes? Specifically addr space qualifiers. I’m thinking no because it always seems to think the param’s in addr space 0, but when I dump the QualType I can clearly see the addr space attribute and its non zero, but I assume it just hasn’t been processed yet.

Is there place where both function and params have had their attributes seen that I can add a check?

Sorry, new to parsers :slight_smile:

-Tanya

Tanya Lattner wrote:

The address-space qualifier has probably migrated to the type of the parameter, rather than sitting on the parameter itself. You can use QualType::getAddressSpace() to figure out the address space of the parameter.

  • Doug

Tanya Lattner wrote:

Hello,

I want to do a check on function parameters similar to the “err_arg_with_address_space” check that I added in ActOnParamDeclarator (r83165). However, this time, I only want to perform the check if the function has a specific attribute. Is ActOnParamDeclarator the right place? I’m not seeing a way to get the function attribute there. Or, any suggestion on where to add this?

ActOnParamDeclarator won’t work, since that gets can get called by the parser before we’ve seen the function attributes. I suggest putting this check into ActOnFunctionDeclarator (e.g., where we check for parameters of “void” type).

Awesome, thanks! At this point in ActOnFunctionDeclarator, do parameters know about their attributes? Specifically addr space qualifiers. I’m thinking no because it always seems to think the param’s in addr space 0, but when I dump the QualType I can clearly see the addr space attribute and its non zero, but I assume it just hasn’t been processed yet.

ActOnFunctionDeclarator doesn’t get called until all the parameters have finished parsing, so all the attributes, address spaces, etc. should be present.

Can you post a code example where it thinks the parameter is in address space 0 (I assume getAddressSpace() is returning 0)?

Yes, I’m using getAddressSpace().

Here is a simple example:
attribute((annotate(“random”))) void foo(attribute((address_space(1))) int *x);

At the end of ActOnFunctionDeclarator (right before the return) is where I have my check.

for (unsigned p = 0, NumParams = NewFD->getNumParams();
p < NumParams; ++p) {
ParmVarDecl *Param = NewFD->getParamDecl(p);
QualType T = Param->getType();
if (T.getAddressSpace() == 0) {
// do stuff

}
}

For some reason, getAddressSpace() is returning 0.

(gdb) p T.getAddressSpace()
$1 = 0
(gdb) p T.dump()
: int attribute((address_space(1)))*identifier
$2 = void

-Tanya

$ cat as.c
__attribute__((annotate("random"))) void foo(__attribute__((address_space(1))) int *x) {
   *x = 0;
   x = 0;
}
$ /Developer/usr/bin/clang -emit-llvm -S as.c -o -
; ModuleID = 'as.c'
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128"
target triple = "x86_64-apple-darwin10.0"

define void @foo(i32 addrspace(1)* %x) nounwind ssp {
   %x.addr = alloca i32 addrspace(1)* ; <i32 addrspace(1)**> [#uses=3]
   store i32 addrspace(1)* %x, i32 addrspace(1)** %x.addr
   %1 = load i32 addrspace(1)** %x.addr ; <i32 addrspace(1)*> [#uses=1]
   store i32 0, i32 addrspace(1)* %1
   store i32 addrspace(1)* null, i32 addrspace(1)** %x.addr
   ret void
}

The 1 is on the thing that x points to, not x itself?

The address space qualifier here applies to the “int”, not to the “int*”. If you get T as a PointerType (T->getAs()) and look at its pointee type, you’ll see an address-space-qualified “int”.

  • Doug

Yes, you are correct. Sorry, my mistake! It is doing the right thing.

Sorry for the noise.

-Tanya

Tanya Lattner wrote: