coverage_util_ios.mm 1.9 KB

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