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 #include <string.h>
36
37 #include <libias/array.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 #include "rules.h"
47
48 enum DedupAction {
49 DEFAULT,
50 USES,
51 };
52
53 struct WalkerData {
54 struct Parser *parser;
55 };
56
57 // Prototypes
58 static enum ASTWalkState refactor_dedup_tokens_walker(struct AST *, struct WalkerData *);
59
60 enum ASTWalkState
refactor_dedup_tokens_walker(struct AST * node,struct WalkerData * this)61 refactor_dedup_tokens_walker(struct AST *node, struct WalkerData *this)
62 {
63 SCOPE_MEMPOOL(pool);
64
65 switch (node->type) {
66 case AST_VARIABLE:
67 if ((parser_settings(this->parser).behavior & PARSER_OUTPUT_EDITED) && !node->edited) {
68 return AST_WALK_CONTINUE;
69 } else if (skip_dedup(this->parser, node->variable.name, node->variable.modifier)) {
70 return AST_WALK_CONTINUE;
71 } else {
72 struct Set *seen = mempool_set(pool, str_compare, NULL);
73 struct Set *uses = mempool_set(pool, str_compare, NULL);
74 enum DedupAction action = DEFAULT;
75 char *helper = NULL;
76 if (is_options_helper(pool, this->parser, node->variable.name, NULL, &helper, NULL)) {
77 if (strcmp(helper, "USES") == 0 || strcmp(helper, "USES_OFF") == 0) {
78 action = USES;
79 }
80 } else if (strcmp(node->variable.name, "USES") == 0) {
81 action = USES;
82 }
83 struct Array *words = mempool_array(pool);
84 ARRAY_FOREACH(node->variable.words, const char *, word) {
85 // XXX: Handle *_DEPENDS (turn 'RUN_DEPENDS=foo>=1.5.6:misc/foo foo>0:misc/foo'
86 // into 'RUN_DEPENDS=foo>=1.5.6:misc/foo')?
87 switch (action) {
88 case USES: {
89 char *buf = str_dup(pool, word);
90 char *args = strchr(buf, ':');
91 if (args) {
92 *args = 0;
93 }
94 // We follow the semantics of the ports framework.
95 // 'USES=compiler:c++11-lang compiler:c++14-lang' is
96 // semantically equivalent to just USES=compiler:c++11-lang
97 // since compiler_ARGS has already been set once before.
98 // As such compiler:c++14-lang can be dropped entirely.
99 if (!set_contains(uses, buf)) {
100 array_append(words, word);
101 set_add(uses, buf);
102 set_add(seen, word);
103 }
104 break;
105 } default:
106 if (!set_contains(seen, word)) {
107 array_append(words, word);
108 set_add(seen, word);
109 }
110 break;
111 }
112 }
113 if (array_len(words) < array_len(node->variable.words)) {
114 node->edited = true;
115 array_truncate(node->variable.words);
116 ARRAY_JOIN(node->variable.words, words);
117 }
118 }
119 break;
120 default:
121 break;
122 }
123
124 AST_WALK_DEFAULT(refactor_dedup_tokens_walker, node, this);
125
126 return AST_WALK_CONTINUE;
127 }
128
PARSER_EDIT(refactor_dedup_tokens)129 PARSER_EDIT(refactor_dedup_tokens)
130 {
131 if (userdata != NULL) {
132 parser_set_error(parser, PARSER_ERROR_INVALID_ARGUMENT, NULL);
133 return;
134 }
135
136 refactor_dedup_tokens_walker(root, &(struct WalkerData){
137 .parser = parser,
138 });
139 }
140