• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef TOOLS_GN_LOCATION_H_
6 #define TOOLS_GN_LOCATION_H_
7 
8 #include <string>
9 
10 class InputFile;
11 
12 // Represents a place in a source file. Used for error reporting.
13 class Location {
14  public:
15   Location();
16   Location(const InputFile* file, int line_number, int column_number);
17 
file()18   const InputFile* file() const { return file_; }
line_number()19   int line_number() const { return line_number_; }
column_number()20   int column_number() const { return column_number_; }
is_null()21   bool is_null() const { return *this == Location(); }
22 
23   bool operator==(const Location& other) const;
24   bool operator!=(const Location& other) const;
25   bool operator<(const Location& other) const;
26   bool operator<=(const Location& other) const;
27   bool operator>(const Location& other) const { return !(*this <= other); }
28   bool operator>=(const Location& other) const { return !(*this < other); }
29 
30   // Returns a string with the file, line, and (optionally) the character
31   // offset for this location. If this location is null, returns an empty
32   // string.
33   std::string Describe(bool include_column_number) const;
34 
35  private:
36   const InputFile* file_ = nullptr;  // Null when unset.
37   int line_number_ = -1;             // -1 when unset. 1-based.
38   int column_number_ = -1;           // -1 when unset. 1-based.
39 };
40 
41 // Represents a range in a source file. Used for error reporting.
42 // The end is exclusive i.e. [begin, end)
43 class LocationRange {
44  public:
45   LocationRange();
46   LocationRange(const Location& begin, const Location& end);
47 
begin()48   const Location& begin() const { return begin_; }
end()49   const Location& end() const { return end_; }
is_null()50   bool is_null() const {
51     return begin_.is_null();  // No need to check both for the null case.
52   }
53 
54   LocationRange Union(const LocationRange& other) const;
55 
56  private:
57   Location begin_;
58   Location end_;
59 };
60 
61 #endif  // TOOLS_GN_LOCATION_H_
62