derived-to-base.cpp 10.3 KB
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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -verify %s
// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -DCONSTRUCTORS=1 -analyzer-config c++-inlining=constructors -verify %s

void clang_analyzer_eval(bool);
void clang_analyzer_checkInlined(bool);

class A {
protected:
  int x;
};

class B : public A {
public:
  void f();
};

void B::f() {
  x = 3;
}


class C : public B {
public:
  void g() {
    // This used to crash because we are upcasting through two bases.
    x = 5;
  }
};


namespace VirtualBaseClasses {
  class A {
  protected:
    int x;
  };

  class B : public virtual A {
  public:
    int getX() { return x; }
  };

  class C : public virtual A {
  public:
    void setX() { x = 42; }
  };

  class D : public B, public C {};
  class DV : virtual public B, public C {};
  class DV2 : public B, virtual public C {};

  void test() {
    D d;
    d.setX();
    clang_analyzer_eval(d.getX() == 42); // expected-warning{{TRUE}}

    DV dv;
    dv.setX();
    clang_analyzer_eval(dv.getX() == 42); // expected-warning{{TRUE}}

    DV2 dv2;
    dv2.setX();
    clang_analyzer_eval(dv2.getX() == 42); // expected-warning{{TRUE}}
  }


  // Make sure we're consistent about the offset of the A subobject within an
  // Intermediate virtual base class.
  class Padding1 { int unused; };
  class Padding2 { int unused; };
  class Intermediate : public Padding1, public A, public Padding2 {};

  class BI : public virtual Intermediate {
  public:
    int getX() { return x; }
  };

  class CI : public virtual Intermediate {
  public:
    void setX() { x = 42; }
  };

  class DI : public BI, public CI {};

  void testIntermediate() {
    DI d;
    d.setX();
    clang_analyzer_eval(d.getX() == 42); // expected-warning{{TRUE}}
  }
}


namespace DynamicVirtualUpcast {
  class A {
  public:
    virtual ~A();
  };

  class B : virtual public A {};
  class C : virtual public B {};
  class D : virtual public C {};

  bool testCast(A *a) {
    return dynamic_cast<B*>(a) && dynamic_cast<C*>(a);
  }

  void test() {
    D d;
    clang_analyzer_eval(testCast(&d)); // expected-warning{{TRUE}}
  }
}

namespace DynamicMultipleInheritanceUpcast {
  class B {
  public:
    virtual ~B();
  };
  class C {
  public:
    virtual ~C();
  };
  class D : public B, public C {};

  bool testCast(B *a) {
    return dynamic_cast<C*>(a);
  }

  void test() {
    D d;
    clang_analyzer_eval(testCast(&d)); // expected-warning{{TRUE}}
  }


  class DV : virtual public B, virtual public C {};

  void testVirtual() {
    DV d;
    clang_analyzer_eval(testCast(&d)); // expected-warning{{TRUE}}
  }
}

namespace LazyBindings {
  struct Base {
    int x;
  };

  struct Derived : public Base {
    int y;
  };

  struct DoubleDerived : public Derived {
    int z;
  };

  int getX(const Base &obj) {
    return obj.x;
  }

  int getY(const Derived &obj) {
    return obj.y;
  }

  void testDerived() {
    Derived d;
    d.x = 1;
    d.y = 2;
    clang_analyzer_eval(getX(d) == 1); // expected-warning{{TRUE}}
    clang_analyzer_eval(getY(d) == 2); // expected-warning{{TRUE}}

    Base b(d);
    clang_analyzer_eval(getX(b) == 1); // expected-warning{{TRUE}}

    Derived d2(d);
    clang_analyzer_eval(getX(d2) == 1); // expected-warning{{TRUE}}
    clang_analyzer_eval(getY(d2) == 2); // expected-warning{{TRUE}}
  }

  void testDoubleDerived() {
    DoubleDerived d;
    d.x = 1;
    d.y = 2;
    clang_analyzer_eval(getX(d) == 1); // expected-warning{{TRUE}}
    clang_analyzer_eval(getY(d) == 2); // expected-warning{{TRUE}}

    Base b(d);
    clang_analyzer_eval(getX(b) == 1); // expected-warning{{TRUE}}

    Derived d2(d);
    clang_analyzer_eval(getX(d2) == 1); // expected-warning{{TRUE}}
    clang_analyzer_eval(getY(d2) == 2); // expected-warning{{TRUE}}

    DoubleDerived d3(d);
    clang_analyzer_eval(getX(d3) == 1); // expected-warning{{TRUE}}
    clang_analyzer_eval(getY(d3) == 2); // expected-warning{{TRUE}}
  }

  namespace WithOffset {
    struct Offset {
      int padding;
    };

    struct OffsetDerived : private Offset, public Base {
      int y;
    };

    struct DoubleOffsetDerived : public OffsetDerived {
      int z;
    };

    int getY(const OffsetDerived &obj) {
      return obj.y;
    }

    void testDerived() {
      OffsetDerived d;
      d.x = 1;
      d.y = 2;
      clang_analyzer_eval(getX(d) == 1); // expected-warning{{TRUE}}
      clang_analyzer_eval(getY(d) == 2); // expected-warning{{TRUE}}

      Base b(d);
      clang_analyzer_eval(getX(b) == 1); // expected-warning{{TRUE}}

      OffsetDerived d2(d);
      clang_analyzer_eval(getX(d2) == 1); // expected-warning{{TRUE}}
      clang_analyzer_eval(getY(d2) == 2); // expected-warning{{TRUE}}
    }

    void testDoubleDerived() {
      DoubleOffsetDerived d;
      d.x = 1;
      d.y = 2;
      clang_analyzer_eval(getX(d) == 1); // expected-warning{{TRUE}}
      clang_analyzer_eval(getY(d) == 2); // expected-warning{{TRUE}}

      Base b(d);
      clang_analyzer_eval(getX(b) == 1); // expected-warning{{TRUE}}

      OffsetDerived d2(d);
      clang_analyzer_eval(getX(d2) == 1); // expected-warning{{TRUE}}
      clang_analyzer_eval(getY(d2) == 2); // expected-warning{{TRUE}}

      DoubleOffsetDerived d3(d);
      clang_analyzer_eval(getX(d3) == 1); // expected-warning{{TRUE}}
      clang_analyzer_eval(getY(d3) == 2); // expected-warning{{TRUE}}
    }
  }

  namespace WithVTable {
    struct DerivedVTBL : public Base {
      int y;
      virtual void method();
    };

    struct DoubleDerivedVTBL : public DerivedVTBL {
      int z;
    };

    int getY(const DerivedVTBL &obj) {
      return obj.y;
    }

    int getZ(const DoubleDerivedVTBL &obj) {
      return obj.z;
    }

    void testDerived() {
      DerivedVTBL d;
      d.x = 1;
      d.y = 2;
      clang_analyzer_eval(getX(d) == 1); // expected-warning{{TRUE}}
      clang_analyzer_eval(getY(d) == 2); // expected-warning{{TRUE}}

      Base b(d);
      clang_analyzer_eval(getX(b) == 1); // expected-warning{{TRUE}}

#if CONSTRUCTORS
      DerivedVTBL d2(d);
      clang_analyzer_eval(getX(d2) == 1); // expected-warning{{TRUE}}
      clang_analyzer_eval(getY(d2) == 2); // expected-warning{{TRUE}}
#endif
    }

#if CONSTRUCTORS
    void testDoubleDerived() {
      DoubleDerivedVTBL d;
      d.x = 1;
      d.y = 2;
      d.z = 3;
      clang_analyzer_eval(getX(d) == 1); // expected-warning{{TRUE}}
      clang_analyzer_eval(getY(d) == 2); // expected-warning{{TRUE}}
      clang_analyzer_eval(getZ(d) == 3); // expected-warning{{TRUE}}

      Base b(d);
      clang_analyzer_eval(getX(b) == 1); // expected-warning{{TRUE}}

      DerivedVTBL d2(d);
      clang_analyzer_eval(getX(d2) == 1); // expected-warning{{TRUE}}
      clang_analyzer_eval(getY(d2) == 2); // expected-warning{{TRUE}}

      DoubleDerivedVTBL d3(d);
      clang_analyzer_eval(getX(d3) == 1); // expected-warning{{TRUE}}
      clang_analyzer_eval(getY(d3) == 2); // expected-warning{{TRUE}}
      clang_analyzer_eval(getZ(d3) == 3); // expected-warning{{TRUE}}
    }
#endif
  }

#if CONSTRUCTORS
  namespace Nested {
    struct NonTrivialCopy {
      int padding;
      NonTrivialCopy() {}
      NonTrivialCopy(const NonTrivialCopy &) {}
    };

    struct FullyDerived : private NonTrivialCopy, public Derived {
      int z;
    };

    struct Wrapper {
      FullyDerived d;
      int zz;

      Wrapper(const FullyDerived &d) : d(d), zz(0) {}
    };

    void test5() {
      Wrapper w((FullyDerived()));
      w.d.x = 1;

      Wrapper w2(w);
      clang_analyzer_eval(getX(w2.d) == 1); // expected-warning{{TRUE}}
    }
  }
#endif
}

namespace Redeclaration {
  class Base;

  class Base {
  public:
    virtual int foo();
    int get() { return value; }

    int value;
  };

  class Derived : public Base {
  public:
    virtual int bar();
  };

  void test(Derived d) {
    d.foo(); // don't crash
    d.bar(); // sanity check

    Base &b = d;
    b.foo(); // don't crash

    d.value = 42; // don't crash
    clang_analyzer_eval(d.get() == 42); // expected-warning{{TRUE}}
    clang_analyzer_eval(b.get() == 42); // expected-warning{{TRUE}}
  }
};

namespace PR15394 {
  namespace Original {
    class Base {
    public:
      virtual int f() = 0;
      int i;
    };

    class Derived1 : public Base {
    public:
      int j;
    };

    class Derived2 : public Derived1 {
    public:
      virtual int f() {
        clang_analyzer_checkInlined(true); // expected-warning{{TRUE}}
        return i + j;
      }
    };

    void testXXX() {
      Derived1 *d1p = reinterpret_cast<Derived1*>(new Derived2);
      d1p->i = 1;
      d1p->j = 2;
      clang_analyzer_eval(d1p->f() == 3); // expected-warning{{TRUE}}
    }
  }

  namespace VirtualInDerived {
    class Base {
    public:
      int i;
    };

    class Derived1 : public Base {
    public:
      virtual int f() = 0;
      int j;
    };

    class Derived2 : public Derived1 {
    public:
      virtual int f() {
        clang_analyzer_checkInlined(true); // expected-warning{{TRUE}}
        return i + j;
      }
    };

    void test() {
      Derived1 *d1p = reinterpret_cast<Derived1*>(new Derived2);
      d1p->i = 1;
      d1p->j = 2;
      clang_analyzer_eval(d1p->f() == 3); // expected-warning{{TRUE}}
    }
  }

  namespace NoCast {
    class Base {
    public:
      int i;
    };

    class Derived1 : public Base {
    public:
      virtual int f() = 0;
      int j;
    };

    class Derived2 : public Derived1 {
    public:
      virtual int f() {
        clang_analyzer_checkInlined(true); // expected-warning{{TRUE}}
        return i + j;
      }
    };

    void test() {
      Derived1 *d1p = new Derived2;
      d1p->i = 1;
      d1p->j = 2;
      clang_analyzer_eval(d1p->f() == 3); // expected-warning{{TRUE}}
    }
  }
};

namespace Bug16309 {
  struct Incomplete;

  struct Base { virtual ~Base(); };

  struct Derived : public Base { int x; };

  void* f(Incomplete *i) {
    Base *b = reinterpret_cast<Base *>(i);
    // This used to crash because of the reinterpret_cast above.
    Derived *d = dynamic_cast<Derived *>(b);
    return d;
  }

  // And check that reinterpret+dynamic casts work correctly after the fix.
  void g() {
    Derived d;
    d.x = 47;
    Base *b = &d;
    Incomplete *i = reinterpret_cast<Incomplete *>(b);
    Base *b2 = reinterpret_cast<Base *>(i);
    Derived *d2 = dynamic_cast<Derived *>(b2);
    clang_analyzer_eval(d2->x == 47); // expected-warning{{TRUE}}
  }
}