Skip to content

Commit 46166ba

Browse files
committed
[-Wunsafe-buffer-usage][NFC] Factor out .data() fixit to a function
1 parent 2d0f4cb commit 46166ba

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

clang/lib/Analysis/UnsafeBufferUsage.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,6 +1453,9 @@ PointerAssignmentGadget::getFixits(const FixitStrategy &S) const {
14531453
return std::nullopt;
14541454
}
14551455

1456+
/// \returns fixit that adds .data() call after \DRE.
1457+
static inline std::optional<FixItList> createDataFixit(const ASTContext& Ctx, const DeclRefExpr * DRE);
1458+
14561459
std::optional<FixItList>
14571460
PointerInitGadget::getFixits(const FixitStrategy &S) const {
14581461
const auto *LeftVD = PtrInitLHS;
@@ -1870,6 +1873,18 @@ PointerDereferenceGadget::getFixits(const FixitStrategy &S) const {
18701873
return std::nullopt;
18711874
}
18721875

1876+
static inline std::optional<FixItList> createDataFixit(const ASTContext& Ctx, const DeclRefExpr * DRE) {
1877+
const SourceManager &SM = Ctx.getSourceManager();
1878+
// Inserts the .data() after the DRE
1879+
std::optional<SourceLocation> EndOfOperand =
1880+
getPastLoc(DRE, SM, Ctx.getLangOpts());
1881+
1882+
if (EndOfOperand)
1883+
return FixItList{{FixItHint::CreateInsertion(*EndOfOperand, ".data()")}};
1884+
1885+
return std::nullopt;
1886+
}
1887+
18731888
// Generates fix-its replacing an expression of the form UPC(DRE) with
18741889
// `DRE.data()`
18751890
std::optional<FixItList>
@@ -1878,14 +1893,7 @@ UPCStandalonePointerGadget::getFixits(const FixitStrategy &S) const {
18781893
switch (S.lookup(VD)) {
18791894
case FixitStrategy::Kind::Array:
18801895
case FixitStrategy::Kind::Span: {
1881-
ASTContext &Ctx = VD->getASTContext();
1882-
SourceManager &SM = Ctx.getSourceManager();
1883-
// Inserts the .data() after the DRE
1884-
std::optional<SourceLocation> EndOfOperand =
1885-
getPastLoc(Node, SM, Ctx.getLangOpts());
1886-
1887-
if (EndOfOperand)
1888-
return FixItList{{FixItHint::CreateInsertion(*EndOfOperand, ".data()")}};
1896+
return createDataFixit(VD->getASTContext(), Node);
18891897
// FIXME: Points inside a macro expansion.
18901898
break;
18911899
}

0 commit comments

Comments
 (0)