[cfe-dev] parsing code snippets

Douglas Gregor dgregor at apple.com
Thu May 5 07:01:05 PDT 2011


On May 5, 2011, at 12:43 AM, Olaf Krzikalla wrote:

> Hi @clang,
> 
> is there any way to parse code snippets wrt. to e.g. a function context?
> Here is the practical problem:
> 
> void foo(float* a)
> {
> #pragma scout vectorize aligned(a)
>   for (int i = 0; i < 100; ++i)
>     a[i] = 0.0;
> }
> 
> The argument for "aligned" is an expression, that is expected to be 
> well-formed in the function context. Now the idea is to parse the 
> argument, then compute the MemRegion it refers to and eventually align 
> all accesses to SubRegions of the given MemRegion (the MemRegion part is 
> already done).
> As long as the argument is an identifier only, retrieving the expression 
> is easy. But of course I want to support other expressions (including 
> member and array subscript expressions) too.
> I'm afraid that there is no way to achieve this using clang means, 
> because there is no way to access an ASTContext from a PragmaHandler.
> The opther way would be to create a Sema object, set its state "somehow" 
> to the particular function DeclContext and the input stream to the 
> argument string and then call ParseExpression. But Sema seems not to be 
> prepared for such a task either.
> However before I write my own stripped-down parser I better ask here. 
> Maybe I've overlooked something.

Why not turn the "#pragma scout vectorize" token sequence into a special token (tok::pragma_scout_vectorize), then have the parser recognize that token (in whatever contexts is makes sense, e.g., a statement context for this case) and handle the actual #pragma parsing?

	- Doug



More information about the cfe-dev mailing list