Skip to content

Commit 893e1c1

Browse files
committed
[randstruct] add expected output for WIN64
This is an attempt to fix a test failure on one of the buildbot Windows machines. It also turns all of the "ASSERT_" macros into "EXPECT_" to catch all other failures. Link: https://lab.llvm.org/buildbot/#/builders/216/builds/2647
1 parent fa34951 commit 893e1c1

File tree

1 file changed

+63
-45
lines changed

1 file changed

+63
-45
lines changed

clang/unittests/AST/RandstructTest.cpp

Lines changed: 63 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,17 @@ using field_names = std::vector<std::string>;
3838

3939
namespace {
4040

41-
std::unique_ptr<ASTUnit> makeAST(const std::string &SourceCode,
42-
bool ExpectErr = false) {
41+
std::unique_ptr<ASTUnit> makeAST(const std::string &SourceCode) {
4342
std::vector<std::string> Args = getCommandLineArgsForTesting(Lang_C99);
4443
Args.push_back("-frandomize-layout-seed=1234567890abcdef");
4544

4645
IgnoringDiagConsumer IgnoringConsumer = IgnoringDiagConsumer();
4746

48-
std::unique_ptr<ASTUnit> AST = tooling::buildASTFromCodeWithArgs(
47+
return tooling::buildASTFromCodeWithArgs(
4948
SourceCode, Args, "input.c", "clang-tool",
5049
std::make_shared<PCHContainerOperations>(),
5150
tooling::getClangStripDependencyFileAdjuster(),
5251
tooling::FileContentMappings(), &IgnoringConsumer);
53-
54-
if (ExpectErr)
55-
EXPECT_TRUE(AST->getDiagnostics().hasErrorOccurred());
56-
else
57-
EXPECT_FALSE(AST->getDiagnostics().hasErrorOccurred());
58-
59-
return AST;
6052
}
6153

6254
RecordDecl *getRecordDeclFromAST(const ASTContext &C, const std::string &Name) {
@@ -104,11 +96,11 @@ namespace ast_matchers {
10496
TEST(RANDSTRUCT_TEST_SUITE_TEST, CanDetermineIfSubsequenceExists) {
10597
const field_names Seq = {"a", "b", "c", "d"};
10698

107-
ASSERT_TRUE(isSubsequence(Seq, {"b", "c"}));
108-
ASSERT_TRUE(isSubsequence(Seq, {"a", "b", "c", "d"}));
109-
ASSERT_TRUE(isSubsequence(Seq, {"b", "c", "d"}));
110-
ASSERT_TRUE(isSubsequence(Seq, {"a"}));
111-
ASSERT_FALSE(isSubsequence(Seq, {"a", "d"}));
99+
EXPECT_TRUE(isSubsequence(Seq, {"b", "c"}));
100+
EXPECT_TRUE(isSubsequence(Seq, {"a", "b", "c", "d"}));
101+
EXPECT_TRUE(isSubsequence(Seq, {"b", "c", "d"}));
102+
EXPECT_TRUE(isSubsequence(Seq, {"a"}));
103+
EXPECT_FALSE(isSubsequence(Seq, {"a", "d"}));
112104
}
113105

114106
#define RANDSTRUCT_TEST StructureLayoutRandomization
@@ -123,12 +115,14 @@ TEST(RANDSTRUCT_TEST, UnmarkedStruct) {
123115
};
124116
)c");
125117

118+
EXPECT_FALSE(AST->getDiagnostics().hasErrorOccurred());
119+
126120
const RecordDecl *RD = getRecordDeclFromAST(AST->getASTContext(), "test");
127121
const field_names Expected = {"bacon", "lettuce", "tomato", "mayonnaise"};
128122

129-
ASSERT_FALSE(RD->hasAttr<RandomizeLayoutAttr>());
130-
ASSERT_FALSE(RD->isRandomized());
131-
ASSERT_EQ(Expected, getFieldNamesFromRecord(RD));
123+
EXPECT_FALSE(RD->hasAttr<RandomizeLayoutAttr>());
124+
EXPECT_FALSE(RD->isRandomized());
125+
EXPECT_EQ(Expected, getFieldNamesFromRecord(RD));
132126
}
133127

134128
TEST(RANDSTRUCT_TEST, MarkedNoRandomize) {
@@ -141,12 +135,14 @@ TEST(RANDSTRUCT_TEST, MarkedNoRandomize) {
141135
} __attribute__((no_randomize_layout));
142136
)c");
143137

138+
EXPECT_FALSE(AST->getDiagnostics().hasErrorOccurred());
139+
144140
const RecordDecl *RD = getRecordDeclFromAST(AST->getASTContext(), "test");
145141
const field_names Expected = {"bacon", "lettuce", "tomato", "mayonnaise"};
146142

147-
ASSERT_TRUE(RD->hasAttr<NoRandomizeLayoutAttr>());
148-
ASSERT_FALSE(RD->isRandomized());
149-
ASSERT_EQ(Expected, getFieldNamesFromRecord(RD));
143+
EXPECT_TRUE(RD->hasAttr<NoRandomizeLayoutAttr>());
144+
EXPECT_FALSE(RD->isRandomized());
145+
EXPECT_EQ(Expected, getFieldNamesFromRecord(RD));
150146
}
151147

152148
TEST(RANDSTRUCT_TEST, MarkedRandomize) {
@@ -159,16 +155,20 @@ TEST(RANDSTRUCT_TEST, MarkedRandomize) {
159155
} __attribute__((randomize_layout));
160156
)c");
161157

158+
EXPECT_FALSE(AST->getDiagnostics().hasErrorOccurred());
159+
162160
const RecordDecl *RD = getRecordDeclFromAST(AST->getASTContext(), "test");
163-
#ifdef _WIN32
161+
#ifdef WIN64
162+
const field_names Expected = { "lettuce", "mayonnaise", "bacon", "tomato" };
163+
#elif defined(_WIN32)
164164
const field_names Expected = {"lettuce", "bacon", "mayonnaise", "tomato"};
165165
#else
166166
const field_names Expected = {"mayonnaise", "bacon", "tomato", "lettuce"};
167167
#endif
168168

169-
ASSERT_TRUE(RD->hasAttr<RandomizeLayoutAttr>());
170-
ASSERT_TRUE(RD->isRandomized());
171-
ASSERT_EQ(Expected, getFieldNamesFromRecord(RD));
169+
EXPECT_TRUE(RD->hasAttr<RandomizeLayoutAttr>());
170+
EXPECT_TRUE(RD->isRandomized());
171+
EXPECT_EQ(Expected, getFieldNamesFromRecord(RD));
172172
}
173173

174174
TEST(RANDSTRUCT_TEST, MismatchedAttrsDeclVsDef) {
@@ -182,6 +182,8 @@ TEST(RANDSTRUCT_TEST, MismatchedAttrsDeclVsDef) {
182182
} __attribute__((no_randomize_layout));
183183
)c");
184184

185+
EXPECT_FALSE(AST->getDiagnostics().hasErrorOccurred());
186+
185187
DiagnosticsEngine &Diags = AST->getDiagnostics();
186188

187189
EXPECT_FALSE(Diags.hasFatalErrorOccurred());
@@ -199,7 +201,9 @@ TEST(RANDSTRUCT_TEST, MismatchedAttrsRandomizeVsNoRandomize) {
199201
long long tomato;
200202
float mayonnaise;
201203
} __attribute__((randomize_layout)) __attribute__((no_randomize_layout));
202-
)c", true);
204+
)c");
205+
206+
EXPECT_TRUE(AST->getDiagnostics().hasErrorOccurred());
203207

204208
DiagnosticsEngine &Diags = AST->getDiagnostics();
205209

@@ -217,7 +221,9 @@ TEST(RANDSTRUCT_TEST, MismatchedAttrsNoRandomizeVsRandomize) {
217221
long long tomato;
218222
float mayonnaise;
219223
} __attribute__((no_randomize_layout)) __attribute__((randomize_layout));
220-
)c", true);
224+
)c");
225+
226+
EXPECT_TRUE(AST->getDiagnostics().hasErrorOccurred());
221227

222228
DiagnosticsEngine &Diags = AST->getDiagnostics();
223229

@@ -239,6 +245,8 @@ TEST(RANDSTRUCT_TEST, CheckAdjacentBitfieldsRemainAdjacentAfterRandomization) {
239245
} __attribute__((randomize_layout));
240246
)c");
241247

248+
EXPECT_FALSE(AST->getDiagnostics().hasErrorOccurred());
249+
242250
const RecordDecl *RD = getRecordDeclFromAST(AST->getASTContext(), "test");
243251

244252
#ifdef _WIN32
@@ -249,8 +257,8 @@ TEST(RANDSTRUCT_TEST, CheckAdjacentBitfieldsRemainAdjacentAfterRandomization) {
249257
const field_names Subseq = {"x", "y", "z"};
250258
const field_names Actual = getFieldNamesFromRecord(RD);
251259

252-
ASSERT_TRUE(isSubsequence(Actual, Subseq));
253-
ASSERT_EQ(Expected, Actual);
260+
EXPECT_TRUE(isSubsequence(Actual, Subseq));
261+
EXPECT_EQ(Expected, Actual);
254262
}
255263

256264
TEST(RANDSTRUCT_TEST, CheckVariableLengthArrayMemberRemainsAtEndOfStructure) {
@@ -263,14 +271,16 @@ TEST(RANDSTRUCT_TEST, CheckVariableLengthArrayMemberRemainsAtEndOfStructure) {
263271
} __attribute__((randomize_layout));
264272
)c");
265273

274+
EXPECT_FALSE(AST->getDiagnostics().hasErrorOccurred());
275+
266276
const RecordDecl *RD = getRecordDeclFromAST(AST->getASTContext(), "test");
267277
#ifdef _WIN32
268278
const field_names Expected = {"b", "a", "c", "name"};
269279
#else
270280
const field_names Expected = {"b", "c", "a", "name"};
271281
#endif
272282

273-
ASSERT_EQ(Expected, getFieldNamesFromRecord(RD));
283+
EXPECT_EQ(Expected, getFieldNamesFromRecord(RD));
274284
}
275285

276286
TEST(RANDSTRUCT_TEST, RandstructDoesNotOverrideThePackedAttr) {
@@ -295,6 +305,8 @@ TEST(RANDSTRUCT_TEST, RandstructDoesNotOverrideThePackedAttr) {
295305
} __attribute__((packed, randomize_layout));
296306
)c");
297307

308+
EXPECT_FALSE(AST->getDiagnostics().hasErrorOccurred());
309+
298310
// FIXME (?): calling getASTRecordLayout is probably a necessary evil so that
299311
// Clang's RecordBuilders can actually flesh out the information like
300312
// alignment, etc.
@@ -309,8 +321,8 @@ TEST(RANDSTRUCT_TEST, RandstructDoesNotOverrideThePackedAttr) {
309321
const field_names Expected = {"c", "a", "d", "b"};
310322
#endif
311323

312-
ASSERT_EQ(19, Layout->getSize().getQuantity());
313-
ASSERT_EQ(Expected, getFieldNamesFromRecord(RD));
324+
EXPECT_EQ(19, Layout->getSize().getQuantity());
325+
EXPECT_EQ(Expected, getFieldNamesFromRecord(RD));
314326
}
315327

316328
{
@@ -324,8 +336,8 @@ TEST(RANDSTRUCT_TEST, RandstructDoesNotOverrideThePackedAttr) {
324336
const field_names Expected = {"c", "a", "b"};
325337
#endif
326338

327-
ASSERT_EQ(10, Layout->getSize().getQuantity());
328-
ASSERT_EQ(Expected, getFieldNamesFromRecord(RD));
339+
EXPECT_EQ(10, Layout->getSize().getQuantity());
340+
EXPECT_EQ(Expected, getFieldNamesFromRecord(RD));
329341
}
330342

331343
{
@@ -335,8 +347,8 @@ TEST(RANDSTRUCT_TEST, RandstructDoesNotOverrideThePackedAttr) {
335347
&AST->getASTContext().getASTRecordLayout(RD);
336348
const field_names Expected = {"b", "c", "a"};
337349

338-
ASSERT_EQ(9, Layout->getSize().getQuantity());
339-
ASSERT_EQ(Expected, getFieldNamesFromRecord(RD));
350+
EXPECT_EQ(9, Layout->getSize().getQuantity());
351+
EXPECT_EQ(Expected, getFieldNamesFromRecord(RD));
340352
}
341353
}
342354

@@ -349,6 +361,8 @@ TEST(RANDSTRUCT_TEST, ZeroWidthBitfieldsSeparateAllocationUnits) {
349361
} __attribute__((randomize_layout));
350362
)c");
351363

364+
EXPECT_FALSE(AST->getDiagnostics().hasErrorOccurred());
365+
352366
const RecordDecl *RD =
353367
getRecordDeclFromAST(AST->getASTContext(), "test_struct");
354368
#ifdef _WIN32
@@ -357,7 +371,7 @@ TEST(RANDSTRUCT_TEST, ZeroWidthBitfieldsSeparateAllocationUnits) {
357371
const field_names Expected = {"", "a", "b"};
358372
#endif
359373

360-
ASSERT_EQ(Expected, getFieldNamesFromRecord(RD));
374+
EXPECT_EQ(Expected, getFieldNamesFromRecord(RD));
361375
}
362376

363377
TEST(RANDSTRUCT_TEST, RandstructDoesNotRandomizeUnionFieldOrder) {
@@ -372,12 +386,14 @@ TEST(RANDSTRUCT_TEST, RandstructDoesNotRandomizeUnionFieldOrder) {
372386
} __attribute__((randomize_layout));
373387
)c");
374388

389+
EXPECT_FALSE(AST->getDiagnostics().hasErrorOccurred());
390+
375391
const RecordDecl *RD =
376392
getRecordDeclFromAST(AST->getASTContext(), "test_union");
377393
const field_names Expected = {"a", "b", "c", "d", "e", "f"};
378394

379-
ASSERT_FALSE(RD->isRandomized());
380-
ASSERT_EQ(Expected, getFieldNamesFromRecord(RD));
395+
EXPECT_FALSE(RD->isRandomized());
396+
EXPECT_EQ(Expected, getFieldNamesFromRecord(RD));
381397
}
382398

383399
TEST(RANDSTRUCT_TEST, AnonymousStructsAndUnionsRetainFieldOrder) {
@@ -411,6 +427,8 @@ TEST(RANDSTRUCT_TEST, AnonymousStructsAndUnionsRetainFieldOrder) {
411427
} __attribute__((randomize_layout));
412428
)c");
413429

430+
EXPECT_FALSE(AST->getDiagnostics().hasErrorOccurred());
431+
414432
const RecordDecl *RD =
415433
getRecordDeclFromAST(AST->getASTContext(), "test_struct");
416434
#ifdef _WIN32
@@ -419,7 +437,7 @@ TEST(RANDSTRUCT_TEST, AnonymousStructsAndUnionsRetainFieldOrder) {
419437
const field_names Expected = {"f", "a", "l", "", "", "s", "r"};
420438
#endif
421439

422-
ASSERT_EQ(Expected, getFieldNamesFromRecord(RD));
440+
EXPECT_EQ(Expected, getFieldNamesFromRecord(RD));
423441

424442
bool AnonStructTested = false;
425443
bool AnonUnionTested = false;
@@ -431,12 +449,12 @@ TEST(RANDSTRUCT_TEST, AnonymousStructsAndUnionsRetainFieldOrder) {
431449
if (RD->isUnion()) {
432450
const field_names Expected = {"m", "n", "o", "p", "q"};
433451

434-
ASSERT_EQ(Expected, getFieldNamesFromRecord(RD));
452+
EXPECT_EQ(Expected, getFieldNamesFromRecord(RD));
435453
AnonUnionTested = true;
436454
} else {
437455
const field_names Expected = {"g", "h", "i", "j", "k"};
438456

439-
ASSERT_EQ(Expected, getFieldNamesFromRecord(RD));
457+
EXPECT_EQ(Expected, getFieldNamesFromRecord(RD));
440458
AnonStructTested = true;
441459
}
442460
} else if (RD->isStruct()) {
@@ -445,13 +463,13 @@ TEST(RANDSTRUCT_TEST, AnonymousStructsAndUnionsRetainFieldOrder) {
445463
#else
446464
const field_names Expected = {"d", "e", "f", "c", "b"};
447465
#endif
448-
ASSERT_EQ(Expected, getFieldNamesFromRecord(RD));
466+
EXPECT_EQ(Expected, getFieldNamesFromRecord(RD));
449467
}
450468
}
451469
}
452470

453-
ASSERT_TRUE(AnonStructTested);
454-
ASSERT_TRUE(AnonUnionTested);
471+
EXPECT_TRUE(AnonStructTested);
472+
EXPECT_TRUE(AnonUnionTested);
455473
}
456474

457475
} // namespace ast_matchers

0 commit comments

Comments
 (0)