Skip to content

Commit e4c9a96

Browse files
committed
[-Wunsafe-buffer-usage][NFC] Factor out .data() fixit to a function
1 parent ed8933d commit e4c9a96

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
@@ -1492,6 +1492,9 @@ PointerAssignmentGadget::getFixits(const FixitStrategy &S) const {
14921492
return std::nullopt;
14931493
}
14941494

1495+
/// \returns fixit that adds .data() call after \DRE.
1496+
static inline std::optional<FixItList> createDataFixit(const ASTContext& Ctx, const DeclRefExpr * DRE);
1497+
14951498
std::optional<FixItList>
14961499
PointerInitGadget::getFixits(const FixitStrategy &S) const {
14971500
const auto *LeftVD = PtrInitLHS;
@@ -1909,6 +1912,18 @@ PointerDereferenceGadget::getFixits(const FixitStrategy &S) const {
19091912
return std::nullopt;
19101913
}
19111914

1915+
static inline std::optional<FixItList> createDataFixit(const ASTContext& Ctx, const DeclRefExpr * DRE) {
1916+
const SourceManager &SM = Ctx.getSourceManager();
1917+
// Inserts the .data() after the DRE
1918+
std::optional<SourceLocation> EndOfOperand =
1919+
getPastLoc(DRE, SM, Ctx.getLangOpts());
1920+
1921+
if (EndOfOperand)
1922+
return FixItList{{FixItHint::CreateInsertion(*EndOfOperand, ".data()")}};
1923+
1924+
return std::nullopt;
1925+
}
1926+
19121927
// Generates fix-its replacing an expression of the form UPC(DRE) with
19131928
// `DRE.data()`
19141929
std::optional<FixItList>
@@ -1917,14 +1932,7 @@ UPCStandalonePointerGadget::getFixits(const FixitStrategy &S) const {
19171932
switch (S.lookup(VD)) {
19181933
case FixitStrategy::Kind::Array:
19191934
case FixitStrategy::Kind::Span: {
1920-
ASTContext &Ctx = VD->getASTContext();
1921-
SourceManager &SM = Ctx.getSourceManager();
1922-
// Inserts the .data() after the DRE
1923-
std::optional<SourceLocation> EndOfOperand =
1924-
getPastLoc(Node, SM, Ctx.getLangOpts());
1925-
1926-
if (EndOfOperand)
1927-
return FixItList{{FixItHint::CreateInsertion(*EndOfOperand, ".data()")}};
1935+
return createDataFixit(VD->getASTContext(), Node);
19281936
// FIXME: Points inside a macro expansion.
19291937
break;
19301938
}

0 commit comments

Comments
 (0)