Replace is_sorted with comparison (#1104)

* Replace is_sorted with comparison

* Switch to StringRefContainsPointer
This commit is contained in:
Jon Meow
2022-03-03 10:21:38 -08:00
committed by GitHub
parent 0a8c0dc271
commit c4c5cdc1f5
5 changed files with 17 additions and 4 deletions
+6 -1
View File
@@ -13,7 +13,7 @@
namespace Carbon {
static constexpr llvm::StringRef TripleQuotes = "\"\"\"";
static constexpr llvm::StringRef TripleQuotes = R"(""")";
static constexpr llvm::StringRef HorizontalWhitespaceChars = " \t";
// Carbon only takes uppercase hex input.
@@ -169,4 +169,9 @@ auto ParseBlockStringLiteral(llvm::StringRef source)
return parsed;
}
auto StringRefContainsPointer(llvm::StringRef ref, const char* ptr) -> bool {
auto le = std::less_equal<const char*>();
return le(ref.begin(), ptr) && le(ptr, ref.end());
}
} // namespace Carbon
+5
View File
@@ -26,6 +26,11 @@ auto UnescapeStringLiteral(llvm::StringRef source, bool is_block_string = false)
auto ParseBlockStringLiteral(llvm::StringRef source)
-> llvm::Expected<std::string>;
// Returns true if the pointer is in the string ref (including equality with
// `ref.end()`). This should be used instead of `<=` comparisons for
// correctness.
auto StringRefContainsPointer(llvm::StringRef ref, const char* ptr) -> bool;
} // namespace Carbon
#endif // COMMON_STRING_HELPERS_H_
+2
View File
@@ -40,6 +40,7 @@ cc_library(
hdrs = ["test_helpers.h"],
deps = [
"//common:check",
"//common:string_helpers",
"//toolchain/diagnostics:diagnostic_emitter",
"@com_google_googletest//:gtest",
"@llvm-project//llvm:Support",
@@ -160,6 +161,7 @@ cc_library(
":token_kind",
"//common:check",
"//common:ostream",
"//common:string_helpers",
"//toolchain/diagnostics:diagnostic_emitter",
"//toolchain/source:source_buffer",
"@llvm-project//llvm:Support",
+2 -1
View File
@@ -11,6 +11,7 @@
#include <string>
#include "common/check.h"
#include "common/string_helpers.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/FormatVariadic.h"
#include "toolchain/diagnostics/diagnostic_emitter.h"
@@ -28,7 +29,7 @@ class SingleTokenDiagnosticTranslator
: token_(token) {}
auto GetLocation(const char* pos) -> Diagnostic::Location override {
CHECK(llvm::is_sorted(std::array{token_.begin(), pos, token_.end()}))
CHECK(StringRefContainsPointer(token_, pos))
<< "invalid diagnostic location";
llvm::StringRef prefix = token_.take_front(pos - token_.begin());
auto [before_last_newline, this_line] = prefix.rsplit('\n');
+2 -2
View File
@@ -11,6 +11,7 @@
#include <string>
#include "common/check.h"
#include "common/string_helpers.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSwitch.h"
@@ -901,8 +902,7 @@ auto TokenizedBuffer::TokenIterator::Print(llvm::raw_ostream& output) const
auto TokenizedBuffer::SourceBufferLocationTranslator::GetLocation(
const char* loc) -> Diagnostic::Location {
CHECK(llvm::is_sorted(std::array{buffer_->source_->Text().begin(), loc,
buffer_->source_->Text().end()}))
CHECK(StringRefContainsPointer(buffer_->source_->Text(), loc))
<< "location not within buffer";
int64_t offset = loc - buffer_->source_->Text().begin();