Skip to content
Permalink
Browse files
Converted SD(E/W)Unless/When to macros
Using macros for schemaDefinitionUnless/When will prevent the need to
allocate an WrappedArray for the variable arguments passed into the
function when we know that the Unless/When test will fail and not need
to throw an SDE or SDW.

DFDL-1460
  • Loading branch information
jadams-tresys committed Jun 29, 2017
1 parent 9561651 commit 41b6a826aa6e629aada893f7203de141924ff23d
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 19 deletions.
@@ -72,14 +72,15 @@ trait ThrowsSDE {

final def schemaDefinitionError(str: String, args: Any*): Nothing = SDE(str, args: _*) // long form synonym

final def schemaDefinitionUnless(testThatWillThrowIfFalse: Boolean, str: String, args: Any*) {
if (!testThatWillThrowIfFalse)
SDE(str, args: _*)
}
/***
* These functions are now macros as the original code:
* final def schemaDefinitionUnless(testThatWillThrowIfFalse: Boolean, str: => String, args: => Any*) = if (!testThatWillThrowIfFalse) SDE(str, args: _*)
* would cause expensive object allocation, even when the
* test would be true and even when the function was inlined
*/
final def schemaDefinitionUnless(testThatWillThrowIfFalse: Boolean, str: String, args: Any*): Unit = macro SDEMacros.schemaDefinitionUnlessMacro

final def schemaDefinitionWhen(testThatWillThrowIfTrue: Boolean, str: String, args: Any*) {
schemaDefinitionUnless(!testThatWillThrowIfTrue, str, args: _*)
}
final def schemaDefinitionWhen(testThatWillThrowIfTrue: Boolean, str: String, args: Any*): Unit = macro SDEMacros.schemaDefinitionWhenMacro

final def notYetImplemented(msg: String, args: Any*): Nothing = SDE("Feature not yet implemented: " + msg, args: _*)

@@ -105,27 +106,25 @@ trait SavesErrorsAndWarnings {

def schemaDefinitionErrorButContinue(str: String, args: Any*): Unit = SDEButContinue(str, args: _*)

def schemaDefinitionWarningUnless(testThatWillWarnIfFalse: Boolean, str: String, args: Any*) {
if (!testThatWillWarnIfFalse) SDW(str, args: _*)
}
/***
* These functions are now macros as the original code:
* final def schemaDefinitionUnless(testThatWillThrowIfFalse: Boolean, str: => String, args: => Any*) = if (!testThatWillThrowIfFalse) SDE(str, args: _*)
* would cause expensive object allocation, even when the
* test would be true and even when the function was inlined
*/
def schemaDefinitionWarningUnless(testThatWillWarnIfFalse: Boolean, str: String, args: Any*): Unit = macro SDEMacros.schemaDefinitionWarningUnlessMacro

/**
* Use this form if you need to be able to suppress the warning
*/
def schemaDefinitionWarningUnless(warnID: WarnID, testThatWillWarnIfFalse: Boolean, str: String, args: Any*) {
if (!testThatWillWarnIfFalse) SDW(warnID, str, args: _*)
}
def schemaDefinitionWarningUnless(warnID: WarnID, testThatWillWarnIfFalse: Boolean, str: String, args: Any*): Unit = macro SDEMacros.schemaDefinitionWarningUnlessSuppressMacro

def schemaDefinitionWarningWhen(testThatWillWarnIfTrue: Boolean, str: String, args: Any*) {
schemaDefinitionWarningUnless(!testThatWillWarnIfTrue, str, args: _*)
}
def schemaDefinitionWarningWhen(testThatWillWarnIfTrue: Boolean, str: String, args: Any*): Unit = macro SDEMacros.schemaDefinitionWarningWhenMacro

/**
* Use this form if you need to be able to suppress the warning
*/
def schemaDefinitionWarningWhen(warnID: WarnID, testThatWillWarnIfTrue: Boolean, str: String, args: Any*) {
schemaDefinitionWarningUnless(warnID, !testThatWillWarnIfTrue, str, args: _*)
}
def schemaDefinitionWarningWhen(warnID: WarnID, testThatWillWarnIfTrue: Boolean, str: String, args: Any*): Unit = macro SDEMacros.schemaDefinitionWarningWhenSuppressMacro

/**
* SDE special case when we're blaming the error on the value of a property.
@@ -0,0 +1,122 @@
/* Copyright (c) 2017 Tresys Technology, LLC. All rights reserved.
*
* Developed by: Tresys Technology, LLC
* http://www.tresys.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal with
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimers.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimers in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the names of Tresys Technology, nor the names of its contributors
* may be used to endorse or promote products derived from this Software
* without specific prior written permission.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
* SOFTWARE.
*/

package edu.illinois.ncsa.daffodil.exceptions

import scala.reflect.macros.blackbox.Context

object SDEMacros {

def schemaDefinitionUnlessMacro(c: Context)(testThatWillThrowIfFalse: c.Expr[Boolean], str: c.Expr[String], args: c.Expr[Any]*): c.Expr[Unit] = {
import c.universe._

val selfExp = c.prefix

c.Expr(q"""
{
if (!($testThatWillThrowIfFalse)) {
$selfExp.SDE($str, ..$args)
}
}
""")
}

def schemaDefinitionWhenMacro(c: Context)(testThatWillThrowIfTrue: c.Expr[Boolean], str: c.Expr[String], args: c.Expr[Any]*): c.Expr[Unit] = {
import c.universe._

val selfExp = c.prefix

c.Expr(q"""
{
if ($testThatWillThrowIfTrue) {
$selfExp.SDE($str, ..$args)
}
}
""")
}

def schemaDefinitionWarningUnlessMacro(c: Context)(testThatWillWarnIfFalse: c.Expr[Boolean], str: c.Expr[String], args: c.Expr[Any]*): c.Expr[Unit] = {
import c.universe._

val selfExp = c.prefix

c.Expr(q"""
{
if (!($testThatWillWarnIfFalse)) {
$selfExp.SDW($str, ..$args)
}
}
""")
}

def schemaDefinitionWarningUnlessSuppressMacro(c: Context)(warnID: c.Tree, testThatWillWarnIfFalse: c.Expr[Boolean], str: c.Expr[String], args: c.Expr[Any]*): c.Expr[Unit] = {
import c.universe._

val selfExp = c.prefix

c.Expr(q"""
{
if (!($testThatWillWarnIfFalse)) {
$selfExp.SDW($warnID, $str, ..$args)
}
}
""")
}

def schemaDefinitionWarningWhenMacro(c: Context)(testThatWillWarnIfTrue: c.Expr[Boolean], str: c.Expr[String], args: c.Expr[Any]*): c.Expr[Unit] = {
import c.universe._

val selfExp = c.prefix

c.Expr(q"""
{
if ($testThatWillWarnIfTrue) {
$selfExp.SDW($str, ..$args)
}
}
""")
}

def schemaDefinitionWarningWhenSuppressMacro(c: Context)(warnID: c.Tree, testThatWillWarnIfTrue: c.Expr[Boolean], str: c.Expr[String], args: c.Expr[Any]*): c.Expr[Unit] = {
import c.universe._

val selfExp = c.prefix

c.Expr(q"""
{
if ($testThatWillWarnIfTrue) {
$selfExp.SDW($warnID, $str, ..$args)
}
}
""")
}
}

0 comments on commit 41b6a82

Please sign in to comment.