OpenASIP  2.0
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  */
39 void
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 
55 void
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 
67 void
69  UValue lineNumber, const std::string& message) {
70 
71  CompilerMessage msg;
72  msg.lineNumber = lineNumber;
73  msg.message = message;
74  msg.assemblerLine = codeLine(lineNumber);
75  codeErrors_.insert(msg);
76 }
77 
78 void
79 AssemblyParserDiagnostic::addError(const std::string& message) {
80  CompilerMessage msg;
81  msg.message = message;
82  otherErrors_.insert(msg);
83 }
84 
85 /**
86  * Clears all accumulated reports.
87  */
88 void
90  warnings_.clear();
91  codeErrors_.clear();
92  otherErrors_.clear();
93 }
94 
95 std::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 
AssemblyParserDiagnostic.hh
AssemblyParserDiagnostic::codeErrors_
std::set< CompilerMessage > codeErrors_
Error messages with line number.
Definition: AssemblyParserDiagnostic.hh:102
CompilerMessage::message
std::string message
Message.
Definition: AssemblyParserDiagnostic.hh:47
CompilerMessage::assemblerLine
std::string assemblerLine
Assembly code line number.
Definition: AssemblyParserDiagnostic.hh:48
assert
#define assert(condition)
Definition: Application.hh:86
CompilerMessage::lineNumber
UValue lineNumber
Message generation line number.
Definition: AssemblyParserDiagnostic.hh:49
AssemblyParserDiagnostic::otherErrors_
std::set< CompilerMessage > otherErrors_
For positionless and internal errors.
Definition: AssemblyParserDiagnostic.hh:105
UValue
unsigned long UValue
Definition: ParserStructs.hh:44
CompilerMessage
Definition: AssemblyParserDiagnostic.hh:46
AssemblyParserDiagnostic::clear
void clear()
Definition: AssemblyParserDiagnostic.cc:89
AssemblyParserDiagnostic::addError
void addError(UValue lineNumber, const std::string &message)
Definition: AssemblyParserDiagnostic.cc:68
AssemblyParserDiagnostic::codeLine
std::string codeLine(UValue lineNumber) const
Definition: AssemblyParserDiagnostic.cc:96
AssemblyParserDiagnostic::lineStarts_
std::vector< UValue > lineStarts_
New line start positions in assembly listing.
Definition: AssemblyParserDiagnostic.hh:111
AssemblyParserDiagnostic::warnings_
std::set< CompilerMessage > warnings_
Warning messages.
Definition: AssemblyParserDiagnostic.hh:99
AssemblyParserDiagnostic::addWarning
void addWarning(UValue lineNumber, const std::string &message)
Definition: AssemblyParserDiagnostic.cc:56
AssemblyParserDiagnostic::listing_
std::shared_ptr< const std::string > listing_
The current assembly listing.
Definition: AssemblyParserDiagnostic.hh:108
AssemblyParserDiagnostic::reset
void reset(std::shared_ptr< const std::string > assemblyText)
Definition: AssemblyParserDiagnostic.cc:40