xref: /freebsd/contrib/llvm-project/clang/lib/Analysis/FlowSensitive/Value.cpp (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
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<ReferenceValue>(&Val1)) {
23     auto *IndVal2 = cast<ReferenceValue>(&Val2);
24     return &IndVal1->getReferentLoc() == &IndVal2->getReferentLoc();
25   }
26   if (auto *IndVal1 = dyn_cast<PointerValue>(&Val1)) {
27     auto *IndVal2 = cast<PointerValue>(&Val2);
28     return &IndVal1->getPointeeLoc() == &IndVal2->getPointeeLoc();
29   }
30   return false;
31 }
32 
33 bool areEquivalentValues(const Value &Val1, const Value &Val2) {
34   return &Val1 == &Val2 || (Val1.getKind() == Val2.getKind() &&
35                             (isa<TopBoolValue>(&Val1) ||
36                              areEquivalentIndirectionValues(Val1, Val2)));
37 }
38 
39 raw_ostream &operator<<(raw_ostream &OS, const Value &Val) {
40   switch (Val.getKind()) {
41   case Value::Kind::Reference: {
42     const auto *RV = cast<ReferenceValue>(&Val);
43     return OS << "Reference(" << &RV->getReferentLoc() << ")";
44   }
45   case Value::Kind::Pointer: {
46     const auto *PV = dyn_cast<PointerValue>(&Val);
47     return OS << "Pointer(" << &PV->getPointeeLoc() << ")";
48   }
49   // FIXME: support remaining cases.
50   default:
51     return OS << debugString(Val.getKind());
52   }
53 }
54 
55 } // namespace dataflow
56 } // namespace clang
57