Skip to content

Commit 9183a63

Browse files
committed
[-Wunsafe-buffer-usage] Clang-format std::array fixits work
1 parent fd791cc commit 9183a63

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

clang/lib/Analysis/UnsafeBufferUsage.cpp

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,13 +1886,13 @@ UPCStandalonePointerGadget::getFixits(const FixitStrategy &S) const {
18861886
return FixItList{{FixItHint::CreateInsertion(*EndOfOperand, ".data()")}};
18871887
// FIXME: Points inside a macro expansion.
18881888
break;
1889-
}
1890-
case FixitStrategy::Kind::Wontfix:
1891-
case FixitStrategy::Kind::Iterator:
1892-
case FixitStrategy::Kind::Array:
1893-
return std::nullopt;
1894-
case FixitStrategy::Kind::Vector:
1895-
llvm_unreachable("unsupported strategies for FixableGadgets");
1889+
}
1890+
case FixitStrategy::Kind::Wontfix:
1891+
case FixitStrategy::Kind::Iterator:
1892+
case FixitStrategy::Kind::Array:
1893+
return std::nullopt;
1894+
case FixitStrategy::Kind::Vector:
1895+
llvm_unreachable("unsupported strategies for FixableGadgets");
18961896
}
18971897

18981898
return std::nullopt;
@@ -2008,7 +2008,6 @@ UPCPreIncrementGadget::getFixits(const FixitStrategy &S) const {
20082008
return std::nullopt; // Not in the cases that we can handle for now, give up.
20092009
}
20102010

2011-
20122011
// For a non-null initializer `Init` of `T *` type, this function returns
20132012
// `FixItHint`s producing a list initializer `{Init, S}` as a part of a fix-it
20142013
// to output stream.
@@ -2497,22 +2496,30 @@ static FixItList fixVarDeclWithArray(const VarDecl *D, const ASTContext &Ctx,
24972496

24982497
const SourceLocation IdentifierLoc = getVarDeclIdentifierLoc(D);
24992498

2500-
// Get the spelling of the element type as written in the source file (including macros, etc.).
2501-
auto MaybeElemTypeTxt = getRangeText({D->getBeginLoc(), IdentifierLoc}, Ctx.getSourceManager(), Ctx.getLangOpts());
2499+
// Get the spelling of the element type as written in the source file
2500+
// (including macros, etc.).
2501+
auto MaybeElemTypeTxt =
2502+
getRangeText({D->getBeginLoc(), IdentifierLoc}, Ctx.getSourceManager(),
2503+
Ctx.getLangOpts());
25022504
if (!MaybeElemTypeTxt)
25032505
return {};
25042506
const llvm::StringRef ElemTypeTxt = MaybeElemTypeTxt->trim();
25052507

25062508
// Find the '[' token.
2507-
std::optional<Token> NextTok = Lexer::findNextToken(IdentifierLoc, Ctx.getSourceManager(), Ctx.getLangOpts());
2509+
std::optional<Token> NextTok = Lexer::findNextToken(
2510+
IdentifierLoc, Ctx.getSourceManager(), Ctx.getLangOpts());
25082511
while (NextTok && !NextTok->is(tok::l_square))
2509-
NextTok = Lexer::findNextToken(NextTok->getLocation(), Ctx.getSourceManager(), Ctx.getLangOpts());
2512+
NextTok = Lexer::findNextToken(NextTok->getLocation(),
2513+
Ctx.getSourceManager(), Ctx.getLangOpts());
25102514
if (!NextTok)
25112515
return {};
25122516
const SourceLocation LSqBracketLoc = NextTok->getLocation();
25132517

2514-
// Get the spelling of the array size as written in the source file (including macros, etc.).
2515-
auto MaybeArraySizeTxt = getRangeText({LSqBracketLoc.getLocWithOffset(1), D->getTypeSpecEndLoc()}, Ctx.getSourceManager(), Ctx.getLangOpts());
2518+
// Get the spelling of the array size as written in the source file
2519+
// (including macros, etc.).
2520+
auto MaybeArraySizeTxt = getRangeText(
2521+
{LSqBracketLoc.getLocWithOffset(1), D->getTypeSpecEndLoc()},
2522+
Ctx.getSourceManager(), Ctx.getLangOpts());
25162523
if (!MaybeArraySizeTxt)
25172524
return {};
25182525
const llvm::StringRef ArraySizeTxt = MaybeArraySizeTxt->trim();
@@ -2521,7 +2528,8 @@ static FixItList fixVarDeclWithArray(const VarDecl *D, const ASTContext &Ctx,
25212528
// Examples:
25222529
// int arr1[] = {0, 1, 2};
25232530
// int arr2{3, 4, 5};
2524-
// We might be able to preserve the non-specified size with `auto` and `std::to_array`:
2531+
// We might be able to preserve the non-specified size with `auto` and
2532+
// `std::to_array`:
25252533
// auto arr1 = std::to_array<int>({0, 1, 2});
25262534
return {};
25272535
}
@@ -2536,8 +2544,8 @@ static FixItList fixVarDeclWithArray(const VarDecl *D, const ASTContext &Ctx,
25362544

25372545
SmallString<32> Replacement;
25382546
raw_svector_ostream OS(Replacement);
2539-
OS << "std::array<" << ElemTypeTxt << ", "
2540-
<< ArraySizeTxt << "> " << IdentText->str();
2547+
OS << "std::array<" << ElemTypeTxt << ", " << ArraySizeTxt << "> "
2548+
<< IdentText->str();
25412549

25422550
FixIts.push_back(FixItHint::CreateReplacement(
25432551
SourceRange{D->getBeginLoc(), D->getTypeSpecEndLoc()}, OS.str()));

0 commit comments

Comments
 (0)