summaryrefslogtreecommitdiff
path: root/language.c
blob: acbeb89c39aa8454e3d22408fa1d60d6695ac0e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 * Copyright (C) 2010 Joel Rosdahl
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "ccache.h"

/*
 * Supported file extensions and corresponding languages (as in parameter to
 * the -x option).
 */
static const struct {
	const char *extension;
	const char *language;
} extensions[] = {
	{".c",   "c"},
	{".C",   "c++"},
	{".cc",  "c++"},
	{".CC",  "c++"},
	{".cp",  "c++"},
	{".CP",  "c++"},
	{".cpp", "c++"},
	{".CPP", "c++"},
	{".cxx", "c++"},
	{".CXX", "c++"},
	{".c++", "c++"},
	{".C++", "c++"},
	{".m",   "objective-c"},
	{".M",   "objective-c++"},
	{".mm",  "objective-c++"},
	/* Preprocessed: */
	{".i",   "cpp-output"},
	{".ii",  "c++-cpp-output"},
	{".mi",  "objc-cpp-output"},
	{".mii", "objc++-cpp-output"},
	/* Header file (for precompilation): */
	{".h",   "c-header"},
	{".H",   "c++-header"},
	{".h++", "c++-header"},
	{".H++", "c++-header"},
	{".hh",  "c++-header"},
	{".HH",  "c++-header"},
	{".hp",  "c++-header"},
	{".HP",  "c++-header"},
	{".hpp", "c++-header"},
	{".HPP", "c++-header"},
	{".hxx", "c++-header"},
	{".HXX", "c++-header"},
	{".tcc", "c++-header"},
	{".TCC", "c++-header"},
	{NULL,  NULL}};

/*
 * Supported languages and corresponding preprocessed languages.
 */
static const struct {
	const char *language;
	const char *p_language;
} languages[] = {
	{"c",                    "cpp-output"},
	{"cpp-output",           "cpp-output"},
	{"c-header",             "cpp-output"},
	{"c++",                  "c++-cpp-output"},
	{"c++-cpp-output",       "c++-cpp-output"},
	{"c++-header",           "c++-cpp-output"},
	{"objective-c",          "objc-cpp-output"},
	{"objective-c-header",   "objc-cpp-output"},
	{"objc-cpp-output",      "objc-cpp-output"},
	{"objective-c++",        "objc++-cpp-output"},
	{"objc++-cpp-output",    "objc++-cpp-output"},
	{"objective-c++-header", "objc++-cpp-output"},
	{NULL,  NULL}};

/*
 * Guess the language of a file based on its extension. Returns NULL if the
 * extension is unknown.
 */
const char *
language_for_file(const char *fname)
{
	int i;
	const char *p;

	p = get_extension(fname);
	for (i = 0; extensions[i].extension; i++) {
		if (str_eq(p, extensions[i].extension)) {
			return extensions[i].language;
		}
	}
	return NULL;
}

/*
 * Return the preprocessed language for a given language, or NULL if unknown.
 */
const char *
p_language_for_language(const char *language)
{
	int i;

	if (!language) {
		return NULL;
	}
	for (i = 0; languages[i].language; ++i) {
		if (str_eq(language, languages[i].language)) {
			return languages[i].p_language;
		}
	}
	return NULL;
}

/*
 * Return the default file extension (including dot) for a language, or NULL if
 * unknown.
 */
const char *
extension_for_language(const char *language)
{
	int i;

	if (!language) {
		return NULL;
	}
	for (i = 0; extensions[i].extension; i++) {
		if (str_eq(language, extensions[i].language)) {
			return extensions[i].extension;
		}
	}
	return NULL;
}

bool
language_is_supported(const char *language)
{
	return p_language_for_language(language) != NULL;
}

bool
language_is_preprocessed(const char *language)
{
	return str_eq(language, p_language_for_language(language));
}