From d5ec9d7c683913c06a4e65fcebfe57edfa3b1354 Mon Sep 17 00:00:00 2001 From: Marc-Andre Laperle Date: Fri, 2 Feb 2024 11:40:47 -0500 Subject: [PATCH] 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 void function() {} Before: function(); // Invalid arguments 'Candidates are: After: function(); // Cannot instantiate template function 'Candidates are: --- .../META-INF/MANIFEST.MF | 2 +- .../OSGI-INF/l10n/bundle.properties | 3 +++ codan/org.eclipse.cdt.codan.checkers/plugin.xml | 10 ++++++++++ .../internal/checkers/ProblemBindingChecker.java | 9 +++++++++ .../cdt/core/parser/tests/ast2/AST2TemplateTests.java | 4 ++-- .../org/eclipse/cdt/core/dom/ast/IProblemBinding.java | 2 ++ .../eclipse/cdt/core/dom/ast/ISemanticProblem.java | 2 ++ .../core/dom/parser/cpp/semantics/CPPSemantics.java | 11 +++++++++-- 8 files changed, 38 insertions(+), 5 deletions(-) diff --git a/codan/org.eclipse.cdt.codan.checkers/META-INF/MANIFEST.MF b/codan/org.eclipse.cdt.codan.checkers/META-INF/MANIFEST.MF index f2ace874a42..e7817f88e0a 100644 --- a/codan/org.eclipse.cdt.codan.checkers/META-INF/MANIFEST.MF +++ b/codan/org.eclipse.cdt.codan.checkers/META-INF/MANIFEST.MF @@ -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, diff --git a/codan/org.eclipse.cdt.codan.checkers/OSGI-INF/l10n/bundle.properties b/codan/org.eclipse.cdt.codan.checkers/OSGI-INF/l10n/bundle.properties index 15bbc5c8fe0..77ebbe6e435 100644 --- a/codan/org.eclipse.cdt.codan.checkers/OSGI-INF/l10n/bundle.properties +++ b/codan/org.eclipse.cdt.codan.checkers/OSGI-INF/l10n/bundle.properties @@ -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 diff --git a/codan/org.eclipse.cdt.codan.checkers/plugin.xml b/codan/org.eclipse.cdt.codan.checkers/plugin.xml index 7a4d11c604f..9e63033f8ab 100644 --- a/codan/org.eclipse.cdt.codan.checkers/plugin.xml +++ b/codan/org.eclipse.cdt.codan.checkers/plugin.xml @@ -253,6 +253,16 @@ messagePattern="%problem.messagePattern.10" name="%problem.name.10"> + + class U, T *pT > class A { @@ -7487,7 +7487,7 @@ public class AST2TemplateTests extends AST2CPPTestBase { 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); diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IProblemBinding.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IProblemBinding.java index 7ec31006142..2cce48437f9 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IProblemBinding.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IProblemBinding.java @@ -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. diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ISemanticProblem.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ISemanticProblem.java index 5c23def3eb8..94171e75a1c 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ISemanticProblem.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ISemanticProblem.java @@ -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; diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPSemantics.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPSemantics.java index b6d65d2112d..5a136b53329 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPSemantics.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPSemantics.java @@ -3103,10 +3103,17 @@ public class CPPSemantics { 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) {