From: <bl...@us...> - 2003-04-06 16:17:22
|
Update of /cvsroot/cpptool/rfta/src/rfta In directory sc8-pr-cvs1:/tmp/cvs-serv7316/src/rfta Modified Files: InlineTempRefactoring.cpp InlineTempRefactoringTest.cpp Log Message: * modified variable decl child node range. It now encompass the ', ' that follow the variable declaration. Index: InlineTempRefactoring.cpp =================================================================== RCS file: /cvsroot/cpptool/rfta/src/rfta/InlineTempRefactoring.cpp,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** InlineTempRefactoring.cpp 5 Apr 2003 12:13:36 -0000 1.6 --- InlineTempRefactoring.cpp 6 Apr 2003 16:17:18 -0000 1.7 *************** *** 122,130 **** // check for initializer property and throw exception if not found ! if (!temporaryDecl->hasProperty(ASTNodeProperties::variableInitializerProperty)) throw RefactoringError( RefactoringError::identifierIsNotInitialized ); ASTNodePtr initializerNode_; ! initializerNode_ = temporaryDecl->getProperty(ASTNodeProperties::variableInitializerProperty); // check for value expression: --- 122,130 ---- // check for initializer property and throw exception if not found ! if (!temporaryDecl_->hasProperty(ASTNodeProperties::variableInitializerProperty)) throw RefactoringError( RefactoringError::identifierIsNotInitialized ); ASTNodePtr initializerNode_; ! initializerNode_ = temporaryDecl_->getProperty(ASTNodeProperties::variableInitializerProperty); // check for value expression: *************** *** 166,170 **** // get the expression this variable belongs to: ASTNodePtr parentExpr = boost::make_shared((*it)->getParentNode()); ! if ( parentExpr != temporaryDecl) { bool go_deeper = false; --- 166,170 ---- // get the expression this variable belongs to: ASTNodePtr parentExpr = boost::make_shared((*it)->getParentNode()); ! if ( parentExpr != temporaryDecl_) { bool go_deeper = false; *************** *** 256,264 **** try { ! temporaryDecl = context.getLocalVariableIdentifierDeclaration( localeVariableNode_ ); ASTNodePtr compoundNode = sourceNode_->getChildAt(0); ToolsBox::getLocalVariableOccurrences( compoundNode, ! temporaryDecl, sourceNode_, context, --- 256,264 ---- try { ! temporaryDecl_ = context.getLocalVariableIdentifierDeclaration( localeVariableNode_ ); ASTNodePtr compoundNode = sourceNode_->getChildAt(0); ToolsBox::getLocalVariableOccurrences( compoundNode, ! temporaryDecl_, sourceNode_, context, *************** *** 275,339 **** * or if it's part of a declaration for more than one variable. */ ! void InlineTempRefactoring::calculateTempDeclRange() { - tempDeclaredInMiddle = false; - // remove the declaration of the temporary: ! ASTNodePtr decl_expr = boost::make_shared(temporaryDecl->getParentNode()); ! int countVarDecl = 0; ! for (int j = 0; j < decl_expr->getChildCount(); j++ ) { - if (decl_expr->getChildAt(j)->getType() == ASTNodeTypes::variableDeclExpression ) - countVarDecl++; - } - // check number of variables declared in this declaration: - if ( countVarDecl == 1) // Temp is declared in its own declaration expression, range is the full expression ! rangeOfTempDeclaration = decl_expr->getRange(); ! else ! { // Temp is declared together with other variables, we need to calculate the range ! ASTNodePtr tempVarNameNode = temporaryDecl->getProperty(ASTNodeProperties::variableNameProperty); ! // find out the declaration just before the temporary is declared: ! int tempVarBegin = tempVarNameNode->getRange().getStartIndex(); ! int tempVarEnd = initValue_->getRange().getEndIndex(); ! int lastChildEndBefore = 0; ! int firstChildStartAfter = decl_expr->getRange().getEndIndex() - 1; // ends just before the semicolon ! bool childDeclaredAfterTemp = false; ! for (int j = 0; j < decl_expr->getChildCount(); j++ ) ! { ! ASTNodePtr child = decl_expr->getChildAt(j); ! if ( temporaryDecl == child ) continue; ! ASTNodePtr childNameNode = child->getProperty(ASTNodeProperties::variableNameProperty); ! ! int childEndIndex = child->getRange().getEndIndex(); ! int childStartIndex = childNameNode->getRange().getStartIndex(); ! ! // check if the current child is the last before the temporary: ! if ( childEndIndex < tempVarBegin && childEndIndex > lastChildEndBefore ) ! lastChildEndBefore = childEndIndex; ! // check if the current child is declared after the temporary: ! if ( childStartIndex > tempVarEnd && firstChildStartAfter > childStartIndex ) ! { ! childDeclaredAfterTemp = true; ! firstChildStartAfter = childStartIndex; ! } ! } ! ! // temp is declared between other in case: ! // there're declaration after its declaration && it is not the first ! tempDeclaredInMiddle = childDeclaredAfterTemp && ( lastChildEndBefore != 0 ); ! // check if temporary is declared as first: ! if ( lastChildEndBefore==0 ) ! lastChildEndBefore = tempVarBegin; // in this case delete range beginning at the temp-var-name ! ! // calculate the range: ! SourceRange merged ( lastChildEndBefore , firstChildStartAfter - lastChildEndBefore ); ! rangeOfTempDeclaration = merged; ! } } bool InlineTempRefactoring::isInitializerAtomic() { --- 275,327 ---- * or if it's part of a declaration for more than one variable. */ ! void ! InlineTempRefactoring::calculateTempDeclRange() { // remove the declaration of the temporary: ! ASTNodePtr decl_expr = boost::make_shared(temporaryDecl_->getParentNode()); ! if ( decl_expr->getChildCount() == 1) { // Temp is declared in its own declaration expression, range is the full expression ! rangeOfTempDeclaration_ = decl_expr->getRange(); ! return; ! } ! // Temp is declared together with other variables, we need to calculate the range ! ASTNodePtr tempVarNameNode = temporaryDecl_->getProperty(ASTNodeProperties::variableNameProperty); ! int startIndex = temporaryDecl_->getRange().getStartIndex(); ! int endIndex = temporaryDecl_->getRange().getEndIndex(); ! int varDeclIndex = findTempIndexInDeclaration(); ! if ( varDeclIndex == decl_expr->getChildCount()-1 ) ! { ! ASTNodePtr previousVarNode = decl_expr->getChildAt( varDeclIndex -1 ); ! ASTNodePtr previousEndNode = previousVarNode->getProperty( ASTNodeProperties::typeDeclSuffixProperty ); ! if ( !previousEndNode ) ! previousEndNode = previousVarNode->getProperty( ASTNodeProperties::variableNameProperty ); ! startIndex = previousEndNode->getRange().getEndIndex(); ! } ! rangeOfTempDeclaration_ = SourceRange( startIndex, endIndex - startIndex ); } + + int + InlineTempRefactoring::findTempIndexInDeclaration() + { + ASTNodePtr tempVarNameNode = temporaryDecl_->getProperty( ASTNodeProperties::variableNameProperty ); + ASTNodePtr decl_expr = boost::make_shared( temporaryDecl_->getParentNode() ); + for ( int index =0; index < decl_expr->getChildCount(); ++index ) + { + ASTNodePtr childNode = decl_expr->getChildAt( index ); + ASTNodePtr childNameNode = childNode->getProperty(ASTNodeProperties::variableNameProperty); + if ( tempVarNameNode->getRange() == childNameNode->getRange() ) + return index; + } + + // should never happen + throw std::logic_error( "InlineTempRefactoring::findTempIndexInDeclaration(): declared local variable not found" ); + } + + bool InlineTempRefactoring::isInitializerAtomic() { *************** *** 361,368 **** calculateTempDeclRange(); ! if (tempDeclaredInMiddle) ! transforms.add( *new ReplaceTextTransform( rangeOfTempDeclaration , " , " ) ); ! else ! transforms.add( *new ReplaceTextTransform( rangeOfTempDeclaration , "" ) ); std::string newtext; --- 349,353 ---- calculateTempDeclRange(); ! transforms.add( *new ReplaceTextTransform( rangeOfTempDeclaration_ , "" ) ); std::string newtext; *************** *** 381,385 **** { ASTNodePtr parentExpr = boost::make_shared((*it)->getParentNode()); ! if ( parentExpr != temporaryDecl) { SourceRange range = (*it)->getRange(); --- 366,370 ---- { ASTNodePtr parentExpr = boost::make_shared((*it)->getParentNode()); ! if ( parentExpr != temporaryDecl_) { SourceRange range = (*it)->getRange(); Index: InlineTempRefactoringTest.cpp =================================================================== RCS file: /cvsroot/cpptool/rfta/src/rfta/InlineTempRefactoringTest.cpp,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** InlineTempRefactoringTest.cpp 5 Apr 2003 12:13:36 -0000 1.4 --- InlineTempRefactoringTest.cpp 6 Apr 2003 16:17:19 -0000 1.5 *************** *** 226,230 **** builder_->add( "{" ! " double y;" ); builder_->addKeyingMid( " double z , ", "x"," = 4;" , "selection" ); builder_->add( " y += y * x;"); --- 226,230 ---- builder_->add( "{" ! " double y;" ); builder_->addKeyingMid( " double z , ", "x"," = 4;" , "selection" ); builder_->add( " y += y * x;"); *************** *** 275,279 **** "{" " double y;" ); ! builder_->addKeyingMid( " double v , ", "x"," = 4, z;", "selection" ); builder_->add( " y += y * x;"); builder_->add( --- 275,279 ---- "{" " double y;" ); ! builder_->addKeyingMid( " double v, ", "x"," = 4, z;", "selection" ); builder_->add( " y += y * x;"); builder_->add( *************** *** 285,289 **** std::string expectedSource( "{" ! " double y; double v , z;" " y += y * 4;" " return 4 * getQuantity();" --- 285,289 ---- std::string expectedSource( "{" ! " double y; double v, z;" " y += y * 4;" " return 4 * getQuantity();" |