Skip to content

Commit 221d69a

Browse files
committed
chore: bitcode cleanup
1 parent 01835f4 commit 221d69a

File tree

11 files changed

+297
-405
lines changed

11 files changed

+297
-405
lines changed

src/lib/-bitcode/BitcodeGenerator.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include "lib/AST/Bitcode.hpp"
1616
#include <mrdox/Support/ThreadPool.hpp>
1717
#include <mrdox/Metadata.hpp>
18+
#include <llvm/Support/FileSystem.h>
19+
#include <llvm/Support/Path.h>
1820

1921
namespace clang {
2022
namespace mrdox {
@@ -68,7 +70,7 @@ class MultiFileBuilder
6870
auto bc = writeBitcode(I);
6971
if((ec = os.error()))
7072
Error(ec).Throw();
71-
os.write(bc.data.data(), bc.data.size());
73+
os.write(bc.data(), bc.size());
7274
if((ec = os.error()))
7375
Error(ec).Throw();
7476
});
@@ -106,7 +108,7 @@ class SingleFileBuilder
106108
operator()(T const& I)
107109
{
108110
auto bc = writeBitcode(I);
109-
os_.write(bc.data.data(), bc.data.size());
111+
os_.write(bc.data(), bc.size());
110112
if constexpr(T::isRecord())
111113
corpus_.traverse(I, *this);
112114
}

src/lib/AST/AnyBlock.hpp

Lines changed: 237 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,195 @@
1414
#define MRDOX_LIB_AST_ANYBLOCK_HPP
1515

1616
#include "BitcodeReader.hpp"
17-
#include "DecodeRecord.hpp"
18-
#include "lib/Support/Debug.hpp"
19-
#include "lib/Support/Error.hpp"
2017

2118
namespace clang {
2219
namespace mrdox {
2320

21+
// bool
22+
inline
23+
Error
24+
decodeRecord(
25+
Record const& R,
26+
bool& Field,
27+
llvm::StringRef Blob)
28+
{
29+
Field = R[0] != 0;
30+
return Error::success();
31+
}
32+
33+
// 32 bit integral types
34+
template<class IntTy>
35+
requires std::integral<IntTy>
36+
Error
37+
decodeRecord(
38+
Record const& R,
39+
IntTy& v,
40+
llvm::StringRef Blob)
41+
{
42+
v = 0;
43+
if (R[0] > (std::numeric_limits<IntTy>::max)())
44+
return formatError("integer overflow");
45+
v = static_cast<IntTy>(R[0]);
46+
return Error::success();
47+
}
48+
49+
// integral types wider than 32 bits
50+
template<class IntTy>
51+
requires (std::integral<IntTy> &&
52+
sizeof(IntTy) > 4)
53+
Error
54+
decodeRecord(
55+
Record const& R,
56+
IntTy& v,
57+
llvm::StringRef Blob)
58+
{
59+
v = static_cast<IntTy>(
60+
static_cast<std::uint64_t>(R[0]) |
61+
(static_cast<std::uint64_t>(R[1]) << 32));
62+
return Error::success();
63+
}
64+
65+
// enumerations
66+
template<class Enum>
67+
requires std::is_enum_v<Enum>
68+
Error
69+
decodeRecord(
70+
Record const& R,
71+
Enum& value,
72+
llvm::StringRef blob)
73+
{
74+
std::underlying_type_t<Enum> temp;
75+
if(auto err = decodeRecord(R, temp, blob))
76+
return err;
77+
value = static_cast<Enum>(temp);
78+
return Error::success();
79+
}
80+
81+
// container of char
82+
template<class Field>
83+
requires std::is_same_v<
84+
typename Field::value_type, char>
85+
Error
86+
decodeRecord(
87+
const Record& R,
88+
Field& f,
89+
llvm::StringRef blob)
90+
requires requires
91+
{
92+
f.assign(blob.begin(), blob.end());
93+
}
94+
{
95+
f.assign(blob.begin(), blob.end());
96+
return Error::success();
97+
}
98+
99+
// range<SymbolID>
100+
inline
101+
Error
102+
decodeRecord(
103+
Record const& R,
104+
std::vector<SymbolID>& f,
105+
llvm::StringRef blob)
106+
{
107+
auto src = R.begin();
108+
auto n = *src++;
109+
f.resize(n);
110+
auto* dest = &f[0];
111+
while(n--)
112+
{
113+
*dest++ = SymbolID(src);
114+
src += BitCodeConstants::USRHashSize;
115+
}
116+
return Error::success();
117+
}
118+
119+
inline
120+
Error
121+
decodeRecord(
122+
const Record& R,
123+
SymbolID& Field,
124+
llvm::StringRef Blob)
125+
{
126+
if (R[0] != BitCodeConstants::USRHashSize)
127+
return formatError("USR digest size={}", R[0]);
128+
129+
Field = SymbolID(&R[1]);
130+
return Error::success();
131+
}
132+
133+
inline
134+
Error
135+
decodeRecord(
136+
Record const& R,
137+
OptionalLocation& Field,
138+
llvm::StringRef Blob)
139+
{
140+
if (R[0] > INT_MAX)
141+
return formatError("integer value {} too large", R[0]);
142+
Field.emplace((int)R[0], Blob, (bool)R[1]);
143+
return Error::success();
144+
}
145+
146+
inline
147+
Error
148+
decodeRecord(
149+
Record const& R,
150+
InfoKind& Kind,
151+
llvm::StringRef Blob)
152+
{
153+
Kind = static_cast<InfoKind>(R[0]);
154+
switch(Kind)
155+
{
156+
case InfoKind::Namespace:
157+
case InfoKind::Record:
158+
case InfoKind::Function:
159+
case InfoKind::Enum:
160+
case InfoKind::Typedef:
161+
case InfoKind::Variable:
162+
case InfoKind::Field:
163+
case InfoKind::Specialization:
164+
return Error::success();
165+
default:
166+
return formatError("InfoKind is invalid");
167+
}
168+
}
169+
170+
inline
171+
Error
172+
decodeRecord(
173+
const Record& R,
174+
std::vector<Location>& Field,
175+
llvm::StringRef Blob)
176+
{
177+
if (R[0] > INT_MAX)
178+
return formatError("integer {} is too large", R[0]);
179+
Field.emplace_back((int)R[0], Blob, (bool)R[1]);
180+
return Error::success();
181+
}
182+
183+
inline
184+
Error
185+
decodeRecord(
186+
Record const& R,
187+
std::initializer_list<BitFieldFullValue*> values,
188+
llvm::StringRef Blob)
189+
{
190+
auto n = R[0];
191+
if(n != values.size())
192+
return formatError("wrong size={} for Bitfields[{}]", n, values.size());
193+
194+
auto itr = values.begin();
195+
for(std::size_t i = 0; i < values.size(); ++i)
196+
{
197+
198+
auto const v = R[i + 1];
199+
if(v > (std::numeric_limits<std::uint32_t>::max)())
200+
return formatError("{} is out of range for Bits", v);
201+
**itr++ = v;
202+
}
203+
return Error::success();
204+
}
205+
24206
//------------------------------------------------
25207

26208
struct BitcodeReader::AnyBlock
@@ -598,6 +780,58 @@ class BaseBlock
598780

599781
//------------------------------------------------
600782

783+
class EnumValueBlock :
784+
public BitcodeReader::AnyBlock
785+
{
786+
EnumValueInfo& I_;
787+
BitcodeReader& br_;
788+
789+
public:
790+
EnumValueBlock(
791+
EnumValueInfo& I,
792+
BitcodeReader& br) noexcept
793+
: I_(I)
794+
, br_(br)
795+
{
796+
}
797+
798+
Error
799+
parseRecord(Record const& R,
800+
unsigned ID, llvm::StringRef Blob) override
801+
{
802+
switch(ID)
803+
{
804+
case ENUM_VALUE_NAME:
805+
return decodeRecord(R, I_.Name, Blob);
806+
default:
807+
return AnyBlock::parseRecord(R, ID, Blob);
808+
}
809+
}
810+
811+
Error
812+
readSubBlock(
813+
unsigned ID) override
814+
{
815+
switch(ID)
816+
{
817+
case BI_JAVADOC_BLOCK_ID:
818+
{
819+
JavadocBlock B(I_.javadoc, br_);
820+
return br_.readBlock(B, ID);
821+
}
822+
case BI_EXPR_BLOCK_ID:
823+
{
824+
ExprBlock B(I_.Initializer, br_);
825+
return br_.readBlock(B, ID);
826+
}
827+
default:
828+
return AnyBlock::readSubBlock(ID);
829+
}
830+
}
831+
};
832+
833+
//------------------------------------------------
834+
601835
class TemplateArgBlock
602836
: public BitcodeReader::AnyBlock
603837
{
@@ -1204,55 +1438,6 @@ class TypedefBlock
12041438

12051439
//------------------------------------------------
12061440

1207-
class EnumValueBlock : public BitcodeReader::AnyBlock
1208-
{
1209-
EnumValueInfo& I_;
1210-
BitcodeReader& br_;
1211-
1212-
public:
1213-
EnumValueBlock(
1214-
EnumValueInfo& I,
1215-
BitcodeReader& br) noexcept
1216-
: I_(I)
1217-
, br_(br)
1218-
{
1219-
}
1220-
1221-
Error
1222-
parseRecord(Record const& R,
1223-
unsigned ID, llvm::StringRef Blob) override
1224-
{
1225-
switch(ID)
1226-
{
1227-
case ENUM_VALUE_NAME:
1228-
return decodeRecord(R, I_.Name, Blob);
1229-
default:
1230-
return AnyBlock::parseRecord(R, ID, Blob);
1231-
}
1232-
}
1233-
1234-
Error
1235-
readSubBlock(
1236-
unsigned ID) override
1237-
{
1238-
switch(ID)
1239-
{
1240-
case BI_JAVADOC_BLOCK_ID:
1241-
{
1242-
JavadocBlock B(I_.javadoc, br_);
1243-
return br_.readBlock(B, ID);
1244-
}
1245-
case BI_EXPR_BLOCK_ID:
1246-
{
1247-
ExprBlock B(I_.Initializer, br_);
1248-
return br_.readBlock(B, ID);
1249-
}
1250-
default:
1251-
return AnyBlock::readSubBlock(ID);
1252-
}
1253-
}
1254-
};
1255-
12561441
class EnumBlock
12571442
: public TopLevelBlock<EnumInfo>
12581443
{

src/lib/AST/Bitcode.cpp

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)