Skip to content

Commit 5e3e2ae

Browse files
committed
Support for building with exceptions disabled
1) A new macro YAML_CPP_NORETURN to annotate functions as not returning in dll.h 2) A new function YAML_throw<ExceptionType>(args...) in exception.h this function will throw an exception unless exceptions are disabled in the compiler, detected by checking the pre-defined macro __cpp_exceptions In this case the exception class will be instantiated, and the user-provided function YAML::handle_exception(const char*) will be called on the exception's what() method 3) if exceptions are disabled,and the library's user does not provide YAML::handle_exception, there will be a linker error 4) all other files have been changed automatedly by running the following sed commands sed -i "s/throw \([A-Za-z]*\)(\(.*\))/YAML_throw<\1>(\2)/g" # throw statements for non-templated exceptions sed -i "s/throw \(.*\)<\(.*\)>(/YAML_throw<\1<\2> >(/g" # throw statements for templated exceptions
1 parent 1b50109 commit 5e3e2ae

File tree

13 files changed

+87
-64
lines changed

13 files changed

+87
-64
lines changed

include/yaml-cpp/dll.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@
5050
# endif
5151
#endif
5252

53+
#ifndef YAML_CPP_NORETURN
54+
# ifdef _MSC_VER
55+
# define YAML_CPP_NORETURN __declspec(noreturn)
56+
# else
57+
# define YAML_CPP_NORETURN __attribute__ ((noreturn))
58+
# endif
59+
#endif
60+
5361
#ifndef YAML_CPP_DEPRECATED_EXPORT
5462
# define YAML_CPP_DEPRECATED_EXPORT YAML_CPP_API YAML_CPP_DEPRECATED
5563
#endif

include/yaml-cpp/exceptions.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@
1515
#include <string>
1616

1717
namespace YAML {
18+
19+
#ifdef __cpp_exceptions
20+
template<typename Ex, typename... Args>
21+
YAML_CPP_NORETURN void YAML_throw(Args&&... args) {
22+
throw Ex(std::forward<Args>(args)...);
23+
}
24+
#else
25+
YAML_CPP_NORETURN void handle_exception(const char* what);
26+
27+
template<typename Ex, typename... Args>
28+
YAML_CPP_NORETURN void YAML_throw(Args&&... args) {
29+
handle_exception(Ex(std::forward<Args>(args)...).what());
30+
}
31+
#endif
32+
1833
// error messages
1934
namespace ErrorMsg {
2035
const char* const YAML_DIRECTIVE_ARGS =

include/yaml-cpp/node/detail/impl.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ inline node* node_data::get(const Key& key,
128128
return pNode;
129129
return nullptr;
130130
case NodeType::Scalar:
131-
throw BadSubscript(m_mark, key);
131+
YAML_throw<BadSubscript>(m_mark, key);
132132
}
133133

134134
auto it = std::find_if(m_map.begin(), m_map.end(), [&](const kv_pair m) {
@@ -154,7 +154,7 @@ inline node& node_data::get(const Key& key, shared_memory_holder pMemory) {
154154
convert_to_map(pMemory);
155155
break;
156156
case NodeType::Scalar:
157-
throw BadSubscript(m_mark, key);
157+
YAML_throw<BadSubscript>(m_mark, key);
158158
}
159159

160160
auto it = std::find_if(m_map.begin(), m_map.end(), [&](const kv_pair m) {
@@ -213,7 +213,7 @@ inline void node_data::force_insert(const Key& key, const Value& value,
213213
convert_to_map(pMemory);
214214
break;
215215
case NodeType::Scalar:
216-
throw BadInsert();
216+
YAML_throw<BadInsert>();
217217
}
218218

219219
node& k = convert_to_node(key, pMemory);

include/yaml-cpp/node/impl.h

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ inline Node::~Node() = default;
5757

5858
inline void Node::EnsureNodeExists() const {
5959
if (!m_isValid)
60-
throw InvalidNode(m_invalidKey);
60+
YAML_throw<InvalidNode>(m_invalidKey);
6161
if (!m_pNode) {
6262
m_pMemory.reset(new detail::memory_holder);
6363
m_pNode = &m_pMemory->create_node();
@@ -74,14 +74,14 @@ inline bool Node::IsDefined() const {
7474

7575
inline Mark Node::Mark() const {
7676
if (!m_isValid) {
77-
throw InvalidNode(m_invalidKey);
77+
YAML_throw<InvalidNode>(m_invalidKey);
7878
}
7979
return m_pNode ? m_pNode->mark() : Mark::null_mark();
8080
}
8181

8282
inline NodeType::value Node::Type() const {
8383
if (!m_isValid)
84-
throw InvalidNode(m_invalidKey);
84+
YAML_throw<InvalidNode>(m_invalidKey);
8585
return m_pNode ? m_pNode->type() : NodeType::Null;
8686
}
8787

@@ -125,12 +125,12 @@ struct as_if<T, void> {
125125

126126
T operator()() const {
127127
if (!node.m_pNode)
128-
throw TypedBadConversion<T>(node.Mark());
128+
YAML_throw<TypedBadConversion<T> >(node.Mark());
129129

130130
T t;
131131
if (convert<T>::decode(node, t))
132132
return t;
133-
throw TypedBadConversion<T>(node.Mark());
133+
YAML_throw<TypedBadConversion<T> >(node.Mark());
134134
}
135135
};
136136

@@ -143,7 +143,7 @@ struct as_if<std::string, void> {
143143
if (node.Type() == NodeType::Null)
144144
return "null";
145145
if (node.Type() != NodeType::Scalar)
146-
throw TypedBadConversion<std::string>(node.Mark());
146+
YAML_throw<TypedBadConversion<std::string> >(node.Mark());
147147
return node.Scalar();
148148
}
149149
};
@@ -152,7 +152,7 @@ struct as_if<std::string, void> {
152152
template <typename T>
153153
inline T Node::as() const {
154154
if (!m_isValid)
155-
throw InvalidNode(m_invalidKey);
155+
YAML_throw<InvalidNode>(m_invalidKey);
156156
return as_if<T, void>(*this)();
157157
}
158158

@@ -165,13 +165,13 @@ inline T Node::as(const S& fallback) const {
165165

166166
inline const std::string& Node::Scalar() const {
167167
if (!m_isValid)
168-
throw InvalidNode(m_invalidKey);
168+
YAML_throw<InvalidNode>(m_invalidKey);
169169
return m_pNode ? m_pNode->scalar() : detail::node_data::empty_scalar();
170170
}
171171

172172
inline const std::string& Node::Tag() const {
173173
if (!m_isValid)
174-
throw InvalidNode(m_invalidKey);
174+
YAML_throw<InvalidNode>(m_invalidKey);
175175
return m_pNode ? m_pNode->tag() : detail::node_data::empty_scalar();
176176
}
177177

@@ -182,7 +182,7 @@ inline void Node::SetTag(const std::string& tag) {
182182

183183
inline EmitterStyle::value Node::Style() const {
184184
if (!m_isValid)
185-
throw InvalidNode(m_invalidKey);
185+
YAML_throw<InvalidNode>(m_invalidKey);
186186
return m_pNode ? m_pNode->style() : EmitterStyle::Default;
187187
}
188188

@@ -194,7 +194,7 @@ inline void Node::SetStyle(EmitterStyle::value style) {
194194
// assignment
195195
inline bool Node::is(const Node& rhs) const {
196196
if (!m_isValid || !rhs.m_isValid)
197-
throw InvalidNode(m_invalidKey);
197+
YAML_throw<InvalidNode>(m_invalidKey);
198198
if (!m_pNode || !rhs.m_pNode)
199199
return false;
200200
return m_pNode->is(*rhs.m_pNode);
@@ -215,15 +215,15 @@ inline Node& Node::operator=(const Node& rhs) {
215215

216216
inline void Node::reset(const YAML::Node& rhs) {
217217
if (!m_isValid || !rhs.m_isValid)
218-
throw InvalidNode(m_invalidKey);
218+
YAML_throw<InvalidNode>(m_invalidKey);
219219
m_pMemory = rhs.m_pMemory;
220220
m_pNode = rhs.m_pNode;
221221
}
222222

223223
template <typename T>
224224
inline void Node::Assign(const T& rhs) {
225225
if (!m_isValid)
226-
throw InvalidNode(m_invalidKey);
226+
YAML_throw<InvalidNode>(m_invalidKey);
227227
AssignData(convert<T>::encode(rhs));
228228
}
229229

@@ -253,7 +253,7 @@ inline void Node::AssignData(const Node& rhs) {
253253

254254
inline void Node::AssignNode(const Node& rhs) {
255255
if (!m_isValid)
256-
throw InvalidNode(m_invalidKey);
256+
YAML_throw<InvalidNode>(m_invalidKey);
257257
rhs.EnsureNodeExists();
258258

259259
if (!m_pNode) {
@@ -270,7 +270,7 @@ inline void Node::AssignNode(const Node& rhs) {
270270
// size/iterator
271271
inline std::size_t Node::size() const {
272272
if (!m_isValid)
273-
throw InvalidNode(m_invalidKey);
273+
YAML_throw<InvalidNode>(m_invalidKey);
274274
return m_pNode ? m_pNode->size() : 0;
275275
}
276276

@@ -303,7 +303,7 @@ inline iterator Node::end() {
303303
template <typename T>
304304
inline void Node::push_back(const T& rhs) {
305305
if (!m_isValid)
306-
throw InvalidNode(m_invalidKey);
306+
YAML_throw<InvalidNode>(m_invalidKey);
307307
push_back(Node(rhs));
308308
}
309309

src/exp.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ unsigned ParseHex(const std::string& str, const Mark& mark) {
2121
else if ('0' <= ch && ch <= '9')
2222
digit = ch - '0';
2323
else
24-
throw ParserException(mark, ErrorMsg::INVALID_HEX);
24+
YAML_throw<ParserException>(mark, ErrorMsg::INVALID_HEX);
2525

2626
value = (value << 4) + digit;
2727
}
@@ -48,7 +48,7 @@ std::string Escape(Stream& in, int codeLength) {
4848
if ((value >= 0xD800 && value <= 0xDFFF) || value > 0x10FFFF) {
4949
std::stringstream msg;
5050
msg << ErrorMsg::INVALID_UNICODE << value;
51-
throw ParserException(in.mark(), msg.str());
51+
YAML_throw<ParserException>(in.mark(), msg.str());
5252
}
5353

5454
// now break it up into chars
@@ -131,7 +131,7 @@ std::string Escape(Stream& in) {
131131
}
132132

133133
std::stringstream msg;
134-
throw ParserException(in.mark(), std::string(ErrorMsg::INVALID_ESCAPE) + ch);
134+
YAML_throw<ParserException>(in.mark(), std::string(ErrorMsg::INVALID_ESCAPE) + ch);
135135
}
136136
} // namespace Exp
137137
} // namespace YAML

src/node_data.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ void node_data::push_back(node& node,
184184
}
185185

186186
if (m_type != NodeType::Sequence)
187-
throw BadPushback();
187+
YAML_throw<BadPushback>();
188188

189189
m_sequence.push_back(&node);
190190
}
@@ -200,7 +200,7 @@ void node_data::insert(node& key, node& value,
200200
convert_to_map(pMemory);
201201
break;
202202
case NodeType::Scalar:
203-
throw BadSubscript(m_mark, key);
203+
YAML_throw<BadSubscript>(m_mark, key);
204204
}
205205

206206
insert_map_pair(key, value);
@@ -231,7 +231,7 @@ node& node_data::get(node& key, const shared_memory_holder& pMemory) {
231231
convert_to_map(pMemory);
232232
break;
233233
case NodeType::Scalar:
234-
throw BadSubscript(m_mark, key);
234+
YAML_throw<BadSubscript>(m_mark, key);
235235
}
236236

237237
for (const auto& it : m_map) {

src/parse.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Node Load(std::istream& input) {
3232
Node LoadFile(const std::string& filename) {
3333
std::ifstream fin(filename);
3434
if (!fin) {
35-
throw BadFile(filename);
35+
YAML_throw<BadFile>(filename);
3636
}
3737
return Load(fin);
3838
}
@@ -65,7 +65,7 @@ std::vector<Node> LoadAll(std::istream& input) {
6565
std::vector<Node> LoadAllFromFile(const std::string& filename) {
6666
std::ifstream fin(filename);
6767
if (!fin) {
68-
throw BadFile(filename);
68+
YAML_throw<BadFile>(filename);
6969
}
7070
return LoadAll(fin);
7171
}

src/parser.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ void Parser::HandleDirective(const Token& token) {
6969

7070
void Parser::HandleYamlDirective(const Token& token) {
7171
if (token.params.size() != 1) {
72-
throw ParserException(token.mark, ErrorMsg::YAML_DIRECTIVE_ARGS);
72+
YAML_throw<ParserException>(token.mark, ErrorMsg::YAML_DIRECTIVE_ARGS);
7373
}
7474

7575
if (!m_pDirectives->version.isDefault) {
76-
throw ParserException(token.mark, ErrorMsg::REPEATED_YAML_DIRECTIVE);
76+
YAML_throw<ParserException>(token.mark, ErrorMsg::REPEATED_YAML_DIRECTIVE);
7777
}
7878

7979
std::stringstream str(token.params[0]);
@@ -86,7 +86,7 @@ void Parser::HandleYamlDirective(const Token& token) {
8686
}
8787

8888
if (m_pDirectives->version.major > 1) {
89-
throw ParserException(token.mark, ErrorMsg::YAML_MAJOR_VERSION);
89+
YAML_throw<ParserException>(token.mark, ErrorMsg::YAML_MAJOR_VERSION);
9090
}
9191

9292
m_pDirectives->version.isDefault = false;
@@ -95,12 +95,12 @@ void Parser::HandleYamlDirective(const Token& token) {
9595

9696
void Parser::HandleTagDirective(const Token& token) {
9797
if (token.params.size() != 2)
98-
throw ParserException(token.mark, ErrorMsg::TAG_DIRECTIVE_ARGS);
98+
YAML_throw<ParserException>(token.mark, ErrorMsg::TAG_DIRECTIVE_ARGS);
9999

100100
const std::string& handle = token.params[0];
101101
const std::string& prefix = token.params[1];
102102
if (m_pDirectives->tags.find(handle) != m_pDirectives->tags.end()) {
103-
throw ParserException(token.mark, ErrorMsg::REPEATED_TAG_DIRECTIVE);
103+
YAML_throw<ParserException>(token.mark, ErrorMsg::REPEATED_TAG_DIRECTIVE);
104104
}
105105

106106
m_pDirectives->tags[handle] = prefix;

src/scanner.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ void Scanner::ScanNextToken() {
170170
}
171171

172172
// don't know what it is!
173-
throw ParserException(INPUT.mark(), ErrorMsg::UNKNOWN_TOKEN);
173+
YAML_throw<ParserException>(INPUT.mark(), ErrorMsg::UNKNOWN_TOKEN);
174174
}
175175

176176
void Scanner::ScanToNextToken() {
@@ -386,6 +386,6 @@ void Scanner::ThrowParserException(const std::string& msg) const {
386386
const Token& token = m_tokens.front();
387387
mark = token.mark;
388388
}
389-
throw ParserException(mark, msg);
389+
YAML_throw<ParserException>(mark, msg);
390390
}
391391
} // namespace YAML

src/scanscalar.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ std::string ScanScalar(Stream& INPUT, ScanScalarParams& params) {
4949
break;
5050
}
5151
if (params.onDocIndicator == THROW) {
52-
throw ParserException(INPUT.mark(), ErrorMsg::DOC_IN_SCALAR);
52+
YAML_throw<ParserException>(INPUT.mark(), ErrorMsg::DOC_IN_SCALAR);
5353
}
5454
}
5555

@@ -85,7 +85,7 @@ std::string ScanScalar(Stream& INPUT, ScanScalarParams& params) {
8585
// eof? if we're looking to eat something, then we throw
8686
if (!INPUT) {
8787
if (params.eatEnd) {
88-
throw ParserException(INPUT.mark(), ErrorMsg::EOF_IN_SCALAR);
88+
YAML_throw<ParserException>(INPUT.mark(), ErrorMsg::EOF_IN_SCALAR);
8989
}
9090
break;
9191
}
@@ -135,7 +135,7 @@ std::string ScanScalar(Stream& INPUT, ScanScalarParams& params) {
135135
// we check for tabs that masquerade as indentation
136136
if (INPUT.peek() == '\t' && INPUT.column() < params.indent &&
137137
params.onTabInIndentation == THROW) {
138-
throw ParserException(INPUT.mark(), ErrorMsg::TAB_IN_INDENTATION);
138+
YAML_throw<ParserException>(INPUT.mark(), ErrorMsg::TAB_IN_INDENTATION);
139139
}
140140

141141
if (!params.eatLeadingWhitespace) {

0 commit comments

Comments
 (0)