Android defaults to pic (#123955)
[llvm-project.git] / clang / unittests / Serialization / SourceLocationEncodingTest.cpp
blobc80a8fd0e52b17b9108f891f0057dd662d5ba2d3
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 //===----------------------------------------------------------------------===//
9 #include "clang/Serialization/SourceLocationEncoding.h"
11 #include "gtest/gtest.h"
12 #include <climits>
13 #include <optional>
15 using namespace llvm;
16 using namespace clang;
18 namespace {
19 using LocSeq = SourceLocationSequence;
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 = SourceLocationEncoding::encode(
27 SourceLocation::getFromRawEncoding(Loc), /*BaseOffset=*/0,
28 /*BaseModuleFileIndex=*/0);
29 if (ExpectedEncoded) {
30 ASSERT_EQ(ActualEncoded, *ExpectedEncoded) << "Encoding " << Loc;
32 SourceLocation::UIntTy DecodedEncoded =
33 SourceLocationEncoding::decode(ActualEncoded).first.getRawEncoding();
34 ASSERT_EQ(DecodedEncoded, Loc) << "Decoding " << ActualEncoded;
37 // As above, but use sequence encoding for a series of locations.
38 void roundTrip(std::vector<SourceLocation::UIntTy> Locs,
39 std::vector<uint64_t> ExpectedEncoded = {}) {
40 std::vector<uint64_t> ActualEncoded;
42 LocSeq::State Seq;
43 for (auto L : Locs)
44 ActualEncoded.push_back(SourceLocationEncoding::encode(
45 SourceLocation::getFromRawEncoding(L), /*BaseOffset=*/0,
46 /*BaseModuleFileIndex=*/0, Seq));
47 if (!ExpectedEncoded.empty()) {
48 ASSERT_EQ(ActualEncoded, ExpectedEncoded)
49 << "Encoding " << testing::PrintToString(Locs);
52 std::vector<SourceLocation::UIntTy> DecodedEncoded;
54 LocSeq::State Seq;
55 for (auto L : ActualEncoded) {
56 SourceLocation Loc = SourceLocationEncoding::decode(L, Seq).first;
57 DecodedEncoded.push_back(Loc.getRawEncoding());
59 ASSERT_EQ(DecodedEncoded, Locs)
60 << "Decoding " << testing::PrintToString(ActualEncoded);
64 constexpr SourceLocation::UIntTy MacroBit =
65 1 << (sizeof(SourceLocation::UIntTy) * CHAR_BIT - 1);
66 constexpr SourceLocation::UIntTy Big = MacroBit >> 1;
67 constexpr SourceLocation::UIntTy Biggest = -1;
69 TEST(SourceLocationEncoding, Individual) {
70 roundTrip(1, 2);
71 roundTrip(100, 200);
72 roundTrip(MacroBit, 1);
73 roundTrip(MacroBit | 5, 11);
74 roundTrip(Big);
75 roundTrip(Big + 1);
76 roundTrip(MacroBit | Big);
77 roundTrip(MacroBit | (Big + 1));
80 TEST(SourceLocationEncoding, Sequence) {
81 roundTrip({1, 2, 3, 3, 2, 1},
82 {2, // 1
83 5, // +2 (+1 of non-raw)
84 5, // +2
85 1, // +0
86 4, // -2
87 4} // -2
89 roundTrip({100, 0, 100},
90 {200, // 100
91 0, // 0
92 1} // +0
95 roundTrip({1, Big}, {2, ((Big - 1) << 2) + 1});
96 roundTrip({2, MacroBit | Big}, {4, ((Big - 1) << 2) - 1});
98 roundTrip({3, MacroBit | 5, MacroBit | 4, 3},
99 {6, // 3
100 11, // +5 (+2 of non-raw + set macro bit)
101 4, // -2
102 6} // -3 (-2 of non-raw, clear macro bit)
105 roundTrip(
106 {123 | MacroBit, 1, 9, Biggest, Big, Big + 1, 0, MacroBit | Big, 0});
109 } // namespace