diff --git a/src/db/db/dbLayoutToNetlist.cc b/src/db/db/dbLayoutToNetlist.cc index 924b11a04..4970ac952 100644 --- a/src/db/db/dbLayoutToNetlist.cc +++ b/src/db/db/dbLayoutToNetlist.cc @@ -1,5 +1,4 @@ - /* KLayout Layout Viewer @@ -397,7 +396,7 @@ void LayoutToNetlist::join_nets (const tl::GlobPattern &cell, const std::set > &name_map_for_class (const return cc->second; } -#if defined(_DEBUG) +#if defined(HAVE_DEBUG) static std::string type_signature (const gsi::ArgType &t) { gsi::ArgType tr (t); @@ -622,7 +622,7 @@ ClassBase::merge_declarations () tl_assert (! c->declaration () || c->declaration () == &*c); } -#if defined(_DEBUG) +#if defined(HAVE_DEBUG) // do a sanity check for (gsi::ClassBase::class_iterator c = gsi::ClassBase::begin_classes (); c != gsi::ClassBase::end_classes (); ++c) { @@ -634,6 +634,16 @@ ClassBase::merge_declarations () method_counts [signature (*m, *s)] += 1; } } + // try to obtain the default values to find potential binding issues + for (gsi::MethodBase::argument_iterator a = (*m)->begin_arguments (); a != (*m)->end_arguments (); ++a) { + if (a->spec ()) { + try { + a->spec ()->default_value (); + } catch (tl::Exception &ex) { + tl::warn << "Method " << signature (*m, *(*m)->begin_synonyms ()) << ": error obtaining default value for argument '" << a->spec ()->name () << "': " << ex.msg (); + } + } + } } for (std::map::const_iterator mc = method_counts.begin (); mc != method_counts.end (); ++mc) { @@ -925,4 +935,3 @@ bool has_class (const std::type_info &ti) } } - diff --git a/src/gsi/gsi/gsiExpression.cc b/src/gsi/gsi/gsiExpression.cc index 3d77ddada..46ca33374 100644 --- a/src/gsi/gsi/gsiExpression.cc +++ b/src/gsi/gsi/gsiExpression.cc @@ -1036,10 +1036,10 @@ VariantUserClassImpl::execute_gsi (const tl::ExpressionParserContext & /*context // leave it to the consumer to establish the default values (that is faster) break; } - const tl::Variant &def_value = a->spec ()->default_value (); - // NOTE: this const_cast means we need to take care that we do not use default values on "out" parameters. - // Otherwise there is a chance we will modify the default value. - gsi::push_arg (arglist, *a, const_cast (def_value), &heap); + // Note: we will use the default value variant for longer, so push it to the heap (#1793) + tl::Variant *def_value = new tl::Variant (a->spec ()->default_value ()); + heap.push (def_value); + gsi::push_arg (arglist, *a, *def_value, &heap); } else { throw tl::Exception (tl::to_string ("No argument provided (positional or keyword) and no default value available")); } diff --git a/src/klayout.pri b/src/klayout.pri index dd882e4c1..e7f90e037 100644 --- a/src/klayout.pri +++ b/src/klayout.pri @@ -359,3 +359,6 @@ DEFINES += \ VERSION = $$KLAYOUT_VERSION +CONFIG(debug, debug|release) { + DEFINES += HAVE_DEBUG +} diff --git a/src/pya/pya/pyaCallables.cc b/src/pya/pya/pyaCallables.cc index 05b463f40..5422c9968 100644 --- a/src/pya/pya/pyaCallables.cc +++ b/src/pya/pya/pyaCallables.cc @@ -764,10 +764,10 @@ push_args (gsi::SerialArgs &arglist, const gsi::MethodBase *meth, PyObject *args // leave it to the consumer to establish the default values (that is faster) break; } - const tl::Variant &def_value = a->spec ()->default_value (); - // NOTE: this const_cast means we need to take care that we do not use default values on "out" parameters. - // Otherwise there is a chance we will modify the default value. - gsi::push_arg (arglist, *a, const_cast (def_value), &heap); + // Note: we will use the default value variant for longer, so push it to the heap (#1793) + tl::Variant *def_value = new tl::Variant (a->spec ()->default_value ()); + heap.push (def_value); + gsi::push_arg (arglist, *a, *def_value, &heap); } else { throw tl::Exception (tl::to_string (tr ("No argument provided (positional or keyword) and no default value available"))); } diff --git a/src/rba/rba/rba.cc b/src/rba/rba/rba.cc index e51abfeb4..21743409b 100644 --- a/src/rba/rba/rba.cc +++ b/src/rba/rba/rba.cc @@ -1125,10 +1125,10 @@ push_args (gsi::SerialArgs &arglist, const gsi::MethodBase *meth, VALUE *argv, i // leave it to the consumer to establish the default values (that is faster) break; } - const tl::Variant &def_value = a->spec ()->default_value (); - // NOTE: this const_cast means we need to take care that we do not use default values on "out" parameters. - // Otherwise there is a chance we will modify the default value. - gsi::push_arg (arglist, *a, const_cast (def_value), &heap); + // Note: we will use the default value variant for longer, so push it to the heap (#1793) + tl::Variant *def_value = new tl::Variant (a->spec ()->default_value ()); + heap.push (def_value); + gsi::push_arg (arglist, *a, *def_value, &heap); } else { throw tl::Exception (tl::to_string (tr ("No argument provided (positional or keyword) and no default value available"))); } diff --git a/src/tl/tl/tlVariant.cc b/src/tl/tl/tlVariant.cc index 28d476282..ced774fbc 100644 --- a/src/tl/tl/tlVariant.cc +++ b/src/tl/tl/tlVariant.cc @@ -317,15 +317,19 @@ Variant::Variant (const std::vector &ba) #if defined(HAVE_QT) Variant::Variant (const QByteArray &qba) - : m_type (t_qbytearray), m_string (0) + : m_type (qba.isNull () ? t_nil : t_qbytearray), m_string (0) { - m_var.m_qbytearray = new QByteArray (qba); + if (! qba.isNull ()) { + m_var.m_qbytearray = new QByteArray (qba); + } } Variant::Variant (const QString &qs) - : m_type (t_qstring), m_string (0) + : m_type (qs.isNull () ? t_nil : t_qstring), m_string (0) { - m_var.m_qstring = new QString (qs); + if (! qs.isNull ()) { + m_var.m_qstring = new QString (qs); + } } Variant::Variant (const QVariant &v) @@ -526,10 +530,14 @@ Variant::Variant (const std::string &s) } Variant::Variant (const char *s) - : m_type (t_string) + : m_type (s != 0 ? t_string : t_nil) { - m_string = new char [strlen (s) + 1]; - strcpy (m_string, s); + if (s) { + m_string = new char [strlen (s) + 1]; + strcpy (m_string, s); + } else { + m_string = 0; + } } Variant::Variant (double d) @@ -676,7 +684,9 @@ Variant::reset () Variant & Variant::operator= (const char *s) { - if (m_type == t_string && s == m_string) { + if (! s) { + reset (); + } else if (m_type == t_string && s == m_string) { // we are assigning to ourselves } else { char *snew = new char [strlen (s) + 1]; @@ -721,7 +731,9 @@ Variant::operator= (const std::vector &s) Variant & Variant::operator= (const QByteArray &qs) { - if (m_type == t_qbytearray && &qs == m_var.m_qbytearray) { + if (qs.isNull ()) { + reset (); + } else if (m_type == t_qbytearray && &qs == m_var.m_qbytearray) { // we are assigning to ourselves } else { QByteArray *snew = new QByteArray (qs); @@ -735,7 +747,9 @@ Variant::operator= (const QByteArray &qs) Variant & Variant::operator= (const QString &qs) { - if (m_type == t_qstring && &qs == m_var.m_qstring) { + if (qs.isNull ()) { + reset (); + } else if (m_type == t_qstring && &qs == m_var.m_qstring) { // we are assigning to ourselves } else { QString *snew = new QString (qs); @@ -1784,9 +1798,9 @@ Variant::to_string () const if (m_type == t_nil) { r = "nil"; } else if (m_type == t_double) { - r = tl::to_string (m_var.m_double); + r = tl::to_string (m_var.m_double, 15); } else if (m_type == t_float) { - r = tl::to_string (m_var.m_float); + r = tl::to_string (m_var.m_float, 7); } else if (m_type == t_char) { r = tl::to_string ((int) m_var.m_char); } else if (m_type == t_schar) { @@ -2421,7 +2435,7 @@ Variant::to_parsable_string () const } else if (is_ulonglong ()) { return "#lu" + tl::to_string (to_ulonglong ()); } else if (is_double ()) { - return "##" + tl::to_string (to_double ()); + return "##" + tl::to_string (to_double (), 15); } else if (is_bool ()) { return m_var.m_bool ? "true" : "false"; } else if (is_nil ()) { diff --git a/src/tl/tl/tlVariant.h b/src/tl/tl/tlVariant.h index 4b12a892a..15b66f06b 100644 --- a/src/tl/tl/tlVariant.h +++ b/src/tl/tl/tlVariant.h @@ -29,6 +29,8 @@ #include #include #include +#include +#include #include #include @@ -358,7 +360,99 @@ class TL_PUBLIC Variant } /** - * @brief Initialize the Variant with an explicit vector or variants + * @brief Initialize with a const user type (will always create a reference and NOT own the object) + */ + template + Variant (const T *obj) + : m_type (t_nil), m_string (0) + { + if (obj) { + *this = make_variant_ref (obj); + } + } + + /** + * @brief Initialize with a non-const user type (will always create a reference and NOT own the object) + */ + template + Variant (T *obj) + : m_type (t_nil), m_string (0) + { + if (obj) { + *this = make_variant_ref (obj); + } + } + + /** + * @brief Initialize the Variant with a STL vector + */ + template + Variant (const std::vector &list) + : m_type (t_list), m_string (0) + { + m_var.m_list = new std::vector (); + m_var.m_list->reserve (list.size ()); + for (auto i = list.begin (); i != list.end (); ++i) { + m_var.m_list->push_back (tl::Variant (*i)); + } + } + + /** + * @brief Initialize the Variant with a STL list + */ + template + Variant (const std::list &list) + : m_type (t_list), m_string (0) + { + m_var.m_list = new std::vector (); + m_var.m_list->reserve (list.size ()); + for (auto i = list.begin (); i != list.end (); ++i) { + m_var.m_list->push_back (tl::Variant (*i)); + } + } + + /** + * @brief Initialize the Variant with a STL set (not maintaining the set character) + */ + template + Variant (const std::set &list) + : m_type (t_list), m_string (0) + { + m_var.m_list = new std::vector (); + m_var.m_list->reserve (list.size ()); + for (auto i = list.begin (); i != list.end (); ++i) { + m_var.m_list->push_back (tl::Variant (*i)); + } + } + + /** + * @brief Initialize the Variant with a STL pair + */ + template + Variant (const std::pair &pair) + : m_type (t_list), m_string (0) + { + m_var.m_list = new std::vector (); + m_var.m_list->reserve (2); + m_var.m_list->push_back (tl::Variant (pair.first)); + m_var.m_list->push_back (tl::Variant (pair.second)); + } + + /** + * @brief Initialize the Variant with a STL map + */ + template + Variant (const std::map &map) + : m_type (t_array), m_string (0) + { + m_var.m_array = new std::map (); + for (auto i = map.begin (); i != map.end (); ++i) { + m_var.m_array->insert (std::make_pair (tl::Variant (i->first), tl::Variant (i->second))); + } + } + + /** + * @brief Initialize the Variant with an explicit vector of variants */ Variant (const std::vector &list) : m_type (t_list), m_string (0) @@ -366,6 +460,15 @@ class TL_PUBLIC Variant m_var.m_list = new std::vector (list); } + /** + * @brief Initialize the Variant with an explicit map of variants + */ + Variant (const std::map &map) + : m_type (t_array), m_string (0) + { + m_var.m_array = new std::map (map); + } + /** * @brief Initialize the Variant with a list */ diff --git a/src/tl/unit_tests/tlExpressionTests.cc b/src/tl/unit_tests/tlExpressionTests.cc index 3d8719900..3f4710ad4 100644 --- a/src/tl/unit_tests/tlExpressionTests.cc +++ b/src/tl/unit_tests/tlExpressionTests.cc @@ -927,7 +927,7 @@ TEST(8) bool t; v = e.parse ("1==2?log('a'):log(2)").execute (); - EXPECT_EQ (v.to_string (), std::string ("0.69314718056")); + EXPECT_EQ (v.to_string (), std::string ("0.693147180559945")); t = false; try { v = e.parse ("1==1?log('a'):log(2)").execute (); diff --git a/src/tl/unit_tests/tlVariantTests.cc b/src/tl/unit_tests/tlVariantTests.cc index b73534887..d03a56d1c 100644 --- a/src/tl/unit_tests/tlVariantTests.cc +++ b/src/tl/unit_tests/tlVariantTests.cc @@ -28,10 +28,12 @@ #include "tlTypeTraits.h" #include "tlUnitTest.h" -#include #include #include +#define _USE_MATH_DEFINES // for MSVC +#include + struct A { std::string a; @@ -1092,8 +1094,84 @@ TEST(6) EXPECT_EQ (tl::Variant (-0.1 * (1.0 + 1.1e-13)) < tl::Variant (0.1), true); } +// precision of double serialization +TEST(7a) +{ + tl::Variant v (M_PI); + tl::Variant vx; + std::string s (v.to_parsable_string ()); + tl::Extractor ex (s.c_str ()); + ex.read (vx); + + EXPECT_EQ (fabs (M_PI - vx.to_double ()) < 4e-15, true); +} + +TEST(7b) +{ + tl::Variant v ((float) M_PI); + tl::Variant vx; + std::string s (v.to_parsable_string ()); + tl::Extractor ex (s.c_str ()); + ex.read (vx); + + EXPECT_EQ (fabs (M_PI - vx.to_double ()) < 1e-7, true); +} + +// null strings +TEST(8) +{ + tl::Variant v ((const char *) 0); + EXPECT_EQ (v.to_parsable_string (), "nil"); + v = tl::Variant ("abc"); + EXPECT_EQ (v.to_parsable_string (), "'abc'"); + v = (const char *) 0; + EXPECT_EQ (v.to_parsable_string (), "nil"); + +#if defined(HAVE_QT) + v = tl::Variant (QString ()); + EXPECT_EQ (v.to_parsable_string (), "nil"); + v = tl::Variant (QString::fromUtf8 ("abc")); + EXPECT_EQ (v.to_parsable_string (), "'abc'"); + v = QString (); + EXPECT_EQ (v.to_parsable_string (), "nil"); + + v = tl::Variant (QByteArray ()); + EXPECT_EQ (v.to_parsable_string (), "nil"); + v = tl::Variant (QByteArray ("abc")); + EXPECT_EQ (v.to_parsable_string (), "'abc'"); + v = QByteArray (); + EXPECT_EQ (v.to_parsable_string (), "nil"); +#endif +} + +// create from STL containers +TEST(9) +{ + tl::Variant v; + + std::vector vi; + vi.push_back (17); + vi.push_back (1); + std::list li; + li.push_back (42); + li.push_back (-17); + std::set si; + si.insert (31); + si.insert (63); + std::pair pi (1, 3); + std::map mi; + mi[17] = 42; + mi[-1] = 31; + + EXPECT_EQ (tl::Variant (vi).to_parsable_string (), "(#17,#1)"); + EXPECT_EQ (tl::Variant (li).to_parsable_string (), "(#42,#-17)"); + EXPECT_EQ (tl::Variant (si).to_parsable_string (), "(#31,#63)"); + EXPECT_EQ (tl::Variant (pi).to_parsable_string (), "(#1,#3)"); + EXPECT_EQ (tl::Variant (mi).to_parsable_string (), "{#-1=>#31,#17=>#42}"); +} + // special numeric values -TEST(7) +TEST(10) { std::string s; tl::Extractor ex; diff --git a/testdata/drc/drcSimpleTests_au5.gds b/testdata/drc/drcSimpleTests_au5.gds index c77c4e703..e89e30330 100644 Binary files a/testdata/drc/drcSimpleTests_au5.gds and b/testdata/drc/drcSimpleTests_au5.gds differ diff --git a/testdata/drc/drcSimpleTests_au6.gds b/testdata/drc/drcSimpleTests_au6.gds index 0b311b67b..752f13b5a 100644 Binary files a/testdata/drc/drcSimpleTests_au6.gds and b/testdata/drc/drcSimpleTests_au6.gds differ diff --git a/testdata/drc/drcSimpleTests_au7.gds b/testdata/drc/drcSimpleTests_au7.gds index b96c9dd3c..c7a3cc36d 100644 Binary files a/testdata/drc/drcSimpleTests_au7.gds and b/testdata/drc/drcSimpleTests_au7.gds differ