summaryrefslogtreecommitdiff
path: root/src/analysis/statement.cpp
blob: 394dcfe388df0bb13220426cfeb104314af26776 (plain) (blame)
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
/*
 *  Copyright (C) 2015  Andrei Karas
 *
 *  This file is part of AstDumper.
 *
 *  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 3 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "analysis/statement.h"

#include "command.h"
#include "logger.h"

#include "analysis/analysis.h"
#include "analysis/walkitem.h"

#include "nodes/expr/eq_expr.h"
#include "nodes/expr/modify_expr.h"
#include "nodes/expr/ne_expr.h"
#include "nodes/expr/pointerplus_expr.h"

#include "nodes/ref/indirect_ref.h"

#include "nodes/stmt/if_stmt.h"

#include <set>

#include "localconsts.h"

namespace Analysis
{

void analyseIfStmt(IfStmtNode *node, const WalkItem &wi, WalkItem &wo)
{
    // need condition node
    if (!node->condition || command == FindArgs)
        return;

    Node *condNode = skipNop(node->condition);
    if (condNode->nodeType == EQ_EXPR)
    {   // if (... == ..)
        EqExprNode *eq = static_cast<EqExprNode*>(condNode);
        // need atleast two operands for EQ_EXPR node
        if (eq->args.size() < 2)
            return;

        // PARM_DECL?
        Node *node1 = skipNop(eq->args[0]);
        // INTEGER_CST?
        Node *node2 = skipNop(eq->args[1]);
        // if (var == 0)
        if (node1 &&
            node2 &&
            node1->nodeType == PARM_DECL &&
            node2->nodeType == INTEGER_CST &&
            wi.checkNullVars.find(node1->label) != wi.checkNullVars.end() &&
            node2->label == "0")
        {
            WalkItem wi2 = wi;
            // found check for parameter and 0.
            // walking to then branch
            walkTree(node->thenNode, wi2, wo);
            wo.removeNullVars.clear();
            const bool returned = wo.isReturned;
//            if (wo.isReturned)
//                Log::log("wo.isReturned 1\n");

            // From else branch remove variable what we just found.
            wi2 = wi;
            wi2.checkNullVars.erase(node1->label);
            walkTree(node->elseNode, wi2, wo);
            wo.removeNullVars.clear();
            wo.stopWalking = true;
//            if (wo.isReturned)
//                Log::log("wo.isReturned 2\n");
            wo.isReturned = false;

            if (returned)
            {
                //Log::log("add removeNullVars: %s\n", node1->label.c_str());
                // add variable for ignore for all parent nodes except special like IF_STMT
                wo.removeNullVars.insert(node1->label);
                wo.checkNullVars.erase(node1->label);
            }

            return;
        }
    }
    else if (condNode->nodeType == NE_EXPR)
    {   // if (... != ..)
        NeExprNode *ne = static_cast<NeExprNode*>(condNode);
        // need atleast two operands for NE_EXPR node
        if (ne->args.size() < 2)
            return;

        // PARM_DECL?
        Node *node1 = skipNop(ne->args[0]);
        // INTEGER_CST?
        Node *node2 = skipNop(ne->args[1]);
        // if (var != 0)
        if (node1 &&
            node2 &&
            node1->nodeType == PARM_DECL &&
            node2->nodeType == INTEGER_CST &&
            wi.checkNullVars.find(node1->label) != wi.checkNullVars.end() &&
            node2->label == "0")
        {
            // found check for parameter and 0.
            // walking to then branch
            WalkItem wi2 = wi;
            wi2.checkNullVars.erase(node1->label);
            // From then branch remove variable what we just found.
            walkTree(node->thenNode, wi2, wo);
//            if (wo.isReturned)
//                Log::log("wo.isReturned 3\n");
            wo.removeNullVars.clear();
            wi2 = wi;
            // walking else node with all variables
            walkTree(node->elseNode, wi2, wo);
//            if (wo.isReturned)
//                Log::log("wo.isReturned 4\n");
            wo.removeNullVars.clear();
            wo.stopWalking = true;

            if (wo.isReturned && node->elseNode)
            {
                //Log::log("add removeNullVars: %s\n", node1->label.c_str());
                // add variable for ignore for all parent nodes except special like IF_STMT
                wo.removeNullVars.insert(node1->label);
                wo.checkNullVars.erase(node1->label);
            }
            wo.isReturned = false;
            return;
        }
    }

    // default case
    walkTree(node->thenNode, wi, wo);
    walkTree(node->elseNode, wi, wo);
    wo.removeNullVars.clear();
//    if (wo.isReturned)
//        Log::log("wo.isReturned 5\n");
    wo.isReturned = false;
    wo.stopWalking = true;
}

}