Просмотр исходного кода

Replace is_sorted with comparison (#1104)

* Replace is_sorted with comparison

* Switch to StringRefContainsPointer
Jon Meow 4 лет назад
Родитель
Сommit
c4c5cdc1f5

+ 6 - 1
common/string_helpers.cpp

@@ -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 - 0
common/string_helpers.h

@@ -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 - 0
toolchain/lexer/BUILD

@@ -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
toolchain/lexer/test_helpers.h

@@ -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
toolchain/lexer/tokenized_buffer.cpp

@@ -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();