Skip to content

Commit 1d22799

Browse files
committed
chore: refactor Error
1 parent 31c99f4 commit 1d22799

File tree

15 files changed

+184
-113
lines changed

15 files changed

+184
-113
lines changed

include/mrdox/Support/Error.hpp

Lines changed: 97 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ namespace mrdox {
3030
class [[nodiscard]] MRDOX_DECL
3131
Error final : public std::exception
3232
{
33-
std::string text_;
33+
std::string message_;
34+
std::string reason_;
35+
std::source_location loc_;
3436

3537
static
3638
std::string
@@ -63,17 +65,22 @@ class [[nodiscard]] MRDOX_DECL
6365

6466
/** Constructor.
6567
66-
@param s The text of the error. This
67-
must not be empty.
68+
@param message The text of the error.
69+
This must not be empty.
70+
71+
@param loc The source location where
72+
the error occurred.
6873
*/
6974
explicit
7075
Error(
71-
std::string s,
76+
std::string reason,
7277
std::source_location loc =
7378
std::source_location::current())
74-
: text_(appendSourceLocation(std::move(s), loc))
79+
: message_(appendSourceLocation(
80+
std::string(reason), loc))
81+
, reason_(std::move(reason))
7582
{
76-
MRDOX_ASSERT(! text_.empty());
83+
MRDOX_ASSERT(! message_.empty());
7784
}
7885

7986
/** Constructor.
@@ -88,33 +95,9 @@ class [[nodiscard]] MRDOX_DECL
8895
{
8996
if(! ec)
9097
return;
91-
text_ = appendSourceLocation(ec.message(), loc);
92-
}
93-
94-
/** Constructor.
95-
96-
@param fs The format string. This
97-
must not be empty.
98-
99-
@param args Zero or more values to
100-
substitute into the format string.
101-
*/
102-
template<class... Args>
103-
explicit
104-
Error(
105-
FormatString<std::type_identity_t<Args>...> fs,
106-
Args&&... args)
107-
: Error(
108-
[&]
109-
{
110-
std::string s;
111-
fmt::vformat_to(
112-
std::back_inserter(s),
113-
fs.fs, fmt::make_format_args(
114-
std::forward<Args>(args)...));
115-
return s;
116-
}(), fs.loc)
117-
{
98+
message_ = appendSourceLocation(ec.message(), loc);
99+
reason_ = ec.message();
100+
loc_ = loc;
118101
}
119102

120103
explicit
@@ -124,7 +107,7 @@ class [[nodiscard]] MRDOX_DECL
124107
*/
125108
bool failed() const noexcept
126109
{
127-
return ! text_.empty();
110+
return ! message_.empty();
128111
}
129112

130113
/** Return true if this holds an error.
@@ -140,23 +123,39 @@ class [[nodiscard]] MRDOX_DECL
140123
std::string_view
141124
message() const noexcept
142125
{
143-
return text_;
126+
return message_;
127+
}
128+
129+
/** Return the reason string.
130+
*/
131+
std::string_view
132+
reason() const noexcept
133+
{
134+
return reason_;
135+
}
136+
137+
/** Return the source location.
138+
*/
139+
std::source_location
140+
location() const noexcept
141+
{
142+
return loc_;
144143
}
145144

146145
/** Return true if this equals other.
147146
*/
148147
bool
149148
operator==(Error const& other) const noexcept
150149
{
151-
return text_ == other.text_;
150+
return message_ == other.message_;
152151
}
153152

154153
/** Return a null-terminated error string.
155154
*/
156155
char const*
157156
what() const noexcept override
158157
{
159-
return text_.c_str();
158+
return reason_.c_str();
160159
}
161160

162161
/** Return a value indicating success.
@@ -174,9 +173,70 @@ success() noexcept
174173
return Error();
175174
}
176175

176+
/** Return a formatted error.
177+
178+
@param fs The format string. This
179+
must not be empty.
180+
181+
@param args Zero or more values to
182+
substitute into the format string.
183+
*/
184+
template<class... Args>
185+
Error
186+
formatError(
187+
FormatString<std::type_identity_t<Args>...> fs,
188+
Args&&... args)
189+
{
190+
std::string s;
191+
fmt::vformat_to(
192+
std::back_inserter(s),
193+
fs.fs, fmt::make_format_args(
194+
std::forward<Args>(args)...));
195+
return Error(std::move(s), fs.loc);
196+
}
197+
198+
//------------------------------------------------
199+
200+
/** A source location with tidying.
201+
*/
202+
class MRDOX_DECL
203+
SourceLocation
204+
{
205+
std::string_view file_;
206+
std::uint_least32_t line_;
207+
std::uint_least32_t col_;
208+
std::string_view func_;
209+
210+
public:
211+
SourceLocation(
212+
std::source_location const& loc) noexcept;
213+
214+
std::string_view file_name() const noexcept
215+
{
216+
return file_;
217+
}
218+
219+
std::uint_least32_t line() const noexcept
220+
{
221+
return line_;
222+
}
223+
224+
std::uint_least32_t column() const noexcept
225+
{
226+
return col_;
227+
}
228+
229+
std::string_view function_name() const noexcept
230+
{
231+
return func_;
232+
}
233+
};
234+
177235
} // mrdox
178236
} // clang
179237

238+
//------------------------------------------------
239+
180240
template<>
181241
struct std::hash<::clang::mrdox::Error>
182242
{

source/-XML/XMLWriter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ build()
149149
writeIndex();
150150

151151
if(! corpus_.traverse(*this, SymbolID::zero))
152-
return Error("visitation aborted");
152+
return formatError("visitation aborted");
153153

154154
if(options_.prolog)
155155
os_ << "</mrdox>\n";

source/-adoc/MultiPageVisitor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ renderPage(
5454
}
5555
catch(std::exception const& ex)
5656
{
57-
throw Error("std::ofstream(\"{}\") threw \"{}\"", fileName, ex.what());
57+
throw formatError("std::ofstream(\"{}\") threw \"{}\"", fileName, ex.what());
5858
}
5959
});
6060
}

source/AST/AnyBlock.hpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ struct BitcodeReader::AnyBlock
3434
unsigned ID,
3535
llvm::StringRef Blob)
3636
{
37-
return Error("unexpected record with ID={}", ID);
37+
return formatError("unexpected record with ID={}", ID);
3838
}
3939

4040
virtual
4141
Error
4242
readSubBlock(unsigned ID)
4343
{
44-
return Error("unexpected sub-block with ID={}", ID);
44+
return formatError("unexpected sub-block with ID={}", ID);
4545
}
4646
};
4747

@@ -63,7 +63,7 @@ class VersionBlock
6363
if(auto err = decodeRecord(R, V, Blob))
6464
return err;
6565
if(V != BitcodeVersion)
66-
return Error("wrong ID for Version");
66+
return formatError("wrong ID for Version");
6767
return Error::success();
6868
default:
6969
return AnyBlock::parseRecord(R, ID, Blob);
@@ -106,7 +106,7 @@ class JavadocNodesBlock
106106
return err;
107107
auto node = nodes.back().get();
108108
if(node->kind != doc::Kind::admonition)
109-
return Error("admonish on wrong kind");
109+
return formatError("admonish on wrong kind");
110110
static_cast<doc::Admonition*>(
111111
node)->style = admonish;
112112
return Error::success();
@@ -120,7 +120,7 @@ class JavadocNodesBlock
120120
return err;
121121
auto node = nodes.back().get();
122122
if(node->kind != doc::Kind::param)
123-
return Error("direction on wrong kind");
123+
return formatError("direction on wrong kind");
124124
auto param = static_cast<doc::Param*>(node);
125125
param->direction = direction;
126126
return Error::success();
@@ -135,7 +135,7 @@ class JavadocNodesBlock
135135
static_cast<doc::Link*>(node)->href = Blob.str();
136136
return Error::success();
137137
default:
138-
return Error("href on wrong kind");
138+
return formatError("href on wrong kind");
139139
}
140140
}
141141

@@ -154,7 +154,7 @@ class JavadocNodesBlock
154154
}
155155
else
156156
{
157-
return Error("unknown doc::Kind");
157+
return formatError("unknown doc::Kind");
158158
}
159159
});
160160
}
@@ -182,7 +182,7 @@ class JavadocNodesBlock
182182
node)->name = Blob.str();
183183
return Error::success();
184184
default:
185-
return Error("string on wrong kind");
185+
return formatError("string on wrong kind");
186186
}
187187
}
188188

@@ -194,7 +194,7 @@ class JavadocNodesBlock
194194
return err;
195195
auto node = nodes.back().get();
196196
if(node->kind != doc::Kind::styled)
197-
return Error("style on wrong kind");
197+
return formatError("style on wrong kind");
198198
static_cast<doc::Styled*>(
199199
node)->style = style;
200200
return Error::success();
@@ -217,7 +217,7 @@ class JavadocNodesBlock
217217
auto node = nodes.back().get();
218218
if(node->kind == doc::Kind::text ||
219219
node->kind == doc::Kind::styled)
220-
return Error("text node cannot have list");
220+
return formatError("text node cannot have list");
221221

222222
JavadocNodesBlock B(br_);
223223
if(auto err = br_.readBlock(B, ID))
@@ -537,7 +537,7 @@ class TemplateParamBlock
537537
I_.emplace<TemplateTParam>();
538538
break;
539539
default:
540-
return Error("invalid template parameter kind");
540+
return formatError("invalid template parameter kind");
541541
}
542542
return Error::success();
543543
}
@@ -552,7 +552,7 @@ class TemplateParamBlock
552552
return decodeRecord(R,
553553
I_.get<TemplateTParam>().Default.emplace(), Blob);
554554
default:
555-
return Error("invalid template parameter kind");
555+
return formatError("invalid template parameter kind");
556556
}
557557
}
558558
default:
@@ -569,7 +569,7 @@ class TemplateParamBlock
569569
case BI_TEMPLATE_PARAM_BLOCK_ID:
570570
{
571571
if(I_.Kind != TParamKind::Template)
572-
return Error("only TemplateTParam may have template parameters");
572+
return formatError("only TemplateTParam may have template parameters");
573573
TemplateParamBlock P(I_.get<TemplateTParam>().Params.emplace_back(), br_);
574574
return br_.readBlock(P, ID);
575575
}
@@ -585,7 +585,7 @@ class TemplateParamBlock
585585
t = &I_.get<NonTypeTParam>().Type;
586586
break;
587587
default:
588-
return Error("invalid TypeInfo block in TParam");
588+
return formatError("invalid TypeInfo block in TParam");
589589
}
590590
TypeBlock B(*t, br_);
591591
return br_.readBlock(B, ID);

source/AST/BitcodeReader.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ getInfos()
3535
if (!MaybeCode)
3636
return toError(MaybeCode.takeError());
3737
if (MaybeCode.get() != llvm::bitc::ENTER_SUBBLOCK)
38-
return Error("no blocks in input");
38+
return formatError("no blocks in input");
3939
llvm::Expected<unsigned> MaybeID = Stream.ReadSubBlockID();
4040
if (!MaybeID)
4141
return toError(MaybeID.takeError());
@@ -126,7 +126,7 @@ getInfos()
126126
case BI_JAVADOC_BLOCK_ID:
127127
case BI_JAVADOC_LIST_BLOCK_ID:
128128
case BI_JAVADOC_NODE_BLOCK_ID:
129-
return Error("invalid top level block");
129+
return formatError("invalid top level block");
130130
case llvm::bitc::BLOCKINFO_BLOCK_ID:
131131
if (auto err = readBlockInfoBlock())
132132
return std::move(err);
@@ -150,7 +150,7 @@ BitcodeReader::
150150
validateStream()
151151
{
152152
if (Stream.AtEndOfStream())
153-
return Error("premature end of stream");
153+
return formatError("premature end of stream");
154154

155155
// Sniff for the signature.
156156
for (int i = 0; i != 4; ++i)
@@ -159,7 +159,7 @@ validateStream()
159159
if (!MaybeRead)
160160
return toError(MaybeRead.takeError());
161161
else if (MaybeRead.get() != BitCodeConstants::Signature[i])
162-
return Error("invalid bitcode signature");
162+
return formatError("invalid bitcode signature");
163163
}
164164
return Error::success();
165165
}
@@ -174,7 +174,7 @@ readBlockInfoBlock()
174174
return toError(MaybeBlockInfo.takeError());
175175
BlockInfo = MaybeBlockInfo.get();
176176
if (!BlockInfo)
177-
return Error("unable to parse BlockInfoBlock");
177+
return formatError("unable to parse BlockInfoBlock");
178178
Stream.setBlockInfo(&*BlockInfo);
179179
return Error::success();
180180
}
@@ -210,7 +210,7 @@ readBlock(
210210
switch (Res)
211211
{
212212
case Cursor::BadBlock:
213-
return Error("bad block found");
213+
return formatError("bad block found");
214214
case Cursor::BlockEnd:
215215
blockStack_.pop_back();
216216
return Error::success();

0 commit comments

Comments
 (0)