xref: /aosp_15_r20/external/cronet/testing/coverage_util_ios.mm (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1// Copyright 2017 The Chromium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#import <Foundation/Foundation.h>
6
7#import "testing/gtest/ios_enable_coverage.h"
8
9#if !defined(NDEBUG) && BUILDFLAG(IOS_ENABLE_COVERAGE) && \
10    TARGET_IPHONE_SIMULATOR
11extern "C" void __llvm_profile_set_filename(const char* name);
12#endif
13
14namespace coverage_util {
15
16void ConfigureCoverageReportPath() {
17// Targets won't build on real devices with BUILDFLAG(IOS_ENABLE_COVERAGE)
18// because of llvm library linking issue for arm64 architecture.
19#if !defined(NDEBUG) && BUILDFLAG(IOS_ENABLE_COVERAGE) && \
20    TARGET_IPHONE_SIMULATOR
21  static dispatch_once_t once_token;
22  dispatch_once(&once_token, ^{
23    // Writes the profraw file to the simulator shared resources directory,
24    // where the app has write rights, and will be preserved after app is
25    // killed.
26    NSString* shared_resources_path =
27        NSProcessInfo.processInfo
28            .environment[@"SIMULATOR_SHARED_RESOURCES_DIRECTORY"];
29    // UUID ensures that there won't be a conflict when multiple apps are
30    // launched in one test suite in EG2. %m enables on-line profile merging.
31    // %c helps preserve coverage data at crash.
32    NSString* file_name = [NSString
33        stringWithFormat:@"%@-%%m-%%c.profraw", NSUUID.UUID.UUIDString];
34    NSString* file_path =
35        [shared_resources_path stringByAppendingPathComponent:file_name];
36
37    // For documentation, see:
38    // http://clang.llvm.org/docs/SourceBasedCodeCoverage.html
39    __llvm_profile_set_filename(
40        [file_path cStringUsingEncoding:NSUTF8StringEncoding]);
41
42    // Print the path for easier retrieval.
43    NSLog(@"Coverage data at %@.", file_path);
44  });
45#endif  // !defined(NDEBUG) && BUILDFLAG(IOS_ENABLE_COVERAGE) &&
46        // TARGET_IPHONE_SIMULATOR
47}
48
49}  // namespace coverage_util
50