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

Reactor classes in the same file with the same name, up to case differences, are prohibited #1741

Merged
merged 11 commits into from
May 18, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void testFederation() throws Exception {
generator.doGenerate(resource, fileAccess, context);

String lfSrc = Files.readAllLines(
((FedFileConfig)context.getFileConfig()).getSrcPath().resolve("a.lf")
((FedFileConfig)context.getFileConfig()).getSrcPath().resolve("federate__a.lf")
).stream().reduce("\n", String::concat);
Model federate = parser.parse(lfSrc);
assertHasTargetProperty(federate, "tracing");
Expand Down
44 changes: 34 additions & 10 deletions org.lflang/src/org/lflang/ModelInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
***************/

Expand All @@ -28,6 +28,8 @@
import static org.eclipse.xtext.xbase.lib.IterableExtensions.filter;
import static org.eclipse.xtext.xbase.lib.IteratorExtensions.toIterable;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.LinkedList;
Expand All @@ -46,6 +48,7 @@
import org.lflang.lf.ParameterReference;
import org.lflang.lf.Reactor;
import org.lflang.lf.STP;
import org.lflang.util.FileUtil;


/**
Expand Down Expand Up @@ -81,7 +84,7 @@ public class ModelInfo {
* interval.
*/
public Set<Deadline> overflowingDeadlines;

/**
* The set of STP offsets that use a too-large constant to specify their time
* interval.
Expand Down Expand Up @@ -137,6 +140,27 @@ public void update(Model model, ErrorReporter reporter) {
if (target == Target.C) {
this.collectOverflowingNodes();
}

checkCaseInsensitiveNameCollisions(model, reporter);
}

public void checkCaseInsensitiveNameCollisions(Model model, ErrorReporter reporter) {
var reactorNames = new HashSet<>();
var bad = new ArrayList<>();
for (var reactor : model.getReactors()) {
var lowerName = getName(reactor).toLowerCase();
if (reactorNames.contains(lowerName)) bad.add(lowerName);
reactorNames.add(lowerName);
}
for (var badName : bad) {
model.getReactors().stream()
.filter(it -> getName(it).toLowerCase().equals(badName))
.forEach(it -> reporter.reportError(it, "Multiple reactors have the same name up to case differences."));
}
}

private String getName(Reactor r) {
return r.getName() != null ? r.getName() : FileUtil.nameWithoutExtension(Path.of(model.eResource().getURI().toFileString()));
}

public Set<NamedInstance<?>> topologyCycles() {
Expand Down Expand Up @@ -223,7 +247,7 @@ private boolean detectOverflow(Set<Instantiation> visited, Parameter current) {
// Check for overflow in the referenced parameter.
overflow = detectOverflow(visited, ((ParameterReference)expr).getParameter()) || overflow;
} else {
// The right-hand side of the assignment is a
// The right-hand side of the assignment is a
// constant; check whether it is too large.
if (isTooLarge(ASTUtils.getLiteralTimeValue(expr))) {
this.overflowingAssignments.add(assignment);
Expand Down
8 changes: 5 additions & 3 deletions org.lflang/src/org/lflang/federated/generator/FedEmitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ Map<Path, CodeMap> generateFederate(
+ " in directory "
+ fileConfig.getSrcPath());

Path lfFilePath = fileConfig.getSrcPath().resolve(
fedName + ".lf");

String federateCode = String.join(
"\n",
new FedTargetEmitter().generateTarget(context, numOfFederates, federate, fileConfig, errorReporter, rtiConfig),
Expand All @@ -67,11 +64,16 @@ Map<Path, CodeMap> generateFederate(
)
);
Map<Path, CodeMap> codeMapMap = new HashMap<>();
var lfFilePath = lfFilePath(fileConfig, federate);
try (var srcWriter = Files.newBufferedWriter(lfFilePath)) {
var codeMap = CodeMap.fromGeneratedCode(federateCode);
codeMapMap.put(lfFilePath, codeMap);
srcWriter.write(codeMap.getGeneratedCode());
}
return codeMapMap;
}

public static Path lfFilePath(FedFileConfig fileConfig, FederateInstance federate) {
return fileConfig.getSrcPath().resolve(federate.name + ".lf");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ private Map<Path, CodeMap> compileFederates(
final int id = i;
compileThreadPool.execute(() -> {
Resource res = rs.getResource(URI.createFileURI(
fileConfig.getSrcPath().resolve(fed.name + ".lf").toAbsolutePath().toString()
FedEmitter.lfFilePath(fileConfig, fed).toAbsolutePath().toString()
), true);
FileConfig subFileConfig = LFGenerator.createFileConfig(res, fileConfig.getSrcGenPath(), true);
ErrorReporter subContextErrorReporter = new LineAdjustingErrorReporter(threadSafeErrorReporter, lf2lfCodeMapMap);
Expand Down
Loading