xref: /freebsd-src/contrib/llvm-project/clang/lib/Analysis/FlowSensitive/Value.cpp (revision 1db9f3b21e39176dd5b67cf8ac378633b172463e)
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   return &Val1 == &Val2 || (Val1.getKind() == Val2.getKind() &&
31                             (isa<TopBoolValue>(&Val1) ||
32                              areEquivalentIndirectionValues(Val1, Val2)));
33 }
34 
35 raw_ostream &operator<<(raw_ostream &OS, const Value &Val) {
36   switch (Val.getKind()) {
37   case Value::Kind::Integer:
38     return OS << "Integer(@" << &Val << ")";
39   case Value::Kind::Pointer:
40     return OS << "Pointer(" << &cast<PointerValue>(Val).getPointeeLoc() << ")";
41   case Value::Kind::Record:
42     return OS << "Record(" << &cast<RecordValue>(Val).getLoc() << ")";
43   case Value::Kind::TopBool:
44     return OS << "TopBool(" << cast<TopBoolValue>(Val).getAtom() << ")";
45   case Value::Kind::AtomicBool:
46     return OS << "AtomicBool(" << cast<AtomicBoolValue>(Val).getAtom() << ")";
47   case Value::Kind::FormulaBool:
48     return OS << "FormulaBool(" << cast<FormulaBoolValue>(Val).formula() << ")";
49   }
50   llvm_unreachable("Unknown clang::dataflow::Value::Kind enum");
51 }
52 
53 } // namespace dataflow
54 } // namespace clang
55