2 * Copyright (C) 2010-2011 Joel Rosdahl
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; either version 3 of the License, or (at your option)
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 51
16 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 * This file contains tests for the processing of compiler arguments.
24 #include "test/framework.h"
25 #include "test/util.h"
27 TEST_SUITE(argument_processing)
29 TEST(dash_E_should_result_in_called_for_preprocessing)
31 struct args *orig = args_init_from_string("cc -c foo.c -E");
32 struct args *preprocessed, *compiler;
34 create_file("foo.c", "");
35 CHECK(!cc_process_args(orig, &preprocessed, &compiler));
36 CHECK_UNS_EQ(1, stats_get_pending(STATS_PREPROCESSING));
41 TEST(dash_M_should_be_unsupported)
43 struct args *orig = args_init_from_string("cc -c foo.c -M");
44 struct args *preprocessed, *compiler;
46 create_file("foo.c", "");
47 CHECK(!cc_process_args(orig, &preprocessed, &compiler));
48 CHECK_UNS_EQ(1, stats_get_pending(STATS_UNSUPPORTED));
53 TEST(dependency_flags_should_only_be_sent_to_the_preprocessor)
56 "cc -c -MD -MMD -MP -MF foo.d -MT mt1 -MT mt2 -MQ mq1 -MQ mq2" \
57 " -Wp,-MD,wpmd -Wp,-MMD,wpmmd"
58 struct args *orig = args_init_from_string(CMD " foo.c -o foo.o");
59 struct args *exp_cpp = args_init_from_string(CMD);
61 struct args *exp_cc = args_init_from_string("cc -c");
62 struct args *act_cpp = NULL, *act_cc = NULL;
63 create_file("foo.c", "");
65 CHECK(cc_process_args(orig, &act_cpp, &act_cc));
66 CHECK_ARGS_EQ_FREE12(exp_cpp, act_cpp);
67 CHECK_ARGS_EQ_FREE12(exp_cc, act_cc);
72 TEST(dependency_flags_that_take_an_argument_should_not_require_space_delimiter)
74 struct args *orig = args_init_from_string(
75 "cc -c -MMD -MFfoo.d -MT mt -MTmt -MQmq foo.c -o foo.o");
76 struct args *exp_cpp = args_init_from_string(
77 "cc -c -MMD -MFfoo.d -MT mt -MTmt -MQmq");
78 struct args *exp_cc = args_init_from_string("cc -c");
79 struct args *act_cpp = NULL, *act_cc = NULL;
80 create_file("foo.c", "");
82 CHECK(cc_process_args(orig, &act_cpp, &act_cc));
83 CHECK_ARGS_EQ_FREE12(exp_cpp, act_cpp);
84 CHECK_ARGS_EQ_FREE12(exp_cc, act_cc);
89 TEST(sysroot_should_be_rewritten_if_basedir_is_used)
91 extern char *base_dir;
92 extern char *current_working_dir;
94 args_init_from_string("cc --sysroot=/some/directory -c foo.c");
95 struct args *act_cpp = NULL, *act_cc = NULL;
96 create_file("foo.c", "");
98 CHECK(cc_process_args(orig, &act_cpp, &act_cc));
99 CHECK_STR_EQ(act_cpp->argv[1], "--sysroot=/some/directory");
103 current_working_dir = get_cwd();
105 CHECK(cc_process_args(orig, &act_cpp, &act_cc));
106 CHECK(str_startswith(act_cpp->argv[1], "--sysroot=../"));
110 current_working_dir = NULL;
113 TEST(MF_flag_with_immediate_argument_should_work_as_last_argument)
115 struct args *orig = args_init_from_string(
116 "cc -c foo.c -o foo.o -MMD -MT bar -MFfoo.d");
117 struct args *exp_cpp = args_init_from_string(
118 "cc -c -MMD -MT bar -MFfoo.d");
119 struct args *exp_cc = args_init_from_string("cc -c");
120 struct args *act_cpp = NULL, *act_cc = NULL;
121 create_file("foo.c", "");
123 CHECK(cc_process_args(orig, &act_cpp, &act_cc));
124 CHECK_ARGS_EQ_FREE12(exp_cpp, act_cpp);
125 CHECK_ARGS_EQ_FREE12(exp_cc, act_cc);
130 TEST(MT_flag_with_immediate_argument_should_work_as_last_argument)
132 struct args *orig = args_init_from_string(
133 "cc -c foo.c -o foo.o -MMD -MFfoo.d -MT foo -MTbar");
134 struct args *exp_cpp = args_init_from_string(
135 "cc -c -MMD -MFfoo.d -MT foo -MTbar");
136 struct args *exp_cc = args_init_from_string("cc -c");
137 struct args *act_cpp = NULL, *act_cc = NULL;
138 create_file("foo.c", "");
140 CHECK(cc_process_args(orig, &act_cpp, &act_cc));
141 CHECK_ARGS_EQ_FREE12(exp_cpp, act_cpp);
142 CHECK_ARGS_EQ_FREE12(exp_cc, act_cc);
147 TEST(MQ_flag_with_immediate_argument_should_work_as_last_argument)
149 struct args *orig = args_init_from_string(
150 "cc -c foo.c -o foo.o -MMD -MFfoo.d -MQ foo -MQbar");
151 struct args *exp_cpp = args_init_from_string(
152 "cc -c -MMD -MFfoo.d -MQ foo -MQbar");
153 struct args *exp_cc = args_init_from_string("cc -c");
154 struct args *act_cpp = NULL, *act_cc = NULL;
155 create_file("foo.c", "");
157 CHECK(cc_process_args(orig, &act_cpp, &act_cc));
158 CHECK_ARGS_EQ_FREE12(exp_cpp, act_cpp);
159 CHECK_ARGS_EQ_FREE12(exp_cc, act_cc);
164 TEST(MQ_flag_without_immediate_argument_should_not_add_MQobj)
166 struct args *orig = args_init_from_string(
167 "gcc -c -MD -MP -MFfoo.d -MQ foo.d foo.c");
168 struct args *exp_cpp = args_init_from_string(
169 "gcc -c -MD -MP -MFfoo.d -MQ foo.d");
170 struct args *exp_cc = args_init_from_string(
172 struct args *act_cpp = NULL, *act_cc = NULL;
173 create_file("foo.c", "");
175 CHECK(cc_process_args(orig, &act_cpp, &act_cc));
176 CHECK_ARGS_EQ_FREE12(exp_cpp, act_cpp);
177 CHECK_ARGS_EQ_FREE12(exp_cc, act_cc);
182 TEST(MT_flag_without_immediate_argument_should_not_add_MTobj)
184 struct args *orig = args_init_from_string(
185 "gcc -c -MD -MP -MFfoo.d -MT foo.d foo.c");
186 struct args *exp_cpp = args_init_from_string(
187 "gcc -c -MD -MP -MFfoo.d -MT foo.d");
188 struct args *exp_cc = args_init_from_string(
190 struct args *act_cpp = NULL, *act_cc = NULL;
191 create_file("foo.c", "");
193 CHECK(cc_process_args(orig, &act_cpp, &act_cc));
194 CHECK_ARGS_EQ_FREE12(exp_cpp, act_cpp);
195 CHECK_ARGS_EQ_FREE12(exp_cc, act_cc);
200 TEST(MQ_flag_with_immediate_argument_should_add_MQobj)
202 struct args *orig = args_init_from_string(
203 "gcc -c -MD -MP -MFfoo.d -MQfoo.d foo.c");
204 struct args *exp_cpp = args_init_from_string(
205 "gcc -c -MD -MP -MFfoo.d -MQfoo.d -MQ foo.o");
206 struct args *exp_cc = args_init_from_string(
208 struct args *act_cpp = NULL, *act_cc = NULL;
209 create_file("foo.c", "");
211 CHECK(cc_process_args(orig, &act_cpp, &act_cc));
212 CHECK_ARGS_EQ_FREE12(exp_cpp, act_cpp);
213 CHECK_ARGS_EQ_FREE12(exp_cc, act_cc);
218 TEST(MT_flag_with_immediate_argument_should_add_MQobj)
220 struct args *orig = args_init_from_string(
221 "gcc -c -MD -MP -MFfoo.d -MTfoo.d foo.c");
222 struct args *exp_cpp = args_init_from_string(
223 "gcc -c -MD -MP -MFfoo.d -MTfoo.d -MQ foo.o");
224 struct args *exp_cc = args_init_from_string(
226 struct args *act_cpp = NULL, *act_cc = NULL;
227 create_file("foo.c", "");
229 CHECK(cc_process_args(orig, &act_cpp, &act_cc));
230 CHECK_ARGS_EQ_FREE12(exp_cpp, act_cpp);
231 CHECK_ARGS_EQ_FREE12(exp_cc, act_cc);