Skip to content

[lldb][DWARFASTParserClang] Adjust language type for conflicting Objective-C++ forward declarations #130768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
38 changes: 38 additions & 0 deletions lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,41 @@ static void PrepareContextToReceiveMembers(TypeSystemClang &ast,
}
}

/// Returns \c true if a forward declaration in C or C++ is actually an
/// Objective-C++ forward declaration. Otherwise, or on error, returns
/// \c false.
static bool IsCppForwardDeclObjC(SymbolFileDWARF &dwarf,
const ParsedDWARFTypeAttributes &attrs) {
assert(attrs.is_forward_declaration);

if (Language::LanguageIsObjC(attrs.class_language))
return false;

TypeQuery query(attrs.name);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm hoping we can be smarter about this lookup to not incur a big penalty for doing these type lookups

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's also some filtering we got to do on the context, we only want to consider types in the global namespace

TypeResults results;

if (SymbolFileDWARFDebugMap *debug_map_symfile = dwarf.GetDebugMapSymfile())
debug_map_symfile->FindTypes(query, results);
else
dwarf.FindTypes(query, results);

// Check that all types we found are Objective-C++ types.
// Otherwise we're dealing with an actual ODR violation and
// we can't say for sure what language this forward declaration
// referred to.
bool all_objc_types = true;
results.GetTypeMap().ForEach([&](const lldb::TypeSP &t) -> bool {
assert(t);

all_objc_types &= Language::LanguageIsObjC(
t->GetForwardCompilerType().GetMinimumLanguage());

return true;
});

return all_objc_types;
}

ParsedDWARFTypeAttributes::ParsedDWARFTypeAttributes(const DWARFDIE &die) {
DWARFAttributes attributes = die.GetAttributes();
for (size_t i = 0; i < attributes.Size(); ++i) {
Expand Down Expand Up @@ -1810,6 +1845,9 @@ DWARFASTParserClang::ParseStructureLikeDIE(const SymbolContext &sc,
}

if (attrs.is_forward_declaration) {
if (IsCppForwardDeclObjC(*dwarf, attrs))
attrs.class_language = eLanguageTypeObjC_plus_plus;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does that mean we do full global search for every C++ fwddecl we encounter?


// See if the type comes from a Clang module and if so, track down
// that type.
TypeSP type_sp = ParseTypeFromClangModule(sc, die, log);
Expand Down
70 changes: 70 additions & 0 deletions lldb/test/Shell/SymbolFile/DWARF/objcxx-forward-decls.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# REQUIRES: system-darwin

# In this test we have two CUs with conflicting forward declaration
# depending on the CU language (one is C++ and the other is Objective-C++).
# We are then stopped in the C++ CU and try to print the type, at which
# point LLDB will try to make it into an Clang AST node. If LLDB were to
# interpret the type as C++ instead of Objective-C, we'd violate Clang
# invariants and crash.
#
# RUN: split-file %s %t
# RUN: %clangxx_host -c -g -x objective-c++ %t/request.m -o %t/request_objc.o
# RUN: %clangxx_host -c -g %t/main.cpp -o %t/main.o
# RUN: %clangxx_host %t/main.o %t/request_objc.o -framework Foundation -o %t/a.out
#
# RUN: %lldb %t/a.out \
# RUN: -o "breakpoint set -p return -X main" \
# RUN: -o run \
# RUN: -o "frame variable r" \
# RUN: -o exit | FileCheck %s
#
# RUN: dsymutil %t/a.out
#
# RUN: %lldb %t/a.out \
# RUN: -o "breakpoint set -p return -X main" \
# RUN: -o run \
# RUN: -o "frame variable r" \
# RUN: -o exit | FileCheck %s --check-prefix=CHECK-DSYM

# CHECK: (lldb) frame variable r
# CHECK-NEXT: (Request) ::r = (m_request = "Hello, World!")

# CHECK-DSYM: (lldb) frame variable r
# CHECK-DSYM-NEXT: (Request) ::r = (m_request = "Hello, World!")

#--- lib.h
#ifndef LIB_H_IN
#define LIB_H_IN

#ifdef __OBJC__
@class NSString;
#else
class NSString;
#endif

struct Request {
NSString * m_request = nullptr;
};

#endif // _H_IN

#--- main.cpp
#include "lib.h"

void process(Request *);

Request r;

int main() {
process(&r);
return 0;
}

#--- request.m
#import <Foundation/Foundation.h>

#include "lib.h"

void process(Request * r) {
r->m_request = @"Hello, World!";
}
Loading