xref: /freebsd/contrib/llvm-project/clang/lib/Analysis/FlowSensitive/DebugSupport.cpp (revision a4e5e0106ac7145f56eb39a691e302cabb4635be)
1 //===- DebugSupport.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 functions which generate more readable forms of data
10 //  structures used in the dataflow analyses, for debugging purposes.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include <utility>
15 
16 #include "clang/Analysis/FlowSensitive/DebugSupport.h"
17 #include "clang/Analysis/FlowSensitive/Solver.h"
18 #include "clang/Analysis/FlowSensitive/Value.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/Support/ErrorHandling.h"
21 
22 namespace clang {
23 namespace dataflow {
24 
25 llvm::StringRef debugString(Value::Kind Kind) {
26   switch (Kind) {
27   case Value::Kind::Integer:
28     return "Integer";
29   case Value::Kind::Reference:
30     return "Reference";
31   case Value::Kind::Pointer:
32     return "Pointer";
33   case Value::Kind::Struct:
34     return "Struct";
35   case Value::Kind::AtomicBool:
36     return "AtomicBool";
37   case Value::Kind::TopBool:
38     return "TopBool";
39   case Value::Kind::FormulaBool:
40     return "FormulaBool";
41   }
42   llvm_unreachable("Unhandled value kind");
43 }
44 
45 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
46                               Solver::Result::Assignment Assignment) {
47   switch (Assignment) {
48   case Solver::Result::Assignment::AssignedFalse:
49     return OS << "False";
50   case Solver::Result::Assignment::AssignedTrue:
51     return OS << "True";
52   }
53   llvm_unreachable("Booleans can only be assigned true/false");
54 }
55 
56 llvm::StringRef debugString(Solver::Result::Status Status) {
57   switch (Status) {
58   case Solver::Result::Status::Satisfiable:
59     return "Satisfiable";
60   case Solver::Result::Status::Unsatisfiable:
61     return "Unsatisfiable";
62   case Solver::Result::Status::TimedOut:
63     return "TimedOut";
64   }
65   llvm_unreachable("Unhandled SAT check result status");
66 }
67 
68 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Solver::Result &R) {
69   OS << debugString(R.getStatus()) << "\n";
70   if (auto Solution = R.getSolution()) {
71     std::vector<std::pair<Atom, Solver::Result::Assignment>> Sorted = {
72         Solution->begin(), Solution->end()};
73     llvm::sort(Sorted);
74     for (const auto &Entry : Sorted)
75       OS << Entry.first << " = " << Entry.second << "\n";
76   }
77   return OS;
78 }
79 
80 } // namespace dataflow
81 } // namespace clang
82