Program: 56851
Project: 56851
Buggy source:
private void trimDirectiveWhitespacesAndComments(ListIterator<AstStatement> it,int block,ParserContext ctx){
  boolean trimDirectiveWhitespaces=ctx.isTrimDirectiveWhitespaces();
  boolean trimDirectiveComments=ctx.isTrimDirectiveComments();
  String trimDirectiveCommentsPrefix=ctx.getTrimDirectiveCommentsPrefix();
  String trimDirectiveCommentsSuffix=ctx.getTrimDirectiveCommentsSuffix();
  if (!trimDirectiveWhitespaces && !trimDirectiveComments) {
    return;
  }
  it.reset();
  while (it.has()) {
    AstStatement stmt=it.peek();
    if (!(stmt instanceof AstText)) {
      it.move();
      continue;
    }
    AstText text=(AstText)stmt;
    boolean trimLeft;
    boolean keepLeftNewLine=false;
    if (it.hasPrevious()) {
      trimLeft=isAstDirective(it.peek(-1));
      if (trimLeft) {
        AstStatement prev=it.peek(-1);
        if (prev != null) {
          if (prev instanceof AstDirective) {
            if (prev instanceof AstDirectiveTag) {
              keepLeftNewLine=true;
            }
 else {
              keepLeftNewLine=((AstDirective)prev).getPosition().getLine() == text.getLine();
            }
          }
        }
      }
    }
 else {
      trimLeft=(block != Tokens.AST_BLOCK_TEMPLATE);
    }
    boolean trimRight;
    if (it.hasNext()) {
      trimRight=isAstDirective(it.peek(1));
    }
 else {
      trimRight=(block != Tokens.AST_BLOCK_TEMPLATE);
    }
    if (trimDirectiveComments) {
      text.trimDirectiveComments(trimLeft,trimRight,trimDirectiveCommentsPrefix,trimDirectiveCommentsSuffix);
    }
    if (trimDirectiveWhitespaces) {
      text.trimDirectiveWhitespaces(trimLeft,trimRight,keepLeftNewLine);
    }
    if (!it.hasNext()) {
      if (block == Tokens.AST_BLOCK_TAG || block == Tokens.AST_BLOCK_MACRO) {
        text.trimLastNewLine();
      }
    }
    if (text.isEmpty()) {
      it.remove();
    }
 else {
      it.move();
    }
  }
}

Fixed version (ground truth):
private void trimDirectiveWhitespacesAndComments(ListIterator<AstStatement> it,int block,ParserContext ctx){
  boolean trimDirectiveWhitespaces=ctx.isTrimDirectiveWhitespaces();
  boolean trimDirectiveComments=ctx.isTrimDirectiveComments();
  String trimDirectiveCommentsPrefix=ctx.getTrimDirectiveCommentsPrefix();
  String trimDirectiveCommentsSuffix=ctx.getTrimDirectiveCommentsSuffix();
  if (!trimDirectiveWhitespaces && !trimDirectiveComments) {
    return;
  }
  it.reset();
  while (it.has()) {
    AstStatement stmt=it.peek();
    if (!(stmt instanceof AstText)) {
      it.move();
      continue;
    }
    AstText text=(AstText)stmt;
    boolean trimWhitespacesLeft, trimCommentsLeft;
    boolean keepLeftNewLine=false;
    if (it.hasPrevious()) {
      trimWhitespacesLeft=isAstDirective(it.peek(-1),false);
      trimCommentsLeft=isAstDirective(it.peek(-1),true);
      if (trimWhitespacesLeft) {
        AstStatement prev=it.peek(-1);
        if (prev != null) {
          if (prev instanceof AstDirective) {
            if (prev instanceof AstDirectiveTag) {
              keepLeftNewLine=true;
            }
 else {
              keepLeftNewLine=((AstDirective)prev).getPosition().getLine() == text.getLine();
            }
          }
        }
      }
    }
 else {
      trimWhitespacesLeft=(block != Tokens.AST_BLOCK_TEMPLATE);
      trimCommentsLeft=trimWhitespacesLeft;
    }
    boolean trimWhitespacesRight, trimCommentsRight;
    if (it.hasNext()) {
      trimWhitespacesRight=isAstDirective(it.peek(1),false);
      trimCommentsRight=isAstDirective(it.peek(1),true);
    }
 else {
      trimWhitespacesRight=(block != Tokens.AST_BLOCK_TEMPLATE);
      trimCommentsRight=trimWhitespacesRight;
    }
    if (trimDirectiveComments) {
      text.trimDirectiveComments(trimCommentsLeft,trimCommentsRight,trimDirectiveCommentsPrefix,trimDirectiveCommentsSuffix);
    }
    if (trimDirectiveWhitespaces) {
      text.trimDirectiveWhitespaces(trimWhitespacesLeft,trimWhitespacesRight,keepLeftNewLine);
    }
    if (!it.hasNext()) {
      if (block == Tokens.AST_BLOCK_TAG || block == Tokens.AST_BLOCK_MACRO) {
        text.trimLastNewLine();
      }
    }
    if (text.isEmpty()) {
      it.remove();
    }
 else {
      it.move();
    }
  }
}