• 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, int byte);
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_; }
byte()21   int byte() const { return byte_; }
is_null()22   bool is_null() const { return *this == Location(); }
23 
24   bool operator==(const Location& other) const;
25   bool operator!=(const Location& other) const;
26   bool operator<(const Location& other) const;
27 
28   // Returns a string with the file, line, and (optionally) the character
29   // offset for this location. If this location is null, returns an empty
30   // string.
31   std::string Describe(bool include_column_number) const;
32 
33  private:
34   const InputFile* file_ = nullptr;  // Null when unset.
35   int line_number_ = -1;             // -1 when unset. 1-based.
36   int column_number_ = -1;           // -1 when unset. 1-based.
37   int byte_ = 0;                     // Index into the buffer, 0-based.
38 };
39 
40 // Represents a range in a source file. Used for error reporting.
41 // The end is exclusive i.e. [begin, end)
42 class LocationRange {
43  public:
44   LocationRange();
45   LocationRange(const Location& begin, const Location& end);
46 
begin()47   const Location& begin() const { return begin_; }
end()48   const Location& end() const { return end_; }
is_null()49   bool is_null() const {
50     return begin_.is_null();  // No need to check both for the null case.
51   }
52 
53   LocationRange Union(const LocationRange& other) const;
54 
55  private:
56   Location begin_;
57   Location end_;
58 };
59 
60 #endif  // TOOLS_GN_LOCATION_H_
61