From 57a32fc5217bc4eda3fd8a7f637184d063030bde Mon Sep 17 00:00:00 2001 From: "Igor V. Kovalenko" Date: Wed, 22 Feb 2023 08:27:25 +0300 Subject: [PATCH] Scan for more template-id alternatives in expression, bug 497931 If parsing expression part for an alternative terminates with BacktrackException, selectFallback() would short-circuit to the longest remaining variant. If that happens to successfully complete parsing till the end of expression token sequence, all of remaining variants are discarded, including the first found alternative which was to parse identifier as template name. This causes expression() to only consider one branchpoint out of all possible variants. Allow it to find more variants by scanning through all branchpoints looking for the alternative with leftmost parsed boundary. This is probably still not ideal but fixes this common std library construct: template inline constexpr bool X = true; template inline constexpr bool Y = true; template bool predicate() { return X && Y; // CDT finds this one: (X) < (T) > (&&Y) // Fix it to also consider (X) && (Y) } Bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=497931 --- .../core/dom/parser/cpp/NameOrTemplateIDVariants.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/NameOrTemplateIDVariants.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/NameOrTemplateIDVariants.java index 28ac86455ce..275cec3c2e4 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/NameOrTemplateIDVariants.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/NameOrTemplateIDVariants.java @@ -150,8 +150,8 @@ public class NameOrTemplateIDVariants { public Variant selectFallback() { // Search for an open variant, with a small right offset and a large left offset + Variant best = null; for (BranchPoint p = fFirst; p != null; p = p.getNext()) { - Variant best = null; for (Variant v = p.getFirstVariant(); v != null; v = v.getNext()) { if (v.getTargetOperator() == null) { if (best == null || v.fRightOffset < best.fRightOffset) { @@ -159,10 +159,10 @@ public class NameOrTemplateIDVariants { } } } - if (best != null) { - remove(best); - return best; - } + } + if (best != null) { + remove(best); + return best; } return null; }