Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [cdt-dev] How to differenciate a call toafunctionfromareferencingits address?

> As written before, to figure out the role of a nested macro expansion
> you can look at
> its ImageLocation.

I'm sorry, Marcus, what is that "nested macro expansion" you are
talking about? Did you mean the nested macro reference?
Let me explain what happens with the following source code:

#define X 15
#define MY_FUNC(p, a1, a2) p->func(a1, a2)

struct myStruct
{
  void (*func)(int x, int y);
};

int main()
{
  struct myStruct p = { ... };
  MY_FUNC(p, 10, X);
}

I'm trying to parse the MY_FUNC function call expression's argiments
('index' is the argument number I'm interested in):

IASTExpression parameter = null;
IASTExpression[] params =
((IASTExpressionList)paramExpression).getExpressions();
if( params != null && params.length > index )
{
	parameter = params[index];
	IASTNodeLocation[] macroLocations = paramExpression.getNodeLocations();
	if( macroLocations.length > 0 && macroLocations[0] instanceof
IASTMacroExpansionLocation)
	{
		IASTPreprocessorMacroExpansion macroExpansion =
((IASTMacroExpansionLocation)macroLocations[0]).getExpansion();
		IASTName[] nested = macroExpansion.getNestedMacroReferences();
		for(int i = 0; i < nested.length; i++)
		{
			IASTImageLocation imageLoc = nested[i].getImageLocation();
			if( imageLoc == null || imageLoc.getLocationKind() !=
IASTImageLocation.ARGUMENT_TO_MACRO_EXPANSION)
				continue;
                        ... // further processing.
		}
	}
}

The problem here is that there is no way to get macro expansion for
nested macro name: 'nested' array contains only ASTMacroReferenceName
which seem cannot be mapped into macro expansion.

Moreover, AST unit seem do not contain macro expansion for X, there is
only MY_FUNC macro expansion.

>
> I am not sure that the problem you are trying to solve makes sense in
> the general
> case of macro expansions.

Well, what I'm trying to do is to find the real relationship between
macro and its nested macro, nothing more.

> There is no strict hierarchy for nested macro
> expansions,
> basically because expansions can nest in two ways:
> (a) arguments of macros in most cases are expanded before substitution.
> (b) after substitution the result is rescanned and further expansions
>    may be performed.
> (*) To make things more complicated you need to consider additional
> tokens from the
>    input when rescanning the expansion.
>
> Example (1)
> #define add operator+
> #define apply(f, x, y) f(x,y)
> apply(add, 1, 2)  // expands to operator+(1,2), 'add' expanded before
> 'apply'
>
> Example (2)
> #define add(x,y) x+y
> #define apply(f, x, y) f(x,y)
> apply(add, 1, 2)  // expands to 1+2, 'add' expanded after 'apply'
>
> Example (*):
> #define open a(
> #define a(x) x+1
> open y)   // expands to y+1

I do not clearly understand why these example cannot be treated as
nested macro expansions. The expansion order should not be related to
nesting hierarhy, on my mind.
I suppose Example 1 and 2 are clear: 'apply' contains the nested
macro. As for Example * we should talk about macro expansion
overlapping. In any case, there should be 2 macro expansions in the
AST unit, I believe. Having that, getNodeLocations() for 'y' node (or
for 'y+1' expression) should return 2 macro expansion locations...

>> Another question is what are the meaning of
>> ASTMacroExpansionLocation's offset and length? How can I use
>> those values?

I was just trying to find a solution for my problem. Is it possible
somehow to figure out the macro expansion location for nested macro
reference? I did yet not find any way.

It looks the best way is to parse the raw signature manually...

Dmitry


Back to the top