Skip to content

Commit ed37b5f

Browse files
authored
Revert "[Support] Validate number of arguments passed to formatv()" (#106589)
Reverts #105745 Some bots are broken apparently.
1 parent 0a00d32 commit ed37b5f

File tree

7 files changed

+58
-195
lines changed

7 files changed

+58
-195
lines changed

clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,10 +1401,7 @@ void StdLibraryFunctionsChecker::checkPostCall(const CallEvent &Call,
14011401
ErrnoNote =
14021402
llvm::formatv("After calling '{0}' {1}", FunctionName, ErrnoNote);
14031403
} else {
1404-
// Disable formatv() validation as the case note may not always have the
1405-
// {0} placeholder for function name.
1406-
CaseNote =
1407-
llvm::formatv(false, Case.getNote().str().c_str(), FunctionName);
1404+
CaseNote = llvm::formatv(Case.getNote().str().c_str(), FunctionName);
14081405
}
14091406
const SVal RV = Call.getReturnValue();
14101407

llvm/benchmarks/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,3 @@ set(LLVM_LINK_COMPONENTS
55
add_benchmark(DummyYAML DummyYAML.cpp PARTIAL_SOURCES_INTENDED)
66
add_benchmark(xxhash xxhash.cpp PARTIAL_SOURCES_INTENDED)
77
add_benchmark(GetIntrinsicForClangBuiltin GetIntrinsicForClangBuiltin.cpp PARTIAL_SOURCES_INTENDED)
8-
add_benchmark(FormatVariadicBM FormatVariadicBM.cpp PARTIAL_SOURCES_INTENDED)

llvm/benchmarks/FormatVariadicBM.cpp

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

llvm/include/llvm/Support/FormatVariadic.h

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,23 @@ class formatv_object_base {
6767
protected:
6868
StringRef Fmt;
6969
ArrayRef<support::detail::format_adapter *> Adapters;
70-
bool Validate;
70+
71+
static bool consumeFieldLayout(StringRef &Spec, AlignStyle &Where,
72+
size_t &Align, char &Pad);
73+
74+
static std::pair<ReplacementItem, StringRef>
75+
splitLiteralAndReplacement(StringRef Fmt);
7176

7277
formatv_object_base(StringRef Fmt,
73-
ArrayRef<support::detail::format_adapter *> Adapters,
74-
bool Validate)
75-
: Fmt(Fmt), Adapters(Adapters), Validate(Validate) {}
78+
ArrayRef<support::detail::format_adapter *> Adapters)
79+
: Fmt(Fmt), Adapters(Adapters) {}
7680

7781
formatv_object_base(formatv_object_base const &rhs) = delete;
7882
formatv_object_base(formatv_object_base &&rhs) = default;
7983

8084
public:
8185
void format(raw_ostream &S) const {
82-
const auto Replacements = parseFormatString(Fmt, Adapters.size(), Validate);
83-
for (const auto &R : Replacements) {
86+
for (auto &R : parseFormatString(Fmt)) {
8487
if (R.Type == ReplacementType::Empty)
8588
continue;
8689
if (R.Type == ReplacementType::Literal) {
@@ -98,10 +101,9 @@ class formatv_object_base {
98101
Align.format(S, R.Options);
99102
}
100103
}
104+
static SmallVector<ReplacementItem, 2> parseFormatString(StringRef Fmt);
101105

102-
// Parse and optionally validate format string (in debug builds).
103-
static SmallVector<ReplacementItem, 2>
104-
parseFormatString(StringRef Fmt, size_t NumArgs, bool Validate);
106+
static std::optional<ReplacementItem> parseReplacementItem(StringRef Spec);
105107

106108
std::string str() const {
107109
std::string Result;
@@ -147,8 +149,8 @@ template <typename Tuple> class formatv_object : public formatv_object_base {
147149
};
148150

149151
public:
150-
formatv_object(StringRef Fmt, Tuple &&Params, bool Validate)
151-
: formatv_object_base(Fmt, ParameterPointers, Validate),
152+
formatv_object(StringRef Fmt, Tuple &&Params)
153+
: formatv_object_base(Fmt, ParameterPointers),
152154
Parameters(std::move(Params)) {
153155
ParameterPointers = std::apply(create_adapters(), Parameters);
154156
}
@@ -245,22 +247,15 @@ template <typename Tuple> class formatv_object : public formatv_object_base {
245247
// assertion. Otherwise, it will try to do something reasonable, but in general
246248
// the details of what that is are undefined.
247249
//
248-
249-
// formatv() with validation enable/disable controlled by the first argument.
250250
template <typename... Ts>
251-
inline auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
251+
inline auto formatv(const char *Fmt, Ts &&...Vals)
252252
-> formatv_object<decltype(std::make_tuple(
253253
support::detail::build_format_adapter(std::forward<Ts>(Vals))...))> {
254254
using ParamTuple = decltype(std::make_tuple(
255255
support::detail::build_format_adapter(std::forward<Ts>(Vals))...));
256-
auto Params = std::make_tuple(
257-
support::detail::build_format_adapter(std::forward<Ts>(Vals))...);
258-
return formatv_object<ParamTuple>(Fmt, std::move(Params), Validate);
259-
}
260-
261-
// formatv() with validation enabled.
262-
template <typename... Ts> inline auto formatv(const char *Fmt, Ts &&...Vals) {
263-
return formatv<Ts...>(true, Fmt, std::forward<Ts>(Vals)...);
256+
return formatv_object<ParamTuple>(
257+
Fmt, std::make_tuple(support::detail::build_format_adapter(
258+
std::forward<Ts>(Vals))...));
264259
}
265260

266261
} // end namespace llvm

llvm/lib/Support/FormatVariadic.cpp

Lines changed: 13 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ static std::optional<AlignStyle> translateLocChar(char C) {
2525
LLVM_BUILTIN_UNREACHABLE;
2626
}
2727

28-
static bool consumeFieldLayout(StringRef &Spec, AlignStyle &Where,
29-
size_t &Align, char &Pad) {
28+
bool formatv_object_base::consumeFieldLayout(StringRef &Spec, AlignStyle &Where,
29+
size_t &Align, char &Pad) {
3030
Where = AlignStyle::Right;
3131
Align = 0;
3232
Pad = ' ';
@@ -35,7 +35,8 @@ static bool consumeFieldLayout(StringRef &Spec, AlignStyle &Where,
3535

3636
if (Spec.size() > 1) {
3737
// A maximum of 2 characters at the beginning can be used for something
38-
// other than the width.
38+
// other
39+
// than the width.
3940
// If Spec[1] is a loc char, then Spec[0] is a pad char and Spec[2:...]
4041
// contains the width.
4142
// Otherwise, if Spec[0] is a loc char, then Spec[1:...] contains the width.
@@ -54,7 +55,8 @@ static bool consumeFieldLayout(StringRef &Spec, AlignStyle &Where,
5455
return !Failed;
5556
}
5657

57-
static std::optional<ReplacementItem> parseReplacementItem(StringRef Spec) {
58+
std::optional<ReplacementItem>
59+
formatv_object_base::parseReplacementItem(StringRef Spec) {
5860
StringRef RepString = Spec.trim("{}");
5961

6062
// If the replacement sequence does not start with a non-negative integer,
@@ -80,14 +82,15 @@ static std::optional<ReplacementItem> parseReplacementItem(StringRef Spec) {
8082
RepString = StringRef();
8183
}
8284
RepString = RepString.trim();
83-
assert(RepString.empty() &&
84-
"Unexpected characters found in replacement string!");
85+
if (!RepString.empty()) {
86+
assert(false && "Unexpected characters found in replacement string!");
87+
}
8588

8689
return ReplacementItem{Spec, Index, Align, Where, Pad, Options};
8790
}
8891

89-
static std::pair<ReplacementItem, StringRef>
90-
splitLiteralAndReplacement(StringRef Fmt) {
92+
std::pair<ReplacementItem, StringRef>
93+
formatv_object_base::splitLiteralAndReplacement(StringRef Fmt) {
9194
while (!Fmt.empty()) {
9295
// Everything up until the first brace is a literal.
9396
if (Fmt.front() != '{') {
@@ -140,77 +143,15 @@ splitLiteralAndReplacement(StringRef Fmt) {
140143
return std::make_pair(ReplacementItem{Fmt}, StringRef());
141144
}
142145

143-
#ifndef NDEBUG
144-
#define ENABLE_VALIDATION 1
145-
#else
146-
#define ENABLE_VALIDATION 0 // Conveniently enable validation in release mode.
147-
#endif
148-
149146
SmallVector<ReplacementItem, 2>
150-
formatv_object_base::parseFormatString(StringRef Fmt, size_t NumArgs,
151-
bool Validate) {
147+
formatv_object_base::parseFormatString(StringRef Fmt) {
152148
SmallVector<ReplacementItem, 2> Replacements;
153-
154-
#if ENABLE_VALIDATION
155-
const StringRef SavedFmtStr = Fmt;
156-
size_t NumExpectedArgs = 0;
157-
#endif
158-
149+
ReplacementItem I;
159150
while (!Fmt.empty()) {
160-
ReplacementItem I;
161151
std::tie(I, Fmt) = splitLiteralAndReplacement(Fmt);
162152
if (I.Type != ReplacementType::Empty)
163153
Replacements.push_back(I);
164-
#if ENABLE_VALIDATION
165-
if (I.Type == ReplacementType::Format)
166-
NumExpectedArgs = std::max(NumExpectedArgs, I.Index + 1);
167-
#endif
168-
}
169-
170-
#if ENABLE_VALIDATION
171-
if (!Validate)
172-
return Replacements;
173-
174-
// Perform additional validation. Verify that the number of arguments matches
175-
// the number of replacement indices and that there are no holes in the
176-
// replacement indices.
177-
178-
// When validation fails, return an array of replacement items that
179-
// will print an error message as the outout of this formatv() (used when
180-
// validation is enabled in release mode).
181-
auto getErrorReplacements = [SavedFmtStr](StringLiteral ErrorMsg) {
182-
return SmallVector<ReplacementItem, 2>{
183-
ReplacementItem("Invalid formatv() call: "), ReplacementItem(ErrorMsg),
184-
ReplacementItem(" for format string: "), ReplacementItem(SavedFmtStr)};
185-
};
186-
187-
if (NumExpectedArgs != NumArgs) {
188-
errs() << formatv(
189-
"Expected {0} Args, but got {1} for format string '{2}'\n",
190-
NumExpectedArgs, NumArgs, SavedFmtStr);
191-
assert(0 && "Invalid formatv() call");
192-
return getErrorReplacements("Unexpected number of arguments");
193-
}
194-
195-
// Find the number of unique indices seen. All replacement indices
196-
// are < NumExpectedArgs.
197-
SmallVector<bool> Indices(NumExpectedArgs);
198-
size_t Count = 0;
199-
for (const ReplacementItem &I : Replacements) {
200-
if (I.Type != ReplacementType::Format || Indices[I.Index])
201-
continue;
202-
Indices[I.Index] = true;
203-
++Count;
204-
}
205-
206-
if (Count != NumExpectedArgs) {
207-
errs() << formatv(
208-
"Replacement field indices cannot have holes for format string '{0}'\n",
209-
SavedFmtStr);
210-
assert(0 && "Invalid format string");
211-
return getErrorReplacements("Replacement indices have holes");
212154
}
213-
#endif // ENABLE_VALIDATION
214155
return Replacements;
215156
}
216157

0 commit comments

Comments
 (0)