1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-08 18:26:01 +02:00

Fixed few issues causing unnecessary includes for template arguments and

target types of typedefs.
This commit is contained in:
Sergey Prigogin 2013-07-26 21:03:23 -07:00
parent 0fda1c9c85
commit 0efa436797
3 changed files with 58 additions and 47 deletions

View file

@ -128,6 +128,22 @@ public class BindingClassifierTest extends OneSourceMultipleHeadersTestCase {
// td1 a = *f(); // td1 a = *f();
public void testTypedef_2() throws Exception { public void testTypedef_2() throws Exception {
assertDefined("f", "td1"); assertDefined("f", "td1");
assertDeclared();
}
// template<typename T> struct allocator {};
// template<typename T, typename U = allocator<T>> class basic_string {};
// typedef basic_string<char> string;
// template<typename T, typename A>
// basic_string<T, A> f(const T* a, const basic_string<T, A>& b);
// void test() {
// string a;
// f("*", a);
// }
public void testTypedef_3() throws Exception {
assertDefined("f", "string"); // "basic_string" and "allocator" should not be defined.
assertDeclared();
} }
// class A { int x; }; // class A { int x; };
@ -137,6 +153,7 @@ public class BindingClassifierTest extends OneSourceMultipleHeadersTestCase {
// int a = f()->x; // int a = f()->x;
public void testClassMember() throws Exception { public void testClassMember() throws Exception {
assertDefined("f", "A"); assertDefined("f", "A");
assertDeclared();
} }
// class A { void m(); }; // class A { void m(); };
@ -146,6 +163,7 @@ public class BindingClassifierTest extends OneSourceMultipleHeadersTestCase {
// } // }
public void testMethodCall() throws Exception { public void testMethodCall() throws Exception {
assertDefined("A"); assertDefined("A");
assertDeclared();
} }
// struct A {}; // struct A {};
@ -167,17 +185,42 @@ public class BindingClassifierTest extends OneSourceMultipleHeadersTestCase {
// } // }
public void testVariableReference() throws Exception { public void testVariableReference() throws Exception {
assertDefined("a"); // Forward declaration of variables is not allowed by default. assertDefined("a"); // Forward declaration of variables is not allowed by default.
assertDeclared();
} }
// struct A { // struct A {
// void operator()(int p); // void operator()(int p);
// }; // };
// const A a; // const A& a;
// void test() { // void test() {
// a(1); // a(1);
// } // }
public void testCallOperator() throws Exception { public void testCallOperator() throws Exception {
assertDefined("A", "a"); // Forward declaration of variables is not allowed by default. assertDefined("A", "a");
assertDeclared();
}
// struct A {};
// template<typename T> struct B {};
// template<typename T, typename U = B<T>> struct C {};
// struct D : public C<A> {};
public void testTemplate_1() throws Exception {
assertDefined("C");
assertDeclared("A");
}
// struct A {};
// template<typename T> struct B {};
// template<typename T, typename U = B<T>> struct C {};
// struct D : public C<A> {};
// void test() {
// D d;
// }
public void testTemplate_2() throws Exception {
assertDefined("D");
assertDeclared();
} }
} }

View file

@ -165,7 +165,8 @@ public class IncludeOrganizerTest extends IncludesTestBase {
//#pragma once //#pragma once
// //
//#include "B.h" //#include "B.h"
//#include "C.h" //
//class C;
// //
//namespace ns { //namespace ns {
//// Comment line 1 //// Comment line 1

View file

@ -84,13 +84,12 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespace; import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespace;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPReferenceType; import org.eclipse.cdt.core.dom.ast.cpp.ICPPReferenceType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPSpecialization; import org.eclipse.cdt.core.dom.ast.cpp.ICPPSpecialization;
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.ICPPTemplateParameter;
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.internal.core.dom.parser.cpp.semantics.SemanticUtil;
/** /**
* For a whole translation unit or a part of it determines a set of externally defined bindings that * For a whole translation unit or a part of it determines a set of externally defined bindings that
* must be defined and a set of bindings that must be declared. * must be defined and a set of bindings that must be declared.
@ -395,12 +394,17 @@ public class BindingClassifier {
if (!fProcessedDefinedBindings.add(binding)) if (!fProcessedDefinedBindings.add(binding))
return; return;
if (fAst.getDefinitionsInAST(binding).length != 0) { if (binding instanceof ITypedef) {
return; // Defined locally IType type = ((ITypedef) binding).getType();
type = SemanticUtil.getNestedType(type, ALLCVQ);
if (type instanceof IBinding) {
// Record the fact that we also have a definition of the typedef's target type.
fProcessedDefinedBindings.add((IBinding) type);
}
} }
if (binding instanceof ICPPTemplateInstance) { if (fAst.getDefinitionsInAST(binding).length != 0) {
defineTemplateArguments((ICPPTemplateInstance) binding); return; // Defined locally
} }
List<IBinding> requiredBindings = getRequiredBindings(binding); List<IBinding> requiredBindings = getRequiredBindings(binding);
@ -410,43 +414,6 @@ public class BindingClassifier {
} }
} }
/**
* Defines non-pointer template arguments.
*/
protected void defineTemplateArguments(ICPPTemplateInstance instance) {
ICPPTemplateDefinition templateDefinition = instance.getTemplateDefinition();
ICPPTemplateParameter[] templateParameters = templateDefinition.getTemplateParameters();
ICPPTemplateArgument[] templateArguments = instance.getTemplateArguments();
for (int i = 0; i < templateArguments.length; i++) {
ICPPTemplateArgument argument = templateArguments[i];
ICPPTemplateParameter parameter = templateParameters[i];
ICPPTemplateArgument parameterDefault = parameter.getDefaultValue();
if (parameterDefault != null) {
// Skip the template arguments if it is the same as parameter default.
if (argument.isSameValue(parameterDefault))
continue;
if (argument.isTypeValue() && parameterDefault.isTypeValue()) {
IType argType = argument.getTypeValue();
IType defType = parameterDefault.getTypeValue();
if (argType instanceof ICPPTemplateInstance && defType instanceof ICPPTemplateInstance) {
IType argTemplate = (IType) ((ICPPTemplateInstance) argType).getTemplateDefinition();
IType defTemplate = (IType) ((ICPPTemplateInstance) defType).getTemplateDefinition();
if (argTemplate.isSameType(defTemplate)) {
defineTemplateArguments((ICPPTemplateInstance) argType);
continue;
}
}
}
}
IType type = argument.getTypeValue();
if (!(type instanceof IPointerType) && !(type instanceof ICPPReferenceType)) {
IBinding binding = getTypeBinding(type);
if (binding != null)
defineBinding(binding);
}
}
}
private void declareFunction(IFunction function, IASTFunctionCallExpression functionCallExpression) { private void declareFunction(IFunction function, IASTFunctionCallExpression functionCallExpression) {
// Handle return or expression type of the function or constructor call. // Handle return or expression type of the function or constructor call.
IType returnType = function.getType().getReturnType(); IType returnType = function.getType().getReturnType();