Skip to content

Commit d5ec9d7

Browse files
authored
Improve ProblemBinding checker message for failed function instantiation (#668)
Display the new more accurate error when all candidates were failed instantiations. Otherwise, if there is a mix of failed instantiation and wrong number of arguments, display the old message. This could really be improved even more... template<typename T> void function() {} Before: function(); // Invalid arguments 'Candidates are: After: function(); // Cannot instantiate template function 'Candidates are:
1 parent e838a23 commit d5ec9d7

File tree

8 files changed

+38
-5
lines changed

8 files changed

+38
-5
lines changed

codan/org.eclipse.cdt.codan.checkers/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: %Bundle-Name
44
Bundle-SymbolicName: org.eclipse.cdt.codan.checkers;singleton:=true
5-
Bundle-Version: 3.5.400.qualifier
5+
Bundle-Version: 3.5.500.qualifier
66
Bundle-Activator: org.eclipse.cdt.codan.checkers.CodanCheckersActivator
77
Require-Bundle: org.eclipse.core.runtime,
88
org.eclipse.core.resources,

codan/org.eclipse.cdt.codan.checkers/OSGI-INF/l10n/bundle.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ problem.name.12 = Field cannot be resolved
116116
problem.description.13 = Name resolution problem found by the indexer
117117
problem.messagePattern.13 = Structured binding initializer expression refers to introduced name ''{0}''
118118
problem.name.13 = Invalid structured binding declaration
119+
problem.description.14 = Name resolution problem found by the indexer
120+
problem.messagePattern.14 = Cannot instantiate template function ''{0}''
121+
problem.name.14 = Function cannot be instantiated
119122
checker.name.AbstractClassCreation = Abstract class cannot be instantiated
120123
problem.name.AbstractClassCreation = Abstract class cannot be instantiated
121124
problem.messagePattern.AbstractClassCreation = The type ''{0}'' must implement the inherited pure virtual method ''{1}''\u0020

codan/org.eclipse.cdt.codan.checkers/plugin.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,16 @@
253253
messagePattern="%problem.messagePattern.10"
254254
name="%problem.name.10">
255255
</problem>
256+
<problem
257+
category="org.eclipse.cdt.codan.core.categories.CompilerErrors"
258+
defaultEnabled="true"
259+
defaultSeverity="Error"
260+
description="%problem.description.14"
261+
id="org.eclipse.cdt.codan.internal.checkers.TemplateInstantiationProblem"
262+
markerType="org.eclipse.cdt.codan.core.codanSemanticProblem"
263+
messagePattern="%problem.messagePattern.14"
264+
name="%problem.name.14">
265+
</problem>
256266
<problem
257267
category="org.eclipse.cdt.codan.core.categories.CompilerErrors"
258268
defaultEnabled="true"

codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/ProblemBindingChecker.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public class ProblemBindingChecker extends AbstractIndexAstChecker {
6666
public static String ERR_ID_VariableResolutionProblem = "org.eclipse.cdt.codan.internal.checkers.VariableResolutionProblem"; //$NON-NLS-1$
6767
public static String ERR_ID_Candidates = "org.eclipse.cdt.codan.internal.checkers.Candidates"; //$NON-NLS-1$
6868
public static String ERR_ID_StructuredBindingDeclarationProblem = "org.eclipse.cdt.codan.internal.checkers.StructuredBindingDeclarationProblem"; //$NON-NLS-1$
69+
public static String ERR_ID_TemplateInstantiationProblem = "org.eclipse.cdt.codan.internal.checkers.TemplateInstantiationProblem"; //$NON-NLS-1$
6970

7071
@Override
7172
public boolean runInEditor() {
@@ -175,6 +176,14 @@ public int visit(IASTName name) {
175176
contextFlagsString);
176177
return PROCESS_CONTINUE;
177178
}
179+
if (id == IProblemBinding.SEMANTIC_INVALID_TEMPLATE_INSTANTIATION) {
180+
if (isFunctionCall(name, parentNode)) {
181+
reportProblem(ERR_ID_TemplateInstantiationProblem, name.getLastName(),
182+
getCandidatesString(problemBinding), contextFlagsString);
183+
}
184+
return PROCESS_CONTINUE;
185+
}
186+
178187
// From this point, we'll deal only with NAME_NOT_FOUND problems.
179188
// If it's something else continue because we don't want to give bad messages.
180189
if (id != IProblemBinding.SEMANTIC_NAME_NOT_FOUND) {

core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2TemplateTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ public void test_dr1591_DeduceArrayFromInitializerList_c() throws Exception {
480480
assertInstance(fCall, IProblemBinding.class);
481481

482482
IProblemBinding fCallPB = (IProblemBinding) fCall;
483-
assertEquals(IProblemBinding.SEMANTIC_NAME_NOT_FOUND, fCallPB.getID());
483+
assertEquals(IProblemBinding.BINDING_INVALID_TEMPLATE_INSTANTIATION, fCallPB.getID());
484484
}
485485

486486
// template < class T, template < class X > class U, T *pT > class A {
@@ -7487,7 +7487,7 @@ public void testTemplatedAliasRedefinitionOfSameFunction() throws Exception {
74877487
public void testTemplatedAliasDeduction() throws Exception {
74887488
BindingAssertionHelper bh = getAssertionHelper();
74897489
bh.assertNonProblem("g(v)", "g", ICPPFunction.class);
7490-
bh.assertProblem("f(v)", "f", ISemanticProblem.BINDING_NOT_FOUND);
7490+
bh.assertProblem("f(v)", "f", ISemanticProblem.BINDING_INVALID_TEMPLATE_INSTANTIATION);
74917491
}
74927492

74937493
// using function = void (&)(int);

core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IProblemBinding.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public interface IProblemBinding extends IBinding, IScope, IType, ISemanticProbl
6767
public static final int SEMANTIC_INVALID_TEMPLATE_ARGUMENTS = BINDING_INVALID_TEMPLATE_ARGUMENTS;
6868
/** @since 8.1 */
6969
public static final int SEMANTIC_INVALID_STRUCTURED_BINDING_INITIALIZER = BINDING_INVALID_STRUCTURED_BINDING_INITIALIZER;
70+
/** @since 8.4 */
71+
public static final int SEMANTIC_INVALID_TEMPLATE_INSTANTIATION = BINDING_INVALID_TEMPLATE_INSTANTIATION;
7072

7173
/**
7274
* @deprecated There may be additional problems.

core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ISemanticProblem.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public interface ISemanticProblem {
3939
int BINDING_NO_CLASS = 16;
4040
/** @since 8.1 */
4141
int BINDING_INVALID_STRUCTURED_BINDING_INITIALIZER = 17;
42+
/** @since 8.4 */
43+
int BINDING_INVALID_TEMPLATE_INSTANTIATION = 18;
4244

4345
int TYPE_NO_NAME = 10000;
4446
int TYPE_UNRESOLVED_NAME = 10001;

core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPSemantics.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3103,10 +3103,17 @@ public static IBinding resolveFunction(LookupData data, ICPPFunction[] fns, bool
31033103
ICPPFunction[] tmp = selectByArgumentCount(data, fns);
31043104
if (tmp.length == 0 || tmp[0] == null)
31053105
return new ProblemBinding(lookupName, lookupPoint, IProblemBinding.SEMANTIC_NAME_NOT_FOUND, fns);
3106+
int nbBeforeInstantiate = tmp.length;
31063107
tmp = CPPTemplates.instantiateForFunctionCall(tmp, data.getTemplateArguments(), Arrays.asList(argTypes),
31073108
Arrays.asList(data.getFunctionArgumentValueCategories()), data.argsContainImpliedObject);
3108-
if (tmp.length == 0 || tmp[0] == null)
3109-
return new ProblemBinding(lookupName, lookupPoint, IProblemBinding.SEMANTIC_NAME_NOT_FOUND, fns);
3109+
if (tmp.length == 0 || tmp[0] == null) {
3110+
// All candidates were failed template instantiations
3111+
if (nbBeforeInstantiate == fns.length)
3112+
return new ProblemBinding(lookupName, lookupPoint,
3113+
IProblemBinding.SEMANTIC_INVALID_TEMPLATE_INSTANTIATION, fns);
3114+
else
3115+
return new ProblemBinding(lookupName, lookupPoint, IProblemBinding.SEMANTIC_NAME_NOT_FOUND, fns);
3116+
}
31103117

31113118
int viableCount = 0;
31123119
for (IFunction f : tmp) {

0 commit comments

Comments
 (0)