private void visitFunc(IASTFunctionDefinition declaration) throws BadLocationException { IASTStatement body = declaration.getBody(); if (!(body instanceof IASTCompoundStatement)) return; // starting a function empties the stack... (which should already be empty on good flow) _scopeStack.clear(); IASTFileLocation location = body.getFileLocation(); int endLoc = location.getNodeOffset() + location.getNodeLength() - 1; IASTFunctionDeclarator declerator = declaration.getDeclarator(); int startLoc = declerator.getFileLocation().getNodeOffset(); StringBuffer hint = new StringBuffer(); hint.append(declerator.getName().getRawSignature()); /* TODO: specific params: exclude function parameters (show only the name) */ hint.append("( "); // $NON-NLS-1$ IASTNode[] decChildren = declerator.getChildren(); boolean firstParam = true; for (int i = 0; i < decChildren.length; i++) { IASTNode node = decChildren[i]; if (node instanceof IASTParameterDeclaration) { IASTParameterDeclaration param = (IASTParameterDeclaration) node; if (firstParam) firstParam = false; else hint.append(", "); // $NON-NLS-1$ hint.append(param.getDeclarator().getName()); } } hint.append(" )"); // $NON-NLS-1$ _container.add(new Hint("function", startLoc, endLoc, hint.toString())); // $NON-NLS-1$ }
private void visitBreak(IASTStatement statement) throws ScopeTraceException, BadLocationException { if (_scopeStack.isEmpty()) throw new ScopeTraceException("break without scope: " + statement); // $NON-NLS-1$ ScopeInfo scope = _scopeStack.peek(); String hintType; if (scope._statement instanceof IASTForStatement) hintType = "break-for"; // $NON-NLS-1$ else if (scope._statement instanceof IASTWhileStatement) hintType = "break-while"; // $NON-NLS-1$ else if (scope._statement instanceof IASTDoStatement) hintType = "break-do"; // $NON-NLS-1$ else if (scope._statement instanceof IASTCaseStatement || scope._statement instanceof IASTDefaultStatement) hintType = "break-case"; // $NON-NLS-1$ else throw new ScopeTraceException( "Unexpect scope (" + scope._statement + ") on break/continue:" + statement); //$NON-NLS-1$ //$NON-NLS-2$ int endLoc = statement.getFileLocation().getNodeOffset() + statement.getFileLocation().getNodeLength() - 1; _container.add(new Hint(hintType, scope._offset, endLoc, scope._str)); }
@Override public boolean accept(ASTVisitor action) { if (action.shouldVisitStatements) { switch (action.visit(this)) { case ASTVisitor.PROCESS_ABORT: return false; case ASTVisitor.PROCESS_SKIP: return true; default: break; } } if (init != null) if (!init.accept(action)) return false; if (condition != null) if (!condition.accept(action)) return false; if (iterationExpression != null) if (!iterationExpression.accept(action)) return false; if (body != null) if (!body.accept(action)) return false; if (action.shouldVisitStatements) { switch (action.leave(this)) { case ASTVisitor.PROCESS_ABORT: return false; case ASTVisitor.PROCESS_SKIP: return true; default: break; } } return true; }
protected void copyForStatement(CASTForStatement copy, CopyStyle style) { copy.setInitializerStatement(init == null ? null : init.copy(style)); copy.setConditionExpression(condition == null ? null : condition.copy(style)); copy.setIterationExpression( iterationExpression == null ? null : iterationExpression.copy(style)); copy.setBody(body == null ? null : body.copy(style)); copy.setOffsetAndLength(this); }
@Override public void setThenClause(IASTStatement thenClause) { assertNotFrozen(); this.thenClause = thenClause; if (thenClause != null) { thenClause.setParent(this); thenClause.setPropertyInParent(THEN); } }
@Override public CPPASTIfStatement copy(CopyStyle style) { CPPASTIfStatement copy = new CPPASTIfStatement(); copy.setConditionDeclaration(condDecl == null ? null : condDecl.copy(style)); copy.setConditionExpression(condition == null ? null : condition.copy(style)); copy.setThenClause(thenClause == null ? null : thenClause.copy(style)); copy.setElseClause(elseClause == null ? null : elseClause.copy(style)); return copy(copy, style); }
@Override public void setBody(IASTStatement statement) { assertNotFrozen(); body = statement; if (statement != null) { statement.setParent(this); statement.setPropertyInParent(BODY); } }
@Override public void setElseClause(IASTStatement elseClause) { assertNotFrozen(); this.elseClause = elseClause; if (elseClause != null) { elseClause.setParent(this); elseClause.setPropertyInParent(ELSE); } }
@Override public void setInitializerStatement(IASTStatement statement) { assertNotFrozen(); init = statement; if (statement != null) { statement.setParent(this); statement.setPropertyInParent(INITIALIZER); } }
@Override public CPPASTIfStatement copy(CopyStyle style) { CPPASTIfStatement copy = new CPPASTIfStatement(); copy.setConditionDeclaration(condDecl == null ? null : condDecl.copy(style)); copy.setConditionExpression(condition == null ? null : condition.copy(style)); copy.setThenClause(thenClause == null ? null : thenClause.copy(style)); copy.setElseClause(elseClause == null ? null : elseClause.copy(style)); copy.setOffsetAndLength(this); if (style == CopyStyle.withLocations) { copy.setCopyLocation(this); } return copy; }
// void f() { // int a= 1 // f() // } public void testExpressionWithoutSemi_314593() throws Exception { final String comment = getAboveComment(); for (ParserLanguage lang : ParserLanguage.values()) { IASTTranslationUnit tu = parse(comment, lang, false, false); IASTFunctionDefinition fdef = getDeclaration(tu, 0); IASTStatement stmt = getStatement(fdef, 0); assertEquals("int a= 1", stmt.getRawSignature()); IASTProblemStatement pstmt = getStatement(fdef, 1); stmt = getStatement(fdef, 2); assertEquals("f()", stmt.getRawSignature()); pstmt = getStatement(fdef, 3); } }
private void visitCase(IASTStatement statement) throws ScopeTraceException { /* TODO: specific params: don't show the switch part (only the case argument) */ ScopeInfo scope = _scopeStack.peek(); if (!(scope._statement instanceof IASTSwitchStatement)) { if (!((scope._statement instanceof IASTCaseStatement) || (scope._statement instanceof IASTDefaultStatement))) { throw new ScopeTraceException( "Lost track of stack (in case), found:" + scope._statement); // $NON-NLS-1$ } _scopeStack.pop(); scope = _scopeStack.peek(); } if (!(scope._statement instanceof IASTSwitchStatement)) { throw new ScopeTraceException( "Lost track of stack (in case2), found:" + scope._statement); // $NON-NLS-1$ } String hint = ""; // $NON-NLS-1$ if (statement instanceof IASTCaseStatement) { IASTExpression cond = ((IASTCaseStatement) statement).getExpression(); if (cond != null) hint = cond.getRawSignature(); hint = "case: " + hint; // $NON-NLS-1$ } else // default { hint = "default"; // $NON-NLS-1$ } int startLoc = statement.getFileLocation().getNodeOffset(); _scopeStack.push(new ScopeInfo(scope._str + " - " + hint, startLoc, statement)); // $NON-NLS-1$ }
@Override public int leave(IASTStatement statement) { try { if ((statement instanceof IASTSwitchStatement) || (statement instanceof IASTForStatement) || (statement instanceof IASTWhileStatement) || (statement instanceof IASTDoStatement)) { ScopeInfo scope; if (statement instanceof IASTSwitchStatement) { scope = _scopeStack.peek(); if ((scope._statement instanceof IASTCaseStatement) || (scope._statement instanceof IASTDefaultStatement)) { _scopeStack.pop(); } } scope = _scopeStack.pop(); if (!scope._statement.getClass().equals(statement.getClass())) { if (Activator.DEBUG) { Activator.log( "Lost track of scope. Expected:" + scope._statement + //$NON-NLS-1$ " but was:" + statement); //$NON-NLS-1$ } } } } catch (EmptyStackException e) { if (Activator.DEBUG) Activator.log(e); } return super.leave(statement); }
private void visitWhile(IASTWhileStatement statement) throws BadLocationException { IASTExpression cond = statement.getCondition(); String hint = ""; // $NON-NLS-1$ if (cond != null) hint = cond.getRawSignature(); hint = "while( " + hint + " )"; // $NON-NLS-1$ //$NON-NLS-2$ int startLoc = statement.getFileLocation().getNodeOffset(); _scopeStack.push(new ScopeInfo(hint, startLoc, statement)); IASTStatement body = statement.getBody(); if (body instanceof IASTCompoundStatement) { IASTFileLocation location = body.getFileLocation(); int endLoc = location.getNodeOffset() + location.getNodeLength() - 1; _container.add(new Hint("while", startLoc, endLoc, hint)); // $NON-NLS-1$ } }
private void visitFor(IASTForStatement statement) throws BadLocationException { /* TODO: specific params: show also initializer && increment expressions */ IASTExpression cond = statement.getConditionExpression(); String hint = ""; // $NON-NLS-1$ if (cond != null) hint = cond.getRawSignature(); hint = "for( " + hint + " )"; // $NON-NLS-1$ //$NON-NLS-2$ int startLoc = statement.getFileLocation().getNodeOffset(); _scopeStack.push(new ScopeInfo(hint, startLoc, statement)); IASTStatement body = statement.getBody(); if (body instanceof IASTCompoundStatement) { IASTFileLocation location = body.getFileLocation(); int endLoc = location.getNodeOffset() + location.getNodeLength() - 1; _container.add(new Hint("for", startLoc, endLoc, hint)); // $NON-NLS-1$ } }
private void writeCompoundStatement(IASTCompoundStatement compoundStatement) { scribe.printLBrace(); scribe.newLine(); for (IASTStatement statements : getNestedStatements(compoundStatement)) { statements.accept(visitor); } if (hasFreestandingComments(compoundStatement)) { writeFreeStandingComments(compoundStatement); } if (decrementIndentationLevelOneMore) { scribe.decrementIndentationLevel(); decrementIndentationLevelOneMore = false; } scribe.printRBrace(); }
protected int writeMixedStatement(IASTStatement statement) { IFile file = FileHelper.getFileFromNode(statement); int offset = statement.getFileLocation().getNodeOffset(); int length = statement.getFileLocation().getNodeLength(); String code = FileContentHelper.getContent(file, offset, length); scribe.println(code); return ASTVisitor.PROCESS_SKIP; }
protected void writeBodyStatement(IASTStatement statement, boolean isDoStatement) { if (statement instanceof IASTCompoundStatement) { // TODO hsr existiert noch eine methode statement.accept(visitor); if (!isDoStatement) { scribe.newLine(); } compoundNoNewLine = false; } else if (statement instanceof IASTNullStatement) { statement.accept(visitor); scribe.newLine(); } else { scribe.incrementIndentationLevel(); scribe.newLine(); statement.accept(visitor); scribe.decrementIndentationLevel(); } }
private void visitIf(IASTIfStatement statement) throws BadLocationException { /* TODO: specific params: don't show the if hint if there's an "else if" after it (by checking if the elseClause is an instance of ifstatment) */ String hint = ""; // $NON-NLS-1$ if (statement.getConditionExpression() != null) { hint = statement.getConditionExpression().getRawSignature(); } else { if ((statement instanceof ICPPASTIfStatement) && ((ICPPASTIfStatement) statement).getConditionDeclaration() != null) { hint = ((ICPPASTIfStatement) statement).getConditionDeclaration().getRawSignature(); } } IASTStatement thenClause = statement.getThenClause(); IASTStatement elseClause = statement.getElseClause(); boolean showIfHint = (elseClause == null); int endLoc = -1; if (!showIfHint) { if (elseClause.getFileLocation().getStartingLineNumber() != thenClause.getFileLocation().getEndingLineNumber()) { showIfHint = true; } // if the else looks like this "} else {", then show the hint on the "{" if (!showIfHint && !(elseClause instanceof IASTIfStatement)) { endLoc = elseClause.getFileLocation().getNodeOffset(); showIfHint = true; } } if (showIfHint && !(thenClause instanceof IASTCompoundStatement)) showIfHint = false; if (showIfHint) { IASTFileLocation location = thenClause.getFileLocation(); if (endLoc == -1) endLoc = location.getNodeOffset() + location.getNodeLength() - 1; int startLoc = statement.getFileLocation().getNodeOffset(); _container.add( new Hint( "if", startLoc, endLoc, "if( " + hint + " )")); // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ } if (elseClause != null && !(elseClause instanceof IASTIfStatement) && (elseClause instanceof IASTCompoundStatement)) { IASTFileLocation location = elseClause.getFileLocation(); endLoc = location.getNodeOffset() + location.getNodeLength() - 1; int startLoc = location.getNodeOffset(); _container.add( new Hint( "if", startLoc, endLoc, "else_of_if( " + hint + " )")); // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ } }
/** * Prints a statement. * * @param statement the statement * @param newLine if true print a newline if statement usually have one. * @return {@link ASTVisitor#PROCESS_SKIP} */ protected int writeStatement(IASTStatement statement, boolean newLine) { if (statement instanceof IASTAmbiguousStatement) { // TODO HSR Leo test statement.accept(visitor); newLine = false; } else if (statement instanceof IASTExpressionStatement) { writeExpressionStatement((IASTExpressionStatement) statement); // usually newLine } else if (statement instanceof IASTDeclarationStatement) { writeDeclarationStatement((IASTDeclarationStatement) statement); newLine = false; } else if (statement instanceof IASTNullStatement) { writeNullStatement((IASTNullStatement) statement); // usually newLine } else if (statement instanceof IASTReturnStatement) { writeReturnStatement((IASTReturnStatement) statement); // usually newLine } else if (statement instanceof IASTGotoStatement) { writeGotoStatement((IASTGotoStatement) statement); // usually newLine } else if (statement instanceof IASTLabelStatement) { writeLabelStatement((IASTLabelStatement) statement); newLine = false; } else if (statement instanceof IASTCaseStatement) { writeCaseStatement((IASTCaseStatement) statement); // usually newLine } else if (statement instanceof IASTDefaultStatement) { writeDefaultStatement((IASTDefaultStatement) statement); } else if (statement instanceof IASTContinueStatement) { writeContinueStatement((IASTContinueStatement) statement); // usually newLine } else if (statement instanceof IASTCompoundStatement) { writeCompoundStatement((IASTCompoundStatement) statement); if (compoundNoNewLine) { newLine = false; compoundNoNewLine = false; } } else if (statement instanceof IASTBreakStatement) { writeBreakStatement((IASTBreakStatement) statement); // usually newLine } else if (statement instanceof IASTSwitchStatement) { writeSwitchStatement((IASTSwitchStatement) statement); newLine = false; } else if (statement instanceof IASTIfStatement) { writeIfStatement((IASTIfStatement) statement); newLine = false; } else if (statement instanceof IASTWhileStatement) { writeWhileStatement((IASTWhileStatement) statement); newLine = false; } else if (statement instanceof IASTForStatement) { writeForStatement((IASTForStatement) statement); newLine = false; } else if (statement instanceof ICPPASTRangeBasedForStatement) { writeForStatement((ICPPASTRangeBasedForStatement) statement); newLine = false; } else if (statement instanceof IASTDoStatement) { writeDoStatement((IASTDoStatement) statement); newLine = true; } else if (statement instanceof ICPPASTTryBlockStatement) { writeTryBlockStatement((ICPPASTTryBlockStatement) statement); newLine = false; } else if (statement instanceof ICPPASTCatchHandler) { writeCatchHandler((ICPPASTCatchHandler) statement); newLine = false; } else if (statement instanceof IASTProblemStatement) { throw new ProblemRuntimeException((IASTProblemStatement) statement); } if (hasTrailingComments(statement)) { writeTrailingComments(statement, newLine); } else if (newLine) { scribe.newLine(); } return ASTVisitor.PROCESS_SKIP; }