Skip to content

Commit c93e9f1

Browse files
committed
factor out nice
1 parent 6ea84b2 commit c93e9f1

File tree

11 files changed

+314
-146
lines changed

11 files changed

+314
-146
lines changed

include/mrdox/Config.hpp

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,17 @@
1717
#include <llvm/ADT/Optional.h>
1818
#include <llvm/ADT/SmallVector.h>
1919
#include <llvm/ADT/StringRef.h>
20+
#include <llvm/Support/YAMLTraits.h>
2021
#include <memory>
2122
#include <string>
2223

24+
namespace llvm {
25+
namespace yaml {
26+
template<class T>
27+
struct MappingTraits;
28+
} // yaml
29+
} // llvm
30+
2331
namespace clang {
2432
namespace mrdox {
2533

@@ -28,8 +36,14 @@ namespace mrdox {
2836
This contains all the settings applied from
2937
the command line and the YML file (if any).
3038
*/
31-
struct Config
39+
class Config
3240
{
41+
template<class T>
42+
friend struct llvm::yaml::MappingTraits;
43+
44+
std::string sourceRoot_;
45+
46+
public:
3347
/** The root path from which all relative paths are calculated.
3448
*/
3549
llvm::SmallString<16> configPath;
@@ -47,12 +61,7 @@ struct Config
4761

4862
// Directory for outputting generated files.
4963
std::string OutDirectory;
50-
51-
// Directory where input files are stored. Links
52-
// to definition locations will only be generated if
53-
// the file is in this dir.
54-
std::vector<std::string> includePaths;
55-
64+
5665
// URL of repository that hosts code used
5766
// for links to definition locations.
5867
llvm::Optional<std::string> RepositoryUrl;
@@ -64,6 +73,18 @@ struct Config
6473
Config(Config&&) = delete;
6574
Config& operator=(Config&&) = delete;
6675

76+
//--------------------------------------------
77+
//
78+
// Observers
79+
//
80+
//--------------------------------------------
81+
82+
llvm::StringRef
83+
sourceRoot() const noexcept
84+
{
85+
return sourceRoot_;
86+
}
87+
6788
/** Returns true if the file should be skipped.
6889
6990
If the file is not skipped, then prefixPath
@@ -75,6 +96,22 @@ struct Config
7596
llvm::StringRef filePath,
7697
llvm::SmallVectorImpl<char>& prefixPath) const noexcept;
7798

99+
//--------------------------------------------
100+
//
101+
// Modifiers
102+
//
103+
//--------------------------------------------
104+
105+
/** Set the directory where the input files are stored.
106+
107+
Symbol documentation will not be emitted unless
108+
the corresponding source file is a child of this
109+
directory.
110+
*/
111+
llvm::Error
112+
setSourceRoot(
113+
llvm::StringRef dirPath);
114+
78115
public:
79116
struct filter { std::vector<std::string> include, exclude; };
80117

include/mrdox/Error.hpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#ifndef MRDOX_ERROR_HPP
1313
#define MRDOX_ERROR_HPP
1414

15+
#include <mrdox/detail/nice.hpp>
1516
#include <llvm/ADT/StringRef.h>
1617
#include <llvm/Support/Error.h>
1718
#include <source_location>
@@ -50,6 +51,32 @@ makeError(
5051
std::source_location loc =
5152
std::source_location::current());
5253

54+
template<class Arg0, class... Args>
55+
struct makeError_ : llvm::Error
56+
{
57+
makeError_(
58+
Arg0&& arg0,
59+
Args&&... args,
60+
std::source_location loc =
61+
std::source_location::current())
62+
: llvm::Error(
63+
[&]
64+
{
65+
using detail::nice;
66+
std::string temp;
67+
llvm::raw_string_ostream os(temp);
68+
os << nice(std::forward<Arg0>(arg0));
69+
(os << ... << nice(std::forward<Args>(args)));
70+
os << ' ' << nice(loc);
71+
return makeError(temp, loc);
72+
}())
73+
{
74+
}
75+
};
76+
77+
template<class Arg0, class... Args>
78+
makeError_(Arg0&&, Args&&...) -> makeError_<Arg0, Args...>;
79+
5380
//------------------------------------------------
5481

5582
/** Return an Error for the given text and source location.

include/mrdox/Reporter.hpp

Lines changed: 44 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#ifndef MRDOX_REPORTER_HPP
1313
#define MRDOX_REPORTER_HPP
1414

15+
#include <mrdox/detail/nice.hpp>
1516
#include <llvm/Support/Error.h>
1617
#include <llvm/Support/Format.h>
1718
#include <llvm/Support/Mutex.h>
@@ -90,6 +91,7 @@ struct Reporter
9091
9192
@param arg0, args The values to write.
9293
*/
94+
/** @{ */
9395
template<
9496
class E,
9597
class Arg0, class... Args>
@@ -100,6 +102,17 @@ struct Reporter
100102
Arg0&& arg0,
101103
Args&&... args);
102104

105+
template<
106+
class E,
107+
class Arg0, class... Args>
108+
[[nodiscard]]
109+
bool
110+
error(
111+
E& e,
112+
Arg0&& arg0,
113+
Args&&... args);
114+
/** @} */
115+
103116
//--------------------------------------------
104117

105118
/** Report a unit test failure.
@@ -113,69 +126,29 @@ struct Reporter
113126
failed_ = true;
114127
}
115128

116-
static
117-
llvm::StringRef
118-
makeString(
119-
std::source_location const& loc);
120-
121129
private:
122130
//--------------------------------------------
123131

124132
template<class T>
125-
static bool isFailure(llvm::Expected<T>& e) noexcept
133+
static bool isFailure(llvm::Expected<T>&& e) noexcept
126134
{
127135
return ! e.operator bool();
128136
}
129137

130138
template<class T>
131-
static bool isFailure(llvm::ErrorOr<T>& e) noexcept
139+
static bool isFailure(llvm::ErrorOr<T>&& e) noexcept
132140
{
133141
return ! e.operator bool();
134142
}
135143

136-
static bool isFailure(std::error_code const& ec) noexcept
137-
{
138-
return ec.operator bool();
139-
}
140-
141-
template<class T>
142-
auto&& nice(T&& t) const
143-
{
144-
return std::forward<T>(t);
145-
}
146-
147-
template<class T>
148-
auto nice(llvm::Expected<T>& e) const
149-
{
150-
return nice(e.takeError());
151-
}
152-
153-
template<class T>
154-
auto nice(llvm::Expected<T>&& e) const
144+
static bool isFailure(llvm::Error&& e) noexcept
155145
{
156-
return nice(e.takeError());
157-
}
158-
159-
template<class T>
160-
auto nice(llvm::ErrorOr<T>& e) const
161-
{
162-
return nice(e.takeError());
163-
}
164-
165-
template<class T>
166-
auto nice(llvm::ErrorOr<T>&& e) const
167-
{
168-
return nice(e.getError());
169-
}
170-
171-
auto nice(std::error_code&& ec) const
172-
{
173-
return ec.message();
146+
return ! e.operator bool();
174147
}
175148

176-
auto nice(std::error_code const& ec) const
149+
static bool isFailure(std::error_code&& ec) noexcept
177150
{
178-
return ec.message();
151+
return ec.operator bool();
179152
}
180153

181154
void
@@ -202,11 +175,12 @@ print(
202175
Arg0&& arg,
203176
Args&&... args)
204177
{
178+
using detail::nice;
205179
auto& temp = temp_string();
206180
temp.clear();
207181
{
208182
llvm::raw_string_ostream os(temp);
209-
os << std::forward<Arg0>(arg);
183+
os << nice(std::forward<Arg0>(arg));
210184
(os << ... << nice(std::forward<Args>(args)));
211185
}
212186
threadSafePrint(llvm::outs(), temp, nullptr);
@@ -220,12 +194,13 @@ failed(
220194
Arg0&& arg,
221195
Args&&... args)
222196
{
197+
using detail::nice;
223198
auto& temp = temp_string();
224199
temp.clear();
225200
{
226201
llvm::raw_string_ostream os(temp);
227202
os << "error: Couldn't ";
228-
os << std::forward<Arg0>(arg);
203+
os << nice(std::forward<Arg0>(arg));
229204
(os << ... << nice(std::forward<Args>(args)));
230205
os << ".";
231206
}
@@ -239,25 +214,41 @@ bool
239214
Reporter::
240215
error(
241216
E&& e,
242-
Arg0&& arg,
217+
Arg0&& arg0,
243218
Args&&... args)
244219
{
245-
if(! isFailure(e))
220+
using detail::nice;
221+
if(! isFailure(std::forward<E>(e)))
246222
return false;
247223
auto& temp = temp_string();
248224
temp.clear();
249225
{
250226
llvm::raw_string_ostream os(temp);
251227
os << "error: Couldn't ";
252-
os << std::forward<Arg0>(arg);
228+
os << nice(std::forward<Arg0>(arg0));
253229
(os << ... << nice(std::forward<Args>(args)));
254-
os << " because " <<
255-
nice(std::move(e)) << '.';
230+
os << " because " << nice(std::forward<E>(e)) << '.';
256231
}
257232
threadSafePrint(llvm::errs(), temp, &errorCount_);
258233
return true;
259234
}
260235

236+
template<
237+
class E,
238+
class Arg0, class... Args>
239+
bool
240+
Reporter::
241+
error(
242+
E& e,
243+
Arg0&& arg0,
244+
Args&&... args)
245+
{
246+
return error(
247+
std::move(e),
248+
std::forward<Arg0>(arg0),
249+
std::forward<Args>(args)...);
250+
}
251+
261252
} // mrdox
262253
} // clang
263254

include/mrdox/detail/nice.hpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//
2+
// This is a derivative work. originally part of the LLVM Project.
3+
// Licensed under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
// Copyright (c) 2023 Vinnie Falco ([email protected])
8+
//
9+
// Official repository: https://github.com/cppalliance/mrdox
10+
//
11+
12+
#ifndef MRDOX_DETAIL_FORMATTER_HPP
13+
#define MRDOX_DETAIL_FORMATTER_HPP
14+
15+
#include <llvm/Support/Error.h>
16+
#include <source_location>
17+
#include <system_error>
18+
19+
namespace clang {
20+
namespace mrdox {
21+
namespace detail {
22+
23+
template<class T>
24+
T& nice(T& t)
25+
{
26+
return t;
27+
}
28+
29+
template<class T>
30+
T&& nice(T&& t)
31+
{
32+
return std::forward<T>(t);
33+
}
34+
35+
template<class T>
36+
auto nice(llvm::Expected<T>&& e)
37+
{
38+
return nice(e.takeError());
39+
}
40+
41+
inline auto nice(std::error_code ec)
42+
{
43+
return ec.message();
44+
}
45+
46+
template<class T>
47+
auto nice(llvm::ErrorOr<T>&& e)
48+
{
49+
return nice(e.getError());
50+
}
51+
52+
llvm::StringRef nice(std::source_location loc);
53+
54+
} // detail
55+
} // mrdox
56+
} // clang
57+
58+
#endif

0 commit comments

Comments
 (0)