From e818ca3de594d15dabad80e48ca06f90a41105a1 Mon Sep 17 00:00:00 2001 From: Florian Angerer <florian.angerer@oracle.com> Date: Wed, 3 May 2017 16:06:33 +0200 Subject: [PATCH] Deparsed source code is now written to temporary files to ease debugging. --- .../truffle/r/runtime/FastROptions.java | 2 ++ .../oracle/truffle/r/runtime/RDeparse.java | 29 +++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) 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 8c99eacf36..9258059341 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 03cdad3ac2..eb5e9f3c07 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)); + } } } -- GitLab