Home | History | Annotate | Download | only in gn
      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 <algorithm>
      9 
     10 #include "base/logging.h"
     11 
     12 class InputFile;
     13 
     14 // Represents a place in a source file. Used for error reporting.
     15 class Location {
     16  public:
     17   Location()
     18       : file_(NULL),
     19         line_number_(-1),
     20         char_offset_(-1) {
     21   }
     22   Location(const InputFile* file, int line_number, int char_offset)
     23       : file_(file),
     24         line_number_(line_number),
     25         char_offset_(char_offset) {
     26   }
     27 
     28   const InputFile* file() const { return file_; }
     29   int line_number() const { return line_number_; }
     30   int char_offset() const { return char_offset_; }
     31 
     32   bool operator==(const Location& other) const {
     33     return other.file_ == file_ &&
     34            other.line_number_ == line_number_ &&
     35            other.char_offset_ == char_offset_;
     36   }
     37 
     38   bool operator<(const Location& other) const {
     39     DCHECK(file_ == other.file_);
     40     if (line_number_ != other.line_number_)
     41       return line_number_ < other.line_number_;
     42     return char_offset_ < other.char_offset_;
     43   }
     44 
     45  private:
     46   const InputFile* file_;  // Null when unset.
     47   int line_number_;  // -1 when unset.
     48   int char_offset_;  // -1 when unset.
     49 };
     50 
     51 // Represents a range in a source file. Used for error reporting.
     52 // The end is exclusive i.e. [begin, end)
     53 class LocationRange {
     54  public:
     55   LocationRange() {}
     56   LocationRange(const Location& begin, const Location& end)
     57       : begin_(begin),
     58         end_(end) {
     59     DCHECK(begin_.file() == end_.file());
     60   }
     61 
     62   const Location& begin() const { return begin_; }
     63   const Location& end() const { return end_; }
     64 
     65   LocationRange Union(const LocationRange& other) const {
     66     DCHECK(begin_.file() == other.begin_.file());
     67     return LocationRange(
     68         begin_ < other.begin_ ? begin_ : other.begin_,
     69         end_ < other.end_ ? other.end_ : end_);
     70   }
     71 
     72  private:
     73   Location begin_;
     74   Location end_;
     75 };
     76 
     77 #endif  // TOOLS_GN_LOCATION_H_
     78