ROSE  0.11.145.0
SourceLocation.h
1 #ifndef ROSE_SourceLocation_H
2 #define ROSE_SourceLocation_H
3 
4 #include <Rose/Location.h>
5 
6 #include <boost/filesystem.hpp>
7 #include <boost/serialization/access.hpp>
8 #include <boost/serialization/nvp.hpp>
9 #include <boost/shared_ptr.hpp>
10 #include <boost/unordered_set.hpp>
11 #include <Sawyer/Optional.h>
12 #include <ostream>
13 #include <string>
14 
15 namespace Rose {
16 
59 class SourceLocation: public Location {
60  using NamePtr = boost::shared_ptr<boost::filesystem::path>;
61  NamePtr fileName_; // null iff default constructed
62  size_t line_;
64 
65 
66  // Shared index for file names
67  struct NameHasher { size_t operator()(const NamePtr&) const; };
68  struct NameEquivalence { bool operator()(const NamePtr&, const NamePtr&) const; };
69  using FileNames = boost::unordered_set<NamePtr, NameHasher, NameEquivalence>;
70  static SAWYER_THREAD_TRAITS::Mutex classMutex_; // protects the following static data members
71  static FileNames fileNames_;
72 
73 private:
74 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
75  friend class boost::serialization::access;
76 
77  template<class S>
78  void serialize(S &s, const unsigned /*version*/) {
79  s & BOOST_SERIALIZATION_NVP(fileName_);
80  s & BOOST_SERIALIZATION_NVP(line_);
81  s & BOOST_SERIALIZATION_NVP(column_);
82  registerFileName(); // necessary for de-serialization; okay to also call for serialization
83  }
84 #endif
85 
86 public:
91  SourceLocation(): line_(0) {}
92 
97  SourceLocation(const boost::filesystem::path &fileName, size_t line, const Sawyer::Optional<size_t> &column = Sawyer::Nothing());
98 
105  static SourceLocation parse(const std::string&);
106 
110  virtual ~SourceLocation();
111 
118  virtual std::string toString() const override;
119 
123  virtual void print(std::ostream&) const override;
124 
130  virtual std::string printableName() const override;
131 
136  virtual bool isEqual(const Location&) const override;
137 
146  virtual bool operator<(const Location&) const override;
147  virtual bool operator<=(const Location&) const override;
148  virtual bool operator>(const Location&) const override;
149  virtual bool operator>=(const Location&) const override;
152  virtual bool isValid() const override;
153 
161  virtual const boost::filesystem::path& fileName() const;
162 
164  virtual size_t line() const {
165  return line_; // no lock necessary since *this is immutable
166  }
167 
169  virtual const Sawyer::Optional<size_t>& column() const {
170  return column_; // no lock necessary since *this is immutable
171  }
172 
173 private:
174  // Compare two source locations like operator<=> in C++17
175  int compare(const SourceLocation &other) const;
176 
177  // When reconstituting an object from a serialization, we need to make sure the file name is an element of the shared index.
178  // It doesn't hurt to call this on any object at any time.
179  void registerFileName();
180 };
181 
182 } // namespace
183 #endif
virtual bool isValid() const override
Test whether this object is valid.
virtual bool operator<(const Location &) const override
Ordered comparison.
Information about a source location.
virtual size_t line() const
Line number.
virtual bool operator<=(const Location &) const override
Ordered comparison.
virtual ~SourceLocation()
Destructor.
Main namespace for the ROSE library.
static SourceLocation parse(const std::string &)
Construct a new source location by parsing a string.
SourceLocation()
Default constructor.
virtual std::string toString() const override
Convert location to string.
Base class for location types.
Definition: Location.h:23
virtual std::string printableName() const override
Convert location to escaped string.
virtual bool operator>=(const Location &) const override
Ordered comparison.
virtual const boost::filesystem::path & fileName() const
File name associated with this location.
virtual const Sawyer::Optional< size_t > & column() const
Column number.
virtual void print(std::ostream &) const override
Output location to a stream.
Represents no value.
Definition: Optional.h:32
virtual bool operator>(const Location &) const override
Ordered comparison.
virtual bool isEqual(const Location &) const override
Equality and inequality.