Skip to content

Commit 7942ebd

Browse files
committed
[clang] Add cc1 option for dumping layout for all complete types
This change adds an option which, in addition to dumping the record layout as is done by -fdump-record-layouts, causes us to compute the layout for all complete record types (rather than the as-needed basis which is usually done by clang), so that we will dump them as well. This is useful if we are looking for layout differences across large code bases without needing to instantiate every type we are interested in. Reviewed By: dexonsmith Differential Revision: https://reviews.llvm.org/D104484
1 parent 422adaa commit 7942ebd

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

clang/include/clang/Basic/LangOptions.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ BENIGN_LANGOPT(ModulesDebugInfo , 1, 0, "Modules debug info")
265265
BENIGN_LANGOPT(ElideConstructors , 1, 1, "C++ copy constructor elision")
266266
BENIGN_LANGOPT(DumpRecordLayouts , 1, 0, "dumping the layout of IRgen'd records")
267267
BENIGN_LANGOPT(DumpRecordLayoutsSimple , 1, 0, "dumping the layout of IRgen'd records in a simple form")
268+
BENIGN_LANGOPT(DumpRecordLayoutsComplete , 1, 0, "dumping the AST layout of all complete records")
268269
BENIGN_LANGOPT(DumpVTableLayouts , 1, 0, "dumping the layouts of emitted vtables")
269270
LANGOPT(NoConstantCFStrings , 1, 0, "no constant CoreFoundation strings")
270271
BENIGN_LANGOPT(InlineVisibilityHidden , 1, 0, "hidden visibility for inline C++ methods")

clang/include/clang/Driver/Options.td

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5381,10 +5381,13 @@ def stats_file : Joined<["-"], "stats-file=">,
53815381
def fdump_record_layouts_simple : Flag<["-"], "fdump-record-layouts-simple">,
53825382
HelpText<"Dump record layout information in a simple form used for testing">,
53835383
MarshallingInfoFlag<LangOpts<"DumpRecordLayoutsSimple">>;
5384+
def fdump_record_layouts_complete : Flag<["-"], "fdump-record-layouts-complete">,
5385+
HelpText<"Dump record layout information for all complete types">,
5386+
MarshallingInfoFlag<LangOpts<"DumpRecordLayoutsComplete">>;
53845387
def fdump_record_layouts : Flag<["-"], "fdump-record-layouts">,
53855388
HelpText<"Dump record layout information">,
53865389
MarshallingInfoFlag<LangOpts<"DumpRecordLayouts">>,
5387-
ImpliedByAnyOf<[fdump_record_layouts_simple.KeyPath]>;
5390+
ImpliedByAnyOf<[fdump_record_layouts_simple.KeyPath, fdump_record_layouts_complete.KeyPath]>;
53885391
def fix_what_you_can : Flag<["-"], "fix-what-you-can">,
53895392
HelpText<"Apply fix-it advice even in the presence of unfixable errors">,
53905393
MarshallingInfoFlag<FrontendOpts<"FixWhatYouCan">>;

clang/lib/AST/Decl.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4581,6 +4581,13 @@ RecordDecl::field_iterator RecordDecl::field_begin() const {
45814581
void RecordDecl::completeDefinition() {
45824582
assert(!isCompleteDefinition() && "Cannot redefine record!");
45834583
TagDecl::completeDefinition();
4584+
4585+
ASTContext &Ctx = getASTContext();
4586+
4587+
// Layouts are dumped when computed, so if we are dumping for all complete
4588+
// types, we need to force usage to get types that wouldn't be used elsewhere.
4589+
if (Ctx.getLangOpts().DumpRecordLayoutsComplete)
4590+
(void)Ctx.getASTRecordLayout(this);
45844591
}
45854592

45864593
/// isMsStruct - Get whether or not this record uses ms_struct layout.

clang/test/Layout/dump-complete.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %clang_cc1 -emit-llvm-only -fdump-record-layouts-complete %s | FileCheck %s
2+
3+
struct a {
4+
int x;
5+
};
6+
7+
struct b {
8+
char y;
9+
} foo;
10+
11+
class c {};
12+
13+
class d;
14+
15+
// CHECK: 0 | struct a
16+
// CHECK: 0 | struct b
17+
// CHECK: 0 | class c
18+
// CHECK-NOT: 0 | class d

0 commit comments

Comments
 (0)