From 668e4e98eb7dbf89b5d5734f3cd4bba6ebbb0c11 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Mon, 3 Jul 2023 07:19:25 -0700 Subject: [PATCH] feat: Diagnostics --- docs/modules/ROOT/pages/design-notes.adoc | 12 ++--- source/AST/ASTDiagnostics.hpp | 30 ----------- source/AST/ASTVisitor.cpp | 2 +- source/AST/ASTVisitor.hpp | 13 ++--- source/AST/Diagnostics.hpp | 64 +++++++++++++++++++++++ 5 files changed, 78 insertions(+), 43 deletions(-) delete mode 100644 source/AST/ASTDiagnostics.hpp create mode 100644 source/AST/Diagnostics.hpp diff --git a/docs/modules/ROOT/pages/design-notes.adoc b/docs/modules/ROOT/pages/design-notes.adoc index f1f59f2cb..d4e6e0f3a 100644 --- a/docs/modules/ROOT/pages/design-notes.adoc +++ b/docs/modules/ROOT/pages/design-notes.adoc @@ -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. diff --git a/source/AST/ASTDiagnostics.hpp b/source/AST/ASTDiagnostics.hpp deleted file mode 100644 index 2707eabe0..000000000 --- a/source/AST/ASTDiagnostics.hpp +++ /dev/null @@ -1,30 +0,0 @@ -// -// This is a derivative work. originally part of the LLVM Project. -// 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 (vinnie.falco@gmail.com) -// -// Official repository: https://github.com/cppalliance/mrdox -// - -#ifndef MRDOX_TOOL_AST_PARSEJAVADOC_HPP -#define MRDOX_TOOL_AST_PARSEJAVADOC_HPP - -#include - -namespace clang { -namespace mrdox { - -class ASTDiagnostics -{ -public: - void - warning(); -}; - -} // mrdox -} // clang - -#endif diff --git a/source/AST/ASTVisitor.cpp b/source/AST/ASTVisitor.cpp index c38031798..5233e8ee7 100644 --- a/source/AST/ASTVisitor.cpp +++ b/source/AST/ASTVisitor.cpp @@ -48,8 +48,8 @@ ASTVisitor( clang::CompilerInstance& compiler) noexcept : ex_(ex) , config_(config) - , IsFileInRootDir_(true) , compiler_(compiler) + , IsFileInRootDir_(true) { } diff --git a/source/AST/ASTVisitor.hpp b/source/AST/ASTVisitor.hpp index e27ae5126..568273581 100644 --- a/source/AST/ASTVisitor.hpp +++ b/source/AST/ASTVisitor.hpp @@ -13,6 +13,7 @@ #ifndef MRDOX_TOOL_AST_ASTVISITOR_HPP #define MRDOX_TOOL_AST_ASTVISITOR_HPP +#include "Diagnostics.hpp" #include "Tool/ConfigImpl.hpp" #include #include @@ -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_; @@ -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, diff --git a/source/AST/Diagnostics.hpp b/source/AST/Diagnostics.hpp new file mode 100644 index 000000000..14c2876ef --- /dev/null +++ b/source/AST/Diagnostics.hpp @@ -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 (vinnie.falco@gmail.com) +// +// Official repository: https://github.com/cppalliance/mrdox +// + +#ifndef MRDOX_TOOL_AST_DIAGNOSTICS_HPP +#define MRDOX_TOOL_AST_DIAGNOSTICS_HPP + +#include +#include +#include +#include + +namespace clang { +namespace mrdox { + +/** Diagnostic information accumulated during visitation. +*/ +class Diagnostics +{ + std::size_t errorCount_ = 0; + std::unordered_set 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