[clang] [-Wunsafe-buffer-usage] Introduce std::array fixits (PR #80084)
Artem Dergachev via cfe-commits
cfe-commits at lists.llvm.org
Thu Feb 1 13:36:57 PST 2024
================
@@ -2495,10 +2470,113 @@ static FixItList fixVariableWithSpan(const VarDecl *VD,
return fixLocalVarDeclWithSpan(VD, Ctx, getUserFillPlaceHolder(), Handler);
}
+static FixItList fixVarDeclWithArray(const VarDecl *D, const ASTContext &Ctx,
+ UnsafeBufferUsageHandler &Handler) {
+ FixItList FixIts{};
+
+ if (auto CAT = dyn_cast<clang::ConstantArrayType>(D->getType())) {
+ const QualType &ArrayEltT = CAT->getElementType();
+ assert(!ArrayEltT.isNull() && "Trying to fix a non-array type variable!");
+
+ // For most types the transformation is simple:
+ // T foo[10]; => std::array<T, 10> foo;
+ // Cv-specifiers are straigtforward:
+ // const T foo[10]; => std::array<const T, 10> foo;
+ // Pointers are straightforward:
+ // T * foo[10]; => std::array<T *, 10> foo;
+ //
+ // However, for const pointers the transformation is different:
+ // T * const foo[10]; => const std::array<T *, 10> foo;
----------------
haoNoQ wrote:
Hmm, wouldn't `std::array<T *const, 10>` have the exact same behavior in practice? (Though I completely agree that `const std::array<T *, 10>` is more readable.) (Testbed: https://godbolt.org/z/aeohe6jW9)
https://github.com/llvm/llvm-project/pull/80084
More information about the cfe-commits
mailing list