LLDB mainline
ClangUtil.cpp
Go to the documentation of this file.
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//===----------------------------------------------------------------------===//
10
13
14using namespace clang;
15using namespace lldb_private;
16
18 // Invalid types are never Clang types.
19 if (!ct)
20 return false;
21
23 return false;
24
25 if (!ct.GetOpaqueQualType())
26 return false;
27
28 return true;
29}
30
31clang::Decl *ClangUtil::GetDecl(const CompilerDecl &decl) {
32 assert(llvm::isa<TypeSystemClang>(decl.GetTypeSystem()));
33 return static_cast<clang::Decl *>(decl.GetOpaqueDecl());
34}
35
37 // Make sure we have a clang type before making a clang::QualType
38 if (!IsClangType(ct))
39 return QualType();
40
41 return QualType::getFromOpaquePtr(ct.GetOpaqueQualType());
42}
43
45 if (!IsClangType(ct))
46 return QualType();
47
48 return GetQualType(ct).getCanonicalType();
49}
50
52 if (!IsClangType(ct))
53 return ct;
54
55 QualType qual_type(GetQualType(ct));
56 qual_type.removeLocalFastQualifiers();
57 return CompilerType(ct.GetTypeSystem(), qual_type.getAsOpaquePtr());
58}
59
60clang::TagDecl *ClangUtil::GetAsTagDecl(const CompilerType &type) {
61 clang::QualType qual_type = ClangUtil::GetCanonicalQualType(type);
62 if (qual_type.isNull())
63 return nullptr;
64
65 return qual_type->getAsTagDecl();
66}
67
68std::string ClangUtil::DumpDecl(const clang::Decl *d) {
69 if (!d)
70 return "nullptr";
71
72 std::string result;
73 llvm::raw_string_ostream stream(result);
74 bool deserialize = false;
75 d->dump(stream, deserialize);
76
77 stream.flush();
78 return result;
79}
80
81std::string ClangUtil::ToString(const clang::Type *t) {
82 return clang::QualType(t, 0).getAsString();
83}
84
85std::string ClangUtil::ToString(const CompilerType &c) {
86 return ClangUtil::GetQualType(c).getAsString();
87}
Represents a generic declaration such as a function declaration.
Definition: CompilerDecl.h:28
void * GetOpaqueDecl() const
Definition: CompilerDecl.h:58
TypeSystem * GetTypeSystem() const
Definition: CompilerDecl.h:56
std::shared_ptr< TypeSystemType > dyn_cast_or_null()
Return a shared_ptr<TypeSystemType> if dyn_cast succeeds.
Definition: CompilerType.h:65
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
TypeSystemSPWrapper GetTypeSystem() const
Accessors.
lldb::opaque_compiler_type_t GetOpaqueQualType() const
Definition: CompilerType.h:287
A TypeSystem implementation based on Clang.
A class that represents a running process on the host machine.
static clang::QualType GetQualType(const CompilerType &ct)
Definition: ClangUtil.cpp:36
static std::string ToString(const clang::Type *t)
Returns a textual representation of the given type.
Definition: ClangUtil.cpp:81
static std::string DumpDecl(const clang::Decl *d)
Returns a textual representation of the given Decl's AST.
Definition: ClangUtil.cpp:68
static clang::Decl * GetDecl(const CompilerDecl &decl)
Returns the clang::Decl of the given CompilerDecl.
Definition: ClangUtil.cpp:31
static clang::QualType GetCanonicalQualType(const CompilerType &ct)
Definition: ClangUtil.cpp:44
static bool IsClangType(const CompilerType &ct)
Definition: ClangUtil.cpp:17
static CompilerType RemoveFastQualifiers(const CompilerType &ct)
Definition: ClangUtil.cpp:51
static clang::TagDecl * GetAsTagDecl(const CompilerType &type)
Definition: ClangUtil.cpp:60