Skip to content

Commit

Permalink
Improve ProblemBinding checker message for failed function instantiat…
Browse files Browse the repository at this point in the history
…ion (#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:
  • Loading branch information
MarkZ3 committed Feb 2, 2024
1 parent e838a23 commit d5ec9d7
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion codan/org.eclipse.cdt.codan.checkers/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %Bundle-Name
Bundle-SymbolicName: org.eclipse.cdt.codan.checkers;singleton:=true
Bundle-Version: 3.5.400.qualifier
Bundle-Version: 3.5.500.qualifier
Bundle-Activator: org.eclipse.cdt.codan.checkers.CodanCheckersActivator
Require-Bundle: org.eclipse.core.runtime,
org.eclipse.core.resources,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ problem.name.12 = Field cannot be resolved
problem.description.13 = Name resolution problem found by the indexer
problem.messagePattern.13 = Structured binding initializer expression refers to introduced name ''{0}''
problem.name.13 = Invalid structured binding declaration
problem.description.14 = Name resolution problem found by the indexer
problem.messagePattern.14 = Cannot instantiate template function ''{0}''
problem.name.14 = Function cannot be instantiated
checker.name.AbstractClassCreation = Abstract class cannot be instantiated
problem.name.AbstractClassCreation = Abstract class cannot be instantiated
problem.messagePattern.AbstractClassCreation = The type ''{0}'' must implement the inherited pure virtual method ''{1}''\u0020
Expand Down
10 changes: 10 additions & 0 deletions codan/org.eclipse.cdt.codan.checkers/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,16 @@
messagePattern="%problem.messagePattern.10"
name="%problem.name.10">
</problem>
<problem
category="org.eclipse.cdt.codan.core.categories.CompilerErrors"
defaultEnabled="true"
defaultSeverity="Error"
description="%problem.description.14"
id="org.eclipse.cdt.codan.internal.checkers.TemplateInstantiationProblem"
markerType="org.eclipse.cdt.codan.core.codanSemanticProblem"
messagePattern="%problem.messagePattern.14"
name="%problem.name.14">
</problem>
<problem
category="org.eclipse.cdt.codan.core.categories.CompilerErrors"
defaultEnabled="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public class ProblemBindingChecker extends AbstractIndexAstChecker {
public static String ERR_ID_VariableResolutionProblem = "org.eclipse.cdt.codan.internal.checkers.VariableResolutionProblem"; //$NON-NLS-1$
public static String ERR_ID_Candidates = "org.eclipse.cdt.codan.internal.checkers.Candidates"; //$NON-NLS-1$
public static String ERR_ID_StructuredBindingDeclarationProblem = "org.eclipse.cdt.codan.internal.checkers.StructuredBindingDeclarationProblem"; //$NON-NLS-1$
public static String ERR_ID_TemplateInstantiationProblem = "org.eclipse.cdt.codan.internal.checkers.TemplateInstantiationProblem"; //$NON-NLS-1$

@Override
public boolean runInEditor() {
Expand Down Expand Up @@ -175,6 +176,14 @@ public int visit(IASTName name) {
contextFlagsString);
return PROCESS_CONTINUE;
}
if (id == IProblemBinding.SEMANTIC_INVALID_TEMPLATE_INSTANTIATION) {
if (isFunctionCall(name, parentNode)) {
reportProblem(ERR_ID_TemplateInstantiationProblem, name.getLastName(),
getCandidatesString(problemBinding), contextFlagsString);
}
return PROCESS_CONTINUE;
}

// From this point, we'll deal only with NAME_NOT_FOUND problems.
// If it's something else continue because we don't want to give bad messages.
if (id != IProblemBinding.SEMANTIC_NAME_NOT_FOUND) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ public void test_dr1591_DeduceArrayFromInitializerList_c() throws Exception {
assertInstance(fCall, IProblemBinding.class);

IProblemBinding fCallPB = (IProblemBinding) fCall;
assertEquals(IProblemBinding.SEMANTIC_NAME_NOT_FOUND, fCallPB.getID());
assertEquals(IProblemBinding.BINDING_INVALID_TEMPLATE_INSTANTIATION, fCallPB.getID());
}

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

// using function = void (&)(int);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public interface IProblemBinding extends IBinding, IScope, IType, ISemanticProbl
public static final int SEMANTIC_INVALID_TEMPLATE_ARGUMENTS = BINDING_INVALID_TEMPLATE_ARGUMENTS;
/** @since 8.1 */
public static final int SEMANTIC_INVALID_STRUCTURED_BINDING_INITIALIZER = BINDING_INVALID_STRUCTURED_BINDING_INITIALIZER;
/** @since 8.4 */
public static final int SEMANTIC_INVALID_TEMPLATE_INSTANTIATION = BINDING_INVALID_TEMPLATE_INSTANTIATION;

/**
* @deprecated There may be additional problems.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public interface ISemanticProblem {
int BINDING_NO_CLASS = 16;
/** @since 8.1 */
int BINDING_INVALID_STRUCTURED_BINDING_INITIALIZER = 17;
/** @since 8.4 */
int BINDING_INVALID_TEMPLATE_INSTANTIATION = 18;

int TYPE_NO_NAME = 10000;
int TYPE_UNRESOLVED_NAME = 10001;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3103,10 +3103,17 @@ public static IBinding resolveFunction(LookupData data, ICPPFunction[] fns, bool
ICPPFunction[] tmp = selectByArgumentCount(data, fns);
if (tmp.length == 0 || tmp[0] == null)
return new ProblemBinding(lookupName, lookupPoint, IProblemBinding.SEMANTIC_NAME_NOT_FOUND, fns);
int nbBeforeInstantiate = tmp.length;
tmp = CPPTemplates.instantiateForFunctionCall(tmp, data.getTemplateArguments(), Arrays.asList(argTypes),
Arrays.asList(data.getFunctionArgumentValueCategories()), data.argsContainImpliedObject);
if (tmp.length == 0 || tmp[0] == null)
return new ProblemBinding(lookupName, lookupPoint, IProblemBinding.SEMANTIC_NAME_NOT_FOUND, fns);
if (tmp.length == 0 || tmp[0] == null) {
// All candidates were failed template instantiations
if (nbBeforeInstantiate == fns.length)
return new ProblemBinding(lookupName, lookupPoint,
IProblemBinding.SEMANTIC_INVALID_TEMPLATE_INSTANTIATION, fns);
else
return new ProblemBinding(lookupName, lookupPoint, IProblemBinding.SEMANTIC_NAME_NOT_FOUND, fns);
}

int viableCount = 0;
for (IFunction f : tmp) {
Expand Down

0 comments on commit d5ec9d7

Please sign in to comment.