[PATCH] D99106: [ASTMatchers][NFC] Use SmallVector when building variadic matcher descriptor
Nathan James via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Mar 22 12:56:54 PDT 2021
njames93 created this revision.
njames93 added reviewers: aaron.ballman, klimek, steveire.
njames93 requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
Saves having to manually deallocate storage and keeps InnerArgs will have good cache locality.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D99106
Files:
clang/lib/ASTMatchers/Dynamic/Marshallers.h
Index: clang/lib/ASTMatchers/Dynamic/Marshallers.h
===================================================================
--- clang/lib/ASTMatchers/Dynamic/Marshallers.h
+++ clang/lib/ASTMatchers/Dynamic/Marshallers.h
@@ -492,9 +492,11 @@
VariantMatcher
variadicMatcherDescriptor(StringRef MatcherName, SourceRange NameRange,
ArrayRef<ParserValue> Args, Diagnostics *Error) {
- ArgT **InnerArgs = new ArgT *[Args.size()]();
+ SmallVector<ArgT *, 8> InnerArgsPtr;
+ InnerArgsPtr.resize_for_overwrite(Args.size());
+ SmallVector<ArgT, 8> InnerArgs;
+ InnerArgs.reserve(Args.size());
- bool HasError = false;
for (size_t i = 0, e = Args.size(); i != e; ++i) {
using ArgTraits = ArgTypeTraits<ArgT>;
@@ -503,8 +505,7 @@
if (!ArgTraits::hasCorrectType(Value)) {
Error->addError(Arg.Range, Error->ET_RegistryWrongArgType)
<< (i + 1) << ArgTraits::getKind().asString() << Value.getTypeAsString();
- HasError = true;
- break;
+ return {};
}
if (!ArgTraits::hasCorrectValue(Value)) {
if (llvm::Optional<std::string> BestGuess =
@@ -521,24 +522,12 @@
<< (i + 1) << ArgTraits::getKind().asString()
<< Value.getTypeAsString();
}
- HasError = true;
- break;
+ return {};
}
-
- InnerArgs[i] = new ArgT(ArgTraits::get(Value));
- }
-
- VariantMatcher Out;
- if (!HasError) {
- Out = outvalueToVariantMatcher(Func(llvm::makeArrayRef(InnerArgs,
- Args.size())));
+ InnerArgs.set_size(i + 1);
+ InnerArgsPtr[i] = new (&InnerArgs[i]) ArgT(ArgTraits::get(Value));
}
-
- for (size_t i = 0, e = Args.size(); i != e; ++i) {
- delete InnerArgs[i];
- }
- delete[] InnerArgs;
- return Out;
+ return outvalueToVariantMatcher(Func(InnerArgsPtr));
}
/// Matcher descriptor for variadic functions.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D99106.332404.patch
Type: text/x-patch
Size: 1907 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210322/3f2a43d8/attachment.bin>
More information about the cfe-commits
mailing list