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