libqasm
library for handling cQASM files
cqasm-error.cpp
Go to the documentation of this file.
1 
5 #include "cqasm-error.hpp"
6 
7 namespace cqasm {
8 namespace error {
9 
15  std::string &&message,
16  const tree::Annotatable *node
17 ) : std::runtime_error("") {
18  this->message << message;
19  if (node) {
20  context(*node);
21  }
22 }
23 
30  if (!location) {
31  if (auto loc = node.get_annotation_ptr<annotations::SourceLocation>()) {
32  location = std::unique_ptr<annotations::SourceLocation>(new annotations::SourceLocation(*loc));
33  }
34  }
35 }
36 
40 const std::string &AnalysisError::get_message() const {
41  std::ostringstream ss;
42  ss << "Error";
43  if (location) {
44  ss << " at " << *location;
45  }
46  ss << ": " << message.str();
47  msg = ss.str();
48  return msg;
49 }
50 
54 const char *AnalysisError::what() const noexcept {
55  // NOTE: this does *not* return a dangling pointer, because get_message()
56  // returns a const reference to the private msg field, which is used to
57  // maintain ownership of the memory used for the string.
58  return get_message().c_str();
59 }
60 
61 } // namespace error
62 } // namespace cqasm
const std::string & get_message() const
Constructs the message string.
Definition: cqasm-error.cpp:40
std::ostringstream message
The stringstream used to construct the message.
Definition: cqasm-error.hpp:41
AnalysisError(std::string &&message="", const tree::Annotatable *node=nullptr)
Constructs a new error.
Definition: cqasm-error.cpp:14
Toplevel namespace with entry points for the new API.
STL namespace.
Source location annotation object, containing source file line numbers etc.
std::unique_ptr< annotations::SourceLocation > location
Attached location in the source file, if any.
Definition: cqasm-error.hpp:46
Contains custom exception objects used by libqasm.
const char * what() const noexcept override
Returns the message exception-style.
Definition: cqasm-error.cpp:54
void context(const tree::Annotatable &node)
Sets the context of this error to the SourceLocation annotation of the given node, if the error doesn&#39;t already have such a context.
Definition: cqasm-error.cpp:29
::tree::annotatable::Annotatable Annotatable
Definition: cqasm-tree.hpp:19
annotations::SourceLocation SourceLocation