Skip to content

Commit 791130c

Browse files
committed
[-Wunsafe-buffer-usage][NFC] Factor out .data() fixit to a function
1 parent 9a1e637 commit 791130c

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

1493+
/// \returns fixit that adds .data() call after \DRE.
1494+
static inline std::optional<FixItList> createDataFixit(const ASTContext& Ctx, const DeclRefExpr * DRE);
1495+
14931496
std::optional<FixItList>
14941497
PointerInitGadget::getFixits(const FixitStrategy &S) const {
14951498
const auto *LeftVD = PtrInitLHS;
@@ -1907,6 +1910,18 @@ PointerDereferenceGadget::getFixits(const FixitStrategy &S) const {
19071910
return std::nullopt;
19081911
}
19091912

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

0 commit comments

Comments
 (0)