Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [cdt-dev] How to figure out which function is using macro withindex only

2008/10/10 Schorn, Markus <Markus.Schorn@xxxxxxxxxxxxx>:
> To figure out the relationship between a macro expansion and function
> definitions you'd need to know the file-offsets of the function
> definition.

I've used the offsets of the names returned by IIndexFile.findNames().
Assumtions was that
1. this list is ordered by offset.
2. macro names are at the end of the list
Here is the algorithm (last argument is supposed to be
PDOMMacroReferenceName or PDOMMacroDefinitionName. I do not use this
method for other names)
private IIndexName getEnclosingFunction(IIndex index, IIndexFile
indexFile, IIndexName indexName) {
		IIndexName func = null;
		try
		{
			IIndexName[] names = indexFile.findNames(0, Integer.MAX_VALUE);
			int offset = indexName.getNodeOffset();
			for(int i = 0; i < names.length; i++)
			{
				int nextNameOffset = (i + 1 < names.length) ?
names[i+1].getNodeOffset() : Integer.MAX_VALUE;
				if( nextNameOffset < names[i].getNodeOffset() ) nextNameOffset =
Integer.MAX_VALUE; // next item in the list starts macro list
				if( offset >= names[i].getNodeOffset() && offset < nextNameOffset )
				{
					IBinding binding = index.findBinding(names[i]);
					// If preceding name is function definition, return it
					if( names[i].isDefinition() && binding instanceof IFunction )
					{
						func = names[i];
					}
					else
					{
						IIndexName eclosingName = names[i].getEnclosingDefinition();
						binding = index.findBinding(eclosingName);
						if( eclosingName.isDefinition() && binding instanceof IFunction )
						{
							func = eclosingName;
						}
					}
					break;
				}
			}
		}
		catch (CoreException e) {}
		return func;
	}

Perhaps it could fail in some cases but I had tested it only on simple examples.
Comments are welcome!

void f1() {
  CALL("bla");  // ok, CALL is used within f1().
}
> TINT f2() {}     // is TINT used within f2()??
> ENTIRE_DEF       // ENTIRE_DEF is used to define func().
> OVERLAP          // OVERLAP is used to define getBla(), setBla(int) and
> more.

Ok, I got it. The problem of index is that I cannot understand any of
these relationships (including just simple usage in the function body)
easily...
I cannot even get exact names used in macro which is function style
macro, right?

Dmitry


Back to the top