mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-29 03:45:35 +02:00
Bug 457374 - A 'using' declaration confuses Organize Includes
This commit is contained in:
parent
16886a1f2a
commit
3fa103dbf8
2 changed files with 39 additions and 10 deletions
|
@ -361,6 +361,16 @@ public class BindingClassifierTest extends OneSourceMultipleHeadersTestCase {
|
||||||
assertDeclared();
|
assertDeclared();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// namespace ns {
|
||||||
|
// class A {};
|
||||||
|
// }
|
||||||
|
|
||||||
|
// using ns::A;
|
||||||
|
public void testUsingDeclaration() throws Exception {
|
||||||
|
assertDefined();
|
||||||
|
assertDeclared("A");
|
||||||
|
}
|
||||||
|
|
||||||
// struct A {
|
// struct A {
|
||||||
// A(const char* s);
|
// A(const char* s);
|
||||||
// };
|
// };
|
||||||
|
|
|
@ -95,6 +95,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateId;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateId;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTypeId;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTypeId;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTUnaryExpression;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTUnaryExpression;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTUsingDeclaration;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassScope;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassScope;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPConstructor;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPConstructor;
|
||||||
|
@ -111,6 +112,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateArgument;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateDefinition;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateDefinition;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateInstance;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateInstance;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateParameterMap;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateParameterMap;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPUsingDeclaration;
|
||||||
import org.eclipse.cdt.core.index.IIndexMacro;
|
import org.eclipse.cdt.core.index.IIndexMacro;
|
||||||
import org.eclipse.cdt.core.index.IndexFilter;
|
import org.eclipse.cdt.core.index.IndexFilter;
|
||||||
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
|
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
|
||||||
|
@ -1041,6 +1043,11 @@ public class BindingClassifier {
|
||||||
return Collections.singleton(binding);
|
return Collections.singleton(binding);
|
||||||
if (binding instanceof ICPPNamespace)
|
if (binding instanceof ICPPNamespace)
|
||||||
return Collections.emptySet();
|
return Collections.emptySet();
|
||||||
|
if (binding instanceof ICPPUsingDeclaration) {
|
||||||
|
Set<IBinding> result = new HashSet<>();
|
||||||
|
Collections.addAll(result, ((ICPPUsingDeclaration) binding).getDelegates());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
Deque<IBinding> queue = new ArrayDeque<>();
|
Deque<IBinding> queue = new ArrayDeque<>();
|
||||||
|
|
||||||
|
@ -1126,15 +1133,14 @@ public class BindingClassifier {
|
||||||
* @param binding The binding to add.
|
* @param binding The binding to add.
|
||||||
*/
|
*/
|
||||||
private void declareBinding(IBinding binding) {
|
private void declareBinding(IBinding binding) {
|
||||||
|
if (binding instanceof ICPPNamespace && !(binding instanceof ICPPNamespaceAlias))
|
||||||
|
return;
|
||||||
if (fProcessedDefinedBindings.contains(binding))
|
if (fProcessedDefinedBindings.contains(binding))
|
||||||
return;
|
return;
|
||||||
|
if (isDeclaredLocally(binding))
|
||||||
if (fAst.getDeclarationsInAST(binding).length != 0)
|
|
||||||
return; // Declared locally.
|
return; // Declared locally.
|
||||||
|
|
||||||
if (!fProcessedDeclaredBindings.add(binding))
|
if (!fProcessedDeclaredBindings.add(binding))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!canForwardDeclare(binding)) {
|
if (!canForwardDeclare(binding)) {
|
||||||
defineBinding(binding);
|
defineBinding(binding);
|
||||||
return;
|
return;
|
||||||
|
@ -1147,11 +1153,8 @@ public class BindingClassifier {
|
||||||
fBindingsToDefine.contains(requiredBinding)) {
|
fBindingsToDefine.contains(requiredBinding)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (fAst.getDefinitionsInAST(requiredBinding).length != 0) {
|
if (isDeclaredLocally(requiredBinding) || isDefinedLocally(requiredBinding)) {
|
||||||
return; // Defined locally
|
return; // Declared or defined locally.
|
||||||
}
|
|
||||||
if (fAst.getDeclarationsInAST(requiredBinding).length != 0) {
|
|
||||||
return; // Defined locally
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (canForwardDeclare(requiredBinding)) {
|
if (canForwardDeclare(requiredBinding)) {
|
||||||
|
@ -1175,7 +1178,9 @@ public class BindingClassifier {
|
||||||
*/
|
*/
|
||||||
private boolean canForwardDeclare(IBinding binding) {
|
private boolean canForwardDeclare(IBinding binding) {
|
||||||
boolean canDeclare = false;
|
boolean canDeclare = false;
|
||||||
if (binding instanceof ICompositeType) {
|
if (binding instanceof ICPPUsingDeclaration) {
|
||||||
|
return true; // Return true to consider delegates later on.
|
||||||
|
} if (binding instanceof ICompositeType) {
|
||||||
canDeclare = fPreferences.forwardDeclareCompositeTypes;
|
canDeclare = fPreferences.forwardDeclareCompositeTypes;
|
||||||
} else if (binding instanceof IEnumeration) {
|
} else if (binding instanceof IEnumeration) {
|
||||||
canDeclare = fPreferences.forwardDeclareEnums && isEnumerationWithoutFixedUnderlyingType(binding);
|
canDeclare = fPreferences.forwardDeclareEnums && isEnumerationWithoutFixedUnderlyingType(binding);
|
||||||
|
@ -1231,6 +1236,8 @@ public class BindingClassifier {
|
||||||
* @param binding The binding to add.
|
* @param binding The binding to add.
|
||||||
*/
|
*/
|
||||||
private void defineBinding(IBinding binding) {
|
private void defineBinding(IBinding binding) {
|
||||||
|
if (binding instanceof ICPPNamespace && !(binding instanceof ICPPNamespaceAlias))
|
||||||
|
return;
|
||||||
if (!markAsDefined(binding))
|
if (!markAsDefined(binding))
|
||||||
return;
|
return;
|
||||||
if (isDefinedLocally(binding))
|
if (isDefinedLocally(binding))
|
||||||
|
@ -1253,6 +1260,18 @@ public class BindingClassifier {
|
||||||
return binding instanceof CPPClosureType || fAst.getDefinitionsInAST(binding).length != 0;
|
return binding instanceof CPPClosureType || fAst.getDefinitionsInAST(binding).length != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isDeclaredLocally(IBinding binding) {
|
||||||
|
IASTName[] declarations = fAst.getDeclarationsInAST(binding);
|
||||||
|
for (IASTName name : declarations) {
|
||||||
|
IASTNode node = name;
|
||||||
|
if (node.getPropertyInParent() == ICPPASTQualifiedName.SEGMENT_NAME)
|
||||||
|
node = node.getParent();
|
||||||
|
if (node.getPropertyInParent() != ICPPASTUsingDeclaration.NAME)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
private void defineBindingForName(IASTName name) {
|
private void defineBindingForName(IASTName name) {
|
||||||
IBinding binding = name.resolveBinding();
|
IBinding binding = name.resolveBinding();
|
||||||
if (!isPartOfExternalMacroDefinition(name))
|
if (!isPartOfExternalMacroDefinition(name))
|
||||||
|
|
Loading…
Add table
Reference in a new issue