mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
Fixed Bug 90978 - references to macros report wrong location information
Updated/cleaned up tests.
This commit is contained in:
parent
1c5c64581c
commit
d85e184a5d
4 changed files with 46 additions and 10 deletions
|
@ -248,10 +248,11 @@ public class DOMLocationMacroTests extends AST2BaseTest {
|
|||
assertEquals( flat.getNodeOffset() , code.indexOf( "_PTR _EXFUN(memchr,(const _PTR, int, size_t));")); //$NON-NLS-1$
|
||||
assertEquals( flat.getNodeLength(), "_PTR _EXFUN(memchr,(const _PTR, int, size_t));".length() ); //$NON-NLS-1$
|
||||
|
||||
IASTNodeLocation [] fullyMonty = tu.getNodeLocations();
|
||||
IASTFileLocation flatMonty = tu.flattenLocationsToFile( fullyMonty );
|
||||
assertEquals( flatMonty.getNodeOffset(), 0 );
|
||||
// assertEquals( flatMonty.getNodeLength(), code.length() );
|
||||
IASTDeclarator d = memchr.getDeclarators()[0];
|
||||
final IASTNodeLocation[] declaratorLocations = d.getNodeLocations();
|
||||
IASTFileLocation f = tu.flattenLocationsToFile( declaratorLocations );
|
||||
assertEquals( code.indexOf( "_PTR _EXFUN(memchr,(const _PTR, int, size_t))"), f.getNodeOffset() ); //$NON-NLS-1$
|
||||
assertEquals( "_PTR _EXFUN(memchr,(const _PTR, int, size_t))".length(), f.getNodeLength() ); //$NON-NLS-1$
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -299,4 +300,26 @@ public class DOMLocationMacroTests extends AST2BaseTest {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
public void testBug90978() throws Exception {
|
||||
StringBuffer buffer = new StringBuffer( "#define MACRO mm\n"); //$NON-NLS-1$
|
||||
buffer.append( "int MACRO;\n"); //$NON-NLS-1$
|
||||
String code = buffer.toString();
|
||||
for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP
|
||||
: null) {
|
||||
IASTTranslationUnit tu = parse(code, p);
|
||||
IASTPreprocessorObjectStyleMacroDefinition MACRO = (IASTPreprocessorObjectStyleMacroDefinition) tu.getMacroDefinitions()[0];
|
||||
IASTName macro_name = MACRO.getName();
|
||||
IMacroBinding binding = (IMacroBinding) macro_name.resolveBinding();
|
||||
IASTName [] references = tu.getReferences( binding );
|
||||
assertEquals( references.length, 1 );
|
||||
IASTName reference = references[0];
|
||||
IASTNodeLocation [] nodeLocations = reference.getNodeLocations();
|
||||
assertEquals( nodeLocations.length, 1 );
|
||||
assertTrue( nodeLocations[0] instanceof IASTFileLocation );
|
||||
IASTFileLocation loc = (IASTFileLocation) nodeLocations[0];
|
||||
assertEquals( code.indexOf( "int MACRO") + "int ".length(), loc.getNodeOffset() ); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
assertEquals( "MACRO".length(), loc.getNodeLength() ); //$NON-NLS-1$
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,7 +30,6 @@ public class DOMParserTestSuite extends TestCase {
|
|||
suite.addTestSuite( AST2TemplateTests.class );
|
||||
suite.addTestSuite( QuickParser2Tests.class );
|
||||
suite.addTestSuite( CompleteParser2Tests.class );
|
||||
// suite.addTestSuite( DOMScannerTests.class );
|
||||
suite.addTestSuite( DOMLocationTests.class );
|
||||
suite.addTestSuite( DOMLocationMacroTests.class );
|
||||
suite.addTest( DOMLocationInclusionTests.suite() );
|
||||
|
|
|
@ -2064,8 +2064,8 @@ abstract class BaseScanner implements IScanner {
|
|||
char[] expText = expMacro.expansion;
|
||||
if (expText.length > 0)
|
||||
pushContext(expText, new MacroData(
|
||||
bufferPos[bufferStackPos] - expMacro.name.length
|
||||
+ 1, bufferPos[bufferStackPos], expMacro));
|
||||
bufferPos[bufferStackPos] - expMacro.name.length + 1,
|
||||
bufferPos[bufferStackPos], expMacro));
|
||||
} else if (expObject instanceof DynamicStyleMacro) {
|
||||
DynamicStyleMacro expMacro = (DynamicStyleMacro) expObject;
|
||||
char[] expText = expMacro.execute();
|
||||
|
|
|
@ -979,8 +979,10 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
|
|||
}
|
||||
|
||||
public boolean containsInDirective(int offset, int length) {
|
||||
if( length > 0 && offset == context_directive_end )
|
||||
return false;
|
||||
if (offset >= context_directive_start
|
||||
&& offset + length - 1<= context_directive_end)
|
||||
&& offset + length - 1 <= context_directive_end )
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
@ -1222,7 +1224,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
|
|||
public IASTPreprocessorMacroDefinition astNode;
|
||||
|
||||
private IMacroBinding bind;
|
||||
|
||||
|
||||
public char[] getName() {
|
||||
return name;
|
||||
}
|
||||
|
@ -1316,7 +1318,7 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
|
|||
expansionName = new ASTMacroName( definition.getName() );
|
||||
((ScannerASTNode)expansionName).setParent( rootNode );
|
||||
((ScannerASTNode)expansionName).setPropertyInParent( IASTTranslationUnit.EXPANSION_NAME );
|
||||
((ScannerASTNode)expansionName).setOffsetAndLength( context_directive_start, context_directive_end - context_directive_start);
|
||||
((ScannerASTNode)expansionName).setOffsetAndLength( context_directive_start, context_directive_end - context_directive_start + 1);
|
||||
}
|
||||
return expansionName;
|
||||
}
|
||||
|
@ -1732,6 +1734,18 @@ public class LocationMap implements ILocationResolver, IScannerPreprocessorLog {
|
|||
}
|
||||
if (c instanceof _MacroExpansion) {
|
||||
_MacroExpansion expansion = (_MacroExpansion) c;
|
||||
//first check to see if we are in the directive rather than the expansion
|
||||
if( c.containsInDirective( offset, length ) )
|
||||
{
|
||||
_CompositeContext parent = c.parent;
|
||||
while (!(parent instanceof _CompositeFileContext))
|
||||
parent = c.parent;
|
||||
_CompositeFileContext fc = (_CompositeFileContext) parent;
|
||||
return new FileLocation(fc.reader.filename, reconcileOffset(fc,
|
||||
c, offset), length);
|
||||
}
|
||||
|
||||
|
||||
IASTNodeLocation[] locations = createSoleLocationArray(c.parent,
|
||||
c.context_directive_start, c.context_directive_end
|
||||
- c.context_directive_start + 1);
|
||||
|
|
Loading…
Add table
Reference in a new issue