1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2019 Tobias Kortkamp <tobik@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include "config.h"
30
31 #include <inttypes.h>
32 #include <stdbool.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35
36 #include <libias/array.h>
37 #include <libias/color.h>
38 #include <libias/flow.h>
39 #include <libias/mempool.h>
40 #include <libias/set.h>
41 #include <libias/str.h>
42
43 #include "ast.h"
44 #include "parser.h"
45 #include "parser/edits.h"
46
47 struct WalkerData {
48 struct Set *seen;
49 struct Set *seen_in_cond;
50 struct Set *clones;
51 struct Mempool *clones_pool;
52 };
53
54 // Prototypes
55 static void add_clones(struct WalkerData *);
56 static enum ASTWalkState lint_clones_walker(struct AST *, struct WalkerData *, uint32_t);
57
58 void
add_clones(struct WalkerData * this)59 add_clones(struct WalkerData *this)
60 {
61 SET_FOREACH(this->seen_in_cond, const char *, name) {
62 if (set_contains(this->seen, name) && !set_contains(this->clones, name)) {
63 set_add(this->clones, str_dup(this->clones_pool, name));
64 }
65 }
66 set_truncate(this->seen_in_cond);
67 }
68
69 enum ASTWalkState
lint_clones_walker(struct AST * node,struct WalkerData * this,uint32_t in_conditional)70 lint_clones_walker(struct AST *node, struct WalkerData *this, uint32_t in_conditional)
71 {
72 switch (node->type) {
73 case AST_FOR:
74 case AST_IF:
75 case AST_INCLUDE:
76 in_conditional++;
77 break;
78 case AST_VARIABLE:
79 if (node->variable.modifier == AST_VARIABLE_MODIFIER_ASSIGN) {
80 if (in_conditional > 0) {
81 set_add(this->seen_in_cond, node->variable.name);
82 } else if (set_contains(this->seen, node->variable.name)) {
83 if (!set_contains(this->clones, node->variable.name)) {
84 set_add(this->clones, str_dup(this->clones_pool, node->variable.name));
85 }
86 } else {
87 set_add(this->seen, node->variable.name);
88 }
89 }
90 break;
91 default:
92 break;
93 }
94
95 AST_WALK_DEFAULT(lint_clones_walker, node, this, in_conditional);
96
97 if (in_conditional == 0) {
98 add_clones(this);
99 }
100
101 return AST_WALK_CONTINUE;
102 }
103
PARSER_EDIT(lint_clones)104 PARSER_EDIT(lint_clones)
105 {
106 SCOPE_MEMPOOL(pool);
107
108 struct Set **clones_ret = userdata;
109 bool no_color = parser_settings(parser).behavior & PARSER_OUTPUT_NO_COLOR;
110
111 struct Mempool *clones_pool = mempool_pool(extpool);
112 struct WalkerData this = {
113 .seen = mempool_set(pool, str_compare, NULL),
114 .seen_in_cond = mempool_set(pool, str_compare, NULL),
115 .clones_pool = clones_pool,
116 .clones = mempool_set(clones_pool, str_compare, NULL),
117 };
118 lint_clones_walker(root, &this, 0);
119
120 if (clones_ret == NULL && set_len(this.clones) > 0) {
121 if (!no_color) {
122 parser_enqueue_output(parser, ANSI_COLOR_CYAN);
123 }
124 parser_enqueue_output(parser, "# Variables set twice or more\n");
125 if (!no_color) {
126 parser_enqueue_output(parser, ANSI_COLOR_RESET);
127 }
128 SET_FOREACH(this.clones, const char *, name) {
129 parser_enqueue_output(parser, name);
130 parser_enqueue_output(parser, "\n");
131 }
132 }
133
134 if (clones_ret) {
135 *clones_ret = this.clones;
136 } else {
137 mempool_release(extpool, clones_pool);
138 }
139 }
140