public void testObjectStyleMacroExpansionSimpleDeclarator() throws Exception { StringBuffer buffer = new StringBuffer("#define ABC D\n"); // $NON-NLS-1$ buffer.append("int ABC;"); // $NON-NLS-1$ String code = buffer.toString(); for (ParserLanguage language : languages) { IASTTranslationUnit tu = parse(code, language); IASTPreprocessorObjectStyleMacroDefinition ABC = (IASTPreprocessorObjectStyleMacroDefinition) tu.getMacroDefinitions()[0]; IASTSimpleDeclaration var = (IASTSimpleDeclaration) tu.getDeclarations()[0]; IASTDeclarator d = var.getDeclarators()[0]; assertEquals(d.getName().toString(), "D"); // $NON-NLS-1$ IASTNodeLocation[] declaratorLocations = d.getNodeLocations(); assertEquals(declaratorLocations.length, 1); IASTMacroExpansionLocation expansion = (IASTMacroExpansionLocation) declaratorLocations[0]; IASTPreprocessorObjectStyleMacroDefinition fromExpansion = (IASTPreprocessorObjectStyleMacroDefinition) expansion.getExpansion().getMacroDefinition(); assertEqualsMacros(fromExpansion, ABC); assertEquals(expansion.getNodeOffset(), 0); assertEquals(expansion.getNodeLength(), 1); IASTNodeLocation[] macroLocation = expansion.getExpansion().getNodeLocations(); assertEquals(macroLocation.length, 1); assertTrue(macroLocation[0] instanceof IASTFileLocation); assertEquals( macroLocation[0].getNodeOffset(), code.indexOf("int ABC;") + "int ".length()); // $NON-NLS-1$ //$NON-NLS-2$ assertEquals(macroLocation[0].getNodeLength(), "ABC".length()); // $NON-NLS-1$ } }
public boolean forFriendship() { if (astName == null) return false; IASTNode node = astName.getParent(); while (node instanceof IASTName) node = node.getParent(); IASTDeclaration decl = null; IASTDeclarator dtor = null; if (node instanceof ICPPASTDeclSpecifier && node.getParent() instanceof IASTDeclaration) { decl = (IASTDeclaration) node.getParent(); } else if (node instanceof IASTDeclarator) { dtor = (IASTDeclarator) node; while (dtor.getParent() instanceof IASTDeclarator) dtor = (IASTDeclarator) dtor.getParent(); if (!(dtor.getParent() instanceof IASTDeclaration)) return false; decl = (IASTDeclaration) dtor.getParent(); } else { return false; } if (decl instanceof IASTSimpleDeclaration) { IASTSimpleDeclaration simple = (IASTSimpleDeclaration) decl; if (!((ICPPASTDeclSpecifier) simple.getDeclSpecifier()).isFriend()) return false; if (dtor != null) return true; return simple.getDeclarators().length == 0; } else if (decl instanceof IASTFunctionDefinition) { IASTFunctionDefinition fnDef = (IASTFunctionDefinition) decl; if (!((ICPPASTDeclSpecifier) fnDef.getDeclSpecifier()).isFriend()) return false; return (dtor != null); } return false; }
private void writeNestedDeclarator(IASTDeclarator funcDec) { IASTDeclarator nestedDeclarator = funcDec.getNestedDeclarator(); if (nestedDeclarator != null) { scribe.print('('); nestedDeclarator.accept(visitor); scribe.print(')'); } }
private IASTDeclarator getInnermostDeclarator(IASTDeclarator node) { IASTDeclarator nested = node.getNestedDeclarator(); while (nested != null) { node = nested; nested = node.getNestedDeclarator(); } return node; }
public void setDeclarator(IASTDeclarator declarator) { assertNotFrozen(); this.declarator = declarator; if (declarator != null) { declarator.setParent(this); declarator.setPropertyInParent(DECLARATOR); } }
@Override public void setNestedDeclarator(IASTDeclarator nested) { assertNotFrozen(); this.nested = nested; if (nested != null) { nested.setParent(this); nested.setPropertyInParent(NESTED_DECLARATOR); } }
private boolean hasNestedPointerOperators(IASTDeclarator declarator) { declarator = declarator.getNestedDeclarator(); while (declarator != null) { if (declarator.getPointerOperators().length > 0) { return true; } declarator = declarator.getNestedDeclarator(); } return false; }
protected void writeDefaultDeclarator(IASTDeclarator declarator) { IASTPointerOperator[] pointOps = declarator.getPointerOperators(); writePointerOperators(declarator, pointOps); IASTName name = declarator.getName(); name.accept(visitor); writeNestedDeclarator(declarator); IASTInitializer init = getInitializer(declarator); if (init != null) { init.accept(visitor); } }
@Override protected IASTName getASTName() { IASTDeclarator dtor = (declarations != null && declarations.length > 0) ? declarations[0] : definition; dtor = ASTQueries.findInnermostDeclarator(dtor); IASTName name = dtor.getName(); if (name instanceof ICPPASTQualifiedName) { IASTName[] ns = ((ICPPASTQualifiedName) name).getNames(); name = ns[ns.length - 1]; } return name; }
public ObjCASTParameterDeclaration copy() { ObjCASTParameterDeclaration copy = new ObjCASTParameterDeclaration(); copy.setDeclSpecifier(declSpec == null ? null : declSpec.copy()); copy.setDeclarator(declarator == null ? null : declarator.copy()); copy.setOffsetAndLength(this); return copy; }
@Override public boolean accept(ASTVisitor action) { if (action.shouldVisitParameterDeclarations) { switch (action.visit(this)) { case ASTVisitor.PROCESS_ABORT: return false; case ASTVisitor.PROCESS_SKIP: return true; default: break; } } if (declSpec != null) { if (!declSpec.accept(action)) { return false; } } if (declarator != null) { if (!declarator.accept(action)) { return false; } } if (action.shouldVisitParameterDeclarations) { switch (action.leave(this)) { case ASTVisitor.PROCESS_ABORT: return false; case ASTVisitor.PROCESS_SKIP: return true; default: break; } } return true; }
public IASTDeclaration getPrimaryDeclaration() { // first check if we already know it if (declarations != null) { for (IASTDeclarator dtor : declarations) { if (dtor == null) { break; } dtor = ASTQueries.findOutermostDeclarator(dtor); IASTDeclaration decl = (IASTDeclaration) dtor.getParent(); if (decl.getParent() instanceof ICPPASTCompositeTypeSpecifier) return decl; } } if (definition != null) { IASTDeclarator dtor = ASTQueries.findOutermostDeclarator(definition); IASTDeclaration decl = (IASTDeclaration) dtor.getParent(); if (decl.getParent() instanceof ICPPASTCompositeTypeSpecifier) return decl; } final char[] myName = getASTName().getLookupKey(); ICPPClassScope scope = (ICPPClassScope) getScope(); ICPPASTCompositeTypeSpecifier compSpec = (ICPPASTCompositeTypeSpecifier) ASTInternal.getPhysicalNodeOfScope(scope); if (compSpec != null) { IASTDeclaration[] members = compSpec.getMembers(); for (IASTDeclaration member : members) { if (member instanceof IASTSimpleDeclaration) { IASTDeclarator[] dtors = ((IASTSimpleDeclaration) member).getDeclarators(); for (IASTDeclarator dtor : dtors) { IASTName name = ASTQueries.findInnermostDeclarator(dtor).getName(); if (CharArrayUtils.equals(name.getLookupKey(), myName) && name.resolveBinding() == this) { return member; } } } else if (member instanceof IASTFunctionDefinition) { final IASTFunctionDeclarator declarator = ((IASTFunctionDefinition) member).getDeclarator(); IASTName name = ASTQueries.findInnermostDeclarator(declarator).getName(); if (CharArrayUtils.equals(name.getLookupKey(), myName) && name.resolveBinding() == this) { return member; } } } } return null; }
private ICPPASTFunctionDeclarator findFunctionDeclarator() { if (declarations != null) { for (IASTDeclarator dtor : declarations) { if (dtor == null) break; dtor = ASTQueries.findOutermostDeclarator(dtor); IASTDeclaration decl = (IASTDeclaration) dtor.getParent(); if (decl.getParent() instanceof ICPPASTCompositeTypeSpecifier) { dtor = ASTQueries.findTypeRelevantDeclarator(dtor); if (dtor instanceof ICPPASTFunctionDeclarator) { return (ICPPASTFunctionDeclarator) dtor; } } } } return definition; }
public void testStdioBug() throws ParserException { StringBuffer buffer = new StringBuffer("#define _PTR void *\n"); // $NON-NLS-1$ buffer.append("#define __cdecl __attribute__ ((__cdecl__))\n"); // $NON-NLS-1$ buffer.append("#define _EXFUN(name, proto) __cdecl name proto\n"); // $NON-NLS-1$ buffer.append("_PTR _EXFUN(memchr,(const _PTR, int, size_t));\n"); // $NON-NLS-1$ String code = buffer.toString(); for (ParserLanguage language : languages) { IASTTranslationUnit tu = parse(code, language, true, true); final IASTPreprocessorMacroDefinition[] macroDefinitions = tu.getMacroDefinitions(); IASTPreprocessorObjectStyleMacroDefinition _PTR = (IASTPreprocessorObjectStyleMacroDefinition) macroDefinitions[0]; IASTPreprocessorFunctionStyleMacroDefinition _EXFUN = (IASTPreprocessorFunctionStyleMacroDefinition) macroDefinitions[2]; IASTSimpleDeclaration memchr = (IASTSimpleDeclaration) tu.getDeclarations()[0]; IASTNodeLocation[] locations = memchr.getNodeLocations(); assertEquals(locations.length, 4); IASTMacroExpansionLocation loc_1 = (IASTMacroExpansionLocation) locations[0]; assertEqualsMacros(_PTR, loc_1.getExpansion().getMacroDefinition()); IASTFileLocation loc_2 = (IASTFileLocation) locations[1]; assertEquals(loc_2.getNodeOffset(), code.indexOf(" _EXFUN(")); // $NON-NLS-1$ assertEquals(loc_2.getNodeLength(), " ".length()); // $NON-NLS-1$ IASTMacroExpansionLocation loc_3 = (IASTMacroExpansionLocation) locations[2]; assertEqualsMacros(_EXFUN, loc_3.getExpansion().getMacroDefinition()); IASTFileLocation loc_4 = (IASTFileLocation) locations[3]; assertEquals(loc_4.getNodeOffset(), code.indexOf(";")); // $NON-NLS-1$ assertEquals(loc_4.getNodeLength(), 1); IASTFileLocation flat = memchr.getFileLocation(); 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$ IASTDeclarator d = memchr.getDeclarators()[0]; IASTFileLocation f = d.getFileLocation(); 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$ } }
private String getDeclaratorName(IASTDeclarator node) { node = getInnermostDeclarator(node); IASTName name = node.getName(); String nodeName = ASTStringUtil.getQualifiedName(name); if (nodeName.length() == 0) { nodeName = ANONYMOUS_NAME; } return nodeName; }
public CASTParameterDeclaration copy(CopyStyle style) { CASTParameterDeclaration copy = new CASTParameterDeclaration(); copy.setDeclSpecifier(declSpec == null ? null : declSpec.copy(style)); copy.setDeclarator(declarator == null ? null : declarator.copy(style)); copy.setOffsetAndLength(this); if (style == CopyStyle.withLocations) { copy.setCopyLocation(this); } return copy; }
protected <T extends CPPASTDeclarator> T copy(T copy, CopyStyle style) { copy.setName(name == null ? null : name.copy(style)); copy.setInitializer(initializer == null ? null : initializer.copy(style)); copy.setNestedDeclarator(nested == null ? null : nested.copy(style)); ((CPPASTDeclarator) copy).isPackExpansion = isPackExpansion; for (IASTPointerOperator pointer : getPointerOperators()) { copy.addPointerOperator(pointer.copy(style)); } return super.copy(copy, style); }
@Override public boolean accept(ASTVisitor action) { if (action.shouldVisitDeclarators) { switch (action.visit(this)) { case ASTVisitor.PROCESS_ABORT: return false; case ASTVisitor.PROCESS_SKIP: return true; default: break; } } if (pointerOps != null) { for (IASTPointerOperator op : pointerOps) { if (op == null) break; if (!op.accept(action)) return false; } } if (!acceptByAttributeSpecifiers(action)) return false; if (nested == null && name != null) { IASTDeclarator outermost = ASTQueries.findOutermostDeclarator(this); if (outermost.getPropertyInParent() != IASTTypeId.ABSTRACT_DECLARATOR) { if (!name.accept(action)) return false; if (action.shouldVisitImplicitNames) { for (IASTImplicitName implicitName : getImplicitNames()) { if (!implicitName.accept(action)) return false; } } } } if (nested != null && !nested.accept(action)) return false; if (!postAccept(action)) return false; if (action.shouldVisitDeclarators && action.leave(this) == ASTVisitor.PROCESS_ABORT) return false; return true; }
private IASTDeclarator createDeclarator( INodeFactory nodeFactory, IASTDeclarator sourceDeclarator, String name) { IASTName astName = name != null ? nodeFactory.newName(name.toCharArray()) : nodeFactory.newName(); IASTDeclarator declarator; if (sourceDeclarator instanceof IASTArrayDeclarator) { IASTArrayDeclarator arrDeclarator = (IASTArrayDeclarator) sourceDeclarator; IASTArrayDeclarator arrayDeclarator = nodeFactory.newArrayDeclarator(astName); IASTArrayModifier[] arrayModifiers = arrDeclarator.getArrayModifiers(); for (IASTArrayModifier arrayModifier : arrayModifiers) { arrayDeclarator.addArrayModifier(arrayModifier.copy(CopyStyle.withLocations)); } declarator = arrayDeclarator; } else { declarator = nodeFactory.newDeclarator(astName); } for (IASTPointerOperator pointerOp : sourceDeclarator.getPointerOperators()) { declarator.addPointerOperator(pointerOp.copy(CopyStyle.withLocations)); } return declarator; }
/* (non-Javadoc) * @see org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor.CPPBaseVisitorAction#processDeclarator(org.eclipse.cdt.core.dom.ast.IASTDeclarator) */ public int visit(IASTDeclarator declarator) { DOMASTNodeLeaf temp = addRoot(declarator); IASTPointerOperator[] ops = declarator.getPointerOperators(); for (IASTPointerOperator op : ops) addRoot(op); if (declarator instanceof IASTArrayDeclarator) { IASTArrayModifier[] mods = ((IASTArrayDeclarator) declarator).getArrayModifiers(); for (IASTArrayModifier mod : mods) addRoot(mod); } if (temp == null) return PROCESS_ABORT; else if (temp instanceof DOMASTNodeLeafContinue) return PROCESS_CONTINUE; else return PROCESS_CONTINUE; }
private IASTParameterDeclaration getParameterDeclaration( INodeFactory nodeFactory, String paramName) { IASTDeclarator sourceDeclarator = getDeclarator(); IASTDeclSpecifier declSpec = safeCopy(getDeclSpecifier()); IASTDeclarator declarator = createDeclarator(nodeFactory, sourceDeclarator, paramName); Indirection indirection = getIndirection(); if (indirection == Indirection.POINTER) { declarator.addPointerOperator(nodeFactory.newPointer()); } else if (indirection == Indirection.REFERENCE) { declarator.addPointerOperator(((ICPPNodeFactory) nodeFactory).newReferenceOperator(false)); } if (indirection != Indirection.NONE && !isWriteAccess && declSpec != null) { declSpec.setConst(true); } declarator.setNestedDeclarator(sourceDeclarator.getNestedDeclarator()); return nodeFactory.newParameterDeclaration(declSpec, declarator); }
protected IASTInitializer getInitializer(IASTDeclarator decl) { return decl.getInitializer(); }