GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/base/exception.h Lines: 32 34 94.1 %
Date: 2021-03-23 Branches: 16 36 44.4 %

Line Exec Source
1
/*********************                                                        */
2
/*! \file exception.h
3
 ** \verbatim
4
 ** Top contributors (to current version):
5
 **   Morgan Deters, Tim King, Mathias Preiner
6
 ** This file is part of the CVC4 project.
7
 ** Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
8
 ** in the top-level source directory and their institutional affiliations.
9
 ** All rights reserved.  See the file COPYING in the top-level source
10
 ** directory for licensing information.\endverbatim
11
 **
12
 ** \brief CVC4's exception base class and some associated utilities
13
 **
14
 ** CVC4's exception base class and some associated utilities.
15
 **/
16
17
#include "cvc4_public.h"
18
19
#ifndef CVC4__EXCEPTION_H
20
#define CVC4__EXCEPTION_H
21
22
#include <exception>
23
#include <iosfwd>
24
#include <string>
25
26
#include "cvc4_export.h"
27
28
namespace CVC4 {
29
30
class Exception : public std::exception
31
{
32
 protected:
33
  std::string d_msg;
34
35
 public:
36
  // Constructors
37
166
  Exception() : d_msg("Unknown exception") {}
38
3643
  Exception(const std::string& msg) : d_msg(msg) {}
39
27
  Exception(const char* msg) : d_msg(msg) {}
40
41
  // Destructor
42
3836
  virtual ~Exception() {}
43
44
  // NON-VIRTUAL METHOD for setting and printing the error message
45
168
  void setMessage(const std::string& msg) { d_msg = msg; }
46
501
  std::string getMessage() const { return d_msg; }
47
48
  // overridden from base class std::exception
49
3
  const char* what() const noexcept override { return d_msg.c_str(); }
50
51
  /**
52
   * Get this exception as a string.  Note that
53
   *   cout << ex.toString();
54
   * is subtly different from
55
   *   cout << ex;
56
   * which is equivalent to
57
   *   ex.toStream(cout);
58
   * That is because with the latter two, the output language (and
59
   * other preferences) for exprs on the stream is respected.  In
60
   * toString(), there is no stream, so the parameters are default
61
   * and you'll get exprs and types printed using the AST language.
62
   */
63
  std::string toString() const;
64
65
  /**
66
   * Printing: feel free to redefine toStream().  When overridden in
67
   * a derived class, it's recommended that this method print the
68
   * type of exception before the actual message.
69
   */
70
13
  virtual void toStream(std::ostream& os) const { os << d_msg; }
71
72
}; /* class Exception */
73
74
148
class CVC4_EXPORT IllegalArgumentException : public Exception
75
{
76
 protected:
77
  IllegalArgumentException() : Exception() {}
78
79
  void construct(const char* header, const char* extra,
80
                 const char* function, const char* tail);
81
82
  void construct(const char* header, const char* extra,
83
                 const char* function);
84
85
  static std::string format_extra(const char* condStr, const char* argDesc);
86
87
  static const char* s_header;
88
89
public:
90
91
122
  IllegalArgumentException(const char* condStr, const char* argDesc,
92
122
                           const char* function, const char* tail) :
93
122
    Exception() {
94
122
    construct(s_header, format_extra(condStr, argDesc).c_str(), function, tail);
95
122
  }
96
97
26
  IllegalArgumentException(const char* condStr, const char* argDesc,
98
26
                           const char* function) :
99
26
    Exception() {
100
26
    construct(s_header, format_extra(condStr, argDesc).c_str(), function);
101
26
  }
102
103
  /**
104
   * This is a convenience function for building usages that are variadic.
105
   *
106
   * Having IllegalArgumentException itself be variadic is problematic for
107
   * making sure calls to IllegalArgumentException clean up memory.
108
   */
109
  static std::string formatVariadic();
110
  static std::string formatVariadic(const char* format, ...);
111
}; /* class IllegalArgumentException */
112
113
inline std::ostream& operator<<(std::ostream& os, const Exception& e);
114
28
inline std::ostream& operator<<(std::ostream& os, const Exception& e)
115
{
116
28
  e.toStream(os);
117
28
  return os;
118
}
119
120
template <class T>
121
inline void CheckArgument(bool cond, const T& arg, const char* tail);
122
5036768
template <class T> inline void CheckArgument(bool cond, const T& arg CVC4_UNUSED,
123
                                             const char* tail CVC4_UNUSED) {
124
5036768
  if(__builtin_expect( ( !cond ), false )) { \
125
4
    throw ::CVC4::IllegalArgumentException("", "", tail); \
126
  } \
127
5036764
}
128
template <class T>
129
inline void CheckArgument(bool cond, const T& arg);
130
12509083
template <class T> inline void CheckArgument(bool cond, const T& arg CVC4_UNUSED) {
131
12509083
  if(__builtin_expect( ( !cond ), false )) { \
132
22
    throw ::CVC4::IllegalArgumentException("", "", ""); \
133
  } \
134
12509061
}
135
136
class CVC4_EXPORT LastExceptionBuffer
137
{
138
 public:
139
  LastExceptionBuffer();
140
  ~LastExceptionBuffer();
141
142
  void setContents(const char* string);
143
  const char* getContents() const { return d_contents; }
144
145
9113
  static LastExceptionBuffer* getCurrent() { return s_currentBuffer; }
146
7909
  static void setCurrent(LastExceptionBuffer* buffer) { s_currentBuffer = buffer; }
147
148
  static const char* currentContents() {
149
    return (getCurrent() == nullptr) ? nullptr : getCurrent()->getContents();
150
  }
151
152
private:
153
  /* Disallow copies */
154
  LastExceptionBuffer(const LastExceptionBuffer&) = delete;
155
  LastExceptionBuffer& operator=(const LastExceptionBuffer&) = delete;
156
157
  char* d_contents;
158
159
  static thread_local LastExceptionBuffer* s_currentBuffer;
160
}; /* class LastExceptionBuffer */
161
162
}/* CVC4 namespace */
163
164
#endif /* CVC4__EXCEPTION_H */