[cfe-dev] Intrinsic/Custom Function Creation with Pragmas

Douglas Gregor dgregor at apple.com
Mon Jan 18 09:03:22 PST 2010

On Jan 13, 2010, at 2:36 PM, Prakash Prabhu wrote:
> An abstract lock condition could be specified as follows:
> #pragma AbstractLockCondn(AbstractLockName(param1, param2, ...), (<C  
> expression involving params>))
> As an example, this is a way by which one could say that a abstract  
> lock L1 is conditioned on variables x and y, where x and y are  
> variables:
> #pragma AbstractLockCondn(L1(x,y), (x>y))
> What I would like to do is to create a function like the following:
> int __L1_Test__(<type of x> x, <type of y> y)
> {
>   return (x>y);
> }
> Basically, I can write a #pragma handler in ParsePragma.cpp which  
> will parse the parameters 1 to n. Much as I would like to invoke the  
> Clang Parser for parsing the C expression and returning a AST node  
> (root of expression tree) for the expression, I am doubtful if that  
> is possible:
> I will not know the type of the parameters until I see their use  
> sites, at which point I will look up the types of the variables in  
> the declaration context having the use sites. So I can't expand the  
> macro at the point where it is parsed.

You could know the types of the variables when they are defined, if  
you checked for the presence of this #pragma in ActOnVariableDeclarator.

> What I can do at this point, is to push this information into Sema  
> object.


> However, I would know the types at the end of the translation unit  
> at which point I could potentially generate a function declaration  
> in string and if possible, insert it into the Preprocessor/ 
> TokenLexer stream and then invoke the parser for parsing this newly  
> generated function. Is this insertion into the preprocessor/ 
> TokenLexer stream possible ? Specifically just before the call to  
> Actions.ActOnEndOfTranslationUnit() in Parser::ParseTopLevelDecl 
> (DeclGroupPtrTy &Result), if I can insert the generated function &  
> its body into the TokenLexer and have the parser process it by  
> calling ParseExternalDeclaration(). So my question is: Is it  
> possible to insert the string containing the function body into  
> token/preprocessor stream and have the parser automatically handle  
> it as if it were a function defined by the programmer ?

You can probably teach the Preprocessor's EnterTokenStream function to  
do this.

	- Doug

More information about the cfe-dev mailing list