1 //===-- ASTDumper.cpp -------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
11 #include "lldb/Symbol/ClangASTContext.h"
12 #include "lldb/Symbol/ClangUtil.h"
13 #include "lldb/Symbol/CompilerType.h"
14 #include "lldb/Utility/Log.h"
16 #include "llvm/Support/raw_ostream.h"
18 using namespace lldb_private;
20 ASTDumper::ASTDumper(clang::Decl *decl) {
21 clang::DeclContext *decl_ctx = llvm::dyn_cast<clang::DeclContext>(decl);
23 bool has_external_lexical_storage;
24 bool has_external_visible_storage;
27 has_external_lexical_storage = decl_ctx->hasExternalLexicalStorage();
28 has_external_visible_storage = decl_ctx->hasExternalVisibleStorage();
29 decl_ctx->setHasExternalLexicalStorage(false);
30 decl_ctx->setHasExternalVisibleStorage(false);
33 llvm::raw_string_ostream os(m_dump);
38 decl_ctx->setHasExternalLexicalStorage(has_external_lexical_storage);
39 decl_ctx->setHasExternalVisibleStorage(has_external_visible_storage);
43 ASTDumper::ASTDumper(clang::DeclContext *decl_ctx) {
44 bool has_external_lexical_storage = decl_ctx->hasExternalLexicalStorage();
45 bool has_external_visible_storage = decl_ctx->hasExternalVisibleStorage();
47 decl_ctx->setHasExternalLexicalStorage(false);
48 decl_ctx->setHasExternalVisibleStorage(false);
50 if (clang::Decl *decl = llvm::dyn_cast<clang::Decl>(decl_ctx)) {
51 llvm::raw_string_ostream os(m_dump);
55 m_dump.assign("<DeclContext is not a Decl>");
58 decl_ctx->setHasExternalLexicalStorage(has_external_lexical_storage);
59 decl_ctx->setHasExternalVisibleStorage(has_external_visible_storage);
62 ASTDumper::ASTDumper(const clang::Type *type) {
63 m_dump = clang::QualType(type, 0).getAsString();
66 ASTDumper::ASTDumper(clang::QualType type) { m_dump = type.getAsString(); }
68 ASTDumper::ASTDumper(lldb::opaque_compiler_type_t type) {
69 m_dump = clang::QualType::getFromOpaquePtr(type).getAsString();
72 ASTDumper::ASTDumper(const CompilerType &compiler_type) {
73 m_dump = ClangUtil::GetQualType(compiler_type).getAsString();
76 const char *ASTDumper::GetCString() { return m_dump.c_str(); }
78 void ASTDumper::ToLog(Log *log, const char *prefix) {
79 size_t len = m_dump.length() + 1;
81 char *alloc = (char *)malloc(len);
84 memcpy(str, m_dump.c_str(), len);
88 end = strchr(str, '\n');
93 LLDB_LOGF(log, "%s%s", prefix, str);
98 end = strchr(str, '\n');
101 LLDB_LOGF(log, "%s%s", prefix, str);