Skip to content

Commit a3fa3dd

Browse files
committed
Hack: quick and dirty randomization for testing!
1 parent 8a27707 commit a3fa3dd

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

clang/lib/AST/RecordLayoutBuilder.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
#include "llvm/Support/Format.h"
2020
#include "llvm/Support/MathExtras.h"
2121

22+
#include <algorithm>
23+
#include <random>
24+
2225
using namespace clang;
2326

2427
namespace {
@@ -1362,14 +1365,27 @@ void ItaniumRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
13621365
bool InsertExtraPadding = D->mayInsertExtraPadding(/*EmitRemark=*/true);
13631366
bool HasFlexibleArrayMember = D->hasFlexibleArrayMember();
13641367

1368+
// A staging area to easily reorder the fields
1369+
SmallVector<Decl *, 64> fields;
1370+
for (auto f : D->fields()) {
1371+
fields.push_back(f);
1372+
}
1373+
13651374
bool ShouldBeRandomized = D->getAttr<RandomizeLayoutAttr>() != nullptr;
13661375
if (ShouldBeRandomized) {
1367-
llvm::outs() << D->getNameAsString() << "\n";
1376+
// FIXME Should call our Randstruct code once we port it.
1377+
auto rng = std::default_random_engine {};
1378+
std::shuffle(std::begin(fields), std::end(fields), rng);
1379+
1380+
// This will rebuild the Decl chain of fields
1381+
D->reorderFields(fields);
13681382
}
13691383

1384+
13701385
for (auto I = D->field_begin(), End = D->field_end(); I != End; ++I) {
13711386
auto Next(I);
13721387
++Next;
1388+
13731389
LayoutField(*I,
13741390
InsertExtraPadding && (Next != End || !HasFlexibleArrayMember));
13751391
}

0 commit comments

Comments
 (0)