Skip to content

Commit

Permalink
Merge pull request #1086.
Browse files Browse the repository at this point in the history
Avoid dangling else in error macros
  • Loading branch information
lballabio committed Apr 18, 2021
2 parents 2dc188c + 85754d8 commit fe86fff
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 25 deletions.
2 changes: 1 addition & 1 deletion ql/cashflows/cpicouponpricer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace QuantLib {

void CPICouponPricer::setCapletVolatility(
const Handle<CPIVolatilitySurface>& capletVol) {
QL_REQUIRE(!capletVol.empty(),"empty capletVol handle")
QL_REQUIRE(!capletVol.empty(),"empty capletVol handle");
capletVol_ = capletVol;
registerWith(capletVol_);
}
Expand Down
2 changes: 1 addition & 1 deletion ql/cashflows/inflationcouponpricer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace QuantLib {

void YoYInflationCouponPricer::setCapletVolatility(
const Handle<YoYOptionletVolatilitySurface>& capletVol) {
QL_REQUIRE(!capletVol.empty(),"empty capletVol handle")
QL_REQUIRE(!capletVol.empty(),"empty capletVol handle");
capletVol_ = capletVol;
registerWith(capletVol_);
}
Expand Down
54 changes: 42 additions & 12 deletions ql/errors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,68 +60,98 @@ namespace QuantLib {

}

/* Fix C4127: conditional expression is constant when wrapping macros
with do { ... } while(false); on MSVC
#define QL_MULTILINE_FAILURE_BEGIN do {

/* Disable warning C4127 (conditional expression is constant) when
wrapping macros with the do { ... } while(false) construct on MSVC
*/
#define MULTILINE_MACRO_BEGIN do {
#if defined(BOOST_MSVC)
#define QL_MULTILINE_FAILURE_END \
__pragma(warning(push)) \
__pragma(warning(disable:4127)) \
} while(false) \
__pragma(warning(pop))
#else
#define QL_MULTILINE_FAILURE_END } while(false)
#endif


/* on MSVC++13 the do { ... } while(false) construct fails inside a
for loop without brackets, so we use a dangling else instead */
#if defined(QL_PATCH_MSVC_2013)
#define QL_MULTILINE_ASSERTION_BEGIN
#else
#define QL_MULTILINE_ASSERTION_BEGIN do {
#endif

/* Disable warning C4127 (conditional expression is constant) when
wrapping macros with the do { ... } while(false) construct on MSVC
*/
#if defined(BOOST_MSVC)
#define MULTILINE_MACRO_END \
#if defined(QL_PATCH_MSVC_2013)
#define QL_MULTILINE_ASSERTION_END else
#else
#define QL_MULTILINE_ASSERTION_END \
__pragma(warning(push)) \
__pragma(warning(disable:4127)) \
} while(false) \
__pragma(warning(pop))
#endif
#else
#define MULTILINE_MACRO_END } while(false)
#define QL_MULTILINE_ASSERTION_END } while(false)
#endif


/*! \def QL_FAIL
\brief throw an error (possibly with file and line information)
*/
#define QL_FAIL(message) \
MULTILINE_MACRO_BEGIN \
QL_MULTILINE_FAILURE_BEGIN \
std::ostringstream _ql_msg_stream; \
_ql_msg_stream << message; \
throw QuantLib::Error(__FILE__,__LINE__, \
BOOST_CURRENT_FUNCTION,_ql_msg_stream.str()); \
MULTILINE_MACRO_END
QL_MULTILINE_FAILURE_END


/*! \def QL_ASSERT
\brief throw an error if the given condition is not verified
*/
#define QL_ASSERT(condition,message) \
QL_MULTILINE_ASSERTION_BEGIN \
if (!(condition)) { \
std::ostringstream _ql_msg_stream; \
_ql_msg_stream << message; \
throw QuantLib::Error(__FILE__,__LINE__, \
BOOST_CURRENT_FUNCTION,_ql_msg_stream.str()); \
} else

} \
QL_MULTILINE_ASSERTION_END

/*! \def QL_REQUIRE
\brief throw an error if the given pre-condition is not verified
*/
#define QL_REQUIRE(condition,message) \
QL_MULTILINE_ASSERTION_BEGIN \
if (!(condition)) { \
std::ostringstream _ql_msg_stream; \
_ql_msg_stream << message; \
throw QuantLib::Error(__FILE__,__LINE__, \
BOOST_CURRENT_FUNCTION,_ql_msg_stream.str()); \
} else

} \
QL_MULTILINE_ASSERTION_END

/*! \def QL_ENSURE
\brief throw an error if the given post-condition is not verified
*/
#define QL_ENSURE(condition,message) \
QL_MULTILINE_ASSERTION_BEGIN \
if (!(condition)) { \
std::ostringstream _ql_msg_stream; \
_ql_msg_stream << message; \
throw QuantLib::Error(__FILE__,__LINE__, \
BOOST_CURRENT_FUNCTION,_ql_msg_stream.str()); \
} else
} \
QL_MULTILINE_ASSERTION_END


#endif
Expand Down
4 changes: 2 additions & 2 deletions ql/experimental/credit/onefactoraffinesurvival.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ namespace QuantLib {
bool extrapolate = false) const
{
#if defined(QL_EXTRA_SAFETY_CHECKS)
QL_REQUIRE(tgt >= tFwd, "Incorrect dates ordering.")
QL_REQUIRE(tgt >= tFwd, "Incorrect dates ordering.");
#endif
checkRange(tFwd, extrapolate);
checkRange(tgt, extrapolate);
Expand All @@ -137,7 +137,7 @@ namespace QuantLib {
virtual Probability conditionalSurvivalProbabilityImpl(Time tFwd, Time tgt, Real yVal) const;

// HazardRateStructure interface
Real hazardRateImpl(Time t) const override {
Real hazardRateImpl(Time) const override {
// no deterministic component
return 0.;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ namespace QuantLib {
VarianceGammaEngine::VarianceGammaEngine(ext::shared_ptr<VarianceGammaProcess> process,
Real absoluteError)
: process_(std::move(process)), absErr_(absoluteError) {
QL_REQUIRE(absErr_ > 0, "absolute error must be positive")
QL_REQUIRE(absErr_ > 0, "absolute error must be positive");
registerWith(process_);
}

Expand Down
2 changes: 1 addition & 1 deletion ql/instruments/vanillaswingoption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ namespace QuantLib {
QL_REQUIRE(exercise, "no exercise given");

QL_REQUIRE(minExerciseRights <= maxExerciseRights,
"minExerciseRights <= maxExerciseRights")
"minExerciseRights <= maxExerciseRights");
QL_REQUIRE(exercise->dates().size() >= maxExerciseRights,
"number of exercise rights exceeds "
"number of exercise dates");
Expand Down
2 changes: 1 addition & 1 deletion ql/math/optimization/constraint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ namespace QuantLib {
}
bool test(const Array& params) const override {
QL_ENSURE(params.size()==low_.size(),
"Number of parameters and boundaries sizes are inconsistent.")
"Number of parameters and boundaries sizes are inconsistent.");
for (Size i = 0; i < params.size(); i++) {
if ((params[i] < low_[i]) || (params[i] > high_[i]))
return false;
Expand Down
2 changes: 1 addition & 1 deletion ql/math/randomnumbers/randomizedlds.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ namespace QuantLib {
QL_REQUIRE(prsg_.dimension()==dimension_,
"generator mismatch: "
<< dimension_ << "-dim low discrepancy "
<< "and " << prsg_.dimension() << "-dim pseudo random")
<< "and " << prsg_.dimension() << "-dim pseudo random");

randomizer_ = prsg_.nextSequence();
}
Expand Down
2 changes: 1 addition & 1 deletion ql/methods/finitedifferences/operators/fdmbatesop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ namespace QuantLib {
= ext::dynamic_pointer_cast<FdmDirichletBoundary>(*iter);

QL_REQUIRE(dirichlet, "FdmBatesOp can only deal with Dirichlet "
"boundary conditions.")
"boundary conditions.");

valueOfDerivative
= dirichlet->applyAfterApplying(x, valueOfDerivative);
Expand Down
4 changes: 2 additions & 2 deletions ql/models/marketmodels/marketmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace QuantLib {
}
QL_REQUIRE(i<covariance_.size(),
"i (" << i <<
") must be less than covariance_.size() (" << covariance_.size() << ")")
") must be less than covariance_.size() (" << covariance_.size() << ")");
return covariance_[i];
}

Expand All @@ -48,7 +48,7 @@ namespace QuantLib {
}
QL_REQUIRE(endIndex<covariance_.size(),
"endIndex (" << endIndex <<
") must be less than covariance_.size() (" << covariance_.size() << ")")
") must be less than covariance_.size() (" << covariance_.size() << ")");
return totalCovariance_[endIndex];
}

Expand Down
2 changes: 1 addition & 1 deletion ql/termstructures/iterativebootstrap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ namespace detail {
void IterativeBootstrap<Curve>::setup(Curve* ts) {
ts_ = ts;
n_ = ts_->instruments_.size();
QL_REQUIRE(n_ > 0, "no bootstrap helpers given")
QL_REQUIRE(n_ > 0, "no bootstrap helpers given");
for (Size j=0; j<n_; ++j)
ts_->registerWith(ts_->instruments_[j]);

Expand Down
2 changes: 1 addition & 1 deletion ql/termstructures/volatility/smilesectionutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ namespace QuantLib {
centralIndex++;

QL_REQUIRE(centralIndex < k_.size(),
"central index is at right boundary")
"central index is at right boundary");

leftIndex_ = centralIndex;
rightIndex_ = centralIndex;
Expand Down

0 comments on commit fe86fff

Please sign in to comment.