1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-11 10:15:39 +02:00

fix for error caused by index based TU in extract constant

This commit is contained in:
Emanuel Graf 2008-08-12 09:14:03 +00:00
parent d7229a14f2
commit ca75909f2c
15 changed files with 171 additions and 65 deletions

View file

@ -42,15 +42,19 @@ public class ExtractConstantRefactoringTest extends RefactoringTest {
IFile refFile = project.getFile(fileName);
NameNVisibilityInformation info = new NameNVisibilityInformation();
ExtractConstantRefactoring refactoring = new ExtractConstantRefactoring( refFile, selection, info);
RefactoringStatus checkInitialConditions = refactoring.checkInitialConditions(NULL_PROGRESS_MONITOR);
assertConditionsOk(checkInitialConditions);
info.setName("theAnswer"); //$NON-NLS-1$
info.setVisibility(visibility);
Change createChange = refactoring.createChange(NULL_PROGRESS_MONITOR);
RefactoringStatus finalConditions = refactoring.checkFinalConditions(NULL_PROGRESS_MONITOR);
assertConditionsOk(finalConditions);
createChange.perform(NULL_PROGRESS_MONITOR);
try {
refactoring.lockIndex();
RefactoringStatus checkInitialConditions = refactoring.checkInitialConditions(NULL_PROGRESS_MONITOR);
assertConditionsOk(checkInitialConditions);
info.setName("theAnswer"); //$NON-NLS-1$
info.setVisibility(visibility);
Change createChange = refactoring.createChange(NULL_PROGRESS_MONITOR);
RefactoringStatus finalConditions = refactoring.checkFinalConditions(NULL_PROGRESS_MONITOR);
assertConditionsOk(finalConditions);
createChange.perform(NULL_PROGRESS_MONITOR);
}finally {
refactoring.unlockIndex();
}
compareFiles(fileMap);
}

View file

@ -43,8 +43,9 @@ public class ImplementMethodRefactoringTest extends RefactoringTest {
IFile refFile = project.getFile(fileName);
CRefactoring refactoring = new ImplementMethodRefactoring(refFile, selection, null);
refactoring.lockIndex();
try {
refactoring.lockIndex();
RefactoringStatus checkInitialConditions = refactoring.checkInitialConditions(NULL_PROGRESS_MONITOR);
if(initialWarnings == 0) {

View file

@ -38,7 +38,7 @@ public class TranslationUnitHelperTest extends RefactoringTest {
@Override
protected void runTest() throws Throwable {
IFile file = project.getFile(fileName);
IASTTranslationUnit unit = TranslationUnitHelper.loadTranslationUnit(file);
IASTTranslationUnit unit = TranslationUnitHelper.loadTranslationUnit(file, false);
IASTNode firstNode = TranslationUnitHelper.getFirstNode(unit);
assertEquals(offset, firstNode.getNodeLocations()[0].getNodeOffset());
}

View file

@ -90,9 +90,9 @@ public class MethodContext {
}
public static boolean isSameClass(ICPPASTQualifiedName qname1, ICPPASTQualifiedName qname2) {
ICPPInternalBinding bind1 = getClassBinding(qname1);
ICPPInternalBinding bind2 = getClassBinding(qname2);
return isSameClass(bind1,bind2);
ICPPClassType bind1 = getClassBinding(qname1);
ICPPClassType bind2 = getClassBinding(qname2);
return bind1.equals(bind2);
}
public static boolean isSameOrSubClass(MethodContext context1, MethodContext contextOfSameOrSubclass) {
@ -194,9 +194,9 @@ public class MethodContext {
return null;
}
private static ICPPInternalBinding getClassBinding(ICPPASTQualifiedName qname){
private static ICPPClassType getClassBinding(ICPPASTQualifiedName qname){
IASTName classname = qname.getNames()[qname.getNames().length - 2];
ICPPInternalBinding bind = (ICPPInternalBinding)classname.resolveBinding();
ICPPClassType bind = (ICPPClassType)classname.resolveBinding();
return bind;
}
}

View file

@ -245,15 +245,15 @@ public class ExtractConstantRefactoring extends CRefactoring {
@Override
protected void collectModifications(IProgressMonitor pm, ModificationCollector collector)
throws CoreException, OperationCanceledException {
throws CoreException, OperationCanceledException{
MethodContext context = NodeHelper.findMethodContext(target);
MethodContext context = NodeHelper.findMethodContext(target, getIndex());
Collection<IASTExpression> locLiteralsToReplace = new ArrayList<IASTExpression>();
if(context.getType() == MethodContext.ContextType.METHOD){
for (IASTExpression expression : literalsToReplace) {
MethodContext exprContext = NodeHelper.findMethodContext(expression);
MethodContext exprContext = NodeHelper.findMethodContext(expression, getIndex());
if(exprContext.getType() == MethodContext.ContextType.METHOD){
if( MethodContext.isSameClass(exprContext.getMethodQName(), context.getMethodQName())){
locLiteralsToReplace.add(expression);

View file

@ -180,7 +180,7 @@ public class ExtractFunctionRefactoring extends CRefactoring {
}
info.setDeclarator(getDeclaration(container.getNodesToWrite().get(0)));
MethodContext context = NodeHelper.findMethodContext(container.getNodesToWrite().get(0));
MethodContext context = NodeHelper.findMethodContext(container.getNodesToWrite().get(0), getIndex());
info.setMethodContext(context);
sm.done();
return status;
@ -237,7 +237,7 @@ public class ExtractFunctionRefactoring extends CRefactoring {
final IASTName astMethodName = new CPPASTName(info.getMethodName()
.toCharArray());
MethodContext context = NodeHelper.findMethodContext(container.getNodesToWrite().get(0));
MethodContext context = NodeHelper.findMethodContext(container.getNodesToWrite().get(0), getIndex());
if (context.getType() == ContextType.METHOD) {
ICPPASTCompositeTypeSpecifier classDeclaration = (ICPPASTCompositeTypeSpecifier) context
@ -266,7 +266,7 @@ public class ExtractFunctionRefactoring extends CRefactoring {
final IASTName astMethodName = new CPPASTName(info.getMethodName()
.toCharArray());
MethodContext context = NodeHelper.findMethodContext(container.getNodesToWrite().get(0));
MethodContext context = NodeHelper.findMethodContext(container.getNodesToWrite().get(0), getIndex());
// Create Declaration in Class
if (context.getType() == ContextType.METHOD) {

View file

@ -185,7 +185,7 @@ public class HideMethodRefactoring extends CRefactoring {
}
}
IASTTranslationUnit transUtmp = TranslationUnitHelper.loadTranslationUnit(declData.filename);
IASTTranslationUnit transUtmp = TranslationUnitHelper.loadTranslationUnit(declData.filename, false);
IASTName expName = ExpressionFinder.findExpressionInTranslationUnit(transUtmp, pdomref);
IASTFunctionDeclarator funcDec = findEnclosingFunction(expName);

View file

@ -145,19 +145,20 @@ public class ImplementMethodRefactoring extends CRefactoring {
}
}
private void createFunctionDefinition(IASTTranslationUnit unit) {
private void createFunctionDefinition(IASTTranslationUnit unit) throws CoreException {
createFunctionDefinition(
methodDeclaration.getDeclSpecifier(),
(ICPPASTFunctionDeclarator) methodDeclaration.getDeclarators()[0],
methodDeclaration.getParent(), unit);
}
public IASTDeclaration createFunctionDefinition() {
public IASTDeclaration createFunctionDefinition() throws CoreException {
createFunctionDefinition(unit);
return createdMethodDefinition;
}
private void createFunctionDefinition(IASTDeclSpecifier declSpecifier, ICPPASTFunctionDeclarator functionDeclarator, IASTNode declarationParent, IASTTranslationUnit unit) {
private void createFunctionDefinition(IASTDeclSpecifier declSpecifier, ICPPASTFunctionDeclarator functionDeclarator,
IASTNode declarationParent, IASTTranslationUnit unit) throws CoreException {
IASTFunctionDefinition func = new CPPASTFunctionDefinition();
func.setParent(unit);
@ -204,7 +205,8 @@ public class ImplementMethodRefactoring extends CRefactoring {
createdMethodDefinition = func;
}
private ICPPASTQualifiedName createQualifiedNameFor(IASTFunctionDeclarator functionDeclarator, IASTNode declarationParent) {
private ICPPASTQualifiedName createQualifiedNameFor(IASTFunctionDeclarator functionDeclarator, IASTNode declarationParent)
throws CoreException {
int insertOffset = insertLocation.getInsertPosition();
return NameHelper.createQualifiedNameFor(functionDeclarator.getName(), file, region.getOffset(), insertLocation.getInsertFile(), insertOffset);
}

View file

@ -13,6 +13,7 @@
package org.eclipse.cdt.internal.ui.refactoring.implementmethod;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
@ -40,7 +41,7 @@ public class InsertLocation {
return nodeToInsertBefore;
}
public IASTNode getPartenOfNodeToInsertBefore() {
public IASTNode getPartenOfNodeToInsertBefore() throws CoreException{
IASTNode affectedNode = getAffectedNode();
return (affectedNode != null) ? affectedNode.getParent() : getTargetTranslationUnit();
}
@ -70,7 +71,7 @@ public class InsertLocation {
return insertFile != null;
}
public IASTTranslationUnit getTargetTranslationUnit() {
public IASTTranslationUnit getTargetTranslationUnit() throws CoreException{
if(targetTranslationUnit == null) {
loadTargetTranslationUnit();
}
@ -78,12 +79,12 @@ public class InsertLocation {
}
private void loadTargetTranslationUnit() {
private void loadTargetTranslationUnit() throws CoreException{
IASTNode affectedNode = getAffectedNode();
if(affectedNode != null) {
targetTranslationUnit = affectedNode.getTranslationUnit();
} else if(hasFile()) {
targetTranslationUnit = TranslationUnitHelper.loadTranslationUnit(insertFile);
targetTranslationUnit = TranslationUnitHelper.loadTranslationUnit(insertFile, false);
}
}

View file

@ -41,7 +41,7 @@ public class DeclarationFinder {
}
String filename2 = pdomref[0].getFileLocation().getFileName();
IASTTranslationUnit transUnit = TranslationUnitHelper.loadTranslationUnit(filename2);
IASTTranslationUnit transUnit = TranslationUnitHelper.loadTranslationUnit(filename2, false);
IASTName declName = DeclarationFinder.findDeclarationInTranslationUnit(transUnit, pdomref[0]);
return new DeclarationFinderDO(allNamesPDom, transUnit, filename2, declName);

View file

@ -44,7 +44,7 @@ import org.eclipse.cdt.internal.ui.refactoring.Container;
*/
public class DefinitionFinder {
public static IASTName getDefinition(IASTSimpleDeclaration simpleDeclaration, IFile file) throws CoreException {
public static IASTName getDefinition(IASTSimpleDeclaration simpleDeclaration, IFile file) throws CoreException{
IASTDeclarator declarator = simpleDeclaration.getDeclarators()[0];
IBinding resolveBinding = declarator.getName().resolveBinding();
return DefinitionFinder.getDefinition(declarator.getName(), resolveBinding, file);
@ -74,7 +74,7 @@ public class DefinitionFinder {
IASTTranslationUnit transUnit;
if (!parsedFiles.containsKey(pdomref[0].getFileLocation().getFileName())) {
String filename = pdomref[0].getFileLocation().getFileName();
transUnit = TranslationUnitHelper.loadTranslationUnit(filename);
transUnit = TranslationUnitHelper.loadTranslationUnit(filename, false);
} else {
transUnit = parsedFiles.get(pdomref[0].getFileLocation().getFileName());
}

View file

@ -14,6 +14,7 @@ package org.eclipse.cdt.internal.ui.refactoring.utils;
import java.util.regex.Pattern;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTName;
@ -59,8 +60,10 @@ public class NameHelper {
* @param insertFile the target file in which the definition is inserted
* @param insertLocation
* @return the correct name for the target
* @throws CoreException
*/
public static ICPPASTQualifiedName createQualifiedNameFor(IASTName declaratorName, IFile declarationFile, int selectionOffset, IFile insertFile, int insertLocation) {
public static ICPPASTQualifiedName createQualifiedNameFor(IASTName declaratorName, IFile declarationFile, int selectionOffset, IFile insertFile, int insertLocation)
throws CoreException {
ICPPASTQualifiedName qname = new CPPASTQualifiedName();
IASTName[] declarationNames = NamespaceHelper.getSurroundingNamespace(declarationFile, selectionOffset).getNames();

View file

@ -11,6 +11,9 @@
*******************************************************************************/
package org.eclipse.cdt.internal.ui.refactoring.utils;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
@ -19,12 +22,12 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceDefinition;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateParameter;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTNamedTypeSpecifier;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTQualifiedName;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTSimpleTypeTemplateParameter;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTTemplateId;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTTypeId;
import org.eclipse.core.resources.IFile;
/**
* Helper class to find Namespace informations.
@ -39,12 +42,13 @@ public class NamespaceHelper {
* @param insertFile
* @param offset
* @return ICPPASTQualifiedName with the names of all namespaces
* @throws CoreException
*/
public static ICPPASTQualifiedName getSurroundingNamespace(final IFile insertFile, final int offset) {
public static ICPPASTQualifiedName getSurroundingNamespace(final IFile insertFile, final int offset) throws CoreException {
final CPPASTQualifiedName qualifiedName = new CPPASTQualifiedName();
TranslationUnitHelper.loadTranslationUnit(insertFile).accept(new CPPASTAllVisitor() {
TranslationUnitHelper.loadTranslationUnit(insertFile, false).accept(new CPPASTAllVisitor() {
@Override
public int visit(IASTDeclSpecifier declSpec) {

View file

@ -11,6 +11,7 @@
*******************************************************************************/
package org.eclipse.cdt.internal.ui.refactoring.utils;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;
import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier;
@ -27,6 +28,9 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.index.IIndexName;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTNamespaceDefinition;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTTranslationUnit;
@ -96,25 +100,36 @@ public class NodeHelper {
return null;
}
public static MethodContext findMethodContext(IASTNode node) {
public static MethodContext findMethodContext(IASTNode node, IIndex index) throws CoreException{
IASTTranslationUnit translationUnit = node.getTranslationUnit();
boolean found = false;
MethodContext context = new MethodContext();
context.setType(MethodContext.ContextType.NONE);
IASTName name = null;
while(node != null && !found){
node = node.getParent();
if(node instanceof IASTFunctionDeclarator){
name=((IASTFunctionDeclarator)node).getName();
found = true;
context.setType(MethodContext.ContextType.FUNCTION);
} else if (node instanceof IASTFunctionDefinition){
name=CPPVisitor.findInnermostDeclarator(((IASTFunctionDefinition)node).getDeclarator()).getName();
found = true;
context.setType(MethodContext.ContextType.FUNCTION);
}
}
if(name instanceof ICPPASTQualifiedName){
IASTName name = null;
while(node != null && !found){
node = node.getParent();
if(node instanceof IASTFunctionDeclarator){
name=((IASTFunctionDeclarator)node).getName();
found = true;
context.setType(MethodContext.ContextType.FUNCTION);
} else if (node instanceof IASTFunctionDefinition){
name=CPPVisitor.findInnermostDeclarator(((IASTFunctionDefinition)node).getDeclarator()).getName();
found = true;
context.setType(MethodContext.ContextType.FUNCTION);
}
}
if(index != null) {
getMethodContexWithIndex(index, translationUnit, context, name);
}else {
getMethodContex(translationUnit, context, name);
}
return context;
}
private static void getMethodContex(IASTTranslationUnit translationUnit,
MethodContext context, IASTName name) {
if(name instanceof ICPPASTQualifiedName){
ICPPASTQualifiedName qname =( ICPPASTQualifiedName )name;
context.setMethodQName(qname);
IBinding bind = qname.resolveBinding();
@ -128,7 +143,36 @@ public class NodeHelper {
}
}
return context;
}
private static void getMethodContexWithIndex(IIndex index,
IASTTranslationUnit translationUnit, MethodContext context,
IASTName name) throws CoreException {
if(name instanceof ICPPASTQualifiedName){
ICPPASTQualifiedName qname =( ICPPASTQualifiedName )name;
context.setMethodQName(qname);
IBinding bind = qname.resolveBinding();
if (bind instanceof ICPPMethod) {
context.setType(MethodContext.ContextType.METHOD);
IIndexName[] decl;
decl = index.findDeclarations(bind);
String tuFileLoc = translationUnit.getFileLocation().getFileName();
for (IIndexName tmpname : decl) {
IASTTranslationUnit locTu = translationUnit;
if(!tuFileLoc.equals(tmpname.getFileLocation().getFileName())) {
locTu = TranslationUnitHelper.loadTranslationUnit(tmpname.getFileLocation().getFileName(), false);
}
IASTName declName = DeclarationFinder.findDeclarationInTranslationUnit(locTu, tmpname);
if(declName != null) {
IASTNode methoddefinition = declName.getParent().getParent();
if (methoddefinition instanceof IASTSimpleDeclaration) {
context.setMethodDeclarationName(declName);
}
}
}
}
}
}
public static IASTCompoundStatement findCompoundStatementInAncestors(IASTNode node) {

View file

@ -14,9 +14,11 @@ package org.eclipse.cdt.internal.ui.refactoring.utils;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.CDOM;
import org.eclipse.cdt.core.dom.IASTServiceProvider.UnsupportedDialectException;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
@ -25,6 +27,11 @@ import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.internal.ui.refactoring.Container;
@ -35,34 +42,60 @@ import org.eclipse.cdt.internal.ui.refactoring.Container;
*
*/
public class TranslationUnitHelper {
private static final int AST_STYLE = ITranslationUnit.AST_CONFIGURE_USING_SOURCE_CONTEXT | ITranslationUnit.AST_SKIP_INDEXED_HEADERS;
/**
* @param filename to load the translation unit from
* @return the translation unit for the file or null
* @throws CoreException
*/
public static IASTTranslationUnit loadTranslationUnit(String filename) {
public static IASTTranslationUnit loadTranslationUnit(String filename, boolean useIndex) throws CoreException{
if (filename != null) {
IPath path = new Path(filename);
IFile tmpFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
return loadTranslationUnit(tmpFile);
return loadTranslationUnit(tmpFile, useIndex);
}
return null;
}
/**
* @param tmpFile to load the translation unit from
* @param file to load the translation unit from
* @return the translation unit for the file or null
* @throws CoreException
*/
public static IASTTranslationUnit loadTranslationUnit(IFile tmpFile) {
if (tmpFile != null) {
try {
IASTTranslationUnit fileUnit = CDOM.getInstance().getTranslationUnit(tmpFile, CDOM.getInstance().getCodeReaderFactory(CDOM.PARSE_SAVED_RESOURCES), true);
return fileUnit;
} catch (UnsupportedDialectException e) {
return null;
}
public static IASTTranslationUnit loadTranslationUnit(IFile file, boolean useIndex) throws CoreException {
if (file == null) {
return null;
}
if(useIndex) {
return loadIndexBasedTranslationUnit(file);
}else {
return loadFileBasedTranslationUnit(file);
}
}
private static IASTTranslationUnit loadFileBasedTranslationUnit(IFile file) {
try {
IASTTranslationUnit fileUnit = CDOM.getInstance().getTranslationUnit(file, CDOM.getInstance().getCodeReaderFactory(CDOM.PARSE_SAVED_RESOURCES), true);
return fileUnit;
} catch (UnsupportedDialectException e) {
return null;
}
}
private static IASTTranslationUnit loadIndexBasedTranslationUnit(IFile file)
throws CoreException {
IIndex index = null;
try {
index = lockIndex();
ITranslationUnit tu = (ITranslationUnit) CCorePlugin.getDefault().getCoreModel().create(file);
return tu.getAST(index, AST_STYLE);
} catch (InterruptedException e) {
CUIPlugin.log(e);
} finally {
unlockIndex(index);
}
return null;
}
@ -111,4 +144,18 @@ public class TranslationUnitHelper {
}
return firstNode;
}
private static IIndex lockIndex() throws CoreException, InterruptedException {
IIndex index;
ICProject[] projects= CoreModel.getDefault().getCModel().getCProjects();
index= CCorePlugin.getIndexManager().getIndex(projects);
index.acquireReadLock();
return index;
}
private static void unlockIndex(IIndex index) {
if (index != null) {
index.releaseReadLock();
}
}
}