From bde6e9e547a4a17e8ea9901842d1980762b8ed08 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Wed, 17 Jun 2015 16:55:12 +0300 Subject: Add file for analysis *_REF nodes. Add null pointer analysis in COMPOUND_REF node. --- src/Makefile.files | 2 ++ src/analysis/analysis.cpp | 6 +++++ src/analysis/ref.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++++++ src/analysis/ref.h | 33 +++++++++++++++++++++++ 4 files changed, 108 insertions(+) create mode 100644 src/analysis/ref.cpp create mode 100644 src/analysis/ref.h diff --git a/src/Makefile.files b/src/Makefile.files index 9ffef9d..c95e6e5 100644 --- a/src/Makefile.files +++ b/src/Makefile.files @@ -6,6 +6,8 @@ SRC = analysis/analysis.cpp \ analysis/expression.h \ analysis/function.cpp \ analysis/function.h \ + analysis/ref.cpp \ + analysis/ref.h \ analysis/statement.cpp \ analysis/statement.h \ analysis/walkitem.h \ diff --git a/src/analysis/analysis.cpp b/src/analysis/analysis.cpp index 86fd683..f3a9631 100644 --- a/src/analysis/analysis.cpp +++ b/src/analysis/analysis.cpp @@ -25,6 +25,7 @@ #include "analysis/declaration.h" #include "analysis/expression.h" #include "analysis/function.h" +#include "analysis/ref.h" #include "analysis/statement.h" #include "analysis/walkitem.h" @@ -35,6 +36,8 @@ #include "nodes/expr/modify_expr.h" #include "nodes/expr/pointerplus_expr.h" +#include "nodes/ref/component_ref.h" + #include "nodes/stmt/if_stmt.h" #include "localconsts.h" @@ -146,6 +149,9 @@ void analyseNode(Node *node, const WalkItem &wi, WalkItem &wo) case IF_STMT: analyseIfStmt(static_cast(node), wi2, wo); break; + case COMPONENT_REF: + analyseComponentRef(static_cast(node), wi2, wo); + break; default: break; } diff --git a/src/analysis/ref.cpp b/src/analysis/ref.cpp new file mode 100644 index 0000000..575fd68 --- /dev/null +++ b/src/analysis/ref.cpp @@ -0,0 +1,67 @@ +/* + * 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 . + */ + +#include "analysis/expression.h" + +#include "command.h" +#include "logger.h" + +#include "analysis/analysis.h" +#include "analysis/walkitem.h" + +#include "nodes/expr/addr_expr.h" +#include "nodes/expr/modify_expr.h" +#include "nodes/expr/pointerplus_expr.h" + +#include "nodes/ref/component_ref.h" +#include "nodes/ref/indirect_ref.h" + +#include + +#include "localconsts.h" + +namespace Analysis +{ + +void analyseComponentRef(ComponentRefNode *node, const WalkItem &wi, WalkItem &wo) +{ + // need atleast one arg for check + if (!node->object || command == FindArgs) + return; + + Node *arg = node->object; + if (arg && arg->nodeType == INDIRECT_REF) + { + IndirectRefNode *refNode = static_cast(arg); + // need atleast one arg for check + if (refNode->ref == nullptr) + return; + arg = refNode->ref; + if (arg->nodeType == PARM_DECL) + { + if (wi.checkNullVars.find(arg->label) != wi.checkNullVars.end()) + { + Log::warn(findBackLocation(node), + "Using variable without check for NULL"); + } + } + } +} + +} diff --git a/src/analysis/ref.h b/src/analysis/ref.h new file mode 100644 index 0000000..b562d01 --- /dev/null +++ b/src/analysis/ref.h @@ -0,0 +1,33 @@ +/* + * 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 . + */ + +#ifndef ANALYSIS_REF_H +#define ANALYSIS_REF_H + +#include "includes.h" + +struct ComponentRefNode; +struct WalkItem; + +namespace Analysis +{ + void analyseComponentRef(ComponentRefNode *node, const WalkItem &wi, WalkItem &wo); +} + +#endif // ANALYSIS_REF_H -- cgit v1.2.3-70-g09d2