summaryrefslogtreecommitdiff
path: root/clang-plugin/gvariant-checker.cpp
blob: 41bafa877018d91dddb64adde017cc8bd4979f5e (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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
/* -*- Mode: C++; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/*
 * Tartan
 * Copyright © 2014 Philip Withnall
 *
 * 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@tecnocode.co.uk>
 */

/**
 * GVariantVisitor:
 *
 * This is a checker for #GVariant format strings and varargs. For #GVariant
 * methods which accept varargs, it validates the type and nullability of each
 * vararg against the corresponding element in the #GVariant format string (if
 * a constant format string is provided — non-constant format strings cannot be
 * validated, but the user should probably be using #GVariantBuilder directly if
 * they’re dynamically generating a format string).
 *
 * For #GVariant methods with format strings but no varargs, the format string
 * is validated.
 *
 * The format string is parsed and varargs are consumed in parallel. The static
 * type of the varargs is used, so if a weird cast is used (e.g. casting a
 * string literal to an integer and passing it to a ‘u’ format string), no error
 * will be raised. One limitation on the current checker is that the types of
 * #GVariants passed in are not checked. e.g. No error is emitted for the
 * following invalid code:
 *     g_variant_new ('@s', g_variant_new_boolean (FALSE));
 *
 * The checker is quite flexible, and a lot of its behaviour is controlled by
 * the set of #VariantCheckFlags in use for the current part of the parse tree.
 *
 * The error messages produced by this checker should give as much context and
 * guidance towards fixing the problem as possible. Empirically, it seems that
 * the GVariant Format String documentation in GLib’s manual is used quite a
 * lot, since people can’t memorise the format strings. Contextual help in the
 * error messages should try to avoid this.
 *
 * FIXME: Future work could be to implement:
 *  • Reference counting validation of #GVariants (might be better placed in a
 *    general reference counting checker).
 *  • #GVariant print format parsing (for g_variant_new_parsed() and
 *    g_variant_builder_add_parsed()).
 *  • Character-granularity error diagnostic locations, e.g. pointing to the
 *    erroneous character in a format string, not just to the start of the
 *    format string argument itself.
 *
 * FIXME: If Clang’s DiagnosticsEngine gains support for multiple
 * SourceLocations, it would be great to highlight both the relevant character
 * of the #GVariant format string, and the erroneous variadic arguments in the
 * function call, when an error is printed. At the moment we have to just pick
 * the most important of the two and highlight that.
 */

#include <cstring>

#include <clang/AST/Attr.h>

#include <glib.h>

#include "debug.h"
#include "gvariant-checker.h"

namespace tartan {

/* Information about the GVariant functions we’re interested in. If you want to
 * add support for a new GVariant function, it may be enough to add a new
 * element here. */
typedef struct {
	/* C name of the function */
	const char *func_name;
	/* Zero-based index of the GVariant format string parameter to the
	 * function; the validity of this string will be checked */
	unsigned int format_param_index;
	/* Zero-based index of the first varargs parameter or va_list. */
	unsigned int first_vararg_param_index;
	/* Whether the function takes a va_list instead of varargs. */
	bool uses_va_list;
	/* True if the argument direction is in; false if it’s out. */
	bool args_in;
} VariantFuncInfo;

static const VariantFuncInfo gvariant_format_funcs[] = {
	{ "g_variant_new", 0, 1, false, true },
	{ "g_variant_new_va", 0, 2, true, true },
	{ "g_variant_get", 1, 2, false, false },
	{ "g_variant_get_va", 1, 3, true, false },
	{ "g_variant_get_child", 2, 3, false, false },
	{ "g_variant_lookup", 2, 3, false, false },
	{ "g_variant_iter_next", 1, 2, false, false },
	{ "g_variant_iter_loop", 1, 2, false, false },
	{ "g_variant_builder_add", 1, 2, false, true },
};

/**
 * VariantCheckFlags:
 * @CHECK_FLAG_NONE: No flags.
 * @CHECK_FLAG_FORCE_GVARIANT: Force the expected type of the next variadic
 * argument to be consumed to be GVariant*.
 * @CHECK_FLAG_FORCE_VALIST: Force the expected type of the next variadic
 * argument to be consumed to be va_list*.
 * @CHECK_FLAG_REQUIRE_CONST: Require that the pointee of the expected type (if
 * it is a pointer type) must be constant. This is ignored it
 * %CHECK_FLAG_DIRECTION_OUT is not set.
 * @CHECK_FLAG_DIRECTION_OUT: Expect the argument to be out-bound, so an extra
 * level of pointer indirection will be expected on the expected type. If
 * %CHECK_FLAG_ALLOW_MAYBE is also set, the top-most pointer can be %NULL.
 * @CHECK_FLAG_ALLOW_MAYBE: Allow the next variadic argument to be consumed to
 * be potentially %NULL. This always examines the top-most argument, not the
 * value it points to if it’s a pointer.
 * @CHECK_FLAG_CONSUME_ARGS: Consume variadic arguments when parsing. If this
 * is not specified, the argument pointer will never be advanced, and all
 * GVariant format strings for a given call will be checked against the same
 * function argument.
 *
 * Flags affecting the parser and checker’s behaviour.
 */
typedef enum {
	CHECK_FLAG_NONE = 0,
	CHECK_FLAG_FORCE_GVARIANT = 1 << 0,
	CHECK_FLAG_FORCE_VALIST = 1 << 2,
	CHECK_FLAG_REQUIRE_CONST = 1 << 4,
	CHECK_FLAG_DIRECTION_OUT = 1 << 5,
	CHECK_FLAG_ALLOW_MAYBE = 1 << 6,
	CHECK_FLAG_CONSUME_ARGS = 1 << 7,
} VariantCheckFlags;


static const VariantFuncInfo *
_func_uses_gvariant_format (const FunctionDecl& func)
{
	const std::string func_name = func.getNameAsString ();
	guint i;

	/* Fast path elimination of irrelevant functions. */
	if (func_name[0] != 'g')
		return NULL;

	for (i = 0; i < G_N_ELEMENTS (gvariant_format_funcs); i++) {
		if (func_name == gvariant_format_funcs[i].func_name)
			return &gvariant_format_funcs[i];
	}

	return NULL;
}

/*
 * Return true if @actual_type and @expected_type compare equal, taking
 * qualifications into account as specified by @flags.
 *
 * Check that @actual_type and @expected_type are equal. For inbound arguments,
 * we need to compare the unqualified (with const, volatile, restrict removed)
 * types, plus the unqualified pointee types if the normal types are pointers,
 * plus the unqualified pointee pointee types, etc.
 *
 * .e.g.
 *    char* ≡ const char*
 *    int ≡ int
 *    char* ≡ char*
 *    GVariant* ≡ const GVariant*
 *    char** ≡ const char * const *
 *
 * For outbound arguments, we must compare qualified types.
 */
static bool
_compare_types (const QualType actual_type, const QualType expected_type,
                unsigned int /* VariantCheckFlags */ flags, ASTContext& context)
{
	DEBUG ("Comparing type ‘" << actual_type.getAsString () << "’ with ‘" <<
	       expected_type.getAsString () << "’.");

	/* Fast path: Simple comparison. */
	if (context.hasSameType (actual_type, expected_type)) {
		return true;
	}

	/* Slow path: Strip pointers off and remove qualifiers for inbound
	 * actual types. */
	const PointerType *actual_pointer_type = dyn_cast<PointerType> (actual_type);
	const PointerType *expected_pointer_type = dyn_cast<PointerType> (expected_type);

	if (actual_pointer_type == NULL || expected_pointer_type == NULL) {
		return false;
	}

	QualType actual_pointee_type, expected_pointee_type;

	actual_pointee_type = actual_pointer_type->getPointeeType ();
	expected_pointee_type = expected_pointer_type->getPointeeType ();

	/* Inbound arguments can be const or not. It’s a bit trickier for
	 * outbound arguments. */
	if (!(flags & CHECK_FLAG_DIRECTION_OUT)) {
		actual_pointee_type = actual_pointee_type.getUnqualifiedType ();
	}

	return _compare_types (actual_pointee_type, expected_pointee_type,
	                       flags, context);
}

/* Consume a single variadic argument from the varargs array, checking that one
 * exists and has the given @expected_type.
 *
 * Iff %CHECK_FLAG_ALLOW_MAYBE is set, the variadic argument may be NULL.
 *
 * This will emit errors where found. */
static bool
_consume_variadic_argument (QualType expected_type,
                            CallExpr::const_arg_iterator *args_begin,
                            CallExpr::const_arg_iterator *args_end,
                            unsigned int /* VariantCheckFlags */ flags,
                            CompilerInstance& compiler,
                            const StringLiteral *format_arg_str,
                            ASTContext& context, TypeManager &type_manager)
{
	/* If the GVariant method doesn’t use varargs, don’t actually consume
	 * the argument. */
	if (!(flags & CHECK_FLAG_CONSUME_ARGS)) {
		return true;
	}

	/* In certain parsing states the expected types have been modified by a
	 * preceding character in the format string. Force the expected types in
	 * those cases. */
	if (flags & CHECK_FLAG_FORCE_GVARIANT) {
		expected_type = type_manager.find_pointer_type_by_name ("GVariant");
	} else if (flags & CHECK_FLAG_FORCE_VALIST) {
		expected_type = type_manager.find_pointer_type_by_name ("va_list");
	}

	/* Handle const-ness of out arguments. We have to insert the const one
	 * layer of pointer indirection down. i.e. char* becomes const char*. */
	if ((flags & CHECK_FLAG_DIRECTION_OUT) &&
	    (flags & CHECK_FLAG_REQUIRE_CONST) &&
	    expected_type->isPointerType ()) {
		const PointerType *expected2_type = dyn_cast<PointerType> (expected_type);
		QualType expected_pointee_type = expected2_type->getPointeeType ();
		expected_pointee_type = context.getConstType (expected_pointee_type);
		expected_type = context.getPointerType (expected_pointee_type);
	}

	/* Handle in/out arguments. This must be done after constness. */
	if ((flags & CHECK_FLAG_DIRECTION_OUT) &&
	    !(flags & CHECK_FLAG_FORCE_VALIST)) {
		expected_type = context.getPointerType (expected_type);
	}

	DEBUG ("Consuming variadic argument with expected type ‘" <<
	       expected_type.getAsString () << "’.");

	if (*args_begin == *args_end) {
		Debug::emit_error ("Expected a GVariant variadic argument of "
		                   "type %0 but there wasn’t one.", compiler,
		                   format_arg_str->getLocStart ())
		<< expected_type;

		return false;
	}

	const Expr *arg = **args_begin;

	/* Check its nullability. */
	QualType actual_type = arg->getType ();
	bool is_null_constant = arg->isNullPointerConstant (context,
	                                                    Expr::NPC_ValueDependentIsNull);

	/* Check for int → uint promotions. */
	llvm::APSInt int_constant_value;
	bool is_int_constant = arg->isIntegerConstantExpr (int_constant_value,
	                                                   context);

	if (is_int_constant && int_constant_value.isNonNegative () &&
	    expected_type->isUnsignedIntegerType () &&
	    actual_type->hasSignedIntegerRepresentation ()) {
		/* Magically promote the int to a uint. */
		actual_type = context.getCorrespondingUnsignedType (actual_type);
	}

	if (is_null_constant && !(flags & CHECK_FLAG_ALLOW_MAYBE) &&
	    expected_type->isPointerType ()) {
		Debug::emit_error ("Expected a GVariant variadic argument of "
		                   "type %0 but saw NULL instead.", compiler,
		                   arg->getLocStart ())
		<< expected_type;

		return false;
	} else if (!is_null_constant) {
		/* Normal case. */
		if (!_compare_types (actual_type, expected_type,
		                     flags, context)) {
			Debug::emit_error ("Expected a GVariant variadic "
			                   "argument of type %0 but saw one "
			                   "of type %1.", compiler,
			                   arg->getLocStart ())
			<< expected_type
			<< actual_type;

			return false;
		}
	}

	/* Consume the format. */
	*args_begin = *args_begin + 1;

	return true;
}

/* Parse a single basic type string from the beginning of the string pointed to
 * by @type_str (i.e. *type_str). Consume any variadic parameters from
 * @args_begin as appropriate. This will emit errors where found.
 *
 * @type_str and @args_begin are updated as the type string and arguments are
 * consumed. */
static bool
_check_basic_type_string (const gchar **type_str,
                          CallExpr::const_arg_iterator *args_begin,
                          CallExpr::const_arg_iterator *args_end,
                          unsigned int /* VariantCheckFlags */ flags,
                          CompilerInstance& compiler,
                          const StringLiteral *format_arg_str,
                          ASTContext& context, TypeManager &type_manager)
{
	DEBUG ("Checking basic type string ‘" << *type_str << "’.");

	QualType expected_type;

	/* Reference: GVariant Type Strings. */
	switch (**type_str) {
	/* Numeric Types */
	case 'b': /* gboolean ≡ gint ≡ int */
		expected_type = context.IntTy;
		break;
	case 'y': /* guchar ≡ unsigned char */
		expected_type = context.UnsignedCharTy;
		break;
	case 'n': /* gint16 */
		expected_type = context.getIntTypeForBitwidth (16, true);
		break;
	case 'q': /* guint16 */
		expected_type = context.getIntTypeForBitwidth (16, false);
		break;
	case 'i':
	case 'h': /* gint32 */
		expected_type = context.getIntTypeForBitwidth (32, true);
		break;
	case 'u': /* guint32 */
		expected_type = context.getIntTypeForBitwidth (32, false);
		break;
	case 'x': /* gint64 */
		expected_type = context.getIntTypeForBitwidth (64, true);
		break;
	case 't': /* guint64 */
		expected_type = context.getIntTypeForBitwidth (64, false);
		break;
	case 'd': /* gdouble ≡ double */
		expected_type = context.DoubleTy;
		break;
	/* Strings */
	case 's':
	case 'o':
	case 'g': /* gchar* ≡ char* */
		/* FIXME: Could also validate o and g as D-Bus object paths and
		 * type signatures. */
		expected_type = context.getPointerType (context.CharTy);
		break;
	/* Basic types */
	case '?': /* GVariant* of any type */
		expected_type = type_manager.find_pointer_type_by_name ("GVariant");
		break;
	default:
		Debug::emit_error ("Expected a GVariant basic type string but "
		                   "saw ‘%0’.", compiler,
		                   format_arg_str->getLocStart ())
		<< std::string (1, **type_str);

		return false;
	}

	/* Handle type promotion. Integer types which are smaller than 32 bits
	 * (for all architectures we care about) are automatically promoted to
	 * 32 bits when passed as varargs.
	 *
	 * A subtlety of the standard (ISO/IEC 9899, §6.3.1.1¶2) means that all
	 * types are promoted to *signed* 32-bit integers. This is because int
	 * can represent all values representable by 16-bit (and smaller)
	 * unsigned integers.
	 *
	 * References:
	 *  • GVariant Format Strings, §Numeric Types
	 *  • ISO/IEC 9899, §6.5.2.2¶6
	 */
	if (!(flags & CHECK_FLAG_DIRECTION_OUT) &&
	    (**type_str == 'y' || **type_str == 'n' || **type_str == 'q')) {
		assert (expected_type->isPromotableIntegerType ());
		expected_type = context.IntTy;
	}

	/* Consume the type string. */
	*type_str = *type_str + 1;

	return _consume_variadic_argument (expected_type,
	                                   args_begin, args_end,
	                                   flags, compiler,
	                                   format_arg_str, context,
	                                   type_manager);
}

/* Parse a single type string from the beginning of the string pointed to
 * by @type_str (i.e. *type_str). Consume any variadic parameters from
 * @args_begin as appropriate. This will emit errors where found.
 *
 * @type_str and @args_begin are updated as the type string and arguments are
 * consumed. */
static bool
_check_type_string (const gchar **type_str,
                    CallExpr::const_arg_iterator *args_begin,
                    CallExpr::const_arg_iterator *args_end,
                    unsigned int /* VariantCheckFlags */ flags,
                    CompilerInstance& compiler,
                    const StringLiteral *format_arg_str,
                    ASTContext& context, TypeManager &type_manager)
{
	DEBUG ("Checking type string ‘" << *type_str << "’.");

	QualType expected_type;

	/* Reference: GVariant Type Strings. */
	switch (**type_str) {
	/* Variants */
	case 'v': /* GVariant* */
		expected_type = type_manager.find_pointer_type_by_name ("GVariant");
		break;
	/* Arrays */
	case 'a':
		/* Consume the ‘a’. */
		*type_str = *type_str + 1;

		/* Update flags for the array element type.
		 *
		 * FIXME: ALLOW_MAYBE only for definite types */
		flags |= CHECK_FLAG_ALLOW_MAYBE;

		if (flags & CHECK_FLAG_DIRECTION_OUT) {
			expected_type = type_manager.find_pointer_type_by_name ("GVariantIter");
		} else {
			expected_type = type_manager.find_pointer_type_by_name ("GVariantBuilder");
		}

		/* Check and consume the type string for the array element
		 * type. */
		if (!_check_type_string (type_str, args_begin, args_end,
		                         flags & ~CHECK_FLAG_CONSUME_ARGS,
		                         compiler, format_arg_str, context,
		                         type_manager)) {
			return false;
		}

		/* Consume the single GVariantBuilder for the array. */
		return _consume_variadic_argument (expected_type,
		                                   args_begin, args_end,
		                                   flags, compiler,
		                                   format_arg_str, context,
		                                   type_manager);
	/* Maybe Types */
	case 'm':
		*type_str = *type_str + 1;  /* consume the ‘m’ */
		return _check_type_string (type_str, args_begin, args_end,
		                           flags | CHECK_FLAG_ALLOW_MAYBE,
		                           compiler, format_arg_str, context,
		                           type_manager);
	/* Tuples */
	case '(':
		*type_str = *type_str + 1;  /* consume the opening bracket */

		while (**type_str != ')' && **type_str != '\0') {
			if (!_check_type_string (type_str, args_begin,
			                         args_end,
			                         flags, compiler,
			                         format_arg_str, context,
			                         type_manager)) {
				return false;
			}
		}

		if (**type_str != ')') {
			Debug::emit_error ("Invalid GVariant type string: "
			                   "tuple did not end with ‘)’.",
			                   compiler,
			                   format_arg_str->getLocStart ());
			return false;
		}

		*type_str = *type_str + 1;  /* consume the closing bracket */

		return true;
	case 'r': /* GVariant* of tuple type */
		/* FIXME: Validate that the GVariant* has a tuple type. */
		expected_type = type_manager.find_pointer_type_by_name ("GVariant");
		break;
	/* Dictionaries */
	case '{':
		*type_str = *type_str + 1;  /* consume the opening brace */

		if (**type_str == '}') {
			Debug::emit_error (
				"Invalid GVariant type string: dict did not "
				"contain exactly two elements.",
				compiler,
				format_arg_str->getLocStart ());
			return false;
		} else if (!_check_basic_type_string (type_str, args_begin,
		                                      args_end,
		                                      flags, compiler,
		                                      format_arg_str, context,
		                                      type_manager)) {
			return false;
		}

		if (**type_str == '}') {
			Debug::emit_error (
				"Invalid GVariant type string: dict did not "
				"contain exactly two elements.",
				compiler,
				format_arg_str->getLocStart ());
			return false;
		} else if (!_check_type_string (type_str, args_begin, args_end,
		                                flags, compiler,
		                                format_arg_str, context,
		                                type_manager)) {
			return false;
		}

		if (**type_str == '\0') {
			Debug::emit_error (
				"Invalid GVariant type string: dict "
				"did not end with ‘}’.",
				compiler,
				format_arg_str->getLocStart ());
			return false;
		} else if (**type_str != '}') {
			Debug::emit_error (
				"Invalid GVariant type string: dict "
				"contains more than two elements.",
				compiler,
				format_arg_str->getLocStart ());
			return false;
		}

		*type_str = *type_str + 1;  /* consume the closing brace */

		return true;
	/* GVariant* */
	case '*': /* GVariant* of any type */
		expected_type = type_manager.find_pointer_type_by_name ("GVariant");
		break;
	default:
		/* Fall back to checking basic types. */
		return _check_basic_type_string (type_str, args_begin, args_end,
		                                 flags, compiler,
		                                 format_arg_str, context,
		                                 type_manager);
	}

	/* Consume the type string. */
	*type_str = *type_str + 1;

	return _consume_variadic_argument (expected_type,
	                                   args_begin, args_end,
	                                   flags, compiler,
	                                   format_arg_str, context,
	                                   type_manager);
}

/* Parse a single basic format string from the beginning of the string pointed
 * to by @format_str (i.e. *format_str). Consume any variadic parameters from
 * @args_begin as appropriate. This will emit errors where found.
 *
 * @format_str and @args_begin are updated as the format string and arguments
 * are consumed. */
static bool
_check_basic_format_string (const gchar **format_str,
                            CallExpr::const_arg_iterator *args_begin,
                            CallExpr::const_arg_iterator *args_end,
                            unsigned int /* VariantCheckFlags */ flags,
                            CompilerInstance& compiler,
                            const StringLiteral *format_arg_str,
                            ASTContext& context, TypeManager &type_manager)
{
	DEBUG ("Checking format string ‘" << *format_str << "’.");

	/* Reference: GVariant Format Strings documentation, §Syntax. */
	switch (**format_str) {
	case '@':
		*format_str = *format_str + 1;  /* consume the ‘@’ */
		return _check_basic_type_string (format_str, args_begin,
		                                 args_end,
		                                 flags |
		                                 CHECK_FLAG_FORCE_GVARIANT,
		                                 compiler, format_arg_str,
		                                 context, type_manager);
	case '?':
		/* Direct GVariant. */
		*format_str = *format_str + 1;  /* consume the argument */
		return _consume_variadic_argument (type_manager.find_pointer_type_by_name ("GVariant"),
		                                   args_begin, args_end,
		                                   flags,
		                                   compiler, format_arg_str,
		                                   context, type_manager);
	case '&':
		/* Ignore it for inbound arguments; require that outbound
		 * arguments are const. */
		*format_str = *format_str + 1;
		return _check_basic_type_string (format_str, args_begin,
		                                 args_end,
		                                 flags |
		                                 CHECK_FLAG_REQUIRE_CONST,
		                                 compiler, format_arg_str,
		                                 context, type_manager);
	case '^': {
		/* Various different hard-coded types. */
		*format_str = *format_str + 1;

		QualType expected_type;
		QualType char_array = context.getPointerType (context.CharTy);
		QualType const_char_array = context.getPointerType (context.getConstType (context.CharTy));
		guint skip;

		/* Effectively hard-code the table from
		 * §Convenience Conversions. */
#define CONVENIENCE_FORMAT(STR_PTR, F) (strncmp (STR_PTR, F, strlen (F)) == 0)
		if (CONVENIENCE_FORMAT (*format_str, "as") ||
		    CONVENIENCE_FORMAT (*format_str, "ao")) {
			expected_type = context.getPointerType (char_array);
			skip = 2;
		} else if (CONVENIENCE_FORMAT (*format_str, "a&s") ||
		           CONVENIENCE_FORMAT (*format_str, "a&o")) {
			expected_type = context.getPointerType (const_char_array);
			skip = 3;
		} else if (CONVENIENCE_FORMAT (*format_str, "aay")) {
			expected_type = context.getPointerType (char_array);
			skip = 3;
		} else if (CONVENIENCE_FORMAT (*format_str, "ay")) {
			expected_type = char_array;
			skip = 2;
		} else if (CONVENIENCE_FORMAT (*format_str, "&ay")) {
			expected_type = const_char_array;
			skip = 3;
		} else if (CONVENIENCE_FORMAT (*format_str, "a&ay")) {
			expected_type = context.getPointerType (const_char_array);
			skip = 4;
		} else {
			Debug::emit_error (
				"Invalid GVariant basic format string: "
				"convenience operator ‘^’ was not followed by "
				"a recognized convenience conversion.",
				compiler,
				format_arg_str->getLocStart ());
			return false;
		}
#undef CONVENIENCE_FORMAT

		*format_str = *format_str + skip;

		return _consume_variadic_argument (expected_type, args_begin,
		                                   args_end,
		                                   flags, compiler,
		                                   format_arg_str, context,
		                                   type_manager);
	}
	default:
		/* Assume it’s a type string. */
		return _check_basic_type_string (format_str, args_begin,
		                                 args_end,
		                                 flags, compiler,
		                                 format_arg_str, context,
		                                 type_manager);
	}
}

/* Parse a single format string from the beginning of the string pointed
 * to by @format_str (i.e. *format_str). Consume any variadic parameters from
 * @args_begin as appropriate. This will emit errors where found.
 *
 * @format_str and @args_begin are updated as the format string and arguments
 * are consumed. */
static bool
_check_format_string (const gchar **format_str,
                      CallExpr::const_arg_iterator *args_begin,
                      CallExpr::const_arg_iterator *args_end,
                      unsigned int /* VariantCheckFlags */ flags,
                      CompilerInstance& compiler,
                      const StringLiteral *format_arg_str,
                      ASTContext& context, TypeManager &type_manager)
{
	DEBUG ("Checking format string ‘" << *format_str << "’.");

	/* Reference: GVariant Format Strings documentation, §Syntax. */
	switch (**format_str) {
	case '@':
		*format_str = *format_str + 1;  /* consume the ‘@’ */
		return _check_type_string (format_str, args_begin, args_end,
		                           flags | CHECK_FLAG_FORCE_GVARIANT,
		                           compiler, format_arg_str, context,
		                           type_manager);
	case 'm':
		*format_str = *format_str + 1;  /* consume the ‘m’ */
		return _check_format_string (format_str, args_begin, args_end,
		                             flags | CHECK_FLAG_ALLOW_MAYBE,
		                             compiler, format_arg_str, context,
		                             type_manager);
	case '*':
	case '?':
	case 'r':
		/* Direct GVariants. */
		*format_str = *format_str + 1;  /* consume the argument */
		return _consume_variadic_argument (type_manager.find_pointer_type_by_name ("GVariant"),
		                                   args_begin, args_end,
		                                   flags,
		                                   compiler, format_arg_str,
		                                   context, type_manager);
	case '(':
		*format_str = *format_str + 1;  /* consume the opening bracket */

		while (**format_str != ')' && **format_str != '\0') {
			if (!_check_format_string (format_str, args_begin,
			                           args_end,
			                           flags, compiler,
			                           format_arg_str, context,
			                           type_manager)) {
				return false;
			}
		}

		if (**format_str != ')') {
			Debug::emit_error (
				"Invalid GVariant format string: tuple "
				"did not end with ‘)’.",
				compiler,
				format_arg_str->getLocStart ());
			return false;
		}

		*format_str = *format_str + 1;  /* consume the closing bracket */

		return true;
	case '{':
		*format_str = *format_str + 1;  /* consume the opening brace */

		if (**format_str == '}') {
			Debug::emit_error (
				"Invalid GVariant format string: dict did not "
				"contain exactly two elements.",
				compiler,
				format_arg_str->getLocStart ());
			return false;
		} else if (!_check_basic_format_string (format_str, args_begin,
		                                        args_end,
		                                        flags, compiler,
		                                        format_arg_str,
		                                        context,
		                                        type_manager)) {
			return false;
		}

		if (**format_str == '}') {
			Debug::emit_error (
				"Invalid GVariant format string: dict did not "
				"contain exactly two elements.",
				compiler,
				format_arg_str->getLocStart ());
			return false;
		} else if (!_check_format_string (format_str, args_begin,
		                                  args_end,
		                                  flags, compiler,
		                                  format_arg_str, context,
		                                  type_manager)) {
			return false;
		}

		if (**format_str == '\0') {
			Debug::emit_error (
				"Invalid GVariant format string: dict "
				"did not end with ‘}’.",
				compiler,
				format_arg_str->getLocStart ());
			return false;
		} else if (**format_str != '}') {
			Debug::emit_error (
				"Invalid GVariant format string: dict "
				"contains more than two elements.",
				compiler,
				format_arg_str->getLocStart ());
			return false;
		}

		*format_str = *format_str + 1;  /* consume the closing brace */

		return true;
	case '&':
		/* Ignore it for inbound arguments; require that outbound
		 * arguments are const. */
		*format_str = *format_str + 1;
		return _check_type_string (format_str, args_begin, args_end,
		                           flags | CHECK_FLAG_REQUIRE_CONST,
		                           compiler, format_arg_str, context,
		                           type_manager);
	case '^':
		/* Handled by the basic format string parser. */
		return _check_basic_format_string (format_str, args_begin,
		                                   args_end,
		                                   flags, compiler,
		                                   format_arg_str, context,
		                                   type_manager);
	default:
		/* Assume it’s a type string. */
		return _check_type_string (format_str, args_begin, args_end,
		                           flags, compiler,
		                           format_arg_str, context,
		                           type_manager);
	}
}

/* Build a GVariant format string to represent the given type, or return NULL if
 * no representation is known. The returned value must be freed using
 * g_free(). */
static gchar *
_gvariant_format_string_for_type (QualType type,
                                  ASTContext &context,
                                  TypeManager &type_manager)
{
	const BuiltinType *bt = dyn_cast<BuiltinType> (type);

	/* Boolean. */
	if ((bt != NULL && bt->getKind () == BuiltinType::Bool) ||
	    type == type_manager.find_pointer_type_by_name ("gboolean")) {
		return g_strdup ("b");
	    } else if (bt != NULL && bt->getKind () == BuiltinType::UChar) {
		return g_strdup ("y");
	} else if (bt != NULL && bt->getKind () == BuiltinType::LongDouble) {
		return g_strdup ("d");
	} else if (type->isSignedIntegerType ()) {
		uint64_t size = context.getTypeSize (type);

		if (size == 16) {
			return g_strdup ("n");
		} else if (size == 32) {
			return g_strdup ("i");
		} else if (size == 64) {
			return g_strdup ("x");
		}
	} else if (type->isUnsignedIntegerType ()) {
		uint64_t size = context.getTypeSize (type);

		if (size == 16) {
			return g_strdup ("q");
		} else if (size == 32) {
			return g_strdup ("u");
		} else if (size == 64) {
			return g_strdup ("t");
		}
	} else if (type->isPointerType ()) {
		const QualType pointee_type = type->getPointeeType ();

		if (pointee_type->isCharType () && pointee_type.isConstQualified ()) {
			return g_strdup ("&s");  /* or 'o' or 'g' */
		} else if (pointee_type->isCharType ()) {
			return g_strdup ("s");
		} else if (pointee_type == type_manager.find_pointer_type_by_name ("GVariant")) {
			return g_strdup ("v");
		} else if (pointee_type->isPointerType ()) {
			const QualType pointee2_type = pointee_type->getPointeeType ();

			if (pointee2_type->isCharType ()) {
				/* const gchar * const * */
				return g_strdup ("^as");
			}
		}
	}

	/* FIXME: Add support for:
	 *  - Arrays
	 *  - Maybe types
	 *  - Tuples
	 */
	return NULL;
}

/* Check a GVariant function call which passes a format parameter. Validate the
 * format parameter string, and if the function takes varargs, validate their
 * types against that parameter.
 *
 * If the format string is not a string literal, we can’t check anything. */
static bool
_check_gvariant_format_param (const CallExpr& call,
                              const FunctionDecl &func,
                              const VariantFuncInfo *func_info,
                              CompilerInstance& compiler,
                              ASTContext& context, TypeManager &type_manager)
{
	/* Grab the format parameter string. */
	const Expr *format_arg = call.getArg (func_info->format_param_index)->IgnoreParenImpCasts ();

	DEBUG ("Checking GVariant format strings in " << func.getNameAsString () << "().");

	const StringLiteral *format_arg_str = dyn_cast<StringLiteral> (format_arg);
	if (format_arg_str == NULL) {
		Debug::emit_warning (
			"Non-literal GVariant format string in call to %0(). "
			"Cannot check format string correctness. Instead "
			"of a non-literal format string, use GVariantBuilder.",
			compiler,
			format_arg->getLocStart ())
		<< func.getNameAsString ();
		return false;
	}

	/* Check the string. Parse it hand-in-hand with iterating through the
	 * varargs list. Take a copy of the format_str because StringRef.str()
	 * only uses a temporary internal buffer. */
	DEBUG ("Checking GVariant format string ‘" <<
	       format_arg_str->getString () << "’ with " <<
	       call.getNumArgs () << " variadic arguments.");

	gchar *whole_format_str;
	whole_format_str = g_strdup (format_arg_str->getString ().str ().c_str ());
	const gchar *format_str = whole_format_str;
	CallExpr::const_arg_iterator args_begin = call.arg_begin ();
	CallExpr::const_arg_iterator args_end = call.arg_end ();

	/* Skip up to the varargs. If args_begin points to a va_list, the rest
	 * of the code will ignore it. */
	for (unsigned int i = 0; i < func_info->first_vararg_param_index; i++)
		++args_begin;

	unsigned int flags = CHECK_FLAG_NONE;
	if (!func_info->uses_va_list)
		flags |= CHECK_FLAG_CONSUME_ARGS;
	else
		flags |= CHECK_FLAG_FORCE_VALIST;

	/* Outbound arguments may always be NULL to skip that GVariant
	 * element. */
	if (!func_info->args_in)
		flags |= (CHECK_FLAG_DIRECTION_OUT | CHECK_FLAG_ALLOW_MAYBE);

	if (!_check_format_string (&format_str, &args_begin, &args_end,
	                           flags, compiler, format_arg_str, context,
	                           type_manager)) {
		g_free (whole_format_str);
		return false;
	}

	/* Sanity check that we’ve consumed all format strings. If not, the
	 * user has probably forgotten to add tuple brackets around their format
	 * string. Don’t emit any error messages about unpaired variadic
	 * arguments because that would just confuse things. */
	if (*format_str != '\0') {
		Debug::emit_error ("Unexpected GVariant format strings ‘%0’ "
		                   "with unpaired arguments. If using multiple "
		                   "format strings, they should be enclosed in "
		                   "brackets to create a tuple (e.g. ‘(%1)’).",
		                   compiler, format_arg_str->getLocStart ())
		<< format_str
		<< whole_format_str;

		g_free (whole_format_str);

		return false;
	}

	g_free (whole_format_str);

	/* Sanity check that we’ve consumed all arguments. */
	bool retval = true;

	for (; !func_info->uses_va_list && args_begin != args_end;
	     ++args_begin) {
		const Expr *arg = *args_begin;
		gchar *error_format_str;

		error_format_str = _gvariant_format_string_for_type (arg->getType (),
		                                                     context,
		                                                     type_manager);

		if (error_format_str != NULL) {
			Debug::emit_error ("Unexpected GVariant variadic "
			                   "argument of type %0. Either it "
			                   "should be removed, or a ‘%1’ "
			                   "(or other valid) GVariant format "
			                   "string should be added to the "
			                   "format argument to use it.",
			                   compiler, arg->getLocStart ())
			<< arg->getType ()
			<< error_format_str;
		} else {
			Debug::emit_error ("Unexpected GVariant variadic "
			                   "argument of type %0. Either it "
			                   "should be removed, or a GVariant "
			                   "format string should be added to "
			                   "the format argument to use it. "
			                   "There is no known GVariant "
			                   "representation of the argument’s "
			                   "type, so the argument must be "
			                   "serialized to a "
			                   "GVariant-representable type first.",
			                   compiler, arg->getLocStart ())
			<< arg->getType ();
		}

		retval = false;
	}

	return retval;
}

void
GVariantConsumer::HandleTranslationUnit (ASTContext& context)
{
	/* Run away if the plugin is disabled. */
	if (!this->is_enabled ()) {
		return;
	}

	this->_visitor.TraverseDecl (context.getTranslationUnitDecl ());
}

/* Note: Specifically overriding the Traverse* method here to re-implement
 * recursion to child nodes. */
bool
GVariantVisitor::VisitCallExpr (CallExpr* expr)
{
	const VariantFuncInfo *func_info;

	/* Can only handle direct function calls (i.e. not calling dereferenced
	 * function pointers). */
	const FunctionDecl *func = expr->getDirectCallee ();
	if (func == NULL)
		return true;

	/* We’re only interested in functions which handle GVariants. */
	func_info = _func_uses_gvariant_format (*func);
	if (func_info == NULL)
		return true;

	/* Check the format parameter. */
	_check_gvariant_format_param (*expr, *func, func_info, this->_compiler,
	                              func->getASTContext (),
	                              this->_type_manager);

	return true;
}

} /* namespace tartan */