xref: /llvm-project/cross-project-tests/debuginfo-tests/llvm-prettyprinters/gdb/llvm-support.cpp (revision 1c9a81b2bd91e8cd9baf742fa66650eefbaf552c)
1 #include "llvm/ADT/ArrayRef.h"
2 #include "llvm/ADT/DenseMap.h"
3 #include "llvm/ADT/PointerIntPair.h"
4 #include "llvm/ADT/PointerUnion.h"
5 #include "llvm/ADT/SmallString.h"
6 #include "llvm/ADT/SmallVector.h"
7 #include "llvm/ADT/StringMap.h"
8 #include "llvm/ADT/Twine.h"
9 #include "llvm/ADT/ilist.h"
10 #include "llvm/Support/Error.h"
11 #include <optional>
12 
13 int Array[] = {1, 2, 3};
14 auto IntPtr = reinterpret_cast<int *>(0xabc);
15 
16 llvm::ArrayRef<int> ArrayRef(Array);
17 llvm::MutableArrayRef<int> MutableArrayRef(Array);
18 llvm::DenseMap<int, int> DenseMap = {{4, 5}, {6, 7}};
19 llvm::StringMap<int> StringMap = {{"foo", 123}, {"bar", 456}};
20 llvm::Expected<int> ExpectedValue(8);
21 llvm::Expected<int> ExpectedError(llvm::createStringError(""));
22 llvm::SmallVector<int, 5> SmallVector = {10, 11, 12};
23 llvm::SmallString<5> SmallString("foo");
24 llvm::StringRef StringRef = "bar";
25 // Should test std::string in Twine too, but it's currently broken because I
26 // don't know how to add 'str' and 'gdb.LazyString' (can't figure out any way to
27 // string-ify LazyString).
28 std::string String = "foo";
29 llvm::Twine TempTwine = llvm::Twine(String) + StringRef;
30 llvm::Twine Twine = TempTwine + "baz";
31 llvm::PointerIntPair<int *, 1> PointerIntPair(IntPtr, 1);
32 
33 struct alignas(8) Z {};
34 llvm::PointerUnion<Z *, int *> PointerUnion(IntPtr);
35 
36 // No members which instantiate PointerUnionUIntTraits<Z *> (e.g. get<T *>())
37 // are called, and this instance will therefore be raw-printed.
38 llvm::PointerUnion<Z *, float *> RawPrintingPointerUnion(nullptr);
39 
40 using IlistTag = llvm::ilist_tag<struct A>;
41 using SimpleIlistTag = llvm::ilist_tag<struct B>;
42 struct IlistNode : llvm::ilist_node<IlistNode, IlistTag>,
43                    llvm::ilist_node<IlistNode, SimpleIlistTag> {
44   int Value;
45 };
__anon47486f690102null46 auto Ilist = [] {
47   llvm::ilist<IlistNode, IlistTag> Result;
48   for (int I : {13, 14, 15}) {
49     Result.push_back(new IlistNode);
50     Result.back().Value = I;
51   }
52   return Result;
53 }();
__anon47486f690202() 54 auto SimpleIlist = []() {
55   llvm::simple_ilist<IlistNode, SimpleIlistTag> Result;
56   for (auto &Node : Ilist)
57     Result.push_front(Node);
58   return Result;
59 }();
60 
main()61 int main() {
62   std::uintptr_t result = 0;
63   auto dont_strip = [&](const auto &val) {
64     result += reinterpret_cast<std::uintptr_t>(&val);
65   };
66   dont_strip(ArrayRef);
67   dont_strip(MutableArrayRef);
68   dont_strip(ExpectedValue);
69   dont_strip(ExpectedError);
70   return result; // Non-zero return value is OK.
71 }
72