ROSE 0.11.145.250
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 <Sawyer/Optional.h>
8#include <Sawyer/Synchronization.h>
9#include <ostream>
10#include <string>
11#include <rose_serialize_path.h>
12
13#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
14#include <boost/serialization/access.hpp>
15#include <boost/serialization/nvp.hpp>
16#endif
17
18#include <boost/shared_ptr.hpp>
19#include <boost/unordered_set.hpp>
20
21#ifdef ROSE_HAVE_CEREAL
22#include <cereal/access.hpp>
23#include <cereal/cereal.hpp>
24#endif
25
26namespace Rose {
27
70class SourceLocation: public Location {
71 using NamePtr = std::shared_ptr<boost::filesystem::path>;
72 NamePtr fileName_; // null iff default constructed
73 size_t line_;
75
76 // Shared index for file names
77 struct NameHasher { size_t operator()(const NamePtr&) const; };
78 struct NameEquivalence { bool operator()(const NamePtr&, const NamePtr&) const; };
79 using FileNames = boost::unordered_set<NamePtr, NameHasher, NameEquivalence>;
80 static SAWYER_THREAD_TRAITS::Mutex classMutex_; // protects the following static data members
81 static FileNames fileNames_;
82
83#ifdef ROSE_ENABLE_BOOST_SERIALIZATION
84private:
85 friend class boost::serialization::access;
86
87 template<class S>
88 void serialize(S &s, const unsigned /*version*/) {
89 s & BOOST_SERIALIZATION_NVP(fileName_);
90 s & BOOST_SERIALIZATION_NVP(line_);
91 s & BOOST_SERIALIZATION_NVP(column_);
92 registerFileName(); // necessary for de-serialization; okay to also call for serialization
93 }
94#endif
95
96#ifdef ROSE_HAVE_CEREAL
97private:
98 friend class cereal::access;
99
100 template<class Archive>
101 void CEREAL_SERIALIZE_FUNCTION_NAME(Archive &archive) {
102 archive(cereal::make_nvp("fileName", fileName_));
103 archive(cereal::make_nvp("line", line_));
104 archive(cereal::make_nvp("column", column_));
105 }
106#endif
107
108public:
113 SourceLocation(): line_(0) {}
114
119 SourceLocation(const boost::filesystem::path &fileName, size_t line, const Sawyer::Optional<size_t> &column = Sawyer::Nothing());
120
127 static SourceLocation parse(const std::string&);
128
133
140 virtual std::string toString() const override;
141
145 virtual void print(std::ostream&) const override;
146
152 virtual std::string printableName() const override;
153
158 virtual bool isEqual(const Location&) const override;
159
168 virtual bool operator<(const Location&) const override;
169 virtual bool operator<=(const Location&) const override;
170 virtual bool operator>(const Location&) const override;
171 virtual bool operator>=(const Location&) const override;
174 virtual bool isValid() const override;
175
183 virtual const boost::filesystem::path& fileName() const;
184
186 virtual size_t line() const {
187 return line_; // no lock necessary since *this is immutable
188 }
189
191 virtual const Sawyer::Optional<size_t>& column() const {
192 return column_; // no lock necessary since *this is immutable
193 }
194
195private:
196 // Compare two source locations like operator<=> in C++17
197 int compare(const SourceLocation &other) const;
198
199 // When reconstituting an object from a serialization, we need to make sure the file name is an element of the shared index.
200 // It doesn't hurt to call this on any object at any time.
201 void registerFileName();
202};
203
204} // namespace
205#endif
Base class for location types.
Definition Location.h:23
Information about a source location.
virtual bool operator<(const Location &) const override
Ordered comparison.
virtual ~SourceLocation()
Destructor.
virtual bool isValid() const override
Test whether this object is valid.
SourceLocation()
Default constructor.
virtual size_t line() const
Line number.
virtual bool operator>(const Location &) const override
Ordered comparison.
virtual std::string printableName() const override
Convert location to escaped string.
virtual bool isEqual(const Location &) const override
Equality and inequality.
static SourceLocation parse(const std::string &)
Construct a new source location by parsing a string.
virtual bool operator>=(const Location &) const override
Ordered comparison.
virtual const boost::filesystem::path & fileName() const
File name associated with this location.
virtual std::string toString() const override
Convert location to string.
virtual void print(std::ostream &) const override
Output location to a stream.
SourceLocation(const boost::filesystem::path &fileName, size_t line, const Sawyer::Optional< size_t > &column=Sawyer::Nothing())
Construct a new source location.
virtual bool operator<=(const Location &) const override
Ordered comparison.
virtual const Sawyer::Optional< size_t > & column() const
Column number.
Represents no value.
Definition Optional.h:36
Holds a value or nothing.
Definition Optional.h:56
The ROSE library.