Program: 56866
Project: 56866
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) {
        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=(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();
    }
  }
}