Skip to content

Instantly share code, notes, and snippets.

@markd2
Last active June 2, 2017 16:43
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save markd2/9ba66145135321fa4830 to your computer and use it in GitHub Desktop.
Save markd2/9ba66145135321fa4830 to your computer and use it in GitHub Desktop.
code coverage on the command line
#import <stdio.h>
// xcrun clang -g -Wall -Werror -fprofile-instr-generate -fcoverage-mapping -o testprof testprof.m
// ./testprof
// xcrun llvm-profdata merge -o testprof.profdata default.profraw
// xcrun llvm-cov show ./testprof -instr-profile=testprof.profdata testprof.m
static int DoSomething (int value) {
if (value == 55) {
printf ("got 55\n");
} else {
printf ("got something else\n");
}
return 23;
} // DoSomething
static void DoSomethingElse (int value) {
int blah = DoSomething(value);
if (blah == 23) {
printf ("yay 23\n");
} else {
printf ("bummer\n");
}
} // DoSomethingElse
int main (int argc, char *argv[]) {
if (argc != 1) {
printf ("bad usage\n");
return -1;
}
DoSomethingElse (55);
return 0;
} // main
#if 0
% ./testprof
got 55
yay 23
% xcrun llvm-profdata merge -o testprof.profdata default.profraw
% xcrun llvm-cov show ./testprof -instr-profile=testprof.profdata testprof.m
| 1|#import <stdio.h>
| 2|
| 3|// clang -g -Wall -Werror -o testprof testprof.m
| 4|// clang -g -Wall -Werror -fprofile-instr-generate -fcoverage-mapping -o testprof testprof.m
| 5|
| 6|
1| 7|static int DoSomething (int value) {
1| 8| if (value == 55) {
1| 9| printf ("got 55\n");
0| 10| } else {
0| 11| printf ("got something else\n");
0| 12| }
1| 13|
1| 14| return 23;
1| 15|
1| 16|} // DoSomething
| 17|
| 18|
1| 19|static void DoSomethingElse (int value) {
1| 20| int blah = DoSomething(value);
1| 21|
1| 22| if (blah == 23) {
1| 23| printf ("yay 23\n");
0| 24| } else {
0| 25| printf ("bummer\n");
0| 26| }
1| 27|
1| 28|} // DoSomethingElse
| 29|
| 30|
1| 31|int main (int argc, char *argv[]) {
1| 32|
0| 33| if (argc != 1) {
0| 34| printf ("bad usage\n");
0| 35| return -1;
0| 36| }
1| 37|
1| 38| DoSomethingElse (55);
1| 39|
1| 40| return 0;
1| 41|
1| 42|} // main
#endif
// xcrun swiftc -profile-generate -profile-coverage-mapping testprof.swift
// ./testprof
// xcrun llvm-profdata merge -o testprof.profdata default.profraw
// xcrun llvm-cov show ./testprof -instr-profile=testprof.profdata testprof.swift
func DoSomething (value: Int) -> Int {
if value == 55 {
print ("got 55\n")
} else {
print ("got something else\n")
}
return 23;
}
func DoSomethingElse (value: Int) -> Void {
let blah = DoSomething (value)
if blah == 23 {
print ("yay 23")
} else {
print ("bummer")
}
}
DoSomethingElse(55)
/*
| 1|// xcrun swiftc -profile-generate -profile-coverage-mapping testprof.swift
| 2|// testprof
| 3|// xcrun llvm-profdata merge -o testprof.profdata default.profraw
| 4|// xcrun llvm-cov show ./testprof -instr-profile=testprof.profdata testprof.swift
| 5|
1| 6|func DoSomething (value: Int) -> Int {
1| 7| if value == 55 {
1| 8| print ("got 55\n")
0| 9| } else {
0| 10| print ("got something else\n")
0| 11| }
1| 12|
1| 13| return 23;
1| 14|}
| 15|
1| 16|func DoSomethingElse (value: Int) -> Void {
1| 17| let blah = DoSomething (value)
1| 18|
1| 19| if blah == 23 {
1| 20| print ("yay 23")
0| 21| } else {
0| 22| print ("bummer")
0| 23| }
1| 24|}
| 25|
| 26|DoSomethingElse(55)
*/
@kleinlieu
Copy link

Any ideas on how I would be able to run this on an entire Xcode project?

@Torreslol
Copy link

the same question

@itrmit
Copy link

itrmit commented Jan 29, 2016

$ xcrun swiftc -profile-generate -profile-coverage-mapping testprof.swift
$ xcrun llvm-profdata merge -o testprof.profdata default.profraw
$ xcrun llvm-cov show ./testprof -instr-profile=testprof.profdata testprof.m

@kenji21
Copy link

kenji21 commented Nov 8, 2016

Got it with a iOS framework project (named CodeCoverage):

xcodebuild test -scheme CodeCoeverage-destination name="iPhone SE" -destination-timeout 60 -derivedDataPath ./DerivedData
xcrun llvm-cov show DerivedData/Build/Intermediates/CodeCoverage/Products/Debug-iphonesimulator/CodeCoverage.framework/CodeCoverage -instr-profile=DerivedData/Build/Intermediates/CodeCoverage/Coverage.profdata

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment