Skip to content

Commit

Permalink
Merge pull request #2092 from lf-lang/no-source-mapping
Browse files Browse the repository at this point in the history
New `--no-source-mapping` switch to disable line directives
  • Loading branch information
lhstrh authored Nov 13, 2023
2 parents e8e7c24 + b15c75f commit f01889e
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 36 deletions.
8 changes: 8 additions & 0 deletions cli/lfc/src/main/java/org/lflang/cli/Lfc.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.lflang.target.property.CompilerProperty;
import org.lflang.target.property.LoggingProperty;
import org.lflang.target.property.NoCompileProperty;
import org.lflang.target.property.NoSourceMappingProperty;
import org.lflang.target.property.PrintStatisticsProperty;
import org.lflang.target.property.RuntimeVersionProperty;
import org.lflang.target.property.SchedulerProperty;
Expand Down Expand Up @@ -146,6 +147,12 @@ public class Lfc extends CliBase {
description = "Specify whether to enable run-time tracing (if supported).")
private Boolean tracing;

@Option(
names = {"--no-source-mapping"},
arity = "0",
description = "Do not map lines in generated code to LF sources.")
private Boolean noSourceMapping;

/** Mutually exclusive options related to threading. */
static class ThreadingMutuallyExclusive {
@Option(
Expand Down Expand Up @@ -374,6 +381,7 @@ public GeneratorArguments getArgs() {
new Argument<>(LoggingProperty.INSTANCE, getLogging()),
new Argument<>(PrintStatisticsProperty.INSTANCE, printStatistics),
new Argument<>(NoCompileProperty.INSTANCE, noCompile),
new Argument<>(NoSourceMappingProperty.INSTANCE, noSourceMapping),
new Argument<>(VerifyProperty.INSTANCE, verify),
new Argument<>(RuntimeVersionProperty.INSTANCE, runtimeVersion),
new Argument<>(SchedulerProperty.INSTANCE, getScheduler()),
Expand Down
16 changes: 12 additions & 4 deletions core/src/main/java/org/lflang/generator/CodeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,12 @@ public void pr(CharSequence text) {
* Print the #line compiler directive with the line number of the specified object.
*
* @param eObject The node.
* @param suppress Do nothing if true.
*/
public void prSourceLineNumber(EObject eObject) {
public void prSourceLineNumber(EObject eObject, boolean suppress) {
if (suppress) {
return;
}
var node = NodeModelUtils.getNode(eObject);
if (node != null) {
// For code blocks (delimited by {= ... =}, unfortunately,
Expand All @@ -127,9 +131,13 @@ public void prSourceLineNumber(EObject eObject) {
}
}

/** Print a tag marking the end of a block corresponding to the source LF file. */
public void prEndSourceLineNumber() {
pr(END_SOURCE_LINE_NUMBER_TAG);
/**
* Print a tag marking the end of a block corresponding to the source LF file.
*
* @param suppress Do nothing if true.
*/
public void prEndSourceLineNumber(boolean suppress) {
if (!suppress) pr(END_SOURCE_LINE_NUMBER_TAG);
}

/**
Expand Down
18 changes: 11 additions & 7 deletions core/src/main/java/org/lflang/generator/c/CGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
import org.lflang.target.property.FedSetupProperty;
import org.lflang.target.property.LoggingProperty;
import org.lflang.target.property.NoCompileProperty;
import org.lflang.target.property.NoSourceMappingProperty;
import org.lflang.target.property.PlatformProperty;
import org.lflang.target.property.PlatformProperty.PlatformOption;
import org.lflang.target.property.ProtobufsProperty;
Expand Down Expand Up @@ -986,10 +987,11 @@ private void generateReactorClassBody(
// Some of the following methods create lines of code that need to
// go into the constructor. Collect those lines of code here:
var constructorCode = new CodeBuilder();
var suppressLineDirectives = targetConfig.get(NoSourceMappingProperty.INSTANCE);
generateAuxiliaryStructs(header, tpr, false);
// The following must go before the self struct so the #include watchdog.h ends up in the
// header.
CWatchdogGenerator.generateWatchdogs(src, header, tpr, messageReporter);
CWatchdogGenerator.generateWatchdogs(src, header, tpr, suppressLineDirectives, messageReporter);
generateSelfStruct(header, tpr, constructorCode);
generateMethods(src, tpr);
generateReactions(src, tpr);
Expand All @@ -998,7 +1000,8 @@ private void generateReactorClassBody(

/** Generate methods for {@code reactor}. */
protected void generateMethods(CodeBuilder src, TypeParameterizedReactor tpr) {
CMethodGenerator.generateMethods(tpr, src, types);
CMethodGenerator.generateMethods(
tpr, src, types, this.targetConfig.get(NoSourceMappingProperty.INSTANCE));
}

/**
Expand All @@ -1009,9 +1012,10 @@ protected void generateMethods(CodeBuilder src, TypeParameterizedReactor tpr) {
protected void generateUserPreamblesForReactor(Reactor reactor, CodeBuilder src) {
for (Preamble p : ASTUtils.allPreambles(reactor)) {
src.pr("// *********** From the preamble, verbatim:");
src.prSourceLineNumber(p.getCode());
var suppressLineDirectives = this.targetConfig.get(NoSourceMappingProperty.INSTANCE);
src.prSourceLineNumber(p.getCode(), suppressLineDirectives);
src.pr(toText(p.getCode()));
src.prEndSourceLineNumber();
src.prEndSourceLineNumber(suppressLineDirectives);
src.pr("\n// *********** End of preamble.");
}
}
Expand Down Expand Up @@ -1083,7 +1087,7 @@ private void generateSelfStruct(
CodeBuilder builder, TypeParameterizedReactor tpr, CodeBuilder constructorCode) {
var reactor = toDefinition(tpr.reactor());
var selfType = CUtil.selfType(tpr);

var suppressLineDirectives = this.targetConfig.get(NoSourceMappingProperty.INSTANCE);
// Construct the typedef for the "self" struct.
// Create a type name for the self struct.
var body = new CodeBuilder();
Expand All @@ -1092,10 +1096,10 @@ private void generateSelfStruct(
generateSelfStructExtension(body, reactor, constructorCode);

// Next handle parameters.
body.pr(CParameterGenerator.generateDeclarations(tpr, types));
body.pr(CParameterGenerator.generateDeclarations(tpr, types, suppressLineDirectives));

// Next handle states.
body.pr(CStateGenerator.generateDeclarations(tpr, types));
body.pr(CStateGenerator.generateDeclarations(tpr, types, suppressLineDirectives));

// Next handle actions.
CActionGenerator.generateDeclarations(tpr, body, constructorCode);
Expand Down
21 changes: 14 additions & 7 deletions core/src/main/java/org/lflang/generator/c/CMethodGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,15 @@ public static void generateMacroUndefsForMethods(Reactor reactor, CodeBuilder bo
* @param method The method.
* @param tpr The concrete reactor class.
* @param types The C-specific type conversion functions.
* @param suppressLineDirectives Whether to suppress the generation of line directives.
*/
public static String generateMethod(Method method, TypeParameterizedReactor tpr, CTypes types) {
public static String generateMethod(
Method method, TypeParameterizedReactor tpr, CTypes types, boolean suppressLineDirectives) {
var code = new CodeBuilder();
var body = ASTUtils.toText(method.getCode());

code.prSourceLineNumber(method);
code.prSourceLineNumber(method, suppressLineDirectives);

code.prComment("Implementation of method " + method.getName() + "()");
code.pr(generateMethodSignature(method, tpr, types) + " {");
code.indent();
Expand All @@ -76,12 +79,11 @@ public static String generateMethod(Method method, TypeParameterizedReactor tpr,
+ "*)instance_args;"
+ " SUPPRESS_UNUSED_WARNING(self);"));
}

code.prSourceLineNumber(method.getCode());
code.prSourceLineNumber(method.getCode(), suppressLineDirectives);
code.pr(body);
code.unindent();
code.pr("}");
code.prEndSourceLineNumber();
code.prEndSourceLineNumber(suppressLineDirectives);
return code.toString();
}

Expand All @@ -92,14 +94,19 @@ public static String generateMethod(Method method, TypeParameterizedReactor tpr,
* @param tpr The reactor.
* @param code The place to put the code.
* @param types The C-specific type conversion functions.
* @param suppressLineDirectives Whether to suppress the generation of line directives.
*/
public static void generateMethods(TypeParameterizedReactor tpr, CodeBuilder code, CTypes types) {
public static void generateMethods(
TypeParameterizedReactor tpr,
CodeBuilder code,
CTypes types,
boolean suppressLineDirectives) {
var reactor = tpr.reactor();
code.prComment("***** Start of method declarations.");
signatures(tpr, code, types);
generateMacrosForMethods(tpr, code);
for (Method method : allMethods(reactor)) {
code.pr(CMethodGenerator.generateMethod(method, tpr, types));
code.pr(CMethodGenerator.generateMethod(method, tpr, types, suppressLineDirectives));
}
generateMacroUndefsForMethods(reactor, code);
code.prComment("***** End of method declarations.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,20 @@ public static String getInitializer(ParameterInstance p) {
*
* @param reactor {@link TypeParameterizedReactor}
* @param types A helper class for types
* @param suppressLineDirectives Whether to suppress the generation of line directives.
*/
public static String generateDeclarations(TypeParameterizedReactor reactor, CTypes types) {
public static String generateDeclarations(
TypeParameterizedReactor reactor, CTypes types, boolean suppressLineDirectives) {
CodeBuilder code = new CodeBuilder();
for (Parameter parameter : ASTUtils.allParameters(reactor.reactor())) {
code.prSourceLineNumber(parameter);
code.prSourceLineNumber(parameter, suppressLineDirectives);
code.pr(
types.getTargetType(reactor.resolveType(ASTUtils.getInferredType(parameter)))
+ " "
+ parameter.getName()
+ ";");
}
code.prEndSourceLineNumber();
code.prEndSourceLineNumber(suppressLineDirectives);
return code.toString();
}
}
20 changes: 14 additions & 6 deletions core/src/main/java/org/lflang/generator/c/CReactionGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.lflang.lf.Variable;
import org.lflang.lf.Watchdog;
import org.lflang.target.TargetConfig;
import org.lflang.target.property.NoSourceMappingProperty;
import org.lflang.util.StringUtil;

public class CReactionGenerator {
Expand Down Expand Up @@ -1121,6 +1122,7 @@ public static String generateReaction(
boolean requiresType) {
var code = new CodeBuilder();
var body = ASTUtils.toText(getCode(types, reaction, tpr));
var suppressLineDirectives = targetConfig.get(NoSourceMappingProperty.INSTANCE);
String init =
generateInitializationForReaction(
body, reaction, tpr, reactionIndex, types, messageReporter, mainDef, requiresType);
Expand All @@ -1132,14 +1134,18 @@ public static String generateReaction(
generateFunction(
generateReactionFunctionHeader(tpr, reactionIndex),
init,
getCode(types, reaction, tpr)));
getCode(types, reaction, tpr),
suppressLineDirectives));
// Now generate code for the late function, if there is one
// Note that this function can only be defined on reactions
// in federates that have inputs from a logical connection.
if (reaction.getStp() != null) {
code.pr(
generateFunction(
generateStpFunctionHeader(tpr, reactionIndex), init, reaction.getStp().getCode()));
generateStpFunctionHeader(tpr, reactionIndex),
init,
reaction.getStp().getCode(),
suppressLineDirectives));
}

// Now generate code for the deadline violation function, if there is one.
Expand All @@ -1148,7 +1154,8 @@ public static String generateReaction(
generateFunction(
generateDeadlineFunctionHeader(tpr, reactionIndex),
init,
reaction.getDeadline().getCode()));
reaction.getDeadline().getCode(),
suppressLineDirectives));
}
CMethodGenerator.generateMacroUndefsForMethods(tpr.reactor(), code);
code.pr("#include " + StringUtil.addDoubleQuotes(CCoreFilesUtils.getCTargetSetUndefHeader()));
Expand All @@ -1168,14 +1175,15 @@ private static Code getCode(CTypes types, Reaction r, TypeParameterizedReactor t
return ret;
}

public static String generateFunction(String header, String init, Code code) {
public static String generateFunction(
String header, String init, Code code, boolean suppressLineDirectives) {
var function = new CodeBuilder();
function.pr(header + " {");
function.indent();
function.pr(init);
function.prSourceLineNumber(code);
function.prSourceLineNumber(code, suppressLineDirectives);
function.pr(ASTUtils.toText(code));
function.prEndSourceLineNumber();
function.prEndSourceLineNumber(suppressLineDirectives);
function.unindent();
function.pr("}");
return function.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@ public class CStateGenerator {
*
* @param reactor {@link TypeParameterizedReactor}
* @param types A helper object for types
* @param suppressLineDirectives Whether to suppress the generation of line directives.
*/
public static String generateDeclarations(TypeParameterizedReactor reactor, CTypes types) {
public static String generateDeclarations(
TypeParameterizedReactor reactor, CTypes types, boolean suppressLineDirectives) {
CodeBuilder code = new CodeBuilder();
for (StateVar stateVar : ASTUtils.allStateVars(reactor.reactor())) {
code.prSourceLineNumber(stateVar);
code.prSourceLineNumber(stateVar, suppressLineDirectives);
code.pr(
types.getTargetType(reactor.resolveType(ASTUtils.getInferredType(stateVar)))
+ " "
+ stateVar.getName()
+ ";");
}
code.prEndSourceLineNumber();
code.prEndSourceLineNumber(suppressLineDirectives);
return code.toString();
}

Expand Down
20 changes: 14 additions & 6 deletions core/src/main/java/org/lflang/generator/c/CWatchdogGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,19 @@ protected static int generateInitializeWatchdogs(CodeBuilder code, ReactorInstan
*
* @param src The place to put the code
* @param header The place to put header code
* @param suppressLineDirectives Whether to suppress the generation of line directives.
* @param tpr The reactor declaration
*/
protected static void generateWatchdogs(
CodeBuilder src,
CodeBuilder header,
TypeParameterizedReactor tpr,
boolean suppressLineDirectives,
MessageReporter messageReporter) {
if (hasWatchdogs(tpr.reactor())) {
header.pr("#include \"core/threaded/watchdog.h\"");
for (Watchdog watchdog : ASTUtils.allWatchdogs(tpr.reactor())) {
src.pr(generateWatchdogFunction(watchdog, tpr, messageReporter));
src.pr(generateWatchdogFunction(watchdog, tpr, messageReporter, suppressLineDirectives));
}
}
}
Expand Down Expand Up @@ -253,29 +255,35 @@ private static String generateInitializationForWatchdog(
* @param header function name and declaration.
* @param init initialize variable.
* @param watchdog The watchdog.
* @param suppressLineDirectives Whether to suppress the generation of line directives.
*/
private static String generateFunction(String header, String init, Watchdog watchdog) {
private static String generateFunction(
String header, String init, Watchdog watchdog, boolean suppressLineDirectives) {
var function = new CodeBuilder();
function.pr(header + " {");
function.indent();
function.pr(init);
function.pr(
"_lf_schedule(self->base.environment, (*" + watchdog.getName() + ").trigger, 0, NULL);");
function.prSourceLineNumber(watchdog.getCode());
function.prSourceLineNumber(watchdog.getCode(), suppressLineDirectives);
function.pr(ASTUtils.toText(watchdog.getCode()));
function.prEndSourceLineNumber();
function.prEndSourceLineNumber(suppressLineDirectives);
function.unindent();
function.pr("}");
return function.toString();
}

/** Generate the watchdog handler function. */
private static String generateWatchdogFunction(
Watchdog watchdog, TypeParameterizedReactor tpr, MessageReporter messageReporter) {
Watchdog watchdog,
TypeParameterizedReactor tpr,
MessageReporter messageReporter,
boolean suppressLineDirectives) {
return generateFunction(
generateWatchdogFunctionHeader(watchdog, tpr),
generateInitializationForWatchdog(watchdog, tpr, messageReporter),
watchdog);
watchdog,
suppressLineDirectives);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/java/org/lflang/target/Target.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.lflang.target.property.FilesProperty;
import org.lflang.target.property.KeepaliveProperty;
import org.lflang.target.property.NoRuntimeValidationProperty;
import org.lflang.target.property.NoSourceMappingProperty;
import org.lflang.target.property.PlatformProperty;
import org.lflang.target.property.PrintStatisticsProperty;
import org.lflang.target.property.ProtobufsProperty;
Expand Down Expand Up @@ -598,6 +599,7 @@ public void initialize(TargetConfig config) {
DockerProperty.INSTANCE,
FilesProperty.INSTANCE,
KeepaliveProperty.INSTANCE,
NoSourceMappingProperty.INSTANCE,
PlatformProperty.INSTANCE,
ProtobufsProperty.INSTANCE,
SchedulerProperty.INSTANCE,
Expand Down Expand Up @@ -631,6 +633,7 @@ public void initialize(TargetConfig config) {
DockerProperty.INSTANCE,
FilesProperty.INSTANCE,
KeepaliveProperty.INSTANCE,
NoSourceMappingProperty.INSTANCE,
ProtobufsProperty.INSTANCE,
SchedulerProperty.INSTANCE,
SingleThreadedProperty.INSTANCE,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.lflang.target.property;

/** Directive to instruct the code generator to not produce line directives. */
public final class NoSourceMappingProperty extends BooleanProperty {

/** Singleton target property instance. */
public static final NoSourceMappingProperty INSTANCE = new NoSourceMappingProperty();

private NoSourceMappingProperty() {
super();
}

@Override
public String name() {
return "no-source-mapping";
}
}

0 comments on commit f01889e

Please sign in to comment.