diff options
| author | Haojian Wu <hokein@google.com> | 2019-04-18 14:18:14 +0000 |
|---|---|---|
| committer | Haojian Wu <hokein@google.com> | 2019-04-18 14:18:14 +0000 |
| commit | 8bbbd31cdd4affa0a23d5022fe2f2c27bb2d124f (patch) | |
| tree | ecb305f0ff3842c3732d4c3711617d9bbf9b2579 | |
| parent | b8f82ca1b2b232cb84c66e72efecfb51bca517fb (diff) | |
| download | bcm5719-llvm-8bbbd31cdd4affa0a23d5022fe2f2c27bb2d124f.tar.gz bcm5719-llvm-8bbbd31cdd4affa0a23d5022fe2f2c27bb2d124f.zip | |
[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
| -rw-r--r-- | clang-tools-extra/clang-tidy/ClangTidy.cpp | 61 | ||||
| -rw-r--r-- | clang-tools-extra/test/clang-tidy/alternative-fixes.cpp | 9 | ||||
| -rw-r--r-- | clang/include/clang/Tooling/Core/Diagnostic.h | 4 |
3 files changed, 41 insertions, 33 deletions
diff --git a/clang-tools-extra/clang-tidy/ClangTidy.cpp b/clang-tools-extra/clang-tidy/ClangTidy.cpp index 6b8428bd929..1d813d65f8d 100644 --- a/clang-tools-extra/clang-tidy/ClangTidy.cpp +++ b/clang-tools-extra/clang-tidy/ClangTidy.cpp @@ -133,41 +133,40 @@ public: for (const auto &Repl : FileAndReplacements.second) { ++TotalFixes; bool CanBeApplied = false; - if (Repl.isApplicable()) { - SourceLocation FixLoc; - SmallString<128> FixAbsoluteFilePath = Repl.getFilePath(); - Files.makeAbsolutePath(FixAbsoluteFilePath); - tooling::Replacement R(FixAbsoluteFilePath, Repl.getOffset(), - Repl.getLength(), - Repl.getReplacementText()); - Replacements &Replacements = FileReplacements[R.getFilePath()]; - llvm::Error Err = Replacements.add(R); - if (Err) { - // FIXME: Implement better conflict handling. - llvm::errs() << "Trying to resolve conflict: " - << llvm::toString(std::move(Err)) << "\n"; - unsigned NewOffset = - Replacements.getShiftedCodePosition(R.getOffset()); - unsigned NewLength = Replacements.getShiftedCodePosition( - R.getOffset() + R.getLength()) - - NewOffset; - if (NewLength == R.getLength()) { - R = Replacement(R.getFilePath(), NewOffset, NewLength, - R.getReplacementText()); - Replacements = Replacements.merge(tooling::Replacements(R)); - CanBeApplied = true; - ++AppliedFixes; - } else { - llvm::errs() - << "Can't resolve conflict, skipping the replacement.\n"; - } - } else { + if (!Repl.isApplicable()) + continue; + SourceLocation FixLoc; + SmallString<128> FixAbsoluteFilePath = Repl.getFilePath(); + Files.makeAbsolutePath(FixAbsoluteFilePath); + tooling::Replacement R(FixAbsoluteFilePath, Repl.getOffset(), + Repl.getLength(), Repl.getReplacementText()); + Replacements &Replacements = FileReplacements[R.getFilePath()]; + llvm::Error Err = Replacements.add(R); + if (Err) { + // FIXME: Implement better conflict handling. + llvm::errs() << "Trying to resolve conflict: " + << llvm::toString(std::move(Err)) << "\n"; + unsigned NewOffset = + Replacements.getShiftedCodePosition(R.getOffset()); + unsigned NewLength = Replacements.getShiftedCodePosition( + R.getOffset() + R.getLength()) - + NewOffset; + if (NewLength == R.getLength()) { + R = Replacement(R.getFilePath(), NewOffset, NewLength, + R.getReplacementText()); + Replacements = Replacements.merge(tooling::Replacements(R)); CanBeApplied = true; ++AppliedFixes; + } else { + llvm::errs() + << "Can't resolve conflict, skipping the replacement.\n"; } - FixLoc = getLocation(FixAbsoluteFilePath, Repl.getOffset()); - FixLocations.push_back(std::make_pair(FixLoc, CanBeApplied)); + } else { + CanBeApplied = true; + ++AppliedFixes; } + FixLoc = getLocation(FixAbsoluteFilePath, Repl.getOffset()); + FixLocations.push_back(std::make_pair(FixLoc, CanBeApplied)); } } } diff --git a/clang-tools-extra/test/clang-tidy/alternative-fixes.cpp b/clang-tools-extra/test/clang-tidy/alternative-fixes.cpp new file mode 100644 index 00000000000..d5cee68d9b1 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/alternative-fixes.cpp @@ -0,0 +1,9 @@ +// RUN: %check_clang_tidy %s "llvm-namespace-comment,clang-diagnostic-*" %t +void foo(int a) { + if (a = 1) { + // CHECK-NOTES: [[@LINE-1]]:9: warning: using the result of an assignment as a condition without parentheses [clang-diagnostic-parentheses] + // CHECK-NOTES: [[@LINE-2]]:9: note: place parentheses around the assignment to silence this warning + // CHECK-NOTES: [[@LINE-3]]:9: note: use '==' to turn this assignment into an equality comparison + // CHECK-FIXES: if ((a = 1)) { + } +} diff --git a/clang/include/clang/Tooling/Core/Diagnostic.h b/clang/include/clang/Tooling/Core/Diagnostic.h index 0491d1355d0..4e0feba6d7d 100644 --- a/clang/include/clang/Tooling/Core/Diagnostic.h +++ b/clang/include/clang/Tooling/Core/Diagnostic.h @@ -93,8 +93,8 @@ struct TranslationUnitDiagnostics { std::vector<Diagnostic> Diagnostics; }; -// Get the first fix to apply for this diagnostic. -// Return nullptr if no fixes attached to the diagnostic. +/// Get the first fix to apply for this diagnostic. +/// \returns nullptr if no fixes are attached to the diagnostic. const llvm::StringMap<Replacements> *selectFirstFix(const Diagnostic& D); } // end namespace tooling |

