[clang] 8298899 - [ASTMatchers][NFC] Use SmallVector when building variadic matcher descriptor
Nathan James via cfe-commits
cfe-commits at lists.llvm.org
Tue Mar 23 09:38:59 PDT 2021
Author: Nathan James
Date: 2021-03-23T16:38:45Z
New Revision: 8298899e56cdf89c68215853010352c45398ab10
URL: https://github.com/llvm/llvm-project/commit/8298899e56cdf89c68215853010352c45398ab10
DIFF: https://github.com/llvm/llvm-project/commit/8298899e56cdf89c68215853010352c45398ab10.diff
LOG: [ASTMatchers][NFC] Use SmallVector when building variadic matcher descriptor
Saves having to manually deallocate storage and keeps InnerArgs will have good cache locality.
Reviewed By: aaron.ballman
Differential Revision: https://reviews.llvm.org/D99106
Added:
Modified:
clang/lib/ASTMatchers/Dynamic/Marshallers.h
Removed:
################################################################################
diff --git a/clang/lib/ASTMatchers/Dynamic/Marshallers.h b/clang/lib/ASTMatchers/Dynamic/Marshallers.h
index 3ffa0d6af217..783fb203c408 100644
--- a/clang/lib/ASTMatchers/Dynamic/Marshallers.h
+++ b/clang/lib/ASTMatchers/Dynamic/Marshallers.h
@@ -492,9 +492,11 @@ template <typename ResultT, typename ArgT,
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 @@ variadicMatcherDescriptor(StringRef MatcherName, SourceRange NameRange,
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 @@ variadicMatcherDescriptor(StringRef MatcherName, SourceRange NameRange,
<< (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.
More information about the cfe-commits
mailing list