1 //===-- Value.cpp -----------------------------------------------*- C++ -*-===//
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 //  This file defines support functions for the `Value` type.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "clang/Analysis/FlowSensitive/Value.h"
14 #include "clang/Analysis/FlowSensitive/DebugSupport.h"
15 #include "llvm/Support/Casting.h"
16 
17 namespace clang {
18 namespace dataflow {
19 
20 static bool areEquivalentIndirectionValues(const Value &Val1,
21                                            const Value &Val2) {
22   if (auto *IndVal1 = dyn_cast<PointerValue>(&Val1)) {
23     auto *IndVal2 = cast<PointerValue>(&Val2);
24     return &IndVal1->getPointeeLoc() == &IndVal2->getPointeeLoc();
25   }
26   return false;
27 }
28 
29 bool areEquivalentValues(const Value &Val1, const Value &Val2) {
30   if (&Val1 == &Val2)
31     return true;
32   if (Val1.getKind() != Val2.getKind())
33     return false;
34   // If values are distinct and have properties, we don't consider them equal,
35   // leaving equality up to the user model.
36   if (!Val1.properties().empty() || !Val2.properties().empty())
37     return false;
38   if (isa<TopBoolValue>(&Val1))
39     return true;
40   return areEquivalentIndirectionValues(Val1, Val2);
41 }
42 
43 raw_ostream &operator<<(raw_ostream &OS, const Value &Val) {
44   switch (Val.getKind()) {
45   case Value::Kind::Integer:
46     return OS << "Integer(@" << &Val << ")";
47   case Value::Kind::Pointer:
48     return OS << "Pointer(" << &cast<PointerValue>(Val).getPointeeLoc() << ")";
49   case Value::Kind::Record:
50     return OS << "Record(" << &cast<RecordValue>(Val).getLoc() << ")";
51   case Value::Kind::TopBool:
52     return OS << "TopBool(" << cast<TopBoolValue>(Val).getAtom() << ")";
53   case Value::Kind::AtomicBool:
54     return OS << "AtomicBool(" << cast<AtomicBoolValue>(Val).getAtom() << ")";
55   case Value::Kind::FormulaBool:
56     return OS << "FormulaBool(" << cast<FormulaBoolValue>(Val).formula() << ")";
57   }
58   llvm_unreachable("Unknown clang::dataflow::Value::Kind enum");
59 }
60 
61 } // namespace dataflow
62 } // namespace clang
63