52 lines · cpp
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-exception6//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 SourceLocation::getFromRawEncoding(Loc), /*BaseOffset=*/0,27 /*BaseModuleFileIndex=*/0);28 if (ExpectedEncoded) {29 ASSERT_EQ(ActualEncoded, *ExpectedEncoded) << "Encoding " << Loc;30 }31 SourceLocation::UIntTy DecodedEncoded =32 SourceLocationEncoding::decode(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(1, 2);42 roundTrip(100, 200);43 roundTrip(MacroBit, 1);44 roundTrip(MacroBit | 5, 11);45 roundTrip(Big);46 roundTrip(Big + 1);47 roundTrip(MacroBit | Big);48 roundTrip(MacroBit | (Big + 1));49}50 51} // namespace52