Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Support/CodeGenCoverage.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- lib/Support/CodeGenCoverage.cpp -------------------------------------==//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
/// \file
9
/// This file implements the CodeGenCoverage class.
10
//===----------------------------------------------------------------------===//
11
12
#include "llvm/Support/CodeGenCoverage.h"
13
14
#include "llvm/Config/llvm-config.h"
15
#include "llvm/Support/Endian.h"
16
#include "llvm/Support/FileSystem.h"
17
#include "llvm/Support/MemoryBuffer.h"
18
#include "llvm/Support/Mutex.h"
19
#include "llvm/Support/ScopedPrinter.h"
20
#include "llvm/Support/ToolOutputFile.h"
21
22
#if LLVM_ON_UNIX
23
#include <unistd.h>
24
#elif defined(_WIN32)
25
#include <windows.h>
26
#endif
27
28
using namespace llvm;
29
30
static sys::SmartMutex<true> OutputMutex;
31
32
235k
CodeGenCoverage::CodeGenCoverage() {}
33
34
0
void CodeGenCoverage::setCovered(uint64_t RuleID) {
35
0
  if (RuleCoverage.size() <= RuleID)
36
0
    RuleCoverage.resize(RuleID + 1, 0);
37
0
  RuleCoverage[RuleID] = true;
38
0
}
39
40
0
bool CodeGenCoverage::isCovered(uint64_t RuleID) const {
41
0
  if (RuleCoverage.size() <= RuleID)
42
0
    return false;
43
0
  return RuleCoverage[RuleID];
44
0
}
45
46
iterator_range<CodeGenCoverage::const_covered_iterator>
47
0
CodeGenCoverage::covered() const {
48
0
  return RuleCoverage.set_bits();
49
0
}
50
51
0
bool CodeGenCoverage::parse(MemoryBuffer &Buffer, StringRef BackendName) {
52
0
  const char *CurPtr = Buffer.getBufferStart();
53
0
54
0
  while (CurPtr != Buffer.getBufferEnd()) {
55
0
    // Read the backend name from the input.
56
0
    const char *LexedBackendName = CurPtr;
57
0
    while (*CurPtr++ != 0)
58
0
      ;
59
0
    if (CurPtr == Buffer.getBufferEnd())
60
0
      return false; // Data is invalid, expected rule id's to follow.
61
0
62
0
    bool IsForThisBackend = BackendName.equals(LexedBackendName);
63
0
    while (CurPtr != Buffer.getBufferEnd()) {
64
0
      if (std::distance(CurPtr, Buffer.getBufferEnd()) < 8)
65
0
        return false; // Data is invalid. Not enough bytes for another rule id.
66
0
67
0
      uint64_t RuleID = support::endian::read64(CurPtr, support::native);
68
0
      CurPtr += 8;
69
0
70
0
      // ~0ull terminates the rule id list.
71
0
      if (RuleID == ~0ull)
72
0
        break;
73
0
74
0
      // Anything else, is recorded or ignored depending on whether it's
75
0
      // intended for the backend we're interested in.
76
0
      if (IsForThisBackend)
77
0
        setCovered(RuleID);
78
0
    }
79
0
  }
80
0
81
0
  return true;
82
0
}
83
84
bool CodeGenCoverage::emit(StringRef CoveragePrefix,
85
223k
                           StringRef BackendName) const {
86
223k
  if (!CoveragePrefix.empty() && 
!RuleCoverage.empty()0
) {
87
0
    sys::SmartScopedLock<true> Lock(OutputMutex);
88
0
89
0
    // We can handle locking within a process easily enough but we don't want to
90
0
    // manage it between multiple processes. Use the process ID to ensure no
91
0
    // more than one process is ever writing to the same file at the same time.
92
0
    std::string Pid =
93
0
#if LLVM_ON_UNIX
94
0
        llvm::to_string(::getpid());
95
#elif defined(_WIN32)
96
        llvm::to_string(::GetCurrentProcessId());
97
#else
98
        "";
99
#endif
100
101
0
    std::string CoverageFilename = (CoveragePrefix + Pid).str();
102
0
103
0
    std::error_code EC;
104
0
    sys::fs::OpenFlags OpenFlags = sys::fs::F_Append;
105
0
    std::unique_ptr<ToolOutputFile> CoverageFile =
106
0
        llvm::make_unique<ToolOutputFile>(CoverageFilename, EC, OpenFlags);
107
0
    if (EC)
108
0
      return false;
109
0
110
0
    uint64_t Zero = 0;
111
0
    uint64_t InvZero = ~0ull;
112
0
    CoverageFile->os() << BackendName;
113
0
    CoverageFile->os().write((const char *)&Zero, sizeof(unsigned char));
114
0
    for (uint64_t I : RuleCoverage.set_bits())
115
0
      CoverageFile->os().write((const char *)&I, sizeof(uint64_t));
116
0
    CoverageFile->os().write((const char *)&InvZero, sizeof(uint64_t));
117
0
118
0
    CoverageFile->keep();
119
0
  }
120
223k
121
223k
  return true;
122
223k
}
123
124
0
void CodeGenCoverage::reset() { RuleCoverage.resize(0); }