Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions lldb/include/lldb/DataFormatters/FormattersContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include "lldb/DataFormatters/TypeSynthetic.h"
#include "lldb/Symbol/CompilerType.h"
#include "lldb/Utility/RegularExpression.h"
#include "lldb/Utility/StringLexer.h"
#include "lldb/ValueObject/ValueObject.h"

namespace lldb_private {
Expand Down Expand Up @@ -59,18 +58,15 @@ class TypeMatcher {
if (type.IsEmpty())
return type;

std::string type_cstr(type.AsCString());
StringLexer type_lexer(type_cstr);
llvm::StringRef type_lexer(type.AsCString());

type_lexer.AdvanceIf("class ");
type_lexer.AdvanceIf("enum ");
type_lexer.AdvanceIf("struct ");
type_lexer.AdvanceIf("union ");
type_lexer.consume_front("class ");
type_lexer.consume_front("enum ");
type_lexer.consume_front("struct ");
type_lexer.consume_front("union ");
type_lexer = type_lexer.ltrim();

while (type_lexer.NextIf({' ', '\t', '\v', '\f'}).first)
;

return ConstString(type_lexer.GetUnlexed());
return ConstString(type_lexer);
}

public:
Expand Down
56 changes: 0 additions & 56 deletions lldb/include/lldb/Utility/StringLexer.h

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,29 @@
#include "lldb/Target/Target.h"
#include "lldb/Utility/LLDBLog.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/StringLexer.h"

#include "clang/Basic/TargetInfo.h"

#include <optional>
#include <vector>

static char popChar(llvm::StringRef &str) {
const char c = str.front();

str = str.drop_front();

return c;
}

static bool consumeChar(llvm::StringRef &str, char c) {
if (!str.starts_with(c))
return false;

str = str.drop_front();

return true;
}

using namespace lldb_private;

AppleObjCTypeEncodingParser::AppleObjCTypeEncodingParser(
Expand All @@ -35,31 +51,33 @@ AppleObjCTypeEncodingParser::AppleObjCTypeEncodingParser(
runtime.GetProcess()->GetTarget().GetArchitecture().GetTriple());
}

std::string AppleObjCTypeEncodingParser::ReadStructName(StringLexer &type) {
std::string AppleObjCTypeEncodingParser::ReadStructName(llvm::StringRef &type) {
StreamString buffer;
while (type.HasAtLeast(1) && type.Peek() != '=')
buffer.Printf("%c", type.Next());
while (!type.empty() && type.front() != '=')
buffer.Printf("%c", popChar(type));

return std::string(buffer.GetString());
}

std::optional<std::string>
AppleObjCTypeEncodingParser::ReadQuotedString(StringLexer &type) {
if (!type.HasAtLeast(1))
AppleObjCTypeEncodingParser::ReadQuotedString(llvm::StringRef &type) {
if (type.empty())
return std::nullopt;

StreamString buffer;
while (type.Peek() != '"') {
buffer.Printf("%c", type.Next());
if (!type.HasAtLeast(1))
while (type.front() != '"') {
buffer.Printf("%c", popChar(type));

if (type.empty())
return std::nullopt;
}
return std::string(buffer.GetString());
}

uint32_t AppleObjCTypeEncodingParser::ReadNumber(StringLexer &type) {
uint32_t AppleObjCTypeEncodingParser::ReadNumber(llvm::StringRef &type) {
uint32_t total = 0;
while (type.HasAtLeast(1) && isdigit(type.Peek()))
total = 10 * total + (type.Next() - '0');
while (!type.empty() && isdigit(type.front()))
total = 10 * total + (popChar(type) - '0');
return total;
}

Expand All @@ -72,10 +90,10 @@ AppleObjCTypeEncodingParser::StructElement::StructElement()

AppleObjCTypeEncodingParser::StructElement
AppleObjCTypeEncodingParser::ReadStructElement(TypeSystemClang &ast_ctx,
StringLexer &type,
llvm::StringRef &type,
bool for_expression) {
StructElement retval;
if (type.NextIf('"')) {
if (type.consume_front("\"")) {
if (auto maybe_name = ReadQuotedString(type))
retval.name = *maybe_name;
else
Expand All @@ -88,22 +106,23 @@ AppleObjCTypeEncodingParser::ReadStructElement(TypeSystemClang &ast_ctx,
}

clang::QualType AppleObjCTypeEncodingParser::BuildStruct(
TypeSystemClang &ast_ctx, StringLexer &type, bool for_expression) {
TypeSystemClang &ast_ctx, llvm::StringRef &type, bool for_expression) {
return BuildAggregate(ast_ctx, type, for_expression, _C_STRUCT_B, _C_STRUCT_E,
llvm::to_underlying(clang::TagTypeKind::Struct));
}

clang::QualType AppleObjCTypeEncodingParser::BuildUnion(
TypeSystemClang &ast_ctx, StringLexer &type, bool for_expression) {
TypeSystemClang &ast_ctx, llvm::StringRef &type, bool for_expression) {
return BuildAggregate(ast_ctx, type, for_expression, _C_UNION_B, _C_UNION_E,
llvm::to_underlying(clang::TagTypeKind::Union));
}

clang::QualType AppleObjCTypeEncodingParser::BuildAggregate(
TypeSystemClang &ast_ctx, StringLexer &type, bool for_expression,
TypeSystemClang &ast_ctx, llvm::StringRef &type, bool for_expression,
char opener, char closer, uint32_t kind) {
if (!type.NextIf(opener))
if (!consumeChar(type, opener))
return clang::QualType();

std::string name(ReadStructName(type));

// We do not handle templated classes/structs at the moment. If the name has
Expand All @@ -112,12 +131,12 @@ clang::QualType AppleObjCTypeEncodingParser::BuildAggregate(

const bool is_templated = name.find('<') != std::string::npos;

if (!type.NextIf('='))
if (!type.consume_front("="))
return clang::QualType();
bool in_union = true;
std::vector<StructElement> elements;
while (in_union && type.HasAtLeast(1)) {
if (type.NextIf(closer)) {
while (in_union && !type.empty()) {
if (consumeChar(type, closer)) {
in_union = false;
break;
} else {
Expand Down Expand Up @@ -158,13 +177,15 @@ clang::QualType AppleObjCTypeEncodingParser::BuildAggregate(
}

clang::QualType AppleObjCTypeEncodingParser::BuildArray(
TypeSystemClang &ast_ctx, StringLexer &type, bool for_expression) {
if (!type.NextIf(_C_ARY_B))
TypeSystemClang &ast_ctx, llvm::StringRef &type, bool for_expression) {
if (!consumeChar(type, _C_ARY_B))
return clang::QualType();

uint32_t size = ReadNumber(type);
clang::QualType element_type(BuildType(ast_ctx, type, for_expression));
if (!type.NextIf(_C_ARY_E))
if (!consumeChar(type, _C_ARY_E))
return clang::QualType();

CompilerType array_type(ast_ctx.CreateArrayType(
CompilerType(ast_ctx.weak_from_this(), element_type.getAsOpaquePtr()),
size, false));
Expand All @@ -177,15 +198,16 @@ clang::QualType AppleObjCTypeEncodingParser::BuildArray(
// consume but ignore the type info and always return an 'id'; if anything,
// dynamic typing will resolve things for us anyway
clang::QualType AppleObjCTypeEncodingParser::BuildObjCObjectPointerType(
TypeSystemClang &clang_ast_ctx, StringLexer &type, bool for_expression) {
if (!type.NextIf(_C_ID))
TypeSystemClang &clang_ast_ctx, llvm::StringRef &type,
bool for_expression) {
if (!consumeChar(type, _C_ID))
return clang::QualType();

clang::ASTContext &ast_ctx = clang_ast_ctx.getASTContext();

std::string name;

if (type.NextIf('"')) {
if (type.consume_front("\"")) {
// We have to be careful here. We're used to seeing
// @"NSString"
// but in records it is possible that the string following an @ is the name
Expand All @@ -205,17 +227,18 @@ clang::QualType AppleObjCTypeEncodingParser::BuildObjCObjectPointerType(
// quoted string is a class name. - If we see anything else, the quoted
// string is a field name and we push it back onto type.

// Save a copy for possible rollback.
llvm::StringRef backup = type;
if (auto maybe_name = ReadQuotedString(type))
name = *maybe_name;
else
return clang::QualType();

if (type.HasAtLeast(1)) {
switch (type.Peek()) {
if (!type.empty()) {
switch (type.front()) {
default:
// roll back
type.PutBack(name.length() +
2); // undo our consumption of the string and of the quotes
// roll back: undo our consumption of the string and of the quotes
type = backup;
name.clear();
break;
case _C_STRUCT_E:
Expand Down Expand Up @@ -263,16 +286,15 @@ clang::QualType AppleObjCTypeEncodingParser::BuildObjCObjectPointerType(
}
}

clang::QualType
AppleObjCTypeEncodingParser::BuildType(TypeSystemClang &clang_ast_ctx,
StringLexer &type, bool for_expression,
uint32_t *bitfield_bit_size) {
if (!type.HasAtLeast(1))
clang::QualType AppleObjCTypeEncodingParser::BuildType(
TypeSystemClang &clang_ast_ctx, llvm::StringRef &type, bool for_expression,
uint32_t *bitfield_bit_size) {
if (type.empty())
return clang::QualType();

clang::ASTContext &ast_ctx = clang_ast_ctx.getASTContext();

switch (type.Peek()) {
switch (type.front()) {
default:
break;
case _C_STRUCT_B:
Expand All @@ -285,9 +307,12 @@ AppleObjCTypeEncodingParser::BuildType(TypeSystemClang &clang_ast_ctx,
return BuildObjCObjectPointerType(clang_ast_ctx, type, for_expression);
}

switch (type.Next()) {
// Save a copy for potential rollback.
llvm::StringRef backup = type;

switch (popChar(type)) {
default:
type.PutBack(1);
type = backup;
return clang::QualType();
case _C_CHR:
return ast_ctx.CharTy;
Expand Down Expand Up @@ -347,7 +372,7 @@ AppleObjCTypeEncodingParser::BuildType(TypeSystemClang &clang_ast_ctx,
return ast_ctx.getConstType(target_type);
}
case _C_PTR: {
if (!for_expression && type.NextIf(_C_UNDEF)) {
if (!for_expression && consumeChar(type, _C_UNDEF)) {
// if we are not supporting the concept of unknownAny, but what is being
// created here is an unknownAny*, then we can just get away with a void*
// this is theoretically wrong (in the same sense as 'theoretically
Expand All @@ -374,7 +399,7 @@ CompilerType AppleObjCTypeEncodingParser::RealizeType(TypeSystemClang &ast_ctx,
const char *name,
bool for_expression) {
if (name && name[0]) {
StringLexer lexer(name);
llvm::StringRef lexer(name);
clang::QualType qual_type = BuildType(ast_ctx, lexer, for_expression);
return ast_ctx.GetType(qual_type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "clang/AST/ASTContext.h"

namespace lldb_private {
class StringLexer;
class AppleObjCTypeEncodingParser : public ObjCLanguageRuntime::EncodingToType {
public:
AppleObjCTypeEncodingParser(ObjCLanguageRuntime &runtime);
Expand All @@ -35,35 +34,35 @@ class AppleObjCTypeEncodingParser : public ObjCLanguageRuntime::EncodingToType {
~StructElement() = default;
};

clang::QualType BuildType(TypeSystemClang &clang_ast_ctx, StringLexer &type,
bool for_expression,
clang::QualType BuildType(TypeSystemClang &clang_ast_ctx,
llvm::StringRef &type, bool for_expression,
uint32_t *bitfield_bit_size = nullptr);

clang::QualType BuildStruct(TypeSystemClang &ast_ctx, StringLexer &type,
clang::QualType BuildStruct(TypeSystemClang &ast_ctx, llvm::StringRef &type,
bool for_expression);

clang::QualType BuildAggregate(TypeSystemClang &clang_ast_ctx,
StringLexer &type, bool for_expression,
llvm::StringRef &type, bool for_expression,
char opener, char closer, uint32_t kind);

clang::QualType BuildUnion(TypeSystemClang &ast_ctx, StringLexer &type,
clang::QualType BuildUnion(TypeSystemClang &ast_ctx, llvm::StringRef &type,
bool for_expression);

clang::QualType BuildArray(TypeSystemClang &ast_ctx, StringLexer &type,
clang::QualType BuildArray(TypeSystemClang &ast_ctx, llvm::StringRef &type,
bool for_expression);

std::string ReadStructName(StringLexer &type);
std::string ReadStructName(llvm::StringRef &type);

StructElement ReadStructElement(TypeSystemClang &ast_ctx, StringLexer &type,
bool for_expression);
StructElement ReadStructElement(TypeSystemClang &ast_ctx,
llvm::StringRef &type, bool for_expression);

clang::QualType BuildObjCObjectPointerType(TypeSystemClang &clang_ast_ctx,
StringLexer &type,
llvm::StringRef &type,
bool for_expression);

uint32_t ReadNumber(StringLexer &type);
uint32_t ReadNumber(llvm::StringRef &type);

std::optional<std::string> ReadQuotedString(StringLexer &type);
std::optional<std::string> ReadQuotedString(llvm::StringRef &type);

ObjCLanguageRuntime &m_runtime;
};
Expand Down
Loading