@@ -38,25 +38,17 @@ using field_names = std::vector<std::string>;
38
38
39
39
namespace {
40
40
41
- std::unique_ptr<ASTUnit> makeAST (const std::string &SourceCode,
42
- bool ExpectErr = false ) {
41
+ std::unique_ptr<ASTUnit> makeAST (const std::string &SourceCode) {
43
42
std::vector<std::string> Args = getCommandLineArgsForTesting (Lang_C99);
44
43
Args.push_back (" -frandomize-layout-seed=1234567890abcdef" );
45
44
46
45
IgnoringDiagConsumer IgnoringConsumer = IgnoringDiagConsumer ();
47
46
48
- std::unique_ptr<ASTUnit> AST = tooling::buildASTFromCodeWithArgs (
47
+ return tooling::buildASTFromCodeWithArgs (
49
48
SourceCode, Args, " input.c" , " clang-tool" ,
50
49
std::make_shared<PCHContainerOperations>(),
51
50
tooling::getClangStripDependencyFileAdjuster (),
52
51
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;
60
52
}
61
53
62
54
RecordDecl *getRecordDeclFromAST (const ASTContext &C, const std::string &Name) {
@@ -104,11 +96,11 @@ namespace ast_matchers {
104
96
TEST (RANDSTRUCT_TEST_SUITE_TEST, CanDetermineIfSubsequenceExists) {
105
97
const field_names Seq = {" a" , " b" , " c" , " d" };
106
98
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" }));
112
104
}
113
105
114
106
#define RANDSTRUCT_TEST StructureLayoutRandomization
@@ -123,12 +115,14 @@ TEST(RANDSTRUCT_TEST, UnmarkedStruct) {
123
115
};
124
116
)c" );
125
117
118
+ EXPECT_FALSE (AST->getDiagnostics ().hasErrorOccurred ());
119
+
126
120
const RecordDecl *RD = getRecordDeclFromAST (AST->getASTContext (), " test" );
127
121
const field_names Expected = {" bacon" , " lettuce" , " tomato" , " mayonnaise" };
128
122
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));
132
126
}
133
127
134
128
TEST (RANDSTRUCT_TEST, MarkedNoRandomize) {
@@ -141,12 +135,14 @@ TEST(RANDSTRUCT_TEST, MarkedNoRandomize) {
141
135
} __attribute__((no_randomize_layout));
142
136
)c" );
143
137
138
+ EXPECT_FALSE (AST->getDiagnostics ().hasErrorOccurred ());
139
+
144
140
const RecordDecl *RD = getRecordDeclFromAST (AST->getASTContext (), " test" );
145
141
const field_names Expected = {" bacon" , " lettuce" , " tomato" , " mayonnaise" };
146
142
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));
150
146
}
151
147
152
148
TEST (RANDSTRUCT_TEST, MarkedRandomize) {
@@ -159,16 +155,20 @@ TEST(RANDSTRUCT_TEST, MarkedRandomize) {
159
155
} __attribute__((randomize_layout));
160
156
)c" );
161
157
158
+ EXPECT_FALSE (AST->getDiagnostics ().hasErrorOccurred ());
159
+
162
160
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)
164
164
const field_names Expected = {" lettuce" , " bacon" , " mayonnaise" , " tomato" };
165
165
#else
166
166
const field_names Expected = {" mayonnaise" , " bacon" , " tomato" , " lettuce" };
167
167
#endif
168
168
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));
172
172
}
173
173
174
174
TEST (RANDSTRUCT_TEST, MismatchedAttrsDeclVsDef) {
@@ -182,6 +182,8 @@ TEST(RANDSTRUCT_TEST, MismatchedAttrsDeclVsDef) {
182
182
} __attribute__((no_randomize_layout));
183
183
)c" );
184
184
185
+ EXPECT_FALSE (AST->getDiagnostics ().hasErrorOccurred ());
186
+
185
187
DiagnosticsEngine &Diags = AST->getDiagnostics ();
186
188
187
189
EXPECT_FALSE (Diags.hasFatalErrorOccurred ());
@@ -199,7 +201,9 @@ TEST(RANDSTRUCT_TEST, MismatchedAttrsRandomizeVsNoRandomize) {
199
201
long long tomato;
200
202
float mayonnaise;
201
203
} __attribute__((randomize_layout)) __attribute__((no_randomize_layout));
202
- )c" , true );
204
+ )c" );
205
+
206
+ EXPECT_TRUE (AST->getDiagnostics ().hasErrorOccurred ());
203
207
204
208
DiagnosticsEngine &Diags = AST->getDiagnostics ();
205
209
@@ -217,7 +221,9 @@ TEST(RANDSTRUCT_TEST, MismatchedAttrsNoRandomizeVsRandomize) {
217
221
long long tomato;
218
222
float mayonnaise;
219
223
} __attribute__((no_randomize_layout)) __attribute__((randomize_layout));
220
- )c" , true );
224
+ )c" );
225
+
226
+ EXPECT_TRUE (AST->getDiagnostics ().hasErrorOccurred ());
221
227
222
228
DiagnosticsEngine &Diags = AST->getDiagnostics ();
223
229
@@ -239,6 +245,8 @@ TEST(RANDSTRUCT_TEST, CheckAdjacentBitfieldsRemainAdjacentAfterRandomization) {
239
245
} __attribute__((randomize_layout));
240
246
)c" );
241
247
248
+ EXPECT_FALSE (AST->getDiagnostics ().hasErrorOccurred ());
249
+
242
250
const RecordDecl *RD = getRecordDeclFromAST (AST->getASTContext (), " test" );
243
251
244
252
#ifdef _WIN32
@@ -249,8 +257,8 @@ TEST(RANDSTRUCT_TEST, CheckAdjacentBitfieldsRemainAdjacentAfterRandomization) {
249
257
const field_names Subseq = {" x" , " y" , " z" };
250
258
const field_names Actual = getFieldNamesFromRecord (RD);
251
259
252
- ASSERT_TRUE (isSubsequence (Actual, Subseq));
253
- ASSERT_EQ (Expected, Actual);
260
+ EXPECT_TRUE (isSubsequence (Actual, Subseq));
261
+ EXPECT_EQ (Expected, Actual);
254
262
}
255
263
256
264
TEST (RANDSTRUCT_TEST, CheckVariableLengthArrayMemberRemainsAtEndOfStructure) {
@@ -263,14 +271,16 @@ TEST(RANDSTRUCT_TEST, CheckVariableLengthArrayMemberRemainsAtEndOfStructure) {
263
271
} __attribute__((randomize_layout));
264
272
)c" );
265
273
274
+ EXPECT_FALSE (AST->getDiagnostics ().hasErrorOccurred ());
275
+
266
276
const RecordDecl *RD = getRecordDeclFromAST (AST->getASTContext (), " test" );
267
277
#ifdef _WIN32
268
278
const field_names Expected = {" b" , " a" , " c" , " name" };
269
279
#else
270
280
const field_names Expected = {" b" , " c" , " a" , " name" };
271
281
#endif
272
282
273
- ASSERT_EQ (Expected, getFieldNamesFromRecord (RD));
283
+ EXPECT_EQ (Expected, getFieldNamesFromRecord (RD));
274
284
}
275
285
276
286
TEST (RANDSTRUCT_TEST, RandstructDoesNotOverrideThePackedAttr) {
@@ -295,6 +305,8 @@ TEST(RANDSTRUCT_TEST, RandstructDoesNotOverrideThePackedAttr) {
295
305
} __attribute__((packed, randomize_layout));
296
306
)c" );
297
307
308
+ EXPECT_FALSE (AST->getDiagnostics ().hasErrorOccurred ());
309
+
298
310
// FIXME (?): calling getASTRecordLayout is probably a necessary evil so that
299
311
// Clang's RecordBuilders can actually flesh out the information like
300
312
// alignment, etc.
@@ -309,8 +321,8 @@ TEST(RANDSTRUCT_TEST, RandstructDoesNotOverrideThePackedAttr) {
309
321
const field_names Expected = {" c" , " a" , " d" , " b" };
310
322
#endif
311
323
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));
314
326
}
315
327
316
328
{
@@ -324,8 +336,8 @@ TEST(RANDSTRUCT_TEST, RandstructDoesNotOverrideThePackedAttr) {
324
336
const field_names Expected = {" c" , " a" , " b" };
325
337
#endif
326
338
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));
329
341
}
330
342
331
343
{
@@ -335,8 +347,8 @@ TEST(RANDSTRUCT_TEST, RandstructDoesNotOverrideThePackedAttr) {
335
347
&AST->getASTContext ().getASTRecordLayout (RD);
336
348
const field_names Expected = {" b" , " c" , " a" };
337
349
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));
340
352
}
341
353
}
342
354
@@ -349,6 +361,8 @@ TEST(RANDSTRUCT_TEST, ZeroWidthBitfieldsSeparateAllocationUnits) {
349
361
} __attribute__((randomize_layout));
350
362
)c" );
351
363
364
+ EXPECT_FALSE (AST->getDiagnostics ().hasErrorOccurred ());
365
+
352
366
const RecordDecl *RD =
353
367
getRecordDeclFromAST (AST->getASTContext (), " test_struct" );
354
368
#ifdef _WIN32
@@ -357,7 +371,7 @@ TEST(RANDSTRUCT_TEST, ZeroWidthBitfieldsSeparateAllocationUnits) {
357
371
const field_names Expected = {" " , " a" , " b" };
358
372
#endif
359
373
360
- ASSERT_EQ (Expected, getFieldNamesFromRecord (RD));
374
+ EXPECT_EQ (Expected, getFieldNamesFromRecord (RD));
361
375
}
362
376
363
377
TEST (RANDSTRUCT_TEST, RandstructDoesNotRandomizeUnionFieldOrder) {
@@ -372,12 +386,14 @@ TEST(RANDSTRUCT_TEST, RandstructDoesNotRandomizeUnionFieldOrder) {
372
386
} __attribute__((randomize_layout));
373
387
)c" );
374
388
389
+ EXPECT_FALSE (AST->getDiagnostics ().hasErrorOccurred ());
390
+
375
391
const RecordDecl *RD =
376
392
getRecordDeclFromAST (AST->getASTContext (), " test_union" );
377
393
const field_names Expected = {" a" , " b" , " c" , " d" , " e" , " f" };
378
394
379
- ASSERT_FALSE (RD->isRandomized ());
380
- ASSERT_EQ (Expected, getFieldNamesFromRecord (RD));
395
+ EXPECT_FALSE (RD->isRandomized ());
396
+ EXPECT_EQ (Expected, getFieldNamesFromRecord (RD));
381
397
}
382
398
383
399
TEST (RANDSTRUCT_TEST, AnonymousStructsAndUnionsRetainFieldOrder) {
@@ -411,6 +427,8 @@ TEST(RANDSTRUCT_TEST, AnonymousStructsAndUnionsRetainFieldOrder) {
411
427
} __attribute__((randomize_layout));
412
428
)c" );
413
429
430
+ EXPECT_FALSE (AST->getDiagnostics ().hasErrorOccurred ());
431
+
414
432
const RecordDecl *RD =
415
433
getRecordDeclFromAST (AST->getASTContext (), " test_struct" );
416
434
#ifdef _WIN32
@@ -419,7 +437,7 @@ TEST(RANDSTRUCT_TEST, AnonymousStructsAndUnionsRetainFieldOrder) {
419
437
const field_names Expected = {" f" , " a" , " l" , " " , " " , " s" , " r" };
420
438
#endif
421
439
422
- ASSERT_EQ (Expected, getFieldNamesFromRecord (RD));
440
+ EXPECT_EQ (Expected, getFieldNamesFromRecord (RD));
423
441
424
442
bool AnonStructTested = false ;
425
443
bool AnonUnionTested = false ;
@@ -431,12 +449,12 @@ TEST(RANDSTRUCT_TEST, AnonymousStructsAndUnionsRetainFieldOrder) {
431
449
if (RD->isUnion ()) {
432
450
const field_names Expected = {" m" , " n" , " o" , " p" , " q" };
433
451
434
- ASSERT_EQ (Expected, getFieldNamesFromRecord (RD));
452
+ EXPECT_EQ (Expected, getFieldNamesFromRecord (RD));
435
453
AnonUnionTested = true ;
436
454
} else {
437
455
const field_names Expected = {" g" , " h" , " i" , " j" , " k" };
438
456
439
- ASSERT_EQ (Expected, getFieldNamesFromRecord (RD));
457
+ EXPECT_EQ (Expected, getFieldNamesFromRecord (RD));
440
458
AnonStructTested = true ;
441
459
}
442
460
} else if (RD->isStruct ()) {
@@ -445,13 +463,13 @@ TEST(RANDSTRUCT_TEST, AnonymousStructsAndUnionsRetainFieldOrder) {
445
463
#else
446
464
const field_names Expected = {" d" , " e" , " f" , " c" , " b" };
447
465
#endif
448
- ASSERT_EQ (Expected, getFieldNamesFromRecord (RD));
466
+ EXPECT_EQ (Expected, getFieldNamesFromRecord (RD));
449
467
}
450
468
}
451
469
}
452
470
453
- ASSERT_TRUE (AnonStructTested);
454
- ASSERT_TRUE (AnonUnionTested);
471
+ EXPECT_TRUE (AnonStructTested);
472
+ EXPECT_TRUE (AnonUnionTested);
455
473
}
456
474
457
475
} // namespace ast_matchers
0 commit comments