]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/lldb/source/Plugins/ExpressionParser/Clang/ASTDumper.cpp
Merge ^/vendor/lldb/dist up to its last change, and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / llvm-project / lldb / source / Plugins / ExpressionParser / Clang / ASTDumper.cpp
1 //===-- ASTDumper.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 //===----------------------------------------------------------------------===//
8
9 #include "ASTDumper.h"
10
11 #include "lldb/Symbol/ClangASTContext.h"
12 #include "lldb/Symbol/ClangUtil.h"
13 #include "lldb/Symbol/CompilerType.h"
14 #include "lldb/Utility/Log.h"
15
16 #include "llvm/Support/raw_ostream.h"
17
18 using namespace lldb_private;
19
20 ASTDumper::ASTDumper(clang::Decl *decl) {
21   clang::DeclContext *decl_ctx = llvm::dyn_cast<clang::DeclContext>(decl);
22
23   bool has_external_lexical_storage;
24   bool has_external_visible_storage;
25
26   if (decl_ctx) {
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);
31   }
32
33   llvm::raw_string_ostream os(m_dump);
34   decl->print(os);
35   os.flush();
36
37   if (decl_ctx) {
38     decl_ctx->setHasExternalLexicalStorage(has_external_lexical_storage);
39     decl_ctx->setHasExternalVisibleStorage(has_external_visible_storage);
40   }
41 }
42
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();
46
47   decl_ctx->setHasExternalLexicalStorage(false);
48   decl_ctx->setHasExternalVisibleStorage(false);
49
50   if (clang::Decl *decl = llvm::dyn_cast<clang::Decl>(decl_ctx)) {
51     llvm::raw_string_ostream os(m_dump);
52     decl->print(os);
53     os.flush();
54   } else {
55     m_dump.assign("<DeclContext is not a Decl>");
56   }
57
58   decl_ctx->setHasExternalLexicalStorage(has_external_lexical_storage);
59   decl_ctx->setHasExternalVisibleStorage(has_external_visible_storage);
60 }
61
62 ASTDumper::ASTDumper(const clang::Type *type) {
63   m_dump = clang::QualType(type, 0).getAsString();
64 }
65
66 ASTDumper::ASTDumper(clang::QualType type) { m_dump = type.getAsString(); }
67
68 ASTDumper::ASTDumper(lldb::opaque_compiler_type_t type) {
69   m_dump = clang::QualType::getFromOpaquePtr(type).getAsString();
70 }
71
72 ASTDumper::ASTDumper(const CompilerType &compiler_type) {
73   m_dump = ClangUtil::GetQualType(compiler_type).getAsString();
74 }
75
76 const char *ASTDumper::GetCString() { return m_dump.c_str(); }
77
78 void ASTDumper::ToLog(Log *log, const char *prefix) {
79   size_t len = m_dump.length() + 1;
80
81   char *alloc = (char *)malloc(len);
82   char *str = alloc;
83
84   memcpy(str, m_dump.c_str(), len);
85
86   char *end = nullptr;
87
88   end = strchr(str, '\n');
89
90   while (end) {
91     *end = '\0';
92
93     LLDB_LOGF(log, "%s%s", prefix, str);
94
95     *end = '\n';
96
97     str = end + 1;
98     end = strchr(str, '\n');
99   }
100
101   LLDB_LOGF(log, "%s%s", prefix, str);
102
103   free(alloc);
104 }