source: src/Common/CodeLocation.hpp @ 478dade

Last change on this file since 478dade was 661e7b0, checked in by Andrew Beach <ajbeach@…>, 3 months ago

After a years (or at least half a year) the CodeLocation? optimization is merged in. Added Symbol (using Racket's name for interned strings), and used it for CodeLocation? file names. The optimizes for the high number of copies - both instances with the same value and copy operations - and consistently brings down runtime by a few percent.

  • Property mode set to 100644
File size: 2.1 KB
RevLine 
[21f0aa8]1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
[c92bdcc]7// CodeLocation.hpp --
[21f0aa8]8//
9// Author           : Andrew Beach
10// Created On       : Thr Aug 17 11:23:00 2017
[b128d3e]11// Last Modified By : Peter A. Buhr
12// Last Modified On : Mon Aug 28 12:46:01 2017
13// Update Count     : 2
[21f0aa8]14//
15
16#pragma once
17
[80ac42d]18#include <iostream>
[661e7b0]19#include "Symbol.hpp"
[21f0aa8]20
21struct CodeLocation {
[d48e529]22        int first_line = -1, first_column = -1, last_line = -1, last_column = -1;
[661e7b0]23        Symbol filename = "";
[21f0aa8]24
25        /// Create a new unset CodeLocation.
[d48e529]26        CodeLocation() = default;
[21f0aa8]27
28        /// Create a new CodeLocation with the given values.
29        CodeLocation( const char* filename, int lineno )
[d48e529]30                : first_line( lineno )
[21f0aa8]31                , filename(filename ? filename : "")
32        {}
33
34        CodeLocation( const CodeLocation& rhs ) = default;
[968f280]35        CodeLocation( CodeLocation&& rhs ) = default;
36        CodeLocation& operator=( const CodeLocation & ) = default;
37        CodeLocation& operator=( CodeLocation && ) = default;
[21f0aa8]38
39        bool isSet () const {
[d48e529]40                return -1 != first_line;
[21f0aa8]41        }
42
43        bool isUnset () const {
44                return !isSet();
45        }
46
[ddcedfe]47        bool startsBefore( CodeLocation const & other ) const {
[661e7b0]48                if( filename.str() < other.filename.str() ) return true;
49                if( filename.str() > other.filename.str() ) return false;
[ddcedfe]50
51                if( first_line < other.first_line ) return true;
52                if( first_line > other.first_line ) return false;
53
54                if( last_line < other.last_line ) return true;
55                return false;
56        }
57
58        bool followedBy( CodeLocation const & other, int seperation ) const {
[d48e529]59                return (first_line + seperation == other.first_line &&
[21f0aa8]60                        filename == other.filename);
61        }
62
[ddcedfe]63        bool operator==( CodeLocation const & other ) const {
[21f0aa8]64                return followedBy( other, 0 );
65        }
66
[ddcedfe]67        bool operator!=( CodeLocation const & other ) const {
[21f0aa8]68                return !(*this == other);
69        }
70};
71
[80ac42d]72inline std::ostream & operator<<( std::ostream & out, const CodeLocation & location ) {
73        // Column number ":1" allows IDEs to parse the error message and position the cursor in the source text.
[661e7b0]74        return location.isSet() ? out << location.filename.str() << ":" << location.first_line << ":1 " : out;
[21f0aa8]75}
Note: See TracBrowser for help on using the repository browser.