Skip to content

Commit 93a0d32

Browse files
author
ahyush
committed
Improved reinterpret cast score + detection (llvm#35)
Only bitcasts are considered now (fixes a lot of spurious detections). Score considers actual type instead of cleaned type; actual type is relevant for reinterpret_cast.
1 parent 01b313b commit 93a0d32

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

clang/tools/clang-tools-extra/cast-chk/PatternDetection.h

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,22 @@ std::string cleanType(CensusKey const &opKey) {
101101

102102
using Score_t = std::unordered_map<CensusKey, TypeScore>;
103103

104-
void recordEdgeScore(CensusKey const &from, CensusKey const &to, Score_t scores) {
104+
void recordEdgeScore(CensusKey const &from, CensusKey const &to, Score_t scores, bool useCleanType = true) {
105105
auto const logKey = from + " -> " + to;
106106
CNS_DEBUG_MSG(logKey, "begin");
107107

108-
scores.at(from).addOutType(cleanType(to));
109-
CNS_DEBUG(logKey, "Updated out score for '{}': {}", from, scores.at(from).outScore());
108+
auto recordType = [&useCleanType](auto const &key) {
109+
if(useCleanType) {
110+
return cleanType(key);
111+
}
112+
return ops(key).type_;
113+
};
114+
115+
scores.at(from).addOutType(recordType(to));
116+
CNS_DEBUG(logKey, "Added type '{}'; Updated out score for '{}': {}", recordType(to), from, scores.at(from).outScore());
110117

111-
scores.at(to).addInType(cleanType(from));
112-
CNS_DEBUG(logKey, "Updated in score for '{}': {}", to, scores.at(to).inScore());
118+
scores.at(to).addInType(recordType(from));
119+
CNS_DEBUG(logKey, "Added type '{}'; Updated in score for '{}': {}", recordType(from), to, scores.at(to).inScore());
113120

114121
CNS_DEBUG_MSG(logKey, "end");
115122
}
@@ -149,9 +156,15 @@ inline void propagateGenericScore(CensusKey const &from, CensusKey const &to) {
149156
}
150157

151158
bool hasReinterpretCast(CensusKey const &from, CensusKey const &to, DominatorData const &linkInfo) {
159+
/*
152160
if(isTransformThroughMember(linkInfo)) {
153161
return false;
154162
}
163+
*/
164+
165+
if(linkInfo.castKind() != "BitCast") {
166+
return false;
167+
}
155168

156169
auto isRelevantNumber = [](auto const &op) {
157170
return isNumeric(op.qn_)
@@ -191,18 +204,18 @@ void scoreSummary(TypeSummary const &ts) {
191204

192205
if(!isTransformThroughMember(to.linkInfo())) {
193206
recordEdgeScore(ts.key(), to.key(), SummarizedGenericScores);
194-
195-
// reinterpret
196-
if(hasReinterpretCast(ts.key(), to.key(), to.linkInfo())) {
197-
recordEdgeScore(ts.key(), to.key(), SummarizedReinterpretScores);
198-
}
199207
}
200208

201209
auto const &from = ops(ts.key());
202210
if(from.type_ == "void *") {
203211
propagateGenericScore(ts.key(), to.key());
204212
}
205213

214+
// reinterpret
215+
if(hasReinterpretCast(ts.key(), to.key(), to.linkInfo())) {
216+
recordEdgeScore(ts.key(), to.key(), SummarizedReinterpretScores, false);
217+
}
218+
206219
scoreSummary(to);
207220

208221
}

0 commit comments

Comments
 (0)