1 //===- unittests/Serialization/SourceLocationEncodingTests.cpp ------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "clang/Serialization/SourceLocationEncoding.h"
10
11 #include "gtest/gtest.h"
12 #include <climits>
13
14 using namespace llvm;
15 using namespace clang;
16
17 namespace {
18 using LocSeq = SourceLocationSequence;
19
20 // Convert a single source location into encoded form and back.
21 // If ExpectedEncoded is provided, verify the encoded value too.
22 // Loc is the raw (in-memory) form of SourceLocation.
roundTrip(SourceLocation::UIntTy Loc,llvm::Optional<uint64_t> ExpectedEncoded=llvm::None)23 void roundTrip(SourceLocation::UIntTy Loc,
24 llvm::Optional<uint64_t> ExpectedEncoded = llvm::None) {
25 uint64_t ActualEncoded =
26 SourceLocationEncoding::encode(SourceLocation::getFromRawEncoding(Loc));
27 if (ExpectedEncoded)
28 ASSERT_EQ(ActualEncoded, *ExpectedEncoded) << "Encoding " << Loc;
29 SourceLocation::UIntTy DecodedEncoded =
30 SourceLocationEncoding::decode(ActualEncoded).getRawEncoding();
31 ASSERT_EQ(DecodedEncoded, Loc) << "Decoding " << ActualEncoded;
32 }
33
34 // As above, but use sequence encoding for a series of locations.
roundTrip(std::vector<SourceLocation::UIntTy> Locs,std::vector<uint64_t> ExpectedEncoded={})35 void roundTrip(std::vector<SourceLocation::UIntTy> Locs,
36 std::vector<uint64_t> ExpectedEncoded = {}) {
37 std::vector<uint64_t> ActualEncoded;
38 {
39 LocSeq::State Seq;
40 for (auto L : Locs)
41 ActualEncoded.push_back(SourceLocationEncoding::encode(
42 SourceLocation::getFromRawEncoding(L), Seq));
43 if (!ExpectedEncoded.empty())
44 ASSERT_EQ(ActualEncoded, ExpectedEncoded)
45 << "Encoding " << testing::PrintToString(Locs);
46 }
47 std::vector<SourceLocation::UIntTy> DecodedEncoded;
48 {
49 LocSeq::State Seq;
50 for (auto L : ActualEncoded) {
51 SourceLocation Loc = SourceLocationEncoding::decode(L, Seq);
52 DecodedEncoded.push_back(Loc.getRawEncoding());
53 }
54 ASSERT_EQ(DecodedEncoded, Locs)
55 << "Decoding " << testing::PrintToString(ActualEncoded);
56 }
57 }
58
59 constexpr SourceLocation::UIntTy MacroBit =
60 1 << (sizeof(SourceLocation::UIntTy) * CHAR_BIT - 1);
61 constexpr SourceLocation::UIntTy Big = MacroBit >> 1;
62 constexpr SourceLocation::UIntTy Biggest = -1;
63
TEST(SourceLocationEncoding,Individual)64 TEST(SourceLocationEncoding, Individual) {
65 roundTrip(1, 2);
66 roundTrip(100, 200);
67 roundTrip(MacroBit, 1);
68 roundTrip(MacroBit | 5, 11);
69 roundTrip(Big);
70 roundTrip(Big + 1);
71 roundTrip(MacroBit | Big);
72 roundTrip(MacroBit | Big + 1);
73 }
74
TEST(SourceLocationEncoding,Sequence)75 TEST(SourceLocationEncoding, Sequence) {
76 roundTrip({1, 2, 3, 3, 2, 1},
77 {2, // 1
78 5, // +2 (+1 of non-raw)
79 5, // +2
80 1, // +0
81 4, // -2
82 4} // -2
83 );
84 roundTrip({100, 0, 100},
85 {200, // 100
86 0, // 0
87 1} // +0
88 );
89
90 roundTrip({1, Big}, {2, ((Big - 1) << 2) + 1});
91 roundTrip({2, MacroBit | Big}, {4, ((Big - 1) << 2) - 1});
92
93 roundTrip({3, MacroBit | 5, MacroBit | 4, 3},
94 {6, // 3
95 11, // +5 (+2 of non-raw + set macro bit)
96 4, // -2
97 6} // -3 (-2 of non-raw, clear macro bit)
98 );
99
100 roundTrip(
101 {123 | MacroBit, 1, 9, Biggest, Big, Big + 1, 0, MacroBit | Big, 0});
102 }
103
104 } // namespace
105