|
| 1 | +//===- ImportVerilogDebugStream.h - A debug stream wrapper for importverilog |
| 2 | +//---------===// |
| 3 | +// |
| 4 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | +// See https://llvm.org/LICENSE.txt for license information. |
| 6 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | + |
| 10 | +#ifndef IMPORT_VERILOG_DEBUG_H |
| 11 | +#define IMPORT_VERILOG_DEBUG_H |
| 12 | + |
| 13 | +#include "mlir/IR/Location.h" |
| 14 | +#include "slang/ast/Compilation.h" |
| 15 | +#include "llvm/ADT/SmallString.h" |
| 16 | +#include "llvm/Support/Debug.h" |
| 17 | +#include "llvm/Support/raw_ostream.h" |
| 18 | +#include <optional> |
| 19 | +#include <source_location> |
| 20 | +#include <utility> |
| 21 | + |
| 22 | +// If includer defined DEBUG_TYPE, use that; else fall back to our default. |
| 23 | +#ifndef IMPORTVERILOG_DEBUG_TYPE |
| 24 | + #ifdef DEBUG_TYPE |
| 25 | + #define IMPORTVERILOG_DEBUG_TYPE DEBUG_TYPE |
| 26 | + #else |
| 27 | + #define IMPORTVERILOG_DEBUG_TYPE "import-verilog" |
| 28 | + #endif |
| 29 | +#endif |
| 30 | + |
| 31 | +struct ImportVerilogDebugStream { |
| 32 | + std::optional<mlir::Location> srcLoc; |
| 33 | + std::optional<std::source_location> compLoc; |
| 34 | + llvm::SmallString<128> buffer; |
| 35 | + llvm::raw_svector_ostream os; |
| 36 | + |
| 37 | + ImportVerilogDebugStream(std::optional<mlir::Location> sl, |
| 38 | + std::optional<std::source_location> cl) |
| 39 | + : srcLoc(sl), compLoc(cl), os(buffer) {} |
| 40 | + |
| 41 | + inline ~ImportVerilogDebugStream() { |
| 42 | + DEBUG_WITH_TYPE(IMPORTVERILOG_DEBUG_TYPE, { |
| 43 | + if(compLoc) |
| 44 | + llvm::dbgs() << compLoc->file_name() << ":" << compLoc->line() << " (" |
| 45 | + << compLoc->function_name() << ") "; |
| 46 | + if (srcLoc) { |
| 47 | + srcLoc->print(llvm::dbgs()); |
| 48 | + llvm::dbgs() << ": "; |
| 49 | + } |
| 50 | + llvm::dbgs() << buffer << "\n"; |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + inline llvm::raw_ostream &stream() { return os; } |
| 55 | + inline const llvm::raw_ostream &stream() const { return os; } |
| 56 | +}; |
| 57 | + |
| 58 | +// lvalue wrapper |
| 59 | +template <typename T> |
| 60 | +inline ImportVerilogDebugStream &operator<<(ImportVerilogDebugStream &s, |
| 61 | + const T &v) { |
| 62 | + s.stream() << v; |
| 63 | + return s; |
| 64 | +} |
| 65 | + |
| 66 | +// rvalue/temporary wrapper |
| 67 | +template <typename T> |
| 68 | +inline ImportVerilogDebugStream &&operator<<(ImportVerilogDebugStream &&s, |
| 69 | + const T &v) { |
| 70 | + s.stream() << v; |
| 71 | + return std::move(s); |
| 72 | +} |
| 73 | + |
| 74 | +// support ostream manipulators that look like functions, e.g. write_hex, etc. |
| 75 | +inline ImportVerilogDebugStream & |
| 76 | +operator<<(ImportVerilogDebugStream &s, |
| 77 | + llvm::raw_ostream &(*manip)(llvm::raw_ostream &)) { |
| 78 | + manip(s.stream()); |
| 79 | + return s; |
| 80 | +} |
| 81 | +inline ImportVerilogDebugStream && |
| 82 | +operator<<(ImportVerilogDebugStream &&s, |
| 83 | + llvm::raw_ostream &(*manip)(llvm::raw_ostream &)) { |
| 84 | + manip(s.stream()); |
| 85 | + return std::move(s); |
| 86 | +} |
| 87 | + |
| 88 | +/// Helper function to set up a debug stream with reasonable defaults |
| 89 | +ImportVerilogDebugStream dbgs( |
| 90 | + std::optional<mlir::Location> sourceLocation = {}, |
| 91 | + std::optional<std::source_location> cl = std::source_location::current()); |
| 92 | + |
| 93 | +#endif // IMPORT_VERILOG_DEBUG_H |
0 commit comments