How to visit an OpenCL kernel source code using Clang library


I’m trying to visit an OpenCL kernal source code using Clang library in order to do some source-to-source transformations in it. Now, my program is able to visit and transform C standard code, but when I pass into it some .cl file, it simply does nothing. Of course, I have installed OpenCL in the system I’m using, even I’ve tested it compiling and executing some sample programs.

I’ve been searching the Internet for a while and I’ve found some references to a way for making Clang compatible with OpenCL, but it only applies when you’re using Clang as a tool (it consist only on passing “-x cl” to the command-line execution), not as a library included in another program. I’ve also tried to add the path to OpenCL headers to my HeaderSearch (creating an instance of HeaderSearchOptions, adding the path to it with AddPath and then applying the changes with ApplyHeaderSearchOptions).

Is there any other solution to this, or I’m right but I’m forgetting any step in the process?

Thank you so much in advance,


Hi again,

Does anyone know any solution for this issue? It’s a key point in the program I’m writing now…

Thank you so much again.

Actually -x cl option can't really make it compatible to clang, rather it
informs for the compiler as the source is cl file. What you want to do by
using clang? If you want to generate llvm IR or AST, it really works.
clang -S -emit-llvm -o file -x cl this can generate LLVM IR for any
.cl file.


I need to generate the AST in order to visit its nodes and do some source-to-source transformations in some of them. But I need to do this using the Clang libraries in a program of my own, not using the clang command-line tool. Or is there some way to pass an AST generated like you are telling me into a program?



2011/11/17 eya <>

You can print or dump the AST that is generated by clang command. It is not clear what kind of your own program you are going to use. By the way do you know which function of clang is generating AST?

2011/11/17 Jorge Fernández Fabeiro <>