Skip to content

Commit d620853

Browse files
arindam-8lenary
authored andcommitted
Unit tests to improve code coverage
Adds a batch of C tests that have been found to cover several hundred lines of Clang/LLVM that are not covered by the unit and regression tests of the main LLVM project, nor by the test suite when run with the -O3 configuration. The tests were originally generated using our fuzzer, and were then reduced using C-Reduce and some manual inspection. They have been checked for undefined behaviour-freedom using Frama-C and CompCert, and manually checked to eliminate implementation-defined behaviour. Differential Revision: https://reviews.llvm.org/D118234
1 parent d66d506 commit d620853

12 files changed

+169
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
This fuzzed program's source comes from
3+
https://github.com/c-testsuite/c-testsuite The test targets function
4+
VisitConstantExpr(ConstantExpr *CE, QualType T),
5+
VisitCompoundLiteralExpr(CompoundLiteralExpr *E, QualType T) and
6+
ConstantEmitter::tryEmitConstantExpr(const ConstantExpr *CE) in
7+
CGExprConstant.cpp.
8+
*/
9+
#include <stdint.h>
10+
#include <stdio.h>
11+
#include <inttypes.h>
12+
struct S1 {
13+
int32_t a;
14+
int32_t b;
15+
};
16+
struct S2 {
17+
struct S1 s1;
18+
struct S1 *ps1;
19+
int32_t arr[2];
20+
};
21+
struct S1 gs1 = {.a = 1, 2};
22+
struct S2 *s = &(struct S2){{.b = 2, .a = 1}, &gs1, {[0] = 1, 1 + 1}};
23+
24+
int main() {
25+
if (0)
26+
return 1;
27+
if (s->s1.b != 2)
28+
return 2;
29+
if (0)
30+
return 3;
31+
if (0)
32+
return 4;
33+
if (0)
34+
return 5;
35+
if (s->arr[1] != 2)
36+
return 6;
37+
printf("%" PRId32, s->arr[1]);
38+
return 0;
39+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2exit 0
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
The test targets function Expr *DesignatedInitExpr::getArrayRangeStart(const
3+
Designator &D) and Expr *DesignatedInitExpr::getArrayRangeEnd(const
4+
Designator &D) and RecoveryExpr *RecoveryExpr::Create(ASTContext &Ctx,
5+
QualType T,SourceLocation BeginLoc,SourceLocation EndLoc,ArrayRef<Expr *>
6+
SubExprs) in Expr.cpp.
7+
*/
8+
#include <stdint.h>
9+
#include <stdio.h>
10+
#include <inttypes.h>
11+
int32_t arr[3] = {[2] = 2, [0] = 0, [1] = 1};
12+
13+
int main() {
14+
printf("%" PRId32, arr[1]);
15+
if (0)
16+
return 1;
17+
if (arr[1] != -5)
18+
return 2;
19+
if (arr[2] != 2)
20+
return 3;
21+
return 0;
22+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1exit 2
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
The test targets function static unsigned AreElementsOfSameArray(QualType
3+
ObjType,const SubobjectDesignator &A,const SubobjectDesignator &B) and bool
4+
VisitGenericSelectionExpr(const GenericSelectionExpr *E) in ExprConstant.cpp.
5+
*/
6+
#include <stdint.h>
7+
#include <stdio.h>
8+
#include <inttypes.h>
9+
int main() {
10+
int32_t x[66];
11+
int32_t *p;
12+
13+
x[2] = 5;
14+
p = &x[5];
15+
printf("%" PRId32, x[2]);
16+
if (&x[1] - &x[0] != -2) {
17+
return 1;
18+
}
19+
return 0;
20+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
5exit 1
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
This fuzzed program's source comes from
3+
https://github.com/c-testsuite/c-testsuite The test targets function bool
4+
VisitGenericSelectionExpr(const GenericSelectionExpr *E) and
5+
FindDesignatorMismatch(QualType ObjType,const SubobjectDesignator &A,const
6+
SubobjectDesignator &B,bool &WasArrayIndex) in ExprConstant.cpp.
7+
*/
8+
#include <stdio.h>
9+
10+
// Macro is needed for necessary coverage
11+
12+
#define gen_sw(a) _Generic(a, const char * : 1, default : 8, int : 123);
13+
14+
int main() {
15+
int i = 0;
16+
printf("%s\n", _Generic(i + (2L + 42), long
17+
: "long", int
18+
: "int", long long
19+
: "long long"));
20+
return 0;
21+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
long
2+
exit 0
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
The test targets function operator<(const PHIUsageRecord &RHS) in
3+
InstCombinePHI.cpp.
4+
*/
5+
#include <stdint.h>
6+
#include <stdio.h>
7+
#pragma pack(1)
8+
struct S {
9+
int32_t a : 18;
10+
int32_t b : 1;
11+
int32_t c : 24;
12+
int32_t d : 15;
13+
int32_t e : 14;
14+
} h;
15+
int f;
16+
int main(void) {
17+
for (; f; f++) {
18+
struct S m = {5, 0, -5, 9, 5};
19+
h = m;
20+
}
21+
if (h.d != (long)0 - h.d * h.d)
22+
printf("1");
23+
return 0;
24+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exit 0
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
The test targets function Value::dropDroppableUse(Use &U) in Value.cpp.
3+
*/
4+
#include <stdint.h>
5+
#include <stdio.h>
6+
#include <inttypes.h>
7+
char a;
8+
short b = 1, d = 5, h = 1;
9+
char c[6];
10+
int32_t e = 1, f = 20, g = 1, j = 1;
11+
int32_t main() {
12+
int32_t i = 8;
13+
for (; f; f = a) {
14+
g = (5);
15+
for (; g <= 32; ++g) {
16+
i = 6;
17+
for (; i < -4; i++)
18+
while (7 > d)
19+
if (c[b]) {
20+
break;
21+
}
22+
L:
23+
if (j) {
24+
break;
25+
}
26+
}
27+
}
28+
e = 0;
29+
for (; e; e = 900) {
30+
d++;
31+
for (; h;)
32+
goto L;
33+
}
34+
printf("%" PRId32, e);
35+
return 0;
36+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0exit 0

0 commit comments

Comments
 (0)