forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc-index-test.c
4996 lines (4400 loc) · 161 KB
/
c-index-test.c
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
/* c-index-test.c */
#include "clang/Config/config.h"
#include "clang-c/Index.h"
#include "clang-c/CXCompilationDatabase.h"
#include "clang-c/BuildSystem.h"
#include "clang-c/Documentation.h"
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#ifdef CLANG_HAVE_LIBXML
#include <libxml/parser.h>
#include <libxml/relaxng.h>
#include <libxml/xmlerror.h>
#endif
#ifdef _WIN32
# include <direct.h>
#else
# include <unistd.h>
#endif
extern int indextest_core_main(int argc, const char **argv);
/******************************************************************************/
/* Utility functions. */
/******************************************************************************/
#ifdef _MSC_VER
char *basename(const char* path)
{
char* base1 = (char*)strrchr(path, '/');
char* base2 = (char*)strrchr(path, '\\');
if (base1 && base2)
return((base1 > base2) ? base1 + 1 : base2 + 1);
else if (base1)
return(base1 + 1);
else if (base2)
return(base2 + 1);
return((char*)path);
}
char *dirname(char* path)
{
char* base1 = (char*)strrchr(path, '/');
char* base2 = (char*)strrchr(path, '\\');
if (base1 && base2)
if (base1 > base2)
*base1 = 0;
else
*base2 = 0;
else if (base1)
*base1 = 0;
else if (base2)
*base2 = 0;
return path;
}
#else
extern char *basename(const char *);
extern char *dirname(char *);
#endif
/** Return the default parsing options. */
static unsigned getDefaultParsingOptions() {
unsigned options = CXTranslationUnit_DetailedPreprocessingRecord;
if (getenv("CINDEXTEST_EDITING"))
options |= clang_defaultEditingTranslationUnitOptions();
if (getenv("CINDEXTEST_COMPLETION_CACHING"))
options |= CXTranslationUnit_CacheCompletionResults;
if (getenv("CINDEXTEST_COMPLETION_NO_CACHING"))
options &= ~CXTranslationUnit_CacheCompletionResults;
if (getenv("CINDEXTEST_SKIP_FUNCTION_BODIES"))
options |= CXTranslationUnit_SkipFunctionBodies;
if (getenv("CINDEXTEST_COMPLETION_BRIEF_COMMENTS"))
options |= CXTranslationUnit_IncludeBriefCommentsInCodeCompletion;
if (getenv("CINDEXTEST_CREATE_PREAMBLE_ON_FIRST_PARSE"))
options |= CXTranslationUnit_CreatePreambleOnFirstParse;
if (getenv("CINDEXTEST_KEEP_GOING"))
options |= CXTranslationUnit_KeepGoing;
if (getenv("CINDEXTEST_LIMIT_SKIP_FUNCTION_BODIES_TO_PREAMBLE"))
options |= CXTranslationUnit_LimitSkipFunctionBodiesToPreamble;
if (getenv("CINDEXTEST_INCLUDE_ATTRIBUTED_TYPES"))
options |= CXTranslationUnit_IncludeAttributedTypes;
if (getenv("CINDEXTEST_VISIT_IMPLICIT_ATTRIBUTES"))
options |= CXTranslationUnit_VisitImplicitAttributes;
if (getenv("CINDEXTEST_IGNORE_NONERRORS_FROM_INCLUDED_FILES"))
options |= CXTranslationUnit_IgnoreNonErrorsFromIncludedFiles;
return options;
}
static void ModifyPrintingPolicyAccordingToEnv(CXPrintingPolicy Policy) {
struct Mapping {
const char *name;
enum CXPrintingPolicyProperty property;
};
struct Mapping mappings[] = {
{"CINDEXTEST_PRINTINGPOLICY_INDENTATION", CXPrintingPolicy_Indentation},
{"CINDEXTEST_PRINTINGPOLICY_SUPPRESSSPECIFIERS",
CXPrintingPolicy_SuppressSpecifiers},
{"CINDEXTEST_PRINTINGPOLICY_SUPPRESSTAGKEYWORD",
CXPrintingPolicy_SuppressTagKeyword},
{"CINDEXTEST_PRINTINGPOLICY_INCLUDETAGDEFINITION",
CXPrintingPolicy_IncludeTagDefinition},
{"CINDEXTEST_PRINTINGPOLICY_SUPPRESSSCOPE",
CXPrintingPolicy_SuppressScope},
{"CINDEXTEST_PRINTINGPOLICY_SUPPRESSUNWRITTENSCOPE",
CXPrintingPolicy_SuppressUnwrittenScope},
{"CINDEXTEST_PRINTINGPOLICY_SUPPRESSINITIALIZERS",
CXPrintingPolicy_SuppressInitializers},
{"CINDEXTEST_PRINTINGPOLICY_CONSTANTARRAYSIZEASWRITTEN",
CXPrintingPolicy_ConstantArraySizeAsWritten},
{"CINDEXTEST_PRINTINGPOLICY_ANONYMOUSTAGLOCATIONS",
CXPrintingPolicy_AnonymousTagLocations},
{"CINDEXTEST_PRINTINGPOLICY_SUPPRESSSTRONGLIFETIME",
CXPrintingPolicy_SuppressStrongLifetime},
{"CINDEXTEST_PRINTINGPOLICY_SUPPRESSLIFETIMEQUALIFIERS",
CXPrintingPolicy_SuppressLifetimeQualifiers},
{"CINDEXTEST_PRINTINGPOLICY_SUPPRESSTEMPLATEARGSINCXXCONSTRUCTORS",
CXPrintingPolicy_SuppressTemplateArgsInCXXConstructors},
{"CINDEXTEST_PRINTINGPOLICY_BOOL", CXPrintingPolicy_Bool},
{"CINDEXTEST_PRINTINGPOLICY_RESTRICT", CXPrintingPolicy_Restrict},
{"CINDEXTEST_PRINTINGPOLICY_ALIGNOF", CXPrintingPolicy_Alignof},
{"CINDEXTEST_PRINTINGPOLICY_UNDERSCOREALIGNOF",
CXPrintingPolicy_UnderscoreAlignof},
{"CINDEXTEST_PRINTINGPOLICY_USEVOIDFORZEROPARAMS",
CXPrintingPolicy_UseVoidForZeroParams},
{"CINDEXTEST_PRINTINGPOLICY_TERSEOUTPUT", CXPrintingPolicy_TerseOutput},
{"CINDEXTEST_PRINTINGPOLICY_POLISHFORDECLARATION",
CXPrintingPolicy_PolishForDeclaration},
{"CINDEXTEST_PRINTINGPOLICY_HALF", CXPrintingPolicy_Half},
{"CINDEXTEST_PRINTINGPOLICY_MSWCHAR", CXPrintingPolicy_MSWChar},
{"CINDEXTEST_PRINTINGPOLICY_INCLUDENEWLINES",
CXPrintingPolicy_IncludeNewlines},
{"CINDEXTEST_PRINTINGPOLICY_MSVCFORMATTING",
CXPrintingPolicy_MSVCFormatting},
{"CINDEXTEST_PRINTINGPOLICY_CONSTANTSASWRITTEN",
CXPrintingPolicy_ConstantsAsWritten},
{"CINDEXTEST_PRINTINGPOLICY_SUPPRESSIMPLICITBASE",
CXPrintingPolicy_SuppressImplicitBase},
{"CINDEXTEST_PRINTINGPOLICY_FULLYQUALIFIEDNAME",
CXPrintingPolicy_FullyQualifiedName},
};
unsigned i;
for (i = 0; i < sizeof(mappings) / sizeof(struct Mapping); i++) {
char *value = getenv(mappings[i].name);
if (value) {
clang_PrintingPolicy_setProperty(Policy, mappings[i].property,
(unsigned)strtoul(value, 0L, 10));
}
}
}
/** Returns 0 in case of success, non-zero in case of a failure. */
static int checkForErrors(CXTranslationUnit TU);
static void describeLibclangFailure(enum CXErrorCode Err) {
switch (Err) {
case CXError_Success:
fprintf(stderr, "Success\n");
return;
case CXError_Failure:
fprintf(stderr, "Failure (no details available)\n");
return;
case CXError_Crashed:
fprintf(stderr, "Failure: libclang crashed\n");
return;
case CXError_InvalidArguments:
fprintf(stderr, "Failure: invalid arguments passed to a libclang routine\n");
return;
case CXError_ASTReadError:
fprintf(stderr, "Failure: AST deserialization error occurred\n");
return;
}
}
static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column,
unsigned end_line, unsigned end_column) {
fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column,
end_line, end_column);
}
static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
CXTranslationUnit *TU) {
enum CXErrorCode Err = clang_createTranslationUnit2(Idx, file, TU);
if (Err != CXError_Success) {
fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
describeLibclangFailure(Err);
*TU = 0;
return 0;
}
return 1;
}
void free_remapped_files(struct CXUnsavedFile *unsaved_files,
int num_unsaved_files) {
int i;
for (i = 0; i != num_unsaved_files; ++i) {
free((char *)unsaved_files[i].Filename);
free((char *)unsaved_files[i].Contents);
}
free(unsaved_files);
}
static int parse_remapped_files_with_opt(const char *opt_name,
int argc, const char **argv,
int start_arg,
struct CXUnsavedFile **unsaved_files,
int *num_unsaved_files) {
int i;
int arg;
int prefix_len = strlen(opt_name);
int arg_indices[20];
*unsaved_files = 0;
*num_unsaved_files = 0;
/* Count the number of remapped files. */
for (arg = start_arg; arg < argc; ++arg) {
if (strncmp(argv[arg], opt_name, prefix_len))
continue;
assert(*num_unsaved_files < (int)(sizeof(arg_indices)/sizeof(int)));
arg_indices[*num_unsaved_files] = arg;
++*num_unsaved_files;
}
if (*num_unsaved_files == 0)
return 0;
*unsaved_files
= (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
*num_unsaved_files);
assert(*unsaved_files);
for (i = 0; i != *num_unsaved_files; ++i) {
struct CXUnsavedFile *unsaved = *unsaved_files + i;
const char *arg_string = argv[arg_indices[i]] + prefix_len;
int filename_len;
char *filename;
char *contents;
FILE *to_file;
const char *sep = strchr(arg_string, ',');
if (!sep) {
fprintf(stderr,
"error: %sfrom:to argument is missing comma\n", opt_name);
free_remapped_files(*unsaved_files, i);
*unsaved_files = 0;
*num_unsaved_files = 0;
return -1;
}
/* Open the file that we're remapping to. */
to_file = fopen(sep + 1, "rb");
if (!to_file) {
fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
sep + 1);
free_remapped_files(*unsaved_files, i);
*unsaved_files = 0;
*num_unsaved_files = 0;
return -1;
}
/* Determine the length of the file we're remapping to. */
fseek(to_file, 0, SEEK_END);
unsaved->Length = ftell(to_file);
fseek(to_file, 0, SEEK_SET);
/* Read the contents of the file we're remapping to. */
contents = (char *)malloc(unsaved->Length + 1);
assert(contents);
if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
(feof(to_file) ? "EOF" : "error"), sep + 1);
fclose(to_file);
free_remapped_files(*unsaved_files, i);
free(contents);
*unsaved_files = 0;
*num_unsaved_files = 0;
return -1;
}
contents[unsaved->Length] = 0;
unsaved->Contents = contents;
/* Close the file. */
fclose(to_file);
/* Copy the file name that we're remapping from. */
filename_len = sep - arg_string;
filename = (char *)malloc(filename_len + 1);
assert(filename);
memcpy(filename, arg_string, filename_len);
filename[filename_len] = 0;
unsaved->Filename = filename;
}
return 0;
}
static int parse_remapped_files(int argc, const char **argv, int start_arg,
struct CXUnsavedFile **unsaved_files,
int *num_unsaved_files) {
return parse_remapped_files_with_opt("-remap-file=", argc, argv, start_arg,
unsaved_files, num_unsaved_files);
}
static int parse_remapped_files_with_try(int try_idx,
int argc, const char **argv,
int start_arg,
struct CXUnsavedFile **unsaved_files,
int *num_unsaved_files) {
struct CXUnsavedFile *unsaved_files_no_try_idx;
int num_unsaved_files_no_try_idx;
struct CXUnsavedFile *unsaved_files_try_idx;
int num_unsaved_files_try_idx;
int ret;
char opt_name[32];
ret = parse_remapped_files(argc, argv, start_arg,
&unsaved_files_no_try_idx, &num_unsaved_files_no_try_idx);
if (ret)
return ret;
sprintf(opt_name, "-remap-file-%d=", try_idx);
ret = parse_remapped_files_with_opt(opt_name, argc, argv, start_arg,
&unsaved_files_try_idx, &num_unsaved_files_try_idx);
if (ret)
return ret;
if (num_unsaved_files_no_try_idx == 0) {
*unsaved_files = unsaved_files_try_idx;
*num_unsaved_files = num_unsaved_files_try_idx;
return 0;
}
if (num_unsaved_files_try_idx == 0) {
*unsaved_files = unsaved_files_no_try_idx;
*num_unsaved_files = num_unsaved_files_no_try_idx;
return 0;
}
*num_unsaved_files = num_unsaved_files_no_try_idx + num_unsaved_files_try_idx;
*unsaved_files
= (struct CXUnsavedFile *)realloc(unsaved_files_no_try_idx,
sizeof(struct CXUnsavedFile) *
*num_unsaved_files);
assert(*unsaved_files);
memcpy(*unsaved_files + num_unsaved_files_no_try_idx,
unsaved_files_try_idx, sizeof(struct CXUnsavedFile) *
num_unsaved_files_try_idx);
free(unsaved_files_try_idx);
return 0;
}
static const char *parse_comments_schema(int argc, const char **argv) {
const char *CommentsSchemaArg = "-comments-xml-schema=";
const char *CommentSchemaFile = NULL;
if (argc == 0)
return CommentSchemaFile;
if (!strncmp(argv[0], CommentsSchemaArg, strlen(CommentsSchemaArg)))
CommentSchemaFile = argv[0] + strlen(CommentsSchemaArg);
return CommentSchemaFile;
}
/******************************************************************************/
/* Pretty-printing. */
/******************************************************************************/
static const char *FileCheckPrefix = "CHECK";
static void PrintCString(const char *CStr) {
if (CStr != NULL && CStr[0] != '\0') {
for ( ; *CStr; ++CStr) {
const char C = *CStr;
switch (C) {
case '\n': printf("\\n"); break;
case '\r': printf("\\r"); break;
case '\t': printf("\\t"); break;
case '\v': printf("\\v"); break;
case '\f': printf("\\f"); break;
default: putchar(C); break;
}
}
}
}
static void PrintCStringWithPrefix(const char *Prefix, const char *CStr) {
printf(" %s=[", Prefix);
PrintCString(CStr);
printf("]");
}
static void PrintCXStringAndDispose(CXString Str) {
PrintCString(clang_getCString(Str));
clang_disposeString(Str);
}
static void PrintCXStringWithPrefix(const char *Prefix, CXString Str) {
PrintCStringWithPrefix(Prefix, clang_getCString(Str));
}
static void PrintCXStringWithPrefixAndDispose(const char *Prefix,
CXString Str) {
PrintCStringWithPrefix(Prefix, clang_getCString(Str));
clang_disposeString(Str);
}
static void PrintRange(CXSourceRange R, const char *str) {
CXFile begin_file, end_file;
unsigned begin_line, begin_column, end_line, end_column;
clang_getSpellingLocation(clang_getRangeStart(R),
&begin_file, &begin_line, &begin_column, 0);
clang_getSpellingLocation(clang_getRangeEnd(R),
&end_file, &end_line, &end_column, 0);
if (!begin_file || !end_file)
return;
if (str)
printf(" %s=", str);
PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
}
static enum DisplayType {
DisplayType_Spelling,
DisplayType_DisplayName,
DisplayType_Pretty
} wanted_display_type = DisplayType_Spelling;
static void printVersion(const char *Prefix, CXVersion Version) {
if (Version.Major < 0)
return;
printf("%s%d", Prefix, Version.Major);
if (Version.Minor < 0)
return;
printf(".%d", Version.Minor);
if (Version.Subminor < 0)
return;
printf(".%d", Version.Subminor);
}
struct CommentASTDumpingContext {
int IndentLevel;
};
static void DumpCXCommentInternal(struct CommentASTDumpingContext *Ctx,
CXComment Comment) {
unsigned i;
unsigned e;
enum CXCommentKind Kind = clang_Comment_getKind(Comment);
Ctx->IndentLevel++;
for (i = 0, e = Ctx->IndentLevel; i != e; ++i)
printf(" ");
printf("(");
switch (Kind) {
case CXComment_Null:
printf("CXComment_Null");
break;
case CXComment_Text:
printf("CXComment_Text");
PrintCXStringWithPrefixAndDispose("Text",
clang_TextComment_getText(Comment));
if (clang_Comment_isWhitespace(Comment))
printf(" IsWhitespace");
if (clang_InlineContentComment_hasTrailingNewline(Comment))
printf(" HasTrailingNewline");
break;
case CXComment_InlineCommand:
printf("CXComment_InlineCommand");
PrintCXStringWithPrefixAndDispose(
"CommandName",
clang_InlineCommandComment_getCommandName(Comment));
switch (clang_InlineCommandComment_getRenderKind(Comment)) {
case CXCommentInlineCommandRenderKind_Normal:
printf(" RenderNormal");
break;
case CXCommentInlineCommandRenderKind_Bold:
printf(" RenderBold");
break;
case CXCommentInlineCommandRenderKind_Monospaced:
printf(" RenderMonospaced");
break;
case CXCommentInlineCommandRenderKind_Emphasized:
printf(" RenderEmphasized");
break;
case CXCommentInlineCommandRenderKind_Anchor:
printf(" RenderAnchor");
break;
}
for (i = 0, e = clang_InlineCommandComment_getNumArgs(Comment);
i != e; ++i) {
printf(" Arg[%u]=", i);
PrintCXStringAndDispose(
clang_InlineCommandComment_getArgText(Comment, i));
}
if (clang_InlineContentComment_hasTrailingNewline(Comment))
printf(" HasTrailingNewline");
break;
case CXComment_HTMLStartTag: {
unsigned NumAttrs;
printf("CXComment_HTMLStartTag");
PrintCXStringWithPrefixAndDispose(
"Name",
clang_HTMLTagComment_getTagName(Comment));
NumAttrs = clang_HTMLStartTag_getNumAttrs(Comment);
if (NumAttrs != 0) {
printf(" Attrs:");
for (i = 0; i != NumAttrs; ++i) {
printf(" ");
PrintCXStringAndDispose(clang_HTMLStartTag_getAttrName(Comment, i));
printf("=");
PrintCXStringAndDispose(clang_HTMLStartTag_getAttrValue(Comment, i));
}
}
if (clang_HTMLStartTagComment_isSelfClosing(Comment))
printf(" SelfClosing");
if (clang_InlineContentComment_hasTrailingNewline(Comment))
printf(" HasTrailingNewline");
break;
}
case CXComment_HTMLEndTag:
printf("CXComment_HTMLEndTag");
PrintCXStringWithPrefixAndDispose(
"Name",
clang_HTMLTagComment_getTagName(Comment));
if (clang_InlineContentComment_hasTrailingNewline(Comment))
printf(" HasTrailingNewline");
break;
case CXComment_Paragraph:
printf("CXComment_Paragraph");
if (clang_Comment_isWhitespace(Comment))
printf(" IsWhitespace");
break;
case CXComment_BlockCommand:
printf("CXComment_BlockCommand");
PrintCXStringWithPrefixAndDispose(
"CommandName",
clang_BlockCommandComment_getCommandName(Comment));
for (i = 0, e = clang_BlockCommandComment_getNumArgs(Comment);
i != e; ++i) {
printf(" Arg[%u]=", i);
PrintCXStringAndDispose(
clang_BlockCommandComment_getArgText(Comment, i));
}
break;
case CXComment_ParamCommand:
printf("CXComment_ParamCommand");
switch (clang_ParamCommandComment_getDirection(Comment)) {
case CXCommentParamPassDirection_In:
printf(" in");
break;
case CXCommentParamPassDirection_Out:
printf(" out");
break;
case CXCommentParamPassDirection_InOut:
printf(" in,out");
break;
}
if (clang_ParamCommandComment_isDirectionExplicit(Comment))
printf(" explicitly");
else
printf(" implicitly");
PrintCXStringWithPrefixAndDispose(
"ParamName",
clang_ParamCommandComment_getParamName(Comment));
if (clang_ParamCommandComment_isParamIndexValid(Comment))
printf(" ParamIndex=%u", clang_ParamCommandComment_getParamIndex(Comment));
else
printf(" ParamIndex=Invalid");
break;
case CXComment_TParamCommand:
printf("CXComment_TParamCommand");
PrintCXStringWithPrefixAndDispose(
"ParamName",
clang_TParamCommandComment_getParamName(Comment));
if (clang_TParamCommandComment_isParamPositionValid(Comment)) {
printf(" ParamPosition={");
for (i = 0, e = clang_TParamCommandComment_getDepth(Comment);
i != e; ++i) {
printf("%u", clang_TParamCommandComment_getIndex(Comment, i));
if (i != e - 1)
printf(", ");
}
printf("}");
} else
printf(" ParamPosition=Invalid");
break;
case CXComment_VerbatimBlockCommand:
printf("CXComment_VerbatimBlockCommand");
PrintCXStringWithPrefixAndDispose(
"CommandName",
clang_BlockCommandComment_getCommandName(Comment));
break;
case CXComment_VerbatimBlockLine:
printf("CXComment_VerbatimBlockLine");
PrintCXStringWithPrefixAndDispose(
"Text",
clang_VerbatimBlockLineComment_getText(Comment));
break;
case CXComment_VerbatimLine:
printf("CXComment_VerbatimLine");
PrintCXStringWithPrefixAndDispose(
"Text",
clang_VerbatimLineComment_getText(Comment));
break;
case CXComment_FullComment:
printf("CXComment_FullComment");
break;
}
if (Kind != CXComment_Null) {
const unsigned NumChildren = clang_Comment_getNumChildren(Comment);
unsigned i;
for (i = 0; i != NumChildren; ++i) {
printf("\n// %s: ", FileCheckPrefix);
DumpCXCommentInternal(Ctx, clang_Comment_getChild(Comment, i));
}
}
printf(")");
Ctx->IndentLevel--;
}
static void DumpCXComment(CXComment Comment) {
struct CommentASTDumpingContext Ctx;
Ctx.IndentLevel = 1;
printf("\n// %s: CommentAST=[\n// %s:", FileCheckPrefix, FileCheckPrefix);
DumpCXCommentInternal(&Ctx, Comment);
printf("]");
}
static void ValidateCommentXML(const char *Str, const char *CommentSchemaFile) {
#ifdef CLANG_HAVE_LIBXML
xmlRelaxNGParserCtxtPtr RNGParser;
xmlRelaxNGPtr Schema;
xmlDocPtr Doc;
xmlRelaxNGValidCtxtPtr ValidationCtxt;
int status;
if (!CommentSchemaFile)
return;
RNGParser = xmlRelaxNGNewParserCtxt(CommentSchemaFile);
if (!RNGParser) {
printf(" libXMLError");
return;
}
Schema = xmlRelaxNGParse(RNGParser);
Doc = xmlParseDoc((const xmlChar *) Str);
if (!Doc) {
xmlErrorPtr Error = xmlGetLastError();
printf(" CommentXMLInvalid [not well-formed XML: %s]", Error->message);
return;
}
ValidationCtxt = xmlRelaxNGNewValidCtxt(Schema);
status = xmlRelaxNGValidateDoc(ValidationCtxt, Doc);
if (!status)
printf(" CommentXMLValid");
else if (status > 0) {
xmlErrorPtr Error = xmlGetLastError();
printf(" CommentXMLInvalid [not valid XML: %s]", Error->message);
} else
printf(" libXMLError");
xmlRelaxNGFreeValidCtxt(ValidationCtxt);
xmlFreeDoc(Doc);
xmlRelaxNGFree(Schema);
xmlRelaxNGFreeParserCtxt(RNGParser);
#endif
}
static void PrintCursorComments(CXCursor Cursor,
const char *CommentSchemaFile) {
{
CXString RawComment;
const char *RawCommentCString;
CXString BriefComment;
const char *BriefCommentCString;
RawComment = clang_Cursor_getRawCommentText(Cursor);
RawCommentCString = clang_getCString(RawComment);
if (RawCommentCString != NULL && RawCommentCString[0] != '\0') {
PrintCStringWithPrefix("RawComment", RawCommentCString);
PrintRange(clang_Cursor_getCommentRange(Cursor), "RawCommentRange");
BriefComment = clang_Cursor_getBriefCommentText(Cursor);
BriefCommentCString = clang_getCString(BriefComment);
if (BriefCommentCString != NULL && BriefCommentCString[0] != '\0')
PrintCStringWithPrefix("BriefComment", BriefCommentCString);
clang_disposeString(BriefComment);
}
clang_disposeString(RawComment);
}
{
CXComment Comment = clang_Cursor_getParsedComment(Cursor);
if (clang_Comment_getKind(Comment) != CXComment_Null) {
PrintCXStringWithPrefixAndDispose("FullCommentAsHTML",
clang_FullComment_getAsHTML(Comment));
{
CXString XML;
XML = clang_FullComment_getAsXML(Comment);
PrintCXStringWithPrefix("FullCommentAsXML", XML);
ValidateCommentXML(clang_getCString(XML), CommentSchemaFile);
clang_disposeString(XML);
}
DumpCXComment(Comment);
}
}
}
typedef struct {
unsigned line;
unsigned col;
} LineCol;
static int lineCol_cmp(const void *p1, const void *p2) {
const LineCol *lhs = p1;
const LineCol *rhs = p2;
if (lhs->line != rhs->line)
return (int)lhs->line - (int)rhs->line;
return (int)lhs->col - (int)rhs->col;
}
static CXString CursorToText(CXCursor Cursor) {
CXString text;
switch (wanted_display_type) {
case DisplayType_Spelling:
return clang_getCursorSpelling(Cursor);
case DisplayType_DisplayName:
return clang_getCursorDisplayName(Cursor);
case DisplayType_Pretty: {
CXPrintingPolicy Policy = clang_getCursorPrintingPolicy(Cursor);
ModifyPrintingPolicyAccordingToEnv(Policy);
text = clang_getCursorPrettyPrinted(Cursor, Policy);
clang_PrintingPolicy_dispose(Policy);
return text;
}
}
assert(0 && "unknown display type"); /* no llvm_unreachable in C. */
/* Set to NULL to prevent uninitialized variable warnings. */
text.data = NULL;
text.private_flags = 0;
return text;
}
static void PrintCursor(CXCursor Cursor, const char *CommentSchemaFile) {
CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor);
if (clang_isInvalid(Cursor.kind)) {
CXString ks = clang_getCursorKindSpelling(Cursor.kind);
printf("Invalid Cursor => %s", clang_getCString(ks));
clang_disposeString(ks);
}
else {
CXString string, ks;
CXCursor Referenced;
unsigned line, column;
CXCursor SpecializationOf;
CXCursor *overridden;
unsigned num_overridden;
unsigned RefNameRangeNr;
CXSourceRange CursorExtent;
CXSourceRange RefNameRange;
int AlwaysUnavailable;
int AlwaysDeprecated;
CXString UnavailableMessage;
CXString DeprecatedMessage;
CXPlatformAvailability PlatformAvailability[2];
int NumPlatformAvailability;
int I;
ks = clang_getCursorKindSpelling(Cursor.kind);
string = CursorToText(Cursor);
printf("%s=%s", clang_getCString(ks),
clang_getCString(string));
clang_disposeString(ks);
clang_disposeString(string);
Referenced = clang_getCursorReferenced(Cursor);
if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
if (clang_getCursorKind(Referenced) == CXCursor_OverloadedDeclRef) {
unsigned I, N = clang_getNumOverloadedDecls(Referenced);
printf("[");
for (I = 0; I != N; ++I) {
CXCursor Ovl = clang_getOverloadedDecl(Referenced, I);
CXSourceLocation Loc;
if (I)
printf(", ");
Loc = clang_getCursorLocation(Ovl);
clang_getSpellingLocation(Loc, 0, &line, &column, 0);
printf("%d:%d", line, column);
}
printf("]");
} else {
CXSourceLocation Loc = clang_getCursorLocation(Referenced);
clang_getSpellingLocation(Loc, 0, &line, &column, 0);
printf(":%d:%d", line, column);
}
if (clang_getCursorKind(Referenced) == CXCursor_TypedefDecl) {
CXType T = clang_getCursorType(Referenced);
if (clang_Type_isTransparentTagTypedef(T)) {
CXType Underlying = clang_getTypedefDeclUnderlyingType(Referenced);
CXString S = clang_getTypeSpelling(Underlying);
printf(" (Transparent: %s)", clang_getCString(S));
clang_disposeString(S);
}
}
}
if (clang_isCursorDefinition(Cursor))
printf(" (Definition)");
switch (clang_getCursorAvailability(Cursor)) {
case CXAvailability_Available:
break;
case CXAvailability_Deprecated:
printf(" (deprecated)");
break;
case CXAvailability_NotAvailable:
printf(" (unavailable)");
break;
case CXAvailability_NotAccessible:
printf(" (inaccessible)");
break;
}
NumPlatformAvailability
= clang_getCursorPlatformAvailability(Cursor,
&AlwaysDeprecated,
&DeprecatedMessage,
&AlwaysUnavailable,
&UnavailableMessage,
PlatformAvailability, 2);
if (AlwaysUnavailable) {
printf(" (always unavailable: \"%s\")",
clang_getCString(UnavailableMessage));
} else if (AlwaysDeprecated) {
printf(" (always deprecated: \"%s\")",
clang_getCString(DeprecatedMessage));
} else {
for (I = 0; I != NumPlatformAvailability; ++I) {
if (I >= 2)
break;
printf(" (%s", clang_getCString(PlatformAvailability[I].Platform));
if (PlatformAvailability[I].Unavailable)
printf(", unavailable");
else {
printVersion(", introduced=", PlatformAvailability[I].Introduced);
printVersion(", deprecated=", PlatformAvailability[I].Deprecated);
printVersion(", obsoleted=", PlatformAvailability[I].Obsoleted);
}
if (clang_getCString(PlatformAvailability[I].Message)[0])
printf(", message=\"%s\"",
clang_getCString(PlatformAvailability[I].Message));
printf(")");
}
}
for (I = 0; I != NumPlatformAvailability; ++I) {
if (I >= 2)
break;
clang_disposeCXPlatformAvailability(PlatformAvailability + I);
}
clang_disposeString(DeprecatedMessage);
clang_disposeString(UnavailableMessage);
if (clang_CXXConstructor_isDefaultConstructor(Cursor))
printf(" (default constructor)");
if (clang_CXXConstructor_isMoveConstructor(Cursor))
printf(" (move constructor)");
if (clang_CXXConstructor_isCopyConstructor(Cursor))
printf(" (copy constructor)");
if (clang_CXXConstructor_isConvertingConstructor(Cursor))
printf(" (converting constructor)");
if (clang_CXXField_isMutable(Cursor))
printf(" (mutable)");
if (clang_CXXMethod_isDefaulted(Cursor))
printf(" (defaulted)");
if (clang_CXXMethod_isStatic(Cursor))
printf(" (static)");
if (clang_CXXMethod_isVirtual(Cursor))
printf(" (virtual)");
if (clang_CXXMethod_isConst(Cursor))
printf(" (const)");
if (clang_CXXMethod_isPureVirtual(Cursor))
printf(" (pure)");
if (clang_CXXRecord_isAbstract(Cursor))
printf(" (abstract)");
if (clang_EnumDecl_isScoped(Cursor))
printf(" (scoped)");
if (clang_Cursor_isVariadic(Cursor))
printf(" (variadic)");
if (clang_Cursor_isObjCOptional(Cursor))
printf(" (@optional)");
if (clang_isInvalidDeclaration(Cursor))
printf(" (invalid)");
switch (clang_getCursorExceptionSpecificationType(Cursor))
{
case CXCursor_ExceptionSpecificationKind_None:
break;
case CXCursor_ExceptionSpecificationKind_DynamicNone:
printf(" (noexcept dynamic none)");
break;
case CXCursor_ExceptionSpecificationKind_Dynamic:
printf(" (noexcept dynamic)");
break;
case CXCursor_ExceptionSpecificationKind_MSAny:
printf(" (noexcept dynamic any)");
break;
case CXCursor_ExceptionSpecificationKind_BasicNoexcept:
printf(" (noexcept)");
break;
case CXCursor_ExceptionSpecificationKind_ComputedNoexcept:
printf(" (computed-noexcept)");
break;
case CXCursor_ExceptionSpecificationKind_Unevaluated:
case CXCursor_ExceptionSpecificationKind_Uninstantiated:
case CXCursor_ExceptionSpecificationKind_Unparsed:
break;
}
{
CXString language;
CXString definedIn;
unsigned generated;
if (clang_Cursor_isExternalSymbol(Cursor, &language, &definedIn,
&generated)) {
printf(" (external lang: %s, defined: %s, gen: %d)",
clang_getCString(language), clang_getCString(definedIn), generated);
clang_disposeString(language);
clang_disposeString(definedIn);
}
}
if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
CXType T =
clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor));
CXString S = clang_getTypeKindSpelling(T.kind);
printf(" [IBOutletCollection=%s]", clang_getCString(S));
clang_disposeString(S);
}
if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
unsigned isVirtual = clang_isVirtualBase(Cursor);
const char *accessStr = 0;
switch (access) {
case CX_CXXInvalidAccessSpecifier:
accessStr = "invalid"; break;
case CX_CXXPublic:
accessStr = "public"; break;
case CX_CXXProtected:
accessStr = "protected"; break;
case CX_CXXPrivate:
accessStr = "private"; break;
}
printf(" [access=%s isVirtual=%s]", accessStr,
isVirtual ? "true" : "false");
}
SpecializationOf = clang_getSpecializedCursorTemplate(Cursor);
if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
CXString Name = clang_getCursorSpelling(SpecializationOf);
clang_getSpellingLocation(Loc, 0, &line, &column, 0);
printf(" [Specialization of %s:%d:%d]",
clang_getCString(Name), line, column);
clang_disposeString(Name);