Skip to content

Commit 037b692

Browse files
committed
refactor error reporting
1 parent 195f8d2 commit 037b692

22 files changed

+403
-354
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
bin/
22
bin64/
33
build/
4+
testfile/
45
.idea/
56
.vs/
67
.vscode/

include/mrdox/Config.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#ifndef MRDOX_CONFIG_HPP
1313
#define MRDOX_CONFIG_HPP
1414

15-
#include <mrdox/Errors.hpp>
15+
#include <mrdox/Reporter.hpp>
1616
#include <clang/Tooling/ArgumentsAdjusters.h>
1717
#include <clang/Tooling/Execution.h>
1818
#include <llvm/ADT/Optional.h>

include/mrdox/Corpus.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
#include "Index.hpp"
1616
#include <mrdox/Config.hpp>
17-
#include <mrdox/Errors.hpp>
17+
#include <mrdox/Reporter.hpp>
1818
#include <clang/Tooling/Execution.h>
1919
#include <llvm/Support/Mutex.h>
2020

@@ -118,6 +118,7 @@ struct Corpus
118118
/** Build the intermediate representation of the code being documented.
119119
*/
120120
static
121+
[[nodiscard]]
121122
std::unique_ptr<Corpus>
122123
build(
123124
tooling::ToolExecutor& ex,

include/mrdox/Errors.hpp

Lines changed: 0 additions & 145 deletions
This file was deleted.

include/mrdox/Generator.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
#include <mrdox/Config.hpp>
1919
#include <mrdox/Corpus.hpp>
20-
#include <mrdox/Errors.hpp>
20+
#include <mrdox/Reporter.hpp>
2121
#include <llvm/ADT/StringRef.h>
2222

2323
namespace clang {

include/mrdox/Reporter.hpp

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
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_REPORTER_HPP
13+
#define MRDOX_REPORTER_HPP
14+
15+
#include <llvm/Support/Error.h>
16+
#include <llvm/Support/Mutex.h>
17+
#include <llvm/Support/raw_ostream.h>
18+
#include <cassert>
19+
#include <new>
20+
#include <source_location>
21+
#include <string>
22+
#include <string_view>
23+
#include <system_error>
24+
#include <type_traits>
25+
26+
namespace clang {
27+
namespace mrdox {
28+
29+
//------------------------------------------------
30+
//
31+
// Reporter
32+
//
33+
//------------------------------------------------
34+
35+
/** Used to check and report errors uniformly.
36+
*/
37+
struct Reporter
38+
{
39+
/** Return a suitable exit code.
40+
*/
41+
int
42+
getExitCode() const noexcept
43+
{
44+
if(! failed_)
45+
return EXIT_SUCCESS;
46+
return EXIT_FAILURE;
47+
}
48+
49+
/** Report the failure of an action.
50+
51+
@param action The operation which failed.
52+
@param e The object which holds the error.
53+
@param loc The location where the report is called.
54+
*/
55+
template<class E>
56+
void
57+
failed(
58+
llvm::StringRef action,
59+
E&& e,
60+
std::source_location const& loc =
61+
std::source_location::current())
62+
{
63+
errs(action, " failed: ", std::move(e), " at ", loc);
64+
}
65+
66+
/** Report a unit test failure.
67+
68+
@par Thread Safety
69+
May be called concurrently.
70+
*/
71+
void
72+
testFailed()
73+
{
74+
failed_ = true;
75+
}
76+
77+
/** Write a formatted string to outputs.
78+
79+
@par Thread Safety
80+
May be called concurrently.
81+
*/
82+
template<class... Args>
83+
void
84+
outs(Args&&... args) const
85+
{
86+
llvm::StringRef s = format(std::forward<Args>(args)...);
87+
std::lock_guard<llvm::sys::Mutex> lock(m_);
88+
llvm::errs() << s << '\n';
89+
}
90+
91+
/** Write a formatted string to errors.
92+
93+
@par Thread Safety
94+
May be called concurrently.
95+
*/
96+
template<class... Args>
97+
void
98+
errs(Args&&... args) const
99+
{
100+
llvm::StringRef s = format(std::forward<Args>(args)...);
101+
std::lock_guard<llvm::sys::Mutex> lock(m_);
102+
llvm::errs() << s << '\n';
103+
}
104+
105+
private:
106+
/** Return a string formatted from arguments.
107+
108+
@par Thread Safety
109+
May be called concurrently.
110+
*/
111+
template<class... Args>
112+
llvm::StringRef
113+
format(
114+
Args&&... args) const
115+
{
116+
static thread_local std::string temp;
117+
temp.clear();
118+
llvm::raw_string_ostream os(temp);
119+
format_impl(os, std::forward<Args>(args)...);
120+
return llvm::StringRef(temp.data(), temp.size());
121+
}
122+
123+
/** Format arguments into an output stream.
124+
*/
125+
template<class Arg0, class... Args>
126+
void
127+
format_impl(
128+
llvm::raw_string_ostream& os,
129+
Arg0&& arg0,
130+
Args&&... args) const
131+
{
132+
format_one(os, std::forward<Arg0>(arg0));
133+
if constexpr(sizeof...(args) > 0)
134+
format_impl(os, std::forward<Args>(args)...);
135+
}
136+
137+
template<class T>
138+
void
139+
format_one(
140+
llvm::raw_string_ostream& os,
141+
T const& t) const
142+
{
143+
os << t;
144+
}
145+
146+
void
147+
format_one(
148+
llvm::raw_string_ostream& os,
149+
std::error_code const& ec) const
150+
{
151+
os << ec.message();
152+
}
153+
154+
void
155+
format_one(
156+
llvm::raw_string_ostream& os,
157+
llvm::Error&& err) const
158+
{
159+
//err.operator bool(); // VFALCO?
160+
os << toString(std::move(err));
161+
}
162+
163+
template<class T>
164+
void
165+
format_one(
166+
llvm::raw_string_ostream& os,
167+
llvm::Expected<T>&& ex) const
168+
{
169+
format_one(os, ex.takeError());
170+
}
171+
172+
template<class T>
173+
void
174+
format_one(
175+
llvm::raw_string_ostream& os,
176+
llvm::ErrorOr<T>&& eor) const
177+
{
178+
format_one(os, eor.getError());
179+
}
180+
181+
void
182+
format_one(
183+
llvm::raw_string_ostream& os,
184+
std::source_location const& loc) const
185+
{
186+
os << makeString(loc);
187+
}
188+
189+
llvm::StringRef
190+
makeString(
191+
std::source_location const& loc) const;
192+
193+
private:
194+
llvm::sys::Mutex mutable m_;
195+
bool failed_ = false;
196+
};
197+
198+
} // mrdox
199+
} // clang
200+
201+
#endif

0 commit comments

Comments
 (0)