[cfe-dev] AST and Sema: find out if a variable with a name is visible in a particular scope

Douglas Gregor dgregor at apple.com
Mon Oct 22 11:23:10 PDT 2012


On Oct 22, 2012, at 6:44 AM, GabrieleCocco <cocco at di.unipi.it> wrote:

> Hi to everybody.
> I'm trying to build a source to source transformation in Clang. In
> particular, I'm trying to generate OpenCL host-side code (create platform,
> context, buffers, transfer data to and from the kernel, etc.) using
> informations stored in the AST and custom attributes.
> My target is to allow programmers to write kernels as C functions (relying
> on OpenCL syntax support) and to avoid to code a big part of the host-side
> code needed to set up devices, contexts, transfer data and so on. The tool
> I'm developing should:
> 
> 1) Lift away the functions marked with "kernel" attribute (call the kernel
> functions) from the translation unit, putting them into .cl files
> 2) Substitute each kernel function with a "wrapper function" that does the
> appropriate invocation of the OpenCL kernel (setKernelArg,
> enqueueNDRangeKernel, ...) 
> 3) Replace each invocation of a kernel function with an invocation of the
> corresponding wrapper function, injecting the appropriate OpenCL API
> functions (create buffer, write buffer, read buffer after the call) before
> and after the call.
> 
> The problem I'm facing by now is pretty simple, but I'm not sure about the
> best way to solve it. Since I'm injecting new declarations, such as
> "cl_context cntx = clCreateContext(....)", I've to deal with variable names
> potentially already defined somewhere and visible in the scope where I'm
> injecting code.
> Can you suggest me a way to find out if a var name is visible in a
> particular scope and/or a way to produce (if clang provides a tool/api to do
> that) an unique ID in a particular scope?


Sema can do it via Sema::LookupName. Unfortunately, there's no way to do it from the AST.

	- Doug





More information about the cfe-dev mailing list