@Override
  public boolean accept(ASTVisitor action) {
    N stack = null;
    ICPPASTIfStatement stmt = this;
    loop:
    for (; ; ) {
      if (action.shouldVisitStatements) {
        switch (action.visit(stmt)) {
          case ASTVisitor.PROCESS_ABORT:
            return false;
          case ASTVisitor.PROCESS_SKIP:
            stmt = null;
            break loop;
          default:
            break;
        }
      }

      if (!((CPPASTIfStatement) stmt).acceptByAttributeSpecifiers(action)) return false;

      IASTNode child = stmt.getConditionExpression();
      if (child != null && !child.accept(action)) return false;
      child = stmt.getConditionDeclaration();
      if (child != null && !child.accept(action)) return false;
      child = stmt.getThenClause();
      if (child != null && !child.accept(action)) return false;
      child = stmt.getElseClause();
      if (child instanceof ICPPASTIfStatement) {
        if (action.shouldVisitStatements) {
          N n = new N(stmt);
          n.fNext = stack;
          stack = n;
        }
        stmt = (ICPPASTIfStatement) child;
      } else {
        if (child != null && !child.accept(action)) return false;
        break loop;
      }
    }

    if (action.shouldVisitStatements) {
      if (stmt != null && action.leave(stmt) == ASTVisitor.PROCESS_ABORT) return false;
      while (stack != null) {
        if (action.leave(stack.fIfStatement) == ASTVisitor.PROCESS_ABORT) return false;
        stack = stack.fNext;
      }
    }
    return true;
  }
Example #2
0
 private void handleAppends(IASTNode node) {
   List<ASTModification> modifications = getModifications(node, ModificationKind.APPEND_CHILD);
   if (modifications.isEmpty()) return;
   ChangeGeneratorWriterVisitor writer =
       new ChangeGeneratorWriterVisitor(modificationStore, commentMap);
   ReplaceEdit anchor = getAppendAnchor(node);
   Assert.isNotNull(anchor);
   IASTNode precedingNode = getLastNodeBeforeAppendPoint(node);
   for (ASTModification modification : modifications) {
     IASTNode newNode = modification.getNewNode();
     if (precedingNode != null) {
       if (ASTWriter.requireBlankLineInBetween(precedingNode, newNode)) {
         writer.newLine();
       }
     } else if (node instanceof ICPPASTNamespaceDefinition) {
       writer.newLine();
     }
     precedingNode = null;
     newNode.accept(writer);
   }
   if (node instanceof ICPPASTNamespaceDefinition) {
     writer.newLine();
   }
   String code = writer.toString();
   IFile file = FileHelper.getFileFromNode(node);
   MultiTextEdit parentEdit = getEdit(node, file);
   ReplaceEdit edit =
       new ReplaceEdit(anchor.getOffset(), anchor.getLength(), code + anchor.getText());
   parentEdit.addChild(edit);
   IASTFileLocation fileLocation = node.getFileLocation();
   sourceOffsets.put(fileLocation.getFileName(), endOffset(fileLocation));
 }
Example #3
0
  private void handleReplace(IASTNode node) {
    List<ASTModification> modifications = getModifications(node, ModificationKind.REPLACE);
    String source = node.getTranslationUnit().getRawSignature();
    TextEdit edit;
    ChangeGeneratorWriterVisitor writer =
        new ChangeGeneratorWriterVisitor(modificationStore, commentMap);
    IASTFileLocation fileLocation = node.getFileLocation();
    Integer val = sourceOffsets.get(fileLocation.getFileName());
    int processedOffset = val != null ? val.intValue() : 0;
    if (modifications.size() == 1 && modifications.get(0).getNewNode() == null) {
      int offset = getOffsetIncludingComments(node);
      int endOffset = getEndOffsetIncludingComments(node);
      offset = Math.max(skipPrecedingBlankLines(source, offset), processedOffset);
      endOffset = skipTrailingBlankLines(source, endOffset);
      IASTNode[] siblingsList = getContainingNodeList(node);
      if (siblingsList != null) {
        if (siblingsList.length > 1) {
          if (node == siblingsList[0]) {
            endOffset = skipToTrailingDelimiter(source, ',', endOffset);
          } else {
            offset = skipToPrecedingDelimiter(source, ',', offset);
          }
        } else if (node.getPropertyInParent() == ICPPASTFunctionDefinition.MEMBER_INITIALIZER) {
          offset = skipToPrecedingDelimiter(source, ':', offset);
        }
      }
      IASTNode prevNode = getPreviousSiblingOrPreprocessorNode(node);
      IASTNode nextNode = getNextSiblingOrPreprocessorNode(node);
      if (prevNode != null && nextNode != null) {
        if (ASTWriter.requireBlankLineInBetween(prevNode, nextNode)) {
          writer.newLine();
        }
      } else if (node.getParent() instanceof ICPPASTNamespaceDefinition) {
        writer.newLine();
      }
      String code = writer.toString();
      edit = new ReplaceEdit(offset, endOffset - offset, code);
    } else {
      node.accept(writer);
      String code = writer.toString();
      int offset = fileLocation.getNodeOffset();
      int endOffset = offset + fileLocation.getNodeLength();
      if (node instanceof IASTStatement || node instanceof IASTDeclaration) {
        // Include trailing comments in the area to be replaced.
        endOffset = Math.max(endOffset, getEndOffsetIncludingTrailingComments(node));
      }
      String lineSeparator = writer.getScribe().getLineSeparator();
      if (code.endsWith(lineSeparator)) {
        code = code.substring(0, code.length() - lineSeparator.length());
      }
      edit = new ReplaceEdit(offset, endOffset - offset, code);
    }
    IFile file = FileHelper.getFileFromNode(node);
    MultiTextEdit parentEdit = getEdit(node, file);
    parentEdit.addChild(edit);

    sourceOffsets.put(fileLocation.getFileName(), edit.getExclusiveEnd());
  }
Example #4
0
  /**
   * Adjusts parameter references under the given node to account for renamed parameters and
   * parameters passed by pointer.
   *
   * @param node the root node of the AST subtree to be adjusted
   * @param changedParameters the map from references to changed parameters to parameters themselves
   * @param rewrite the rewrite for the node
   * @param group the edit group to add the changes to
   */
  protected static void adjustParameterReferences(
      IASTNode node,
      final Map<IASTName, NameInformation> changedParameters,
      final INodeFactory nodeFactory,
      final ASTRewrite rewrite,
      final TextEditGroup group) {
    if (changedParameters.isEmpty()) return;
    node.accept(
        new ASTVisitor() {
          {
            shouldVisitNames = true;
          }

          @Override
          public int visit(IASTName name) {
            NameInformation param = changedParameters.get(name.getOriginalNode());
            if (param != null) {
              IASTName newName = null;
              if (param.isRenamed()) {
                newName = nodeFactory.newName(param.getNewName().toCharArray());
              }
              if (param.getIndirection() == Indirection.POINTER
                  && name.getPropertyInParent() == IASTIdExpression.ID_NAME) {
                IASTIdExpression idExp = (IASTIdExpression) name.getParent();
                if (idExp.getPropertyInParent() == IASTFieldReference.FIELD_OWNER
                    && !((IASTFieldReference) idExp.getParent()).isPointerDereference()) {
                  IASTFieldReference dotRef = (IASTFieldReference) idExp.getParent();
                  IASTFieldReference arrowRef = dotRef.copy(CopyStyle.withLocations);
                  arrowRef.setIsPointerDereference(true);
                  if (newName != null) {
                    idExp = (IASTIdExpression) arrowRef.getFieldOwner();
                    idExp.setName(newName);
                  }
                  rewrite.replace(dotRef, arrowRef, group);
                } else {
                  IASTIdExpression newIdExp = idExp.copy(CopyStyle.withLocations);
                  IASTUnaryExpression starExp =
                      nodeFactory.newUnaryExpression(IASTUnaryExpression.op_star, newIdExp);
                  if (newName != null) {
                    newIdExp.setName(newName);
                  }
                  rewrite.replace(idExp, starExp, group);
                }
              } else if (newName != null) {
                rewrite.replace(name, newName, group);
              }
            }
            return super.visit(name);
          }
        });
  }
Example #5
0
 public void generateChange(IASTNode rootNode, ASTVisitor pathProvider)
     throws ProblemRuntimeException {
   change = new CompositeChange(ChangeGeneratorMessages.ChangeGenerator_compositeChange);
   classifyModifications();
   rootNode.accept(pathProvider);
   for (IFile currentFile : changes.keySet()) {
     MultiTextEdit edit = changes.get(currentFile);
     edit =
         formatChangedCode(
             edit, rootNode.getTranslationUnit().getRawSignature(), currentFile.getProject());
     TextFileChange subchange = ASTRewriteAnalyzer.createCTextFileChange(currentFile);
     subchange.setEdit(edit);
     change.add(subchange);
   }
 }
  @Deprecated
  public IASTName[] getSelectedNames(IASTTranslationUnit ast, int start, int length) {
    IASTNode selectedNode = ast.getNodeSelector(null).findNode(start, length);

    if (selectedNode == null) return new IASTName[0];

    if (selectedNode instanceof IASTName) return new IASTName[] {(IASTName) selectedNode};

    if (selectedNode instanceof IASTPreprocessorMacroExpansion) {
      return new IASTName[] {((IASTPreprocessorMacroExpansion) selectedNode).getMacroReference()};
    }

    NameCollector collector = new NameCollector();
    selectedNode.accept(collector);
    return collector.getNames();
  }
Example #7
0
  private void handleAppends(IASTTranslationUnit tu) {
    List<ASTModification> modifications = getModifications(tu, ModificationKind.APPEND_CHILD);
    if (modifications.isEmpty()) return;

    IASTNode prevNode = null;
    IASTDeclaration[] declarations = tu.getDeclarations();
    if (declarations.length != 0) {
      prevNode = declarations[declarations.length - 1];
    } else {
      IASTPreprocessorStatement[] preprocessorStatements = tu.getAllPreprocessorStatements();
      if (preprocessorStatements.length != 0) {
        prevNode = preprocessorStatements[preprocessorStatements.length - 1];
      }
    }
    int offset = prevNode != null ? getEndOffsetIncludingComments(prevNode) : 0;
    String source = tu.getRawSignature();
    int endOffset = skipTrailingBlankLines(source, offset);

    ChangeGeneratorWriterVisitor writer =
        new ChangeGeneratorWriterVisitor(modificationStore, commentMap);
    IASTNode newNode = null;
    for (ASTModification modification : modifications) {
      boolean first = newNode == null;
      newNode = modification.getNewNode();
      if (first) {
        if (prevNode != null) {
          writer.newLine();
          if (ASTWriter.requireBlankLineInBetween(prevNode, newNode)) {
            writer.newLine();
          }
        }
      }
      newNode.accept(writer);
    }
    if (prevNode != null) {
      IASTNode nextNode = getNextSiblingOrPreprocessorNode(prevNode);
      if (nextNode != null && ASTWriter.requireBlankLineInBetween(newNode, nextNode)) {
        writer.newLine();
      }
    }

    String code = writer.toString();
    IFile file = FileHelper.getFileFromNode(tu);
    MultiTextEdit parentEdit = getEdit(tu, file);
    parentEdit.addChild(new ReplaceEdit(offset, endOffset - offset, code));
  }
Example #8
0
 private void handleInserts(IASTNode anchorNode) {
   List<ASTModification> modifications =
       getModifications(anchorNode, ModificationKind.INSERT_BEFORE);
   if (modifications.isEmpty()) return;
   ChangeGeneratorWriterVisitor writer =
       new ChangeGeneratorWriterVisitor(modificationStore, commentMap);
   IASTNode newNode = null;
   for (ASTModification modification : modifications) {
     boolean first = newNode == null;
     newNode = modification.getNewNode();
     if (first) {
       IASTNode prevNode = getPreviousSiblingOrPreprocessorNode(anchorNode);
       if (prevNode != null) {
         if (ASTWriter.requireBlankLineInBetween(prevNode, newNode)) {
           writer.newLine();
         }
       } else if (anchorNode.getParent() instanceof ICPPASTNamespaceDefinition) {
         writer.newLine();
       }
     }
     newNode.accept(writer);
     if (getContainingNodeList(anchorNode) != null) {
       writer.getScribe().print(", "); // $NON-NLS-1$
     }
   }
   if (ASTWriter.requireBlankLineInBetween(newNode, anchorNode)) {
     writer.newLine();
   }
   int insertPos = getOffsetIncludingComments(anchorNode);
   int length = 0;
   if (writer.getScribe().isAtBeginningOfLine()) {
     String tuCode = anchorNode.getTranslationUnit().getRawSignature();
     insertPos = skipPrecedingWhitespace(tuCode, insertPos);
     length = insertPos;
     insertPos = skipPrecedingBlankLines(tuCode, insertPos);
     length -= insertPos;
   }
   String code = writer.toString();
   ReplaceEdit edit = new ReplaceEdit(insertPos, length, code);
   IFile file = FileHelper.getFileFromNode(anchorNode);
   MultiTextEdit parentEdit = getEdit(anchorNode, file);
   parentEdit.addChild(edit);
   sourceOffsets.put(file.getName(), edit.getOffset());
 }