OpenASIP 2.2
Loading...
Searching...
No Matches
AssemblyParserDiagnostic.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2002-2017 Tampere University.
3 *
4 * This file is part of TTA-Based Codesign Environment (TCE).
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25/**
26 * @file AssemblyParserDiagnostic.hh
27 *
28 * Implementation of assembly parser diagnostics.
29 *
30 * @author Henry Linjamäki 2017 (henry.linjamaki-no.spam-tut.fi)
31 * @note rating: red
32 */
33
35
36/**
37 * Resets assembly text. Also clears all reports.
38 */
39void
41 std::shared_ptr<const std::string> assemblyText) {
42
43 clear();
44
45 listing_ = assemblyText;
46 lineStarts_.push_back(0);
47 for (size_t pos = 0; pos < listing_->size(); pos++) {
48 char c = listing_->at(pos);
49 if (c == '\n') {
50 lineStarts_.push_back(pos+1);
51 }
52 }
53}
54
55void
57 UValue lineNumber,
58 const std::string& message) {
59
60 CompilerMessage newWarning;
61 newWarning.lineNumber = lineNumber;
62 newWarning.message = message;
63 newWarning.assemblerLine = codeLine(lineNumber);
64 warnings_.insert(newWarning);
65}
66
67void
69 UValue lineNumber, const std::string& message) {
70
72 msg.lineNumber = lineNumber;
73 msg.message = message;
74 msg.assemblerLine = codeLine(lineNumber);
75 codeErrors_.insert(msg);
76}
77
78void
79AssemblyParserDiagnostic::addError(const std::string& message) {
81 msg.message = message;
82 otherErrors_.insert(msg);
83}
84
85/**
86 * Clears all accumulated reports.
87 */
88void
90 warnings_.clear();
91 codeErrors_.clear();
92 otherErrors_.clear();
93}
94
95std::string
97 std::string errorLine;
98
99 assert(listing_ && "reset() must be called before codeLine().");
100
101 unsigned int errorLineNum =
102 static_cast<unsigned int>(lineNumber - 1);
103
104 if (errorLineNum < lineStarts_.size()) {
105
106 std::string::size_type startPos =
107 lineStarts_[errorLineNum];
108
109 std::string::size_type endPos = listing_->find('\n', startPos);
110
111 // no line feed at end of file
112 if (endPos == std::string::npos) {
113 endPos = listing_->length();
114 }
115
116 errorLine = listing_->substr(startPos, endPos - startPos);
117
118 } else {
119 errorLine = "Invalid line number info, probably last line of file.";
120 }
121
122 return errorLine;
123}
124
#define assert(condition)
unsigned long UValue
std::string codeLine(UValue lineNumber) const
std::shared_ptr< const std::string > listing_
The current assembly listing.
std::set< CompilerMessage > otherErrors_
For positionless and internal errors.
std::vector< UValue > lineStarts_
New line start positions in assembly listing.
std::set< CompilerMessage > codeErrors_
Error messages with line number.
void reset(std::shared_ptr< const std::string > assemblyText)
void addWarning(UValue lineNumber, const std::string &message)
std::set< CompilerMessage > warnings_
Warning messages.
void addError(UValue lineNumber, const std::string &message)
std::string message
Message.
std::string assemblerLine
Assembly code line number.
UValue lineNumber
Message generation line number.