File: dotwriter.cpp

package info (click to toggle)
codelite 14.0+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 112,816 kB
  • sloc: cpp: 483,662; ansic: 150,144; php: 9,569; lex: 4,186; python: 3,417; yacc: 2,820; sh: 1,147; makefile: 52; xml: 13
file content (367 lines) | stat: -rw-r--r-- 12,395 bytes parent folder | download
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright            : (C) 2014 Eran Ifrah
// file name            : dotwriter.cpp
//
// -------------------------------------------------------------------------
// A
//              _____           _      _     _ _
//             /  __ \         | |    | |   (_) |
//             | /  \/ ___   __| | ___| |    _| |_ ___
//             | |    / _ \ / _  |/ _ \ |   | | __/ _ )
//             | \__/\ (_) | (_| |  __/ |___| | ||  __/
//              \____/\___/ \__,_|\___\_____/_|\__\___|
//
//                                                  F i l e
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

#include "dotwriter.h"
#include <wx/strconv.h>
#include <wx/file.h>
#include <wx/msgdlg.h>
#include <wx/math.h>
#include <wx/regex.h>
#include <math.h>

DotWriter::DotWriter()
{
    begin_graph = wxT("digraph\n{");
    end_graph = wxT("}\n");
    fontname = wxT("Arial");
    style = wxT("filled,rounded");
    shape = wxT("box");
    cwhite = wxT("white");
    cblack = wxT("black");
    hnode = wxT("");
    dedge = wxT("");
    hedge = wxT("");
    dlabel = wxT("");
    graph = wxT("");
    // m_OutputString = wxT("");
    mlines = NULL;
    dwcn = 0;
    dwce = 0;
    dwtn = 0;
    dwte = 0;
    m_writedotfileFlag = false;
    dwhideparams = false;
    dwhidenamespaces = false;
    dwstripparams = false;
}

DotWriter::~DotWriter() {}

void DotWriter::SetLineParser(LineParserList* pLines) //, int numOfLines)
{
    mlines = pLines;
}

void DotWriter::SetDotWriterFromDialogSettings(IManager* mgr)
{
    mgr->GetConfigTool()->ReadObject(wxT("CallGraph"), &confData);
    dwcn = confData.GetColorsNode();
    dwce = confData.GetColorsEdge();
    dwtn = confData.GetTresholdNode();
    dwte = confData.GetTresholdEdge();
    dwhideparams = confData.GetHideParams();
    dwhidenamespaces = confData.GetHideNamespaces();
    dwstripparams = confData.GetStripParams();
}

void DotWriter::SetDotWriterFromDetails(int colnode,
                                        int coledge,
                                        int thrnode,
                                        int thredge,
                                        bool hideparams,
                                        bool stripparams,
                                        bool hidenamespaces)
{
    dwcn = colnode;
    dwce = coledge;
    dwtn = thrnode;
    dwte = thredge;
    dwhideparams = hideparams;
    dwstripparams = stripparams;
    dwhidenamespaces = hidenamespaces;
}

void DotWriter::WriteToDotLanguage()
{
    int pl_index = 0;
    float pl_time = 0;
    bool is_node = false;
    wxArrayInt index_pl_nodes;

    if(mlines == NULL) return;

    graph = wxT("graph [ranksep=\"0.25\", fontname=") + fontname + wxT(", nodesep=\"0.125\"];");

    hnode = wxT("node [label=\"\\N\", fontsize=\"9.00\", fontname=") + fontname + wxT(", style=\"") + style +
            wxT("\", height=0, width=0, shape=") + shape + wxT(", fontcolor=") + cwhite + wxT("];");

    hedge = wxT("edge [fontname=") + fontname + wxT("];");

    // graph []; -- not used

    m_OutputString += begin_graph + wxT("\n") + graph + wxT("\n") + hnode + wxT("\n") + hedge + wxT("\n");

    LineParserList::compatibility_iterator it = mlines->GetFirst();

    while(it) {
        LineParser* line = it->GetData();

        if(line->pline && wxRound(line->time) >= dwtn) {
            is_node = true;
            index_pl_nodes.Add(line->index);
            dlabel = wxString::Format(wxT("%i"), line->index);
            dlabel += wxT(" [label=\"");
            dlabel += OptionsShortNameAndParameters(line->name);
            dlabel += wxT("\\n");
            dlabel += wxString::Format(wxT("%.2f"), line->time);
            dlabel += wxT("% \\n");
            dlabel += wxT("(");
            // self or children if have function childern line
            // if(line->self >= line->childern)
            //	dlabel += wxString::Format(wxT("%.2f"), line->self);
            // else
            dlabel += wxString::Format(wxT("%.2f"), line->self + line->children);
            dlabel += wxT("s)");
            dlabel += wxT("\\n");
            if(line->called0 != -1) dlabel += wxString::Format(wxT("%i"), line->called0) + wxT("x");
            // if(line->recursive)
            //	dlabel += wxT(" (") + wxString::Format(wxT("%i"),line->called0 + line->called1) + wxT("x)");
            dlabel += wxT("\",fontcolor=\"");
            dlabel += DefineColorForLabel(ReturnIndexForColor(line->time, dwcn));
            dlabel += wxT("\", color=\"");
            dlabel += DefineColorForNodeEdge(ReturnIndexForColor(line->time, dwcn));
            //
            dlabel += wxT("\"];"); //, fontsize=\"10.00\"
            //
            m_OutputString += dlabel + wxT("\n");
            //
            dlabel.Clear();
        }
        it = it->GetNext();
    }

    it = mlines->GetFirst();

    float max_time = -2;

    while(it) {
        LineParser* line = it->GetData();

        if(max_time < line->time) max_time = line->time;

        if(line->pline) {
            pl_index = line->index; // index for primary node
            pl_time = line->time;   // time for primary node
        }

        if(line->child && IsInArray(line->nameid, index_pl_nodes) && IsInArray(pl_index, index_pl_nodes) &&
           (wxRound(pl_time) >= dwte)) {
            dedge = wxString::Format(wxT("%i"), pl_index);
            dedge += wxT(" -> ");
            dedge += wxString::Format(wxT("%i"), line->nameid);
            dedge += wxT(" [color=\"");
            dedge += DefineColorForNodeEdge(ReturnIndexForColor(pl_time, dwce)); // color by primary node
            dedge += wxT("\", label=\"");
            // if(line->self != -1)
            // dedge += wxString::Format(wxT("%.2f"),line->self) + wxT("%");
            // dedge += wxT("\\n");
            dedge += wxString::Format(wxT("%i"), line->called0);
            dedge += wxT("x");
            dedge += wxT("\" ,arrowsize=\"0.50\", fontsize=\"9.00\", fontcolor=\"");
            dedge += cblack;
            dedge += wxT("\", penwidth=\"2.00\"];"); // labeldistance=\"4.00\",
            //
            m_OutputString += dedge + wxT("\n");
            //
            dedge.Clear();
        }
        it = it->GetNext();
    }
    m_OutputString += end_graph;

    if(!is_node) { // if the call graph is empty create new graph with label node
        m_OutputString = wxT("digraph e {0 [label=");
        m_OutputString +=
            _(wxString::Format("\"The call-graph is empty; the node threshold ceiling is %d !\"", wxRound(max_time)));
        m_OutputString += wxT(", shape=none, height=2, width=2, fontname=Arial, fontsize=14.00];}");
    }
}

bool DotWriter::SendToDotAppOutputDirectory(const wxString& dot_fn)
{ // (re)write dot.txt file
    wxFile pFile(dot_fn, wxFile::write);

    bool ok = pFile.Write(m_OutputString);

    return ok;
}

wxString DotWriter::OptionsShortNameAndParameters(const wxString& name)
{
    if((dwhidenamespaces || dwhideparams) && name.Contains(wxT('(')) && name.Contains(wxT(')'))) {
        wxString out = name;

        if(dwhidenamespaces) {
            wxRegEx re;
            // remove STL
            int start, end;
            while(GetOuterTempleate(out, &start, &end)) {
                out.Replace(out.Mid(start, end - start + 1), wxT("%STL%"));
            }
            out.Replace(wxT("%STL%"), wxT("<...>"));
            // remove namespace
            if(re.Compile(wxT("::[a-zA-Z_~]+[a-zA-Z_0-9<!=\\-\\+\\*/%]*\\(.*\\)[ ]*(const)?[ ]*$"), wxRE_ADVANCED) &&
               re.Matches(name)) {
                out = re.GetMatch(name);
                out.Replace(wxT("::"), wxEmptyString);
            }
        }

        if(dwhideparams) {
            out = out.BeforeFirst(wxT('('));
            out += wxT("()"); // for function return just ()
        }

        return out;

    } else if(name.Contains(wxT('(')) && name.Contains(wxT(')')) && dwstripparams) {
        wxString out = name.BeforeFirst(wxT('('));
        wxString sub = name.AfterFirst(wxT('(')).BeforeFirst(wxT(')'));
        if(sub.IsEmpty()) {
            out += wxT("\\n(\\n)");
            return out;
        } else if(sub.Contains(wxT(","))) {
            sub.Replace(wxT(","), wxT(",\\n"));
            out += wxT("\\n(\\n") + sub + wxT("\\n)");
            return out;
        } else {
            out += wxT("\\n(\\n") + sub + wxT("\\n)");
            return out;
        }
    } else
        return name;
}

int DotWriter::ReturnIndexForColor(float time, int dwc)
{
    int index = 0;
    struct colorRange {
        int downIndex;
        int upIndex;
        int indexColor;
    };

    colorRange* colorSelect;
    colorSelect = new colorRange[dwc];

    if(dwc == 1) {
        colorSelect[0].downIndex = 0;
        colorSelect[0].upIndex = 100;
        colorSelect[0].indexColor = 0;
    } else if(dwc == 2) {
        colorSelect[0].downIndex = 0;
        colorSelect[0].upIndex = 50;
        colorSelect[0].indexColor = 0;
        colorSelect[1].downIndex = 51;
        colorSelect[1].upIndex = 100;
        colorSelect[1].indexColor = 9;
    } else if(dwc > 2 && dwc < 11) {
        int numInterval = dwc - 1;
        float stepInterval = 8.0 / (float)numInterval;
        float lenInterval = 100.0 / (float)dwc;
        lenInterval = round(lenInterval);
        int afterModulo = 8 % numInterval;
        float restAdd = (float)afterModulo / numInterval;
        float addValue = 0.0;

        for(int i = 0; i < dwc; i++) {
            addValue += restAdd;
            if(i == 0) {
                colorSelect[i].downIndex = 0;
                colorSelect[i].upIndex = (int)lenInterval;
                colorSelect[i].indexColor = 0;
            } else if(i == (dwc - 1)) {
                colorSelect[i].downIndex = colorSelect[i - 1].upIndex + 1;
                colorSelect[i].upIndex = 100;
                colorSelect[i].indexColor = 9;
            } else if(i > 0 && i < (dwc - 1)) {
                int addValueNext = 0;
                if(0.8 < addValue && addValue < 1.2) {
                    addValueNext = 1;
                    addValue = 0;
                }
                colorSelect[i].downIndex = colorSelect[i - 1].upIndex + 1;
                colorSelect[i].upIndex = colorSelect[i - 1].upIndex + (int)lenInterval;
                colorSelect[i].indexColor =
                    (int)round((float)colorSelect[i - 1].indexColor + stepInterval) + addValueNext;
            }
        }
    }

    for(int i = 0; i < dwc; i++) {
        if((int)colorSelect[i].downIndex <= (int)time && (int)time <= (int)colorSelect[i].upIndex) {
            index = (int)colorSelect[i].indexColor;
            break;
        }
    }
    wxDELETEA(colorSelect);
    return index;
}

wxString DotWriter::DefineColorForNodeEdge(int index)
{
    wxString colors[10] = { wxT("#006837"), wxT("#1a9850"), wxT("#66bd63"), wxT("#a6d96a"), wxT("#d9ef8b"),
                            wxT("#fee08b"), wxT("#fdae61"), wxT("#f46d43"), wxT("#d73027"), wxT("#a50026") };
    return colors[index];
}

bool DotWriter::IsInArray(int index, const wxArrayInt& arr)
{
    for(unsigned int i = 0; i < arr.GetCount(); i++) {
        if(arr.Item(i) == index) return true;
    }
    return false;
}

wxString DotWriter::DefineColorForLabel(int index)
{
    if((index < 3) || (index > 6)) {
        return cwhite;
    } else {
        return cblack;
    }
}

bool DotWriter::GetOuterTempleate(const wxString& txt, int* start, int* end)
{
    int cnt = 0;
    int pos = 0;

    for(wxString::const_iterator it = txt.begin(); it != txt.end(); ++it) {
        if(*it == wxT('<')) {
            if(cnt == 0) *start = pos;
            cnt++;
        } else if(*it == wxT('>')) {
            cnt--;
            if(cnt == 0) *end = pos;
            return true;
        }
        pos++;
    }

    *start = -1;
    *end = -1;
    return false;
}