/** * Copyright (c) 2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef ES2PANDA_UTIL_DIAGNOSTIC_ENGINE_H #define ES2PANDA_UTIL_DIAGNOSTIC_ENGINE_H #include #include #include "es2panda.h" #include "util/es2pandaMacros.h" #include "generated/diagnostic.h" #include "util/diagnostic.h" #include "lexer/token/sourceLocation.h" namespace ark::es2panda::util { class DiagnosticPrinter { public: DiagnosticPrinter() = default; NO_COPY_SEMANTIC(DiagnosticPrinter); NO_MOVE_SEMANTIC(DiagnosticPrinter); virtual ~DiagnosticPrinter() = default; virtual void Print(const DiagnosticBase &diagnostic) const = 0; virtual void Print(const DiagnosticBase &diagnostic, std::ostream &out) const = 0; }; class CLIDiagnosticPrinter : public DiagnosticPrinter { public: CLIDiagnosticPrinter() = default; NO_COPY_SEMANTIC(CLIDiagnosticPrinter); NO_MOVE_SEMANTIC(CLIDiagnosticPrinter); ~CLIDiagnosticPrinter() override = default; void Print(const DiagnosticBase &diagnostic) const override; void Print(const DiagnosticBase &diagnostic, std::ostream &out) const override; }; using DiagnosticStorage = std::vector>; class DiagnosticEngine { public: explicit DiagnosticEngine() : printer_(std::make_unique()) { g_diagnosticEngine = this; } NO_COPY_SEMANTIC(DiagnosticEngine); NO_MOVE_SEMANTIC(DiagnosticEngine); ~DiagnosticEngine() { g_diagnosticEngine = nullptr; } // NOTE(schernykh): should be removed const DiagnosticBase &GetAnyError() const; [[nodiscard]] bool IsAnyError() const noexcept; template Suggestion *CreateSuggestion(T &&...args) { return CreateDiagnostic(std::forward(args)...); } template void LogDiagnostic(T &&...args) { LogDiagnostic(std::forward(args)...); } // NOTE(schernykh): should be removed void Log([[maybe_unused]] const ThrowableDiagnostic &error) { printer_->Print(error); }; template void LogSyntaxError(T &&...args) { LogThrowableDiagnostic(DiagnosticType::SYNTAX, std::forward(args)...); } template void LogSemanticError(T &&...args) { LogThrowableDiagnostic(DiagnosticType::SEMANTIC, std::forward(args)...); } template void LogFatalError(T &&...args) { LogThrowableDiagnostic(DiagnosticType::FATAL, std::forward(args)...); } // NOTE(schernykh): should not be able from ETS template [[noreturn]] void ThrowSyntaxError(T &&...args) { ThrowDiagnostic(DiagnosticType::SYNTAX, std::forward(args)...); } template [[noreturn]] void ThrowSemanticError(T &&...args) { ThrowDiagnostic(DiagnosticType::SEMANTIC, std::forward(args)...); } template [[noreturn]] void ThrowFatalError(T &&...args) { ThrowDiagnostic(DiagnosticType::FATAL, std::forward(args)...); } void FlushDiagnostic(); std::string PrintAndFlushErrorDiagnostic(); void SetWError(bool wError) { wError_ = wError; } const DiagnosticStorage &GetDiagnosticStorage(DiagnosticType type); static void InitializeSignalHandlers(); private: template DIAGNOSTIC *CreateDiagnostic(T &&...args) { auto diag = std::make_unique(std::forward(args)...); auto type = diag->Type(); diagnostics_[type].push_back(std::move(diag)); return reinterpret_cast(diagnostics_[type].back().get()); } template void LogDiagnostic(T &&...args) { CreateDiagnostic(std::forward(args)...); } template void LogThrowableDiagnostic(T &&...args) { LogDiagnostic(std::forward(args)...); } template [[noreturn]] void ThrowDiagnostic(T &&...args) const { Throw(ThrowableDiagnostic {std::forward(args)...}); } [[noreturn]] void Throw(ThrowableDiagnostic diag) const; bool IsError(DiagnosticType type) const; DiagnosticStorage GetAllDiagnostic(); DiagnosticStorage GetErrorDiagnostic(); void WriteLog(const DiagnosticBase &error); private: std::array(DiagnosticType::COUNT)> diagnostics_; std::unique_ptr printer_; bool wError_ {false}; }; } // namespace ark::es2panda::util #endif // ES2PANDA_UTIL_DIAGNOSTIC_ENGINE_H