llvm/llvm/unittests/IR/ValueHandleTest.cpp

//===- ValueHandleTest.cpp - ValueHandle tests ----------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/IR/ValueHandle.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "gtest/gtest.h"
#include <memory>

usingnamespacellvm;

namespace {

class ValueHandle : public testing::Test {};

class ConcreteCallbackVH final : public CallbackVH {};

TEST_F(ValueHandle, WeakVH_BasicOperation) {}

TEST_F(ValueHandle, WeakTrackingVH_BasicOperation) {}

TEST_F(ValueHandle, WeakTrackingVH_Comparisons) {}

TEST_F(ValueHandle, WeakTrackingVH_FollowsRAUW) {}

TEST_F(ValueHandle, WeakTrackingVH_NullOnDeletion) {}


TEST_F(ValueHandle, AssertingVH_BasicOperation) {}

TEST_F(ValueHandle, AssertingVH_Const) {}

TEST_F(ValueHandle, AssertingVH_Comparisons) {}

TEST_F(ValueHandle, AssertingVH_DoesNotFollowRAUW) {}

#ifdef NDEBUG

TEST_F(ValueHandle, AssertingVH_ReducesToPointer) {}

#elif LLVM_ENABLE_ABI_BREAKING_CHECKS // && !NDEBUG

#ifdef GTEST_HAS_DEATH_TEST

TEST_F(ValueHandle, AssertingVH_Asserts) {
  AssertingVH<Value> AVH(BitcastV.get());
  EXPECT_DEATH({BitcastV.reset();},
               "An asserting value handle still pointed to this value!");
  AssertingVH<Value> Copy(AVH);
  AVH = nullptr;
  EXPECT_DEATH({BitcastV.reset();},
               "An asserting value handle still pointed to this value!");
  Copy = nullptr;
  BitcastV.reset();
}

#endif  // GTEST_HAS_DEATH_TEST

#endif  // NDEBUG

TEST_F(ValueHandle, CallbackVH_BasicOperation) {}

TEST_F(ValueHandle, CallbackVH_Comparisons) {}

TEST_F(ValueHandle, CallbackVH_CallbackOnDeletion) {}

TEST_F(ValueHandle, CallbackVH_CallbackOnRAUW) {}

TEST_F(ValueHandle, CallbackVH_DeletionCanRAUW) {}

TEST_F(ValueHandle, DestroyingOtherVHOnSameValueDoesntBreakIteration) {}

TEST_F(ValueHandle, AssertingVHCheckedLast) {}

TEST_F(ValueHandle, PoisoningVH_BasicOperation) {}

TEST_F(ValueHandle, PoisoningVH_Const) {}

TEST_F(ValueHandle, PoisoningVH_Comparisons) {}

TEST_F(ValueHandle, PoisoningVH_DoesNotFollowRAUW) {}

TEST_F(ValueHandle, AssertingVH_DenseMap) {}

TEST_F(ValueHandle, PoisoningVH_DenseMap) {}

#ifdef NDEBUG

TEST_F(ValueHandle, PoisoningVH_ReducesToPointer) {}

#else // !NDEBUG

TEST_F(ValueHandle, TrackingVH_Tracks) {
  TrackingVH<Value> VH(BitcastV.get());
  BitcastV->replaceAllUsesWith(ConstantV);
  EXPECT_EQ(VH, ConstantV);
}

#ifdef GTEST_HAS_DEATH_TEST
#if LLVM_ENABLE_ABI_BREAKING_CHECKS

TEST_F(ValueHandle, PoisoningVH_Asserts) {
  PoisoningVH<Value> VH(BitcastV.get());

  // The poisoned handle shouldn't assert when the value is deleted.
  BitcastV.reset(new BitCastInst(ConstantV, Type::getInt32Ty(Context)));
  // But should when we access the handle.
  EXPECT_DEATH((void)*VH, "Accessed a poisoned value handle!");

  // Now check that poison catches RAUW.
  VH = BitcastV.get();
  // The replace doesn't trigger anything immediately.
  BitcastV->replaceAllUsesWith(ConstantV);
  // But a use does.
  EXPECT_DEATH((void)*VH, "Accessed a poisoned value handle!");

  // Don't clear anything out here as destroying the handles should be fine.
}

#endif // LLVM_ENABLE_ABI_BREAKING_CHECKS

TEST_F(ValueHandle, TrackingVH_Asserts) {
  {
    TrackingVH<Value> VH(BitcastV.get());

    // The tracking handle shouldn't assert when the value is deleted.
    BitcastV.reset(new BitCastInst(ConstantV, Type::getInt32Ty(Context)));
    // But should when we access the handle.
    EXPECT_DEATH((void)*VH,
                 "TrackingVH must be non-null and valid on dereference!");
  }

  {
    TrackingVH<Instruction> VH(BitcastV.get());

    BitcastV->replaceAllUsesWith(ConstantV);
    EXPECT_DEATH((void)*VH,
                 "Tracked Value was replaced by one with an invalid type!");
  }
}

#endif // GTEST_HAS_DEATH_TEST

#endif // NDEBUG
}