summaryrefslogtreecommitdiff
path: root/clang-plugin/plugin.cpp
blob: 22c423bf343e87b50bef826b5b08baf6b32e8b9a (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* -*- Mode: C++; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/*
 * Tartan
 * Copyright © 2013 Collabora Ltd.
 *
 * Tartan 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.
 *
 * Tartan 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 Tartan.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *     Philip Withnall <philip.withnall@collabora.co.uk>
 */

#include <clang/Frontend/FrontendPluginRegistry.h>
#include <clang/AST/AST.h>
#include <clang/AST/ASTConsumer.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Frontend/MultiplexConsumer.h>
#include <llvm/Support/raw_ostream.h>

#include "debug.h"
#include "gir-attributes.h"
#include "gassert-attributes.h"
#include "gsignal-checker.h"
#include "gvariant-checker.h"
#include "nullability-checker.h"

using namespace clang;

namespace {

/**
 * Plugin core.
 */
class TartanAction : public PluginASTAction {
private:
	std::shared_ptr<GirManager> _gir_manager =
		std::make_shared<GirManager> ();

protected:
	/* Note: This is called before ParseArgs, and must transfer ownership
	 * of the ASTConsumer. The TartanAction object is destroyed immediately
	 * after this function call returns, so must be careful not to retain
	 * state which is needed by the consumers. */
	ASTConsumer *
	CreateASTConsumer (CompilerInstance &compiler, llvm::StringRef in_file)
	{
		std::vector<ASTConsumer*> consumers;
		consumers.push_back (
			new GirAttributesConsumer (this->_gir_manager));
		consumers.push_back (new GAssertAttributesConsumer ());
		consumers.push_back (
			new NullabilityConsumer (compiler,
			                         this->_gir_manager));
		consumers.push_back (
			new GVariantConsumer (compiler));
		consumers.push_back (
			new GSignalConsumer (compiler, this->_gir_manager));
		consumers.push_back (
			new GirAttributesChecker (compiler,
			                          this->_gir_manager));

		return new MultiplexConsumer (consumers);
	}

private:
	bool
	_load_typelib (const CompilerInstance &CI,
	               const std::string& gi_namespace_and_version)
	{
		std::string::size_type p = gi_namespace_and_version.find ("-");

		if (p == std::string::npos) {
			/* Ignore it — probably a non-typelib file. */
			return false;
		}

		std::string gi_namespace =
			gi_namespace_and_version.substr (0, p);
		std::string gi_version =
			gi_namespace_and_version.substr (p + 1);

		DEBUG ("Loading typelib " + gi_namespace + " " + gi_version);

		/* Load the repository. */
		GError *error = NULL;

		this->_gir_manager.get ()->load_namespace (gi_namespace,
		                                           gi_version,
		                                           &error);
		if (error != NULL &&
		    !g_error_matches (error, G_IREPOSITORY_ERROR,
		                      G_IREPOSITORY_ERROR_NAMESPACE_VERSION_CONFLICT)) {
			DiagnosticsEngine &d = CI.getDiagnostics ();
			unsigned int id = d.getCustomDiagID (
				DiagnosticsEngine::Warning,
				"Fail to load GI repository ‘" + gi_namespace +
				"’ (version " + gi_version + "): " +
				error->message);
			d.Report (id);

			g_error_free (error);

			return false;
		}

		g_clear_error (&error);

		return true;
	}

	/* Load all the GI typelibs we can find. This shouldn’t take long, and
	 * saves the user having to specify which typelibs to use (or us having
	 * to try and work out which ones the user’s code uses by looking at
	 * #included files). */
	bool
	_load_gi_repositories (const CompilerInstance &CI)
	{
		GSList/*<unowned string>*/ *typelib_paths, *l;

		typelib_paths = g_irepository_get_search_path ();

		for (l = typelib_paths; l != NULL; l = l->next) {
			GDir *dir;
			const gchar *typelib_path, *typelib_filename;
			GError *error = NULL;

			typelib_path = (const gchar *) l->data;
			dir = g_dir_open (typelib_path, 0, &error);

			if (error != NULL) {
				/* Warn about the bogus include path and
				 * continue. */
				DiagnosticsEngine &d = CI.getDiagnostics ();

				unsigned int id = d.getCustomDiagID (
					DiagnosticsEngine::Warning,
					"Error opening typelib path ‘%0’: %1");
				d.Report (id)
					<< typelib_path
					<< error->message;

				continue;
			}

			while ((typelib_filename = g_dir_read_name (dir)) != NULL) {
				/* Load the typelib. Ignore failure. */

				std::string _typelib_filename (typelib_filename);
				std::string::size_type last_dot = _typelib_filename.find_last_of (".");
				if (last_dot == std::string::npos) {
					/* No ‘.typelib’ suffix — ignore. */
					continue;
				}

				std::string gi_namespace_and_version = _typelib_filename.substr (0, last_dot);
				this->_load_typelib (CI, gi_namespace_and_version);
			}

			g_dir_close (dir);
		}

		return true;
	}

protected:
	/* Parse command line arguments for the plugin. Note: This is called
	 * after CreateASTConsumer. */
	bool
	ParseArgs (const CompilerInstance &CI,
	           const std::vector<std::string>& args)
	{
		/* Load all typelibs. */
		this->_load_gi_repositories (CI);

		for (std::vector<std::string>::const_iterator it = args.begin();
		     it != args.end (); ++it) {
			std::string arg = *it;

			if (arg == "--help") {
				this->PrintHelp (llvm::errs ());
			}
		}

		return true;
	}

	/* Print plugin-specific help. */
	void
	PrintHelp (llvm::raw_ostream& out)
	{
		/* TODO: i18n */
		out << "A plugin to enable extra static analysis checks and "
		       "warnings for C code which uses GLib, by making use of "
		       "GIR metadata and other GLib coding conventions.\n"
		       "\n"
		       "Usage:\n"
		       "    clang -cc1 -load /path/to/libtartan.so "
		           "-add-plugin tartan\n";
	}

	bool
	shouldEraseOutputFiles ()
	{
		/* TODO: Make this conditional on an error occurring. */
		return false;
	}
};


/* Register the plugin with LLVM. */
static FrontendPluginRegistry::Add<TartanAction>
X("tartan", "add attributes and warnings using GLib-specific metadata");

} /* namespace */