1 //===-- ClangUtil.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 // A collection of helper methods and data structures for manipulating clang 8 // types and decls. 9 //===----------------------------------------------------------------------===// 10 11 #include "lldb/Symbol/ClangUtil.h" 12 #include "lldb/Symbol/ClangASTContext.h" 13 14 using namespace clang; 15 using namespace lldb_private; 16 IsClangType(const CompilerType & ct)17bool ClangUtil::IsClangType(const CompilerType &ct) { 18 // Invalid types are never Clang types. 19 if (!ct) 20 return false; 21 22 if (llvm::dyn_cast_or_null<ClangASTContext>(ct.GetTypeSystem()) == nullptr) 23 return false; 24 25 if (!ct.GetOpaqueQualType()) 26 return false; 27 28 return true; 29 } 30 GetQualType(const CompilerType & ct)31QualType ClangUtil::GetQualType(const CompilerType &ct) { 32 // Make sure we have a clang type before making a clang::QualType 33 if (!IsClangType(ct)) 34 return QualType(); 35 36 return QualType::getFromOpaquePtr(ct.GetOpaqueQualType()); 37 } 38 GetCanonicalQualType(const CompilerType & ct)39QualType ClangUtil::GetCanonicalQualType(const CompilerType &ct) { 40 if (!IsClangType(ct)) 41 return QualType(); 42 43 return GetQualType(ct).getCanonicalType(); 44 } 45 RemoveFastQualifiers(const CompilerType & ct)46CompilerType ClangUtil::RemoveFastQualifiers(const CompilerType &ct) { 47 if (!IsClangType(ct)) 48 return ct; 49 50 QualType qual_type(GetQualType(ct)); 51 qual_type.removeLocalFastQualifiers(); 52 return CompilerType(ct.GetTypeSystem(), qual_type.getAsOpaquePtr()); 53 } 54 GetAsTagDecl(const CompilerType & type)55clang::TagDecl *ClangUtil::GetAsTagDecl(const CompilerType &type) { 56 clang::QualType qual_type = ClangUtil::GetCanonicalQualType(type); 57 if (qual_type.isNull()) 58 return nullptr; 59 60 return qual_type->getAsTagDecl(); 61 } 62 DumpDecl(const clang::Decl * d)63std::string ClangUtil::DumpDecl(const clang::Decl *d) { 64 if (!d) 65 return "nullptr"; 66 67 std::string result; 68 llvm::raw_string_ostream stream(result); 69 bool deserialize = false; 70 d->dump(stream, deserialize); 71 72 stream.flush(); 73 return result; 74 } 75 ToString(const clang::Type * t)76std::string ClangUtil::ToString(const clang::Type *t) { 77 return clang::QualType(t, 0).getAsString(); 78 } 79 ToString(const CompilerType & c)80std::string ClangUtil::ToString(const CompilerType &c) { 81 return ClangUtil::GetQualType(c).getAsString(); 82 } 83