File: UnsafeBufferUsageTest.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (60 lines) | stat: -rw-r--r-- 2,177 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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));
}