ROSE  0.11.96.11
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, 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
Rose::SourceLocation::column
virtual const Sawyer::Optional< size_t > & column() const
Column number.
Definition: SourceLocation.h:169
Sawyer::Optional< size_t >
Sawyer::Nothing
Represents no value.
Definition: Optional.h:32
Rose::SourceLocation::printableName
virtual std::string printableName() const override
Convert location to escaped string.
Rose::SourceLocation::~SourceLocation
virtual ~SourceLocation()
Destructor.
Rose::SourceLocation::parse
static SourceLocation parse(const std::string &)
Construct a new source location by parsing a string.
Rose::SourceLocation
Information about a source location.
Definition: SourceLocation.h:59
Rose::Location
Base class for location types.
Definition: Location.h:23
Rose::SourceLocation::fileName
virtual const boost::filesystem::path & fileName() const
File name associated with this location.
Rose::SourceLocation::operator<=
virtual bool operator<=(const Location &) const override
Ordered comparison.
Rose::SourceLocation::operator>
virtual bool operator>(const Location &) const override
Ordered comparison.
Rose::SourceLocation::SourceLocation
SourceLocation()
Default constructor.
Definition: SourceLocation.h:91
Rose::SourceLocation::line
virtual size_t line() const
Line number.
Definition: SourceLocation.h:164
Rose::SourceLocation::toString
virtual std::string toString() const override
Convert location to string.
Rose
Main namespace for the ROSE library.
Definition: BinaryTutorial.dox:3
Rose::SourceLocation::operator<
virtual bool operator<(const Location &) const override
Ordered comparison.
Rose::SourceLocation::isEqual
virtual bool isEqual(const Location &) const override
Equality and inequality.
Rose::SourceLocation::operator>=
virtual bool operator>=(const Location &) const override
Ordered comparison.
Rose::SourceLocation::isValid
virtual bool isValid() const override
Test whether this object is valid.
Rose::SourceLocation::print
virtual void print(std::ostream &) const override
Output location to a stream.