[PATCH] D54757: [clang-tidy] new check: bugprone-branch-clone
Kristóf Umann via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri May 3 08:52:18 PDT 2019
Szelethus updated this revision to Diff 198025.
Szelethus added a comment.
FIx the above mentioned crash.
diff --git a/clang-tidy/bugprone/BranchCloneCheck.cpp b/clang-tidy/bugprone/BranchCloneCheck.cpp
index f371231..a898311 100644
--- a/clang-tidy/bugprone/BranchCloneCheck.cpp
+++ b/clang-tidy/bugprone/BranchCloneCheck.cpp
@@ -197,8 +197,19 @@ void BranchCloneCheck::check(const MatchFinder::MatchResult &Result) {
diag(BeginCurrent->front()->getBeginLoc(),
"switch has %0 consecutive identical branches")
<< static_cast<int>(std::distance(BeginCurrent, EndCurrent));
- diag(Lexer::getLocForEndOfToken((EndCurrent - 1)->back()->getEndLoc(),
- 0, *Result.SourceManager,
+
+ SourceLocation EndLoc = (EndCurrent - 1)->back()->getEndLoc();
+ // If the case statement is generated from a macro, it's SourceLocation
+ // may be invalid, resuling in an assertation failure down the line.
+ // While not optimal, try the begin location in this case, it's still
+ // better then nothing.
+ if (EndLoc.isInvalid())
+ EndLoc = (EndCurrent - 1)->back()->getBeginLoc();
+
+ if (EndLoc.isMacroID())
+ EndLoc = Context.getSourceManager().getExpansionLoc(EndLoc);
+
+ diag(Lexer::getLocForEndOfToken(EndLoc, 0, *Result.SourceManager,
getLangOpts()),
"last of these clones ends here", DiagnosticIDs::Note);
}
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D54757/new/
https://reviews.llvm.org/D54757
Files:
clang-tidy/bugprone/BranchCloneCheck.cpp
clang-tidy/bugprone/BranchCloneCheck.h
clang-tidy/bugprone/BugproneTidyModule.cpp
clang-tidy/bugprone/CMakeLists.txt
docs/ReleaseNotes.rst
docs/clang-tidy/checks/bugprone-branch-clone.rst
docs/clang-tidy/checks/list.rst
test/clang-tidy/bugprone-branch-clone.cpp
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D54757.198025.patch
Type: text/x-patch
Size: 44055 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190503/49945d17/attachment-0001.bin>
More information about the cfe-commits
mailing list