Skip to content

Commit 668e4e9

Browse files
committed
feat: Diagnostics
1 parent d1b0340 commit 668e4e9

File tree

5 files changed

+78
-43
lines changed

5 files changed

+78
-43
lines changed

docs/modules/ROOT/pages/design-notes.adoc

+6-6
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ does not vary based on the platform.
4242

4343
== Exceptions
4444

45-
In functions which cannot return an error, such as work submitted to a thread
46-
pool or in a constructor, the implementation usually throws `Error`. These
47-
are caught and reported, and the process exits gracefully. If any exception
48-
is thrown which is not derived from `Error`, then it should not be caught.
49-
The uncaught exception handler should print a stack trace and exit the process
50-
immediately.
45+
Errors thrown by the program should always have type `Exception`. Objects
46+
of this type are capable of transporting an `Error` object. This is important
47+
for the scripting to work; exceptions are used to propagate errors from
48+
library code to scripts and back to the invoking code. For exceptional cases,
49+
these thrown exceptions should be uncaught. The tool installs an uncaught exception
50+
handler that prints a stack trace and exits the process immediately.

source/AST/ASTDiagnostics.hpp

-30
This file was deleted.

source/AST/ASTVisitor.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ ASTVisitor(
4848
clang::CompilerInstance& compiler) noexcept
4949
: ex_(ex)
5050
, config_(config)
51-
, IsFileInRootDir_(true)
5251
, compiler_(compiler)
52+
, IsFileInRootDir_(true)
5353
{
5454
}
5555

source/AST/ASTVisitor.hpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef MRDOX_TOOL_AST_ASTVISITOR_HPP
1414
#define MRDOX_TOOL_AST_ASTVISITOR_HPP
1515

16+
#include "Diagnostics.hpp"
1617
#include "Tool/ConfigImpl.hpp"
1718
#include <mrdox/MetadataFwd.hpp>
1819
#include <clang/Sema/SemaConsumer.h>
@@ -47,6 +48,12 @@ class ASTVisitor
4748

4849
tooling::ExecutionContext& ex_;
4950
ConfigImpl const& config_;
51+
clang::CompilerInstance& compiler_;
52+
Diagnostics diags_;
53+
54+
ASTContext* astContext_ = nullptr;
55+
SourceManager* sourceManager_ = nullptr;
56+
Sema* sema_ = nullptr;
5057

5158
llvm::SmallString<512> File_;
5259
bool IsFileInRootDir_;
@@ -57,12 +64,6 @@ class ASTVisitor
5764
clang::SourceLocation::UIntTy,
5865
FileFilter> fileFilter_;
5966

60-
clang::CompilerInstance& compiler_;
61-
62-
ASTContext* astContext_ = nullptr;
63-
SourceManager* sourceManager_ = nullptr;
64-
Sema* sema_ = nullptr;
65-
6667
public:
6768
ASTVisitor(
6869
tooling::ExecutionContext& ex,

source/AST/Diagnostics.hpp

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//
2+
// Licensed under the Apache License v2.0 with LLVM Exceptions.
3+
// See https://llvm.org/LICENSE.txt for license information.
4+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
//
6+
// Copyright (c) 2023 Vinnie Falco ([email protected])
7+
//
8+
// Official repository: https://github.com/cppalliance/mrdox
9+
//
10+
11+
#ifndef MRDOX_TOOL_AST_DIAGNOSTICS_HPP
12+
#define MRDOX_TOOL_AST_DIAGNOSTICS_HPP
13+
14+
#include <mrdox/Support/Error.hpp>
15+
#include <llvm/Support/raw_ostream.h>
16+
#include <string>
17+
#include <unordered_set>
18+
19+
namespace clang {
20+
namespace mrdox {
21+
22+
/** Diagnostic information accumulated during visitation.
23+
*/
24+
class Diagnostics
25+
{
26+
std::size_t errorCount_ = 0;
27+
std::unordered_set<std::string> messages_;
28+
29+
public:
30+
void reportError(std::string s)
31+
{
32+
auto result =
33+
messages_.emplace(std::move(s));
34+
if(result.second)
35+
++errorCount_;
36+
}
37+
38+
void reportWarning(std::string s)
39+
{
40+
auto result =
41+
messages_.emplace(std::move(s));
42+
if(result.second)
43+
++errorCount_;
44+
}
45+
46+
void
47+
merge(
48+
Diagnostics&& other,
49+
llvm::raw_ostream* os = nullptr)
50+
{
51+
for(auto&& s : other.messages_)
52+
{
53+
auto result = messages_.emplace(std::move(s));
54+
if(os && result.second)
55+
*os << *result.first;
56+
}
57+
other.messages_.clear();
58+
}
59+
};
60+
61+
} // mrdox
62+
} // clang
63+
64+
#endif

0 commit comments

Comments
 (0)