Index: lldb/source/Symbol/ClangASTContext.cpp =================================================================== --- lldb/source/Symbol/ClangASTContext.cpp +++ lldb/source/Symbol/ClangASTContext.cpp @@ -15,7 +15,6 @@ #include #include - // Clang headers like to use NDEBUG inside of them to enable/disable debug // related features using "#ifndef NDEBUG" preprocessor blocks to do one thing // or another. This is bad because it means that if clang was built in release @@ -206,7 +205,7 @@ llvm::cast(overridden_decl)); } } -} +} // namespace static lldb::addr_t GetVTableAddress(Process &process, VTableContextBase &vtable_ctx, @@ -953,8 +952,7 @@ return CompilerType(); } -lldb::BasicType -ClangASTContext::GetBasicTypeEnumeration(ConstString name) { +lldb::BasicType ClangASTContext::GetBasicTypeEnumeration(ConstString name) { if (name) { typedef UniqueCStringMap TypeNameToBasicTypeMap; static TypeNameToBasicTypeMap g_type_map; @@ -1019,8 +1017,7 @@ return eBasicTypeInvalid; } -CompilerType ClangASTContext::GetBasicType(ASTContext *ast, - ConstString name) { +CompilerType ClangASTContext::GetBasicType(ASTContext *ast, ConstString name) { if (ast) { lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration(name); return ClangASTContext::GetBasicType(ast, basic_type); @@ -1264,14 +1261,16 @@ // This assert should fire for anything that we don't catch above so we know // to fix any issues we run into. if (type_name) { - Host::SystemLog(Host::eSystemLogError, "error: need to add support for " - "DW_TAG_base_type '%s' encoded with " - "DW_ATE = 0x%x, bit_size = %u\n", + Host::SystemLog(Host::eSystemLogError, + "error: need to add support for " + "DW_TAG_base_type '%s' encoded with " + "DW_ATE = 0x%x, bit_size = %u\n", type_name, dw_ate, bit_size); } else { - Host::SystemLog(Host::eSystemLogError, "error: need to add support for " - "DW_TAG_base_type encoded with " - "DW_ATE = 0x%x, bit_size = %u\n", + Host::SystemLog(Host::eSystemLogError, + "error: need to add support for " + "DW_TAG_base_type encoded with " + "DW_ATE = 0x%x, bit_size = %u\n", dw_ate, bit_size); } return CompilerType(); @@ -1302,7 +1301,8 @@ clang::Decl *ClangASTContext::CopyDecl(ASTContext *dst_ast, ASTContext *src_ast, clang::Decl *source_decl) { FileSystemOptions file_system_options; - FileManager file_manager(file_system_options); + FileManager file_manager(file_system_options, + FileSystem::Instance().GetVirtualFileSystem()); ASTImporter importer(*dst_ast, file_manager, *src_ast, file_manager, false); if (llvm::Expected ret_or_error = @@ -1407,7 +1407,7 @@ CXXRecordDecl *decl = CXXRecordDecl::Create( *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(), - SourceLocation(), has_name ? &ast->Idents.get(name) : nullptr); + SourceLocation(), has_name ? &ast->Idents.get(name) : nullptr); if (!has_name) { // In C++ a lambda is also represented as an unnamed class. This is @@ -1457,10 +1457,10 @@ } namespace { - bool IsValueParam(const clang::TemplateArgument &argument) { - return argument.getKind() == TemplateArgument::Integral; - } +bool IsValueParam(const clang::TemplateArgument &argument) { + return argument.getKind() == TemplateArgument::Integral; } +} // namespace static TemplateParameterList *CreateTemplateParameterList( ASTContext *ast, @@ -1480,16 +1480,14 @@ identifier_info = &ast->Idents.get(name); if (IsValueParam(template_param_infos.args[i])) { template_param_decls.push_back(NonTypeTemplateParmDecl::Create( - *ast, decl_context, - SourceLocation(), SourceLocation(), depth, i, identifier_info, - template_param_infos.args[i].getIntegralType(), parameter_pack, - nullptr)); + *ast, decl_context, SourceLocation(), SourceLocation(), depth, i, + identifier_info, template_param_infos.args[i].getIntegralType(), + parameter_pack, nullptr)); } else { template_param_decls.push_back(TemplateTypeParmDecl::Create( - *ast, decl_context, - SourceLocation(), SourceLocation(), depth, i, identifier_info, - is_typename, parameter_pack)); + *ast, decl_context, SourceLocation(), SourceLocation(), depth, i, + identifier_info, is_typename, parameter_pack)); } } @@ -1659,8 +1657,7 @@ ClassTemplateSpecializationDecl *class_template_specialization_decl = ClassTemplateSpecializationDecl::Create( *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(), - SourceLocation(), class_template_decl, args, - nullptr); + SourceLocation(), class_template_decl, args, nullptr); class_template_specialization_decl->setSpecializationKind( TSK_ExplicitSpecialization); @@ -2154,7 +2151,8 @@ return CompilerType(ClangASTContext::GetASTContext(ast), ast->getFunctionType(ClangUtil::GetQualType(result_type), - qual_type_args, proto_info).getAsOpaquePtr()); + qual_type_args, proto_info) + .getAsOpaquePtr()); } ParmVarDecl *ClangASTContext::CreateParameterDeclaration( @@ -2261,11 +2259,9 @@ #pragma mark Enumeration Types -CompilerType -ClangASTContext::CreateEnumerationType(const char *name, DeclContext *decl_ctx, - const Declaration &decl, - const CompilerType &integer_clang_type, - bool is_scoped) { +CompilerType ClangASTContext::CreateEnumerationType( + const char *name, DeclContext *decl_ctx, const Declaration &decl, + const CompilerType &integer_clang_type, bool is_scoped) { // TODO: Do something intelligent with the Declaration object passed in // like maybe filling in the SourceLocation with it... ASTContext *ast = getASTContext(); @@ -2733,9 +2729,10 @@ } break; case clang::Type::Typedef: - return GetCompleteQualType(ast, llvm::cast(qual_type) - ->getDecl() - ->getUnderlyingType(), + return GetCompleteQualType(ast, + llvm::cast(qual_type) + ->getDecl() + ->getUnderlyingType(), allow_completion); case clang::Type::Auto: @@ -2974,8 +2971,8 @@ if (size) *size = ext_vector_type->getNumElements(); if (element_type) - *element_type = - CompilerType(this, ext_vector_type->getElementType().getAsOpaquePtr()); + *element_type = CompilerType( + this, ext_vector_type->getElementType().getAsOpaquePtr()); } return true; } @@ -3168,7 +3165,8 @@ ++num_fields; } if (base_type_ptr) - *base_type_ptr = CompilerType(this, base_qual_type.getAsOpaquePtr()); + *base_type_ptr = + CompilerType(this, base_qual_type.getAsOpaquePtr()); return num_fields; } } @@ -3450,7 +3448,8 @@ if (pointee_type) pointee_type->SetCompilerType( this, llvm::cast(qual_type) - ->getPointeeType().getAsOpaquePtr()); + ->getPointeeType() + .getAsOpaquePtr()); return true; case clang::Type::BlockPointer: if (pointee_type) @@ -3709,9 +3708,8 @@ const clang::Type::TypeClass type_class = qual_type->getTypeClass(); switch (type_class) { case clang::Type::Builtin: - if (check_objc && - llvm::cast(qual_type)->getKind() == - clang::BuiltinType::ObjCId) { + if (check_objc && llvm::cast(qual_type)->getKind() == + clang::BuiltinType::ObjCId) { if (dynamic_pointee_type) dynamic_pointee_type->SetCompilerType(this, type); return true; @@ -3876,8 +3874,7 @@ } bool ClangASTContext::CanPassInRegisters(const CompilerType &type) { - if (auto *record_decl = - ClangASTContext::GetAsRecordDecl(type)) { + if (auto *record_decl = ClangASTContext::GetAsRecordDecl(type)) { return record_decl->canPassInRegisters(); } return false; @@ -4010,10 +4007,10 @@ const clang::Type::TypeClass type_class = qual_type->getTypeClass(); switch (type_class) { case clang::Type::Attributed: - return GetTypeInfo( - qual_type->getAs() - ->getModifiedType().getAsOpaquePtr(), - pointee_or_element_clang_type); + return GetTypeInfo(qual_type->getAs() + ->getModifiedType() + .getAsOpaquePtr(), + pointee_or_element_clang_type); case clang::Type::Builtin: { const clang::BuiltinType *builtin_type = llvm::dyn_cast( qual_type->getCanonicalTypeInternal()); @@ -4542,8 +4539,8 @@ ClangASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) { if (type) return CompilerType( - this, - GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)).getAsOpaquePtr()); + this, GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)) + .getAsOpaquePtr()); return CompilerType(); } @@ -5044,9 +5041,8 @@ ->getCanonicalTypeUnqualified()); } if (qual_type->isObjCObjectOrInterfaceType()) - return bit_size + - getASTContext()->getTypeSize( - getASTContext()->ObjCBuiltinClassTy); + return bit_size + getASTContext()->getTypeSize( + getASTContext()->ObjCBuiltinClassTy); // Function types actually have a size of 0, that's not an error. if (qual_type->isFunctionProtoType()) return bit_size; @@ -5685,7 +5681,7 @@ clang::QualType pointee_type(pointer_type->getPointeeType()); uint32_t num_pointee_children = CompilerType(this, pointee_type.getAsOpaquePtr()) - .GetNumChildren(omit_empty_base_classes, exe_ctx); + .GetNumChildren(omit_empty_base_classes, exe_ctx); if (num_pointee_children == 0) { // We have a pointer to a pointee type that claims it has no children. We // will want to look at @@ -5824,8 +5820,7 @@ void ClangASTContext::ForEachEnumerator( lldb::opaque_compiler_type_t type, - std::function const &callback) { const clang::EnumType *enum_type = llvm::dyn_cast(GetCanonicalQualType(type)); @@ -6308,9 +6303,10 @@ if (superclass_interface_decl) { if (bit_offset_ptr) *bit_offset_ptr = 0; - return CompilerType(this, - getASTContext()->getObjCInterfaceType( - superclass_interface_decl).getAsOpaquePtr()); + return CompilerType( + this, getASTContext() + ->getObjCInterfaceType(superclass_interface_decl) + .getAsOpaquePtr()); } } } @@ -7743,7 +7739,7 @@ const clang::Type::TypeClass type_class = qual_type->getTypeClass(); switch (type_class) { case clang::Type::Record: { - if (! GetCompleteType(type)) + if (!GetCompleteType(type)) return nullptr; const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl(); @@ -7784,7 +7780,7 @@ size_t arg_idx) { const clang::ClassTemplateSpecializationDecl *template_decl = GetAsTemplateSpecialization(type); - if (! template_decl || arg_idx >= template_decl->getTemplateArgs().size()) + if (!template_decl || arg_idx >= template_decl->getTemplateArgs().size()) return eTemplateArgumentKindNull; switch (template_decl->getTemplateArgs()[arg_idx].getKind()) { @@ -7839,7 +7835,7 @@ size_t idx) { const clang::ClassTemplateSpecializationDecl *template_decl = GetAsTemplateSpecialization(type); - if (! template_decl || idx >= template_decl->getTemplateArgs().size()) + if (!template_decl || idx >= template_decl->getTemplateArgs().size()) return llvm::None; const clang::TemplateArgument &template_arg = @@ -8159,10 +8155,10 @@ } clang::CXXMethodDecl *ClangASTContext::AddMethodToCXXRecordType( - lldb::opaque_compiler_type_t type, const char *name, const char *mangled_name, - const CompilerType &method_clang_type, lldb::AccessType access, - bool is_virtual, bool is_static, bool is_inline, bool is_explicit, - bool is_attr_used, bool is_artificial) { + lldb::opaque_compiler_type_t type, const char *name, + const char *mangled_name, const CompilerType &method_clang_type, + lldb::AccessType access, bool is_virtual, bool is_static, bool is_inline, + bool is_explicit, bool is_attr_used, bool is_artificial) { if (!type || !method_clang_type.IsValid() || name == nullptr || name[0] == '\0') return nullptr; @@ -8202,9 +8198,8 @@ return nullptr; // skip everything artificial const clang::ExplicitSpecifier explicit_spec( - nullptr /*expr*/, is_explicit - ? clang::ExplicitSpecKind::ResolvedTrue - : clang::ExplicitSpecKind::ResolvedFalse); + nullptr /*expr*/, is_explicit ? clang::ExplicitSpecKind::ResolvedTrue + : clang::ExplicitSpecKind::ResolvedFalse); if (name[0] == '~') { cxx_dtor_decl = clang::CXXDestructorDecl::Create( *getASTContext(), cxx_record_decl, clang::SourceLocation(), @@ -8258,8 +8253,7 @@ clang::SourceLocation()), method_qual_type, nullptr, // TypeSourceInfo * - is_inline, explicit_spec, CSK_unspecified, - clang::SourceLocation()); + is_inline, explicit_spec, CSK_unspecified, clang::SourceLocation()); } } @@ -10323,8 +10317,7 @@ UserExpression *ClangASTContextForExpressions::GetUserExpression( llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language, Expression::ResultType desired_type, - const EvaluateExpressionOptions &options, - ValueObject *ctx_obj) { + const EvaluateExpressionOptions &options, ValueObject *ctx_obj) { TargetSP target_sp = m_target_wp.lock(); if (!target_sp) return nullptr; @@ -10363,8 +10356,7 @@ return m_persistent_variables.get(); } -clang::ExternalASTMerger & -ClangASTContextForExpressions::GetMergerUnchecked() { +clang::ExternalASTMerger &ClangASTContextForExpressions::GetMergerUnchecked() { lldbassert(m_scratch_ast_source_up != nullptr); return m_scratch_ast_source_up->GetMergerUnchecked(); } Index: llvm/lib/MC/MCInstPrinter.cpp =================================================================== --- llvm/lib/MC/MCInstPrinter.cpp +++ llvm/lib/MC/MCInstPrinter.cpp @@ -83,15 +83,19 @@ } format_object MCInstPrinter::formatHex(int64_t Value) const { - switch(PrintHexStyle) { + switch (PrintHexStyle) { case HexStyle::C: - if (Value < 0) + if (Value == std::numeric_limits::min()) + return format("-0x8000000000000000"); + else if (Value < 0) return format("-0x%" PRIx64, -Value); else return format("0x%" PRIx64, Value); case HexStyle::Asm: - if (Value < 0) { - if (needsLeadingZero((uint64_t)(-Value))) + if (Value == std::numeric_limits::min()) + return format("-8000000000000000h"); + else if (Value < 0) { + if (needsLeadingZero(-(uint64_t)(Value))) return format("-0%" PRIx64 "h", -Value); else return format("-%" PRIx64 "h", -Value); Index: llvm/unittests/MC/CMakeLists.txt =================================================================== --- llvm/unittests/MC/CMakeLists.txt +++ llvm/unittests/MC/CMakeLists.txt @@ -8,6 +8,7 @@ add_llvm_unittest(MCTests Disassembler.cpp DwarfLineTables.cpp + MCInstPrinter.cpp StringTableBuilderTest.cpp TargetRegistry.cpp ) Index: llvm/unittests/MC/MCInstPrinter.cpp =================================================================== --- /dev/null +++ llvm/unittests/MC/MCInstPrinter.cpp @@ -0,0 +1,76 @@ +//===- llvm/unittest/MC/MCInstPrinter.cpp ---------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "llvm/MC/MCInstPrinter.h" +#include "llvm/MC/MCAsmInfo.h" +#include "llvm/MC/MCInstrInfo.h" +#include "llvm/Support/TargetRegistry.h" +#include "llvm/Support/TargetSelect.h" +#include "llvm/Target/TargetMachine.h" +#include "llvm/Target/TargetOptions.h" +#include "gtest/gtest.h" + +using namespace llvm; + +namespace { + +struct Printer { + std::unique_ptr MRI; + std::unique_ptr MAI; + std::unique_ptr MII; + std::unique_ptr MIP; + + Printer() { + llvm::InitializeAllTargetInfos(); + llvm::InitializeAllTargetMCs(); + + std::string TripleName = "x86_64-pc-linux"; + std::string ErrorStr; + + const Target *TheTarget = + TargetRegistry::lookupTarget(TripleName, ErrorStr); + + // If we didn't build x86, do not run the test. + if (!TheTarget) + return; + + MRI.reset(TheTarget->createMCRegInfo(TripleName)); + MAI.reset(TheTarget->createMCAsmInfo(*MRI, TripleName)); + MII.reset(TheTarget->createMCInstrInfo()); + MIP.reset(TheTarget->createMCInstPrinter( + Triple(TripleName), MAI->getAssemblerDialect(), *MAI, *MII, *MRI)); + } + + operator bool() { return MIP.get(); } + + template std::string formatHex(T i) { + std::string Buffer; + raw_string_ostream OS(Buffer); + OS << MIP->formatHex(i); + OS.flush(); + return Buffer; + } +}; + +Printer &getPrinter() { + static Printer P; + return P; +} +} // namespace + +TEST(MCInstPrinter, formatHex) { + Printer &P = getPrinter(); + if (!P) + return; + + EXPECT_EQ("0x1", P.formatHex(1)); + EXPECT_EQ("0x7fffffffffffffff", + P.formatHex(std::numeric_limits::max())); + EXPECT_EQ("-0x8000000000000000", + P.formatHex(std::numeric_limits::min())); +}