ROSE 0.11.145.147
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_HAVE_BOOST_SERIALIZATION_LIB
14#include <boost/serialization/access.hpp>
15#include <boost/serialization/nvp.hpp>
16#include <boost/shared_ptr.hpp>
17#include <boost/unordered_set.hpp>
18#endif
19
20#ifdef ROSE_HAVE_CEREAL
21#include <cereal/access.hpp>
22#include <cereal/cereal.hpp>
23#endif
24
25namespace Rose {
26
69class SourceLocation: public Location {
70 using NamePtr = std::shared_ptr<boost::filesystem::path>;
71 NamePtr fileName_; // null iff default constructed
72 size_t line_;
74
75 // Shared index for file names
76 struct NameHasher { size_t operator()(const NamePtr&) const; };
77 struct NameEquivalence { bool operator()(const NamePtr&, const NamePtr&) const; };
78 using FileNames = boost::unordered_set<NamePtr, NameHasher, NameEquivalence>;
79 static SAWYER_THREAD_TRAITS::Mutex classMutex_; // protects the following static data members
80 static FileNames fileNames_;
81
82#ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
83private:
84 friend class boost::serialization::access;
85
86 template<class S>
87 void serialize(S &s, const unsigned /*version*/) {
88 s & BOOST_SERIALIZATION_NVP(fileName_);
89 s & BOOST_SERIALIZATION_NVP(line_);
90 s & BOOST_SERIALIZATION_NVP(column_);
91 registerFileName(); // necessary for de-serialization; okay to also call for serialization
92 }
93#endif
94
95#ifdef ROSE_HAVE_CEREAL
96private:
97 friend class cereal::access;
98
99 template<class Archive>
100 void CEREAL_SERIALIZE_FUNCTION_NAME(Archive &archive) {
101 archive(cereal::make_nvp("fileName", fileName_));
102 archive(cereal::make_nvp("line", line_));
103 archive(cereal::make_nvp("column", column_));
104 }
105#endif
106
107public:
112 SourceLocation(): line_(0) {}
113
118 SourceLocation(const boost::filesystem::path &fileName, size_t line, const Sawyer::Optional<size_t> &column = Sawyer::Nothing());
119
126 static SourceLocation parse(const std::string&);
127
132
139 virtual std::string toString() const override;
140
144 virtual void print(std::ostream&) const override;
145
151 virtual std::string printableName() const override;
152
157 virtual bool isEqual(const Location&) const override;
158
167 virtual bool operator<(const Location&) const override;
168 virtual bool operator<=(const Location&) const override;
169 virtual bool operator>(const Location&) const override;
170 virtual bool operator>=(const Location&) const override;
173 virtual bool isValid() const override;
174
182 virtual const boost::filesystem::path& fileName() const;
183
185 virtual size_t line() const {
186 return line_; // no lock necessary since *this is immutable
187 }
188
190 virtual const Sawyer::Optional<size_t>& column() const {
191 return column_; // no lock necessary since *this is immutable
192 }
193
194private:
195 // Compare two source locations like operator<=> in C++17
196 int compare(const SourceLocation &other) const;
197
198 // When reconstituting an object from a serialization, we need to make sure the file name is an element of the shared index.
199 // It doesn't hurt to call this on any object at any time.
200 void registerFileName();
201};
202
203} // namespace
204#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.