[clang][bytecode] Fix reporting failed local constexpr initializers (#123588)
[llvm-project.git] / llvm / lib / Support / CodeGenCoverage.cpp
blob2e35019e12c168445d832fd88fec48849ebb34d0
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 //===----------------------------------------------------------------------===//
12 #include "llvm/Support/CodeGenCoverage.h"
14 #include "llvm/Support/Endian.h"
15 #include "llvm/Support/FileSystem.h"
16 #include "llvm/Support/MemoryBuffer.h"
17 #include "llvm/Support/Mutex.h"
18 #include "llvm/Support/Process.h"
19 #include "llvm/Support/ScopedPrinter.h"
20 #include "llvm/Support/ToolOutputFile.h"
22 using namespace llvm;
24 CodeGenCoverage::CodeGenCoverage() = default;
26 void CodeGenCoverage::setCovered(uint64_t RuleID) {
27 if (RuleCoverage.size() <= RuleID)
28 RuleCoverage.resize(RuleID + 1, false);
29 RuleCoverage[RuleID] = true;
32 bool CodeGenCoverage::isCovered(uint64_t RuleID) const {
33 if (RuleCoverage.size() <= RuleID)
34 return false;
35 return RuleCoverage[RuleID];
38 iterator_range<CodeGenCoverage::const_covered_iterator>
39 CodeGenCoverage::covered() const {
40 return RuleCoverage.set_bits();
43 bool CodeGenCoverage::parse(MemoryBuffer &Buffer, StringRef BackendName) {
44 const char *CurPtr = Buffer.getBufferStart();
46 while (CurPtr != Buffer.getBufferEnd()) {
47 // Read the backend name from the input.
48 const char *LexedBackendName = CurPtr;
49 while (*CurPtr++ != 0)
51 if (CurPtr == Buffer.getBufferEnd())
52 return false; // Data is invalid, expected rule id's to follow.
54 bool IsForThisBackend = BackendName == LexedBackendName;
55 while (CurPtr != Buffer.getBufferEnd()) {
56 if (std::distance(CurPtr, Buffer.getBufferEnd()) < 8)
57 return false; // Data is invalid. Not enough bytes for another rule id.
59 uint64_t RuleID =
60 support::endian::read64(CurPtr, llvm::endianness::native);
61 CurPtr += 8;
63 // ~0ull terminates the rule id list.
64 if (RuleID == ~0ull)
65 break;
67 // Anything else, is recorded or ignored depending on whether it's
68 // intended for the backend we're interested in.
69 if (IsForThisBackend)
70 setCovered(RuleID);
74 return true;
77 bool CodeGenCoverage::emit(StringRef CoveragePrefix,
78 StringRef BackendName) const {
79 if (!CoveragePrefix.empty() && !RuleCoverage.empty()) {
80 static sys::SmartMutex<true> OutputMutex;
81 sys::SmartScopedLock<true> Lock(OutputMutex);
83 // We can handle locking within a process easily enough but we don't want to
84 // manage it between multiple processes. Use the process ID to ensure no
85 // more than one process is ever writing to the same file at the same time.
86 std::string Pid = llvm::to_string(sys::Process::getProcessId());
88 std::string CoverageFilename = (CoveragePrefix + Pid).str();
90 std::error_code EC;
91 sys::fs::OpenFlags OpenFlags = sys::fs::OF_Append;
92 std::unique_ptr<ToolOutputFile> CoverageFile =
93 std::make_unique<ToolOutputFile>(CoverageFilename, EC, OpenFlags);
94 if (EC)
95 return false;
97 uint64_t Zero = 0;
98 uint64_t InvZero = ~0ull;
99 CoverageFile->os() << BackendName;
100 CoverageFile->os().write((const char *)&Zero, sizeof(unsigned char));
101 for (uint64_t I : RuleCoverage.set_bits())
102 CoverageFile->os().write((const char *)&I, sizeof(uint64_t));
103 CoverageFile->os().write((const char *)&InvZero, sizeof(uint64_t));
105 CoverageFile->keep();
108 return true;
111 void CodeGenCoverage::reset() { RuleCoverage.resize(0); }