aboutsummaryrefslogtreecommitdiff
path: root/source/Expression/ASTDumper.cpp
blob: 5210d149e666b5fe1afe3fe7c904c3daca68899e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//===-- ASTDumper.cpp -------------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "lldb/Core/Log.h"
#include "lldb/Expression/ASTDumper.h"
#include "lldb/Symbol/ClangASTType.h"

#include "llvm/Support/raw_ostream.h"

using namespace lldb_private;

ASTDumper::ASTDumper (clang::Decl *decl)
{
    clang::DeclContext *decl_ctx = llvm::dyn_cast<clang::DeclContext>(decl);
    
    bool has_external_lexical_storage;
    bool has_external_visible_storage;
    
    if (decl_ctx)
    {
        has_external_lexical_storage = decl_ctx->hasExternalLexicalStorage();
        has_external_visible_storage = decl_ctx->hasExternalVisibleStorage();
        decl_ctx->setHasExternalLexicalStorage(false);
        decl_ctx->setHasExternalVisibleStorage(false);
    }
    
    llvm::raw_string_ostream os(m_dump);
    decl->print (os);
    os.flush();
    
    if (decl_ctx)
    {
        decl_ctx->setHasExternalLexicalStorage(has_external_lexical_storage);
        decl_ctx->setHasExternalVisibleStorage(has_external_visible_storage);
    }
}

ASTDumper::ASTDumper (clang::DeclContext *decl_ctx)
{
    bool has_external_lexical_storage = decl_ctx->hasExternalLexicalStorage();
    bool has_external_visible_storage = decl_ctx->hasExternalVisibleStorage();
    
    decl_ctx->setHasExternalLexicalStorage(false);
    decl_ctx->setHasExternalVisibleStorage(false);
    
    if (clang::Decl *decl = llvm::dyn_cast<clang::Decl>(decl_ctx))
    {
        llvm::raw_string_ostream os(m_dump);
        decl->print (os);
        os.flush();
    }
    else
    {
        m_dump.assign("<DeclContext is not a Decl>");
    }
    
    decl_ctx->setHasExternalLexicalStorage(has_external_lexical_storage);
    decl_ctx->setHasExternalVisibleStorage(has_external_visible_storage);
}

ASTDumper::ASTDumper (const clang::Type *type)
{
    m_dump = clang::QualType(type, 0).getAsString();
}

ASTDumper::ASTDumper (clang::QualType type)
{
    m_dump = type.getAsString();
}

ASTDumper::ASTDumper (lldb::clang_type_t type)
{
    m_dump = clang::QualType::getFromOpaquePtr(type).getAsString();
}

ASTDumper::ASTDumper (const ClangASTType &clang_type)
{
    m_dump = clang_type.GetQualType().getAsString();
}


const char *
ASTDumper::GetCString()
{
    return m_dump.c_str();
}

void ASTDumper::ToSTDERR()
{
    fprintf(stderr, "%s\n", m_dump.c_str());
}

void ASTDumper::ToLog(Log *log, const char *prefix)
{
    size_t len = m_dump.length() + 1;
    
    char *alloc = (char*)malloc(len);
    char *str = alloc;
    
    memcpy(str, m_dump.c_str(), len);
    
    char *end = NULL;
    
    end = strchr(str, '\n');
    
    while (end)
    {
        *end = '\0';
        
        log->Printf("%s%s", prefix, str);
        
        *end = '\n';
        
        str = end + 1;
        end = strchr(str, '\n');
    }
    
    log->Printf("%s%s", prefix, str);
                    
    free(alloc);
}

void ASTDumper::ToStream(lldb::StreamSP &stream)
{
    stream->PutCString(m_dump.c_str());
}