Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DE575629 - Most program has errors in SQL-ERROR-RTN paragraph #2210

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public ElementaryItemNode(
protected String getVariableDisplayString() {
StringBuilder stringBuilder = new StringBuilder(getFormattedSuffix());
if (picClause != null) stringBuilder.append(" PIC ").append(picClause);
if (usageFormat != UsageFormat.UNDEFINED)
if (usageFormat != null && usageFormat != UsageFormat.UNDEFINED)
stringBuilder.append(" USAGE ").append(usageFormat.toDisplayString());
if (StringUtils.isNoneBlank(value)) stringBuilder.append(" VALUE ").append(value);
return stringBuilder.append(".").toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ protected String getVariableDisplayString() {
StringBuilder stringBuilder = new StringBuilder(getFormattedSuffix());
stringBuilder.append(String.format(" OCCURS %1$d TIMES", occursTimes));
if (picClause != null) stringBuilder.append(" PIC ").append(picClause);
if (usageFormat != UsageFormat.UNDEFINED)
if (usageFormat != null && usageFormat != UsageFormat.UNDEFINED)
stringBuilder.append(" USAGE ").append(usageFormat.toDisplayString());
if (StringUtils.isNoneBlank(value)) stringBuilder.append(" VALUE ").append(value);
return stringBuilder.append(".").toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class VariableUsageUtils {
public static List<VariableNode> findVariablesForUsage(
Multimap<String, VariableNode> definedVariables, List<VariableUsageNode> usageNodes) {
Map<VariableNode, Integer> variableToStepCountsToMatchParentsMap =
definedVariables.get(usageNodes.get(0).getName()).stream()
findDefinedVariable(usageNodes.get(0).getName(), definedVariables).stream()
.map(
it ->
mapVariableToStepCountsToMatchParents(
Expand All @@ -68,6 +68,23 @@ public static List<VariableNode> findVariablesForUsage(
: exactHierarchyMatchedVariables;
}

private Collection<VariableNode> findDefinedVariable(String name, Multimap<String, VariableNode> definedVariables) {
Collection<VariableNode> foundVariable = definedVariables.get(name);
if (foundVariable.size() > 0) {
return foundVariable;
}
Optional<VariableNode> node = definedVariables.values()
.stream().flatMap(Node::getDepthFirstStream)
.filter(VariableNode.class::isInstance)
.map(VariableNode.class::cast)
.filter(var -> var.getName().equals(name))
.findFirst();
if (node.isPresent()) {
foundVariable.add(node.get());
}
return foundVariable;
}

private static Map<VariableNode, Integer> mapVariableToStepCountsToMatchParents(
VariableNode variable, List<VariableUsageNode> parents) {
VariableNode referredVariable = variable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ PIC X(08).
addElement(variable, 5, "SQLCA-DB-VRS", "X(2)");
addElement(variable, 5, "SQLCA-DB-RLS", "X(2)");
addElement(variable, 5, "SQLCA-LUWID", "X(8)");
addElement(variable, 5, "SQLCODE", "S9(9)", UsageFormat.COMP_5);
variable.addChild(new ElementaryItemNode(LOCALITY, 5, "SQLCA-SQLCODE", false, "S9(9)",
null, UsageFormat.COMP, false, false, false));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2024 Broadcom.
* The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Broadcom, Inc. - initial API and implementation
*
*/

package org.eclipse.lsp.cobol.core.model;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Arrays;
import java.util.List;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import org.eclipse.lsp.cobol.common.model.Locality;
import org.eclipse.lsp.cobol.common.model.tree.variable.*;
import org.eclipse.lsp.cobol.common.utils.ImplicitCodeUtils;
import org.eclipse.lsp.cobol.implicitDialects.sql.generator.Db2ImplicitVariablesGenerator;
import org.junit.jupiter.api.Test;

/** Check if the variable used is part of the defined variables */
public class VariableUsageUtilsTest {
List<VariableNode> datacomNodes = Db2ImplicitVariablesGenerator.generateDatacomNodes();
Locality locality = Locality.builder()
.uri(ImplicitCodeUtils.createFullUrl("implicit-code-SQLCA_DB2"))
.build();
@Test
void testFindVariablesForUsage() {
VariableUsageNode usageNode1 = new VariableUsageNode("SQLCA-ERR-MSG", locality);
List<VariableUsageNode> usageNodes = Arrays.asList(usageNode1);
VariableNode variableNode1 = new ElementaryItemNode(locality, 10, "SQLCA-ERR-MSG", false, "X(80)",
null, UsageFormat.UNDEFINED, false, false, false);
Multimap<String, VariableNode> definedVariables = ArrayListMultimap.create();
definedVariables.put("SQLCA", datacomNodes.get(0));
List<VariableNode> result = VariableUsageUtils.findVariablesForUsage(definedVariables, usageNodes);

assertEquals(1, result.size());
assertTrue(result.contains(variableNode1));
}

@Test
void testFindVariablesForUsageNoMatch() {
VariableUsageNode usageNode1 = new VariableUsageNode("SQLCA-NO-MATCH", locality);
List<VariableUsageNode> usageNodes = Arrays.asList(usageNode1);
Multimap<String, VariableNode> definedVariables = ArrayListMultimap.create();
definedVariables.put("SQLCA", datacomNodes.get(0));
List<VariableNode> result = VariableUsageUtils.findVariablesForUsage(definedVariables, usageNodes);

assertTrue(result.isEmpty());
}
}
Loading