diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/FastROptions.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/FastROptions.java index 8c99eacf36a525b2824780f99de4f8bb3092e927..92580593411763353fab24fbff7e2315f5ae0f27 100644 --- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/FastROptions.java +++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/FastROptions.java @@ -57,6 +57,8 @@ public enum FastROptions { SharedContexts("Whether all child contexts are to be shared contexts", true), SearchPathForcePromises("Whether all promises for frames on shared path are forced in presence of shared contexts", false), LoadPackagesNativeCode("Load native code of packages, including builtin packages.", !FastRConfig.ManagedMode), + EmitTmpSource("Write deparsed source code to temporary files for better debugging.", true), + EmitTmpDir("The directory where to allocate temporary files with deparsed source code.", null, true), // Promises optimizations EagerEval("If enabled, overrides all other EagerEval switches (see EagerEvalHelper)", false), diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RDeparse.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RDeparse.java index 03cdad3ac2418da7b92327c41e95a7bf8cb393f8..eb5e9f3c07b8e47633e6fe6eefd96dc1c33cbee2 100644 --- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RDeparse.java +++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RDeparse.java @@ -11,6 +11,12 @@ */ package com.oracle.truffle.r.runtime; +import java.io.BufferedWriter; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; @@ -317,9 +323,26 @@ public class RDeparse { } public void fixupSources() { - Source source = RSource.fromTextInternal(sb.toString(), RSource.Internal.DEPARSE); - for (SourceSectionElement s : sources) { - s.element.setSourceSection(source.createSection(s.start, s.length)); + if (FastROptions.EmitTmpSource.getBooleanValue()) { + String tmpDirStr = FastROptions.EmitTmpDir.getStringValue(); + Path tmpDir = tmpDirStr != null ? Paths.get(tmpDirStr) : null; + for (SourceSectionElement s : sources) { + try { + Path path = Files.createTempFile(tmpDir, "deparse-", ".r"); + try (BufferedWriter bw = Files.newBufferedWriter(path, StandardOpenOption.WRITE)) { + bw.write(sb.toString()); + } + Source source = RSource.fromFile(path.toFile()); + s.element.setSourceSection(source.createSection(s.start, s.length)); + } catch (IOException e) { + RInternalError.reportError(e); + } + } + } else { + Source source = RSource.fromTextInternal(sb.toString(), RSource.Internal.DEPARSE); + for (SourceSectionElement s : sources) { + s.element.setSourceSection(source.createSection(s.start, s.length)); + } } }