Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / lldb / source / Plugins / ExpressionParser / Clang / ClangUtil.cpp
blob2e0bb318cb5076dbfbed4790061867e38fe1bf6a
1 //===-- ClangUtil.cpp -----------------------------------------------------===//
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 // A collection of helper methods and data structures for manipulating clang
8 // types and decls.
9 //===----------------------------------------------------------------------===//
11 #include "Plugins/ExpressionParser/Clang/ClangUtil.h"
12 #include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
14 using namespace clang;
15 using namespace lldb_private;
17 bool ClangUtil::IsClangType(const CompilerType &ct) {
18 // Invalid types are never Clang types.
19 if (!ct)
20 return false;
22 if (!ct.GetTypeSystem().dyn_cast_or_null<TypeSystemClang>())
23 return false;
25 if (!ct.GetOpaqueQualType())
26 return false;
28 return true;
31 clang::Decl *ClangUtil::GetDecl(const CompilerDecl &decl) {
32 assert(llvm::isa<TypeSystemClang>(decl.GetTypeSystem()));
33 return static_cast<clang::Decl *>(decl.GetOpaqueDecl());
36 QualType ClangUtil::GetQualType(const CompilerType &ct) {
37 // Make sure we have a clang type before making a clang::QualType
38 if (!IsClangType(ct))
39 return QualType();
41 return QualType::getFromOpaquePtr(ct.GetOpaqueQualType());
44 QualType ClangUtil::GetCanonicalQualType(const CompilerType &ct) {
45 if (!IsClangType(ct))
46 return QualType();
48 return GetQualType(ct).getCanonicalType();
51 CompilerType ClangUtil::RemoveFastQualifiers(const CompilerType &ct) {
52 if (!IsClangType(ct))
53 return ct;
55 QualType qual_type(GetQualType(ct));
56 qual_type.removeLocalFastQualifiers();
57 return CompilerType(ct.GetTypeSystem(), qual_type.getAsOpaquePtr());
60 clang::TagDecl *ClangUtil::GetAsTagDecl(const CompilerType &type) {
61 clang::QualType qual_type = ClangUtil::GetCanonicalQualType(type);
62 if (qual_type.isNull())
63 return nullptr;
65 return qual_type->getAsTagDecl();
68 std::string ClangUtil::DumpDecl(const clang::Decl *d) {
69 if (!d)
70 return "nullptr";
72 std::string result;
73 llvm::raw_string_ostream stream(result);
74 bool deserialize = false;
75 d->dump(stream, deserialize);
77 stream.flush();
78 return result;
81 std::string ClangUtil::ToString(const clang::Type *t) {
82 return clang::QualType(t, 0).getAsString();
85 std::string ClangUtil::ToString(const CompilerType &c) {
86 return ClangUtil::GetQualType(c).getAsString();