Skip to content

Commit

Permalink
Make it so that empty string views returned by APIs are nonetheless b…
Browse files Browse the repository at this point in the history
…acked by a data pointer (#502)
  • Loading branch information
Peter Goodman authored Jan 5, 2024
1 parent 158c3df commit 10434d5
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 31 deletions.
8 changes: 7 additions & 1 deletion lib/Compilation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
#include <iostream>

namespace mx {
namespace {

// A zero-sized string view that nontheless has a valid `.data()` pointer.
static const std::string_view kEmptyStringView("");

} // namespace

CompilationImpl::~CompilationImpl(void) noexcept {}

Expand All @@ -31,7 +37,7 @@ std::string_view CompilationImpl::SourceIR(void) const & noexcept {
return std::string_view(mlir.cStr(), size);
}
}
return {};
return kEmptyStringView;
}

// Return a pointer to the source IR object.
Expand Down
10 changes: 8 additions & 2 deletions lib/FileImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
#include <multiplier/Frontend/TokenKind.h>

namespace mx {
namespace {

// A zero-sized string view that nontheless has a valid `.data()` pointer.
static const std::string_view kEmptyStringView("");

} // namespace

FileImpl::~FileImpl(void) noexcept {}

Expand All @@ -29,7 +35,7 @@ std::string_view FileImpl::Data(void) const & noexcept {
return std::string_view(data.cStr(), size);
}
}
return {};
return kEmptyStringView;
}

// Return the number of tokens in the file.
Expand All @@ -55,7 +61,7 @@ std::string_view ReadFileTokensFromFile::NthTokenData(
auto eo = tor[token_index + 1u];
return std::string_view(&(file->reader.getData().cStr()[bo]), eo - bo);
} else {
return {};
return kEmptyStringView;
}
}

Expand Down
14 changes: 10 additions & 4 deletions lib/FragmentImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
#include "File.h"

namespace mx {
namespace {

// A zero-sized string view that nontheless has a valid `.data()` pointer.
static const std::string_view kEmptyStringView("");

} // namespace

FragmentImpl::FragmentImpl(EntityProviderPtr ep_,
kj::Array<capnp::word> data_,
Expand Down Expand Up @@ -87,7 +93,7 @@ std::string_view ReadMacroTokensFromFragment::NthTokenData(
EntityOffset ti) const {
if (ti >= fragment->num_tokens) {
assert(false);
return {};
return kEmptyStringView;
}

const auto &reader = fragment->reader;
Expand Down Expand Up @@ -307,13 +313,13 @@ std::string_view ReadParsedTokensFromFragment::NthTokenData(
EntityOffset to) const {
if (to >= fragment->num_parsed_tokens) {
assert(false);
return {};
return kEmptyStringView;
}

auto ti = fragment->reader.getParsedTokenOffsetToIndex()[to];
if (ti >= fragment->num_tokens) {
assert(false);
return {};
return kEmptyStringView;
}

return this->ReadMacroTokensFromFragment::NthTokenData(ti);
Expand Down Expand Up @@ -456,7 +462,7 @@ std::string_view FragmentImpl::Data(void) const & noexcept {
return std::string_view(toks.cStr(), size);
}
}
return {};
return kEmptyStringView;
}

// Return the token associated with a specific entity ID.
Expand Down
19 changes: 11 additions & 8 deletions lib/Re2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@

#include <cassert>

#ifndef MX_DISABLE_RE2

namespace mx {
namespace {

// A zero-sized string view that nontheless has a valid `.data()` pointer.
static const std::string_view kEmptyStringView("");

} // namespace

#ifndef MX_DISABLE_RE2

// NOTE(pag): `RE2::FindAndConsume` is a variadic function taking additional
// arguments per sub-match, hence the requirement for enclosing
Expand Down Expand Up @@ -72,7 +78,7 @@ std::string_view RegexQuery::pattern(void) const {
// NOTE(pag): Need to remove the wrapping `(` and `)`.
return std::string_view(impl->pattern).substr(1u, impl->pattern.size() - 2u);
} else {
return {};
return kEmptyStringView;
}
}

Expand All @@ -81,11 +87,8 @@ bool RegexQuery::is_valid(void) const {
return impl && impl->IsValid();
}

} // namespace mx
#else

namespace mx {

// NOTE(pag): `RE2::FindAndConsume` is a variadic function taking additional
// arguments per sub-match, hence the requirement for enclosing
// `pattern_` in a match group with `(` and `)`.
Expand All @@ -112,13 +115,13 @@ void RegexQuery::for_each_match(

// Returns the underlying pattern.
std::string_view RegexQuery::pattern(void) const {
return {};
return kEmptyStringView;
}

// Returns `true` if we successfully compiled this regular expression.
bool RegexQuery::is_valid(void) const {
return false;
}

} // namespace mx
#endif // MX_DISABLE_RE2
} // namespace mx
19 changes: 11 additions & 8 deletions lib/Re2Impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@
#include "Types.h"
#include "Util.h"

#ifndef MX_DISABLE_RE2

namespace mx {
namespace {

// A zero-sized string view that nontheless has a valid `.data()` pointer.
static const std::string_view kEmptyStringView("");

} // namespace

#ifndef MX_DISABLE_RE2

RegexQueryResultImpl::~RegexQueryResultImpl(void) noexcept {}

Expand Down Expand Up @@ -345,7 +351,7 @@ std::string_view RegexQueryMatch::data(void) const noexcept {
auto real_impl = dynamic_cast<const RegexQueryMatchImpl *>(impl.get());
if (!real_impl) {
assert(false);
return {};
return kEmptyStringView;
}

return real_impl->matched_ranges[0];
Expand Down Expand Up @@ -453,9 +459,7 @@ gap::generator<RegexQueryMatch> RegexQuery::match_fragments(
}
}

} // namespace mx
#else
namespace mx {

RegexQueryMatchImpl::~RegexQueryMatchImpl(void) {}

Expand Down Expand Up @@ -538,7 +542,7 @@ size_t RegexQueryMatch::num_captures(void) const {
// The actual range of matched data. This is possibly a sub-sequence of
// `this->TokenRange::data()`.
std::string_view RegexQueryMatch::data(void) const noexcept {
return {};
return kEmptyStringView;
}

// Return a list of matched variables.
Expand Down Expand Up @@ -567,6 +571,5 @@ RegexQuery::match_fragments(const Fragment &) const & {
co_return;
}

} // namespace mx
#endif // MX_DISABLE_RE2

} // namespace mx
18 changes: 12 additions & 6 deletions lib/Token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
#include "Type.h"

namespace mx {
namespace {

// A zero-sized string view that nontheless has a valid `.data()` pointer.
static const std::string_view kEmptyStringView("");

} // namespace

class TemplateDecl;

Expand Down Expand Up @@ -934,7 +940,7 @@ TokenKind InvalidTokenReader::NthTokenKind(EntityOffset) const {

// Return the data of the Nth token.
std::string_view InvalidTokenReader::NthTokenData(EntityOffset) const {
return {};
return kEmptyStringView;
}

// Return the id of the token from which the Nth token is derived.
Expand Down Expand Up @@ -1129,7 +1135,7 @@ TokenCategory CustomTokenReader::NthTokenCategory(EntityOffset to) const {
// Return the data of the Nth token.
std::string_view CustomTokenReader::NthTokenData(EntityOffset to) const {
if ((to + 1u) >= data_offset.size()) {
return {};
return kEmptyStringView;
}

auto begin_offset = data_offset[to];
Expand All @@ -1138,7 +1144,7 @@ std::string_view CustomTokenReader::NthTokenData(EntityOffset to) const {
begin_offset > data.size() ||
end_offset > data.size()) {
assert(false);
return {};
return kEmptyStringView;
}

return std::string_view(data).substr(
Expand Down Expand Up @@ -1470,23 +1476,23 @@ std::optional<unsigned> TokenRange::index_of(const Token &that) const noexcept {
// token range.
std::string_view TokenRange::data(void) const & {
if (!impl || impl.get() == kInvalidTokenReader.get() || !num_tokens) {
return {};
return kEmptyStringView;
}

auto data_begin = impl->NthTokenData(index);
auto data_end = impl->NthTokenData(num_tokens - 1u);

if (data_begin.data() > data_end.data()) {
assert(false);
return {};
return kEmptyStringView;
}

auto size = static_cast<size_t>(data_end.data() - data_begin.data()) +
data_end.size();

if (static_cast<EntityOffset>(size) != size) {
assert(false);
return {};
return kEmptyStringView;
}

return std::string_view(data_begin.data(), size);
Expand Down
10 changes: 8 additions & 2 deletions lib/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
#include "Types.h"

namespace mx {
namespace {

// A zero-sized string view that nontheless has a valid `.data()` pointer.
static const std::string_view kEmptyStringView("");

} // namespace

// Return the number of tokens in the fragment.
EntityOffset ReadTypeTokens::NumTokens(void) const {
Expand All @@ -30,7 +36,7 @@ TokenKind ReadTypeTokens::NthTokenKind(EntityOffset to) const {
// Return the data of the Nth token.
std::string_view ReadTypeTokens::NthTokenData(EntityOffset to) const {
if (to >= type->num_type_tokens) {
return {};
return kEmptyStringView;
}

const auto &reader = type->frag_reader;
Expand Down Expand Up @@ -145,7 +151,7 @@ std::string_view TypeImpl::Data(void) const & noexcept {
return std::string_view(toks.cStr(), size);
}
}
return {};
return kEmptyStringView;
}

// Return the token associated with a specific entity ID.
Expand Down

0 comments on commit 10434d5

Please sign in to comment.