Skip to content

[lldb][ClangASTImporter] Import record layouts from origin if available #83295

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

Merged
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
23 changes: 16 additions & 7 deletions lldb/source/Plugins/ExpressionParser/Clang/ClangASTImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,6 @@ bool ClangASTImporter::LayoutRecordType(
&vbase_offsets) {
RecordDeclToLayoutMap::iterator pos =
m_record_decl_to_layout_map.find(record_decl);
bool success = false;
base_offsets.clear();
vbase_offsets.clear();
if (pos != m_record_decl_to_layout_map.end()) {
Expand All @@ -770,13 +769,23 @@ bool ClangASTImporter::LayoutRecordType(
base_offsets.swap(pos->second.base_offsets);
vbase_offsets.swap(pos->second.vbase_offsets);
m_record_decl_to_layout_map.erase(pos);
success = true;
} else {
bit_size = 0;
alignment = 0;
field_offsets.clear();
return true;
}
return success;

// It's possible that we calculated the layout in a different
// ClangASTImporter instance. Try to import such layout if
// our decl has an origin.
if (auto origin = GetDeclOrigin(record_decl); origin.Valid())
if (importRecordLayoutFromOrigin(record_decl, bit_size, alignment,
field_offsets, base_offsets,
vbase_offsets))
return true;

bit_size = 0;
alignment = 0;
field_offsets.clear();

return false;
}

void ClangASTImporter::SetRecordLayout(clang::RecordDecl *decl,
Expand Down
4 changes: 4 additions & 0 deletions lldb/test/API/lang/cpp/gmodules/alignment/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
PCH_CXX_SOURCE = pch.h
CXX_SOURCES = main.cpp

include Makefile.rules
60 changes: 60 additions & 0 deletions lldb/test/API/lang/cpp/gmodules/alignment/TestPchAlignment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""
Tests that we correctly track AST layout info
(specifically alignment) when moving AST nodes
between ClangASTImporter instances (in this case,
from pch to executable to expression AST).
"""

import lldb
import os
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil


class TestPchAlignment(TestBase):
@add_test_categories(["gmodules"])
def test_expr(self):
self.build()
lldbutil.run_to_source_breakpoint(
self, "return data", lldb.SBFileSpec("main.cpp")
)

self.expect(
"frame variable data",
substrs=["row = 1", "col = 2", "row = 3", "col = 4", "stride = 5"],
)

@add_test_categories(["gmodules"])
def test_frame_var(self):
self.build()
lldbutil.run_to_source_breakpoint(
self, "return data", lldb.SBFileSpec("main.cpp")
)

self.expect_expr(
"data",
result_type="MatrixData",
result_children=[
ValueCheck(
name="section",
children=[
ValueCheck(
name="origin",
children=[
ValueCheck(name="row", value="1"),
ValueCheck(name="col", value="2"),
],
),
ValueCheck(
name="size",
children=[
ValueCheck(name="row", value="3"),
ValueCheck(name="col", value="4"),
],
),
],
),
ValueCheck(name="stride", value="5"),
],
)
10 changes: 10 additions & 0 deletions lldb/test/API/lang/cpp/gmodules/alignment/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
int main(int argc, const char *argv[]) {
struct MatrixData data = {0};
data.section.origin.row = 1;
data.section.origin.col = 2;
data.section.size.row = 3;
data.section.size.col = 4;
data.stride = 5;

return data.section.size.row;
}
21 changes: 21 additions & 0 deletions lldb/test/API/lang/cpp/gmodules/alignment/pch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef PCH_H_IN
#define PCH_H_IN

static const int kAlignment = 64;

struct [[gnu::aligned(kAlignment)]] RowCol {
unsigned row;
unsigned col;
};

struct [[gnu::aligned(kAlignment)]] Submatrix {
struct RowCol origin;
struct RowCol size;
};

struct [[gnu::aligned(kAlignment)]] MatrixData {
struct Submatrix section;
unsigned stride;
};

#endif // _H_IN