Program: 56872
Project: 56872
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=(it.peek(-1) instanceof AstDirective);
      if (trimLeft) {
        if (!it.hasNext() && block == Tokens.AST_BLOCK_TEMPLATE) {
          keepLeftNewLine=true;
        }
 else {
          int offset=-1;
          while (it.has(offset)) {
            AstStatement prev=it.peek(offset);
            if (prev instanceof AstText) {
              keepLeftNewLine=((AstText)prev).getLineStop() == text.getLineStart();
              break;
            }
            offset--;
          }
        }
      }
    }
 else {
      trimLeft=(block != Tokens.AST_BLOCK_TEMPLATE);
    }
    boolean trimRight;
    if (it.hasNext()) {
      trimRight=(it.peek(1) instanceof AstDirective);
    }
 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 trimLeft;
    boolean keepLeftNewLine=false;
    if (it.hasPrevious()) {
      trimLeft=isAstDirective(it.peek(-1));
      if (trimLeft) {
        if (!it.hasNext() && block == Tokens.AST_BLOCK_TEMPLATE) {
          keepLeftNewLine=true;
        }
 else         if (text.getLine() == 1) {
          keepLeftNewLine=true;
        }
 else {
          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();
    }
  }
}