Skip to content

Commit a21c77b

Browse files
committed
Simplify parseSanitizerWeightedValue
and add clamp
1 parent be0f4e8 commit a21c77b

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

clang/lib/Basic/Sanitizers.cpp

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "llvm/ADT/SmallVector.h"
1616
#include "llvm/ADT/StringSwitch.h"
1717
#include "llvm/Support/MathExtras.h"
18+
#include <algorithm>
1819

1920
using namespace clang;
2021

@@ -46,26 +47,20 @@ bool clang::parseSanitizerWeightedValue(StringRef Value, bool AllowGroups,
4647
#include "clang/Basic/Sanitizers.def"
4748
.Default(SanitizerMask());
4849

49-
if (ParsedKind) {
50-
size_t equalsIndex = Value.find_first_of('=');
51-
if (equalsIndex != llvm::StringLiteral::npos) {
52-
double arg;
53-
if ((Value.size() > (equalsIndex + 1)) &&
54-
!Value.substr(equalsIndex + 1).getAsDouble(arg)) {
55-
// AllowGroups is already taken into account for ParsedKind,
56-
// hence we unconditionally expandSanitizerGroups.
57-
SanitizerMask ExpandedKind = expandSanitizerGroups(ParsedKind);
58-
59-
for (unsigned int i = 0; i < SanitizerKind::SO_Count; i++)
60-
if (ExpandedKind & SanitizerMask::bitPosToMask(i))
61-
Cutoffs[i] = arg;
62-
63-
return true;
64-
}
65-
}
66-
}
67-
68-
return false;
50+
if (!ParsedKind)
51+
return false;
52+
auto [N, W] = Value.split('=');
53+
double A;
54+
if (W.getAsDouble(A))
55+
return false;
56+
float C = std::clamp()(A, 0.0, 1.0);
57+
// AllowGroups is already taken into account for ParsedKind,
58+
// hence we unconditionally expandSanitizerGroups.
59+
SanitizerMask ExpandedKind = expandSanitizerGroups(ParsedKind);
60+
for (unsigned int i = 0; i < SanitizerKind::SO_Count; i++)
61+
if (ExpandedKind & SanitizerMask::bitPosToMask(i))
62+
Cutoffs[i] = C;
63+
return true;
6964
}
7065

7166
void clang::serializeSanitizerSet(SanitizerSet Set,

0 commit comments

Comments
 (0)