예제 #1
0
  /**
   * Returns the list of nodes the given node is part of, for example function parameters if the
   * node is a parameter.
   *
   * @param node the node possibly belonging to a list.
   * @return the list of nodes containing the given node, or <code>null</code> if the node does not
   *     belong to a list
   */
  private IASTNode[] getContainingNodeList(IASTNode node) {
    if (node.getPropertyInParent() == IASTStandardFunctionDeclarator.FUNCTION_PARAMETER) {
      return ((IASTStandardFunctionDeclarator) node.getParent()).getParameters();
    } else if (node.getPropertyInParent() == IASTExpressionList.NESTED_EXPRESSION) {
      return ((IASTExpressionList) node.getParent()).getExpressions();
    } else if (node.getPropertyInParent() == ICPPASTFunctionDefinition.MEMBER_INITIALIZER) {
      return ((ICPPASTFunctionDefinition) node.getParent()).getMemberInitializers();
    } else if (node.getPropertyInParent() == ICPPASTFunctionDeclarator.EXCEPTION_TYPEID) {
      return ((ICPPASTFunctionDeclarator) node.getParent()).getExceptionSpecification();
    }

    return null;
  }
 public void replace(IASTNode child, IASTNode other) {
   if (child == declarator) {
     other.setPropertyInParent(child.getPropertyInParent());
     other.setParent(child.getParent());
     declarator = (IASTDeclarator) other;
   }
 }
예제 #3
0
 public boolean isFunctionCall() {
   if (astName == null) return false;
   if (astName.getPropertyInParent() == CPPSemantics.STRING_LOOKUP_PROPERTY) return false;
   IASTNode p1 = astName.getParent();
   if (p1 instanceof ICPPASTQualifiedName) p1 = p1.getParent();
   return (p1 instanceof IASTIdExpression
       && p1.getPropertyInParent() == IASTFunctionCallExpression.FUNCTION_NAME);
 }
예제 #4
0
 @Override
 public void replace(IASTNode child, IASTNode other) {
   if (child == nested) {
     other.setPropertyInParent(child.getPropertyInParent());
     other.setParent(child.getParent());
     nested = (IASTDeclarator) other;
   }
 }
 @Override
 public void replace(IASTNode child, IASTNode other) {
   if (child == operand) {
     other.setPropertyInParent(child.getPropertyInParent());
     other.setParent(child.getParent());
     operand = (IASTExpression) other;
   }
 }
예제 #6
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());
  }
예제 #7
0
 @Override
 public void replace(IASTNode child, IASTNode other) {
   if (thenClause == child) {
     other.setParent(child.getParent());
     other.setPropertyInParent(child.getPropertyInParent());
     thenClause = (IASTStatement) other;
   } else if (elseClause == child) {
     other.setParent(child.getParent());
     other.setPropertyInParent(child.getPropertyInParent());
     elseClause = (IASTStatement) other;
   } else if (condition == child || condDecl == child) {
     if (other instanceof IASTExpression) {
       setConditionExpression((IASTExpression) other);
     } else if (other instanceof IASTDeclaration) {
       setConditionDeclaration((IASTDeclaration) other);
     }
   }
 }
 @Override
 public void replace(IASTNode child, IASTNode other) {
   assert child.isActive() == other.isActive();
   for (int i = 0; i <= fLastDeclaration; ++i) {
     if (fAllDeclarations[i] == child) {
       other.setParent(child.getParent());
       other.setPropertyInParent(child.getPropertyInParent());
       fAllDeclarations[i] = (IASTDeclaration) other;
       fActiveDeclarations = null;
       break;
     }
   }
 }
예제 #9
0
 @Override
 public void replace(IASTNode child, IASTNode other) {
   if (body == child) {
     other.setPropertyInParent(child.getPropertyInParent());
     other.setParent(child.getParent());
     body = (IASTStatement) other;
   }
   if (child == init) {
     other.setPropertyInParent(child.getPropertyInParent());
     other.setParent(child.getParent());
     init = (IASTStatement) other;
   }
   if (child == iterationExpression) {
     other.setPropertyInParent(child.getPropertyInParent());
     other.setParent(child.getParent());
     iterationExpression = (IASTExpression) other;
   }
   if (child == condition) {
     other.setPropertyInParent(child.getPropertyInParent());
     other.setParent(child.getParent());
     condition = (IASTExpression) other;
   }
 }
예제 #10
0
  public boolean checkClassContainingFriend() {
    if (astName == null || astName instanceof ICPPASTQualifiedName) return false;

    IASTNode p = astName.getParent();
    ASTNodeProperty prop = null;
    while (p != null) {
      prop = p.getPropertyInParent();
      if (prop == ICPPASTTemplateId.TEMPLATE_ID_ARGUMENT || prop == IASTDeclarator.DECLARATOR_NAME)
        return false;
      if (p instanceof IASTDeclarator
          && !(((IASTDeclarator) p).getName() instanceof ICPPASTQualifiedName)) return false;
      if (p instanceof IASTDeclaration) {
        if (prop == IASTCompositeTypeSpecifier.MEMBER_DECLARATION) {
          return CPPVisitor.isFriendDeclaration(p);
        } else {
          return false;
        }
      }
      p = p.getParent();
    }
    return false;
  }
예제 #11
0
  public static boolean checkWholeClassScope(IASTName name) {
    if (name == null) return false;
    if (name.getPropertyInParent() == CPPSemantics.STRING_LOOKUP_PROPERTY) return true;

    IASTNode node = name.getParent();
    while (node instanceof IASTName) {
      name = (IASTName) node;
      node = name.getParent();
    }

    final ASTNodeProperty nameProp = name.getPropertyInParent();
    if (nameProp == IASTIdExpression.ID_NAME
        || nameProp == IASTFieldReference.FIELD_NAME
        || nameProp == ICASTFieldDesignator.FIELD_NAME
        || nameProp == ICPPASTUsingDirective.QUALIFIED_NAME
        || nameProp == ICPPASTUsingDeclaration.NAME
        || nameProp == IASTFunctionCallExpression.FUNCTION_NAME
        || nameProp == IASTNamedTypeSpecifier.NAME
        || nameProp == ICPPASTConstructorChainInitializer.MEMBER_ID) {
      // Potentially we need to consider the entire class scope
    } else {
      return false;
    }

    for (; node != null; node = node.getParent()) {
      // 3.3.7-5
      if (node.getParent() instanceof IASTFunctionDefinition) {
        // In a function body
        final ASTNodeProperty prop = node.getPropertyInParent();
        if (prop == IASTFunctionDefinition.DECL_SPECIFIER
            || prop == IASTFunctionDefinition.DECLARATOR) {
          return false;
        }
        IASTNode parent = node.getParent();
        while (parent != null) {
          if (parent instanceof ICPPASTCompositeTypeSpecifier) return true;
          parent = parent.getParent();
        }
        // No inline method.
        return false;
      }
      if (node instanceof IASTInitializerList || node instanceof IASTEqualsInitializer) {
        if (node.getPropertyInParent() == IASTDeclarator.INITIALIZER) {
          IASTNode decl = node.getParent();
          while (decl instanceof IASTDeclarator) {
            decl = decl.getParent();
          }
          if (decl instanceof IASTParameterDeclaration) {
            // Default argument
            IASTNode parent = decl.getParent();
            while (parent != null) {
              if (parent instanceof ICPPASTCompositeTypeSpecifier) return true;
              parent = parent.getParent();
            }
            // Not within a class definition
            return false;
          }

          if (decl instanceof IASTSimpleDeclaration
              && decl.getPropertyInParent() == IASTCompositeTypeSpecifier.MEMBER_DECLARATION) {
            // Initializer of non-static data member
            IASTDeclSpecifier declSpec = ((IASTSimpleDeclaration) decl).getDeclSpecifier();
            if (declSpec.getStorageClass() != IASTDeclSpecifier.sc_static) {
              return true;
            }
            // Continue search, we could still be in a method.
          }
        }
      }
    }
    return false;
  }