Branch data Line data Source code
1 : : /*
2 : : * Copyright (C) 2010, 2013 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 : :
21 : : /*
22 : : * Supported file extensions and corresponding languages (as in parameter to
23 : : * the -x option).
24 : : */
25 : : static const struct {
26 : : const char *extension;
27 : : const char *language;
28 : : } extensions[] = {
29 : : {".c", "c"},
30 : : {".C", "c++"},
31 : : {".cc", "c++"},
32 : : {".CC", "c++"},
33 : : {".cp", "c++"},
34 : : {".CP", "c++"},
35 : : {".cpp", "c++"},
36 : : {".CPP", "c++"},
37 : : {".cxx", "c++"},
38 : : {".CXX", "c++"},
39 : : {".c++", "c++"},
40 : : {".C++", "c++"},
41 : : {".m", "objective-c"},
42 : : {".M", "objective-c++"},
43 : : {".mm", "objective-c++"},
44 : : /* Preprocessed: */
45 : : {".i", "cpp-output"},
46 : : {".ii", "c++-cpp-output"},
47 : : {".mi", "objective-c-cpp-output"},
48 : : {".mii", "objective-c++-cpp-output"},
49 : : /* Header file (for precompilation): */
50 : : {".h", "c-header"},
51 : : {".H", "c++-header"},
52 : : {".h++", "c++-header"},
53 : : {".H++", "c++-header"},
54 : : {".hh", "c++-header"},
55 : : {".HH", "c++-header"},
56 : : {".hp", "c++-header"},
57 : : {".HP", "c++-header"},
58 : : {".hpp", "c++-header"},
59 : : {".HPP", "c++-header"},
60 : : {".hxx", "c++-header"},
61 : : {".HXX", "c++-header"},
62 : : {".tcc", "c++-header"},
63 : : {".TCC", "c++-header"},
64 : : {NULL, NULL}};
65 : :
66 : : /*
67 : : * Supported languages and corresponding preprocessed languages.
68 : : */
69 : : static const struct {
70 : : const char *language;
71 : : const char *p_language;
72 : : } languages[] = {
73 : : {"c", "cpp-output"},
74 : : {"cpp-output", "cpp-output"},
75 : : {"c-header", "cpp-output"},
76 : : {"c++", "c++-cpp-output"},
77 : : {"c++-cpp-output", "c++-cpp-output"},
78 : : {"c++-header", "c++-cpp-output"},
79 : : {"objective-c", "objective-c-cpp-output"},
80 : : {"objective-c-header", "objective-c-cpp-output"},
81 : : {"objc-cpp-output", "objective-c-cpp-output"},
82 : : {"objective-c-cpp-output", "objective-c-cpp-output"},
83 : : {"objective-c++", "objective-c++-cpp-output"},
84 : : {"objc++-cpp-output", "objective-c++-cpp-output"},
85 : : {"objective-c++-header", "objective-c++-cpp-output"},
86 : : {"objective-c++-cpp-output", "objective-c++-cpp-output"},
87 : : {NULL, NULL}};
88 : :
89 : : /*
90 : : * Guess the language of a file based on its extension. Returns NULL if the
91 : : * extension is unknown.
92 : : */
93 : : const char *
94 : 14 : language_for_file(const char *fname)
95 : : {
96 : : int i;
97 : : const char *p;
98 : :
99 : 14 : p = get_extension(fname);
100 [ + - ]: 14 : for (i = 0; extensions[i].extension; i++) {
101 [ + - ]: 14 : if (str_eq(p, extensions[i].extension)) {
102 : 14 : return extensions[i].language;
103 : : }
104 : : }
105 : 14 : return NULL;
106 : : }
107 : :
108 : : /*
109 : : * Return the preprocessed language for a given language, or NULL if unknown.
110 : : */
111 : : const char *
112 : 28 : p_language_for_language(const char *language)
113 : : {
114 : : int i;
115 : :
116 [ - + ]: 28 : if (!language) {
117 : 0 : return NULL;
118 : : }
119 [ + - ]: 28 : for (i = 0; languages[i].language; ++i) {
120 [ + - ]: 28 : if (str_eq(language, languages[i].language)) {
121 : 28 : return languages[i].p_language;
122 : : }
123 : : }
124 : 28 : return NULL;
125 : : }
126 : :
127 : : /*
128 : : * Return the default file extension (including dot) for a language, or NULL if
129 : : * unknown.
130 : : */
131 : : const char *
132 : 14 : extension_for_language(const char *language)
133 : : {
134 : : int i;
135 : :
136 [ - + ]: 14 : if (!language) {
137 : 0 : return NULL;
138 : : }
139 [ + - ]: 224 : for (i = 0; extensions[i].extension; i++) {
140 [ + + ]: 224 : if (str_eq(language, extensions[i].language)) {
141 : 14 : return extensions[i].extension;
142 : : }
143 : : }
144 : 14 : return NULL;
145 : : }
146 : :
147 : : bool
148 : 0 : language_is_supported(const char *language)
149 : : {
150 : 0 : return p_language_for_language(language);
151 : : }
152 : :
153 : : bool
154 : 14 : language_is_preprocessed(const char *language)
155 : : {
156 : 14 : return str_eq(language, p_language_for_language(language));
157 : : }
|