| 1 | #include "gtest/gtest.h" |
| 2 | #include "clang/Basic/SourceLocation.h" |
| 3 | #include "clang/Basic/SourceManager.h" |
| 4 | #include "clang/Basic/Diagnostic.h" |
| 5 | #include "clang/Basic/FileManager.h" |
| 6 | #include "clang/Analysis/Analyses/UnsafeBufferUsage.h" |
| 7 | |
| 8 | using namespace clang; |
| 9 | |
| 10 | namespace { |
| 11 | // The test fixture. |
| 12 | class UnsafeBufferUsageTest : public ::testing::Test { |
| 13 | protected: |
| 14 | UnsafeBufferUsageTest() |
| 15 | : FileMgr(FileMgrOpts), DiagID(new DiagnosticIDs()), |
| 16 | Diags(DiagID, DiagOpts, new IgnoringDiagConsumer()), |
| 17 | SourceMgr(Diags, FileMgr) {} |
| 18 | |
| 19 | FileSystemOptions FileMgrOpts; |
| 20 | FileManager FileMgr; |
| 21 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID; |
| 22 | DiagnosticOptions DiagOpts; |
| 23 | DiagnosticsEngine Diags; |
| 24 | SourceManager SourceMgr; |
| 25 | }; |
| 26 | } // namespace |
| 27 | |
| 28 | TEST_F(UnsafeBufferUsageTest, FixItHintsConflict) { |
| 29 | FileEntryRef DummyFile = FileMgr.getVirtualFileRef(Filename: "<virtual>" , Size: 100, ModificationTime: 0); |
| 30 | FileID DummyFileID = SourceMgr.getOrCreateFileID(SourceFile: DummyFile, FileCharacter: SrcMgr::C_User); |
| 31 | SourceLocation StartLoc = SourceMgr.getLocForStartOfFile(FID: DummyFileID); |
| 32 | |
| 33 | #define MkDummyHint(Begin, End) \ |
| 34 | FixItHint::CreateReplacement(SourceRange(StartLoc.getLocWithOffset((Begin)), \ |
| 35 | StartLoc.getLocWithOffset((End))), \ |
| 36 | "dummy") |
| 37 | |
| 38 | FixItHint H1 = MkDummyHint(0, 5); |
| 39 | FixItHint H2 = MkDummyHint(6, 10); |
| 40 | FixItHint H3 = MkDummyHint(20, 25); |
| 41 | llvm::SmallVector<FixItHint> Fixes; |
| 42 | |
| 43 | // Test non-overlapping fix-its: |
| 44 | Fixes = {H1, H2, H3}; |
| 45 | EXPECT_FALSE(internal::anyConflict(Fixes, SourceMgr)); |
| 46 | Fixes = {H3, H2, H1}; // re-order |
| 47 | EXPECT_FALSE(internal::anyConflict(Fixes, SourceMgr)); |
| 48 | |
| 49 | // Test overlapping fix-its: |
| 50 | Fixes = {H1, H2, H3, MkDummyHint(0, 4) /* included in H1 */}; |
| 51 | EXPECT_TRUE(internal::anyConflict(Fixes, SourceMgr)); |
| 52 | |
| 53 | Fixes = {H1, H2, H3, MkDummyHint(10, 15) /* overlaps H2 */}; |
| 54 | EXPECT_TRUE(internal::anyConflict(Fixes, SourceMgr)); |
| 55 | |
| 56 | Fixes = {H1, H2, H3, MkDummyHint(7, 23) /* overlaps H2, H3 */}; |
| 57 | EXPECT_TRUE(internal::anyConflict(Fixes, SourceMgr)); |
| 58 | |
| 59 | Fixes = {H1, H2, H3, MkDummyHint(2, 23) /* overlaps H1, H2, and H3 */}; |
| 60 | EXPECT_TRUE(internal::anyConflict(Fixes, SourceMgr)); |
| 61 | } |