Skip to content

Commit

Permalink
feat: Diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
vinniefalco committed Jul 3, 2023
1 parent d1b0340 commit 668e4e9
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 43 deletions.
12 changes: 6 additions & 6 deletions docs/modules/ROOT/pages/design-notes.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ does not vary based on the platform.

== Exceptions

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

This file was deleted.

2 changes: 1 addition & 1 deletion source/AST/ASTVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ ASTVisitor(
clang::CompilerInstance& compiler) noexcept
: ex_(ex)
, config_(config)
, IsFileInRootDir_(true)
, compiler_(compiler)
, IsFileInRootDir_(true)
{
}

Expand Down
13 changes: 7 additions & 6 deletions source/AST/ASTVisitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#ifndef MRDOX_TOOL_AST_ASTVISITOR_HPP
#define MRDOX_TOOL_AST_ASTVISITOR_HPP

#include "Diagnostics.hpp"
#include "Tool/ConfigImpl.hpp"
#include <mrdox/MetadataFwd.hpp>
#include <clang/Sema/SemaConsumer.h>
Expand Down Expand Up @@ -47,6 +48,12 @@ class ASTVisitor

tooling::ExecutionContext& ex_;
ConfigImpl const& config_;
clang::CompilerInstance& compiler_;
Diagnostics diags_;

ASTContext* astContext_ = nullptr;
SourceManager* sourceManager_ = nullptr;
Sema* sema_ = nullptr;

llvm::SmallString<512> File_;
bool IsFileInRootDir_;
Expand All @@ -57,12 +64,6 @@ class ASTVisitor
clang::SourceLocation::UIntTy,
FileFilter> fileFilter_;

clang::CompilerInstance& compiler_;

ASTContext* astContext_ = nullptr;
SourceManager* sourceManager_ = nullptr;
Sema* sema_ = nullptr;

public:
ASTVisitor(
tooling::ExecutionContext& ex,
Expand Down
64 changes: 64 additions & 0 deletions source/AST/Diagnostics.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// Licensed under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// Copyright (c) 2023 Vinnie Falco ([email protected])
//
// Official repository: https://github.com/cppalliance/mrdox
//

#ifndef MRDOX_TOOL_AST_DIAGNOSTICS_HPP
#define MRDOX_TOOL_AST_DIAGNOSTICS_HPP

#include <mrdox/Support/Error.hpp>
#include <llvm/Support/raw_ostream.h>
#include <string>
#include <unordered_set>

namespace clang {
namespace mrdox {

/** Diagnostic information accumulated during visitation.
*/
class Diagnostics
{
std::size_t errorCount_ = 0;
std::unordered_set<std::string> messages_;

public:
void reportError(std::string s)
{
auto result =
messages_.emplace(std::move(s));
if(result.second)
++errorCount_;
}

void reportWarning(std::string s)
{
auto result =
messages_.emplace(std::move(s));
if(result.second)
++errorCount_;
}

void
merge(
Diagnostics&& other,
llvm::raw_ostream* os = nullptr)
{
for(auto&& s : other.messages_)
{
auto result = messages_.emplace(std::move(s));
if(os && result.second)
*os << *result.first;
}
other.messages_.clear();
}
};

} // mrdox
} // clang

#endif

0 comments on commit 668e4e9

Please sign in to comment.