Skip to content

Commit 8bbbd31

Browse files
committedApr 18, 2019
[clang-tidy] Address post-commit comments
Summary: Also add a test to verify clang-tidy only apply the first alternative fix. Reviewers: alexfh Subscribers: xazax.hun, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D60857 llvm-svn: 358666
1 parent b8f82ca commit 8bbbd31

File tree

3 files changed

+41
-33
lines changed

3 files changed

+41
-33
lines changed
 

‎clang-tools-extra/clang-tidy/ClangTidy.cpp

+30-31
Original file line numberDiff line numberDiff line change
@@ -133,41 +133,40 @@ class ErrorReporter {
133133
for (const auto &Repl : FileAndReplacements.second) {
134134
++TotalFixes;
135135
bool CanBeApplied = false;
136-
if (Repl.isApplicable()) {
137-
SourceLocation FixLoc;
138-
SmallString<128> FixAbsoluteFilePath = Repl.getFilePath();
139-
Files.makeAbsolutePath(FixAbsoluteFilePath);
140-
tooling::Replacement R(FixAbsoluteFilePath, Repl.getOffset(),
141-
Repl.getLength(),
142-
Repl.getReplacementText());
143-
Replacements &Replacements = FileReplacements[R.getFilePath()];
144-
llvm::Error Err = Replacements.add(R);
145-
if (Err) {
146-
// FIXME: Implement better conflict handling.
147-
llvm::errs() << "Trying to resolve conflict: "
148-
<< llvm::toString(std::move(Err)) << "\n";
149-
unsigned NewOffset =
150-
Replacements.getShiftedCodePosition(R.getOffset());
151-
unsigned NewLength = Replacements.getShiftedCodePosition(
152-
R.getOffset() + R.getLength()) -
153-
NewOffset;
154-
if (NewLength == R.getLength()) {
155-
R = Replacement(R.getFilePath(), NewOffset, NewLength,
156-
R.getReplacementText());
157-
Replacements = Replacements.merge(tooling::Replacements(R));
158-
CanBeApplied = true;
159-
++AppliedFixes;
160-
} else {
161-
llvm::errs()
162-
<< "Can't resolve conflict, skipping the replacement.\n";
163-
}
164-
} else {
136+
if (!Repl.isApplicable())
137+
continue;
138+
SourceLocation FixLoc;
139+
SmallString<128> FixAbsoluteFilePath = Repl.getFilePath();
140+
Files.makeAbsolutePath(FixAbsoluteFilePath);
141+
tooling::Replacement R(FixAbsoluteFilePath, Repl.getOffset(),
142+
Repl.getLength(), Repl.getReplacementText());
143+
Replacements &Replacements = FileReplacements[R.getFilePath()];
144+
llvm::Error Err = Replacements.add(R);
145+
if (Err) {
146+
// FIXME: Implement better conflict handling.
147+
llvm::errs() << "Trying to resolve conflict: "
148+
<< llvm::toString(std::move(Err)) << "\n";
149+
unsigned NewOffset =
150+
Replacements.getShiftedCodePosition(R.getOffset());
151+
unsigned NewLength = Replacements.getShiftedCodePosition(
152+
R.getOffset() + R.getLength()) -
153+
NewOffset;
154+
if (NewLength == R.getLength()) {
155+
R = Replacement(R.getFilePath(), NewOffset, NewLength,
156+
R.getReplacementText());
157+
Replacements = Replacements.merge(tooling::Replacements(R));
165158
CanBeApplied = true;
166159
++AppliedFixes;
160+
} else {
161+
llvm::errs()
162+
<< "Can't resolve conflict, skipping the replacement.\n";
167163
}
168-
FixLoc = getLocation(FixAbsoluteFilePath, Repl.getOffset());
169-
FixLocations.push_back(std::make_pair(FixLoc, CanBeApplied));
164+
} else {
165+
CanBeApplied = true;
166+
++AppliedFixes;
170167
}
168+
FixLoc = getLocation(FixAbsoluteFilePath, Repl.getOffset());
169+
FixLocations.push_back(std::make_pair(FixLoc, CanBeApplied));
171170
}
172171
}
173172
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %check_clang_tidy %s "llvm-namespace-comment,clang-diagnostic-*" %t
2+
void foo(int a) {
3+
if (a = 1) {
4+
// CHECK-NOTES: [[@LINE-1]]:9: warning: using the result of an assignment as a condition without parentheses [clang-diagnostic-parentheses]
5+
// CHECK-NOTES: [[@LINE-2]]:9: note: place parentheses around the assignment to silence this warning
6+
// CHECK-NOTES: [[@LINE-3]]:9: note: use '==' to turn this assignment into an equality comparison
7+
// CHECK-FIXES: if ((a = 1)) {
8+
}
9+
}

‎clang/include/clang/Tooling/Core/Diagnostic.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ struct TranslationUnitDiagnostics {
9393
std::vector<Diagnostic> Diagnostics;
9494
};
9595

96-
// Get the first fix to apply for this diagnostic.
97-
// Return nullptr if no fixes attached to the diagnostic.
96+
/// Get the first fix to apply for this diagnostic.
97+
/// \returns nullptr if no fixes are attached to the diagnostic.
9898
const llvm::StringMap<Replacements> *selectFirstFix(const Diagnostic& D);
9999

100100
} // end namespace tooling

0 commit comments

Comments
 (0)
Please sign in to comment.