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

source code of clang/unittests/Analysis/UnsafeBufferUsageTest.cpp