~ubuntu-branches/debian/stretch/ccache/stretch

« back to all changes in this revision

Viewing changes to compopt.c

  • Committer: Bazaar Package Importer
  • Author(s): Joel Rosdahl
  • Date: 2010-12-02 21:05:17 UTC
  • mfrom: (5.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20101202210517-ji5owl2qa3s5c1rg
New upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2010 Joel Rosdahl
 
3
 *
 
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)
 
7
 * any later version.
 
8
 *
 
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
 
12
 * more details.
 
13
 *
 
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
 
17
 */
 
18
 
 
19
#include "ccache.h"
 
20
#include "compopt.h"
 
21
 
 
22
#define TOO_HARD         (1 << 0)
 
23
#define TOO_HARD_DIRECT  (1 << 1)
 
24
#define TAKES_ARG        (1 << 2)
 
25
#define TAKES_CONCAT_ARG (1 << 3)
 
26
#define TAKES_PATH       (1 << 4)
 
27
#define AFFECTS_CPP      (1 << 5)
 
28
 
 
29
struct compopt {
 
30
        const char *name;
 
31
        int type;
 
32
};
 
33
 
 
34
static const struct compopt compopts[] = {
 
35
        {"--coverage",      TOO_HARD},
 
36
        {"--param",         TAKES_ARG},
 
37
        {"-A",              TAKES_ARG},
 
38
        {"-D",              AFFECTS_CPP | TAKES_ARG | TAKES_CONCAT_ARG},
 
39
        {"-E",              TOO_HARD},
 
40
        {"-G",              TAKES_ARG},
 
41
        {"-I",              AFFECTS_CPP | TAKES_ARG | TAKES_CONCAT_ARG | TAKES_PATH},
 
42
        {"-L",              TAKES_ARG},
 
43
        {"-M",              TOO_HARD},
 
44
        {"-MF",             TAKES_ARG},
 
45
        {"-MM",             TOO_HARD},
 
46
        {"-MQ",             TAKES_ARG},
 
47
        {"-MT",             TAKES_ARG},
 
48
        {"-U",              AFFECTS_CPP | TAKES_ARG | TAKES_CONCAT_ARG},
 
49
        {"-V",              TAKES_ARG},
 
50
        {"-Xassembler",     TAKES_ARG},
 
51
        {"-Xlinker",        TAKES_ARG},
 
52
        {"-Xpreprocessor",  TOO_HARD_DIRECT | TAKES_ARG},
 
53
        {"-aux-info",       TAKES_ARG},
 
54
        {"-b",              TAKES_ARG},
 
55
        {"-fbranch-probabilities", TOO_HARD},
 
56
        {"-fprofile-arcs",  TOO_HARD},
 
57
        {"-fprofile-generate", TOO_HARD},
 
58
        {"-fprofile-use",   TOO_HARD},
 
59
        {"-frepo",          TOO_HARD},
 
60
        {"-ftest-coverage", TOO_HARD},
 
61
        {"-idirafter",      AFFECTS_CPP | TAKES_ARG | TAKES_PATH},
 
62
        {"-imacros",        AFFECTS_CPP | TAKES_ARG | TAKES_PATH},
 
63
        {"-imultilib",      AFFECTS_CPP | TAKES_ARG | TAKES_PATH},
 
64
        {"-include",        AFFECTS_CPP | TAKES_ARG | TAKES_PATH},
 
65
        {"-install_name",   TAKES_ARG}, /* Darwin linker option */
 
66
        {"-iprefix",        AFFECTS_CPP | TAKES_ARG | TAKES_PATH},
 
67
        {"-iquote",         AFFECTS_CPP | TAKES_ARG | TAKES_PATH},
 
68
        {"-isysroot",       AFFECTS_CPP | TAKES_ARG | TAKES_PATH},
 
69
        {"-isystem",        AFFECTS_CPP | TAKES_ARG | TAKES_PATH},
 
70
        {"-iwithprefix",    AFFECTS_CPP | TAKES_ARG | TAKES_PATH},
 
71
        {"-iwithprefixbefore", AFFECTS_CPP | TAKES_ARG | TAKES_PATH},
 
72
        {"-nostdinc",       AFFECTS_CPP},
 
73
        {"-nostdinc++",     AFFECTS_CPP},
 
74
        {"-save-temps",     TOO_HARD},
 
75
        {"-u",              TAKES_ARG},
 
76
};
 
77
 
 
78
static int
 
79
compare_compopts(const void *key1, const void *key2)
 
80
{
 
81
        const struct compopt *opt1 = (const struct compopt *)key1;
 
82
        const struct compopt *opt2 = (const struct compopt *)key2;
 
83
        return strcmp(opt1->name, opt2->name);
 
84
}
 
85
 
 
86
static const struct compopt *
 
87
find(const char *option)
 
88
{
 
89
        struct compopt key;
 
90
        key.name = option;
 
91
        return bsearch(
 
92
                &key, compopts, sizeof(compopts) / sizeof(compopts[0]),
 
93
                sizeof(compopts[0]), compare_compopts);
 
94
}
 
95
 
 
96
/* Runs fn on the first two characters of option. */
 
97
bool
 
98
compopt_short(bool (*fn)(const char *), const char *option)
 
99
{
 
100
        char *short_opt = x_strndup(option, 2);
 
101
        bool retval = fn(short_opt);
 
102
        free(short_opt);
 
103
        return retval;
 
104
}
 
105
 
 
106
/* For test purposes. */
 
107
bool
 
108
compopt_verify_sortedness(void)
 
109
{
 
110
        size_t i;
 
111
        for (i = 1; i < sizeof(compopts)/sizeof(compopts[0]); i++) {
 
112
                if (strcmp(compopts[i-1].name, compopts[i].name) >= 0) {
 
113
                        fprintf(stderr,
 
114
                                "compopt_verify_sortedness: %s >= %s\n",
 
115
                                compopts[i-1].name,
 
116
                                compopts[i].name);
 
117
                        return false;
 
118
                }
 
119
        }
 
120
        return true;
 
121
}
 
122
 
 
123
bool
 
124
compopt_affects_cpp(const char *option)
 
125
{
 
126
        const struct compopt *co = find(option);
 
127
        return co && (co->type & AFFECTS_CPP);
 
128
}
 
129
 
 
130
bool
 
131
compopt_too_hard(const char *option)
 
132
{
 
133
        const struct compopt *co = find(option);
 
134
        return co && (co->type & TOO_HARD);
 
135
}
 
136
 
 
137
bool
 
138
compopt_too_hard_for_direct_mode(const char *option)
 
139
{
 
140
        const struct compopt *co = find(option);
 
141
        return co && (co->type & TOO_HARD_DIRECT);
 
142
}
 
143
 
 
144
bool
 
145
compopt_takes_path(const char *option)
 
146
{
 
147
        const struct compopt *co = find(option);
 
148
        return co && (co->type & TAKES_PATH);
 
149
}
 
150
 
 
151
bool
 
152
compopt_takes_arg(const char *option)
 
153
{
 
154
        const struct compopt *co = find(option);
 
155
        return co && (co->type & TAKES_ARG);
 
156
}