Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[-Wunsafe-buffer-usage] Filter out conflicting fix-its
Two fix-its conflict if they have overlapping source ranges. We shall not emit conflicting fix-its. This patch checks conflicts in fix-its generated for one variable (including variable declaration fix-its and variable usage fix-its). If there is any, we do NOT emit any fix-it for that variable. Reviewed by: NoQ Differential revision: https://reviews.llvm.org/D141338
- Loading branch information
1 parent
b1d8f40
commit 692da62
Showing
4 changed files
with
104 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#include "gtest/gtest.h" | ||
#include "clang/Basic/SourceLocation.h" | ||
#include "clang/Basic/SourceManager.h" | ||
#include "clang/Basic/Diagnostic.h" | ||
#include "clang/Basic/FileManager.h" | ||
#include "clang/Analysis/Analyses/UnsafeBufferUsage.h" | ||
|
||
using namespace clang; | ||
|
||
namespace { | ||
// The test fixture. | ||
class UnsafeBufferUsageTest : public ::testing::Test { | ||
protected: | ||
UnsafeBufferUsageTest() | ||
: FileMgr(FileMgrOpts), DiagID(new DiagnosticIDs()), | ||
Diags(DiagID, new DiagnosticOptions, new IgnoringDiagConsumer()), | ||
SourceMgr(Diags, FileMgr) {} | ||
|
||
FileSystemOptions FileMgrOpts; | ||
FileManager FileMgr; | ||
IntrusiveRefCntPtr<DiagnosticIDs> DiagID; | ||
DiagnosticsEngine Diags; | ||
SourceManager SourceMgr; | ||
}; | ||
} // namespace | ||
|
||
TEST_F(UnsafeBufferUsageTest, FixItHintsConflict) { | ||
const FileEntry *DummyFile = FileMgr.getVirtualFile("<virtual>", 100, 0); | ||
FileID DummyFileID = SourceMgr.getOrCreateFileID(DummyFile, SrcMgr::C_User); | ||
SourceLocation StartLoc = SourceMgr.getLocForStartOfFile(DummyFileID); | ||
|
||
#define MkDummyHint(Begin, End) \ | ||
FixItHint::CreateReplacement(SourceRange(StartLoc.getLocWithOffset((Begin)), \ | ||
StartLoc.getLocWithOffset((End))), \ | ||
"dummy") | ||
|
||
FixItHint H1 = MkDummyHint(0, 5); | ||
FixItHint H2 = MkDummyHint(6, 10); | ||
FixItHint H3 = MkDummyHint(20, 25); | ||
llvm::SmallVector<FixItHint> Fixes; | ||
|
||
// Test non-overlapping fix-its: | ||
Fixes = {H1, H2, H3}; | ||
EXPECT_FALSE(internal::anyConflict(Fixes, SourceMgr)); | ||
Fixes = {H3, H2, H1}; // re-order | ||
EXPECT_FALSE(internal::anyConflict(Fixes, SourceMgr)); | ||
|
||
// Test overlapping fix-its: | ||
Fixes = {H1, H2, H3, MkDummyHint(0, 4) /* included in H1 */}; | ||
EXPECT_TRUE(internal::anyConflict(Fixes, SourceMgr)); | ||
|
||
Fixes = {H1, H2, H3, MkDummyHint(10, 15) /* overlaps H2 */}; | ||
EXPECT_TRUE(internal::anyConflict(Fixes, SourceMgr)); | ||
|
||
Fixes = {H1, H2, H3, MkDummyHint(7, 23) /* overlaps H2, H3 */}; | ||
EXPECT_TRUE(internal::anyConflict(Fixes, SourceMgr)); | ||
|
||
Fixes = {H1, H2, H3, MkDummyHint(2, 23) /* overlaps H1, H2, and H3 */}; | ||
EXPECT_TRUE(internal::anyConflict(Fixes, SourceMgr)); | ||
} |