Example #1
0
  private void writeIfStatement(IASTIfStatement ifStatement) {
    scribe.print(IF);
    scribe.noNewLines();
    if (ifStatement instanceof ICPPASTIfStatement) {
      ICPPASTIfStatement cppIfStatment = (ICPPASTIfStatement) ifStatement;

      if (cppIfStatment.getConditionDeclaration() == null) {
        cppIfStatment.getConditionExpression().accept(visitor);
      } else {
        writeDeclarationWithoutSemicolon(cppIfStatment.getConditionDeclaration());
      }
    } else {
      ifStatement.getConditionExpression().accept(visitor);
    }

    scribe.print(')');
    scribe.newLines();
    nextCompoundNoNewLine();
    IASTStatement elseClause = ifStatement.getElseClause();
    writeBodyStatement(ifStatement.getThenClause(), elseClause != null);

    if (elseClause != null) {
      scribe.print(ELSE);
      nextCompoundNoNewLine();
      writeBodyStatement(elseClause, false);
    }
  }
  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$
    }
  }
Example #3
0
 // int f(){
 //    if( 12 A )
 //       return -1;
 //    int v;
 // }
 public void testProblemInIfExpression_Bug100321() throws Exception {
   final String comment = getAboveComment();
   for (ParserLanguage lang : ParserLanguage.values()) {
     IASTTranslationUnit tu = parse(comment, lang, false, false);
     IASTFunctionDefinition fdef = getDeclaration(tu, 0);
     IASTIfStatement ifstmt = getStatement(fdef, 0);
     assertInstance(ifstmt.getConditionExpression(), IASTProblemExpression.class);
     assertEquals("12 A", ifstmt.getConditionExpression().getRawSignature());
     assertInstance(ifstmt.getThenClause(), IASTReturnStatement.class);
   }
 }