Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 47 additions & 8 deletions clang-tools-extra/clang-tidy/readability/UseStdMinMaxCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,11 @@ static QualType getReplacementCastType(const Expr *CondLhs, const Expr *CondRhs,
return GlobalImplicitCastType;
}

static std::string createReplacement(const Expr *CondLhs, const Expr *CondRhs,
const Expr *AssignLhs,
const SourceManager &Source,
const LangOptions &LO,
StringRef FunctionName,
const BinaryOperator *BO) {
static std::string
createReplacement(const Expr *CondLhs, const Expr *CondRhs,
const Expr *AssignLhs, const SourceManager &Source,
const LangOptions &LO, StringRef FunctionName,
const BinaryOperator *BO, StringRef Comment = "") {
const llvm::StringRef CondLhsStr = Lexer::getSourceText(
Source.getExpansionRange(CondLhs->getSourceRange()), Source, LO);
const llvm::StringRef CondRhsStr = Lexer::getSourceText(
Expand All @@ -116,7 +115,8 @@ static std::string createReplacement(const Expr *CondLhs, const Expr *CondRhs,
(!GlobalImplicitCastType.isNull()
? "<" + GlobalImplicitCastType.getAsString() + ">("
: "(") +
CondLhsStr + ", " + CondRhsStr + ");")
CondLhsStr + ", " + CondRhsStr + ");" + (Comment.empty() ? "" : " ") +
Comment)
.str();
}

Expand Down Expand Up @@ -172,13 +172,52 @@ void UseStdMinMaxCheck::check(const MatchFinder::MatchResult &Result) {

auto ReplaceAndDiagnose = [&](const llvm::StringRef FunctionName) {
const SourceManager &Source = *Result.SourceManager;
llvm::SmallString<64> Comment;

const auto AppendNormalized = [&](llvm::StringRef Text) {
Text = Text.ltrim();
if (!Text.empty()) {
if (!Comment.empty())
Comment += " ";
Comment += Text;
}
};

AppendNormalized(Lexer::getSourceText(
CharSourceRange::getCharRange(
Lexer::getLocForEndOfToken(If->getRParenLoc(), 0, Source, LO),
If->getThen()->getBeginLoc()),
Source, LO));

if (const auto *CS = dyn_cast<CompoundStmt>(If->getThen())) {
const Stmt *Inner = CS->body_front();
AppendNormalized(Lexer::getSourceText(
CharSourceRange::getCharRange(
Lexer::getLocForEndOfToken(CS->getBeginLoc(), 0, Source, LO),
Inner->getBeginLoc()),
Source, LO));

llvm::StringRef PostInner = Lexer::getSourceText(
CharSourceRange::getCharRange(
Lexer::getLocForEndOfToken(Inner->getEndLoc(), 0, Source, LO),
CS->getEndLoc()),
Source, LO);

const size_t Semi = PostInner.find(';');
if (Semi != llvm::StringRef::npos &&
PostInner.take_front(Semi).trim().empty()) {
PostInner = PostInner.drop_front(Semi + 1);
}
AppendNormalized(PostInner);
}

diag(IfLocation, "use `%0` instead of `%1`")
<< FunctionName << BinaryOp->getOpcodeStr()
<< FixItHint::CreateReplacement(
SourceRange(IfLocation, Lexer::getLocForEndOfToken(
ThenLocation, 0, Source, LO)),
createReplacement(CondLhs, CondRhs, AssignLhs, Source, LO,
FunctionName, BinaryOp))
FunctionName, BinaryOp, Comment))
<< IncludeInserter.createIncludeInsertion(
Source.getFileID(If->getBeginLoc()), AlgorithmHeader);
};
Expand Down
5 changes: 5 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,11 @@ Changes in existing checks
<clang-tidy/checks/readability/use-concise-preprocessor-directives>` check to
generate correct fix-its for forms without a space after the directive.

- Improved :doc:`readability-use-std-min-max
<clang-tidy/checks/readability/use-std-min-max>` check by ensuring that
comments between the ``if`` condition and the ``then`` block are preserved
when applying the fix.

Removed checks
^^^^^^^^^^^^^^

Expand Down
Loading