Skip to content

Commit 0bd88ee

Browse files
committed
Columns and Bytes parent type
1 parent 795bad3 commit 0bd88ee

File tree

1 file changed

+46
-66
lines changed

1 file changed

+46
-66
lines changed

clang/lib/Frontend/TextDiagnostic.cpp

Lines changed: 46 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -47,63 +47,40 @@ static constexpr raw_ostream::Colors CommentColor = raw_ostream::YELLOW;
4747
static constexpr raw_ostream::Colors LiteralColor = raw_ostream::GREEN;
4848
static constexpr raw_ostream::Colors KeywordColor = raw_ostream::BLUE;
4949

50-
struct Bytes {
50+
class ColumnsOrBytes {
51+
public:
5152
int V = 0;
52-
Bytes() = default;
53-
Bytes(int V) : V(V) {}
54-
Bytes next() const { return Bytes(V + 1); }
55-
Bytes prev() const { return Bytes(V - 1); }
53+
ColumnsOrBytes(int V) : V(V) {}
5654
bool isValid() const { return V != -1; }
57-
bool operator>(int V) { return this->V > V; }
58-
bool operator>(Bytes O) { return V > O.V; }
59-
bool operator<(int V) { return this->V < V; }
60-
bool operator<(Bytes O) { return V < O.V; }
61-
Bytes &operator++() {
62-
++V;
63-
return *this;
64-
}
65-
Bytes &operator+=(Bytes B) {
66-
V += B.V;
67-
return *this;
68-
}
69-
Bytes &operator--() {
70-
--V;
71-
return *this;
72-
}
73-
bool operator<=(Bytes B) { return V <= B.V; }
74-
Bytes operator-(int V) { return Bytes{this->V - V}; }
75-
bool operator!=(int V) { return this->V != V; }
76-
};
7755

78-
struct Columns {
79-
int V = 0;
80-
Columns() = default;
81-
Columns(int V) : V(V) {}
82-
bool isValid() const { return V != -1; }
83-
Columns operator+(Columns B) { return Columns{V + B.V}; }
84-
Columns &operator+=(Columns B) {
56+
bool operator>(ColumnsOrBytes O) { return V > O.V; }
57+
bool operator<(ColumnsOrBytes O) { return V < O.V; }
58+
bool operator<=(ColumnsOrBytes B) { return V <= B.V; }
59+
bool operator!=(ColumnsOrBytes C) { return C.V != V; }
60+
61+
ColumnsOrBytes &operator+=(ColumnsOrBytes B) {
8562
V += B.V;
8663
return *this;
8764
}
88-
Columns &operator-=(Columns B) {
65+
ColumnsOrBytes &operator-=(ColumnsOrBytes B) {
8966
V -= B.V;
9067
return *this;
9168
}
69+
};
70+
class Bytes final : public ColumnsOrBytes {
71+
public:
72+
Bytes(int V) : ColumnsOrBytes(V) {}
73+
Bytes next() const { return Bytes(V + 1); }
74+
Bytes prev() const { return Bytes(V - 1); }
75+
Bytes operator+(Bytes B) { return Bytes{V + B.V}; }
76+
};
77+
class Columns final : public ColumnsOrBytes {
78+
public:
79+
Columns(int V) : ColumnsOrBytes(V) {}
80+
Columns next() const { return Columns(V + 1); }
81+
Columns prev() const { return Columns(V - 1); }
9282
Columns operator-(Columns B) { return Columns{V - B.V}; }
93-
bool operator==(int V) { return this->V == V; }
94-
bool operator<=(Columns B) { return V <= B.V; }
95-
bool operator<(Columns B) { return V < B.V; }
96-
bool operator>(Columns B) { return V > B.V; }
97-
Columns &operator++() {
98-
++V;
99-
return *this;
100-
}
101-
Columns &operator--() {
102-
--V;
103-
return *this;
104-
}
105-
bool operator!=(int V) { return this->V != V; }
106-
bool operator!=(Columns C) { return C.V != V; }
83+
Columns operator+(Columns B) { return Columns{V + B.V}; }
10784
};
10885

10986
/// Add highlights to differences in template strings.
@@ -292,19 +269,19 @@ static void genColumnByteMapping(StringRef SourceLine, unsigned TabStop,
292269

293270
ColumnsOut.resize(SourceLine.size() + 1, -1);
294271

295-
int NumColumns = 0;
272+
Columns NumColumns = 0;
296273
size_t I = 0;
297274
while (I < SourceLine.size()) {
298-
ColumnsOut[I] = Columns(NumColumns);
299-
BytesOut.resize(NumColumns + 1, -1);
275+
ColumnsOut[I] = NumColumns;
276+
BytesOut.resize(NumColumns.V + 1, -1);
300277
BytesOut.back() = Bytes(I);
301278
auto [Str, Printable] =
302279
printableTextForNextCharacter(SourceLine, &I, TabStop);
303-
NumColumns += llvm::sys::locale::columnWidth(Str);
280+
NumColumns += Columns(llvm::sys::locale::columnWidth(Str));
304281
}
305282

306-
ColumnsOut.back() = Columns(NumColumns);
307-
BytesOut.resize(NumColumns + 1, -1);
283+
ColumnsOut.back() = NumColumns;
284+
BytesOut.resize(NumColumns.V + 1, -1);
308285
BytesOut.back() = Bytes(I);
309286
}
310287

@@ -351,14 +328,16 @@ struct SourceColumnMap {
351328
/// Map from a byte index to the next byte which starts a column.
352329
Bytes startOfNextColumn(Bytes N) const {
353330
assert(0 <= N.V && N.V < static_cast<int>(m_byteToColumn.size() - 1));
354-
while (byteToColumn(++N) == -1) {}
331+
while (!byteToColumn(N).isValid())
332+
N = N.next();
355333
return N;
356334
}
357335

358336
/// Map from a byte index to the previous byte which starts a column.
359337
Bytes startOfPreviousColumn(Bytes N) const {
360338
assert(0 < N.V && N.V < static_cast<int>(m_byteToColumn.size()));
361-
while (byteToColumn(--N) == -1) {}
339+
while (!byteToColumn(N).isValid())
340+
N = N.prev();
362341
return N;
363342
}
364343

@@ -395,11 +374,11 @@ static void selectInterestingSourceRegion(std::string &SourceLine,
395374
// Find the slice that we need to display the full caret line
396375
// correctly.
397376
Columns CaretStart = 0, CaretEnd = CaretLine.size();
398-
for (; CaretStart != CaretEnd; ++CaretStart)
377+
for (; CaretStart != CaretEnd; CaretStart = CaretStart.next())
399378
if (!isWhitespace(CaretLine[CaretStart.V]))
400379
break;
401380

402-
for (; CaretEnd != CaretStart; --CaretEnd)
381+
for (; CaretEnd != CaretStart; CaretEnd = CaretEnd.prev())
403382
if (!isWhitespace(CaretLine[CaretEnd.V - 1]))
404383
break;
405384

@@ -433,7 +412,7 @@ static void selectInterestingSourceRegion(std::string &SourceLine,
433412
// If it's not at a character's first column then advance it past the current
434413
// character.
435414
while (CaretEnd < map.columns() && !map.columnToByte(CaretEnd).isValid())
436-
++CaretEnd;
415+
CaretEnd = CaretEnd.next();
437416

438417
assert(
439418
(CaretStart > map.columns() || map.columnToByte(CaretStart).isValid()) &&
@@ -1038,8 +1017,8 @@ static void highlightRange(const LineRange &R, const SourceColumnMap &Map,
10381017

10391018
// Pick the last non-whitespace column.
10401019
Bytes EndByte = Bytes{std::min(R.EndByte.V, Map.bytes().V)};
1041-
while (EndByte != 0 && (Map.getSourceLine()[EndByte.V - 1] == ' ' ||
1042-
Map.getSourceLine()[EndByte.V - 1] == '\t'))
1020+
while (EndByte.V != 0 && (Map.getSourceLine()[EndByte.V - 1] == ' ' ||
1021+
Map.getSourceLine()[EndByte.V - 1] == '\t'))
10431022
EndByte = Map.startOfPreviousColumn(EndByte);
10441023

10451024
// If the start/end passed each other, then we are trying to highlight a
@@ -1086,7 +1065,8 @@ static std::string buildFixItInsertionLine(FileID FID, unsigned LineNo,
10861065
// "column" (printed width, platform-dependent) and what is a
10871066
// "byte offset" (SourceManager "column").
10881067
Bytes HintByteOffset =
1089-
Bytes(SM.getColumnNumber(HintLocInfo.first, HintLocInfo.second)) - 1;
1068+
Bytes(SM.getColumnNumber(HintLocInfo.first, HintLocInfo.second))
1069+
.prev();
10901070

10911071
// The hint must start inside the source or right at the end
10921072
assert(HintByteOffset < map.bytes().next());
@@ -1158,26 +1138,26 @@ prepareAndFilterRanges(const SmallVectorImpl<CharSourceRange> &Ranges,
11581138

11591139
Bytes StartByte = SM.getExpansionColumnNumber(Begin);
11601140
Bytes EndByte = SM.getExpansionColumnNumber(End);
1161-
assert(StartByte != 0 && "StartByte must be valid, 0 is invalid");
1162-
assert(EndByte != 0 && "EndByte must be valid, 0 is invalid");
1141+
assert(StartByte.V != 0 && "StartByte must be valid, 0 is invalid");
1142+
assert(EndByte.V != 0 && "EndByte must be valid, 0 is invalid");
11631143
if (R.isTokenRange())
11641144
EndByte += Bytes(Lexer::MeasureTokenLength(End, SM, LangOpts));
11651145

11661146
// Only a single line.
11671147
if (StartLineNo == EndLineNo) {
1168-
LineRanges.push_back({StartLineNo, StartByte - 1, EndByte - 1});
1148+
LineRanges.push_back({StartLineNo, StartByte.prev(), EndByte.prev()});
11691149
continue;
11701150
}
11711151

11721152
// Start line.
1173-
LineRanges.push_back({StartLineNo, StartByte - 1, ~0u});
1153+
LineRanges.push_back({StartLineNo, StartByte.prev(), ~0u});
11741154

11751155
// Middle lines.
11761156
for (unsigned S = StartLineNo + 1; S != EndLineNo; ++S)
11771157
LineRanges.push_back({S, 0, ~0u});
11781158

11791159
// End line.
1180-
LineRanges.push_back({EndLineNo, 0, EndByte - 1});
1160+
LineRanges.push_back({EndLineNo, 0, EndByte.prev()});
11811161
}
11821162

11831163
return LineRanges;

0 commit comments

Comments
 (0)