ROSE  0.11.87.0
Rose/SourceLocation.h
1 #ifndef ROSE_SourceLocation_H
2 #define ROSE_SourceLocation_H
3 
4 #include <boost/filesystem.hpp>
5 #include <boost/serialization/access.hpp>
6 #include <boost/serialization/nvp.hpp>
7 #include <boost/shared_ptr.hpp>
8 #include <boost/unordered_set.hpp>
9 #include <Sawyer/Optional.h>
10 
11 namespace Rose {
12 
56  typedef boost::shared_ptr<boost::filesystem::path> NamePtr;
57  NamePtr fileName_; // null iff default constructed
58  size_t line_;
60 
61 
62  // Shared index for file names
63  struct NameHasher { size_t operator()(const NamePtr&) const; };
64  struct NameEquivalence { bool operator()(const NamePtr&, const NamePtr&) const; };
65  typedef boost::unordered_set<NamePtr, NameHasher, NameEquivalence> FileNames;
66  static SAWYER_THREAD_TRAITS::Mutex classMutex_; // protects the following static data members
67  static FileNames fileNames_;
68 
69 private:
70 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
71  friend class boost::serialization::access;
72 
73  template<class S>
74  void serialize(S &s, unsigned version) {
75  s & BOOST_SERIALIZATION_NVP(fileName_);
76  s & BOOST_SERIALIZATION_NVP(line_);
77  s & BOOST_SERIALIZATION_NVP(column_);
78  registerFileName(); // necessary for de-serialization; okay to also call for serialization
79  }
80 #endif
81 
82 public:
87  SourceLocation(): line_(0) {}
88 
93  SourceLocation(const boost::filesystem::path &fileName, size_t line, const Sawyer::Optional<size_t> &column = Sawyer::Nothing());
94 
101  static SourceLocation parse(const std::string&);
102 
106  ~SourceLocation();
107 
113  bool isEmpty() const;
114 
122  const boost::filesystem::path& fileName() const;
123 
125  size_t line() const {
126  return line_; // no lock necessary since *this is immutable
127  }
128 
131  return column_; // no lock necessary since *this is immutable
132  }
133 
140  bool operator==(const SourceLocation &other) const;
141  bool operator!=(const SourceLocation &other) const {
142  return !(*this == other);
143  }
154  int compare(const SourceLocation &other) const;
155  bool operator<(const SourceLocation &other) const {
156  return compare(other) < 0;
157  }
158  bool operator<=(const SourceLocation &other) const {
159  return compare(other) <= 0;
160  }
161  bool operator>(const SourceLocation &other) const {
162  return compare(other) > 0;
163  }
164  bool operator>=(const SourceLocation &other) const {
165  return compare(other) >= 0;
166  }
175  std::string toString() const;
176 
182  std::string printableName() const;
183 
187  void print(std::ostream&) const;
188 
189  // The following trickery is to allow things like "if (x)" to work but without having an implicit
190  // conversion to bool which would cause no end of other problems. This is fixed in C++11.
191 private:
192  typedef void(SourceLocation::*unspecified_bool)() const;
193  void this_type_does_not_support_comparisons() const {}
194 public:
199  operator unspecified_bool() const {
200  return isEmpty() ? 0 : &SourceLocation::this_type_does_not_support_comparisons;;
201  }
202 
203 private:
204  // When reconstituting an object from a serialization, we need to make sure the file name is an element of the shared index.
205  // It doesn't hurt to call this on any object at any time.
206  void registerFileName();
207 };
208 
209 std::ostream& operator<<(std::ostream&, const SourceLocation&);
210 
211 } // namespace
212 
213 #endif
Information about a source location.
std::string printableName() const
Convert location to escaped string.
bool operator>=(const SourceLocation &other) const
Ordered comparison.
int compare(const SourceLocation &other) const
Ordered comparison.
Main namespace for the ROSE library.
static SourceLocation parse(const std::string &)
Construct a new source location by parsing a string.
SourceLocation()
Default constructor.
bool isEmpty() const
Test whether object is empty.
size_t line() const
Line number.
const boost::filesystem::path & fileName() const
File name associated with this location.
bool operator==(const SourceLocation &other) const
Equality and inequality.
const Sawyer::Optional< size_t > & column() const
Column number.
void print(std::ostream &) const
Output location to a stream.
bool operator<(const SourceLocation &other) const
Ordered comparison.
bool operator!=(const SourceLocation &other) const
Equality and inequality.
std::string toString() const
Convert location to string.
~SourceLocation()
Destructor.
bool operator>(const SourceLocation &other) const
Ordered comparison.
Represents no value.
Definition: Optional.h:32
bool operator<=(const SourceLocation &other) const
Ordered comparison.