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"
12 class UnsafeBufferUsageTest
: public ::testing::Test
{
14 UnsafeBufferUsageTest()
15 : FileMgr(FileMgrOpts
), DiagID(new DiagnosticIDs()),
16 Diags(DiagID
, new DiagnosticOptions
, new IgnoringDiagConsumer()),
17 SourceMgr(Diags
, FileMgr
) {}
19 FileSystemOptions FileMgrOpts
;
21 IntrusiveRefCntPtr
<DiagnosticIDs
> DiagID
;
22 DiagnosticsEngine Diags
;
23 SourceManager SourceMgr
;
27 TEST_F(UnsafeBufferUsageTest
, FixItHintsConflict
) {
28 FileEntryRef DummyFile
= FileMgr
.getVirtualFileRef("<virtual>", 100, 0);
29 FileID DummyFileID
= SourceMgr
.getOrCreateFileID(DummyFile
, SrcMgr::C_User
);
30 SourceLocation StartLoc
= SourceMgr
.getLocForStartOfFile(DummyFileID
);
32 #define MkDummyHint(Begin, End) \
33 FixItHint::CreateReplacement(SourceRange(StartLoc.getLocWithOffset((Begin)), \
34 StartLoc.getLocWithOffset((End))), \
37 FixItHint H1
= MkDummyHint(0, 5);
38 FixItHint H2
= MkDummyHint(6, 10);
39 FixItHint H3
= MkDummyHint(20, 25);
40 llvm::SmallVector
<FixItHint
> Fixes
;
42 // Test non-overlapping fix-its:
44 EXPECT_FALSE(internal::anyConflict(Fixes
, SourceMgr
));
45 Fixes
= {H3
, H2
, H1
}; // re-order
46 EXPECT_FALSE(internal::anyConflict(Fixes
, SourceMgr
));
48 // Test overlapping fix-its:
49 Fixes
= {H1
, H2
, H3
, MkDummyHint(0, 4) /* included in H1 */};
50 EXPECT_TRUE(internal::anyConflict(Fixes
, SourceMgr
));
52 Fixes
= {H1
, H2
, H3
, MkDummyHint(10, 15) /* overlaps H2 */};
53 EXPECT_TRUE(internal::anyConflict(Fixes
, SourceMgr
));
55 Fixes
= {H1
, H2
, H3
, MkDummyHint(7, 23) /* overlaps H2, H3 */};
56 EXPECT_TRUE(internal::anyConflict(Fixes
, SourceMgr
));
58 Fixes
= {H1
, H2
, H3
, MkDummyHint(2, 23) /* overlaps H1, H2, and H3 */};
59 EXPECT_TRUE(internal::anyConflict(Fixes
, SourceMgr
));