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, 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
28TEST_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}

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

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