Skip to content

Commit 938b5cf

Browse files
Nixoncolejeff
authored andcommitted
WIP, first attempt at emitting compiler warning
1 parent 063f1b1 commit 938b5cf

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

clang/include/clang/Basic/DiagnosticASTKinds.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,4 +343,10 @@ def warn_padded_struct_size : Warning<
343343
InGroup<Padded>, DefaultIgnore;
344344
def warn_unnecessary_packed : Warning<
345345
"packed attribute is unnecessary for %0">, InGroup<Packed>, DefaultIgnore;
346+
347+
// RandStruct implementation
348+
def warn_randomize_attr_conflict : Warning<
349+
"struct declared with 'randomize_layout' and 'no_randomize_layout' attributes. "
350+
"attribute 'no_randomize_layout' takes precedence ">;
351+
346352
}

clang/lib/AST/RecordLayoutBuilder.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2986,11 +2986,27 @@ ASTContext::getASTRecordLayout(const RecordDecl *D) const {
29862986
if (Entry) return *Entry;
29872987

29882988
const ASTRecordLayout *NewEntry = nullptr;
2989-
2989+
29902990
bool ShouldBeRandomized = D->getAttr<RandomizeLayoutAttr>() != nullptr;
29912991
if (ShouldBeRandomized) {
29922992
Randstruct randstruct;
29932993
randstruct.reorganizeFields(*this, D);
2994+
bool NotToBeRandomized = D->getAttr<NoRandomizeLayoutAttr>() != nullptr;
2995+
2996+
if (ShouldBeRandomized && NotToBeRandomized) {
2997+
Diag(D->getLocation(), diag::warn_randomize_attr_conflict);
2998+
}
2999+
else if (ShouldBeRandomized) {
3000+
// A staging area to easily reorder the fields
3001+
SmallVector<Decl *, 64> fields;
3002+
for (auto f : D->fields()) {
3003+
fields.push_back(f);
3004+
}
3005+
3006+
fields = rearrange(*this, fields);
3007+
3008+
// This will rebuild the Decl chain of fields
3009+
D->reorderFields(fields);
29943010
}
29953011

29963012
if (isMsLayout(*this)) {

0 commit comments

Comments
 (0)