Skip to content

Commit 8059ade

Browse files
bulbazordjrtc27
authored andcommitted
[Core] Remove use of ClangASTContext in DumpDataExtractor
Summary: DumpDataExtractor uses ClangASTContext in order to get the proper llvm fltSemantics for the type it needs so that it can dump floats in a more precise way. However, there's no reason that this behavior needs to be specific ClangASTContext. Instead, I think it makes sense to ask TypeSystems for the float semantics for a type of a given size. Differential Revision: https://reviews.llvm.org/D67239 llvm-svn: 371258
2 parents 88846eb + b482db6 commit 8059ade

File tree

4 files changed

+49
-52
lines changed

4 files changed

+49
-52
lines changed

lldb/include/lldb/Symbol/ClangASTContext.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,8 @@ class ClangASTContext : public TypeSystem {
694694

695695
// Exploring the type
696696

697+
const llvm::fltSemantics &GetFloatTypeSemantics(size_t byte_size) override;
698+
697699
llvm::Optional<uint64_t> GetByteSize(lldb::opaque_compiler_type_t type,
698700
ExecutionContextScope *exe_scope) {
699701
if (llvm::Optional<uint64_t> bit_size = GetBitSize(type, exe_scope))

lldb/include/lldb/Symbol/TypeSystem.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <mutex>
1515
#include <string>
1616

17+
#include "llvm/ADT/APFloat.h"
1718
#include "llvm/ADT/APSInt.h"
1819
#include "llvm/ADT/SmallBitVector.h"
1920
#include "llvm/Support/Casting.h"
@@ -276,6 +277,8 @@ class TypeSystem : public PluginInterface {
276277

277278
// Exploring the type
278279

280+
virtual const llvm::fltSemantics &GetFloatTypeSemantics(size_t byte_size) = 0;
281+
279282
virtual llvm::Optional<uint64_t>
280283
GetBitSize(lldb::opaque_compiler_type_t type,
281284
ExecutionContextScope *exe_scope) = 0;

lldb/source/Core/DumpDataExtractor.cpp

Lines changed: 28 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include "lldb/Core/Address.h"
1515
#include "lldb/Core/Disassembler.h"
1616
#include "lldb/Core/ModuleList.h"
17-
#include "lldb/Symbol/ClangASTContext.h"
1817
#include "lldb/Target/ExecutionContext.h"
1918
#include "lldb/Target/ExecutionContextScope.h"
2019
#include "lldb/Target/SectionLoadList.h"
@@ -69,6 +68,9 @@ static float half2float(uint16_t half) {
6968
static llvm::Optional<llvm::APInt> GetAPInt(const DataExtractor &data,
7069
lldb::offset_t *offset_ptr,
7170
lldb::offset_t byte_size) {
71+
if (byte_size == 0)
72+
return llvm::None;
73+
7274
llvm::SmallVector<uint64_t, 2> uint64_array;
7375
lldb::offset_t bytes_left = byte_size;
7476
uint64_t u64;
@@ -556,57 +558,31 @@ lldb::offset_t lldb_private::DumpDataExtractor(
556558
if (exe_scope)
557559
target_sp = exe_scope->CalculateTarget();
558560
if (target_sp) {
559-
ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
560-
if (clang_ast) {
561-
clang::ASTContext *ast = clang_ast->getASTContext();
562-
if (ast) {
563-
llvm::SmallVector<char, 256> sv;
564-
// Show full precision when printing float values
565-
const unsigned format_precision = 0;
566-
const unsigned format_max_padding =
567-
target_sp->GetMaxZeroPaddingInFloatFormat();
568-
size_t item_bit_size = item_byte_size * 8;
569-
570-
if (item_bit_size == ast->getTypeSize(ast->FloatTy)) {
571-
llvm::Optional<llvm::APInt> apint =
572-
GetAPInt(DE, &offset, item_byte_size);
573-
if (apint.hasValue()) {
574-
llvm::APFloat apfloat(ast->getFloatTypeSemantics(ast->FloatTy),
575-
apint.getValue());
576-
apfloat.toString(sv, format_precision, format_max_padding);
577-
}
578-
} else if (item_bit_size == ast->getTypeSize(ast->DoubleTy)) {
579-
llvm::Optional<llvm::APInt> apint =
580-
GetAPInt(DE, &offset, item_byte_size);
581-
if (apint.hasValue()) {
582-
llvm::APFloat apfloat(ast->getFloatTypeSemantics(ast->DoubleTy),
583-
apint.getValue());
584-
apfloat.toString(sv, format_precision, format_max_padding);
585-
}
586-
} else if (item_bit_size == ast->getTypeSize(ast->LongDoubleTy)) {
587-
const auto &semantics =
588-
ast->getFloatTypeSemantics(ast->LongDoubleTy);
589-
590-
offset_t byte_size = item_byte_size;
591-
if (&semantics == &llvm::APFloatBase::x87DoubleExtended())
592-
byte_size = (llvm::APFloat::getSizeInBits(semantics) + 7) / 8;
593-
594-
llvm::Optional<llvm::APInt> apint =
595-
GetAPInt(DE, &offset, byte_size);
596-
if (apint.hasValue()) {
597-
llvm::APFloat apfloat(semantics, apint.getValue());
598-
apfloat.toString(sv, format_precision, format_max_padding);
599-
}
600-
} else if (item_bit_size == ast->getTypeSize(ast->HalfTy)) {
601-
llvm::Optional<llvm::APInt> apint =
602-
GetAPInt(DE, &offset, item_byte_size);
603-
if (apint.hasValue()) {
604-
llvm::APFloat apfloat(ast->getFloatTypeSemantics(ast->HalfTy),
605-
apint.getValue());
606-
apfloat.toString(sv, format_precision, format_max_padding);
607-
}
608-
}
609-
561+
auto type_system_or_err =
562+
target_sp->GetScratchTypeSystemForLanguage(eLanguageTypeC);
563+
if (!type_system_or_err) {
564+
llvm::consumeError(type_system_or_err.takeError());
565+
} else {
566+
auto &type_system = *type_system_or_err;
567+
llvm::SmallVector<char, 256> sv;
568+
// Show full precision when printing float values
569+
const unsigned format_precision = 0;
570+
const unsigned format_max_padding =
571+
target_sp->GetMaxZeroPaddingInFloatFormat();
572+
573+
const auto &semantics =
574+
type_system.GetFloatTypeSemantics(item_byte_size);
575+
576+
// Recalculate the byte size in case of a difference. This is possible
577+
// when item_byte_size is 16 (128-bit), because you could get back the
578+
// x87DoubleExtended semantics which has a byte size of 10 (80-bit).
579+
const size_t semantics_byte_size =
580+
(llvm::APFloat::getSizeInBits(semantics) + 7) / 8;
581+
llvm::Optional<llvm::APInt> apint =
582+
GetAPInt(DE, &offset, semantics_byte_size);
583+
if (apint.hasValue()) {
584+
llvm::APFloat apfloat(semantics, apint.getValue());
585+
apfloat.toString(sv, format_precision, format_max_padding);
610586
if (!sv.empty()) {
611587
s->Printf("%*.*s", (int)sv.size(), (int)sv.size(), sv.data());
612588
used_upfloat = true;

lldb/source/Symbol/ClangASTContext.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4991,6 +4991,22 @@ CompilerType ClangASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type) {
49914991
}
49924992
// Exploring the type
49934993

4994+
const llvm::fltSemantics &
4995+
ClangASTContext::GetFloatTypeSemantics(size_t byte_size) {
4996+
if (auto *ast = getASTContext()) {
4997+
const size_t bit_size = byte_size * 8;
4998+
if (bit_size == ast->getTypeSize(ast->FloatTy))
4999+
return ast->getFloatTypeSemantics(ast->FloatTy);
5000+
else if (bit_size == ast->getTypeSize(ast->DoubleTy))
5001+
return ast->getFloatTypeSemantics(ast->DoubleTy);
5002+
else if (bit_size == ast->getTypeSize(ast->LongDoubleTy))
5003+
return ast->getFloatTypeSemantics(ast->LongDoubleTy);
5004+
else if (bit_size == ast->getTypeSize(ast->HalfTy))
5005+
return ast->getFloatTypeSemantics(ast->HalfTy);
5006+
}
5007+
return llvm::APFloatBase::Bogus();
5008+
}
5009+
49945010
Optional<uint64_t>
49955011
ClangASTContext::GetBitSize(lldb::opaque_compiler_type_t type,
49965012
ExecutionContextScope *exe_scope) {

0 commit comments

Comments
 (0)