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