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:
parent
d7229a14f2
commit
ca75909f2c
15 changed files with 171 additions and 65 deletions
|
@ -42,15 +42,19 @@ public class ExtractConstantRefactoringTest extends RefactoringTest {
|
||||||
IFile refFile = project.getFile(fileName);
|
IFile refFile = project.getFile(fileName);
|
||||||
NameNVisibilityInformation info = new NameNVisibilityInformation();
|
NameNVisibilityInformation info = new NameNVisibilityInformation();
|
||||||
ExtractConstantRefactoring refactoring = new ExtractConstantRefactoring( refFile, selection, info);
|
ExtractConstantRefactoring refactoring = new ExtractConstantRefactoring( refFile, selection, info);
|
||||||
RefactoringStatus checkInitialConditions = refactoring.checkInitialConditions(NULL_PROGRESS_MONITOR);
|
try {
|
||||||
assertConditionsOk(checkInitialConditions);
|
refactoring.lockIndex();
|
||||||
info.setName("theAnswer"); //$NON-NLS-1$
|
RefactoringStatus checkInitialConditions = refactoring.checkInitialConditions(NULL_PROGRESS_MONITOR);
|
||||||
info.setVisibility(visibility);
|
assertConditionsOk(checkInitialConditions);
|
||||||
Change createChange = refactoring.createChange(NULL_PROGRESS_MONITOR);
|
info.setName("theAnswer"); //$NON-NLS-1$
|
||||||
RefactoringStatus finalConditions = refactoring.checkFinalConditions(NULL_PROGRESS_MONITOR);
|
info.setVisibility(visibility);
|
||||||
assertConditionsOk(finalConditions);
|
Change createChange = refactoring.createChange(NULL_PROGRESS_MONITOR);
|
||||||
createChange.perform(NULL_PROGRESS_MONITOR);
|
RefactoringStatus finalConditions = refactoring.checkFinalConditions(NULL_PROGRESS_MONITOR);
|
||||||
|
assertConditionsOk(finalConditions);
|
||||||
|
createChange.perform(NULL_PROGRESS_MONITOR);
|
||||||
|
}finally {
|
||||||
|
refactoring.unlockIndex();
|
||||||
|
}
|
||||||
compareFiles(fileMap);
|
compareFiles(fileMap);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,8 +43,9 @@ public class ImplementMethodRefactoringTest extends RefactoringTest {
|
||||||
IFile refFile = project.getFile(fileName);
|
IFile refFile = project.getFile(fileName);
|
||||||
|
|
||||||
CRefactoring refactoring = new ImplementMethodRefactoring(refFile, selection, null);
|
CRefactoring refactoring = new ImplementMethodRefactoring(refFile, selection, null);
|
||||||
refactoring.lockIndex();
|
|
||||||
try {
|
try {
|
||||||
|
refactoring.lockIndex();
|
||||||
RefactoringStatus checkInitialConditions = refactoring.checkInitialConditions(NULL_PROGRESS_MONITOR);
|
RefactoringStatus checkInitialConditions = refactoring.checkInitialConditions(NULL_PROGRESS_MONITOR);
|
||||||
|
|
||||||
if(initialWarnings == 0) {
|
if(initialWarnings == 0) {
|
||||||
|
|
|
@ -38,7 +38,7 @@ public class TranslationUnitHelperTest extends RefactoringTest {
|
||||||
@Override
|
@Override
|
||||||
protected void runTest() throws Throwable {
|
protected void runTest() throws Throwable {
|
||||||
IFile file = project.getFile(fileName);
|
IFile file = project.getFile(fileName);
|
||||||
IASTTranslationUnit unit = TranslationUnitHelper.loadTranslationUnit(file);
|
IASTTranslationUnit unit = TranslationUnitHelper.loadTranslationUnit(file, false);
|
||||||
IASTNode firstNode = TranslationUnitHelper.getFirstNode(unit);
|
IASTNode firstNode = TranslationUnitHelper.getFirstNode(unit);
|
||||||
assertEquals(offset, firstNode.getNodeLocations()[0].getNodeOffset());
|
assertEquals(offset, firstNode.getNodeLocations()[0].getNodeOffset());
|
||||||
}
|
}
|
||||||
|
|
|
@ -90,9 +90,9 @@ public class MethodContext {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isSameClass(ICPPASTQualifiedName qname1, ICPPASTQualifiedName qname2) {
|
public static boolean isSameClass(ICPPASTQualifiedName qname1, ICPPASTQualifiedName qname2) {
|
||||||
ICPPInternalBinding bind1 = getClassBinding(qname1);
|
ICPPClassType bind1 = getClassBinding(qname1);
|
||||||
ICPPInternalBinding bind2 = getClassBinding(qname2);
|
ICPPClassType bind2 = getClassBinding(qname2);
|
||||||
return isSameClass(bind1,bind2);
|
return bind1.equals(bind2);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isSameOrSubClass(MethodContext context1, MethodContext contextOfSameOrSubclass) {
|
public static boolean isSameOrSubClass(MethodContext context1, MethodContext contextOfSameOrSubclass) {
|
||||||
|
@ -194,9 +194,9 @@ public class MethodContext {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ICPPInternalBinding getClassBinding(ICPPASTQualifiedName qname){
|
private static ICPPClassType getClassBinding(ICPPASTQualifiedName qname){
|
||||||
IASTName classname = qname.getNames()[qname.getNames().length - 2];
|
IASTName classname = qname.getNames()[qname.getNames().length - 2];
|
||||||
ICPPInternalBinding bind = (ICPPInternalBinding)classname.resolveBinding();
|
ICPPClassType bind = (ICPPClassType)classname.resolveBinding();
|
||||||
return bind;
|
return bind;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -245,15 +245,15 @@ public class ExtractConstantRefactoring extends CRefactoring {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void collectModifications(IProgressMonitor pm, ModificationCollector collector)
|
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>();
|
Collection<IASTExpression> locLiteralsToReplace = new ArrayList<IASTExpression>();
|
||||||
|
|
||||||
if(context.getType() == MethodContext.ContextType.METHOD){
|
if(context.getType() == MethodContext.ContextType.METHOD){
|
||||||
|
|
||||||
for (IASTExpression expression : literalsToReplace) {
|
for (IASTExpression expression : literalsToReplace) {
|
||||||
MethodContext exprContext = NodeHelper.findMethodContext(expression);
|
MethodContext exprContext = NodeHelper.findMethodContext(expression, getIndex());
|
||||||
if(exprContext.getType() == MethodContext.ContextType.METHOD){
|
if(exprContext.getType() == MethodContext.ContextType.METHOD){
|
||||||
if( MethodContext.isSameClass(exprContext.getMethodQName(), context.getMethodQName())){
|
if( MethodContext.isSameClass(exprContext.getMethodQName(), context.getMethodQName())){
|
||||||
locLiteralsToReplace.add(expression);
|
locLiteralsToReplace.add(expression);
|
||||||
|
|
|
@ -180,7 +180,7 @@ public class ExtractFunctionRefactoring extends CRefactoring {
|
||||||
}
|
}
|
||||||
|
|
||||||
info.setDeclarator(getDeclaration(container.getNodesToWrite().get(0)));
|
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);
|
info.setMethodContext(context);
|
||||||
sm.done();
|
sm.done();
|
||||||
return status;
|
return status;
|
||||||
|
@ -237,7 +237,7 @@ public class ExtractFunctionRefactoring extends CRefactoring {
|
||||||
|
|
||||||
final IASTName astMethodName = new CPPASTName(info.getMethodName()
|
final IASTName astMethodName = new CPPASTName(info.getMethodName()
|
||||||
.toCharArray());
|
.toCharArray());
|
||||||
MethodContext context = NodeHelper.findMethodContext(container.getNodesToWrite().get(0));
|
MethodContext context = NodeHelper.findMethodContext(container.getNodesToWrite().get(0), getIndex());
|
||||||
|
|
||||||
if (context.getType() == ContextType.METHOD) {
|
if (context.getType() == ContextType.METHOD) {
|
||||||
ICPPASTCompositeTypeSpecifier classDeclaration = (ICPPASTCompositeTypeSpecifier) context
|
ICPPASTCompositeTypeSpecifier classDeclaration = (ICPPASTCompositeTypeSpecifier) context
|
||||||
|
@ -266,7 +266,7 @@ public class ExtractFunctionRefactoring extends CRefactoring {
|
||||||
final IASTName astMethodName = new CPPASTName(info.getMethodName()
|
final IASTName astMethodName = new CPPASTName(info.getMethodName()
|
||||||
.toCharArray());
|
.toCharArray());
|
||||||
|
|
||||||
MethodContext context = NodeHelper.findMethodContext(container.getNodesToWrite().get(0));
|
MethodContext context = NodeHelper.findMethodContext(container.getNodesToWrite().get(0), getIndex());
|
||||||
|
|
||||||
// Create Declaration in Class
|
// Create Declaration in Class
|
||||||
if (context.getType() == ContextType.METHOD) {
|
if (context.getType() == ContextType.METHOD) {
|
||||||
|
|
|
@ -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);
|
IASTName expName = ExpressionFinder.findExpressionInTranslationUnit(transUtmp, pdomref);
|
||||||
|
|
||||||
IASTFunctionDeclarator funcDec = findEnclosingFunction(expName);
|
IASTFunctionDeclarator funcDec = findEnclosingFunction(expName);
|
||||||
|
|
|
@ -145,19 +145,20 @@ public class ImplementMethodRefactoring extends CRefactoring {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createFunctionDefinition(IASTTranslationUnit unit) {
|
private void createFunctionDefinition(IASTTranslationUnit unit) throws CoreException {
|
||||||
createFunctionDefinition(
|
createFunctionDefinition(
|
||||||
methodDeclaration.getDeclSpecifier(),
|
methodDeclaration.getDeclSpecifier(),
|
||||||
(ICPPASTFunctionDeclarator) methodDeclaration.getDeclarators()[0],
|
(ICPPASTFunctionDeclarator) methodDeclaration.getDeclarators()[0],
|
||||||
methodDeclaration.getParent(), unit);
|
methodDeclaration.getParent(), unit);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IASTDeclaration createFunctionDefinition() {
|
public IASTDeclaration createFunctionDefinition() throws CoreException {
|
||||||
createFunctionDefinition(unit);
|
createFunctionDefinition(unit);
|
||||||
return createdMethodDefinition;
|
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();
|
IASTFunctionDefinition func = new CPPASTFunctionDefinition();
|
||||||
func.setParent(unit);
|
func.setParent(unit);
|
||||||
|
@ -204,7 +205,8 @@ public class ImplementMethodRefactoring extends CRefactoring {
|
||||||
createdMethodDefinition = func;
|
createdMethodDefinition = func;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ICPPASTQualifiedName createQualifiedNameFor(IASTFunctionDeclarator functionDeclarator, IASTNode declarationParent) {
|
private ICPPASTQualifiedName createQualifiedNameFor(IASTFunctionDeclarator functionDeclarator, IASTNode declarationParent)
|
||||||
|
throws CoreException {
|
||||||
int insertOffset = insertLocation.getInsertPosition();
|
int insertOffset = insertLocation.getInsertPosition();
|
||||||
return NameHelper.createQualifiedNameFor(functionDeclarator.getName(), file, region.getOffset(), insertLocation.getInsertFile(), insertOffset);
|
return NameHelper.createQualifiedNameFor(functionDeclarator.getName(), file, region.getOffset(), insertLocation.getInsertFile(), insertOffset);
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
package org.eclipse.cdt.internal.ui.refactoring.implementmethod;
|
package org.eclipse.cdt.internal.ui.refactoring.implementmethod;
|
||||||
|
|
||||||
import org.eclipse.core.resources.IFile;
|
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.IASTNode;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
||||||
|
@ -40,7 +41,7 @@ public class InsertLocation {
|
||||||
return nodeToInsertBefore;
|
return nodeToInsertBefore;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IASTNode getPartenOfNodeToInsertBefore() {
|
public IASTNode getPartenOfNodeToInsertBefore() throws CoreException{
|
||||||
IASTNode affectedNode = getAffectedNode();
|
IASTNode affectedNode = getAffectedNode();
|
||||||
return (affectedNode != null) ? affectedNode.getParent() : getTargetTranslationUnit();
|
return (affectedNode != null) ? affectedNode.getParent() : getTargetTranslationUnit();
|
||||||
}
|
}
|
||||||
|
@ -70,7 +71,7 @@ public class InsertLocation {
|
||||||
return insertFile != null;
|
return insertFile != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IASTTranslationUnit getTargetTranslationUnit() {
|
public IASTTranslationUnit getTargetTranslationUnit() throws CoreException{
|
||||||
if(targetTranslationUnit == null) {
|
if(targetTranslationUnit == null) {
|
||||||
loadTargetTranslationUnit();
|
loadTargetTranslationUnit();
|
||||||
}
|
}
|
||||||
|
@ -78,12 +79,12 @@ public class InsertLocation {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadTargetTranslationUnit() {
|
private void loadTargetTranslationUnit() throws CoreException{
|
||||||
IASTNode affectedNode = getAffectedNode();
|
IASTNode affectedNode = getAffectedNode();
|
||||||
if(affectedNode != null) {
|
if(affectedNode != null) {
|
||||||
targetTranslationUnit = affectedNode.getTranslationUnit();
|
targetTranslationUnit = affectedNode.getTranslationUnit();
|
||||||
} else if(hasFile()) {
|
} else if(hasFile()) {
|
||||||
targetTranslationUnit = TranslationUnitHelper.loadTranslationUnit(insertFile);
|
targetTranslationUnit = TranslationUnitHelper.loadTranslationUnit(insertFile, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -41,7 +41,7 @@ public class DeclarationFinder {
|
||||||
}
|
}
|
||||||
|
|
||||||
String filename2 = pdomref[0].getFileLocation().getFileName();
|
String filename2 = pdomref[0].getFileLocation().getFileName();
|
||||||
IASTTranslationUnit transUnit = TranslationUnitHelper.loadTranslationUnit(filename2);
|
IASTTranslationUnit transUnit = TranslationUnitHelper.loadTranslationUnit(filename2, false);
|
||||||
IASTName declName = DeclarationFinder.findDeclarationInTranslationUnit(transUnit, pdomref[0]);
|
IASTName declName = DeclarationFinder.findDeclarationInTranslationUnit(transUnit, pdomref[0]);
|
||||||
|
|
||||||
return new DeclarationFinderDO(allNamesPDom, transUnit, filename2, declName);
|
return new DeclarationFinderDO(allNamesPDom, transUnit, filename2, declName);
|
||||||
|
|
|
@ -44,7 +44,7 @@ import org.eclipse.cdt.internal.ui.refactoring.Container;
|
||||||
*/
|
*/
|
||||||
public class DefinitionFinder {
|
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];
|
IASTDeclarator declarator = simpleDeclaration.getDeclarators()[0];
|
||||||
IBinding resolveBinding = declarator.getName().resolveBinding();
|
IBinding resolveBinding = declarator.getName().resolveBinding();
|
||||||
return DefinitionFinder.getDefinition(declarator.getName(), resolveBinding, file);
|
return DefinitionFinder.getDefinition(declarator.getName(), resolveBinding, file);
|
||||||
|
@ -74,7 +74,7 @@ public class DefinitionFinder {
|
||||||
IASTTranslationUnit transUnit;
|
IASTTranslationUnit transUnit;
|
||||||
if (!parsedFiles.containsKey(pdomref[0].getFileLocation().getFileName())) {
|
if (!parsedFiles.containsKey(pdomref[0].getFileLocation().getFileName())) {
|
||||||
String filename = pdomref[0].getFileLocation().getFileName();
|
String filename = pdomref[0].getFileLocation().getFileName();
|
||||||
transUnit = TranslationUnitHelper.loadTranslationUnit(filename);
|
transUnit = TranslationUnitHelper.loadTranslationUnit(filename, false);
|
||||||
} else {
|
} else {
|
||||||
transUnit = parsedFiles.get(pdomref[0].getFileLocation().getFileName());
|
transUnit = parsedFiles.get(pdomref[0].getFileLocation().getFileName());
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,7 @@ package org.eclipse.cdt.internal.ui.refactoring.utils;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import org.eclipse.core.resources.IFile;
|
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.IASTDeclSpecifier;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTName;
|
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 insertFile the target file in which the definition is inserted
|
||||||
* @param insertLocation
|
* @param insertLocation
|
||||||
* @return the correct name for the target
|
* @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();
|
ICPPASTQualifiedName qname = new CPPASTQualifiedName();
|
||||||
|
|
||||||
IASTName[] declarationNames = NamespaceHelper.getSurroundingNamespace(declarationFile, selectionOffset).getNames();
|
IASTName[] declarationNames = NamespaceHelper.getSurroundingNamespace(declarationFile, selectionOffset).getNames();
|
||||||
|
|
|
@ -11,6 +11,9 @@
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package org.eclipse.cdt.internal.ui.refactoring.utils;
|
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.IASTDeclSpecifier;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTName;
|
import org.eclipse.cdt.core.dom.ast.IASTName;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTNode;
|
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.ICPPASTQualifiedName;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateDeclaration;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateDeclaration;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateParameter;
|
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.CPPASTNamedTypeSpecifier;
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTQualifiedName;
|
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.CPPASTSimpleTypeTemplateParameter;
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTTemplateId;
|
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTTemplateId;
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTTypeId;
|
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTTypeId;
|
||||||
import org.eclipse.core.resources.IFile;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper class to find Namespace informations.
|
* Helper class to find Namespace informations.
|
||||||
|
@ -39,12 +42,13 @@ public class NamespaceHelper {
|
||||||
* @param insertFile
|
* @param insertFile
|
||||||
* @param offset
|
* @param offset
|
||||||
* @return ICPPASTQualifiedName with the names of all namespaces
|
* @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();
|
final CPPASTQualifiedName qualifiedName = new CPPASTQualifiedName();
|
||||||
|
|
||||||
TranslationUnitHelper.loadTranslationUnit(insertFile).accept(new CPPASTAllVisitor() {
|
TranslationUnitHelper.loadTranslationUnit(insertFile, false).accept(new CPPASTAllVisitor() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int visit(IASTDeclSpecifier declSpec) {
|
public int visit(IASTDeclSpecifier declSpec) {
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package org.eclipse.cdt.internal.ui.refactoring.utils;
|
package org.eclipse.cdt.internal.ui.refactoring.utils;
|
||||||
|
|
||||||
|
import org.eclipse.core.runtime.CoreException;
|
||||||
import org.eclipse.core.runtime.Path;
|
import org.eclipse.core.runtime.Path;
|
||||||
|
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier;
|
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.ICPPASTFunctionDeclarator;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
|
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.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.CPPASTNamespaceDefinition;
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTTranslationUnit;
|
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTTranslationUnit;
|
||||||
|
@ -96,25 +100,36 @@ public class NodeHelper {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static MethodContext findMethodContext(IASTNode node) {
|
public static MethodContext findMethodContext(IASTNode node, IIndex index) throws CoreException{
|
||||||
IASTTranslationUnit translationUnit = node.getTranslationUnit();
|
IASTTranslationUnit translationUnit = node.getTranslationUnit();
|
||||||
boolean found = false;
|
boolean found = false;
|
||||||
MethodContext context = new MethodContext();
|
MethodContext context = new MethodContext();
|
||||||
context.setType(MethodContext.ContextType.NONE);
|
context.setType(MethodContext.ContextType.NONE);
|
||||||
IASTName name = null;
|
IASTName name = null;
|
||||||
while(node != null && !found){
|
while(node != null && !found){
|
||||||
node = node.getParent();
|
node = node.getParent();
|
||||||
if(node instanceof IASTFunctionDeclarator){
|
if(node instanceof IASTFunctionDeclarator){
|
||||||
name=((IASTFunctionDeclarator)node).getName();
|
name=((IASTFunctionDeclarator)node).getName();
|
||||||
found = true;
|
found = true;
|
||||||
context.setType(MethodContext.ContextType.FUNCTION);
|
context.setType(MethodContext.ContextType.FUNCTION);
|
||||||
} else if (node instanceof IASTFunctionDefinition){
|
} else if (node instanceof IASTFunctionDefinition){
|
||||||
name=CPPVisitor.findInnermostDeclarator(((IASTFunctionDefinition)node).getDeclarator()).getName();
|
name=CPPVisitor.findInnermostDeclarator(((IASTFunctionDefinition)node).getDeclarator()).getName();
|
||||||
found = true;
|
found = true;
|
||||||
context.setType(MethodContext.ContextType.FUNCTION);
|
context.setType(MethodContext.ContextType.FUNCTION);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(name instanceof ICPPASTQualifiedName){
|
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;
|
ICPPASTQualifiedName qname =( ICPPASTQualifiedName )name;
|
||||||
context.setMethodQName(qname);
|
context.setMethodQName(qname);
|
||||||
IBinding bind = qname.resolveBinding();
|
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) {
|
public static IASTCompoundStatement findCompoundStatementInAncestors(IASTNode node) {
|
||||||
|
|
|
@ -14,9 +14,11 @@ package org.eclipse.cdt.internal.ui.refactoring.utils;
|
||||||
|
|
||||||
import org.eclipse.core.resources.IFile;
|
import org.eclipse.core.resources.IFile;
|
||||||
import org.eclipse.core.resources.ResourcesPlugin;
|
import org.eclipse.core.resources.ResourcesPlugin;
|
||||||
|
import org.eclipse.core.runtime.CoreException;
|
||||||
import org.eclipse.core.runtime.IPath;
|
import org.eclipse.core.runtime.IPath;
|
||||||
import org.eclipse.core.runtime.Path;
|
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.CDOM;
|
||||||
import org.eclipse.cdt.core.dom.IASTServiceProvider.UnsupportedDialectException;
|
import org.eclipse.cdt.core.dom.IASTServiceProvider.UnsupportedDialectException;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
|
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.IASTNode;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor;
|
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;
|
import org.eclipse.cdt.internal.ui.refactoring.Container;
|
||||||
|
|
||||||
|
@ -35,34 +42,60 @@ import org.eclipse.cdt.internal.ui.refactoring.Container;
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class TranslationUnitHelper {
|
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
|
* @param filename to load the translation unit from
|
||||||
* @return the translation unit for the file or null
|
* @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) {
|
if (filename != null) {
|
||||||
IPath path = new Path(filename);
|
IPath path = new Path(filename);
|
||||||
IFile tmpFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
|
IFile tmpFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
|
||||||
return loadTranslationUnit(tmpFile);
|
return loadTranslationUnit(tmpFile, useIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
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
|
* @return the translation unit for the file or null
|
||||||
|
* @throws CoreException
|
||||||
*/
|
*/
|
||||||
public static IASTTranslationUnit loadTranslationUnit(IFile tmpFile) {
|
public static IASTTranslationUnit loadTranslationUnit(IFile file, boolean useIndex) throws CoreException {
|
||||||
if (tmpFile != null) {
|
if (file == null) {
|
||||||
try {
|
return null;
|
||||||
IASTTranslationUnit fileUnit = CDOM.getInstance().getTranslationUnit(tmpFile, CDOM.getInstance().getCodeReaderFactory(CDOM.PARSE_SAVED_RESOURCES), true);
|
}
|
||||||
return fileUnit;
|
if(useIndex) {
|
||||||
} catch (UnsupportedDialectException e) {
|
return loadIndexBasedTranslationUnit(file);
|
||||||
return null;
|
}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;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -111,4 +144,18 @@ public class TranslationUnitHelper {
|
||||||
}
|
}
|
||||||
return firstNode;
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue