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
15using namespace llvm;
16using namespace clang;
17
18namespace {
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.
23void roundTrip(SourceLocation::UIntTy Loc,
24 std::optional<uint64_t> ExpectedEncoded = std::nullopt) {
25 uint64_t ActualEncoded = SourceLocationEncoding::encode(
26 Loc: SourceLocation::getFromRawEncoding(Encoding: Loc), /*BaseOffset=*/0,
27 /*BaseModuleFileIndex=*/0);
28 if (ExpectedEncoded) {
29 ASSERT_EQ(ActualEncoded, *ExpectedEncoded) << "Encoding " << Loc;
30 }
31 SourceLocation::UIntTy DecodedEncoded =
32 SourceLocationEncoding::decode(Encoded: ActualEncoded).first.getRawEncoding();
33 ASSERT_EQ(DecodedEncoded, Loc) << "Decoding " << ActualEncoded;
34}
35
36constexpr SourceLocation::UIntTy MacroBit =
37 1 << (sizeof(SourceLocation::UIntTy) * CHAR_BIT - 1);
38constexpr SourceLocation::UIntTy Big = MacroBit >> 1;
39
40TEST(SourceLocationEncoding, Individual) {
41 roundTrip(Loc: 1, ExpectedEncoded: 2);
42 roundTrip(Loc: 100, ExpectedEncoded: 200);
43 roundTrip(Loc: MacroBit, ExpectedEncoded: 1);
44 roundTrip(Loc: MacroBit | 5, ExpectedEncoded: 11);
45 roundTrip(Loc: Big);
46 roundTrip(Loc: Big + 1);
47 roundTrip(Loc: MacroBit | Big);
48 roundTrip(Loc: MacroBit | (Big + 1));
49}
50
51} // namespace
52

source code of clang/unittests/Serialization/SourceLocationEncodingTest.cpp