[cfe-dev] parsing code snippets

Olaf Krzikalla Olaf.Krzikalla at tu-dresden.de
Thu May 5 00:43:31 PDT 2011


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.


Best regards
Olaf Krzikalla



More information about the cfe-dev mailing list