skia2/include/sksl/SkSLPosition.h
Kevin Lubick 68d6ff9222 [includes] Reduce IWYU exports from SkTypes.h
SkTypes.h was found exporting <stddef.h> and <stdint.h>.
Thus if a Skia file includes SkTypes.h, it did not need
to include either of those files to use things like
size_t or uint8_t respectively. [1]

This also resulted in strange IWYU warnings like:
warning: size_t is defined in "include/core/SkTypes.h", which isn't directly #included.

Thus, this CL removes those additional exports, and as a result,
adds many more includes of <cstddef> and <cstdint>.

The second change this CL is to not use the default IWYU
mappings which are hard-coded into IWYU [2]. These defaults
are valid, but make suggestions for the C version of
headers and not the C++ ones (i.e. <stddef.h> over <cstddef>).

To make these recommendations align better with our preferred
style (the C++ ones), we use IWYU with --no_default_mappings
and then have to expand our existing mappings to better deal
with how IWYU would resolve some of these headers.

[1] https://codereview.chromium.org/1207893002/#msg49
[2] 4c0f396159/iwyu_include_picker.cc (L1221-L1234)

Change-Id: Iafebe190f8f3e86f252147b9f538c182bcc34af4
Bug: skia:13052
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/555516
Auto-Submit: Kevin Lubick <kjlubick@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
Reviewed-by: Herb Derby <herb@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
2022-07-06 14:52:55 +00:00

105 lines
2.6 KiB
C++

/*
* Copyright 2021 Google LLC.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SKSL_POSITION
#define SKSL_POSITION
#include "include/core/SkTypes.h"
#include <cstdint>
#include <string_view>
namespace SkSL {
class Position {
public:
Position()
: fStartOffset(-1)
, fLength(0) {}
static Position Range(int startOffset, int endOffset) {
SkASSERT(startOffset <= endOffset);
SkASSERT(startOffset <= 0xFFFFFF);
int length = endOffset - startOffset;
Position result;
result.fStartOffset = startOffset;
result.fLength = length <= 0xFF ? length : 0xFF;
return result;
}
bool valid() const {
return fStartOffset != -1;
}
int line(std::string_view source) const;
int startOffset() const {
SkASSERT(this->valid());
return fStartOffset;
}
int endOffset() const {
SkASSERT(this->valid());
return fStartOffset + fLength;
}
// Returns the position from this through, and including the entirety of, end.
Position rangeThrough(Position end) const {
if (fStartOffset == -1 || end.fStartOffset == -1) {
return *this;
}
SkASSERTF(this->startOffset() <= end.startOffset() && this->endOffset() <= end.endOffset(),
"Invalid range: (%d-%d) - (%d-%d)\n", this->startOffset(), this->endOffset(),
end.startOffset(), end.endOffset());
return Range(this->startOffset(), end.endOffset());
}
// Returns a position representing the character immediately after this position
Position after() const {
int endOffset = this->endOffset();
return Range(endOffset, endOffset + 1);
}
bool operator==(const Position& other) const {
return fStartOffset == other.fStartOffset && fLength == other.fLength;
}
bool operator!=(const Position& other) const {
return !(*this == other);
}
bool operator>(const Position& other) const {
return fStartOffset > other.fStartOffset;
}
bool operator>=(const Position& other) const {
return fStartOffset >= other.fStartOffset;
}
bool operator<(const Position& other) const {
return fStartOffset < other.fStartOffset;
}
bool operator<=(const Position& other) const {
return fStartOffset <= other.fStartOffset;
}
private:
int32_t fStartOffset : 24;
uint32_t fLength : 8;
};
struct ForLoopPositions {
Position initPosition = Position();
Position conditionPosition = Position();
Position nextPosition = Position();
};
} // namespace SkSL
#endif