]> git.ipfire.org Git - thirdparty/ccache.git/blob - language.c
Improve description on how to fix bad object files in the cache
[thirdparty/ccache.git] / language.c
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
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", "objc-cpp-output"},
48 {".mii", "objc++-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", "objc-cpp-output"},
80 {"objective-c-header", "objc-cpp-output"},
81 {"objc-cpp-output", "objc-cpp-output"},
82 {"objective-c++", "objc++-cpp-output"},
83 {"objc++-cpp-output", "objc++-cpp-output"},
84 {"objective-c++-header", "objc++-cpp-output"},
85 {NULL, NULL}};
86
87 /*
88 * Guess the language of a file based on its extension. Returns NULL if the
89 * extension is unknown.
90 */
91 const char *
92 language_for_file(const char *fname)
93 {
94 int i;
95 const char *p;
96
97 p = get_extension(fname);
98 for (i = 0; extensions[i].extension; i++) {
99 if (str_eq(p, extensions[i].extension)) {
100 return extensions[i].language;
101 }
102 }
103 return NULL;
104 }
105
106 /*
107 * Return the preprocessed language for a given language, or NULL if unknown.
108 */
109 const char *
110 p_language_for_language(const char *language)
111 {
112 int i;
113
114 if (!language) {
115 return NULL;
116 }
117 for (i = 0; languages[i].language; ++i) {
118 if (str_eq(language, languages[i].language)) {
119 return languages[i].p_language;
120 }
121 }
122 return NULL;
123 }
124
125 /*
126 * Return the default file extension (including dot) for a language, or NULL if
127 * unknown.
128 */
129 const char *
130 extension_for_language(const char *language)
131 {
132 int i;
133
134 if (!language) {
135 return NULL;
136 }
137 for (i = 0; extensions[i].extension; i++) {
138 if (str_eq(language, extensions[i].language)) {
139 return extensions[i].extension;
140 }
141 }
142 return NULL;
143 }
144
145 bool
146 language_is_supported(const char *language)
147 {
148 return p_language_for_language(language) != NULL;
149 }
150
151 bool
152 language_is_preprocessed(const char *language)
153 {
154 return str_eq(language, p_language_for_language(language));
155 }