Skip to content

Commit

Permalink
Merge pull request #882 from lf-lang/lint-optional-in-standalone
Browse files Browse the repository at this point in the history
Make linting optional in standalone mode
  • Loading branch information
petervdonovan authored Jan 25, 2022
2 parents 8485602 + 5779119 commit 2d7e975
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 13 deletions.
4 changes: 4 additions & 0 deletions .github/scripts/test-lfc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ bin/lfc --federated --rti rti test/C/src/Minimal.lf
# -h,--help Display this information.
bin/lfc --help

# -l, --lint Enable linting during build.
bin/lfc -l test/Python/src/Minimal.lf
bin/lfc --lint test/Python/src/Minimal.lf

# -n,--no-compile Do not invoke target compiler.
bin/lfc -n test/C/src/Minimal.lf
bin/lfc --no-compile test/C/src/Minimal.lf
Expand Down
3 changes: 2 additions & 1 deletion org.lflang.lfc/src/org/lflang/lfc/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ enum CLIOption {
COMPILER(null, "target-compiler", true, false, "Target compiler to invoke.", true),
CLEAN("c", "clean", false, false, "Clean before building.", true),
HELP("h", "help", false, false, "Display this information.", true),
LINT("l", "lint", false, false, "Enable or disable linting of generated code.", true),
NO_COMPILE("n", "no-compile", false, false, "Do not invoke target compiler.", true),
FEDERATED("f", "federated", false, false, "Treat main reactor as federated.", false),
THREADS("t", "threads", true, false, "Specify the default number of threads.", true),
Expand All @@ -120,7 +121,7 @@ enum CLIOption {
public final Option option;

/**
* Whether or not to pass this option to the code generator.
* Whether to pass this option to the code generator.
*/
public final boolean passOn;

Expand Down
26 changes: 22 additions & 4 deletions org.lflang/src/org/lflang/generator/Validator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.eclipse.xtext.util.CancelIndicator;

import org.lflang.ErrorReporter;
import org.lflang.TargetConfig.Mode;
import org.lflang.util.LFCommand;
import java.nio.file.Path;
import java.util.ArrayList;
Expand Down Expand Up @@ -50,13 +51,13 @@ protected Validator(ErrorReporter errorReporter, Map<Path, CodeMap> codeMaps) {

/**
* Validate this Validator's group of generated files.
* @param cancelIndicator The cancel indicator for the
* current operation.
* @param context The context of the current build.
*/
public final void doValidate(CancelIndicator cancelIndicator) throws ExecutionException, InterruptedException {
public final void doValidate(LFGeneratorContext context) throws ExecutionException, InterruptedException {
if (!validationEnabled(context)) return;
final List<Callable<Pair<ValidationStrategy, LFCommand>>> tasks = getValidationStrategies().stream().map(
it -> (Callable<Pair<ValidationStrategy, LFCommand>>) () -> {
it.second.run(cancelIndicator, true);
it.second.run(context.getCancelIndicator(), true);
return it;
}
).collect(Collectors.toList());
Expand All @@ -66,6 +67,23 @@ public final void doValidate(CancelIndicator cancelIndicator) throws ExecutionEx
}
}

/**
* Return whether generated code validation is enabled for this build.
* @param context The context of the current build.
*/
private boolean validationEnabled(LFGeneratorContext context) {
return context.getArgs().containsKey("lint") || validationEnabledByDefault(context);
}

/**
* Return whether validation of generated code is enabled by default.
* @param context The context of the current build.
* @return Whether validation of generated code is enabled by default.
*/
protected boolean validationEnabledByDefault(LFGeneratorContext context) {
return context.getMode() != Mode.STANDALONE;
}

/**
* Invoke all the given tasks.
* @param tasks Any set of tasks.
Expand Down
2 changes: 1 addition & 1 deletion org.lflang/src/org/lflang/generator/cpp/CppGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class CppGenerator(
// We must compile in order to install the dependencies. Future validations will be faster.
doCompile(context, codeMaps)
} else if (runCmake(context).first == 0) {
CppValidator(cppFileConfig, errorReporter, codeMaps).doValidate(context.cancelIndicator)
CppValidator(cppFileConfig, errorReporter, codeMaps).doValidate(context)
context.finish(GeneratorResult.GENERATED_NO_EXECUTABLE.apply(codeMaps))
} else {
context.unsuccessfulFinish()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1294,7 +1294,7 @@ class PythonGenerator extends CGenerator {
100 * federateCount / federates.size()
)
// If there are no federates, compile and install the generated code
new PythonValidator(fileConfig, errorReporter, codeMaps, protoNames).doValidate(context.cancelIndicator)
new PythonValidator(fileConfig, errorReporter, codeMaps, protoNames).doValidate(context)
if (!errorsOccurred() && context.mode != Mode.LSP_MEDIUM) {
compilingFederatesContext.reportProgress(
String.format("Validation complete. Compiling and installing %d/%d Python modules...", federateCount, federates.size()),
Expand Down
2 changes: 1 addition & 1 deletion org.lflang/src/org/lflang/generator/rust/RustGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class RustGenerator(
)
val exec = fileConfig.binPath.toAbsolutePath().resolve(gen.executableName)
Files.deleteIfExists(exec) // cleanup, cargo doesn't do it
if (context.mode == TargetConfig.Mode.LSP_MEDIUM) RustValidator(fileConfig, errorReporter, codeMaps).doValidate(context.cancelIndicator)
if (context.mode == TargetConfig.Mode.LSP_MEDIUM) RustValidator(fileConfig, errorReporter, codeMaps).doValidate(context)
else invokeRustCompiler(context, gen.executableName, codeMaps)
}
}
Expand Down
4 changes: 2 additions & 2 deletions org.lflang/src/org/lflang/generator/ts/TSGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,10 @@ class TSGenerator(
*/
private fun passesChecks(validator: TSValidator, parsingContext: LFGeneratorContext): Boolean {
parsingContext.reportProgress("Linting generated code...", 0)
validator.doLint(parsingContext.cancelIndicator)
validator.doLint(parsingContext)
if (errorsOccurred()) return false
parsingContext.reportProgress("Validating generated code...", 25)
validator.doValidate(parsingContext.cancelIndicator)
validator.doValidate(parsingContext)
return !errorsOccurred()
}

Expand Down
10 changes: 7 additions & 3 deletions org.lflang/src/org/lflang/generator/ts/TSValidator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.lflang.ErrorReporter
import org.lflang.generator.CodeMap
import org.lflang.generator.DiagnosticReporting
import org.lflang.generator.HumanReadableReportingStrategy
import org.lflang.generator.LFGeneratorContext
import org.lflang.generator.Position
import org.lflang.generator.ValidationStrategy
import org.lflang.generator.Validator
Expand Down Expand Up @@ -152,9 +153,12 @@ class TSValidator(

/**
* Run a relatively fast linter on the generated code.
* @param cancelIndicator The indicator of whether this build process is cancelled.
* @param context The context of the current build.
*/
fun doLint(cancelIndicator: CancelIndicator) {
TSLinter(fileConfig, errorReporter, codeMaps).doValidate(cancelIndicator)
fun doLint(context: LFGeneratorContext) {
TSLinter(fileConfig, errorReporter, codeMaps).doValidate(context)
}

// If this is not true, then the user might as well be writing JavaScript.
override fun validationEnabledByDefault(context: LFGeneratorContext?): Boolean = true
}

0 comments on commit 2d7e975

Please sign in to comment.