1/*
2 * Copyright 2018 Google, Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * Authors: Gabe Black
28 */
29
30#include <cstring>
31
32#include "base/logging.hh"
33#include "systemc/ext/utils/messages.hh"
34#include "systemc/ext/utils/sc_report.hh"
35#include "systemc/ext/utils/sc_report_handler.hh"
36#include "systemc/utils/report.hh"
37
38namespace sc_core
39{
40
41sc_report::sc_report(sc_severity _severity, const char *msg_type,
42        const char *msg, int _verbosity, const char *_fileName,
43        int _lineNumber, sc_time _time, const char *_processName, int _id) :
44    _severity(_severity), _msgType(msg_type), _msg(msg),
45    _verbosity(_verbosity), _fileName(_fileName), _lineNumber(_lineNumber),
46    _time(_time), _processName(_processName), _id(_id)
47{
48    if (_msgType)
49        _msgType = strdup(_msgType);
50    if (_msg)
51        _msg = strdup(_msg);
52    _what = sc_report_compose_message(*this);
53}
54
55sc_report::sc_report(const sc_report &r) :
56    sc_report(r._severity, r._msgType, r._msg, r._verbosity, r._fileName,
57            r._lineNumber, r._time, r._processName, r._id)
58{}
59
60sc_report &
61sc_report::operator = (const sc_report &r)
62{
63    _severity = r._severity;
64    free((void *)_msgType);
65    _msgType = r._msgType ? strdup(r._msgType) : nullptr;
66    free((void *)_msg);
67    _msg = r._msg ? strdup(r._msg) : nullptr;
68    _verbosity = r._verbosity;
69    _fileName = r._fileName;
70    _lineNumber = r._lineNumber;
71    _time = r._time;
72    _processName = r._processName;
73    _id = r._id;
74    return *this;
75}
76
77sc_report::~sc_report() throw()
78{
79    free((void *)_msgType);
80    free((void *)_msg);
81}
82
83const char *
84sc_report::what() const throw()
85{
86    return _what.c_str();
87}
88
89const char *
90sc_report::get_message(int id)
91{
92    auto it = sc_gem5::reportIdToMsgMap().find(id);
93    if (it == sc_gem5::reportIdToMsgMap().end())
94        return "unknown id";
95    else
96        return it->second.c_str();
97}
98
99bool
100sc_report::is_suppressed(int id)
101{
102    auto it = sc_gem5::reportIdToMsgMap().find(id);
103    if (it == sc_gem5::reportIdToMsgMap().end())
104        return false;
105
106    auto &msgInfo = sc_gem5::reportMsgInfoMap()[it->second];
107
108    return (msgInfo.actions == SC_DO_NOTHING ||
109            (msgInfo.sevActions[SC_INFO] == SC_DO_NOTHING &&
110             msgInfo.sevActions[SC_WARNING] == SC_DO_NOTHING));
111}
112
113void
114sc_report::make_warnings_errors(bool val)
115{
116    sc_gem5::reportWarningsAsErrors = val;
117}
118
119void
120sc_report::register_id(int id, const char *msg)
121{
122    if (id < 0) {
123        SC_REPORT_ERROR(SC_ID_REGISTER_ID_FAILED_, "invalid report id");
124        return;
125    }
126    if (!msg) {
127        SC_REPORT_ERROR(SC_ID_REGISTER_ID_FAILED_, "invalid report message");
128        return;
129    }
130    auto p = sc_gem5::reportIdToMsgMap().insert(
131            std::pair<int, std::string>(id, msg));
132    if (!p.second) {
133        SC_REPORT_ERROR(SC_ID_REGISTER_ID_FAILED_, "report id already exists");
134    } else {
135        sc_gem5::reportMsgInfoMap()[msg].id = id;
136    }
137}
138
139void
140sc_report::suppress_id(int id, bool suppress)
141{
142    auto it = sc_gem5::reportIdToMsgMap().find(id);
143    if (it == sc_gem5::reportIdToMsgMap().end())
144        return;
145
146    if (suppress) {
147        sc_gem5::reportMsgInfoMap()[it->second].
148            sevActions[SC_INFO] = SC_DO_NOTHING;
149        sc_gem5::reportMsgInfoMap()[it->second].
150            sevActions[SC_WARNING] = SC_DO_NOTHING;
151    } else {
152        sc_gem5::reportMsgInfoMap()[it->second].
153            sevActions[SC_INFO] = SC_UNSPECIFIED;
154        sc_gem5::reportMsgInfoMap()[it->second].
155            sevActions[SC_WARNING] = SC_UNSPECIFIED;
156    }
157}
158
159void
160sc_report::suppress_infos(bool suppress)
161{
162    if (suppress)
163        sc_gem5::reportSevInfos[SC_INFO].actions = SC_DO_NOTHING;
164    else
165        sc_gem5::reportSevInfos[SC_INFO].actions = SC_DEFAULT_INFO_ACTIONS;
166}
167
168void
169sc_report::suppress_warnings(bool suppress)
170{
171    if (suppress) {
172        sc_gem5::reportSevInfos[SC_WARNING].actions = SC_DO_NOTHING;
173    } else {
174        sc_gem5::reportSevInfos[SC_WARNING].actions =
175            SC_DEFAULT_WARNING_ACTIONS;
176    }
177}
178
179void
180sc_abort()
181{
182    panic("simulation aborted");
183}
184
185} // namespace sc_core
186