1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109

base / memory / ref_counted_nocompile.nc [blame]

// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/memory/ref_counted.h"
#include "base/memory/ref_counted_delete_on_sequence.h"

namespace base {

class Base : public RefCounted<Base> {
 private:
  friend class RefCounted<Base>;

  ~Base() = default;
};

class ThreadSafeBase : public RefCountedThreadSafe<ThreadSafeBase> {
 private:
  friend class RefCountedThreadSafe<ThreadSafeBase>;

  ~ThreadSafeBase() = default;
};

class DeleteOnSequenceBase
    : public RefCountedDeleteOnSequence<DeleteOnSequenceBase> {
 private:
  friend class RefCountedDeleteOnSequence<DeleteOnSequenceBase>;
  friend class DeleteHelper<DeleteOnSequenceBase>;

  ~DeleteOnSequenceBase() = default;
};

void AdoptRefToZeroStart() {
  class InitialRefCountIsZero : public RefCounted<InitialRefCountIsZero> {
   public:
    InitialRefCountIsZero() = default;

   private:
    friend class RefCounted<InitialRefCountIsZero>;

    ~InitialRefCountIsZero() = default;
  };

  AdoptRef(new InitialRefCountIsZero());  // expected-error@*:* {{Use AdoptRef only if the reference count starts from one.}}
}

void WrongRefcountBaseClass() {
  class Derived : public RefCounted<Base> {
   private:
    friend class RefCounted<Derived>;

    ~Derived() = default;
  };

  scoped_refptr<Derived> ptr;  // expected-error@*:* {{T implements RefCounted<U>, but U is not a base of T.}}
}

void WrongRefcountThreadsafeBaseClass() {
  class Derived : public RefCountedThreadSafe<ThreadSafeBase> {
   private:
    friend class RefCountedThreadSafe<Derived>;

    ~Derived() = default;
  };

  scoped_refptr<Derived> ptr;  // expected-error@*:* {{T implements RefCountedThreadSafe<U>, but U is not a base of T.}}
}

void WrongRefcountDeleteOnSequenceBaseClass() {
  class DeleteOnSequenceDerived
      : public RefCountedDeleteOnSequence<DeleteOnSequenceBase> {
   private:
    friend class RefCountedDeleteOnSequence<DeleteOnSequenceDerived>;
    friend class DeleteHelper<DeleteOnSequenceDerived>;

    ~DeleteOnSequenceDerived() = default;
  };

  scoped_refptr<DeleteOnSequenceDerived> ptr;  // expected-error@*:* {{T implements RefCountedDeleteOnSequence<U>, but U is not a base of T.}}
}

void SubclassOverridesRefcountPreference() {
  class Derived : public Base {
   public:
    REQUIRE_ADOPTION_FOR_REFCOUNTED_TYPE();
  };

  scoped_refptr<Derived> ptr;  // expected-error@*:* {{It's unsafe to override the ref count preference. Please remove REQUIRE_ADOPTION_FOR_REFCOUNTED_TYPE from subclasses.}}
}

void SubclassOverridesRefcountPreferenceThreadsafe() {
  class Derived : public ThreadSafeBase {
   public:
    REQUIRE_ADOPTION_FOR_REFCOUNTED_TYPE();
  };

  scoped_refptr<Derived> ptr;  // expected-error@*:* {{It's unsafe to override the ref count preference. Please remove REQUIRE_ADOPTION_FOR_REFCOUNTED_TYPE from subclasses.}}
}

void SubclassOverridesRefcountPreferenceDeleteOnSequence() {
  class Derived : public DeleteOnSequenceBase {
   public:
    REQUIRE_ADOPTION_FOR_REFCOUNTED_TYPE();
  };

  scoped_refptr<Derived> ptr;  // expected-error@*:* {{It's unsafe to override the ref count preference. Please remove REQUIRE_ADOPTION_FOR_REFCOUNTED_TYPE from subclasses.}}
}

}  // namespace base