Update of /cvsroot/cpptool/rfta/src/rfta In directory sc8-pr-cvs1:/tmp/cvs-serv7548/src/rfta Modified Files: IdentifierResolverContext.cpp IdentifierResolverContextTest.cpp IdentifierResolverContextTest.h RenameTempRefactoringTest.cpp RenameTempRefactoringTest.h Log Message: * fixed crash that occurred when trying to resolve an identifier after it has gone out of scope: { { int index; } index; } => crashed * added test to check that refactoring fail if identifier out of scope Index: IdentifierResolverContext.cpp =================================================================== RCS file: /cvsroot/cpptool/rfta/src/rfta/IdentifierResolverContext.cpp,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** IdentifierResolverContext.cpp 27 Apr 2003 22:02:02 -0000 1.7 --- IdentifierResolverContext.cpp 10 May 2003 10:55:14 -0000 1.8 *************** *** 7,10 **** --- 7,11 ---- #include "IdentifierResolverContext.h" #include <rfta/parser/ASTNodes.h> + #include <stdexcept> *************** *** 32,35 **** --- 33,40 ---- IdentifierResolverContext::exitLocalVariableScope() { + if ( scopeLocalVariables_.empty() ) + throw std::logic_error( "IdentifierResolverContext::exitLocalVariableScope(): " + "no matching enterNewLocalVariableScope()." ); + LocalVariables &variables = scopeLocalVariables_.back(); LocalVariables::iterator it = variables.begin(); *************** *** 38,42 **** { const ASTNodePtr &node = *it++; ! visibleLocalVariables_[ getLocalVariableName(node) ].pop_front(); } --- 43,58 ---- { const ASTNodePtr &node = *it++; ! VisibleLocalVariables::iterator itName = ! visibleLocalVariables_.find( getLocalVariableName(node) ); ! if ( itName == visibleLocalVariables_.end() ) ! { ! throw std::logic_error( "IdentifierResolverContext::exitLocalVariableScope(): " ! "inconsitent state, variable declared but not in visible variable list." ); ! } ! ! if ( itName->second.size() == 1 ) ! visibleLocalVariables_.erase( itName ); ! else ! itName->second.pop_front(); } *************** *** 69,72 **** --- 85,95 ---- if ( it == visibleLocalVariables_.end() ) return; + + if ( it->second.empty() ) + { + throw std::logic_error( "IdentifierResolverContext::resolveUnqualifiedIdentifier( " + + identifierNode->getBlankedText() + "): variable found in " + "list of visible variable, but no declaration found!" ); + } const ASTNodePtr &localVariableDeclNode = it->second.front(); Index: IdentifierResolverContextTest.cpp =================================================================== RCS file: /cvsroot/cpptool/rfta/src/rfta/IdentifierResolverContextTest.cpp,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** IdentifierResolverContextTest.cpp 27 Apr 2003 22:02:02 -0000 1.4 --- IdentifierResolverContextTest.cpp 10 May 2003 10:55:14 -0000 1.5 *************** *** 165,167 **** --- 165,197 ---- + void + IdentifierResolverContextTest::testLocalVariableResolutionFailIfOutOfScope() + { + builder_->add( "{" ); + builder_->add( " {" ); + builder_->addKeyingMid( " int ", "x", ";", "x.0" ); + builder_->add( " }" ); + builder_->addKeyingMid( " ", "x", ";", "x.1" ); + builder_->add( "}" ); + + initializeResolver(); + ASTNodePtr x0 = getVariableNode( "x.0" ); + ASTNodePtr x1 = getIdentifierNode( "x.1" ); + context_->enterNewLocalVariableScope(); + + context_->enterNewLocalVariableScope(); + context_->declareLocalVariable( x0 ); + context_->exitLocalVariableScope(); + + context_->resolveUnqualifiedIdentifier( x1 ); + context_->exitLocalVariableScope(); + + CPPUNIT_ASSERT( context_->isLocalVariableIdentifier( getIdentifierNode( "x.0" ) ) ); + CPPUNIT_ASSERT( x0 == + context_->getLocalVariableIdentifierDeclaration( getIdentifierNode( "x.0" ) ) ); + + CPPUNIT_ASSERT( !context_->isLocalVariableIdentifier( x1 ) ); + } + + } // namespace Refactoring Index: IdentifierResolverContextTest.h =================================================================== RCS file: /cvsroot/cpptool/rfta/src/rfta/IdentifierResolverContextTest.h,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** IdentifierResolverContextTest.h 16 Dec 2002 20:09:46 -0000 1.3 --- IdentifierResolverContextTest.h 10 May 2003 10:55:14 -0000 1.4 *************** *** 22,25 **** --- 22,26 ---- CPPUNIT_TEST( testLocalVariableResolutionWithSubScope ); CPPUNIT_TEST( testDeclareNewLocalVariableInSubScope ); + CPPUNIT_TEST( testLocalVariableResolutionFailIfOutOfScope ); CPPUNIT_TEST_SUITE_END(); *************** *** 38,41 **** --- 39,43 ---- void testLocalVariableResolutionWithSubScope(); void testDeclareNewLocalVariableInSubScope(); + void testLocalVariableResolutionFailIfOutOfScope(); private: Index: RenameTempRefactoringTest.cpp =================================================================== RCS file: /cvsroot/cpptool/rfta/src/rfta/RenameTempRefactoringTest.cpp,v retrieving revision 1.15 retrieving revision 1.16 diff -C2 -d -r1.15 -r1.16 *** RenameTempRefactoringTest.cpp 5 Apr 2003 12:14:45 -0000 1.15 --- RenameTempRefactoringTest.cpp 10 May 2003 10:55:14 -0000 1.16 *************** *** 8,11 **** --- 8,12 ---- #include <rfta/refactoring/RenameTempRefactoring.h> #include <rfta/refactoring/PlainTextDocument.h> + #include <rfta/refactoring/RefactoringError.h> *************** *** 242,245 **** --- 243,270 ---- RFTA_ASSERT_EQUAL( expectedSource, actualSource ); } + + + void + RenameTempRefactoringTest::testFailIfDeclarationNotInScope() + { + builder_->add( + "{" + " { int index; }" + ); + builder_->addKeyingMid( " ", "index", ";", "selection" ); + builder_->add( + "}" ); + + try + { + applyRefactoring( "index2", "index" ); + CPPUNIT_FAIL( "applyRefactoring() should have thrown a RefactoringError exception." ); + } + catch ( RefactoringError &e ) + { + RFTA_ASSERT_EQUAL( RefactoringError::identifierIsNotLocalVariable, e.getCause() ); + } + } + Index: RenameTempRefactoringTest.h =================================================================== RCS file: /cvsroot/cpptool/rfta/src/rfta/RenameTempRefactoringTest.h,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** RenameTempRefactoringTest.h 5 Apr 2003 12:14:45 -0000 1.9 --- RenameTempRefactoringTest.h 10 May 2003 10:55:14 -0000 1.10 *************** *** 26,29 **** --- 26,30 ---- CPPUNIT_TEST( testRenameInVariableInitializer ); CPPUNIT_TEST( testSelectInSubScope ); + CPPUNIT_TEST( testFailIfDeclarationNotInScope ); CPPUNIT_TEST_SUITE_END(); *************** *** 47,50 **** --- 48,53 ---- void testSelectInSubScope(); + + void testFailIfDeclarationNotInScope(); private: |