mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-06 07:54:41 +01:00
Replace is_sorted with comparison (#1104)
* Replace is_sorted with comparison * Switch to StringRefContainsPointer
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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_
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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');
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user