public void testMacroBindings() throws Exception { StringBuffer buffer = new StringBuffer("#define ABC def\n"); // $NON-NLS-1$ buffer.append("int ABC;\n"); // $NON-NLS-1$ buffer.append("#undef ABC\n"); // $NON-NLS-1$ buffer.append("#define ABC ghi\n"); // $NON-NLS-1$ buffer.append("int ABC;\n"); // $NON-NLS-1$ String code = buffer.toString(); for (ParserLanguage language : languages) { IASTTranslationUnit tu = parse(code, language); IASTPreprocessorMacroDefinition[] macros = tu.getMacroDefinitions(); assertEquals(macros.length, 2); IASTPreprocessorObjectStyleMacroDefinition ABC1 = (IASTPreprocessorObjectStyleMacroDefinition) macros[0]; IASTPreprocessorObjectStyleMacroDefinition ABC2 = (IASTPreprocessorObjectStyleMacroDefinition) macros[1]; IMacroBinding binding1 = (IMacroBinding) ABC1.getName().resolveBinding(); assertNotNull(binding1); IMacroBinding binding2 = (IMacroBinding) ABC2.getName().resolveBinding(); assertNotNull(binding2); assertNotSame(binding1, binding2); IASTName[] firstReferences = tu.getReferences(binding1); IASTName[] firstDeclarations = tu.getDeclarationsInAST(binding1); assertEquals(firstReferences.length, 2); assertEquals( firstReferences[0].getPropertyInParent(), IASTPreprocessorMacroExpansion.EXPANSION_NAME); assertEquals(firstReferences[0].getParent().getParent(), tu); assertEquals(firstReferences[1].getPropertyInParent(), IASTPreprocessorStatement.MACRO_NAME); assertTrue(firstReferences[1].getParent() instanceof IASTPreprocessorUndefStatement); assertEquals(firstDeclarations.length, 1); assertSame(ABC1.getName(), firstDeclarations[0]); IASTName[] secondReferences = tu.getReferences(binding2); IASTName[] secondDeclarations = tu.getDeclarationsInAST(binding2); assertEquals(1, secondReferences.length); assertEquals( secondReferences[0].getPropertyInParent(), IASTPreprocessorMacroExpansion.EXPANSION_NAME); assertEquals(secondReferences[0].getParent().getParent(), tu); assertSame(ABC2.getName(), secondDeclarations[0]); } }
private IName[] findDeclarations(IIndex index, IASTTranslationUnit ast, IBinding binding) throws CoreException { IName[] declNames = ast.getDeclarationsInAST(binding); for (int i = 0; i < declNames.length; i++) { IName name = declNames[i]; if (name.isDefinition()) declNames[i] = null; } declNames = (IName[]) ArrayUtil.removeNulls(IName.class, declNames); if (declNames.length == 0) { declNames = index.findNames( binding, IIndex.FIND_DECLARATIONS | IIndex.SEARCH_ACROSS_LANGUAGE_BOUNDARIES); } return declNames; }