xref: /minix3/external/bsd/llvm/dist/clang/tools/c-index-test/c-index-test.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc /* c-index-test.c */
2f4a2713aSLionel Sambuc 
3*0a6a1f1dSLionel Sambuc #include "clang/Config/config.h"
4f4a2713aSLionel Sambuc #include "clang-c/Index.h"
5f4a2713aSLionel Sambuc #include "clang-c/CXCompilationDatabase.h"
6*0a6a1f1dSLionel Sambuc #include "clang-c/BuildSystem.h"
7*0a6a1f1dSLionel Sambuc #include "clang-c/Documentation.h"
8f4a2713aSLionel Sambuc #include <ctype.h>
9f4a2713aSLionel Sambuc #include <stdlib.h>
10f4a2713aSLionel Sambuc #include <stdio.h>
11f4a2713aSLionel Sambuc #include <string.h>
12f4a2713aSLionel Sambuc #include <assert.h>
13f4a2713aSLionel Sambuc 
14f4a2713aSLionel Sambuc #ifdef CLANG_HAVE_LIBXML
15f4a2713aSLionel Sambuc #include <libxml/parser.h>
16f4a2713aSLionel Sambuc #include <libxml/relaxng.h>
17f4a2713aSLionel Sambuc #include <libxml/xmlerror.h>
18f4a2713aSLionel Sambuc #endif
19f4a2713aSLionel Sambuc 
20f4a2713aSLionel Sambuc #ifdef _WIN32
21f4a2713aSLionel Sambuc #  include <direct.h>
22f4a2713aSLionel Sambuc #else
23f4a2713aSLionel Sambuc #  include <unistd.h>
24f4a2713aSLionel Sambuc #endif
25f4a2713aSLionel Sambuc 
26f4a2713aSLionel Sambuc /******************************************************************************/
27f4a2713aSLionel Sambuc /* Utility functions.                                                         */
28f4a2713aSLionel Sambuc /******************************************************************************/
29f4a2713aSLionel Sambuc 
30f4a2713aSLionel Sambuc #ifdef _MSC_VER
basename(const char * path)31f4a2713aSLionel Sambuc char *basename(const char* path)
32f4a2713aSLionel Sambuc {
33f4a2713aSLionel Sambuc     char* base1 = (char*)strrchr(path, '/');
34f4a2713aSLionel Sambuc     char* base2 = (char*)strrchr(path, '\\');
35f4a2713aSLionel Sambuc     if (base1 && base2)
36f4a2713aSLionel Sambuc         return((base1 > base2) ? base1 + 1 : base2 + 1);
37f4a2713aSLionel Sambuc     else if (base1)
38f4a2713aSLionel Sambuc         return(base1 + 1);
39f4a2713aSLionel Sambuc     else if (base2)
40f4a2713aSLionel Sambuc         return(base2 + 1);
41f4a2713aSLionel Sambuc 
42f4a2713aSLionel Sambuc     return((char*)path);
43f4a2713aSLionel Sambuc }
dirname(char * path)44f4a2713aSLionel Sambuc char *dirname(char* path)
45f4a2713aSLionel Sambuc {
46f4a2713aSLionel Sambuc     char* base1 = (char*)strrchr(path, '/');
47f4a2713aSLionel Sambuc     char* base2 = (char*)strrchr(path, '\\');
48f4a2713aSLionel Sambuc     if (base1 && base2)
49f4a2713aSLionel Sambuc         if (base1 > base2)
50f4a2713aSLionel Sambuc           *base1 = 0;
51f4a2713aSLionel Sambuc         else
52f4a2713aSLionel Sambuc           *base2 = 0;
53f4a2713aSLionel Sambuc     else if (base1)
54f4a2713aSLionel Sambuc         *base1 = 0;
55f4a2713aSLionel Sambuc     else if (base2)
56f4a2713aSLionel Sambuc         *base2 = 0;
57f4a2713aSLionel Sambuc 
58f4a2713aSLionel Sambuc     return path;
59f4a2713aSLionel Sambuc }
60f4a2713aSLionel Sambuc #else
61f4a2713aSLionel Sambuc extern char *basename(const char *);
62f4a2713aSLionel Sambuc extern char *dirname(char *);
63f4a2713aSLionel Sambuc #endif
64f4a2713aSLionel Sambuc 
65f4a2713aSLionel Sambuc /** \brief Return the default parsing options. */
getDefaultParsingOptions()66f4a2713aSLionel Sambuc static unsigned getDefaultParsingOptions() {
67f4a2713aSLionel Sambuc   unsigned options = CXTranslationUnit_DetailedPreprocessingRecord;
68f4a2713aSLionel Sambuc 
69f4a2713aSLionel Sambuc   if (getenv("CINDEXTEST_EDITING"))
70f4a2713aSLionel Sambuc     options |= clang_defaultEditingTranslationUnitOptions();
71f4a2713aSLionel Sambuc   if (getenv("CINDEXTEST_COMPLETION_CACHING"))
72f4a2713aSLionel Sambuc     options |= CXTranslationUnit_CacheCompletionResults;
73f4a2713aSLionel Sambuc   if (getenv("CINDEXTEST_COMPLETION_NO_CACHING"))
74f4a2713aSLionel Sambuc     options &= ~CXTranslationUnit_CacheCompletionResults;
75f4a2713aSLionel Sambuc   if (getenv("CINDEXTEST_SKIP_FUNCTION_BODIES"))
76f4a2713aSLionel Sambuc     options |= CXTranslationUnit_SkipFunctionBodies;
77f4a2713aSLionel Sambuc   if (getenv("CINDEXTEST_COMPLETION_BRIEF_COMMENTS"))
78f4a2713aSLionel Sambuc     options |= CXTranslationUnit_IncludeBriefCommentsInCodeCompletion;
79f4a2713aSLionel Sambuc 
80f4a2713aSLionel Sambuc   return options;
81f4a2713aSLionel Sambuc }
82f4a2713aSLionel Sambuc 
83*0a6a1f1dSLionel Sambuc /** \brief Returns 0 in case of success, non-zero in case of a failure. */
84f4a2713aSLionel Sambuc static int checkForErrors(CXTranslationUnit TU);
85f4a2713aSLionel Sambuc 
describeLibclangFailure(enum CXErrorCode Err)86*0a6a1f1dSLionel Sambuc static void describeLibclangFailure(enum CXErrorCode Err) {
87*0a6a1f1dSLionel Sambuc   switch (Err) {
88*0a6a1f1dSLionel Sambuc   case CXError_Success:
89*0a6a1f1dSLionel Sambuc     fprintf(stderr, "Success\n");
90*0a6a1f1dSLionel Sambuc     return;
91*0a6a1f1dSLionel Sambuc 
92*0a6a1f1dSLionel Sambuc   case CXError_Failure:
93*0a6a1f1dSLionel Sambuc     fprintf(stderr, "Failure (no details available)\n");
94*0a6a1f1dSLionel Sambuc     return;
95*0a6a1f1dSLionel Sambuc 
96*0a6a1f1dSLionel Sambuc   case CXError_Crashed:
97*0a6a1f1dSLionel Sambuc     fprintf(stderr, "Failure: libclang crashed\n");
98*0a6a1f1dSLionel Sambuc     return;
99*0a6a1f1dSLionel Sambuc 
100*0a6a1f1dSLionel Sambuc   case CXError_InvalidArguments:
101*0a6a1f1dSLionel Sambuc     fprintf(stderr, "Failure: invalid arguments passed to a libclang routine\n");
102*0a6a1f1dSLionel Sambuc     return;
103*0a6a1f1dSLionel Sambuc 
104*0a6a1f1dSLionel Sambuc   case CXError_ASTReadError:
105*0a6a1f1dSLionel Sambuc     fprintf(stderr, "Failure: AST deserialization error occurred\n");
106*0a6a1f1dSLionel Sambuc     return;
107*0a6a1f1dSLionel Sambuc   }
108*0a6a1f1dSLionel Sambuc }
109*0a6a1f1dSLionel Sambuc 
PrintExtent(FILE * out,unsigned begin_line,unsigned begin_column,unsigned end_line,unsigned end_column)110f4a2713aSLionel Sambuc static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column,
111f4a2713aSLionel Sambuc                         unsigned end_line, unsigned end_column) {
112f4a2713aSLionel Sambuc   fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column,
113f4a2713aSLionel Sambuc           end_line, end_column);
114f4a2713aSLionel Sambuc }
115f4a2713aSLionel Sambuc 
CreateTranslationUnit(CXIndex Idx,const char * file,CXTranslationUnit * TU)116f4a2713aSLionel Sambuc static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
117f4a2713aSLionel Sambuc                                       CXTranslationUnit *TU) {
118*0a6a1f1dSLionel Sambuc   enum CXErrorCode Err = clang_createTranslationUnit2(Idx, file, TU);
119*0a6a1f1dSLionel Sambuc   if (Err != CXError_Success) {
120f4a2713aSLionel Sambuc     fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
121*0a6a1f1dSLionel Sambuc     describeLibclangFailure(Err);
122*0a6a1f1dSLionel Sambuc     *TU = 0;
123f4a2713aSLionel Sambuc     return 0;
124f4a2713aSLionel Sambuc   }
125f4a2713aSLionel Sambuc   return 1;
126f4a2713aSLionel Sambuc }
127f4a2713aSLionel Sambuc 
free_remapped_files(struct CXUnsavedFile * unsaved_files,int num_unsaved_files)128f4a2713aSLionel Sambuc void free_remapped_files(struct CXUnsavedFile *unsaved_files,
129f4a2713aSLionel Sambuc                          int num_unsaved_files) {
130f4a2713aSLionel Sambuc   int i;
131f4a2713aSLionel Sambuc   for (i = 0; i != num_unsaved_files; ++i) {
132f4a2713aSLionel Sambuc     free((char *)unsaved_files[i].Filename);
133f4a2713aSLionel Sambuc     free((char *)unsaved_files[i].Contents);
134f4a2713aSLionel Sambuc   }
135f4a2713aSLionel Sambuc   free(unsaved_files);
136f4a2713aSLionel Sambuc }
137f4a2713aSLionel Sambuc 
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)138*0a6a1f1dSLionel Sambuc static int parse_remapped_files_with_opt(const char *opt_name,
139*0a6a1f1dSLionel Sambuc                                          int argc, const char **argv,
140*0a6a1f1dSLionel Sambuc                                          int start_arg,
141f4a2713aSLionel Sambuc                                          struct CXUnsavedFile **unsaved_files,
142f4a2713aSLionel Sambuc                                          int *num_unsaved_files) {
143f4a2713aSLionel Sambuc   int i;
144f4a2713aSLionel Sambuc   int arg;
145*0a6a1f1dSLionel Sambuc   int prefix_len = strlen(opt_name);
146*0a6a1f1dSLionel Sambuc   int arg_indices[20];
147f4a2713aSLionel Sambuc   *unsaved_files = 0;
148f4a2713aSLionel Sambuc   *num_unsaved_files = 0;
149f4a2713aSLionel Sambuc 
150f4a2713aSLionel Sambuc   /* Count the number of remapped files. */
151f4a2713aSLionel Sambuc   for (arg = start_arg; arg < argc; ++arg) {
152*0a6a1f1dSLionel Sambuc     if (strncmp(argv[arg], opt_name, prefix_len))
153*0a6a1f1dSLionel Sambuc       continue;
154f4a2713aSLionel Sambuc 
155*0a6a1f1dSLionel Sambuc     assert(*num_unsaved_files < (int)(sizeof(arg_indices)/sizeof(int)));
156*0a6a1f1dSLionel Sambuc     arg_indices[*num_unsaved_files] = arg;
157f4a2713aSLionel Sambuc     ++*num_unsaved_files;
158f4a2713aSLionel Sambuc   }
159f4a2713aSLionel Sambuc 
160f4a2713aSLionel Sambuc   if (*num_unsaved_files == 0)
161f4a2713aSLionel Sambuc     return 0;
162f4a2713aSLionel Sambuc 
163f4a2713aSLionel Sambuc   *unsaved_files
164f4a2713aSLionel Sambuc     = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
165f4a2713aSLionel Sambuc                                      *num_unsaved_files);
166*0a6a1f1dSLionel Sambuc   for (i = 0; i != *num_unsaved_files; ++i) {
167f4a2713aSLionel Sambuc     struct CXUnsavedFile *unsaved = *unsaved_files + i;
168*0a6a1f1dSLionel Sambuc     const char *arg_string = argv[arg_indices[i]] + prefix_len;
169f4a2713aSLionel Sambuc     int filename_len;
170f4a2713aSLionel Sambuc     char *filename;
171f4a2713aSLionel Sambuc     char *contents;
172f4a2713aSLionel Sambuc     FILE *to_file;
173*0a6a1f1dSLionel Sambuc     const char *sep = strchr(arg_string, ',');
174*0a6a1f1dSLionel Sambuc     if (!sep) {
175f4a2713aSLionel Sambuc       fprintf(stderr,
176*0a6a1f1dSLionel Sambuc               "error: %sfrom:to argument is missing comma\n", opt_name);
177f4a2713aSLionel Sambuc       free_remapped_files(*unsaved_files, i);
178f4a2713aSLionel Sambuc       *unsaved_files = 0;
179f4a2713aSLionel Sambuc       *num_unsaved_files = 0;
180f4a2713aSLionel Sambuc       return -1;
181f4a2713aSLionel Sambuc     }
182f4a2713aSLionel Sambuc 
183f4a2713aSLionel Sambuc     /* Open the file that we're remapping to. */
184*0a6a1f1dSLionel Sambuc     to_file = fopen(sep + 1, "rb");
185f4a2713aSLionel Sambuc     if (!to_file) {
186f4a2713aSLionel Sambuc       fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
187*0a6a1f1dSLionel Sambuc               sep + 1);
188f4a2713aSLionel Sambuc       free_remapped_files(*unsaved_files, i);
189f4a2713aSLionel Sambuc       *unsaved_files = 0;
190f4a2713aSLionel Sambuc       *num_unsaved_files = 0;
191f4a2713aSLionel Sambuc       return -1;
192f4a2713aSLionel Sambuc     }
193f4a2713aSLionel Sambuc 
194f4a2713aSLionel Sambuc     /* Determine the length of the file we're remapping to. */
195f4a2713aSLionel Sambuc     fseek(to_file, 0, SEEK_END);
196f4a2713aSLionel Sambuc     unsaved->Length = ftell(to_file);
197f4a2713aSLionel Sambuc     fseek(to_file, 0, SEEK_SET);
198f4a2713aSLionel Sambuc 
199f4a2713aSLionel Sambuc     /* Read the contents of the file we're remapping to. */
200f4a2713aSLionel Sambuc     contents = (char *)malloc(unsaved->Length + 1);
201f4a2713aSLionel Sambuc     if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
202f4a2713aSLionel Sambuc       fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
203*0a6a1f1dSLionel Sambuc               (feof(to_file) ? "EOF" : "error"), sep + 1);
204f4a2713aSLionel Sambuc       fclose(to_file);
205f4a2713aSLionel Sambuc       free_remapped_files(*unsaved_files, i);
206f4a2713aSLionel Sambuc       free(contents);
207f4a2713aSLionel Sambuc       *unsaved_files = 0;
208f4a2713aSLionel Sambuc       *num_unsaved_files = 0;
209f4a2713aSLionel Sambuc       return -1;
210f4a2713aSLionel Sambuc     }
211f4a2713aSLionel Sambuc     contents[unsaved->Length] = 0;
212f4a2713aSLionel Sambuc     unsaved->Contents = contents;
213f4a2713aSLionel Sambuc 
214f4a2713aSLionel Sambuc     /* Close the file. */
215f4a2713aSLionel Sambuc     fclose(to_file);
216f4a2713aSLionel Sambuc 
217f4a2713aSLionel Sambuc     /* Copy the file name that we're remapping from. */
218*0a6a1f1dSLionel Sambuc     filename_len = sep - arg_string;
219f4a2713aSLionel Sambuc     filename = (char *)malloc(filename_len + 1);
220f4a2713aSLionel Sambuc     memcpy(filename, arg_string, filename_len);
221f4a2713aSLionel Sambuc     filename[filename_len] = 0;
222f4a2713aSLionel Sambuc     unsaved->Filename = filename;
223f4a2713aSLionel Sambuc   }
224f4a2713aSLionel Sambuc 
225f4a2713aSLionel Sambuc   return 0;
226f4a2713aSLionel Sambuc }
227f4a2713aSLionel Sambuc 
parse_remapped_files(int argc,const char ** argv,int start_arg,struct CXUnsavedFile ** unsaved_files,int * num_unsaved_files)228*0a6a1f1dSLionel Sambuc static int parse_remapped_files(int argc, const char **argv, int start_arg,
229*0a6a1f1dSLionel Sambuc                                 struct CXUnsavedFile **unsaved_files,
230*0a6a1f1dSLionel Sambuc                                 int *num_unsaved_files) {
231*0a6a1f1dSLionel Sambuc   return parse_remapped_files_with_opt("-remap-file=", argc, argv, start_arg,
232*0a6a1f1dSLionel Sambuc       unsaved_files, num_unsaved_files);
233*0a6a1f1dSLionel Sambuc }
234*0a6a1f1dSLionel Sambuc 
parse_remapped_files_with_try(int try_idx,int argc,const char ** argv,int start_arg,struct CXUnsavedFile ** unsaved_files,int * num_unsaved_files)235*0a6a1f1dSLionel Sambuc static int parse_remapped_files_with_try(int try_idx,
236*0a6a1f1dSLionel Sambuc                                          int argc, const char **argv,
237*0a6a1f1dSLionel Sambuc                                          int start_arg,
238*0a6a1f1dSLionel Sambuc                                          struct CXUnsavedFile **unsaved_files,
239*0a6a1f1dSLionel Sambuc                                          int *num_unsaved_files) {
240*0a6a1f1dSLionel Sambuc   struct CXUnsavedFile *unsaved_files_no_try_idx;
241*0a6a1f1dSLionel Sambuc   int num_unsaved_files_no_try_idx;
242*0a6a1f1dSLionel Sambuc   struct CXUnsavedFile *unsaved_files_try_idx;
243*0a6a1f1dSLionel Sambuc   int num_unsaved_files_try_idx;
244*0a6a1f1dSLionel Sambuc   int ret;
245*0a6a1f1dSLionel Sambuc   char opt_name[32];
246*0a6a1f1dSLionel Sambuc 
247*0a6a1f1dSLionel Sambuc   ret = parse_remapped_files(argc, argv, start_arg,
248*0a6a1f1dSLionel Sambuc       &unsaved_files_no_try_idx, &num_unsaved_files_no_try_idx);
249*0a6a1f1dSLionel Sambuc   if (ret)
250*0a6a1f1dSLionel Sambuc     return ret;
251*0a6a1f1dSLionel Sambuc 
252*0a6a1f1dSLionel Sambuc   sprintf(opt_name, "-remap-file-%d=", try_idx);
253*0a6a1f1dSLionel Sambuc   ret = parse_remapped_files_with_opt(opt_name, argc, argv, start_arg,
254*0a6a1f1dSLionel Sambuc       &unsaved_files_try_idx, &num_unsaved_files_try_idx);
255*0a6a1f1dSLionel Sambuc   if (ret)
256*0a6a1f1dSLionel Sambuc     return ret;
257*0a6a1f1dSLionel Sambuc 
258*0a6a1f1dSLionel Sambuc   *num_unsaved_files = num_unsaved_files_no_try_idx + num_unsaved_files_try_idx;
259*0a6a1f1dSLionel Sambuc   *unsaved_files
260*0a6a1f1dSLionel Sambuc     = (struct CXUnsavedFile *)realloc(unsaved_files_no_try_idx,
261*0a6a1f1dSLionel Sambuc                                       sizeof(struct CXUnsavedFile) *
262*0a6a1f1dSLionel Sambuc                                         *num_unsaved_files);
263*0a6a1f1dSLionel Sambuc   memcpy(*unsaved_files + num_unsaved_files_no_try_idx,
264*0a6a1f1dSLionel Sambuc          unsaved_files_try_idx, sizeof(struct CXUnsavedFile) *
265*0a6a1f1dSLionel Sambuc             num_unsaved_files_try_idx);
266*0a6a1f1dSLionel Sambuc   free(unsaved_files_try_idx);
267*0a6a1f1dSLionel Sambuc   return 0;
268*0a6a1f1dSLionel Sambuc }
269*0a6a1f1dSLionel Sambuc 
parse_comments_schema(int argc,const char ** argv)270f4a2713aSLionel Sambuc static const char *parse_comments_schema(int argc, const char **argv) {
271f4a2713aSLionel Sambuc   const char *CommentsSchemaArg = "-comments-xml-schema=";
272f4a2713aSLionel Sambuc   const char *CommentSchemaFile = NULL;
273f4a2713aSLionel Sambuc 
274f4a2713aSLionel Sambuc   if (argc == 0)
275f4a2713aSLionel Sambuc     return CommentSchemaFile;
276f4a2713aSLionel Sambuc 
277f4a2713aSLionel Sambuc   if (!strncmp(argv[0], CommentsSchemaArg, strlen(CommentsSchemaArg)))
278f4a2713aSLionel Sambuc     CommentSchemaFile = argv[0] + strlen(CommentsSchemaArg);
279f4a2713aSLionel Sambuc 
280f4a2713aSLionel Sambuc   return CommentSchemaFile;
281f4a2713aSLionel Sambuc }
282f4a2713aSLionel Sambuc 
283f4a2713aSLionel Sambuc /******************************************************************************/
284f4a2713aSLionel Sambuc /* Pretty-printing.                                                           */
285f4a2713aSLionel Sambuc /******************************************************************************/
286f4a2713aSLionel Sambuc 
287f4a2713aSLionel Sambuc static const char *FileCheckPrefix = "CHECK";
288f4a2713aSLionel Sambuc 
PrintCString(const char * CStr)289f4a2713aSLionel Sambuc static void PrintCString(const char *CStr) {
290f4a2713aSLionel Sambuc   if (CStr != NULL && CStr[0] != '\0') {
291f4a2713aSLionel Sambuc     for ( ; *CStr; ++CStr) {
292f4a2713aSLionel Sambuc       const char C = *CStr;
293f4a2713aSLionel Sambuc       switch (C) {
294f4a2713aSLionel Sambuc         case '\n': printf("\\n"); break;
295f4a2713aSLionel Sambuc         case '\r': printf("\\r"); break;
296f4a2713aSLionel Sambuc         case '\t': printf("\\t"); break;
297f4a2713aSLionel Sambuc         case '\v': printf("\\v"); break;
298f4a2713aSLionel Sambuc         case '\f': printf("\\f"); break;
299f4a2713aSLionel Sambuc         default:   putchar(C);    break;
300f4a2713aSLionel Sambuc       }
301f4a2713aSLionel Sambuc     }
302f4a2713aSLionel Sambuc   }
303f4a2713aSLionel Sambuc }
304f4a2713aSLionel Sambuc 
PrintCStringWithPrefix(const char * Prefix,const char * CStr)305f4a2713aSLionel Sambuc static void PrintCStringWithPrefix(const char *Prefix, const char *CStr) {
306f4a2713aSLionel Sambuc   printf(" %s=[", Prefix);
307f4a2713aSLionel Sambuc   PrintCString(CStr);
308f4a2713aSLionel Sambuc   printf("]");
309f4a2713aSLionel Sambuc }
310f4a2713aSLionel Sambuc 
PrintCXStringAndDispose(CXString Str)311f4a2713aSLionel Sambuc static void PrintCXStringAndDispose(CXString Str) {
312f4a2713aSLionel Sambuc   PrintCString(clang_getCString(Str));
313f4a2713aSLionel Sambuc   clang_disposeString(Str);
314f4a2713aSLionel Sambuc }
315f4a2713aSLionel Sambuc 
PrintCXStringWithPrefix(const char * Prefix,CXString Str)316f4a2713aSLionel Sambuc static void PrintCXStringWithPrefix(const char *Prefix, CXString Str) {
317f4a2713aSLionel Sambuc   PrintCStringWithPrefix(Prefix, clang_getCString(Str));
318f4a2713aSLionel Sambuc }
319f4a2713aSLionel Sambuc 
PrintCXStringWithPrefixAndDispose(const char * Prefix,CXString Str)320f4a2713aSLionel Sambuc static void PrintCXStringWithPrefixAndDispose(const char *Prefix,
321f4a2713aSLionel Sambuc                                               CXString Str) {
322f4a2713aSLionel Sambuc   PrintCStringWithPrefix(Prefix, clang_getCString(Str));
323f4a2713aSLionel Sambuc   clang_disposeString(Str);
324f4a2713aSLionel Sambuc }
325f4a2713aSLionel Sambuc 
PrintRange(CXSourceRange R,const char * str)326f4a2713aSLionel Sambuc static void PrintRange(CXSourceRange R, const char *str) {
327f4a2713aSLionel Sambuc   CXFile begin_file, end_file;
328f4a2713aSLionel Sambuc   unsigned begin_line, begin_column, end_line, end_column;
329f4a2713aSLionel Sambuc 
330f4a2713aSLionel Sambuc   clang_getSpellingLocation(clang_getRangeStart(R),
331f4a2713aSLionel Sambuc                             &begin_file, &begin_line, &begin_column, 0);
332f4a2713aSLionel Sambuc   clang_getSpellingLocation(clang_getRangeEnd(R),
333f4a2713aSLionel Sambuc                             &end_file, &end_line, &end_column, 0);
334f4a2713aSLionel Sambuc   if (!begin_file || !end_file)
335f4a2713aSLionel Sambuc     return;
336f4a2713aSLionel Sambuc 
337f4a2713aSLionel Sambuc   if (str)
338f4a2713aSLionel Sambuc     printf(" %s=", str);
339f4a2713aSLionel Sambuc   PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
340f4a2713aSLionel Sambuc }
341f4a2713aSLionel Sambuc 
342f4a2713aSLionel Sambuc int want_display_name = 0;
343f4a2713aSLionel Sambuc 
printVersion(const char * Prefix,CXVersion Version)344f4a2713aSLionel Sambuc static void printVersion(const char *Prefix, CXVersion Version) {
345f4a2713aSLionel Sambuc   if (Version.Major < 0)
346f4a2713aSLionel Sambuc     return;
347f4a2713aSLionel Sambuc   printf("%s%d", Prefix, Version.Major);
348f4a2713aSLionel Sambuc 
349f4a2713aSLionel Sambuc   if (Version.Minor < 0)
350f4a2713aSLionel Sambuc     return;
351f4a2713aSLionel Sambuc   printf(".%d", Version.Minor);
352f4a2713aSLionel Sambuc 
353f4a2713aSLionel Sambuc   if (Version.Subminor < 0)
354f4a2713aSLionel Sambuc     return;
355f4a2713aSLionel Sambuc   printf(".%d", Version.Subminor);
356f4a2713aSLionel Sambuc }
357f4a2713aSLionel Sambuc 
358f4a2713aSLionel Sambuc struct CommentASTDumpingContext {
359f4a2713aSLionel Sambuc   int IndentLevel;
360f4a2713aSLionel Sambuc };
361f4a2713aSLionel Sambuc 
DumpCXCommentInternal(struct CommentASTDumpingContext * Ctx,CXComment Comment)362f4a2713aSLionel Sambuc static void DumpCXCommentInternal(struct CommentASTDumpingContext *Ctx,
363f4a2713aSLionel Sambuc                                   CXComment Comment) {
364f4a2713aSLionel Sambuc   unsigned i;
365f4a2713aSLionel Sambuc   unsigned e;
366f4a2713aSLionel Sambuc   enum CXCommentKind Kind = clang_Comment_getKind(Comment);
367f4a2713aSLionel Sambuc 
368f4a2713aSLionel Sambuc   Ctx->IndentLevel++;
369f4a2713aSLionel Sambuc   for (i = 0, e = Ctx->IndentLevel; i != e; ++i)
370f4a2713aSLionel Sambuc     printf("  ");
371f4a2713aSLionel Sambuc 
372f4a2713aSLionel Sambuc   printf("(");
373f4a2713aSLionel Sambuc   switch (Kind) {
374f4a2713aSLionel Sambuc   case CXComment_Null:
375f4a2713aSLionel Sambuc     printf("CXComment_Null");
376f4a2713aSLionel Sambuc     break;
377f4a2713aSLionel Sambuc   case CXComment_Text:
378f4a2713aSLionel Sambuc     printf("CXComment_Text");
379f4a2713aSLionel Sambuc     PrintCXStringWithPrefixAndDispose("Text",
380f4a2713aSLionel Sambuc                                       clang_TextComment_getText(Comment));
381f4a2713aSLionel Sambuc     if (clang_Comment_isWhitespace(Comment))
382f4a2713aSLionel Sambuc       printf(" IsWhitespace");
383f4a2713aSLionel Sambuc     if (clang_InlineContentComment_hasTrailingNewline(Comment))
384f4a2713aSLionel Sambuc       printf(" HasTrailingNewline");
385f4a2713aSLionel Sambuc     break;
386f4a2713aSLionel Sambuc   case CXComment_InlineCommand:
387f4a2713aSLionel Sambuc     printf("CXComment_InlineCommand");
388f4a2713aSLionel Sambuc     PrintCXStringWithPrefixAndDispose(
389f4a2713aSLionel Sambuc         "CommandName",
390f4a2713aSLionel Sambuc         clang_InlineCommandComment_getCommandName(Comment));
391f4a2713aSLionel Sambuc     switch (clang_InlineCommandComment_getRenderKind(Comment)) {
392f4a2713aSLionel Sambuc     case CXCommentInlineCommandRenderKind_Normal:
393f4a2713aSLionel Sambuc       printf(" RenderNormal");
394f4a2713aSLionel Sambuc       break;
395f4a2713aSLionel Sambuc     case CXCommentInlineCommandRenderKind_Bold:
396f4a2713aSLionel Sambuc       printf(" RenderBold");
397f4a2713aSLionel Sambuc       break;
398f4a2713aSLionel Sambuc     case CXCommentInlineCommandRenderKind_Monospaced:
399f4a2713aSLionel Sambuc       printf(" RenderMonospaced");
400f4a2713aSLionel Sambuc       break;
401f4a2713aSLionel Sambuc     case CXCommentInlineCommandRenderKind_Emphasized:
402f4a2713aSLionel Sambuc       printf(" RenderEmphasized");
403f4a2713aSLionel Sambuc       break;
404f4a2713aSLionel Sambuc     }
405f4a2713aSLionel Sambuc     for (i = 0, e = clang_InlineCommandComment_getNumArgs(Comment);
406f4a2713aSLionel Sambuc          i != e; ++i) {
407f4a2713aSLionel Sambuc       printf(" Arg[%u]=", i);
408f4a2713aSLionel Sambuc       PrintCXStringAndDispose(
409f4a2713aSLionel Sambuc           clang_InlineCommandComment_getArgText(Comment, i));
410f4a2713aSLionel Sambuc     }
411f4a2713aSLionel Sambuc     if (clang_InlineContentComment_hasTrailingNewline(Comment))
412f4a2713aSLionel Sambuc       printf(" HasTrailingNewline");
413f4a2713aSLionel Sambuc     break;
414f4a2713aSLionel Sambuc   case CXComment_HTMLStartTag: {
415f4a2713aSLionel Sambuc     unsigned NumAttrs;
416f4a2713aSLionel Sambuc     printf("CXComment_HTMLStartTag");
417f4a2713aSLionel Sambuc     PrintCXStringWithPrefixAndDispose(
418f4a2713aSLionel Sambuc         "Name",
419f4a2713aSLionel Sambuc         clang_HTMLTagComment_getTagName(Comment));
420f4a2713aSLionel Sambuc     NumAttrs = clang_HTMLStartTag_getNumAttrs(Comment);
421f4a2713aSLionel Sambuc     if (NumAttrs != 0) {
422f4a2713aSLionel Sambuc       printf(" Attrs:");
423f4a2713aSLionel Sambuc       for (i = 0; i != NumAttrs; ++i) {
424f4a2713aSLionel Sambuc         printf(" ");
425f4a2713aSLionel Sambuc         PrintCXStringAndDispose(clang_HTMLStartTag_getAttrName(Comment, i));
426f4a2713aSLionel Sambuc         printf("=");
427f4a2713aSLionel Sambuc         PrintCXStringAndDispose(clang_HTMLStartTag_getAttrValue(Comment, i));
428f4a2713aSLionel Sambuc       }
429f4a2713aSLionel Sambuc     }
430f4a2713aSLionel Sambuc     if (clang_HTMLStartTagComment_isSelfClosing(Comment))
431f4a2713aSLionel Sambuc       printf(" SelfClosing");
432f4a2713aSLionel Sambuc     if (clang_InlineContentComment_hasTrailingNewline(Comment))
433f4a2713aSLionel Sambuc       printf(" HasTrailingNewline");
434f4a2713aSLionel Sambuc     break;
435f4a2713aSLionel Sambuc   }
436f4a2713aSLionel Sambuc   case CXComment_HTMLEndTag:
437f4a2713aSLionel Sambuc     printf("CXComment_HTMLEndTag");
438f4a2713aSLionel Sambuc     PrintCXStringWithPrefixAndDispose(
439f4a2713aSLionel Sambuc         "Name",
440f4a2713aSLionel Sambuc         clang_HTMLTagComment_getTagName(Comment));
441f4a2713aSLionel Sambuc     if (clang_InlineContentComment_hasTrailingNewline(Comment))
442f4a2713aSLionel Sambuc       printf(" HasTrailingNewline");
443f4a2713aSLionel Sambuc     break;
444f4a2713aSLionel Sambuc   case CXComment_Paragraph:
445f4a2713aSLionel Sambuc     printf("CXComment_Paragraph");
446f4a2713aSLionel Sambuc     if (clang_Comment_isWhitespace(Comment))
447f4a2713aSLionel Sambuc       printf(" IsWhitespace");
448f4a2713aSLionel Sambuc     break;
449f4a2713aSLionel Sambuc   case CXComment_BlockCommand:
450f4a2713aSLionel Sambuc     printf("CXComment_BlockCommand");
451f4a2713aSLionel Sambuc     PrintCXStringWithPrefixAndDispose(
452f4a2713aSLionel Sambuc         "CommandName",
453f4a2713aSLionel Sambuc         clang_BlockCommandComment_getCommandName(Comment));
454f4a2713aSLionel Sambuc     for (i = 0, e = clang_BlockCommandComment_getNumArgs(Comment);
455f4a2713aSLionel Sambuc          i != e; ++i) {
456f4a2713aSLionel Sambuc       printf(" Arg[%u]=", i);
457f4a2713aSLionel Sambuc       PrintCXStringAndDispose(
458f4a2713aSLionel Sambuc           clang_BlockCommandComment_getArgText(Comment, i));
459f4a2713aSLionel Sambuc     }
460f4a2713aSLionel Sambuc     break;
461f4a2713aSLionel Sambuc   case CXComment_ParamCommand:
462f4a2713aSLionel Sambuc     printf("CXComment_ParamCommand");
463f4a2713aSLionel Sambuc     switch (clang_ParamCommandComment_getDirection(Comment)) {
464f4a2713aSLionel Sambuc     case CXCommentParamPassDirection_In:
465f4a2713aSLionel Sambuc       printf(" in");
466f4a2713aSLionel Sambuc       break;
467f4a2713aSLionel Sambuc     case CXCommentParamPassDirection_Out:
468f4a2713aSLionel Sambuc       printf(" out");
469f4a2713aSLionel Sambuc       break;
470f4a2713aSLionel Sambuc     case CXCommentParamPassDirection_InOut:
471f4a2713aSLionel Sambuc       printf(" in,out");
472f4a2713aSLionel Sambuc       break;
473f4a2713aSLionel Sambuc     }
474f4a2713aSLionel Sambuc     if (clang_ParamCommandComment_isDirectionExplicit(Comment))
475f4a2713aSLionel Sambuc       printf(" explicitly");
476f4a2713aSLionel Sambuc     else
477f4a2713aSLionel Sambuc       printf(" implicitly");
478f4a2713aSLionel Sambuc     PrintCXStringWithPrefixAndDispose(
479f4a2713aSLionel Sambuc         "ParamName",
480f4a2713aSLionel Sambuc         clang_ParamCommandComment_getParamName(Comment));
481f4a2713aSLionel Sambuc     if (clang_ParamCommandComment_isParamIndexValid(Comment))
482f4a2713aSLionel Sambuc       printf(" ParamIndex=%u", clang_ParamCommandComment_getParamIndex(Comment));
483f4a2713aSLionel Sambuc     else
484f4a2713aSLionel Sambuc       printf(" ParamIndex=Invalid");
485f4a2713aSLionel Sambuc     break;
486f4a2713aSLionel Sambuc   case CXComment_TParamCommand:
487f4a2713aSLionel Sambuc     printf("CXComment_TParamCommand");
488f4a2713aSLionel Sambuc     PrintCXStringWithPrefixAndDispose(
489f4a2713aSLionel Sambuc         "ParamName",
490f4a2713aSLionel Sambuc         clang_TParamCommandComment_getParamName(Comment));
491f4a2713aSLionel Sambuc     if (clang_TParamCommandComment_isParamPositionValid(Comment)) {
492f4a2713aSLionel Sambuc       printf(" ParamPosition={");
493f4a2713aSLionel Sambuc       for (i = 0, e = clang_TParamCommandComment_getDepth(Comment);
494f4a2713aSLionel Sambuc            i != e; ++i) {
495f4a2713aSLionel Sambuc         printf("%u", clang_TParamCommandComment_getIndex(Comment, i));
496f4a2713aSLionel Sambuc         if (i != e - 1)
497f4a2713aSLionel Sambuc           printf(", ");
498f4a2713aSLionel Sambuc       }
499f4a2713aSLionel Sambuc       printf("}");
500f4a2713aSLionel Sambuc     } else
501f4a2713aSLionel Sambuc       printf(" ParamPosition=Invalid");
502f4a2713aSLionel Sambuc     break;
503f4a2713aSLionel Sambuc   case CXComment_VerbatimBlockCommand:
504f4a2713aSLionel Sambuc     printf("CXComment_VerbatimBlockCommand");
505f4a2713aSLionel Sambuc     PrintCXStringWithPrefixAndDispose(
506f4a2713aSLionel Sambuc         "CommandName",
507f4a2713aSLionel Sambuc         clang_BlockCommandComment_getCommandName(Comment));
508f4a2713aSLionel Sambuc     break;
509f4a2713aSLionel Sambuc   case CXComment_VerbatimBlockLine:
510f4a2713aSLionel Sambuc     printf("CXComment_VerbatimBlockLine");
511f4a2713aSLionel Sambuc     PrintCXStringWithPrefixAndDispose(
512f4a2713aSLionel Sambuc         "Text",
513f4a2713aSLionel Sambuc         clang_VerbatimBlockLineComment_getText(Comment));
514f4a2713aSLionel Sambuc     break;
515f4a2713aSLionel Sambuc   case CXComment_VerbatimLine:
516f4a2713aSLionel Sambuc     printf("CXComment_VerbatimLine");
517f4a2713aSLionel Sambuc     PrintCXStringWithPrefixAndDispose(
518f4a2713aSLionel Sambuc         "Text",
519f4a2713aSLionel Sambuc         clang_VerbatimLineComment_getText(Comment));
520f4a2713aSLionel Sambuc     break;
521f4a2713aSLionel Sambuc   case CXComment_FullComment:
522f4a2713aSLionel Sambuc     printf("CXComment_FullComment");
523f4a2713aSLionel Sambuc     break;
524f4a2713aSLionel Sambuc   }
525f4a2713aSLionel Sambuc   if (Kind != CXComment_Null) {
526f4a2713aSLionel Sambuc     const unsigned NumChildren = clang_Comment_getNumChildren(Comment);
527f4a2713aSLionel Sambuc     unsigned i;
528f4a2713aSLionel Sambuc     for (i = 0; i != NumChildren; ++i) {
529f4a2713aSLionel Sambuc       printf("\n// %s: ", FileCheckPrefix);
530f4a2713aSLionel Sambuc       DumpCXCommentInternal(Ctx, clang_Comment_getChild(Comment, i));
531f4a2713aSLionel Sambuc     }
532f4a2713aSLionel Sambuc   }
533f4a2713aSLionel Sambuc   printf(")");
534f4a2713aSLionel Sambuc   Ctx->IndentLevel--;
535f4a2713aSLionel Sambuc }
536f4a2713aSLionel Sambuc 
DumpCXComment(CXComment Comment)537f4a2713aSLionel Sambuc static void DumpCXComment(CXComment Comment) {
538f4a2713aSLionel Sambuc   struct CommentASTDumpingContext Ctx;
539f4a2713aSLionel Sambuc   Ctx.IndentLevel = 1;
540f4a2713aSLionel Sambuc   printf("\n// %s:  CommentAST=[\n// %s:", FileCheckPrefix, FileCheckPrefix);
541f4a2713aSLionel Sambuc   DumpCXCommentInternal(&Ctx, Comment);
542f4a2713aSLionel Sambuc   printf("]");
543f4a2713aSLionel Sambuc }
544f4a2713aSLionel Sambuc 
ValidateCommentXML(const char * Str,const char * CommentSchemaFile)545*0a6a1f1dSLionel Sambuc static void ValidateCommentXML(const char *Str, const char *CommentSchemaFile) {
546f4a2713aSLionel Sambuc #ifdef CLANG_HAVE_LIBXML
547f4a2713aSLionel Sambuc   xmlRelaxNGParserCtxtPtr RNGParser;
548f4a2713aSLionel Sambuc   xmlRelaxNGPtr Schema;
549f4a2713aSLionel Sambuc   xmlDocPtr Doc;
550f4a2713aSLionel Sambuc   xmlRelaxNGValidCtxtPtr ValidationCtxt;
551f4a2713aSLionel Sambuc   int status;
552f4a2713aSLionel Sambuc 
553*0a6a1f1dSLionel Sambuc   if (!CommentSchemaFile)
554f4a2713aSLionel Sambuc     return;
555f4a2713aSLionel Sambuc 
556*0a6a1f1dSLionel Sambuc   RNGParser = xmlRelaxNGNewParserCtxt(CommentSchemaFile);
557*0a6a1f1dSLionel Sambuc   if (!RNGParser) {
558f4a2713aSLionel Sambuc     printf(" libXMLError");
559f4a2713aSLionel Sambuc     return;
560f4a2713aSLionel Sambuc   }
561*0a6a1f1dSLionel Sambuc   Schema = xmlRelaxNGParse(RNGParser);
562f4a2713aSLionel Sambuc 
563f4a2713aSLionel Sambuc   Doc = xmlParseDoc((const xmlChar *) Str);
564f4a2713aSLionel Sambuc 
565f4a2713aSLionel Sambuc   if (!Doc) {
566f4a2713aSLionel Sambuc     xmlErrorPtr Error = xmlGetLastError();
567f4a2713aSLionel Sambuc     printf(" CommentXMLInvalid [not well-formed XML: %s]", Error->message);
568f4a2713aSLionel Sambuc     return;
569f4a2713aSLionel Sambuc   }
570f4a2713aSLionel Sambuc 
571*0a6a1f1dSLionel Sambuc   ValidationCtxt = xmlRelaxNGNewValidCtxt(Schema);
572f4a2713aSLionel Sambuc   status = xmlRelaxNGValidateDoc(ValidationCtxt, Doc);
573f4a2713aSLionel Sambuc   if (!status)
574f4a2713aSLionel Sambuc     printf(" CommentXMLValid");
575f4a2713aSLionel Sambuc   else if (status > 0) {
576f4a2713aSLionel Sambuc     xmlErrorPtr Error = xmlGetLastError();
577f4a2713aSLionel Sambuc     printf(" CommentXMLInvalid [not vaild XML: %s]", Error->message);
578f4a2713aSLionel Sambuc   } else
579f4a2713aSLionel Sambuc     printf(" libXMLError");
580f4a2713aSLionel Sambuc 
581f4a2713aSLionel Sambuc   xmlRelaxNGFreeValidCtxt(ValidationCtxt);
582f4a2713aSLionel Sambuc   xmlFreeDoc(Doc);
583*0a6a1f1dSLionel Sambuc   xmlRelaxNGFree(Schema);
584*0a6a1f1dSLionel Sambuc   xmlRelaxNGFreeParserCtxt(RNGParser);
585f4a2713aSLionel Sambuc #endif
586f4a2713aSLionel Sambuc }
587f4a2713aSLionel Sambuc 
PrintCursorComments(CXCursor Cursor,const char * CommentSchemaFile)588f4a2713aSLionel Sambuc static void PrintCursorComments(CXCursor Cursor,
589*0a6a1f1dSLionel Sambuc                                 const char *CommentSchemaFile) {
590f4a2713aSLionel Sambuc   {
591f4a2713aSLionel Sambuc     CXString RawComment;
592f4a2713aSLionel Sambuc     const char *RawCommentCString;
593f4a2713aSLionel Sambuc     CXString BriefComment;
594f4a2713aSLionel Sambuc     const char *BriefCommentCString;
595f4a2713aSLionel Sambuc 
596f4a2713aSLionel Sambuc     RawComment = clang_Cursor_getRawCommentText(Cursor);
597f4a2713aSLionel Sambuc     RawCommentCString = clang_getCString(RawComment);
598f4a2713aSLionel Sambuc     if (RawCommentCString != NULL && RawCommentCString[0] != '\0') {
599f4a2713aSLionel Sambuc       PrintCStringWithPrefix("RawComment", RawCommentCString);
600f4a2713aSLionel Sambuc       PrintRange(clang_Cursor_getCommentRange(Cursor), "RawCommentRange");
601f4a2713aSLionel Sambuc 
602f4a2713aSLionel Sambuc       BriefComment = clang_Cursor_getBriefCommentText(Cursor);
603f4a2713aSLionel Sambuc       BriefCommentCString = clang_getCString(BriefComment);
604f4a2713aSLionel Sambuc       if (BriefCommentCString != NULL && BriefCommentCString[0] != '\0')
605f4a2713aSLionel Sambuc         PrintCStringWithPrefix("BriefComment", BriefCommentCString);
606f4a2713aSLionel Sambuc       clang_disposeString(BriefComment);
607f4a2713aSLionel Sambuc     }
608f4a2713aSLionel Sambuc     clang_disposeString(RawComment);
609f4a2713aSLionel Sambuc   }
610f4a2713aSLionel Sambuc 
611f4a2713aSLionel Sambuc   {
612f4a2713aSLionel Sambuc     CXComment Comment = clang_Cursor_getParsedComment(Cursor);
613f4a2713aSLionel Sambuc     if (clang_Comment_getKind(Comment) != CXComment_Null) {
614f4a2713aSLionel Sambuc       PrintCXStringWithPrefixAndDispose("FullCommentAsHTML",
615f4a2713aSLionel Sambuc                                         clang_FullComment_getAsHTML(Comment));
616f4a2713aSLionel Sambuc       {
617f4a2713aSLionel Sambuc         CXString XML;
618f4a2713aSLionel Sambuc         XML = clang_FullComment_getAsXML(Comment);
619f4a2713aSLionel Sambuc         PrintCXStringWithPrefix("FullCommentAsXML", XML);
620*0a6a1f1dSLionel Sambuc         ValidateCommentXML(clang_getCString(XML), CommentSchemaFile);
621f4a2713aSLionel Sambuc         clang_disposeString(XML);
622f4a2713aSLionel Sambuc       }
623f4a2713aSLionel Sambuc 
624f4a2713aSLionel Sambuc       DumpCXComment(Comment);
625f4a2713aSLionel Sambuc     }
626f4a2713aSLionel Sambuc   }
627f4a2713aSLionel Sambuc }
628f4a2713aSLionel Sambuc 
629f4a2713aSLionel Sambuc typedef struct {
630f4a2713aSLionel Sambuc   unsigned line;
631f4a2713aSLionel Sambuc   unsigned col;
632f4a2713aSLionel Sambuc } LineCol;
633f4a2713aSLionel Sambuc 
lineCol_cmp(const void * p1,const void * p2)634f4a2713aSLionel Sambuc static int lineCol_cmp(const void *p1, const void *p2) {
635f4a2713aSLionel Sambuc   const LineCol *lhs = p1;
636f4a2713aSLionel Sambuc   const LineCol *rhs = p2;
637f4a2713aSLionel Sambuc   if (lhs->line != rhs->line)
638f4a2713aSLionel Sambuc     return (int)lhs->line - (int)rhs->line;
639f4a2713aSLionel Sambuc   return (int)lhs->col - (int)rhs->col;
640f4a2713aSLionel Sambuc }
641f4a2713aSLionel Sambuc 
PrintCursor(CXCursor Cursor,const char * CommentSchemaFile)642*0a6a1f1dSLionel Sambuc static void PrintCursor(CXCursor Cursor, const char *CommentSchemaFile) {
643f4a2713aSLionel Sambuc   CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor);
644f4a2713aSLionel Sambuc   if (clang_isInvalid(Cursor.kind)) {
645f4a2713aSLionel Sambuc     CXString ks = clang_getCursorKindSpelling(Cursor.kind);
646f4a2713aSLionel Sambuc     printf("Invalid Cursor => %s", clang_getCString(ks));
647f4a2713aSLionel Sambuc     clang_disposeString(ks);
648f4a2713aSLionel Sambuc   }
649f4a2713aSLionel Sambuc   else {
650f4a2713aSLionel Sambuc     CXString string, ks;
651f4a2713aSLionel Sambuc     CXCursor Referenced;
652f4a2713aSLionel Sambuc     unsigned line, column;
653f4a2713aSLionel Sambuc     CXCursor SpecializationOf;
654f4a2713aSLionel Sambuc     CXCursor *overridden;
655f4a2713aSLionel Sambuc     unsigned num_overridden;
656f4a2713aSLionel Sambuc     unsigned RefNameRangeNr;
657f4a2713aSLionel Sambuc     CXSourceRange CursorExtent;
658f4a2713aSLionel Sambuc     CXSourceRange RefNameRange;
659f4a2713aSLionel Sambuc     int AlwaysUnavailable;
660f4a2713aSLionel Sambuc     int AlwaysDeprecated;
661f4a2713aSLionel Sambuc     CXString UnavailableMessage;
662f4a2713aSLionel Sambuc     CXString DeprecatedMessage;
663f4a2713aSLionel Sambuc     CXPlatformAvailability PlatformAvailability[2];
664f4a2713aSLionel Sambuc     int NumPlatformAvailability;
665f4a2713aSLionel Sambuc     int I;
666f4a2713aSLionel Sambuc 
667f4a2713aSLionel Sambuc     ks = clang_getCursorKindSpelling(Cursor.kind);
668f4a2713aSLionel Sambuc     string = want_display_name? clang_getCursorDisplayName(Cursor)
669f4a2713aSLionel Sambuc                               : clang_getCursorSpelling(Cursor);
670f4a2713aSLionel Sambuc     printf("%s=%s", clang_getCString(ks),
671f4a2713aSLionel Sambuc                     clang_getCString(string));
672f4a2713aSLionel Sambuc     clang_disposeString(ks);
673f4a2713aSLionel Sambuc     clang_disposeString(string);
674f4a2713aSLionel Sambuc 
675f4a2713aSLionel Sambuc     Referenced = clang_getCursorReferenced(Cursor);
676f4a2713aSLionel Sambuc     if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
677f4a2713aSLionel Sambuc       if (clang_getCursorKind(Referenced) == CXCursor_OverloadedDeclRef) {
678f4a2713aSLionel Sambuc         unsigned I, N = clang_getNumOverloadedDecls(Referenced);
679f4a2713aSLionel Sambuc         printf("[");
680f4a2713aSLionel Sambuc         for (I = 0; I != N; ++I) {
681f4a2713aSLionel Sambuc           CXCursor Ovl = clang_getOverloadedDecl(Referenced, I);
682f4a2713aSLionel Sambuc           CXSourceLocation Loc;
683f4a2713aSLionel Sambuc           if (I)
684f4a2713aSLionel Sambuc             printf(", ");
685f4a2713aSLionel Sambuc 
686f4a2713aSLionel Sambuc           Loc = clang_getCursorLocation(Ovl);
687f4a2713aSLionel Sambuc           clang_getSpellingLocation(Loc, 0, &line, &column, 0);
688f4a2713aSLionel Sambuc           printf("%d:%d", line, column);
689f4a2713aSLionel Sambuc         }
690f4a2713aSLionel Sambuc         printf("]");
691f4a2713aSLionel Sambuc       } else {
692f4a2713aSLionel Sambuc         CXSourceLocation Loc = clang_getCursorLocation(Referenced);
693f4a2713aSLionel Sambuc         clang_getSpellingLocation(Loc, 0, &line, &column, 0);
694f4a2713aSLionel Sambuc         printf(":%d:%d", line, column);
695f4a2713aSLionel Sambuc       }
696f4a2713aSLionel Sambuc     }
697f4a2713aSLionel Sambuc 
698f4a2713aSLionel Sambuc     if (clang_isCursorDefinition(Cursor))
699f4a2713aSLionel Sambuc       printf(" (Definition)");
700f4a2713aSLionel Sambuc 
701f4a2713aSLionel Sambuc     switch (clang_getCursorAvailability(Cursor)) {
702f4a2713aSLionel Sambuc       case CXAvailability_Available:
703f4a2713aSLionel Sambuc         break;
704f4a2713aSLionel Sambuc 
705f4a2713aSLionel Sambuc       case CXAvailability_Deprecated:
706f4a2713aSLionel Sambuc         printf(" (deprecated)");
707f4a2713aSLionel Sambuc         break;
708f4a2713aSLionel Sambuc 
709f4a2713aSLionel Sambuc       case CXAvailability_NotAvailable:
710f4a2713aSLionel Sambuc         printf(" (unavailable)");
711f4a2713aSLionel Sambuc         break;
712f4a2713aSLionel Sambuc 
713f4a2713aSLionel Sambuc       case CXAvailability_NotAccessible:
714f4a2713aSLionel Sambuc         printf(" (inaccessible)");
715f4a2713aSLionel Sambuc         break;
716f4a2713aSLionel Sambuc     }
717f4a2713aSLionel Sambuc 
718f4a2713aSLionel Sambuc     NumPlatformAvailability
719f4a2713aSLionel Sambuc       = clang_getCursorPlatformAvailability(Cursor,
720f4a2713aSLionel Sambuc                                             &AlwaysDeprecated,
721f4a2713aSLionel Sambuc                                             &DeprecatedMessage,
722f4a2713aSLionel Sambuc                                             &AlwaysUnavailable,
723f4a2713aSLionel Sambuc                                             &UnavailableMessage,
724f4a2713aSLionel Sambuc                                             PlatformAvailability, 2);
725f4a2713aSLionel Sambuc     if (AlwaysUnavailable) {
726f4a2713aSLionel Sambuc       printf("  (always unavailable: \"%s\")",
727f4a2713aSLionel Sambuc              clang_getCString(UnavailableMessage));
728f4a2713aSLionel Sambuc     } else if (AlwaysDeprecated) {
729f4a2713aSLionel Sambuc       printf("  (always deprecated: \"%s\")",
730f4a2713aSLionel Sambuc              clang_getCString(DeprecatedMessage));
731f4a2713aSLionel Sambuc     } else {
732f4a2713aSLionel Sambuc       for (I = 0; I != NumPlatformAvailability; ++I) {
733f4a2713aSLionel Sambuc         if (I >= 2)
734f4a2713aSLionel Sambuc           break;
735f4a2713aSLionel Sambuc 
736f4a2713aSLionel Sambuc         printf("  (%s", clang_getCString(PlatformAvailability[I].Platform));
737f4a2713aSLionel Sambuc         if (PlatformAvailability[I].Unavailable)
738f4a2713aSLionel Sambuc           printf(", unavailable");
739f4a2713aSLionel Sambuc         else {
740f4a2713aSLionel Sambuc           printVersion(", introduced=", PlatformAvailability[I].Introduced);
741f4a2713aSLionel Sambuc           printVersion(", deprecated=", PlatformAvailability[I].Deprecated);
742f4a2713aSLionel Sambuc           printVersion(", obsoleted=", PlatformAvailability[I].Obsoleted);
743f4a2713aSLionel Sambuc         }
744f4a2713aSLionel Sambuc         if (clang_getCString(PlatformAvailability[I].Message)[0])
745f4a2713aSLionel Sambuc           printf(", message=\"%s\"",
746f4a2713aSLionel Sambuc                  clang_getCString(PlatformAvailability[I].Message));
747f4a2713aSLionel Sambuc         printf(")");
748f4a2713aSLionel Sambuc       }
749f4a2713aSLionel Sambuc     }
750f4a2713aSLionel Sambuc     for (I = 0; I != NumPlatformAvailability; ++I) {
751f4a2713aSLionel Sambuc       if (I >= 2)
752f4a2713aSLionel Sambuc         break;
753f4a2713aSLionel Sambuc       clang_disposeCXPlatformAvailability(PlatformAvailability + I);
754f4a2713aSLionel Sambuc     }
755f4a2713aSLionel Sambuc 
756f4a2713aSLionel Sambuc     clang_disposeString(DeprecatedMessage);
757f4a2713aSLionel Sambuc     clang_disposeString(UnavailableMessage);
758f4a2713aSLionel Sambuc 
759f4a2713aSLionel Sambuc     if (clang_CXXMethod_isStatic(Cursor))
760f4a2713aSLionel Sambuc       printf(" (static)");
761f4a2713aSLionel Sambuc     if (clang_CXXMethod_isVirtual(Cursor))
762f4a2713aSLionel Sambuc       printf(" (virtual)");
763*0a6a1f1dSLionel Sambuc     if (clang_CXXMethod_isConst(Cursor))
764*0a6a1f1dSLionel Sambuc       printf(" (const)");
765f4a2713aSLionel Sambuc     if (clang_CXXMethod_isPureVirtual(Cursor))
766f4a2713aSLionel Sambuc       printf(" (pure)");
767f4a2713aSLionel Sambuc     if (clang_Cursor_isVariadic(Cursor))
768f4a2713aSLionel Sambuc       printf(" (variadic)");
769f4a2713aSLionel Sambuc     if (clang_Cursor_isObjCOptional(Cursor))
770f4a2713aSLionel Sambuc       printf(" (@optional)");
771f4a2713aSLionel Sambuc 
772f4a2713aSLionel Sambuc     if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
773f4a2713aSLionel Sambuc       CXType T =
774f4a2713aSLionel Sambuc         clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor));
775f4a2713aSLionel Sambuc       CXString S = clang_getTypeKindSpelling(T.kind);
776f4a2713aSLionel Sambuc       printf(" [IBOutletCollection=%s]", clang_getCString(S));
777f4a2713aSLionel Sambuc       clang_disposeString(S);
778f4a2713aSLionel Sambuc     }
779f4a2713aSLionel Sambuc 
780f4a2713aSLionel Sambuc     if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
781f4a2713aSLionel Sambuc       enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
782f4a2713aSLionel Sambuc       unsigned isVirtual = clang_isVirtualBase(Cursor);
783f4a2713aSLionel Sambuc       const char *accessStr = 0;
784f4a2713aSLionel Sambuc 
785f4a2713aSLionel Sambuc       switch (access) {
786f4a2713aSLionel Sambuc         case CX_CXXInvalidAccessSpecifier:
787f4a2713aSLionel Sambuc           accessStr = "invalid"; break;
788f4a2713aSLionel Sambuc         case CX_CXXPublic:
789f4a2713aSLionel Sambuc           accessStr = "public"; break;
790f4a2713aSLionel Sambuc         case CX_CXXProtected:
791f4a2713aSLionel Sambuc           accessStr = "protected"; break;
792f4a2713aSLionel Sambuc         case CX_CXXPrivate:
793f4a2713aSLionel Sambuc           accessStr = "private"; break;
794f4a2713aSLionel Sambuc       }
795f4a2713aSLionel Sambuc 
796f4a2713aSLionel Sambuc       printf(" [access=%s isVirtual=%s]", accessStr,
797f4a2713aSLionel Sambuc              isVirtual ? "true" : "false");
798f4a2713aSLionel Sambuc     }
799f4a2713aSLionel Sambuc 
800f4a2713aSLionel Sambuc     SpecializationOf = clang_getSpecializedCursorTemplate(Cursor);
801f4a2713aSLionel Sambuc     if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
802f4a2713aSLionel Sambuc       CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
803f4a2713aSLionel Sambuc       CXString Name = clang_getCursorSpelling(SpecializationOf);
804f4a2713aSLionel Sambuc       clang_getSpellingLocation(Loc, 0, &line, &column, 0);
805f4a2713aSLionel Sambuc       printf(" [Specialization of %s:%d:%d]",
806f4a2713aSLionel Sambuc              clang_getCString(Name), line, column);
807f4a2713aSLionel Sambuc       clang_disposeString(Name);
808*0a6a1f1dSLionel Sambuc 
809*0a6a1f1dSLionel Sambuc       if (Cursor.kind == CXCursor_FunctionDecl) {
810*0a6a1f1dSLionel Sambuc         /* Collect the template parameter kinds from the base template. */
811*0a6a1f1dSLionel Sambuc         unsigned NumTemplateArgs = clang_Cursor_getNumTemplateArguments(Cursor);
812*0a6a1f1dSLionel Sambuc         unsigned I;
813*0a6a1f1dSLionel Sambuc         for (I = 0; I < NumTemplateArgs; I++) {
814*0a6a1f1dSLionel Sambuc           enum CXTemplateArgumentKind TAK =
815*0a6a1f1dSLionel Sambuc               clang_Cursor_getTemplateArgumentKind(Cursor, I);
816*0a6a1f1dSLionel Sambuc           switch(TAK) {
817*0a6a1f1dSLionel Sambuc             case CXTemplateArgumentKind_Type:
818*0a6a1f1dSLionel Sambuc               {
819*0a6a1f1dSLionel Sambuc                 CXType T = clang_Cursor_getTemplateArgumentType(Cursor, I);
820*0a6a1f1dSLionel Sambuc                 CXString S = clang_getTypeSpelling(T);
821*0a6a1f1dSLionel Sambuc                 printf(" [Template arg %d: kind: %d, type: %s]",
822*0a6a1f1dSLionel Sambuc                        I, TAK, clang_getCString(S));
823*0a6a1f1dSLionel Sambuc                 clang_disposeString(S);
824*0a6a1f1dSLionel Sambuc               }
825*0a6a1f1dSLionel Sambuc               break;
826*0a6a1f1dSLionel Sambuc             case CXTemplateArgumentKind_Integral:
827*0a6a1f1dSLionel Sambuc               printf(" [Template arg %d: kind: %d, intval: %lld]",
828*0a6a1f1dSLionel Sambuc                      I, TAK, clang_Cursor_getTemplateArgumentValue(Cursor, I));
829*0a6a1f1dSLionel Sambuc               break;
830*0a6a1f1dSLionel Sambuc             default:
831*0a6a1f1dSLionel Sambuc               printf(" [Template arg %d: kind: %d]\n", I, TAK);
832*0a6a1f1dSLionel Sambuc           }
833*0a6a1f1dSLionel Sambuc         }
834*0a6a1f1dSLionel Sambuc       }
835f4a2713aSLionel Sambuc     }
836f4a2713aSLionel Sambuc 
837f4a2713aSLionel Sambuc     clang_getOverriddenCursors(Cursor, &overridden, &num_overridden);
838f4a2713aSLionel Sambuc     if (num_overridden) {
839f4a2713aSLionel Sambuc       unsigned I;
840f4a2713aSLionel Sambuc       LineCol lineCols[50];
841f4a2713aSLionel Sambuc       assert(num_overridden <= 50);
842f4a2713aSLionel Sambuc       printf(" [Overrides ");
843f4a2713aSLionel Sambuc       for (I = 0; I != num_overridden; ++I) {
844f4a2713aSLionel Sambuc         CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
845f4a2713aSLionel Sambuc         clang_getSpellingLocation(Loc, 0, &line, &column, 0);
846f4a2713aSLionel Sambuc         lineCols[I].line = line;
847f4a2713aSLionel Sambuc         lineCols[I].col = column;
848f4a2713aSLionel Sambuc       }
849f4a2713aSLionel Sambuc       /* Make the order of the override list deterministic. */
850f4a2713aSLionel Sambuc       qsort(lineCols, num_overridden, sizeof(LineCol), lineCol_cmp);
851f4a2713aSLionel Sambuc       for (I = 0; I != num_overridden; ++I) {
852f4a2713aSLionel Sambuc         if (I)
853f4a2713aSLionel Sambuc           printf(", ");
854f4a2713aSLionel Sambuc         printf("@%d:%d", lineCols[I].line, lineCols[I].col);
855f4a2713aSLionel Sambuc       }
856f4a2713aSLionel Sambuc       printf("]");
857f4a2713aSLionel Sambuc       clang_disposeOverriddenCursors(overridden);
858f4a2713aSLionel Sambuc     }
859f4a2713aSLionel Sambuc 
860f4a2713aSLionel Sambuc     if (Cursor.kind == CXCursor_InclusionDirective) {
861f4a2713aSLionel Sambuc       CXFile File = clang_getIncludedFile(Cursor);
862f4a2713aSLionel Sambuc       CXString Included = clang_getFileName(File);
863f4a2713aSLionel Sambuc       printf(" (%s)", clang_getCString(Included));
864f4a2713aSLionel Sambuc       clang_disposeString(Included);
865f4a2713aSLionel Sambuc 
866f4a2713aSLionel Sambuc       if (clang_isFileMultipleIncludeGuarded(TU, File))
867f4a2713aSLionel Sambuc         printf("  [multi-include guarded]");
868f4a2713aSLionel Sambuc     }
869f4a2713aSLionel Sambuc 
870f4a2713aSLionel Sambuc     CursorExtent = clang_getCursorExtent(Cursor);
871f4a2713aSLionel Sambuc     RefNameRange = clang_getCursorReferenceNameRange(Cursor,
872f4a2713aSLionel Sambuc                                                    CXNameRange_WantQualifier
873f4a2713aSLionel Sambuc                                                  | CXNameRange_WantSinglePiece
874f4a2713aSLionel Sambuc                                                  | CXNameRange_WantTemplateArgs,
875f4a2713aSLionel Sambuc                                                      0);
876f4a2713aSLionel Sambuc     if (!clang_equalRanges(CursorExtent, RefNameRange))
877f4a2713aSLionel Sambuc       PrintRange(RefNameRange, "SingleRefName");
878f4a2713aSLionel Sambuc 
879f4a2713aSLionel Sambuc     for (RefNameRangeNr = 0; 1; RefNameRangeNr++) {
880f4a2713aSLionel Sambuc       RefNameRange = clang_getCursorReferenceNameRange(Cursor,
881f4a2713aSLionel Sambuc                                                    CXNameRange_WantQualifier
882f4a2713aSLionel Sambuc                                                  | CXNameRange_WantTemplateArgs,
883f4a2713aSLionel Sambuc                                                        RefNameRangeNr);
884f4a2713aSLionel Sambuc       if (clang_equalRanges(clang_getNullRange(), RefNameRange))
885f4a2713aSLionel Sambuc         break;
886f4a2713aSLionel Sambuc       if (!clang_equalRanges(CursorExtent, RefNameRange))
887f4a2713aSLionel Sambuc         PrintRange(RefNameRange, "RefName");
888f4a2713aSLionel Sambuc     }
889f4a2713aSLionel Sambuc 
890*0a6a1f1dSLionel Sambuc     PrintCursorComments(Cursor, CommentSchemaFile);
891f4a2713aSLionel Sambuc 
892f4a2713aSLionel Sambuc     {
893f4a2713aSLionel Sambuc       unsigned PropAttrs = clang_Cursor_getObjCPropertyAttributes(Cursor, 0);
894f4a2713aSLionel Sambuc       if (PropAttrs != CXObjCPropertyAttr_noattr) {
895f4a2713aSLionel Sambuc         printf(" [");
896f4a2713aSLionel Sambuc         #define PRINT_PROP_ATTR(A) \
897f4a2713aSLionel Sambuc           if (PropAttrs & CXObjCPropertyAttr_##A) printf(#A ",")
898f4a2713aSLionel Sambuc         PRINT_PROP_ATTR(readonly);
899f4a2713aSLionel Sambuc         PRINT_PROP_ATTR(getter);
900f4a2713aSLionel Sambuc         PRINT_PROP_ATTR(assign);
901f4a2713aSLionel Sambuc         PRINT_PROP_ATTR(readwrite);
902f4a2713aSLionel Sambuc         PRINT_PROP_ATTR(retain);
903f4a2713aSLionel Sambuc         PRINT_PROP_ATTR(copy);
904f4a2713aSLionel Sambuc         PRINT_PROP_ATTR(nonatomic);
905f4a2713aSLionel Sambuc         PRINT_PROP_ATTR(setter);
906f4a2713aSLionel Sambuc         PRINT_PROP_ATTR(atomic);
907f4a2713aSLionel Sambuc         PRINT_PROP_ATTR(weak);
908f4a2713aSLionel Sambuc         PRINT_PROP_ATTR(strong);
909f4a2713aSLionel Sambuc         PRINT_PROP_ATTR(unsafe_unretained);
910f4a2713aSLionel Sambuc         printf("]");
911f4a2713aSLionel Sambuc       }
912f4a2713aSLionel Sambuc     }
913f4a2713aSLionel Sambuc 
914f4a2713aSLionel Sambuc     {
915f4a2713aSLionel Sambuc       unsigned QT = clang_Cursor_getObjCDeclQualifiers(Cursor);
916f4a2713aSLionel Sambuc       if (QT != CXObjCDeclQualifier_None) {
917f4a2713aSLionel Sambuc         printf(" [");
918f4a2713aSLionel Sambuc         #define PRINT_OBJC_QUAL(A) \
919f4a2713aSLionel Sambuc           if (QT & CXObjCDeclQualifier_##A) printf(#A ",")
920f4a2713aSLionel Sambuc         PRINT_OBJC_QUAL(In);
921f4a2713aSLionel Sambuc         PRINT_OBJC_QUAL(Inout);
922f4a2713aSLionel Sambuc         PRINT_OBJC_QUAL(Out);
923f4a2713aSLionel Sambuc         PRINT_OBJC_QUAL(Bycopy);
924f4a2713aSLionel Sambuc         PRINT_OBJC_QUAL(Byref);
925f4a2713aSLionel Sambuc         PRINT_OBJC_QUAL(Oneway);
926f4a2713aSLionel Sambuc         printf("]");
927f4a2713aSLionel Sambuc       }
928f4a2713aSLionel Sambuc     }
929f4a2713aSLionel Sambuc   }
930f4a2713aSLionel Sambuc }
931f4a2713aSLionel Sambuc 
GetCursorSource(CXCursor Cursor)932f4a2713aSLionel Sambuc static const char* GetCursorSource(CXCursor Cursor) {
933f4a2713aSLionel Sambuc   CXSourceLocation Loc = clang_getCursorLocation(Cursor);
934f4a2713aSLionel Sambuc   CXString source;
935f4a2713aSLionel Sambuc   CXFile file;
936f4a2713aSLionel Sambuc   clang_getExpansionLocation(Loc, &file, 0, 0, 0);
937f4a2713aSLionel Sambuc   source = clang_getFileName(file);
938f4a2713aSLionel Sambuc   if (!clang_getCString(source)) {
939f4a2713aSLionel Sambuc     clang_disposeString(source);
940f4a2713aSLionel Sambuc     return "<invalid loc>";
941f4a2713aSLionel Sambuc   }
942f4a2713aSLionel Sambuc   else {
943f4a2713aSLionel Sambuc     const char *b = basename(clang_getCString(source));
944f4a2713aSLionel Sambuc     clang_disposeString(source);
945f4a2713aSLionel Sambuc     return b;
946f4a2713aSLionel Sambuc   }
947f4a2713aSLionel Sambuc }
948f4a2713aSLionel Sambuc 
949f4a2713aSLionel Sambuc /******************************************************************************/
950f4a2713aSLionel Sambuc /* Callbacks.                                                                 */
951f4a2713aSLionel Sambuc /******************************************************************************/
952f4a2713aSLionel Sambuc 
953f4a2713aSLionel Sambuc typedef void (*PostVisitTU)(CXTranslationUnit);
954f4a2713aSLionel Sambuc 
PrintDiagnostic(CXDiagnostic Diagnostic)955f4a2713aSLionel Sambuc void PrintDiagnostic(CXDiagnostic Diagnostic) {
956f4a2713aSLionel Sambuc   FILE *out = stderr;
957f4a2713aSLionel Sambuc   CXFile file;
958f4a2713aSLionel Sambuc   CXString Msg;
959f4a2713aSLionel Sambuc   unsigned display_opts = CXDiagnostic_DisplaySourceLocation
960f4a2713aSLionel Sambuc     | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges
961f4a2713aSLionel Sambuc     | CXDiagnostic_DisplayOption;
962f4a2713aSLionel Sambuc   unsigned i, num_fixits;
963f4a2713aSLionel Sambuc 
964f4a2713aSLionel Sambuc   if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
965f4a2713aSLionel Sambuc     return;
966f4a2713aSLionel Sambuc 
967f4a2713aSLionel Sambuc   Msg = clang_formatDiagnostic(Diagnostic, display_opts);
968f4a2713aSLionel Sambuc   fprintf(stderr, "%s\n", clang_getCString(Msg));
969f4a2713aSLionel Sambuc   clang_disposeString(Msg);
970f4a2713aSLionel Sambuc 
971f4a2713aSLionel Sambuc   clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
972f4a2713aSLionel Sambuc                             &file, 0, 0, 0);
973f4a2713aSLionel Sambuc   if (!file)
974f4a2713aSLionel Sambuc     return;
975f4a2713aSLionel Sambuc 
976f4a2713aSLionel Sambuc   num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
977f4a2713aSLionel Sambuc   fprintf(stderr, "Number FIX-ITs = %d\n", num_fixits);
978f4a2713aSLionel Sambuc   for (i = 0; i != num_fixits; ++i) {
979f4a2713aSLionel Sambuc     CXSourceRange range;
980f4a2713aSLionel Sambuc     CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
981f4a2713aSLionel Sambuc     CXSourceLocation start = clang_getRangeStart(range);
982f4a2713aSLionel Sambuc     CXSourceLocation end = clang_getRangeEnd(range);
983f4a2713aSLionel Sambuc     unsigned start_line, start_column, end_line, end_column;
984f4a2713aSLionel Sambuc     CXFile start_file, end_file;
985f4a2713aSLionel Sambuc     clang_getSpellingLocation(start, &start_file, &start_line,
986f4a2713aSLionel Sambuc                               &start_column, 0);
987f4a2713aSLionel Sambuc     clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
988f4a2713aSLionel Sambuc     if (clang_equalLocations(start, end)) {
989f4a2713aSLionel Sambuc       /* Insertion. */
990f4a2713aSLionel Sambuc       if (start_file == file)
991f4a2713aSLionel Sambuc         fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
992f4a2713aSLionel Sambuc                 clang_getCString(insertion_text), start_line, start_column);
993f4a2713aSLionel Sambuc     } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
994f4a2713aSLionel Sambuc       /* Removal. */
995f4a2713aSLionel Sambuc       if (start_file == file && end_file == file) {
996f4a2713aSLionel Sambuc         fprintf(out, "FIX-IT: Remove ");
997f4a2713aSLionel Sambuc         PrintExtent(out, start_line, start_column, end_line, end_column);
998f4a2713aSLionel Sambuc         fprintf(out, "\n");
999f4a2713aSLionel Sambuc       }
1000f4a2713aSLionel Sambuc     } else {
1001f4a2713aSLionel Sambuc       /* Replacement. */
1002f4a2713aSLionel Sambuc       if (start_file == end_file) {
1003f4a2713aSLionel Sambuc         fprintf(out, "FIX-IT: Replace ");
1004f4a2713aSLionel Sambuc         PrintExtent(out, start_line, start_column, end_line, end_column);
1005f4a2713aSLionel Sambuc         fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
1006f4a2713aSLionel Sambuc       }
1007f4a2713aSLionel Sambuc     }
1008f4a2713aSLionel Sambuc     clang_disposeString(insertion_text);
1009f4a2713aSLionel Sambuc   }
1010f4a2713aSLionel Sambuc }
1011f4a2713aSLionel Sambuc 
PrintDiagnosticSet(CXDiagnosticSet Set)1012f4a2713aSLionel Sambuc void PrintDiagnosticSet(CXDiagnosticSet Set) {
1013f4a2713aSLionel Sambuc   int i = 0, n = clang_getNumDiagnosticsInSet(Set);
1014f4a2713aSLionel Sambuc   for ( ; i != n ; ++i) {
1015f4a2713aSLionel Sambuc     CXDiagnostic Diag = clang_getDiagnosticInSet(Set, i);
1016f4a2713aSLionel Sambuc     CXDiagnosticSet ChildDiags = clang_getChildDiagnostics(Diag);
1017f4a2713aSLionel Sambuc     PrintDiagnostic(Diag);
1018f4a2713aSLionel Sambuc     if (ChildDiags)
1019f4a2713aSLionel Sambuc       PrintDiagnosticSet(ChildDiags);
1020f4a2713aSLionel Sambuc   }
1021f4a2713aSLionel Sambuc }
1022f4a2713aSLionel Sambuc 
PrintDiagnostics(CXTranslationUnit TU)1023f4a2713aSLionel Sambuc void PrintDiagnostics(CXTranslationUnit TU) {
1024f4a2713aSLionel Sambuc   CXDiagnosticSet TUSet = clang_getDiagnosticSetFromTU(TU);
1025f4a2713aSLionel Sambuc   PrintDiagnosticSet(TUSet);
1026f4a2713aSLionel Sambuc   clang_disposeDiagnosticSet(TUSet);
1027f4a2713aSLionel Sambuc }
1028f4a2713aSLionel Sambuc 
PrintMemoryUsage(CXTranslationUnit TU)1029f4a2713aSLionel Sambuc void PrintMemoryUsage(CXTranslationUnit TU) {
1030f4a2713aSLionel Sambuc   unsigned long total = 0;
1031f4a2713aSLionel Sambuc   unsigned i = 0;
1032f4a2713aSLionel Sambuc   CXTUResourceUsage usage = clang_getCXTUResourceUsage(TU);
1033f4a2713aSLionel Sambuc   fprintf(stderr, "Memory usage:\n");
1034f4a2713aSLionel Sambuc   for (i = 0 ; i != usage.numEntries; ++i) {
1035f4a2713aSLionel Sambuc     const char *name = clang_getTUResourceUsageName(usage.entries[i].kind);
1036f4a2713aSLionel Sambuc     unsigned long amount = usage.entries[i].amount;
1037f4a2713aSLionel Sambuc     total += amount;
1038f4a2713aSLionel Sambuc     fprintf(stderr, "  %s : %ld bytes (%f MBytes)\n", name, amount,
1039f4a2713aSLionel Sambuc             ((double) amount)/(1024*1024));
1040f4a2713aSLionel Sambuc   }
1041f4a2713aSLionel Sambuc   fprintf(stderr, "  TOTAL = %ld bytes (%f MBytes)\n", total,
1042f4a2713aSLionel Sambuc           ((double) total)/(1024*1024));
1043f4a2713aSLionel Sambuc   clang_disposeCXTUResourceUsage(usage);
1044f4a2713aSLionel Sambuc }
1045f4a2713aSLionel Sambuc 
1046f4a2713aSLionel Sambuc /******************************************************************************/
1047f4a2713aSLionel Sambuc /* Logic for testing traversal.                                               */
1048f4a2713aSLionel Sambuc /******************************************************************************/
1049f4a2713aSLionel Sambuc 
PrintCursorExtent(CXCursor C)1050f4a2713aSLionel Sambuc static void PrintCursorExtent(CXCursor C) {
1051f4a2713aSLionel Sambuc   CXSourceRange extent = clang_getCursorExtent(C);
1052f4a2713aSLionel Sambuc   PrintRange(extent, "Extent");
1053f4a2713aSLionel Sambuc }
1054f4a2713aSLionel Sambuc 
1055f4a2713aSLionel Sambuc /* Data used by the visitors. */
1056f4a2713aSLionel Sambuc typedef struct {
1057f4a2713aSLionel Sambuc   CXTranslationUnit TU;
1058f4a2713aSLionel Sambuc   enum CXCursorKind *Filter;
1059*0a6a1f1dSLionel Sambuc   const char *CommentSchemaFile;
1060f4a2713aSLionel Sambuc } VisitorData;
1061f4a2713aSLionel Sambuc 
1062f4a2713aSLionel Sambuc 
FilteredPrintingVisitor(CXCursor Cursor,CXCursor Parent,CXClientData ClientData)1063f4a2713aSLionel Sambuc enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
1064f4a2713aSLionel Sambuc                                                 CXCursor Parent,
1065f4a2713aSLionel Sambuc                                                 CXClientData ClientData) {
1066f4a2713aSLionel Sambuc   VisitorData *Data = (VisitorData *)ClientData;
1067f4a2713aSLionel Sambuc   if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
1068f4a2713aSLionel Sambuc     CXSourceLocation Loc = clang_getCursorLocation(Cursor);
1069f4a2713aSLionel Sambuc     unsigned line, column;
1070f4a2713aSLionel Sambuc     clang_getSpellingLocation(Loc, 0, &line, &column, 0);
1071f4a2713aSLionel Sambuc     printf("// %s: %s:%d:%d: ", FileCheckPrefix,
1072f4a2713aSLionel Sambuc            GetCursorSource(Cursor), line, column);
1073*0a6a1f1dSLionel Sambuc     PrintCursor(Cursor, Data->CommentSchemaFile);
1074f4a2713aSLionel Sambuc     PrintCursorExtent(Cursor);
1075f4a2713aSLionel Sambuc     if (clang_isDeclaration(Cursor.kind)) {
1076f4a2713aSLionel Sambuc       enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
1077f4a2713aSLionel Sambuc       const char *accessStr = 0;
1078f4a2713aSLionel Sambuc 
1079f4a2713aSLionel Sambuc       switch (access) {
1080f4a2713aSLionel Sambuc         case CX_CXXInvalidAccessSpecifier: break;
1081f4a2713aSLionel Sambuc         case CX_CXXPublic:
1082f4a2713aSLionel Sambuc           accessStr = "public"; break;
1083f4a2713aSLionel Sambuc         case CX_CXXProtected:
1084f4a2713aSLionel Sambuc           accessStr = "protected"; break;
1085f4a2713aSLionel Sambuc         case CX_CXXPrivate:
1086f4a2713aSLionel Sambuc           accessStr = "private"; break;
1087f4a2713aSLionel Sambuc       }
1088f4a2713aSLionel Sambuc 
1089f4a2713aSLionel Sambuc       if (accessStr)
1090f4a2713aSLionel Sambuc         printf(" [access=%s]", accessStr);
1091f4a2713aSLionel Sambuc     }
1092f4a2713aSLionel Sambuc     printf("\n");
1093f4a2713aSLionel Sambuc     return CXChildVisit_Recurse;
1094f4a2713aSLionel Sambuc   }
1095f4a2713aSLionel Sambuc 
1096f4a2713aSLionel Sambuc   return CXChildVisit_Continue;
1097f4a2713aSLionel Sambuc }
1098f4a2713aSLionel Sambuc 
FunctionScanVisitor(CXCursor Cursor,CXCursor Parent,CXClientData ClientData)1099f4a2713aSLionel Sambuc static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
1100f4a2713aSLionel Sambuc                                                    CXCursor Parent,
1101f4a2713aSLionel Sambuc                                                    CXClientData ClientData) {
1102f4a2713aSLionel Sambuc   const char *startBuf, *endBuf;
1103f4a2713aSLionel Sambuc   unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
1104f4a2713aSLionel Sambuc   CXCursor Ref;
1105f4a2713aSLionel Sambuc   VisitorData *Data = (VisitorData *)ClientData;
1106f4a2713aSLionel Sambuc 
1107f4a2713aSLionel Sambuc   if (Cursor.kind != CXCursor_FunctionDecl ||
1108f4a2713aSLionel Sambuc       !clang_isCursorDefinition(Cursor))
1109f4a2713aSLionel Sambuc     return CXChildVisit_Continue;
1110f4a2713aSLionel Sambuc 
1111f4a2713aSLionel Sambuc   clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
1112f4a2713aSLionel Sambuc                                        &startLine, &startColumn,
1113f4a2713aSLionel Sambuc                                        &endLine, &endColumn);
1114f4a2713aSLionel Sambuc   /* Probe the entire body, looking for both decls and refs. */
1115f4a2713aSLionel Sambuc   curLine = startLine;
1116f4a2713aSLionel Sambuc   curColumn = startColumn;
1117f4a2713aSLionel Sambuc 
1118f4a2713aSLionel Sambuc   while (startBuf < endBuf) {
1119f4a2713aSLionel Sambuc     CXSourceLocation Loc;
1120f4a2713aSLionel Sambuc     CXFile file;
1121f4a2713aSLionel Sambuc     CXString source;
1122f4a2713aSLionel Sambuc 
1123f4a2713aSLionel Sambuc     if (*startBuf == '\n') {
1124f4a2713aSLionel Sambuc       startBuf++;
1125f4a2713aSLionel Sambuc       curLine++;
1126f4a2713aSLionel Sambuc       curColumn = 1;
1127f4a2713aSLionel Sambuc     } else if (*startBuf != '\t')
1128f4a2713aSLionel Sambuc       curColumn++;
1129f4a2713aSLionel Sambuc 
1130f4a2713aSLionel Sambuc     Loc = clang_getCursorLocation(Cursor);
1131f4a2713aSLionel Sambuc     clang_getSpellingLocation(Loc, &file, 0, 0, 0);
1132f4a2713aSLionel Sambuc 
1133f4a2713aSLionel Sambuc     source = clang_getFileName(file);
1134f4a2713aSLionel Sambuc     if (clang_getCString(source)) {
1135f4a2713aSLionel Sambuc       CXSourceLocation RefLoc
1136f4a2713aSLionel Sambuc         = clang_getLocation(Data->TU, file, curLine, curColumn);
1137f4a2713aSLionel Sambuc       Ref = clang_getCursor(Data->TU, RefLoc);
1138f4a2713aSLionel Sambuc       if (Ref.kind == CXCursor_NoDeclFound) {
1139f4a2713aSLionel Sambuc         /* Nothing found here; that's fine. */
1140f4a2713aSLionel Sambuc       } else if (Ref.kind != CXCursor_FunctionDecl) {
1141f4a2713aSLionel Sambuc         printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
1142f4a2713aSLionel Sambuc                curLine, curColumn);
1143*0a6a1f1dSLionel Sambuc         PrintCursor(Ref, Data->CommentSchemaFile);
1144f4a2713aSLionel Sambuc         printf("\n");
1145f4a2713aSLionel Sambuc       }
1146f4a2713aSLionel Sambuc     }
1147f4a2713aSLionel Sambuc     clang_disposeString(source);
1148f4a2713aSLionel Sambuc     startBuf++;
1149f4a2713aSLionel Sambuc   }
1150f4a2713aSLionel Sambuc 
1151f4a2713aSLionel Sambuc   return CXChildVisit_Continue;
1152f4a2713aSLionel Sambuc }
1153f4a2713aSLionel Sambuc 
1154f4a2713aSLionel Sambuc /******************************************************************************/
1155f4a2713aSLionel Sambuc /* USR testing.                                                               */
1156f4a2713aSLionel Sambuc /******************************************************************************/
1157f4a2713aSLionel Sambuc 
USRVisitor(CXCursor C,CXCursor parent,CXClientData ClientData)1158f4a2713aSLionel Sambuc enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
1159f4a2713aSLionel Sambuc                                    CXClientData ClientData) {
1160f4a2713aSLionel Sambuc   VisitorData *Data = (VisitorData *)ClientData;
1161f4a2713aSLionel Sambuc   if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
1162f4a2713aSLionel Sambuc     CXString USR = clang_getCursorUSR(C);
1163f4a2713aSLionel Sambuc     const char *cstr = clang_getCString(USR);
1164f4a2713aSLionel Sambuc     if (!cstr || cstr[0] == '\0') {
1165f4a2713aSLionel Sambuc       clang_disposeString(USR);
1166f4a2713aSLionel Sambuc       return CXChildVisit_Recurse;
1167f4a2713aSLionel Sambuc     }
1168f4a2713aSLionel Sambuc     printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
1169f4a2713aSLionel Sambuc 
1170f4a2713aSLionel Sambuc     PrintCursorExtent(C);
1171f4a2713aSLionel Sambuc     printf("\n");
1172f4a2713aSLionel Sambuc     clang_disposeString(USR);
1173f4a2713aSLionel Sambuc 
1174f4a2713aSLionel Sambuc     return CXChildVisit_Recurse;
1175f4a2713aSLionel Sambuc   }
1176f4a2713aSLionel Sambuc 
1177f4a2713aSLionel Sambuc   return CXChildVisit_Continue;
1178f4a2713aSLionel Sambuc }
1179f4a2713aSLionel Sambuc 
1180f4a2713aSLionel Sambuc /******************************************************************************/
1181f4a2713aSLionel Sambuc /* Inclusion stack testing.                                                   */
1182f4a2713aSLionel Sambuc /******************************************************************************/
1183f4a2713aSLionel Sambuc 
InclusionVisitor(CXFile includedFile,CXSourceLocation * includeStack,unsigned includeStackLen,CXClientData data)1184f4a2713aSLionel Sambuc void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
1185f4a2713aSLionel Sambuc                       unsigned includeStackLen, CXClientData data) {
1186f4a2713aSLionel Sambuc 
1187f4a2713aSLionel Sambuc   unsigned i;
1188f4a2713aSLionel Sambuc   CXString fname;
1189f4a2713aSLionel Sambuc 
1190f4a2713aSLionel Sambuc   fname = clang_getFileName(includedFile);
1191f4a2713aSLionel Sambuc   printf("file: %s\nincluded by:\n", clang_getCString(fname));
1192f4a2713aSLionel Sambuc   clang_disposeString(fname);
1193f4a2713aSLionel Sambuc 
1194f4a2713aSLionel Sambuc   for (i = 0; i < includeStackLen; ++i) {
1195f4a2713aSLionel Sambuc     CXFile includingFile;
1196f4a2713aSLionel Sambuc     unsigned line, column;
1197f4a2713aSLionel Sambuc     clang_getSpellingLocation(includeStack[i], &includingFile, &line,
1198f4a2713aSLionel Sambuc                               &column, 0);
1199f4a2713aSLionel Sambuc     fname = clang_getFileName(includingFile);
1200f4a2713aSLionel Sambuc     printf("  %s:%d:%d\n", clang_getCString(fname), line, column);
1201f4a2713aSLionel Sambuc     clang_disposeString(fname);
1202f4a2713aSLionel Sambuc   }
1203f4a2713aSLionel Sambuc   printf("\n");
1204f4a2713aSLionel Sambuc }
1205f4a2713aSLionel Sambuc 
PrintInclusionStack(CXTranslationUnit TU)1206f4a2713aSLionel Sambuc void PrintInclusionStack(CXTranslationUnit TU) {
1207f4a2713aSLionel Sambuc   clang_getInclusions(TU, InclusionVisitor, NULL);
1208f4a2713aSLionel Sambuc }
1209f4a2713aSLionel Sambuc 
1210f4a2713aSLionel Sambuc /******************************************************************************/
1211f4a2713aSLionel Sambuc /* Linkage testing.                                                           */
1212f4a2713aSLionel Sambuc /******************************************************************************/
1213f4a2713aSLionel Sambuc 
PrintLinkage(CXCursor cursor,CXCursor p,CXClientData d)1214f4a2713aSLionel Sambuc static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
1215f4a2713aSLionel Sambuc                                             CXClientData d) {
1216f4a2713aSLionel Sambuc   const char *linkage = 0;
1217f4a2713aSLionel Sambuc 
1218f4a2713aSLionel Sambuc   if (clang_isInvalid(clang_getCursorKind(cursor)))
1219f4a2713aSLionel Sambuc     return CXChildVisit_Recurse;
1220f4a2713aSLionel Sambuc 
1221f4a2713aSLionel Sambuc   switch (clang_getCursorLinkage(cursor)) {
1222f4a2713aSLionel Sambuc     case CXLinkage_Invalid: break;
1223f4a2713aSLionel Sambuc     case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
1224f4a2713aSLionel Sambuc     case CXLinkage_Internal: linkage = "Internal"; break;
1225f4a2713aSLionel Sambuc     case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
1226f4a2713aSLionel Sambuc     case CXLinkage_External: linkage = "External"; break;
1227f4a2713aSLionel Sambuc   }
1228f4a2713aSLionel Sambuc 
1229f4a2713aSLionel Sambuc   if (linkage) {
1230f4a2713aSLionel Sambuc     PrintCursor(cursor, NULL);
1231f4a2713aSLionel Sambuc     printf("linkage=%s\n", linkage);
1232f4a2713aSLionel Sambuc   }
1233f4a2713aSLionel Sambuc 
1234f4a2713aSLionel Sambuc   return CXChildVisit_Recurse;
1235f4a2713aSLionel Sambuc }
1236f4a2713aSLionel Sambuc 
1237f4a2713aSLionel Sambuc /******************************************************************************/
1238f4a2713aSLionel Sambuc /* Typekind testing.                                                          */
1239f4a2713aSLionel Sambuc /******************************************************************************/
1240f4a2713aSLionel Sambuc 
PrintTypeAndTypeKind(CXType T,const char * Format)1241f4a2713aSLionel Sambuc static void PrintTypeAndTypeKind(CXType T, const char *Format) {
1242f4a2713aSLionel Sambuc   CXString TypeSpelling, TypeKindSpelling;
1243f4a2713aSLionel Sambuc 
1244f4a2713aSLionel Sambuc   TypeSpelling = clang_getTypeSpelling(T);
1245f4a2713aSLionel Sambuc   TypeKindSpelling = clang_getTypeKindSpelling(T.kind);
1246f4a2713aSLionel Sambuc   printf(Format,
1247f4a2713aSLionel Sambuc          clang_getCString(TypeSpelling),
1248f4a2713aSLionel Sambuc          clang_getCString(TypeKindSpelling));
1249f4a2713aSLionel Sambuc   clang_disposeString(TypeSpelling);
1250f4a2713aSLionel Sambuc   clang_disposeString(TypeKindSpelling);
1251f4a2713aSLionel Sambuc }
1252f4a2713aSLionel Sambuc 
PrintType(CXCursor cursor,CXCursor p,CXClientData d)1253f4a2713aSLionel Sambuc static enum CXChildVisitResult PrintType(CXCursor cursor, CXCursor p,
1254f4a2713aSLionel Sambuc                                          CXClientData d) {
1255f4a2713aSLionel Sambuc   if (!clang_isInvalid(clang_getCursorKind(cursor))) {
1256f4a2713aSLionel Sambuc     CXType T = clang_getCursorType(cursor);
1257f4a2713aSLionel Sambuc     enum CXRefQualifierKind RQ = clang_Type_getCXXRefQualifier(T);
1258f4a2713aSLionel Sambuc     PrintCursor(cursor, NULL);
1259f4a2713aSLionel Sambuc     PrintTypeAndTypeKind(T, " [type=%s] [typekind=%s]");
1260f4a2713aSLionel Sambuc     if (clang_isConstQualifiedType(T))
1261f4a2713aSLionel Sambuc       printf(" const");
1262f4a2713aSLionel Sambuc     if (clang_isVolatileQualifiedType(T))
1263f4a2713aSLionel Sambuc       printf(" volatile");
1264f4a2713aSLionel Sambuc     if (clang_isRestrictQualifiedType(T))
1265f4a2713aSLionel Sambuc       printf(" restrict");
1266f4a2713aSLionel Sambuc     if (RQ == CXRefQualifier_LValue)
1267f4a2713aSLionel Sambuc       printf(" lvalue-ref-qualifier");
1268f4a2713aSLionel Sambuc     if (RQ == CXRefQualifier_RValue)
1269f4a2713aSLionel Sambuc       printf(" rvalue-ref-qualifier");
1270f4a2713aSLionel Sambuc     /* Print the canonical type if it is different. */
1271f4a2713aSLionel Sambuc     {
1272f4a2713aSLionel Sambuc       CXType CT = clang_getCanonicalType(T);
1273f4a2713aSLionel Sambuc       if (!clang_equalTypes(T, CT)) {
1274f4a2713aSLionel Sambuc         PrintTypeAndTypeKind(CT, " [canonicaltype=%s] [canonicaltypekind=%s]");
1275f4a2713aSLionel Sambuc       }
1276f4a2713aSLionel Sambuc     }
1277f4a2713aSLionel Sambuc     /* Print the return type if it exists. */
1278f4a2713aSLionel Sambuc     {
1279f4a2713aSLionel Sambuc       CXType RT = clang_getCursorResultType(cursor);
1280f4a2713aSLionel Sambuc       if (RT.kind != CXType_Invalid) {
1281f4a2713aSLionel Sambuc         PrintTypeAndTypeKind(RT, " [resulttype=%s] [resulttypekind=%s]");
1282f4a2713aSLionel Sambuc       }
1283f4a2713aSLionel Sambuc     }
1284f4a2713aSLionel Sambuc     /* Print the argument types if they exist. */
1285f4a2713aSLionel Sambuc     {
1286*0a6a1f1dSLionel Sambuc       int NumArgs = clang_Cursor_getNumArguments(cursor);
1287*0a6a1f1dSLionel Sambuc       if (NumArgs != -1 && NumArgs != 0) {
1288f4a2713aSLionel Sambuc         int i;
1289f4a2713aSLionel Sambuc         printf(" [args=");
1290*0a6a1f1dSLionel Sambuc         for (i = 0; i < NumArgs; ++i) {
1291f4a2713aSLionel Sambuc           CXType T = clang_getCursorType(clang_Cursor_getArgument(cursor, i));
1292f4a2713aSLionel Sambuc           if (T.kind != CXType_Invalid) {
1293f4a2713aSLionel Sambuc             PrintTypeAndTypeKind(T, " [%s] [%s]");
1294f4a2713aSLionel Sambuc           }
1295f4a2713aSLionel Sambuc         }
1296f4a2713aSLionel Sambuc         printf("]");
1297f4a2713aSLionel Sambuc       }
1298f4a2713aSLionel Sambuc     }
1299*0a6a1f1dSLionel Sambuc     /* Print the template argument types if they exist. */
1300*0a6a1f1dSLionel Sambuc     {
1301*0a6a1f1dSLionel Sambuc       int NumTArgs = clang_Type_getNumTemplateArguments(T);
1302*0a6a1f1dSLionel Sambuc       if (NumTArgs != -1 && NumTArgs != 0) {
1303*0a6a1f1dSLionel Sambuc         int i;
1304*0a6a1f1dSLionel Sambuc         printf(" [templateargs/%d=", NumTArgs);
1305*0a6a1f1dSLionel Sambuc         for (i = 0; i < NumTArgs; ++i) {
1306*0a6a1f1dSLionel Sambuc           CXType TArg = clang_Type_getTemplateArgumentAsType(T, i);
1307*0a6a1f1dSLionel Sambuc           if (TArg.kind != CXType_Invalid) {
1308*0a6a1f1dSLionel Sambuc             PrintTypeAndTypeKind(TArg, " [type=%s] [typekind=%s]");
1309*0a6a1f1dSLionel Sambuc           }
1310*0a6a1f1dSLionel Sambuc         }
1311*0a6a1f1dSLionel Sambuc         printf("]");
1312*0a6a1f1dSLionel Sambuc       }
1313*0a6a1f1dSLionel Sambuc     }
1314f4a2713aSLionel Sambuc     /* Print if this is a non-POD type. */
1315f4a2713aSLionel Sambuc     printf(" [isPOD=%d]", clang_isPODType(T));
1316*0a6a1f1dSLionel Sambuc     /* Print the pointee type. */
1317*0a6a1f1dSLionel Sambuc     {
1318*0a6a1f1dSLionel Sambuc       CXType PT = clang_getPointeeType(T);
1319*0a6a1f1dSLionel Sambuc       if (PT.kind != CXType_Invalid) {
1320*0a6a1f1dSLionel Sambuc         PrintTypeAndTypeKind(PT, " [pointeetype=%s] [pointeekind=%s]");
1321*0a6a1f1dSLionel Sambuc       }
1322*0a6a1f1dSLionel Sambuc     }
1323f4a2713aSLionel Sambuc 
1324f4a2713aSLionel Sambuc     printf("\n");
1325f4a2713aSLionel Sambuc   }
1326f4a2713aSLionel Sambuc   return CXChildVisit_Recurse;
1327f4a2713aSLionel Sambuc }
1328f4a2713aSLionel Sambuc 
PrintTypeSize(CXCursor cursor,CXCursor p,CXClientData d)1329f4a2713aSLionel Sambuc static enum CXChildVisitResult PrintTypeSize(CXCursor cursor, CXCursor p,
1330f4a2713aSLionel Sambuc                                              CXClientData d) {
1331f4a2713aSLionel Sambuc   CXType T;
1332f4a2713aSLionel Sambuc   enum CXCursorKind K = clang_getCursorKind(cursor);
1333f4a2713aSLionel Sambuc   if (clang_isInvalid(K))
1334f4a2713aSLionel Sambuc     return CXChildVisit_Recurse;
1335f4a2713aSLionel Sambuc   T = clang_getCursorType(cursor);
1336f4a2713aSLionel Sambuc   PrintCursor(cursor, NULL);
1337f4a2713aSLionel Sambuc   PrintTypeAndTypeKind(T, " [type=%s] [typekind=%s]");
1338f4a2713aSLionel Sambuc   /* Print the type sizeof if applicable. */
1339f4a2713aSLionel Sambuc   {
1340f4a2713aSLionel Sambuc     long long Size = clang_Type_getSizeOf(T);
1341f4a2713aSLionel Sambuc     if (Size >= 0 || Size < -1 ) {
1342f4a2713aSLionel Sambuc       printf(" [sizeof=%lld]", Size);
1343f4a2713aSLionel Sambuc     }
1344f4a2713aSLionel Sambuc   }
1345f4a2713aSLionel Sambuc   /* Print the type alignof if applicable. */
1346f4a2713aSLionel Sambuc   {
1347f4a2713aSLionel Sambuc     long long Align = clang_Type_getAlignOf(T);
1348f4a2713aSLionel Sambuc     if (Align >= 0 || Align < -1) {
1349f4a2713aSLionel Sambuc       printf(" [alignof=%lld]", Align);
1350f4a2713aSLionel Sambuc     }
1351f4a2713aSLionel Sambuc   }
1352f4a2713aSLionel Sambuc   /* Print the record field offset if applicable. */
1353f4a2713aSLionel Sambuc   {
1354*0a6a1f1dSLionel Sambuc     CXString FieldSpelling = clang_getCursorSpelling(cursor);
1355*0a6a1f1dSLionel Sambuc     const char *FieldName = clang_getCString(FieldSpelling);
1356f4a2713aSLionel Sambuc     /* recurse to get the root anonymous record parent */
1357f4a2713aSLionel Sambuc     CXCursor Parent, Root;
1358f4a2713aSLionel Sambuc     if (clang_getCursorKind(cursor) == CXCursor_FieldDecl) {
1359*0a6a1f1dSLionel Sambuc       CXString RootParentSpelling;
1360*0a6a1f1dSLionel Sambuc       const char *RootParentName = 0;
1361*0a6a1f1dSLionel Sambuc       Parent = p;
1362f4a2713aSLionel Sambuc       do {
1363*0a6a1f1dSLionel Sambuc         if (RootParentName != 0)
1364*0a6a1f1dSLionel Sambuc           clang_disposeString(RootParentSpelling);
1365*0a6a1f1dSLionel Sambuc 
1366f4a2713aSLionel Sambuc         Root = Parent;
1367*0a6a1f1dSLionel Sambuc         RootParentSpelling = clang_getCursorSpelling(Root);
1368*0a6a1f1dSLionel Sambuc         RootParentName = clang_getCString(RootParentSpelling);
1369f4a2713aSLionel Sambuc         Parent = clang_getCursorSemanticParent(Root);
1370f4a2713aSLionel Sambuc       } while (clang_getCursorType(Parent).kind == CXType_Record &&
1371f4a2713aSLionel Sambuc                !strcmp(RootParentName, ""));
1372*0a6a1f1dSLionel Sambuc       clang_disposeString(RootParentSpelling);
1373f4a2713aSLionel Sambuc       /* if RootParentName is "", record is anonymous. */
1374f4a2713aSLionel Sambuc       {
1375f4a2713aSLionel Sambuc         long long Offset = clang_Type_getOffsetOf(clang_getCursorType(Root),
1376f4a2713aSLionel Sambuc                                                   FieldName);
1377f4a2713aSLionel Sambuc         printf(" [offsetof=%lld]", Offset);
1378f4a2713aSLionel Sambuc       }
1379f4a2713aSLionel Sambuc     }
1380*0a6a1f1dSLionel Sambuc     clang_disposeString(FieldSpelling);
1381f4a2713aSLionel Sambuc   }
1382f4a2713aSLionel Sambuc   /* Print if its a bitfield */
1383f4a2713aSLionel Sambuc   {
1384f4a2713aSLionel Sambuc     int IsBitfield = clang_Cursor_isBitField(cursor);
1385f4a2713aSLionel Sambuc     if (IsBitfield)
1386f4a2713aSLionel Sambuc       printf(" [BitFieldSize=%d]", clang_getFieldDeclBitWidth(cursor));
1387f4a2713aSLionel Sambuc   }
1388f4a2713aSLionel Sambuc   printf("\n");
1389f4a2713aSLionel Sambuc   return CXChildVisit_Recurse;
1390f4a2713aSLionel Sambuc }
1391f4a2713aSLionel Sambuc 
1392f4a2713aSLionel Sambuc /******************************************************************************/
1393*0a6a1f1dSLionel Sambuc /* Mangling testing.                                                          */
1394*0a6a1f1dSLionel Sambuc /******************************************************************************/
1395*0a6a1f1dSLionel Sambuc 
PrintMangledName(CXCursor cursor,CXCursor p,CXClientData d)1396*0a6a1f1dSLionel Sambuc static enum CXChildVisitResult PrintMangledName(CXCursor cursor, CXCursor p,
1397*0a6a1f1dSLionel Sambuc                                                 CXClientData d) {
1398*0a6a1f1dSLionel Sambuc   CXString MangledName;
1399*0a6a1f1dSLionel Sambuc   PrintCursor(cursor, NULL);
1400*0a6a1f1dSLionel Sambuc   MangledName = clang_Cursor_getMangling(cursor);
1401*0a6a1f1dSLionel Sambuc   printf(" [mangled=%s]\n", clang_getCString(MangledName));
1402*0a6a1f1dSLionel Sambuc   clang_disposeString(MangledName);
1403*0a6a1f1dSLionel Sambuc   return CXChildVisit_Continue;
1404*0a6a1f1dSLionel Sambuc }
1405*0a6a1f1dSLionel Sambuc 
1406*0a6a1f1dSLionel Sambuc /******************************************************************************/
1407f4a2713aSLionel Sambuc /* Bitwidth testing.                                                          */
1408f4a2713aSLionel Sambuc /******************************************************************************/
1409f4a2713aSLionel Sambuc 
PrintBitWidth(CXCursor cursor,CXCursor p,CXClientData d)1410f4a2713aSLionel Sambuc static enum CXChildVisitResult PrintBitWidth(CXCursor cursor, CXCursor p,
1411f4a2713aSLionel Sambuc                                              CXClientData d) {
1412f4a2713aSLionel Sambuc   int Bitwidth;
1413f4a2713aSLionel Sambuc   if (clang_getCursorKind(cursor) != CXCursor_FieldDecl)
1414f4a2713aSLionel Sambuc     return CXChildVisit_Recurse;
1415f4a2713aSLionel Sambuc 
1416f4a2713aSLionel Sambuc   Bitwidth = clang_getFieldDeclBitWidth(cursor);
1417f4a2713aSLionel Sambuc   if (Bitwidth >= 0) {
1418f4a2713aSLionel Sambuc     PrintCursor(cursor, NULL);
1419f4a2713aSLionel Sambuc     printf(" bitwidth=%d\n", Bitwidth);
1420f4a2713aSLionel Sambuc   }
1421f4a2713aSLionel Sambuc 
1422f4a2713aSLionel Sambuc   return CXChildVisit_Recurse;
1423f4a2713aSLionel Sambuc }
1424f4a2713aSLionel Sambuc 
1425f4a2713aSLionel Sambuc /******************************************************************************/
1426f4a2713aSLionel Sambuc /* Loading ASTs/source.                                                       */
1427f4a2713aSLionel Sambuc /******************************************************************************/
1428f4a2713aSLionel Sambuc 
perform_test_load(CXIndex Idx,CXTranslationUnit TU,const char * filter,const char * prefix,CXCursorVisitor Visitor,PostVisitTU PV,const char * CommentSchemaFile)1429f4a2713aSLionel Sambuc static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
1430f4a2713aSLionel Sambuc                              const char *filter, const char *prefix,
1431f4a2713aSLionel Sambuc                              CXCursorVisitor Visitor,
1432f4a2713aSLionel Sambuc                              PostVisitTU PV,
1433f4a2713aSLionel Sambuc                              const char *CommentSchemaFile) {
1434f4a2713aSLionel Sambuc 
1435f4a2713aSLionel Sambuc   if (prefix)
1436f4a2713aSLionel Sambuc     FileCheckPrefix = prefix;
1437f4a2713aSLionel Sambuc 
1438f4a2713aSLionel Sambuc   if (Visitor) {
1439f4a2713aSLionel Sambuc     enum CXCursorKind K = CXCursor_NotImplemented;
1440f4a2713aSLionel Sambuc     enum CXCursorKind *ck = &K;
1441f4a2713aSLionel Sambuc     VisitorData Data;
1442f4a2713aSLionel Sambuc 
1443f4a2713aSLionel Sambuc     /* Perform some simple filtering. */
1444f4a2713aSLionel Sambuc     if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
1445f4a2713aSLionel Sambuc     else if (!strcmp(filter, "all-display") ||
1446f4a2713aSLionel Sambuc              !strcmp(filter, "local-display")) {
1447f4a2713aSLionel Sambuc       ck = NULL;
1448f4a2713aSLionel Sambuc       want_display_name = 1;
1449f4a2713aSLionel Sambuc     }
1450f4a2713aSLionel Sambuc     else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
1451f4a2713aSLionel Sambuc     else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
1452f4a2713aSLionel Sambuc     else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
1453f4a2713aSLionel Sambuc     else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
1454f4a2713aSLionel Sambuc     else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
1455f4a2713aSLionel Sambuc     else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
1456f4a2713aSLionel Sambuc     else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
1457f4a2713aSLionel Sambuc     else {
1458f4a2713aSLionel Sambuc       fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
1459f4a2713aSLionel Sambuc       return 1;
1460f4a2713aSLionel Sambuc     }
1461f4a2713aSLionel Sambuc 
1462f4a2713aSLionel Sambuc     Data.TU = TU;
1463f4a2713aSLionel Sambuc     Data.Filter = ck;
1464*0a6a1f1dSLionel Sambuc     Data.CommentSchemaFile = CommentSchemaFile;
1465f4a2713aSLionel Sambuc     clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
1466f4a2713aSLionel Sambuc   }
1467f4a2713aSLionel Sambuc 
1468f4a2713aSLionel Sambuc   if (PV)
1469f4a2713aSLionel Sambuc     PV(TU);
1470f4a2713aSLionel Sambuc 
1471f4a2713aSLionel Sambuc   PrintDiagnostics(TU);
1472f4a2713aSLionel Sambuc   if (checkForErrors(TU) != 0) {
1473f4a2713aSLionel Sambuc     clang_disposeTranslationUnit(TU);
1474f4a2713aSLionel Sambuc     return -1;
1475f4a2713aSLionel Sambuc   }
1476f4a2713aSLionel Sambuc 
1477f4a2713aSLionel Sambuc   clang_disposeTranslationUnit(TU);
1478f4a2713aSLionel Sambuc   return 0;
1479f4a2713aSLionel Sambuc }
1480f4a2713aSLionel Sambuc 
perform_test_load_tu(const char * file,const char * filter,const char * prefix,CXCursorVisitor Visitor,PostVisitTU PV)1481f4a2713aSLionel Sambuc int perform_test_load_tu(const char *file, const char *filter,
1482f4a2713aSLionel Sambuc                          const char *prefix, CXCursorVisitor Visitor,
1483f4a2713aSLionel Sambuc                          PostVisitTU PV) {
1484f4a2713aSLionel Sambuc   CXIndex Idx;
1485f4a2713aSLionel Sambuc   CXTranslationUnit TU;
1486f4a2713aSLionel Sambuc   int result;
1487f4a2713aSLionel Sambuc   Idx = clang_createIndex(/* excludeDeclsFromPCH */
1488f4a2713aSLionel Sambuc                           !strcmp(filter, "local") ? 1 : 0,
1489f4a2713aSLionel Sambuc                           /* displayDiagnostics=*/1);
1490f4a2713aSLionel Sambuc 
1491f4a2713aSLionel Sambuc   if (!CreateTranslationUnit(Idx, file, &TU)) {
1492f4a2713aSLionel Sambuc     clang_disposeIndex(Idx);
1493f4a2713aSLionel Sambuc     return 1;
1494f4a2713aSLionel Sambuc   }
1495f4a2713aSLionel Sambuc 
1496f4a2713aSLionel Sambuc   result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV, NULL);
1497f4a2713aSLionel Sambuc   clang_disposeIndex(Idx);
1498f4a2713aSLionel Sambuc   return result;
1499f4a2713aSLionel Sambuc }
1500f4a2713aSLionel Sambuc 
perform_test_load_source(int argc,const char ** argv,const char * filter,CXCursorVisitor Visitor,PostVisitTU PV)1501f4a2713aSLionel Sambuc int perform_test_load_source(int argc, const char **argv,
1502f4a2713aSLionel Sambuc                              const char *filter, CXCursorVisitor Visitor,
1503f4a2713aSLionel Sambuc                              PostVisitTU PV) {
1504f4a2713aSLionel Sambuc   CXIndex Idx;
1505f4a2713aSLionel Sambuc   CXTranslationUnit TU;
1506f4a2713aSLionel Sambuc   const char *CommentSchemaFile;
1507f4a2713aSLionel Sambuc   struct CXUnsavedFile *unsaved_files = 0;
1508f4a2713aSLionel Sambuc   int num_unsaved_files = 0;
1509*0a6a1f1dSLionel Sambuc   enum CXErrorCode Err;
1510f4a2713aSLionel Sambuc   int result;
1511f4a2713aSLionel Sambuc 
1512f4a2713aSLionel Sambuc   Idx = clang_createIndex(/* excludeDeclsFromPCH */
1513f4a2713aSLionel Sambuc                           (!strcmp(filter, "local") ||
1514f4a2713aSLionel Sambuc                            !strcmp(filter, "local-display"))? 1 : 0,
1515f4a2713aSLionel Sambuc                           /* displayDiagnostics=*/1);
1516f4a2713aSLionel Sambuc 
1517f4a2713aSLionel Sambuc   if ((CommentSchemaFile = parse_comments_schema(argc, argv))) {
1518f4a2713aSLionel Sambuc     argc--;
1519f4a2713aSLionel Sambuc     argv++;
1520f4a2713aSLionel Sambuc   }
1521f4a2713aSLionel Sambuc 
1522f4a2713aSLionel Sambuc   if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1523f4a2713aSLionel Sambuc     clang_disposeIndex(Idx);
1524f4a2713aSLionel Sambuc     return -1;
1525f4a2713aSLionel Sambuc   }
1526f4a2713aSLionel Sambuc 
1527*0a6a1f1dSLionel Sambuc   Err = clang_parseTranslationUnit2(Idx, 0,
1528f4a2713aSLionel Sambuc                                     argv + num_unsaved_files,
1529f4a2713aSLionel Sambuc                                     argc - num_unsaved_files,
1530f4a2713aSLionel Sambuc                                     unsaved_files, num_unsaved_files,
1531*0a6a1f1dSLionel Sambuc                                     getDefaultParsingOptions(), &TU);
1532*0a6a1f1dSLionel Sambuc   if (Err != CXError_Success) {
1533f4a2713aSLionel Sambuc     fprintf(stderr, "Unable to load translation unit!\n");
1534*0a6a1f1dSLionel Sambuc     describeLibclangFailure(Err);
1535f4a2713aSLionel Sambuc     free_remapped_files(unsaved_files, num_unsaved_files);
1536f4a2713aSLionel Sambuc     clang_disposeIndex(Idx);
1537f4a2713aSLionel Sambuc     return 1;
1538f4a2713aSLionel Sambuc   }
1539f4a2713aSLionel Sambuc 
1540f4a2713aSLionel Sambuc   result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV,
1541f4a2713aSLionel Sambuc                              CommentSchemaFile);
1542f4a2713aSLionel Sambuc   free_remapped_files(unsaved_files, num_unsaved_files);
1543f4a2713aSLionel Sambuc   clang_disposeIndex(Idx);
1544f4a2713aSLionel Sambuc   return result;
1545f4a2713aSLionel Sambuc }
1546f4a2713aSLionel Sambuc 
perform_test_reparse_source(int argc,const char ** argv,int trials,const char * filter,CXCursorVisitor Visitor,PostVisitTU PV)1547f4a2713aSLionel Sambuc int perform_test_reparse_source(int argc, const char **argv, int trials,
1548f4a2713aSLionel Sambuc                                 const char *filter, CXCursorVisitor Visitor,
1549f4a2713aSLionel Sambuc                                 PostVisitTU PV) {
1550f4a2713aSLionel Sambuc   CXIndex Idx;
1551f4a2713aSLionel Sambuc   CXTranslationUnit TU;
1552f4a2713aSLionel Sambuc   struct CXUnsavedFile *unsaved_files = 0;
1553f4a2713aSLionel Sambuc   int num_unsaved_files = 0;
1554*0a6a1f1dSLionel Sambuc   int compiler_arg_idx = 0;
1555*0a6a1f1dSLionel Sambuc   enum CXErrorCode Err;
1556*0a6a1f1dSLionel Sambuc   int result, i;
1557f4a2713aSLionel Sambuc   int trial;
1558f4a2713aSLionel Sambuc   int remap_after_trial = 0;
1559f4a2713aSLionel Sambuc   char *endptr = 0;
1560f4a2713aSLionel Sambuc 
1561f4a2713aSLionel Sambuc   Idx = clang_createIndex(/* excludeDeclsFromPCH */
1562f4a2713aSLionel Sambuc                           !strcmp(filter, "local") ? 1 : 0,
1563f4a2713aSLionel Sambuc                           /* displayDiagnostics=*/1);
1564f4a2713aSLionel Sambuc 
1565f4a2713aSLionel Sambuc   if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1566f4a2713aSLionel Sambuc     clang_disposeIndex(Idx);
1567f4a2713aSLionel Sambuc     return -1;
1568f4a2713aSLionel Sambuc   }
1569f4a2713aSLionel Sambuc 
1570*0a6a1f1dSLionel Sambuc   for (i = 0; i < argc; ++i) {
1571*0a6a1f1dSLionel Sambuc     if (strcmp(argv[i], "--") == 0)
1572*0a6a1f1dSLionel Sambuc       break;
1573*0a6a1f1dSLionel Sambuc   }
1574*0a6a1f1dSLionel Sambuc   if (i < argc)
1575*0a6a1f1dSLionel Sambuc     compiler_arg_idx = i+1;
1576*0a6a1f1dSLionel Sambuc   if (num_unsaved_files > compiler_arg_idx)
1577*0a6a1f1dSLionel Sambuc     compiler_arg_idx = num_unsaved_files;
1578*0a6a1f1dSLionel Sambuc 
1579f4a2713aSLionel Sambuc   /* Load the initial translation unit -- we do this without honoring remapped
1580f4a2713aSLionel Sambuc    * files, so that we have a way to test results after changing the source. */
1581*0a6a1f1dSLionel Sambuc   Err = clang_parseTranslationUnit2(Idx, 0,
1582*0a6a1f1dSLionel Sambuc                                     argv + compiler_arg_idx,
1583*0a6a1f1dSLionel Sambuc                                     argc - compiler_arg_idx,
1584*0a6a1f1dSLionel Sambuc                                     0, 0, getDefaultParsingOptions(), &TU);
1585*0a6a1f1dSLionel Sambuc   if (Err != CXError_Success) {
1586f4a2713aSLionel Sambuc     fprintf(stderr, "Unable to load translation unit!\n");
1587*0a6a1f1dSLionel Sambuc     describeLibclangFailure(Err);
1588f4a2713aSLionel Sambuc     free_remapped_files(unsaved_files, num_unsaved_files);
1589f4a2713aSLionel Sambuc     clang_disposeIndex(Idx);
1590f4a2713aSLionel Sambuc     return 1;
1591f4a2713aSLionel Sambuc   }
1592f4a2713aSLionel Sambuc 
1593f4a2713aSLionel Sambuc   if (checkForErrors(TU) != 0)
1594f4a2713aSLionel Sambuc     return -1;
1595f4a2713aSLionel Sambuc 
1596f4a2713aSLionel Sambuc   if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) {
1597f4a2713aSLionel Sambuc     remap_after_trial =
1598f4a2713aSLionel Sambuc         strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10);
1599f4a2713aSLionel Sambuc   }
1600f4a2713aSLionel Sambuc 
1601f4a2713aSLionel Sambuc   for (trial = 0; trial < trials; ++trial) {
1602*0a6a1f1dSLionel Sambuc     free_remapped_files(unsaved_files, num_unsaved_files);
1603*0a6a1f1dSLionel Sambuc     if (parse_remapped_files_with_try(trial, argc, argv, 0,
1604*0a6a1f1dSLionel Sambuc                                       &unsaved_files, &num_unsaved_files)) {
1605*0a6a1f1dSLionel Sambuc       clang_disposeTranslationUnit(TU);
1606*0a6a1f1dSLionel Sambuc       clang_disposeIndex(Idx);
1607*0a6a1f1dSLionel Sambuc       return -1;
1608*0a6a1f1dSLionel Sambuc     }
1609*0a6a1f1dSLionel Sambuc 
1610*0a6a1f1dSLionel Sambuc     Err = clang_reparseTranslationUnit(
1611*0a6a1f1dSLionel Sambuc         TU,
1612f4a2713aSLionel Sambuc         trial >= remap_after_trial ? num_unsaved_files : 0,
1613f4a2713aSLionel Sambuc         trial >= remap_after_trial ? unsaved_files : 0,
1614*0a6a1f1dSLionel Sambuc         clang_defaultReparseOptions(TU));
1615*0a6a1f1dSLionel Sambuc     if (Err != CXError_Success) {
1616f4a2713aSLionel Sambuc       fprintf(stderr, "Unable to reparse translation unit!\n");
1617*0a6a1f1dSLionel Sambuc       describeLibclangFailure(Err);
1618f4a2713aSLionel Sambuc       clang_disposeTranslationUnit(TU);
1619f4a2713aSLionel Sambuc       free_remapped_files(unsaved_files, num_unsaved_files);
1620f4a2713aSLionel Sambuc       clang_disposeIndex(Idx);
1621f4a2713aSLionel Sambuc       return -1;
1622f4a2713aSLionel Sambuc     }
1623f4a2713aSLionel Sambuc 
1624f4a2713aSLionel Sambuc     if (checkForErrors(TU) != 0)
1625f4a2713aSLionel Sambuc       return -1;
1626f4a2713aSLionel Sambuc   }
1627f4a2713aSLionel Sambuc 
1628f4a2713aSLionel Sambuc   result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV, NULL);
1629f4a2713aSLionel Sambuc 
1630f4a2713aSLionel Sambuc   free_remapped_files(unsaved_files, num_unsaved_files);
1631f4a2713aSLionel Sambuc   clang_disposeIndex(Idx);
1632f4a2713aSLionel Sambuc   return result;
1633f4a2713aSLionel Sambuc }
1634f4a2713aSLionel Sambuc 
1635f4a2713aSLionel Sambuc /******************************************************************************/
1636f4a2713aSLionel Sambuc /* Logic for testing clang_getCursor().                                       */
1637f4a2713aSLionel Sambuc /******************************************************************************/
1638f4a2713aSLionel Sambuc 
print_cursor_file_scan(CXTranslationUnit TU,CXCursor cursor,unsigned start_line,unsigned start_col,unsigned end_line,unsigned end_col,const char * prefix)1639f4a2713aSLionel Sambuc static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor,
1640f4a2713aSLionel Sambuc                                    unsigned start_line, unsigned start_col,
1641f4a2713aSLionel Sambuc                                    unsigned end_line, unsigned end_col,
1642f4a2713aSLionel Sambuc                                    const char *prefix) {
1643f4a2713aSLionel Sambuc   printf("// %s: ", FileCheckPrefix);
1644f4a2713aSLionel Sambuc   if (prefix)
1645f4a2713aSLionel Sambuc     printf("-%s", prefix);
1646f4a2713aSLionel Sambuc   PrintExtent(stdout, start_line, start_col, end_line, end_col);
1647f4a2713aSLionel Sambuc   printf(" ");
1648f4a2713aSLionel Sambuc   PrintCursor(cursor, NULL);
1649f4a2713aSLionel Sambuc   printf("\n");
1650f4a2713aSLionel Sambuc }
1651f4a2713aSLionel Sambuc 
perform_file_scan(const char * ast_file,const char * source_file,const char * prefix)1652f4a2713aSLionel Sambuc static int perform_file_scan(const char *ast_file, const char *source_file,
1653f4a2713aSLionel Sambuc                              const char *prefix) {
1654f4a2713aSLionel Sambuc   CXIndex Idx;
1655f4a2713aSLionel Sambuc   CXTranslationUnit TU;
1656f4a2713aSLionel Sambuc   FILE *fp;
1657f4a2713aSLionel Sambuc   CXCursor prevCursor = clang_getNullCursor();
1658f4a2713aSLionel Sambuc   CXFile file;
1659f4a2713aSLionel Sambuc   unsigned line = 1, col = 1;
1660f4a2713aSLionel Sambuc   unsigned start_line = 1, start_col = 1;
1661f4a2713aSLionel Sambuc 
1662f4a2713aSLionel Sambuc   if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
1663f4a2713aSLionel Sambuc                                 /* displayDiagnostics=*/1))) {
1664f4a2713aSLionel Sambuc     fprintf(stderr, "Could not create Index\n");
1665f4a2713aSLionel Sambuc     return 1;
1666f4a2713aSLionel Sambuc   }
1667f4a2713aSLionel Sambuc 
1668f4a2713aSLionel Sambuc   if (!CreateTranslationUnit(Idx, ast_file, &TU))
1669f4a2713aSLionel Sambuc     return 1;
1670f4a2713aSLionel Sambuc 
1671f4a2713aSLionel Sambuc   if ((fp = fopen(source_file, "r")) == NULL) {
1672f4a2713aSLionel Sambuc     fprintf(stderr, "Could not open '%s'\n", source_file);
1673*0a6a1f1dSLionel Sambuc     clang_disposeTranslationUnit(TU);
1674f4a2713aSLionel Sambuc     return 1;
1675f4a2713aSLionel Sambuc   }
1676f4a2713aSLionel Sambuc 
1677f4a2713aSLionel Sambuc   file = clang_getFile(TU, source_file);
1678f4a2713aSLionel Sambuc   for (;;) {
1679f4a2713aSLionel Sambuc     CXCursor cursor;
1680f4a2713aSLionel Sambuc     int c = fgetc(fp);
1681f4a2713aSLionel Sambuc 
1682f4a2713aSLionel Sambuc     if (c == '\n') {
1683f4a2713aSLionel Sambuc       ++line;
1684f4a2713aSLionel Sambuc       col = 1;
1685f4a2713aSLionel Sambuc     } else
1686f4a2713aSLionel Sambuc       ++col;
1687f4a2713aSLionel Sambuc 
1688f4a2713aSLionel Sambuc     /* Check the cursor at this position, and dump the previous one if we have
1689f4a2713aSLionel Sambuc      * found something new.
1690f4a2713aSLionel Sambuc      */
1691f4a2713aSLionel Sambuc     cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
1692f4a2713aSLionel Sambuc     if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
1693f4a2713aSLionel Sambuc         prevCursor.kind != CXCursor_InvalidFile) {
1694f4a2713aSLionel Sambuc       print_cursor_file_scan(TU, prevCursor, start_line, start_col,
1695f4a2713aSLionel Sambuc                              line, col, prefix);
1696f4a2713aSLionel Sambuc       start_line = line;
1697f4a2713aSLionel Sambuc       start_col = col;
1698f4a2713aSLionel Sambuc     }
1699f4a2713aSLionel Sambuc     if (c == EOF)
1700f4a2713aSLionel Sambuc       break;
1701f4a2713aSLionel Sambuc 
1702f4a2713aSLionel Sambuc     prevCursor = cursor;
1703f4a2713aSLionel Sambuc   }
1704f4a2713aSLionel Sambuc 
1705f4a2713aSLionel Sambuc   fclose(fp);
1706f4a2713aSLionel Sambuc   clang_disposeTranslationUnit(TU);
1707f4a2713aSLionel Sambuc   clang_disposeIndex(Idx);
1708f4a2713aSLionel Sambuc   return 0;
1709f4a2713aSLionel Sambuc }
1710f4a2713aSLionel Sambuc 
1711f4a2713aSLionel Sambuc /******************************************************************************/
1712f4a2713aSLionel Sambuc /* Logic for testing clang code completion.                                   */
1713f4a2713aSLionel Sambuc /******************************************************************************/
1714f4a2713aSLionel Sambuc 
1715f4a2713aSLionel Sambuc /* Parse file:line:column from the input string. Returns 0 on success, non-zero
1716f4a2713aSLionel Sambuc    on failure. If successful, the pointer *filename will contain newly-allocated
1717f4a2713aSLionel Sambuc    memory (that will be owned by the caller) to store the file name. */
parse_file_line_column(const char * input,char ** filename,unsigned * line,unsigned * column,unsigned * second_line,unsigned * second_column)1718f4a2713aSLionel Sambuc int parse_file_line_column(const char *input, char **filename, unsigned *line,
1719f4a2713aSLionel Sambuc                            unsigned *column, unsigned *second_line,
1720f4a2713aSLionel Sambuc                            unsigned *second_column) {
1721f4a2713aSLionel Sambuc   /* Find the second colon. */
1722f4a2713aSLionel Sambuc   const char *last_colon = strrchr(input, ':');
1723f4a2713aSLionel Sambuc   unsigned values[4], i;
1724f4a2713aSLionel Sambuc   unsigned num_values = (second_line && second_column)? 4 : 2;
1725f4a2713aSLionel Sambuc 
1726f4a2713aSLionel Sambuc   char *endptr = 0;
1727f4a2713aSLionel Sambuc   if (!last_colon || last_colon == input) {
1728f4a2713aSLionel Sambuc     if (num_values == 4)
1729f4a2713aSLionel Sambuc       fprintf(stderr, "could not parse filename:line:column:line:column in "
1730f4a2713aSLionel Sambuc               "'%s'\n", input);
1731f4a2713aSLionel Sambuc     else
1732f4a2713aSLionel Sambuc       fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
1733f4a2713aSLionel Sambuc     return 1;
1734f4a2713aSLionel Sambuc   }
1735f4a2713aSLionel Sambuc 
1736f4a2713aSLionel Sambuc   for (i = 0; i != num_values; ++i) {
1737f4a2713aSLionel Sambuc     const char *prev_colon;
1738f4a2713aSLionel Sambuc 
1739f4a2713aSLionel Sambuc     /* Parse the next line or column. */
1740f4a2713aSLionel Sambuc     values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
1741f4a2713aSLionel Sambuc     if (*endptr != 0 && *endptr != ':') {
1742f4a2713aSLionel Sambuc       fprintf(stderr, "could not parse %s in '%s'\n",
1743f4a2713aSLionel Sambuc               (i % 2 ? "column" : "line"), input);
1744f4a2713aSLionel Sambuc       return 1;
1745f4a2713aSLionel Sambuc     }
1746f4a2713aSLionel Sambuc 
1747f4a2713aSLionel Sambuc     if (i + 1 == num_values)
1748f4a2713aSLionel Sambuc       break;
1749f4a2713aSLionel Sambuc 
1750f4a2713aSLionel Sambuc     /* Find the previous colon. */
1751f4a2713aSLionel Sambuc     prev_colon = last_colon - 1;
1752f4a2713aSLionel Sambuc     while (prev_colon != input && *prev_colon != ':')
1753f4a2713aSLionel Sambuc       --prev_colon;
1754f4a2713aSLionel Sambuc     if (prev_colon == input) {
1755f4a2713aSLionel Sambuc       fprintf(stderr, "could not parse %s in '%s'\n",
1756f4a2713aSLionel Sambuc               (i % 2 == 0? "column" : "line"), input);
1757f4a2713aSLionel Sambuc       return 1;
1758f4a2713aSLionel Sambuc     }
1759f4a2713aSLionel Sambuc 
1760f4a2713aSLionel Sambuc     last_colon = prev_colon;
1761f4a2713aSLionel Sambuc   }
1762f4a2713aSLionel Sambuc 
1763f4a2713aSLionel Sambuc   *line = values[0];
1764f4a2713aSLionel Sambuc   *column = values[1];
1765f4a2713aSLionel Sambuc 
1766f4a2713aSLionel Sambuc   if (second_line && second_column) {
1767f4a2713aSLionel Sambuc     *second_line = values[2];
1768f4a2713aSLionel Sambuc     *second_column = values[3];
1769f4a2713aSLionel Sambuc   }
1770f4a2713aSLionel Sambuc 
1771f4a2713aSLionel Sambuc   /* Copy the file name. */
1772f4a2713aSLionel Sambuc   *filename = (char*)malloc(last_colon - input + 1);
1773f4a2713aSLionel Sambuc   memcpy(*filename, input, last_colon - input);
1774f4a2713aSLionel Sambuc   (*filename)[last_colon - input] = 0;
1775f4a2713aSLionel Sambuc   return 0;
1776f4a2713aSLionel Sambuc }
1777f4a2713aSLionel Sambuc 
1778f4a2713aSLionel Sambuc const char *
clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind)1779f4a2713aSLionel Sambuc clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
1780f4a2713aSLionel Sambuc   switch (Kind) {
1781f4a2713aSLionel Sambuc   case CXCompletionChunk_Optional: return "Optional";
1782f4a2713aSLionel Sambuc   case CXCompletionChunk_TypedText: return "TypedText";
1783f4a2713aSLionel Sambuc   case CXCompletionChunk_Text: return "Text";
1784f4a2713aSLionel Sambuc   case CXCompletionChunk_Placeholder: return "Placeholder";
1785f4a2713aSLionel Sambuc   case CXCompletionChunk_Informative: return "Informative";
1786f4a2713aSLionel Sambuc   case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
1787f4a2713aSLionel Sambuc   case CXCompletionChunk_LeftParen: return "LeftParen";
1788f4a2713aSLionel Sambuc   case CXCompletionChunk_RightParen: return "RightParen";
1789f4a2713aSLionel Sambuc   case CXCompletionChunk_LeftBracket: return "LeftBracket";
1790f4a2713aSLionel Sambuc   case CXCompletionChunk_RightBracket: return "RightBracket";
1791f4a2713aSLionel Sambuc   case CXCompletionChunk_LeftBrace: return "LeftBrace";
1792f4a2713aSLionel Sambuc   case CXCompletionChunk_RightBrace: return "RightBrace";
1793f4a2713aSLionel Sambuc   case CXCompletionChunk_LeftAngle: return "LeftAngle";
1794f4a2713aSLionel Sambuc   case CXCompletionChunk_RightAngle: return "RightAngle";
1795f4a2713aSLionel Sambuc   case CXCompletionChunk_Comma: return "Comma";
1796f4a2713aSLionel Sambuc   case CXCompletionChunk_ResultType: return "ResultType";
1797f4a2713aSLionel Sambuc   case CXCompletionChunk_Colon: return "Colon";
1798f4a2713aSLionel Sambuc   case CXCompletionChunk_SemiColon: return "SemiColon";
1799f4a2713aSLionel Sambuc   case CXCompletionChunk_Equal: return "Equal";
1800f4a2713aSLionel Sambuc   case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
1801f4a2713aSLionel Sambuc   case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
1802f4a2713aSLionel Sambuc   }
1803f4a2713aSLionel Sambuc 
1804f4a2713aSLionel Sambuc   return "Unknown";
1805f4a2713aSLionel Sambuc }
1806f4a2713aSLionel Sambuc 
checkForErrors(CXTranslationUnit TU)1807f4a2713aSLionel Sambuc static int checkForErrors(CXTranslationUnit TU) {
1808f4a2713aSLionel Sambuc   unsigned Num, i;
1809f4a2713aSLionel Sambuc   CXDiagnostic Diag;
1810f4a2713aSLionel Sambuc   CXString DiagStr;
1811f4a2713aSLionel Sambuc 
1812f4a2713aSLionel Sambuc   if (!getenv("CINDEXTEST_FAILONERROR"))
1813f4a2713aSLionel Sambuc     return 0;
1814f4a2713aSLionel Sambuc 
1815f4a2713aSLionel Sambuc   Num = clang_getNumDiagnostics(TU);
1816f4a2713aSLionel Sambuc   for (i = 0; i != Num; ++i) {
1817f4a2713aSLionel Sambuc     Diag = clang_getDiagnostic(TU, i);
1818f4a2713aSLionel Sambuc     if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) {
1819f4a2713aSLionel Sambuc       DiagStr = clang_formatDiagnostic(Diag,
1820f4a2713aSLionel Sambuc                                        clang_defaultDiagnosticDisplayOptions());
1821f4a2713aSLionel Sambuc       fprintf(stderr, "%s\n", clang_getCString(DiagStr));
1822f4a2713aSLionel Sambuc       clang_disposeString(DiagStr);
1823f4a2713aSLionel Sambuc       clang_disposeDiagnostic(Diag);
1824f4a2713aSLionel Sambuc       return -1;
1825f4a2713aSLionel Sambuc     }
1826f4a2713aSLionel Sambuc     clang_disposeDiagnostic(Diag);
1827f4a2713aSLionel Sambuc   }
1828f4a2713aSLionel Sambuc 
1829f4a2713aSLionel Sambuc   return 0;
1830f4a2713aSLionel Sambuc }
1831f4a2713aSLionel Sambuc 
print_completion_string(CXCompletionString completion_string,FILE * file)1832*0a6a1f1dSLionel Sambuc static void print_completion_string(CXCompletionString completion_string,
1833*0a6a1f1dSLionel Sambuc                                     FILE *file) {
1834f4a2713aSLionel Sambuc   int I, N;
1835f4a2713aSLionel Sambuc 
1836f4a2713aSLionel Sambuc   N = clang_getNumCompletionChunks(completion_string);
1837f4a2713aSLionel Sambuc   for (I = 0; I != N; ++I) {
1838f4a2713aSLionel Sambuc     CXString text;
1839f4a2713aSLionel Sambuc     const char *cstr;
1840f4a2713aSLionel Sambuc     enum CXCompletionChunkKind Kind
1841f4a2713aSLionel Sambuc       = clang_getCompletionChunkKind(completion_string, I);
1842f4a2713aSLionel Sambuc 
1843f4a2713aSLionel Sambuc     if (Kind == CXCompletionChunk_Optional) {
1844f4a2713aSLionel Sambuc       fprintf(file, "{Optional ");
1845f4a2713aSLionel Sambuc       print_completion_string(
1846f4a2713aSLionel Sambuc                 clang_getCompletionChunkCompletionString(completion_string, I),
1847f4a2713aSLionel Sambuc                               file);
1848f4a2713aSLionel Sambuc       fprintf(file, "}");
1849f4a2713aSLionel Sambuc       continue;
1850f4a2713aSLionel Sambuc     }
1851f4a2713aSLionel Sambuc 
1852f4a2713aSLionel Sambuc     if (Kind == CXCompletionChunk_VerticalSpace) {
1853f4a2713aSLionel Sambuc       fprintf(file, "{VerticalSpace  }");
1854f4a2713aSLionel Sambuc       continue;
1855f4a2713aSLionel Sambuc     }
1856f4a2713aSLionel Sambuc 
1857f4a2713aSLionel Sambuc     text = clang_getCompletionChunkText(completion_string, I);
1858f4a2713aSLionel Sambuc     cstr = clang_getCString(text);
1859f4a2713aSLionel Sambuc     fprintf(file, "{%s %s}",
1860f4a2713aSLionel Sambuc             clang_getCompletionChunkKindSpelling(Kind),
1861f4a2713aSLionel Sambuc             cstr ? cstr : "");
1862f4a2713aSLionel Sambuc     clang_disposeString(text);
1863f4a2713aSLionel Sambuc   }
1864f4a2713aSLionel Sambuc 
1865f4a2713aSLionel Sambuc }
1866f4a2713aSLionel Sambuc 
print_completion_result(CXCompletionResult * completion_result,FILE * file)1867*0a6a1f1dSLionel Sambuc static void print_completion_result(CXCompletionResult *completion_result,
1868*0a6a1f1dSLionel Sambuc                                     FILE *file) {
1869f4a2713aSLionel Sambuc   CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
1870f4a2713aSLionel Sambuc   unsigned annotationCount;
1871f4a2713aSLionel Sambuc   enum CXCursorKind ParentKind;
1872f4a2713aSLionel Sambuc   CXString ParentName;
1873f4a2713aSLionel Sambuc   CXString BriefComment;
1874f4a2713aSLionel Sambuc   const char *BriefCommentCString;
1875f4a2713aSLionel Sambuc 
1876f4a2713aSLionel Sambuc   fprintf(file, "%s:", clang_getCString(ks));
1877f4a2713aSLionel Sambuc   clang_disposeString(ks);
1878f4a2713aSLionel Sambuc 
1879f4a2713aSLionel Sambuc   print_completion_string(completion_result->CompletionString, file);
1880f4a2713aSLionel Sambuc   fprintf(file, " (%u)",
1881f4a2713aSLionel Sambuc           clang_getCompletionPriority(completion_result->CompletionString));
1882f4a2713aSLionel Sambuc   switch (clang_getCompletionAvailability(completion_result->CompletionString)){
1883f4a2713aSLionel Sambuc   case CXAvailability_Available:
1884f4a2713aSLionel Sambuc     break;
1885f4a2713aSLionel Sambuc 
1886f4a2713aSLionel Sambuc   case CXAvailability_Deprecated:
1887f4a2713aSLionel Sambuc     fprintf(file, " (deprecated)");
1888f4a2713aSLionel Sambuc     break;
1889f4a2713aSLionel Sambuc 
1890f4a2713aSLionel Sambuc   case CXAvailability_NotAvailable:
1891f4a2713aSLionel Sambuc     fprintf(file, " (unavailable)");
1892f4a2713aSLionel Sambuc     break;
1893f4a2713aSLionel Sambuc 
1894f4a2713aSLionel Sambuc   case CXAvailability_NotAccessible:
1895f4a2713aSLionel Sambuc     fprintf(file, " (inaccessible)");
1896f4a2713aSLionel Sambuc     break;
1897f4a2713aSLionel Sambuc   }
1898f4a2713aSLionel Sambuc 
1899f4a2713aSLionel Sambuc   annotationCount = clang_getCompletionNumAnnotations(
1900f4a2713aSLionel Sambuc         completion_result->CompletionString);
1901f4a2713aSLionel Sambuc   if (annotationCount) {
1902f4a2713aSLionel Sambuc     unsigned i;
1903f4a2713aSLionel Sambuc     fprintf(file, " (");
1904f4a2713aSLionel Sambuc     for (i = 0; i < annotationCount; ++i) {
1905f4a2713aSLionel Sambuc       if (i != 0)
1906f4a2713aSLionel Sambuc         fprintf(file, ", ");
1907f4a2713aSLionel Sambuc       fprintf(file, "\"%s\"",
1908f4a2713aSLionel Sambuc               clang_getCString(clang_getCompletionAnnotation(
1909f4a2713aSLionel Sambuc                                  completion_result->CompletionString, i)));
1910f4a2713aSLionel Sambuc     }
1911f4a2713aSLionel Sambuc     fprintf(file, ")");
1912f4a2713aSLionel Sambuc   }
1913f4a2713aSLionel Sambuc 
1914f4a2713aSLionel Sambuc   if (!getenv("CINDEXTEST_NO_COMPLETION_PARENTS")) {
1915f4a2713aSLionel Sambuc     ParentName = clang_getCompletionParent(completion_result->CompletionString,
1916f4a2713aSLionel Sambuc                                            &ParentKind);
1917f4a2713aSLionel Sambuc     if (ParentKind != CXCursor_NotImplemented) {
1918f4a2713aSLionel Sambuc       CXString KindSpelling = clang_getCursorKindSpelling(ParentKind);
1919f4a2713aSLionel Sambuc       fprintf(file, " (parent: %s '%s')",
1920f4a2713aSLionel Sambuc               clang_getCString(KindSpelling),
1921f4a2713aSLionel Sambuc               clang_getCString(ParentName));
1922f4a2713aSLionel Sambuc       clang_disposeString(KindSpelling);
1923f4a2713aSLionel Sambuc     }
1924f4a2713aSLionel Sambuc     clang_disposeString(ParentName);
1925f4a2713aSLionel Sambuc   }
1926f4a2713aSLionel Sambuc 
1927f4a2713aSLionel Sambuc   BriefComment = clang_getCompletionBriefComment(
1928f4a2713aSLionel Sambuc                                         completion_result->CompletionString);
1929f4a2713aSLionel Sambuc   BriefCommentCString = clang_getCString(BriefComment);
1930f4a2713aSLionel Sambuc   if (BriefCommentCString && *BriefCommentCString != '\0') {
1931f4a2713aSLionel Sambuc     fprintf(file, "(brief comment: %s)", BriefCommentCString);
1932f4a2713aSLionel Sambuc   }
1933f4a2713aSLionel Sambuc   clang_disposeString(BriefComment);
1934f4a2713aSLionel Sambuc 
1935f4a2713aSLionel Sambuc   fprintf(file, "\n");
1936f4a2713aSLionel Sambuc }
1937f4a2713aSLionel Sambuc 
print_completion_contexts(unsigned long long contexts,FILE * file)1938f4a2713aSLionel Sambuc void print_completion_contexts(unsigned long long contexts, FILE *file) {
1939f4a2713aSLionel Sambuc   fprintf(file, "Completion contexts:\n");
1940f4a2713aSLionel Sambuc   if (contexts == CXCompletionContext_Unknown) {
1941f4a2713aSLionel Sambuc     fprintf(file, "Unknown\n");
1942f4a2713aSLionel Sambuc   }
1943f4a2713aSLionel Sambuc   if (contexts & CXCompletionContext_AnyType) {
1944f4a2713aSLionel Sambuc     fprintf(file, "Any type\n");
1945f4a2713aSLionel Sambuc   }
1946f4a2713aSLionel Sambuc   if (contexts & CXCompletionContext_AnyValue) {
1947f4a2713aSLionel Sambuc     fprintf(file, "Any value\n");
1948f4a2713aSLionel Sambuc   }
1949f4a2713aSLionel Sambuc   if (contexts & CXCompletionContext_ObjCObjectValue) {
1950f4a2713aSLionel Sambuc     fprintf(file, "Objective-C object value\n");
1951f4a2713aSLionel Sambuc   }
1952f4a2713aSLionel Sambuc   if (contexts & CXCompletionContext_ObjCSelectorValue) {
1953f4a2713aSLionel Sambuc     fprintf(file, "Objective-C selector value\n");
1954f4a2713aSLionel Sambuc   }
1955f4a2713aSLionel Sambuc   if (contexts & CXCompletionContext_CXXClassTypeValue) {
1956f4a2713aSLionel Sambuc     fprintf(file, "C++ class type value\n");
1957f4a2713aSLionel Sambuc   }
1958f4a2713aSLionel Sambuc   if (contexts & CXCompletionContext_DotMemberAccess) {
1959f4a2713aSLionel Sambuc     fprintf(file, "Dot member access\n");
1960f4a2713aSLionel Sambuc   }
1961f4a2713aSLionel Sambuc   if (contexts & CXCompletionContext_ArrowMemberAccess) {
1962f4a2713aSLionel Sambuc     fprintf(file, "Arrow member access\n");
1963f4a2713aSLionel Sambuc   }
1964f4a2713aSLionel Sambuc   if (contexts & CXCompletionContext_ObjCPropertyAccess) {
1965f4a2713aSLionel Sambuc     fprintf(file, "Objective-C property access\n");
1966f4a2713aSLionel Sambuc   }
1967f4a2713aSLionel Sambuc   if (contexts & CXCompletionContext_EnumTag) {
1968f4a2713aSLionel Sambuc     fprintf(file, "Enum tag\n");
1969f4a2713aSLionel Sambuc   }
1970f4a2713aSLionel Sambuc   if (contexts & CXCompletionContext_UnionTag) {
1971f4a2713aSLionel Sambuc     fprintf(file, "Union tag\n");
1972f4a2713aSLionel Sambuc   }
1973f4a2713aSLionel Sambuc   if (contexts & CXCompletionContext_StructTag) {
1974f4a2713aSLionel Sambuc     fprintf(file, "Struct tag\n");
1975f4a2713aSLionel Sambuc   }
1976f4a2713aSLionel Sambuc   if (contexts & CXCompletionContext_ClassTag) {
1977f4a2713aSLionel Sambuc     fprintf(file, "Class name\n");
1978f4a2713aSLionel Sambuc   }
1979f4a2713aSLionel Sambuc   if (contexts & CXCompletionContext_Namespace) {
1980f4a2713aSLionel Sambuc     fprintf(file, "Namespace or namespace alias\n");
1981f4a2713aSLionel Sambuc   }
1982f4a2713aSLionel Sambuc   if (contexts & CXCompletionContext_NestedNameSpecifier) {
1983f4a2713aSLionel Sambuc     fprintf(file, "Nested name specifier\n");
1984f4a2713aSLionel Sambuc   }
1985f4a2713aSLionel Sambuc   if (contexts & CXCompletionContext_ObjCInterface) {
1986f4a2713aSLionel Sambuc     fprintf(file, "Objective-C interface\n");
1987f4a2713aSLionel Sambuc   }
1988f4a2713aSLionel Sambuc   if (contexts & CXCompletionContext_ObjCProtocol) {
1989f4a2713aSLionel Sambuc     fprintf(file, "Objective-C protocol\n");
1990f4a2713aSLionel Sambuc   }
1991f4a2713aSLionel Sambuc   if (contexts & CXCompletionContext_ObjCCategory) {
1992f4a2713aSLionel Sambuc     fprintf(file, "Objective-C category\n");
1993f4a2713aSLionel Sambuc   }
1994f4a2713aSLionel Sambuc   if (contexts & CXCompletionContext_ObjCInstanceMessage) {
1995f4a2713aSLionel Sambuc     fprintf(file, "Objective-C instance method\n");
1996f4a2713aSLionel Sambuc   }
1997f4a2713aSLionel Sambuc   if (contexts & CXCompletionContext_ObjCClassMessage) {
1998f4a2713aSLionel Sambuc     fprintf(file, "Objective-C class method\n");
1999f4a2713aSLionel Sambuc   }
2000f4a2713aSLionel Sambuc   if (contexts & CXCompletionContext_ObjCSelectorName) {
2001f4a2713aSLionel Sambuc     fprintf(file, "Objective-C selector name\n");
2002f4a2713aSLionel Sambuc   }
2003f4a2713aSLionel Sambuc   if (contexts & CXCompletionContext_MacroName) {
2004f4a2713aSLionel Sambuc     fprintf(file, "Macro name\n");
2005f4a2713aSLionel Sambuc   }
2006f4a2713aSLionel Sambuc   if (contexts & CXCompletionContext_NaturalLanguage) {
2007f4a2713aSLionel Sambuc     fprintf(file, "Natural language\n");
2008f4a2713aSLionel Sambuc   }
2009f4a2713aSLionel Sambuc }
2010f4a2713aSLionel Sambuc 
my_stricmp(const char * s1,const char * s2)2011f4a2713aSLionel Sambuc int my_stricmp(const char *s1, const char *s2) {
2012f4a2713aSLionel Sambuc   while (*s1 && *s2) {
2013f4a2713aSLionel Sambuc     int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
2014f4a2713aSLionel Sambuc     if (c1 < c2)
2015f4a2713aSLionel Sambuc       return -1;
2016f4a2713aSLionel Sambuc     else if (c1 > c2)
2017f4a2713aSLionel Sambuc       return 1;
2018f4a2713aSLionel Sambuc 
2019f4a2713aSLionel Sambuc     ++s1;
2020f4a2713aSLionel Sambuc     ++s2;
2021f4a2713aSLionel Sambuc   }
2022f4a2713aSLionel Sambuc 
2023f4a2713aSLionel Sambuc   if (*s1)
2024f4a2713aSLionel Sambuc     return 1;
2025f4a2713aSLionel Sambuc   else if (*s2)
2026f4a2713aSLionel Sambuc     return -1;
2027f4a2713aSLionel Sambuc   return 0;
2028f4a2713aSLionel Sambuc }
2029f4a2713aSLionel Sambuc 
perform_code_completion(int argc,const char ** argv,int timing_only)2030f4a2713aSLionel Sambuc int perform_code_completion(int argc, const char **argv, int timing_only) {
2031f4a2713aSLionel Sambuc   const char *input = argv[1];
2032f4a2713aSLionel Sambuc   char *filename = 0;
2033f4a2713aSLionel Sambuc   unsigned line;
2034f4a2713aSLionel Sambuc   unsigned column;
2035f4a2713aSLionel Sambuc   CXIndex CIdx;
2036f4a2713aSLionel Sambuc   int errorCode;
2037f4a2713aSLionel Sambuc   struct CXUnsavedFile *unsaved_files = 0;
2038f4a2713aSLionel Sambuc   int num_unsaved_files = 0;
2039f4a2713aSLionel Sambuc   CXCodeCompleteResults *results = 0;
2040*0a6a1f1dSLionel Sambuc   enum CXErrorCode Err;
2041*0a6a1f1dSLionel Sambuc   CXTranslationUnit TU;
2042f4a2713aSLionel Sambuc   unsigned I, Repeats = 1;
2043f4a2713aSLionel Sambuc   unsigned completionOptions = clang_defaultCodeCompleteOptions();
2044f4a2713aSLionel Sambuc 
2045f4a2713aSLionel Sambuc   if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
2046f4a2713aSLionel Sambuc     completionOptions |= CXCodeComplete_IncludeCodePatterns;
2047f4a2713aSLionel Sambuc   if (getenv("CINDEXTEST_COMPLETION_BRIEF_COMMENTS"))
2048f4a2713aSLionel Sambuc     completionOptions |= CXCodeComplete_IncludeBriefComments;
2049f4a2713aSLionel Sambuc 
2050f4a2713aSLionel Sambuc   if (timing_only)
2051f4a2713aSLionel Sambuc     input += strlen("-code-completion-timing=");
2052f4a2713aSLionel Sambuc   else
2053f4a2713aSLionel Sambuc     input += strlen("-code-completion-at=");
2054f4a2713aSLionel Sambuc 
2055f4a2713aSLionel Sambuc   if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
2056f4a2713aSLionel Sambuc                                           0, 0)))
2057f4a2713aSLionel Sambuc     return errorCode;
2058f4a2713aSLionel Sambuc 
2059f4a2713aSLionel Sambuc   if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
2060f4a2713aSLionel Sambuc     return -1;
2061f4a2713aSLionel Sambuc 
2062f4a2713aSLionel Sambuc   CIdx = clang_createIndex(0, 0);
2063f4a2713aSLionel Sambuc 
2064f4a2713aSLionel Sambuc   if (getenv("CINDEXTEST_EDITING"))
2065f4a2713aSLionel Sambuc     Repeats = 5;
2066f4a2713aSLionel Sambuc 
2067*0a6a1f1dSLionel Sambuc   Err = clang_parseTranslationUnit2(CIdx, 0,
2068f4a2713aSLionel Sambuc                                     argv + num_unsaved_files + 2,
2069f4a2713aSLionel Sambuc                                     argc - num_unsaved_files - 2,
2070*0a6a1f1dSLionel Sambuc                                     0, 0, getDefaultParsingOptions(), &TU);
2071*0a6a1f1dSLionel Sambuc   if (Err != CXError_Success) {
2072f4a2713aSLionel Sambuc     fprintf(stderr, "Unable to load translation unit!\n");
2073*0a6a1f1dSLionel Sambuc     describeLibclangFailure(Err);
2074f4a2713aSLionel Sambuc     return 1;
2075f4a2713aSLionel Sambuc   }
2076f4a2713aSLionel Sambuc 
2077*0a6a1f1dSLionel Sambuc   Err = clang_reparseTranslationUnit(TU, 0, 0,
2078*0a6a1f1dSLionel Sambuc                                      clang_defaultReparseOptions(TU));
2079*0a6a1f1dSLionel Sambuc 
2080*0a6a1f1dSLionel Sambuc   if (Err != CXError_Success) {
2081f4a2713aSLionel Sambuc     fprintf(stderr, "Unable to reparse translation init!\n");
2082*0a6a1f1dSLionel Sambuc     describeLibclangFailure(Err);
2083*0a6a1f1dSLionel Sambuc     clang_disposeTranslationUnit(TU);
2084f4a2713aSLionel Sambuc     return 1;
2085f4a2713aSLionel Sambuc   }
2086f4a2713aSLionel Sambuc 
2087f4a2713aSLionel Sambuc   for (I = 0; I != Repeats; ++I) {
2088f4a2713aSLionel Sambuc     results = clang_codeCompleteAt(TU, filename, line, column,
2089f4a2713aSLionel Sambuc                                    unsaved_files, num_unsaved_files,
2090f4a2713aSLionel Sambuc                                    completionOptions);
2091f4a2713aSLionel Sambuc     if (!results) {
2092f4a2713aSLionel Sambuc       fprintf(stderr, "Unable to perform code completion!\n");
2093f4a2713aSLionel Sambuc       return 1;
2094f4a2713aSLionel Sambuc     }
2095f4a2713aSLionel Sambuc     if (I != Repeats-1)
2096f4a2713aSLionel Sambuc       clang_disposeCodeCompleteResults(results);
2097f4a2713aSLionel Sambuc   }
2098f4a2713aSLionel Sambuc 
2099f4a2713aSLionel Sambuc   if (results) {
2100f4a2713aSLionel Sambuc     unsigned i, n = results->NumResults, containerIsIncomplete = 0;
2101f4a2713aSLionel Sambuc     unsigned long long contexts;
2102f4a2713aSLionel Sambuc     enum CXCursorKind containerKind;
2103f4a2713aSLionel Sambuc     CXString objCSelector;
2104f4a2713aSLionel Sambuc     const char *selectorString;
2105f4a2713aSLionel Sambuc     if (!timing_only) {
2106f4a2713aSLionel Sambuc       /* Sort the code-completion results based on the typed text. */
2107f4a2713aSLionel Sambuc       clang_sortCodeCompletionResults(results->Results, results->NumResults);
2108f4a2713aSLionel Sambuc 
2109f4a2713aSLionel Sambuc       for (i = 0; i != n; ++i)
2110f4a2713aSLionel Sambuc         print_completion_result(results->Results + i, stdout);
2111f4a2713aSLionel Sambuc     }
2112f4a2713aSLionel Sambuc     n = clang_codeCompleteGetNumDiagnostics(results);
2113f4a2713aSLionel Sambuc     for (i = 0; i != n; ++i) {
2114f4a2713aSLionel Sambuc       CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
2115f4a2713aSLionel Sambuc       PrintDiagnostic(diag);
2116f4a2713aSLionel Sambuc       clang_disposeDiagnostic(diag);
2117f4a2713aSLionel Sambuc     }
2118f4a2713aSLionel Sambuc 
2119f4a2713aSLionel Sambuc     contexts = clang_codeCompleteGetContexts(results);
2120f4a2713aSLionel Sambuc     print_completion_contexts(contexts, stdout);
2121f4a2713aSLionel Sambuc 
2122f4a2713aSLionel Sambuc     containerKind = clang_codeCompleteGetContainerKind(results,
2123f4a2713aSLionel Sambuc                                                        &containerIsIncomplete);
2124f4a2713aSLionel Sambuc 
2125f4a2713aSLionel Sambuc     if (containerKind != CXCursor_InvalidCode) {
2126f4a2713aSLionel Sambuc       /* We have found a container */
2127f4a2713aSLionel Sambuc       CXString containerUSR, containerKindSpelling;
2128f4a2713aSLionel Sambuc       containerKindSpelling = clang_getCursorKindSpelling(containerKind);
2129f4a2713aSLionel Sambuc       printf("Container Kind: %s\n", clang_getCString(containerKindSpelling));
2130f4a2713aSLionel Sambuc       clang_disposeString(containerKindSpelling);
2131f4a2713aSLionel Sambuc 
2132f4a2713aSLionel Sambuc       if (containerIsIncomplete) {
2133f4a2713aSLionel Sambuc         printf("Container is incomplete\n");
2134f4a2713aSLionel Sambuc       }
2135f4a2713aSLionel Sambuc       else {
2136f4a2713aSLionel Sambuc         printf("Container is complete\n");
2137f4a2713aSLionel Sambuc       }
2138f4a2713aSLionel Sambuc 
2139f4a2713aSLionel Sambuc       containerUSR = clang_codeCompleteGetContainerUSR(results);
2140f4a2713aSLionel Sambuc       printf("Container USR: %s\n", clang_getCString(containerUSR));
2141f4a2713aSLionel Sambuc       clang_disposeString(containerUSR);
2142f4a2713aSLionel Sambuc     }
2143f4a2713aSLionel Sambuc 
2144f4a2713aSLionel Sambuc     objCSelector = clang_codeCompleteGetObjCSelector(results);
2145f4a2713aSLionel Sambuc     selectorString = clang_getCString(objCSelector);
2146f4a2713aSLionel Sambuc     if (selectorString && strlen(selectorString) > 0) {
2147f4a2713aSLionel Sambuc       printf("Objective-C selector: %s\n", selectorString);
2148f4a2713aSLionel Sambuc     }
2149f4a2713aSLionel Sambuc     clang_disposeString(objCSelector);
2150f4a2713aSLionel Sambuc 
2151f4a2713aSLionel Sambuc     clang_disposeCodeCompleteResults(results);
2152f4a2713aSLionel Sambuc   }
2153f4a2713aSLionel Sambuc   clang_disposeTranslationUnit(TU);
2154f4a2713aSLionel Sambuc   clang_disposeIndex(CIdx);
2155f4a2713aSLionel Sambuc   free(filename);
2156f4a2713aSLionel Sambuc 
2157f4a2713aSLionel Sambuc   free_remapped_files(unsaved_files, num_unsaved_files);
2158f4a2713aSLionel Sambuc 
2159f4a2713aSLionel Sambuc   return 0;
2160f4a2713aSLionel Sambuc }
2161f4a2713aSLionel Sambuc 
2162f4a2713aSLionel Sambuc typedef struct {
2163f4a2713aSLionel Sambuc   char *filename;
2164f4a2713aSLionel Sambuc   unsigned line;
2165f4a2713aSLionel Sambuc   unsigned column;
2166f4a2713aSLionel Sambuc } CursorSourceLocation;
2167f4a2713aSLionel Sambuc 
inspect_cursor_at(int argc,const char ** argv)2168f4a2713aSLionel Sambuc static int inspect_cursor_at(int argc, const char **argv) {
2169f4a2713aSLionel Sambuc   CXIndex CIdx;
2170f4a2713aSLionel Sambuc   int errorCode;
2171f4a2713aSLionel Sambuc   struct CXUnsavedFile *unsaved_files = 0;
2172f4a2713aSLionel Sambuc   int num_unsaved_files = 0;
2173*0a6a1f1dSLionel Sambuc   enum CXErrorCode Err;
2174f4a2713aSLionel Sambuc   CXTranslationUnit TU;
2175f4a2713aSLionel Sambuc   CXCursor Cursor;
2176f4a2713aSLionel Sambuc   CursorSourceLocation *Locations = 0;
2177f4a2713aSLionel Sambuc   unsigned NumLocations = 0, Loc;
2178f4a2713aSLionel Sambuc   unsigned Repeats = 1;
2179f4a2713aSLionel Sambuc   unsigned I;
2180f4a2713aSLionel Sambuc 
2181f4a2713aSLionel Sambuc   /* Count the number of locations. */
2182f4a2713aSLionel Sambuc   while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
2183f4a2713aSLionel Sambuc     ++NumLocations;
2184f4a2713aSLionel Sambuc 
2185f4a2713aSLionel Sambuc   /* Parse the locations. */
2186f4a2713aSLionel Sambuc   assert(NumLocations > 0 && "Unable to count locations?");
2187f4a2713aSLionel Sambuc   Locations = (CursorSourceLocation *)malloc(
2188f4a2713aSLionel Sambuc                                   NumLocations * sizeof(CursorSourceLocation));
2189f4a2713aSLionel Sambuc   for (Loc = 0; Loc < NumLocations; ++Loc) {
2190f4a2713aSLionel Sambuc     const char *input = argv[Loc + 1] + strlen("-cursor-at=");
2191f4a2713aSLionel Sambuc     if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
2192f4a2713aSLionel Sambuc                                             &Locations[Loc].line,
2193f4a2713aSLionel Sambuc                                             &Locations[Loc].column, 0, 0)))
2194f4a2713aSLionel Sambuc       return errorCode;
2195f4a2713aSLionel Sambuc   }
2196f4a2713aSLionel Sambuc 
2197f4a2713aSLionel Sambuc   if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
2198f4a2713aSLionel Sambuc                            &num_unsaved_files))
2199f4a2713aSLionel Sambuc     return -1;
2200f4a2713aSLionel Sambuc 
2201f4a2713aSLionel Sambuc   if (getenv("CINDEXTEST_EDITING"))
2202f4a2713aSLionel Sambuc     Repeats = 5;
2203f4a2713aSLionel Sambuc 
2204f4a2713aSLionel Sambuc   /* Parse the translation unit. When we're testing clang_getCursor() after
2205f4a2713aSLionel Sambuc      reparsing, don't remap unsaved files until the second parse. */
2206f4a2713aSLionel Sambuc   CIdx = clang_createIndex(1, 1);
2207*0a6a1f1dSLionel Sambuc   Err = clang_parseTranslationUnit2(CIdx, argv[argc - 1],
2208f4a2713aSLionel Sambuc                                    argv + num_unsaved_files + 1 + NumLocations,
2209f4a2713aSLionel Sambuc                                    argc - num_unsaved_files - 2 - NumLocations,
2210f4a2713aSLionel Sambuc                                    unsaved_files,
2211f4a2713aSLionel Sambuc                                    Repeats > 1? 0 : num_unsaved_files,
2212*0a6a1f1dSLionel Sambuc                                    getDefaultParsingOptions(), &TU);
2213*0a6a1f1dSLionel Sambuc   if (Err != CXError_Success) {
2214f4a2713aSLionel Sambuc     fprintf(stderr, "unable to parse input\n");
2215*0a6a1f1dSLionel Sambuc     describeLibclangFailure(Err);
2216f4a2713aSLionel Sambuc     return -1;
2217f4a2713aSLionel Sambuc   }
2218f4a2713aSLionel Sambuc 
2219f4a2713aSLionel Sambuc   if (checkForErrors(TU) != 0)
2220f4a2713aSLionel Sambuc     return -1;
2221f4a2713aSLionel Sambuc 
2222f4a2713aSLionel Sambuc   for (I = 0; I != Repeats; ++I) {
2223*0a6a1f1dSLionel Sambuc     if (Repeats > 1) {
2224*0a6a1f1dSLionel Sambuc       Err = clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2225*0a6a1f1dSLionel Sambuc                                          clang_defaultReparseOptions(TU));
2226*0a6a1f1dSLionel Sambuc       if (Err != CXError_Success) {
2227*0a6a1f1dSLionel Sambuc         describeLibclangFailure(Err);
2228f4a2713aSLionel Sambuc         clang_disposeTranslationUnit(TU);
2229f4a2713aSLionel Sambuc         return 1;
2230f4a2713aSLionel Sambuc       }
2231*0a6a1f1dSLionel Sambuc     }
2232f4a2713aSLionel Sambuc 
2233f4a2713aSLionel Sambuc     if (checkForErrors(TU) != 0)
2234f4a2713aSLionel Sambuc       return -1;
2235f4a2713aSLionel Sambuc 
2236f4a2713aSLionel Sambuc     for (Loc = 0; Loc < NumLocations; ++Loc) {
2237f4a2713aSLionel Sambuc       CXFile file = clang_getFile(TU, Locations[Loc].filename);
2238f4a2713aSLionel Sambuc       if (!file)
2239f4a2713aSLionel Sambuc         continue;
2240f4a2713aSLionel Sambuc 
2241f4a2713aSLionel Sambuc       Cursor = clang_getCursor(TU,
2242f4a2713aSLionel Sambuc                                clang_getLocation(TU, file, Locations[Loc].line,
2243f4a2713aSLionel Sambuc                                                  Locations[Loc].column));
2244f4a2713aSLionel Sambuc 
2245f4a2713aSLionel Sambuc       if (checkForErrors(TU) != 0)
2246f4a2713aSLionel Sambuc         return -1;
2247f4a2713aSLionel Sambuc 
2248f4a2713aSLionel Sambuc       if (I + 1 == Repeats) {
2249f4a2713aSLionel Sambuc         CXCompletionString completionString = clang_getCursorCompletionString(
2250f4a2713aSLionel Sambuc                                                                         Cursor);
2251f4a2713aSLionel Sambuc         CXSourceLocation CursorLoc = clang_getCursorLocation(Cursor);
2252f4a2713aSLionel Sambuc         CXString Spelling;
2253f4a2713aSLionel Sambuc         const char *cspell;
2254f4a2713aSLionel Sambuc         unsigned line, column;
2255f4a2713aSLionel Sambuc         clang_getSpellingLocation(CursorLoc, 0, &line, &column, 0);
2256f4a2713aSLionel Sambuc         printf("%d:%d ", line, column);
2257f4a2713aSLionel Sambuc         PrintCursor(Cursor, NULL);
2258f4a2713aSLionel Sambuc         PrintCursorExtent(Cursor);
2259f4a2713aSLionel Sambuc         Spelling = clang_getCursorSpelling(Cursor);
2260f4a2713aSLionel Sambuc         cspell = clang_getCString(Spelling);
2261f4a2713aSLionel Sambuc         if (cspell && strlen(cspell) != 0) {
2262f4a2713aSLionel Sambuc           unsigned pieceIndex;
2263f4a2713aSLionel Sambuc           printf(" Spelling=%s (", cspell);
2264f4a2713aSLionel Sambuc           for (pieceIndex = 0; ; ++pieceIndex) {
2265f4a2713aSLionel Sambuc             CXSourceRange range =
2266f4a2713aSLionel Sambuc               clang_Cursor_getSpellingNameRange(Cursor, pieceIndex, 0);
2267f4a2713aSLionel Sambuc             if (clang_Range_isNull(range))
2268f4a2713aSLionel Sambuc               break;
2269f4a2713aSLionel Sambuc             PrintRange(range, 0);
2270f4a2713aSLionel Sambuc           }
2271f4a2713aSLionel Sambuc           printf(")");
2272f4a2713aSLionel Sambuc         }
2273f4a2713aSLionel Sambuc         clang_disposeString(Spelling);
2274f4a2713aSLionel Sambuc         if (clang_Cursor_getObjCSelectorIndex(Cursor) != -1)
2275*0a6a1f1dSLionel Sambuc           printf(" Selector index=%d",
2276*0a6a1f1dSLionel Sambuc                  clang_Cursor_getObjCSelectorIndex(Cursor));
2277f4a2713aSLionel Sambuc         if (clang_Cursor_isDynamicCall(Cursor))
2278f4a2713aSLionel Sambuc           printf(" Dynamic-call");
2279f4a2713aSLionel Sambuc         if (Cursor.kind == CXCursor_ObjCMessageExpr) {
2280f4a2713aSLionel Sambuc           CXType T = clang_Cursor_getReceiverType(Cursor);
2281f4a2713aSLionel Sambuc           CXString S = clang_getTypeKindSpelling(T.kind);
2282f4a2713aSLionel Sambuc           printf(" Receiver-type=%s", clang_getCString(S));
2283f4a2713aSLionel Sambuc           clang_disposeString(S);
2284f4a2713aSLionel Sambuc         }
2285f4a2713aSLionel Sambuc 
2286f4a2713aSLionel Sambuc         {
2287f4a2713aSLionel Sambuc           CXModule mod = clang_Cursor_getModule(Cursor);
2288f4a2713aSLionel Sambuc           CXFile astFile;
2289f4a2713aSLionel Sambuc           CXString name, astFilename;
2290f4a2713aSLionel Sambuc           unsigned i, numHeaders;
2291f4a2713aSLionel Sambuc           if (mod) {
2292f4a2713aSLionel Sambuc             astFile = clang_Module_getASTFile(mod);
2293f4a2713aSLionel Sambuc             astFilename = clang_getFileName(astFile);
2294f4a2713aSLionel Sambuc             name = clang_Module_getFullName(mod);
2295f4a2713aSLionel Sambuc             numHeaders = clang_Module_getNumTopLevelHeaders(TU, mod);
2296*0a6a1f1dSLionel Sambuc             printf(" ModuleName=%s (%s) system=%d Headers(%d):",
2297f4a2713aSLionel Sambuc                    clang_getCString(name), clang_getCString(astFilename),
2298*0a6a1f1dSLionel Sambuc                    clang_Module_isSystem(mod), numHeaders);
2299f4a2713aSLionel Sambuc             clang_disposeString(name);
2300f4a2713aSLionel Sambuc             clang_disposeString(astFilename);
2301f4a2713aSLionel Sambuc             for (i = 0; i < numHeaders; ++i) {
2302f4a2713aSLionel Sambuc               CXFile file = clang_Module_getTopLevelHeader(TU, mod, i);
2303f4a2713aSLionel Sambuc               CXString filename = clang_getFileName(file);
2304f4a2713aSLionel Sambuc               printf("\n%s", clang_getCString(filename));
2305f4a2713aSLionel Sambuc               clang_disposeString(filename);
2306f4a2713aSLionel Sambuc             }
2307f4a2713aSLionel Sambuc           }
2308f4a2713aSLionel Sambuc         }
2309f4a2713aSLionel Sambuc 
2310f4a2713aSLionel Sambuc         if (completionString != NULL) {
2311f4a2713aSLionel Sambuc           printf("\nCompletion string: ");
2312f4a2713aSLionel Sambuc           print_completion_string(completionString, stdout);
2313f4a2713aSLionel Sambuc         }
2314f4a2713aSLionel Sambuc         printf("\n");
2315f4a2713aSLionel Sambuc         free(Locations[Loc].filename);
2316f4a2713aSLionel Sambuc       }
2317f4a2713aSLionel Sambuc     }
2318f4a2713aSLionel Sambuc   }
2319f4a2713aSLionel Sambuc 
2320f4a2713aSLionel Sambuc   PrintDiagnostics(TU);
2321f4a2713aSLionel Sambuc   clang_disposeTranslationUnit(TU);
2322f4a2713aSLionel Sambuc   clang_disposeIndex(CIdx);
2323f4a2713aSLionel Sambuc   free(Locations);
2324f4a2713aSLionel Sambuc   free_remapped_files(unsaved_files, num_unsaved_files);
2325f4a2713aSLionel Sambuc   return 0;
2326f4a2713aSLionel Sambuc }
2327f4a2713aSLionel Sambuc 
findFileRefsVisit(void * context,CXCursor cursor,CXSourceRange range)2328f4a2713aSLionel Sambuc static enum CXVisitorResult findFileRefsVisit(void *context,
2329f4a2713aSLionel Sambuc                                          CXCursor cursor, CXSourceRange range) {
2330f4a2713aSLionel Sambuc   if (clang_Range_isNull(range))
2331f4a2713aSLionel Sambuc     return CXVisit_Continue;
2332f4a2713aSLionel Sambuc 
2333f4a2713aSLionel Sambuc   PrintCursor(cursor, NULL);
2334f4a2713aSLionel Sambuc   PrintRange(range, "");
2335f4a2713aSLionel Sambuc   printf("\n");
2336f4a2713aSLionel Sambuc   return CXVisit_Continue;
2337f4a2713aSLionel Sambuc }
2338f4a2713aSLionel Sambuc 
find_file_refs_at(int argc,const char ** argv)2339f4a2713aSLionel Sambuc static int find_file_refs_at(int argc, const char **argv) {
2340f4a2713aSLionel Sambuc   CXIndex CIdx;
2341f4a2713aSLionel Sambuc   int errorCode;
2342f4a2713aSLionel Sambuc   struct CXUnsavedFile *unsaved_files = 0;
2343f4a2713aSLionel Sambuc   int num_unsaved_files = 0;
2344*0a6a1f1dSLionel Sambuc   enum CXErrorCode Err;
2345f4a2713aSLionel Sambuc   CXTranslationUnit TU;
2346f4a2713aSLionel Sambuc   CXCursor Cursor;
2347f4a2713aSLionel Sambuc   CursorSourceLocation *Locations = 0;
2348f4a2713aSLionel Sambuc   unsigned NumLocations = 0, Loc;
2349f4a2713aSLionel Sambuc   unsigned Repeats = 1;
2350f4a2713aSLionel Sambuc   unsigned I;
2351f4a2713aSLionel Sambuc 
2352f4a2713aSLionel Sambuc   /* Count the number of locations. */
2353f4a2713aSLionel Sambuc   while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1])
2354f4a2713aSLionel Sambuc     ++NumLocations;
2355f4a2713aSLionel Sambuc 
2356f4a2713aSLionel Sambuc   /* Parse the locations. */
2357f4a2713aSLionel Sambuc   assert(NumLocations > 0 && "Unable to count locations?");
2358f4a2713aSLionel Sambuc   Locations = (CursorSourceLocation *)malloc(
2359f4a2713aSLionel Sambuc                                   NumLocations * sizeof(CursorSourceLocation));
2360f4a2713aSLionel Sambuc   for (Loc = 0; Loc < NumLocations; ++Loc) {
2361f4a2713aSLionel Sambuc     const char *input = argv[Loc + 1] + strlen("-file-refs-at=");
2362f4a2713aSLionel Sambuc     if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
2363f4a2713aSLionel Sambuc                                             &Locations[Loc].line,
2364f4a2713aSLionel Sambuc                                             &Locations[Loc].column, 0, 0)))
2365f4a2713aSLionel Sambuc       return errorCode;
2366f4a2713aSLionel Sambuc   }
2367f4a2713aSLionel Sambuc 
2368f4a2713aSLionel Sambuc   if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
2369f4a2713aSLionel Sambuc                            &num_unsaved_files))
2370f4a2713aSLionel Sambuc     return -1;
2371f4a2713aSLionel Sambuc 
2372f4a2713aSLionel Sambuc   if (getenv("CINDEXTEST_EDITING"))
2373f4a2713aSLionel Sambuc     Repeats = 5;
2374f4a2713aSLionel Sambuc 
2375f4a2713aSLionel Sambuc   /* Parse the translation unit. When we're testing clang_getCursor() after
2376f4a2713aSLionel Sambuc      reparsing, don't remap unsaved files until the second parse. */
2377f4a2713aSLionel Sambuc   CIdx = clang_createIndex(1, 1);
2378*0a6a1f1dSLionel Sambuc   Err = clang_parseTranslationUnit2(CIdx, argv[argc - 1],
2379f4a2713aSLionel Sambuc                                     argv + num_unsaved_files + 1 + NumLocations,
2380f4a2713aSLionel Sambuc                                     argc - num_unsaved_files - 2 - NumLocations,
2381f4a2713aSLionel Sambuc                                     unsaved_files,
2382f4a2713aSLionel Sambuc                                     Repeats > 1? 0 : num_unsaved_files,
2383*0a6a1f1dSLionel Sambuc                                     getDefaultParsingOptions(), &TU);
2384*0a6a1f1dSLionel Sambuc   if (Err != CXError_Success) {
2385f4a2713aSLionel Sambuc     fprintf(stderr, "unable to parse input\n");
2386*0a6a1f1dSLionel Sambuc     describeLibclangFailure(Err);
2387*0a6a1f1dSLionel Sambuc     clang_disposeTranslationUnit(TU);
2388f4a2713aSLionel Sambuc     return -1;
2389f4a2713aSLionel Sambuc   }
2390f4a2713aSLionel Sambuc 
2391f4a2713aSLionel Sambuc   if (checkForErrors(TU) != 0)
2392f4a2713aSLionel Sambuc     return -1;
2393f4a2713aSLionel Sambuc 
2394f4a2713aSLionel Sambuc   for (I = 0; I != Repeats; ++I) {
2395*0a6a1f1dSLionel Sambuc     if (Repeats > 1) {
2396*0a6a1f1dSLionel Sambuc       Err = clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2397*0a6a1f1dSLionel Sambuc                                          clang_defaultReparseOptions(TU));
2398*0a6a1f1dSLionel Sambuc       if (Err != CXError_Success) {
2399*0a6a1f1dSLionel Sambuc         describeLibclangFailure(Err);
2400f4a2713aSLionel Sambuc         clang_disposeTranslationUnit(TU);
2401f4a2713aSLionel Sambuc         return 1;
2402f4a2713aSLionel Sambuc       }
2403*0a6a1f1dSLionel Sambuc     }
2404f4a2713aSLionel Sambuc 
2405f4a2713aSLionel Sambuc     if (checkForErrors(TU) != 0)
2406f4a2713aSLionel Sambuc       return -1;
2407f4a2713aSLionel Sambuc 
2408f4a2713aSLionel Sambuc     for (Loc = 0; Loc < NumLocations; ++Loc) {
2409f4a2713aSLionel Sambuc       CXFile file = clang_getFile(TU, Locations[Loc].filename);
2410f4a2713aSLionel Sambuc       if (!file)
2411f4a2713aSLionel Sambuc         continue;
2412f4a2713aSLionel Sambuc 
2413f4a2713aSLionel Sambuc       Cursor = clang_getCursor(TU,
2414f4a2713aSLionel Sambuc                                clang_getLocation(TU, file, Locations[Loc].line,
2415f4a2713aSLionel Sambuc                                                  Locations[Loc].column));
2416f4a2713aSLionel Sambuc 
2417f4a2713aSLionel Sambuc       if (checkForErrors(TU) != 0)
2418f4a2713aSLionel Sambuc         return -1;
2419f4a2713aSLionel Sambuc 
2420f4a2713aSLionel Sambuc       if (I + 1 == Repeats) {
2421f4a2713aSLionel Sambuc         CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit };
2422f4a2713aSLionel Sambuc         PrintCursor(Cursor, NULL);
2423f4a2713aSLionel Sambuc         printf("\n");
2424f4a2713aSLionel Sambuc         clang_findReferencesInFile(Cursor, file, visitor);
2425f4a2713aSLionel Sambuc         free(Locations[Loc].filename);
2426f4a2713aSLionel Sambuc 
2427f4a2713aSLionel Sambuc         if (checkForErrors(TU) != 0)
2428f4a2713aSLionel Sambuc           return -1;
2429f4a2713aSLionel Sambuc       }
2430f4a2713aSLionel Sambuc     }
2431f4a2713aSLionel Sambuc   }
2432f4a2713aSLionel Sambuc 
2433f4a2713aSLionel Sambuc   PrintDiagnostics(TU);
2434f4a2713aSLionel Sambuc   clang_disposeTranslationUnit(TU);
2435f4a2713aSLionel Sambuc   clang_disposeIndex(CIdx);
2436f4a2713aSLionel Sambuc   free(Locations);
2437f4a2713aSLionel Sambuc   free_remapped_files(unsaved_files, num_unsaved_files);
2438f4a2713aSLionel Sambuc   return 0;
2439f4a2713aSLionel Sambuc }
2440f4a2713aSLionel Sambuc 
findFileIncludesVisit(void * context,CXCursor cursor,CXSourceRange range)2441f4a2713aSLionel Sambuc static enum CXVisitorResult findFileIncludesVisit(void *context,
2442f4a2713aSLionel Sambuc                                          CXCursor cursor, CXSourceRange range) {
2443f4a2713aSLionel Sambuc   PrintCursor(cursor, NULL);
2444f4a2713aSLionel Sambuc   PrintRange(range, "");
2445f4a2713aSLionel Sambuc   printf("\n");
2446f4a2713aSLionel Sambuc   return CXVisit_Continue;
2447f4a2713aSLionel Sambuc }
2448f4a2713aSLionel Sambuc 
find_file_includes_in(int argc,const char ** argv)2449f4a2713aSLionel Sambuc static int find_file_includes_in(int argc, const char **argv) {
2450f4a2713aSLionel Sambuc   CXIndex CIdx;
2451f4a2713aSLionel Sambuc   struct CXUnsavedFile *unsaved_files = 0;
2452f4a2713aSLionel Sambuc   int num_unsaved_files = 0;
2453*0a6a1f1dSLionel Sambuc   enum CXErrorCode Err;
2454f4a2713aSLionel Sambuc   CXTranslationUnit TU;
2455f4a2713aSLionel Sambuc   const char **Filenames = 0;
2456f4a2713aSLionel Sambuc   unsigned NumFilenames = 0;
2457f4a2713aSLionel Sambuc   unsigned Repeats = 1;
2458f4a2713aSLionel Sambuc   unsigned I, FI;
2459f4a2713aSLionel Sambuc 
2460f4a2713aSLionel Sambuc   /* Count the number of locations. */
2461f4a2713aSLionel Sambuc   while (strstr(argv[NumFilenames+1], "-file-includes-in=") == argv[NumFilenames+1])
2462f4a2713aSLionel Sambuc     ++NumFilenames;
2463f4a2713aSLionel Sambuc 
2464f4a2713aSLionel Sambuc   /* Parse the locations. */
2465f4a2713aSLionel Sambuc   assert(NumFilenames > 0 && "Unable to count filenames?");
2466f4a2713aSLionel Sambuc   Filenames = (const char **)malloc(NumFilenames * sizeof(const char *));
2467f4a2713aSLionel Sambuc   for (I = 0; I < NumFilenames; ++I) {
2468f4a2713aSLionel Sambuc     const char *input = argv[I + 1] + strlen("-file-includes-in=");
2469f4a2713aSLionel Sambuc     /* Copy the file name. */
2470f4a2713aSLionel Sambuc     Filenames[I] = input;
2471f4a2713aSLionel Sambuc   }
2472f4a2713aSLionel Sambuc 
2473f4a2713aSLionel Sambuc   if (parse_remapped_files(argc, argv, NumFilenames + 1, &unsaved_files,
2474f4a2713aSLionel Sambuc                            &num_unsaved_files))
2475f4a2713aSLionel Sambuc     return -1;
2476f4a2713aSLionel Sambuc 
2477f4a2713aSLionel Sambuc   if (getenv("CINDEXTEST_EDITING"))
2478f4a2713aSLionel Sambuc     Repeats = 2;
2479f4a2713aSLionel Sambuc 
2480f4a2713aSLionel Sambuc   /* Parse the translation unit. When we're testing clang_getCursor() after
2481f4a2713aSLionel Sambuc      reparsing, don't remap unsaved files until the second parse. */
2482f4a2713aSLionel Sambuc   CIdx = clang_createIndex(1, 1);
2483*0a6a1f1dSLionel Sambuc   Err = clang_parseTranslationUnit2(
2484*0a6a1f1dSLionel Sambuc       CIdx, argv[argc - 1],
2485f4a2713aSLionel Sambuc       argv + num_unsaved_files + 1 + NumFilenames,
2486f4a2713aSLionel Sambuc       argc - num_unsaved_files - 2 - NumFilenames,
2487f4a2713aSLionel Sambuc       unsaved_files,
2488*0a6a1f1dSLionel Sambuc       Repeats > 1 ? 0 : num_unsaved_files, getDefaultParsingOptions(), &TU);
2489f4a2713aSLionel Sambuc 
2490*0a6a1f1dSLionel Sambuc   if (Err != CXError_Success) {
2491f4a2713aSLionel Sambuc     fprintf(stderr, "unable to parse input\n");
2492*0a6a1f1dSLionel Sambuc     describeLibclangFailure(Err);
2493*0a6a1f1dSLionel Sambuc     clang_disposeTranslationUnit(TU);
2494f4a2713aSLionel Sambuc     return -1;
2495f4a2713aSLionel Sambuc   }
2496f4a2713aSLionel Sambuc 
2497f4a2713aSLionel Sambuc   if (checkForErrors(TU) != 0)
2498f4a2713aSLionel Sambuc     return -1;
2499f4a2713aSLionel Sambuc 
2500f4a2713aSLionel Sambuc   for (I = 0; I != Repeats; ++I) {
2501*0a6a1f1dSLionel Sambuc     if (Repeats > 1) {
2502*0a6a1f1dSLionel Sambuc       Err = clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2503*0a6a1f1dSLionel Sambuc                                          clang_defaultReparseOptions(TU));
2504*0a6a1f1dSLionel Sambuc       if (Err != CXError_Success) {
2505*0a6a1f1dSLionel Sambuc         describeLibclangFailure(Err);
2506f4a2713aSLionel Sambuc         clang_disposeTranslationUnit(TU);
2507f4a2713aSLionel Sambuc         return 1;
2508f4a2713aSLionel Sambuc       }
2509*0a6a1f1dSLionel Sambuc     }
2510f4a2713aSLionel Sambuc 
2511f4a2713aSLionel Sambuc     if (checkForErrors(TU) != 0)
2512f4a2713aSLionel Sambuc       return -1;
2513f4a2713aSLionel Sambuc 
2514f4a2713aSLionel Sambuc     for (FI = 0; FI < NumFilenames; ++FI) {
2515f4a2713aSLionel Sambuc       CXFile file = clang_getFile(TU, Filenames[FI]);
2516f4a2713aSLionel Sambuc       if (!file)
2517f4a2713aSLionel Sambuc         continue;
2518f4a2713aSLionel Sambuc 
2519f4a2713aSLionel Sambuc       if (checkForErrors(TU) != 0)
2520f4a2713aSLionel Sambuc         return -1;
2521f4a2713aSLionel Sambuc 
2522f4a2713aSLionel Sambuc       if (I + 1 == Repeats) {
2523f4a2713aSLionel Sambuc         CXCursorAndRangeVisitor visitor = { 0, findFileIncludesVisit };
2524f4a2713aSLionel Sambuc         clang_findIncludesInFile(TU, file, visitor);
2525f4a2713aSLionel Sambuc 
2526f4a2713aSLionel Sambuc         if (checkForErrors(TU) != 0)
2527f4a2713aSLionel Sambuc           return -1;
2528f4a2713aSLionel Sambuc       }
2529f4a2713aSLionel Sambuc     }
2530f4a2713aSLionel Sambuc   }
2531f4a2713aSLionel Sambuc 
2532f4a2713aSLionel Sambuc   PrintDiagnostics(TU);
2533f4a2713aSLionel Sambuc   clang_disposeTranslationUnit(TU);
2534f4a2713aSLionel Sambuc   clang_disposeIndex(CIdx);
2535f4a2713aSLionel Sambuc   free((void *)Filenames);
2536f4a2713aSLionel Sambuc   free_remapped_files(unsaved_files, num_unsaved_files);
2537f4a2713aSLionel Sambuc   return 0;
2538f4a2713aSLionel Sambuc }
2539f4a2713aSLionel Sambuc 
2540f4a2713aSLionel Sambuc #define MAX_IMPORTED_ASTFILES 200
2541f4a2713aSLionel Sambuc 
2542f4a2713aSLionel Sambuc typedef struct {
2543f4a2713aSLionel Sambuc   char **filenames;
2544f4a2713aSLionel Sambuc   unsigned num_files;
2545f4a2713aSLionel Sambuc } ImportedASTFilesData;
2546f4a2713aSLionel Sambuc 
importedASTs_create()2547f4a2713aSLionel Sambuc static ImportedASTFilesData *importedASTs_create() {
2548f4a2713aSLionel Sambuc   ImportedASTFilesData *p;
2549f4a2713aSLionel Sambuc   p = malloc(sizeof(ImportedASTFilesData));
2550f4a2713aSLionel Sambuc   p->filenames = malloc(MAX_IMPORTED_ASTFILES * sizeof(const char *));
2551f4a2713aSLionel Sambuc   p->num_files = 0;
2552f4a2713aSLionel Sambuc   return p;
2553f4a2713aSLionel Sambuc }
2554f4a2713aSLionel Sambuc 
importedASTs_dispose(ImportedASTFilesData * p)2555f4a2713aSLionel Sambuc static void importedASTs_dispose(ImportedASTFilesData *p) {
2556f4a2713aSLionel Sambuc   unsigned i;
2557f4a2713aSLionel Sambuc   if (!p)
2558f4a2713aSLionel Sambuc     return;
2559f4a2713aSLionel Sambuc 
2560f4a2713aSLionel Sambuc   for (i = 0; i < p->num_files; ++i)
2561f4a2713aSLionel Sambuc     free(p->filenames[i]);
2562f4a2713aSLionel Sambuc   free(p->filenames);
2563f4a2713aSLionel Sambuc   free(p);
2564f4a2713aSLionel Sambuc }
2565f4a2713aSLionel Sambuc 
importedASTS_insert(ImportedASTFilesData * p,const char * file)2566f4a2713aSLionel Sambuc static void importedASTS_insert(ImportedASTFilesData *p, const char *file) {
2567f4a2713aSLionel Sambuc   unsigned i;
2568f4a2713aSLionel Sambuc   assert(p && file);
2569f4a2713aSLionel Sambuc   for (i = 0; i < p->num_files; ++i)
2570f4a2713aSLionel Sambuc     if (strcmp(file, p->filenames[i]) == 0)
2571f4a2713aSLionel Sambuc       return;
2572f4a2713aSLionel Sambuc   assert(p->num_files + 1 < MAX_IMPORTED_ASTFILES);
2573f4a2713aSLionel Sambuc   p->filenames[p->num_files++] = strdup(file);
2574f4a2713aSLionel Sambuc }
2575f4a2713aSLionel Sambuc 
2576*0a6a1f1dSLionel Sambuc typedef struct IndexDataStringList_ {
2577*0a6a1f1dSLionel Sambuc   struct IndexDataStringList_ *next;
2578*0a6a1f1dSLionel Sambuc   char data[1]; /* Dynamically sized. */
2579*0a6a1f1dSLionel Sambuc } IndexDataStringList;
2580*0a6a1f1dSLionel Sambuc 
2581f4a2713aSLionel Sambuc typedef struct {
2582f4a2713aSLionel Sambuc   const char *check_prefix;
2583f4a2713aSLionel Sambuc   int first_check_printed;
2584f4a2713aSLionel Sambuc   int fail_for_error;
2585f4a2713aSLionel Sambuc   int abort;
2586f4a2713aSLionel Sambuc   const char *main_filename;
2587f4a2713aSLionel Sambuc   ImportedASTFilesData *importedASTs;
2588*0a6a1f1dSLionel Sambuc   IndexDataStringList *strings;
2589*0a6a1f1dSLionel Sambuc   CXTranslationUnit TU;
2590f4a2713aSLionel Sambuc } IndexData;
2591f4a2713aSLionel Sambuc 
free_client_data(IndexData * index_data)2592*0a6a1f1dSLionel Sambuc static void free_client_data(IndexData *index_data) {
2593*0a6a1f1dSLionel Sambuc   IndexDataStringList *node = index_data->strings;
2594*0a6a1f1dSLionel Sambuc   while (node) {
2595*0a6a1f1dSLionel Sambuc     IndexDataStringList *next = node->next;
2596*0a6a1f1dSLionel Sambuc     free(node);
2597*0a6a1f1dSLionel Sambuc     node = next;
2598*0a6a1f1dSLionel Sambuc   }
2599*0a6a1f1dSLionel Sambuc   index_data->strings = NULL;
2600*0a6a1f1dSLionel Sambuc }
2601*0a6a1f1dSLionel Sambuc 
printCheck(IndexData * data)2602f4a2713aSLionel Sambuc static void printCheck(IndexData *data) {
2603f4a2713aSLionel Sambuc   if (data->check_prefix) {
2604f4a2713aSLionel Sambuc     if (data->first_check_printed) {
2605f4a2713aSLionel Sambuc       printf("// %s-NEXT: ", data->check_prefix);
2606f4a2713aSLionel Sambuc     } else {
2607f4a2713aSLionel Sambuc       printf("// %s     : ", data->check_prefix);
2608f4a2713aSLionel Sambuc       data->first_check_printed = 1;
2609f4a2713aSLionel Sambuc     }
2610f4a2713aSLionel Sambuc   }
2611f4a2713aSLionel Sambuc }
2612f4a2713aSLionel Sambuc 
printCXIndexFile(CXIdxClientFile file)2613f4a2713aSLionel Sambuc static void printCXIndexFile(CXIdxClientFile file) {
2614f4a2713aSLionel Sambuc   CXString filename = clang_getFileName((CXFile)file);
2615f4a2713aSLionel Sambuc   printf("%s", clang_getCString(filename));
2616f4a2713aSLionel Sambuc   clang_disposeString(filename);
2617f4a2713aSLionel Sambuc }
2618f4a2713aSLionel Sambuc 
printCXIndexLoc(CXIdxLoc loc,CXClientData client_data)2619f4a2713aSLionel Sambuc static void printCXIndexLoc(CXIdxLoc loc, CXClientData client_data) {
2620f4a2713aSLionel Sambuc   IndexData *index_data;
2621f4a2713aSLionel Sambuc   CXString filename;
2622f4a2713aSLionel Sambuc   const char *cname;
2623f4a2713aSLionel Sambuc   CXIdxClientFile file;
2624f4a2713aSLionel Sambuc   unsigned line, column;
2625f4a2713aSLionel Sambuc   int isMainFile;
2626f4a2713aSLionel Sambuc 
2627f4a2713aSLionel Sambuc   index_data = (IndexData *)client_data;
2628f4a2713aSLionel Sambuc   clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
2629f4a2713aSLionel Sambuc   if (line == 0) {
2630f4a2713aSLionel Sambuc     printf("<invalid>");
2631f4a2713aSLionel Sambuc     return;
2632f4a2713aSLionel Sambuc   }
2633f4a2713aSLionel Sambuc   if (!file) {
2634f4a2713aSLionel Sambuc     printf("<no idxfile>");
2635f4a2713aSLionel Sambuc     return;
2636f4a2713aSLionel Sambuc   }
2637f4a2713aSLionel Sambuc   filename = clang_getFileName((CXFile)file);
2638f4a2713aSLionel Sambuc   cname = clang_getCString(filename);
2639f4a2713aSLionel Sambuc   if (strcmp(cname, index_data->main_filename) == 0)
2640f4a2713aSLionel Sambuc     isMainFile = 1;
2641f4a2713aSLionel Sambuc   else
2642f4a2713aSLionel Sambuc     isMainFile = 0;
2643f4a2713aSLionel Sambuc   clang_disposeString(filename);
2644f4a2713aSLionel Sambuc 
2645f4a2713aSLionel Sambuc   if (!isMainFile) {
2646f4a2713aSLionel Sambuc     printCXIndexFile(file);
2647f4a2713aSLionel Sambuc     printf(":");
2648f4a2713aSLionel Sambuc   }
2649f4a2713aSLionel Sambuc   printf("%d:%d", line, column);
2650f4a2713aSLionel Sambuc }
2651f4a2713aSLionel Sambuc 
digitCount(unsigned val)2652f4a2713aSLionel Sambuc static unsigned digitCount(unsigned val) {
2653f4a2713aSLionel Sambuc   unsigned c = 1;
2654f4a2713aSLionel Sambuc   while (1) {
2655f4a2713aSLionel Sambuc     if (val < 10)
2656f4a2713aSLionel Sambuc       return c;
2657f4a2713aSLionel Sambuc     ++c;
2658f4a2713aSLionel Sambuc     val /= 10;
2659f4a2713aSLionel Sambuc   }
2660f4a2713aSLionel Sambuc }
2661f4a2713aSLionel Sambuc 
makeClientContainer(CXClientData * client_data,const CXIdxEntityInfo * info,CXIdxLoc loc)2662*0a6a1f1dSLionel Sambuc static CXIdxClientContainer makeClientContainer(CXClientData *client_data,
2663*0a6a1f1dSLionel Sambuc                                                 const CXIdxEntityInfo *info,
2664f4a2713aSLionel Sambuc                                                 CXIdxLoc loc) {
2665*0a6a1f1dSLionel Sambuc   IndexData *index_data;
2666*0a6a1f1dSLionel Sambuc   IndexDataStringList *node;
2667f4a2713aSLionel Sambuc   const char *name;
2668f4a2713aSLionel Sambuc   char *newStr;
2669f4a2713aSLionel Sambuc   CXIdxClientFile file;
2670f4a2713aSLionel Sambuc   unsigned line, column;
2671f4a2713aSLionel Sambuc 
2672f4a2713aSLionel Sambuc   name = info->name;
2673f4a2713aSLionel Sambuc   if (!name)
2674f4a2713aSLionel Sambuc     name = "<anon-tag>";
2675f4a2713aSLionel Sambuc 
2676f4a2713aSLionel Sambuc   clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
2677*0a6a1f1dSLionel Sambuc 
2678*0a6a1f1dSLionel Sambuc   node =
2679*0a6a1f1dSLionel Sambuc       (IndexDataStringList *)malloc(sizeof(IndexDataStringList) + strlen(name) +
2680*0a6a1f1dSLionel Sambuc                                     digitCount(line) + digitCount(column) + 2);
2681*0a6a1f1dSLionel Sambuc   newStr = node->data;
2682f4a2713aSLionel Sambuc   sprintf(newStr, "%s:%d:%d", name, line, column);
2683*0a6a1f1dSLionel Sambuc 
2684*0a6a1f1dSLionel Sambuc   /* Remember string so it can be freed later. */
2685*0a6a1f1dSLionel Sambuc   index_data = (IndexData *)client_data;
2686*0a6a1f1dSLionel Sambuc   node->next = index_data->strings;
2687*0a6a1f1dSLionel Sambuc   index_data->strings = node;
2688*0a6a1f1dSLionel Sambuc 
2689f4a2713aSLionel Sambuc   return (CXIdxClientContainer)newStr;
2690f4a2713aSLionel Sambuc }
2691f4a2713aSLionel Sambuc 
printCXIndexContainer(const CXIdxContainerInfo * info)2692f4a2713aSLionel Sambuc static void printCXIndexContainer(const CXIdxContainerInfo *info) {
2693f4a2713aSLionel Sambuc   CXIdxClientContainer container;
2694f4a2713aSLionel Sambuc   container = clang_index_getClientContainer(info);
2695f4a2713aSLionel Sambuc   if (!container)
2696f4a2713aSLionel Sambuc     printf("[<<NULL>>]");
2697f4a2713aSLionel Sambuc   else
2698f4a2713aSLionel Sambuc     printf("[%s]", (const char *)container);
2699f4a2713aSLionel Sambuc }
2700f4a2713aSLionel Sambuc 
getEntityKindString(CXIdxEntityKind kind)2701f4a2713aSLionel Sambuc static const char *getEntityKindString(CXIdxEntityKind kind) {
2702f4a2713aSLionel Sambuc   switch (kind) {
2703f4a2713aSLionel Sambuc   case CXIdxEntity_Unexposed: return "<<UNEXPOSED>>";
2704f4a2713aSLionel Sambuc   case CXIdxEntity_Typedef: return "typedef";
2705f4a2713aSLionel Sambuc   case CXIdxEntity_Function: return "function";
2706f4a2713aSLionel Sambuc   case CXIdxEntity_Variable: return "variable";
2707f4a2713aSLionel Sambuc   case CXIdxEntity_Field: return "field";
2708f4a2713aSLionel Sambuc   case CXIdxEntity_EnumConstant: return "enumerator";
2709f4a2713aSLionel Sambuc   case CXIdxEntity_ObjCClass: return "objc-class";
2710f4a2713aSLionel Sambuc   case CXIdxEntity_ObjCProtocol: return "objc-protocol";
2711f4a2713aSLionel Sambuc   case CXIdxEntity_ObjCCategory: return "objc-category";
2712f4a2713aSLionel Sambuc   case CXIdxEntity_ObjCInstanceMethod: return "objc-instance-method";
2713f4a2713aSLionel Sambuc   case CXIdxEntity_ObjCClassMethod: return "objc-class-method";
2714f4a2713aSLionel Sambuc   case CXIdxEntity_ObjCProperty: return "objc-property";
2715f4a2713aSLionel Sambuc   case CXIdxEntity_ObjCIvar: return "objc-ivar";
2716f4a2713aSLionel Sambuc   case CXIdxEntity_Enum: return "enum";
2717f4a2713aSLionel Sambuc   case CXIdxEntity_Struct: return "struct";
2718f4a2713aSLionel Sambuc   case CXIdxEntity_Union: return "union";
2719f4a2713aSLionel Sambuc   case CXIdxEntity_CXXClass: return "c++-class";
2720f4a2713aSLionel Sambuc   case CXIdxEntity_CXXNamespace: return "namespace";
2721f4a2713aSLionel Sambuc   case CXIdxEntity_CXXNamespaceAlias: return "namespace-alias";
2722f4a2713aSLionel Sambuc   case CXIdxEntity_CXXStaticVariable: return "c++-static-var";
2723f4a2713aSLionel Sambuc   case CXIdxEntity_CXXStaticMethod: return "c++-static-method";
2724f4a2713aSLionel Sambuc   case CXIdxEntity_CXXInstanceMethod: return "c++-instance-method";
2725f4a2713aSLionel Sambuc   case CXIdxEntity_CXXConstructor: return "constructor";
2726f4a2713aSLionel Sambuc   case CXIdxEntity_CXXDestructor: return "destructor";
2727f4a2713aSLionel Sambuc   case CXIdxEntity_CXXConversionFunction: return "conversion-func";
2728f4a2713aSLionel Sambuc   case CXIdxEntity_CXXTypeAlias: return "type-alias";
2729f4a2713aSLionel Sambuc   case CXIdxEntity_CXXInterface: return "c++-__interface";
2730f4a2713aSLionel Sambuc   }
2731f4a2713aSLionel Sambuc   assert(0 && "Garbage entity kind");
2732f4a2713aSLionel Sambuc   return 0;
2733f4a2713aSLionel Sambuc }
2734f4a2713aSLionel Sambuc 
getEntityTemplateKindString(CXIdxEntityCXXTemplateKind kind)2735f4a2713aSLionel Sambuc static const char *getEntityTemplateKindString(CXIdxEntityCXXTemplateKind kind) {
2736f4a2713aSLionel Sambuc   switch (kind) {
2737f4a2713aSLionel Sambuc   case CXIdxEntity_NonTemplate: return "";
2738f4a2713aSLionel Sambuc   case CXIdxEntity_Template: return "-template";
2739f4a2713aSLionel Sambuc   case CXIdxEntity_TemplatePartialSpecialization:
2740f4a2713aSLionel Sambuc     return "-template-partial-spec";
2741f4a2713aSLionel Sambuc   case CXIdxEntity_TemplateSpecialization: return "-template-spec";
2742f4a2713aSLionel Sambuc   }
2743f4a2713aSLionel Sambuc   assert(0 && "Garbage entity kind");
2744f4a2713aSLionel Sambuc   return 0;
2745f4a2713aSLionel Sambuc }
2746f4a2713aSLionel Sambuc 
getEntityLanguageString(CXIdxEntityLanguage kind)2747f4a2713aSLionel Sambuc static const char *getEntityLanguageString(CXIdxEntityLanguage kind) {
2748f4a2713aSLionel Sambuc   switch (kind) {
2749f4a2713aSLionel Sambuc   case CXIdxEntityLang_None: return "<none>";
2750f4a2713aSLionel Sambuc   case CXIdxEntityLang_C: return "C";
2751f4a2713aSLionel Sambuc   case CXIdxEntityLang_ObjC: return "ObjC";
2752f4a2713aSLionel Sambuc   case CXIdxEntityLang_CXX: return "C++";
2753f4a2713aSLionel Sambuc   }
2754f4a2713aSLionel Sambuc   assert(0 && "Garbage language kind");
2755f4a2713aSLionel Sambuc   return 0;
2756f4a2713aSLionel Sambuc }
2757f4a2713aSLionel Sambuc 
printEntityInfo(const char * cb,CXClientData client_data,const CXIdxEntityInfo * info)2758f4a2713aSLionel Sambuc static void printEntityInfo(const char *cb,
2759f4a2713aSLionel Sambuc                             CXClientData client_data,
2760f4a2713aSLionel Sambuc                             const CXIdxEntityInfo *info) {
2761f4a2713aSLionel Sambuc   const char *name;
2762f4a2713aSLionel Sambuc   IndexData *index_data;
2763f4a2713aSLionel Sambuc   unsigned i;
2764f4a2713aSLionel Sambuc   index_data = (IndexData *)client_data;
2765f4a2713aSLionel Sambuc   printCheck(index_data);
2766f4a2713aSLionel Sambuc 
2767f4a2713aSLionel Sambuc   if (!info) {
2768f4a2713aSLionel Sambuc     printf("%s: <<NULL>>", cb);
2769f4a2713aSLionel Sambuc     return;
2770f4a2713aSLionel Sambuc   }
2771f4a2713aSLionel Sambuc 
2772f4a2713aSLionel Sambuc   name = info->name;
2773f4a2713aSLionel Sambuc   if (!name)
2774f4a2713aSLionel Sambuc     name = "<anon-tag>";
2775f4a2713aSLionel Sambuc 
2776f4a2713aSLionel Sambuc   printf("%s: kind: %s%s", cb, getEntityKindString(info->kind),
2777f4a2713aSLionel Sambuc          getEntityTemplateKindString(info->templateKind));
2778f4a2713aSLionel Sambuc   printf(" | name: %s", name);
2779f4a2713aSLionel Sambuc   printf(" | USR: %s", info->USR);
2780f4a2713aSLionel Sambuc   printf(" | lang: %s", getEntityLanguageString(info->lang));
2781f4a2713aSLionel Sambuc 
2782f4a2713aSLionel Sambuc   for (i = 0; i != info->numAttributes; ++i) {
2783f4a2713aSLionel Sambuc     const CXIdxAttrInfo *Attr = info->attributes[i];
2784f4a2713aSLionel Sambuc     printf("     <attribute>: ");
2785f4a2713aSLionel Sambuc     PrintCursor(Attr->cursor, NULL);
2786f4a2713aSLionel Sambuc   }
2787f4a2713aSLionel Sambuc }
2788f4a2713aSLionel Sambuc 
printBaseClassInfo(CXClientData client_data,const CXIdxBaseClassInfo * info)2789f4a2713aSLionel Sambuc static void printBaseClassInfo(CXClientData client_data,
2790f4a2713aSLionel Sambuc                                const CXIdxBaseClassInfo *info) {
2791f4a2713aSLionel Sambuc   printEntityInfo("     <base>", client_data, info->base);
2792f4a2713aSLionel Sambuc   printf(" | cursor: ");
2793f4a2713aSLionel Sambuc   PrintCursor(info->cursor, NULL);
2794f4a2713aSLionel Sambuc   printf(" | loc: ");
2795f4a2713aSLionel Sambuc   printCXIndexLoc(info->loc, client_data);
2796f4a2713aSLionel Sambuc }
2797f4a2713aSLionel Sambuc 
printProtocolList(const CXIdxObjCProtocolRefListInfo * ProtoInfo,CXClientData client_data)2798f4a2713aSLionel Sambuc static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo,
2799f4a2713aSLionel Sambuc                               CXClientData client_data) {
2800f4a2713aSLionel Sambuc   unsigned i;
2801f4a2713aSLionel Sambuc   for (i = 0; i < ProtoInfo->numProtocols; ++i) {
2802f4a2713aSLionel Sambuc     printEntityInfo("     <protocol>", client_data,
2803f4a2713aSLionel Sambuc                     ProtoInfo->protocols[i]->protocol);
2804f4a2713aSLionel Sambuc     printf(" | cursor: ");
2805f4a2713aSLionel Sambuc     PrintCursor(ProtoInfo->protocols[i]->cursor, NULL);
2806f4a2713aSLionel Sambuc     printf(" | loc: ");
2807f4a2713aSLionel Sambuc     printCXIndexLoc(ProtoInfo->protocols[i]->loc, client_data);
2808f4a2713aSLionel Sambuc     printf("\n");
2809f4a2713aSLionel Sambuc   }
2810f4a2713aSLionel Sambuc }
2811f4a2713aSLionel Sambuc 
index_diagnostic(CXClientData client_data,CXDiagnosticSet diagSet,void * reserved)2812f4a2713aSLionel Sambuc static void index_diagnostic(CXClientData client_data,
2813f4a2713aSLionel Sambuc                              CXDiagnosticSet diagSet, void *reserved) {
2814f4a2713aSLionel Sambuc   CXString str;
2815f4a2713aSLionel Sambuc   const char *cstr;
2816f4a2713aSLionel Sambuc   unsigned numDiags, i;
2817f4a2713aSLionel Sambuc   CXDiagnostic diag;
2818f4a2713aSLionel Sambuc   IndexData *index_data;
2819f4a2713aSLionel Sambuc   index_data = (IndexData *)client_data;
2820f4a2713aSLionel Sambuc   printCheck(index_data);
2821f4a2713aSLionel Sambuc 
2822f4a2713aSLionel Sambuc   numDiags = clang_getNumDiagnosticsInSet(diagSet);
2823f4a2713aSLionel Sambuc   for (i = 0; i != numDiags; ++i) {
2824f4a2713aSLionel Sambuc     diag = clang_getDiagnosticInSet(diagSet, i);
2825f4a2713aSLionel Sambuc     str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions());
2826f4a2713aSLionel Sambuc     cstr = clang_getCString(str);
2827f4a2713aSLionel Sambuc     printf("[diagnostic]: %s\n", cstr);
2828f4a2713aSLionel Sambuc     clang_disposeString(str);
2829f4a2713aSLionel Sambuc 
2830f4a2713aSLionel Sambuc     if (getenv("CINDEXTEST_FAILONERROR") &&
2831f4a2713aSLionel Sambuc         clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) {
2832f4a2713aSLionel Sambuc       index_data->fail_for_error = 1;
2833f4a2713aSLionel Sambuc     }
2834f4a2713aSLionel Sambuc   }
2835f4a2713aSLionel Sambuc }
2836f4a2713aSLionel Sambuc 
index_enteredMainFile(CXClientData client_data,CXFile file,void * reserved)2837f4a2713aSLionel Sambuc static CXIdxClientFile index_enteredMainFile(CXClientData client_data,
2838f4a2713aSLionel Sambuc                                        CXFile file, void *reserved) {
2839f4a2713aSLionel Sambuc   IndexData *index_data;
2840f4a2713aSLionel Sambuc   CXString filename;
2841f4a2713aSLionel Sambuc 
2842f4a2713aSLionel Sambuc   index_data = (IndexData *)client_data;
2843f4a2713aSLionel Sambuc   printCheck(index_data);
2844f4a2713aSLionel Sambuc 
2845f4a2713aSLionel Sambuc   filename = clang_getFileName(file);
2846f4a2713aSLionel Sambuc   index_data->main_filename = clang_getCString(filename);
2847f4a2713aSLionel Sambuc   clang_disposeString(filename);
2848f4a2713aSLionel Sambuc 
2849f4a2713aSLionel Sambuc   printf("[enteredMainFile]: ");
2850f4a2713aSLionel Sambuc   printCXIndexFile((CXIdxClientFile)file);
2851f4a2713aSLionel Sambuc   printf("\n");
2852f4a2713aSLionel Sambuc 
2853f4a2713aSLionel Sambuc   return (CXIdxClientFile)file;
2854f4a2713aSLionel Sambuc }
2855f4a2713aSLionel Sambuc 
index_ppIncludedFile(CXClientData client_data,const CXIdxIncludedFileInfo * info)2856f4a2713aSLionel Sambuc static CXIdxClientFile index_ppIncludedFile(CXClientData client_data,
2857f4a2713aSLionel Sambuc                                             const CXIdxIncludedFileInfo *info) {
2858f4a2713aSLionel Sambuc   IndexData *index_data;
2859*0a6a1f1dSLionel Sambuc   CXModule Mod;
2860f4a2713aSLionel Sambuc   index_data = (IndexData *)client_data;
2861f4a2713aSLionel Sambuc   printCheck(index_data);
2862f4a2713aSLionel Sambuc 
2863f4a2713aSLionel Sambuc   printf("[ppIncludedFile]: ");
2864f4a2713aSLionel Sambuc   printCXIndexFile((CXIdxClientFile)info->file);
2865f4a2713aSLionel Sambuc   printf(" | name: \"%s\"", info->filename);
2866f4a2713aSLionel Sambuc   printf(" | hash loc: ");
2867f4a2713aSLionel Sambuc   printCXIndexLoc(info->hashLoc, client_data);
2868*0a6a1f1dSLionel Sambuc   printf(" | isImport: %d | isAngled: %d | isModule: %d",
2869f4a2713aSLionel Sambuc          info->isImport, info->isAngled, info->isModuleImport);
2870f4a2713aSLionel Sambuc 
2871*0a6a1f1dSLionel Sambuc   Mod = clang_getModuleForFile(index_data->TU, (CXFile)info->file);
2872*0a6a1f1dSLionel Sambuc   if (Mod) {
2873*0a6a1f1dSLionel Sambuc     CXString str = clang_Module_getFullName(Mod);
2874*0a6a1f1dSLionel Sambuc     const char *cstr = clang_getCString(str);
2875*0a6a1f1dSLionel Sambuc     printf(" | module: %s", cstr);
2876*0a6a1f1dSLionel Sambuc     clang_disposeString(str);
2877*0a6a1f1dSLionel Sambuc   }
2878*0a6a1f1dSLionel Sambuc 
2879*0a6a1f1dSLionel Sambuc   printf("\n");
2880*0a6a1f1dSLionel Sambuc 
2881f4a2713aSLionel Sambuc   return (CXIdxClientFile)info->file;
2882f4a2713aSLionel Sambuc }
2883f4a2713aSLionel Sambuc 
index_importedASTFile(CXClientData client_data,const CXIdxImportedASTFileInfo * info)2884f4a2713aSLionel Sambuc static CXIdxClientFile index_importedASTFile(CXClientData client_data,
2885f4a2713aSLionel Sambuc                                          const CXIdxImportedASTFileInfo *info) {
2886f4a2713aSLionel Sambuc   IndexData *index_data;
2887f4a2713aSLionel Sambuc   index_data = (IndexData *)client_data;
2888f4a2713aSLionel Sambuc   printCheck(index_data);
2889f4a2713aSLionel Sambuc 
2890f4a2713aSLionel Sambuc   if (index_data->importedASTs) {
2891f4a2713aSLionel Sambuc     CXString filename = clang_getFileName(info->file);
2892f4a2713aSLionel Sambuc     importedASTS_insert(index_data->importedASTs, clang_getCString(filename));
2893f4a2713aSLionel Sambuc     clang_disposeString(filename);
2894f4a2713aSLionel Sambuc   }
2895f4a2713aSLionel Sambuc 
2896f4a2713aSLionel Sambuc   printf("[importedASTFile]: ");
2897f4a2713aSLionel Sambuc   printCXIndexFile((CXIdxClientFile)info->file);
2898f4a2713aSLionel Sambuc   if (info->module) {
2899f4a2713aSLionel Sambuc     CXString name = clang_Module_getFullName(info->module);
2900f4a2713aSLionel Sambuc     printf(" | loc: ");
2901f4a2713aSLionel Sambuc     printCXIndexLoc(info->loc, client_data);
2902f4a2713aSLionel Sambuc     printf(" | name: \"%s\"", clang_getCString(name));
2903f4a2713aSLionel Sambuc     printf(" | isImplicit: %d\n", info->isImplicit);
2904f4a2713aSLionel Sambuc     clang_disposeString(name);
2905f4a2713aSLionel Sambuc   } else {
2906f4a2713aSLionel Sambuc     /* PCH file, the rest are not relevant. */
2907f4a2713aSLionel Sambuc     printf("\n");
2908f4a2713aSLionel Sambuc   }
2909f4a2713aSLionel Sambuc 
2910f4a2713aSLionel Sambuc   return (CXIdxClientFile)info->file;
2911f4a2713aSLionel Sambuc }
2912f4a2713aSLionel Sambuc 
2913*0a6a1f1dSLionel Sambuc static CXIdxClientContainer
index_startedTranslationUnit(CXClientData client_data,void * reserved)2914*0a6a1f1dSLionel Sambuc index_startedTranslationUnit(CXClientData client_data, void *reserved) {
2915f4a2713aSLionel Sambuc   IndexData *index_data;
2916f4a2713aSLionel Sambuc   index_data = (IndexData *)client_data;
2917f4a2713aSLionel Sambuc   printCheck(index_data);
2918f4a2713aSLionel Sambuc 
2919f4a2713aSLionel Sambuc   printf("[startedTranslationUnit]\n");
2920f4a2713aSLionel Sambuc   return (CXIdxClientContainer)"TU";
2921f4a2713aSLionel Sambuc }
2922f4a2713aSLionel Sambuc 
index_indexDeclaration(CXClientData client_data,const CXIdxDeclInfo * info)2923f4a2713aSLionel Sambuc static void index_indexDeclaration(CXClientData client_data,
2924f4a2713aSLionel Sambuc                                    const CXIdxDeclInfo *info) {
2925f4a2713aSLionel Sambuc   IndexData *index_data;
2926f4a2713aSLionel Sambuc   const CXIdxObjCCategoryDeclInfo *CatInfo;
2927f4a2713aSLionel Sambuc   const CXIdxObjCInterfaceDeclInfo *InterInfo;
2928f4a2713aSLionel Sambuc   const CXIdxObjCProtocolRefListInfo *ProtoInfo;
2929f4a2713aSLionel Sambuc   const CXIdxObjCPropertyDeclInfo *PropInfo;
2930f4a2713aSLionel Sambuc   const CXIdxCXXClassDeclInfo *CXXClassInfo;
2931f4a2713aSLionel Sambuc   unsigned i;
2932f4a2713aSLionel Sambuc   index_data = (IndexData *)client_data;
2933f4a2713aSLionel Sambuc 
2934f4a2713aSLionel Sambuc   printEntityInfo("[indexDeclaration]", client_data, info->entityInfo);
2935f4a2713aSLionel Sambuc   printf(" | cursor: ");
2936f4a2713aSLionel Sambuc   PrintCursor(info->cursor, NULL);
2937f4a2713aSLionel Sambuc   printf(" | loc: ");
2938f4a2713aSLionel Sambuc   printCXIndexLoc(info->loc, client_data);
2939f4a2713aSLionel Sambuc   printf(" | semantic-container: ");
2940f4a2713aSLionel Sambuc   printCXIndexContainer(info->semanticContainer);
2941f4a2713aSLionel Sambuc   printf(" | lexical-container: ");
2942f4a2713aSLionel Sambuc   printCXIndexContainer(info->lexicalContainer);
2943f4a2713aSLionel Sambuc   printf(" | isRedecl: %d", info->isRedeclaration);
2944f4a2713aSLionel Sambuc   printf(" | isDef: %d", info->isDefinition);
2945f4a2713aSLionel Sambuc   if (info->flags & CXIdxDeclFlag_Skipped) {
2946f4a2713aSLionel Sambuc     assert(!info->isContainer);
2947f4a2713aSLionel Sambuc     printf(" | isContainer: skipped");
2948f4a2713aSLionel Sambuc   } else {
2949f4a2713aSLionel Sambuc     printf(" | isContainer: %d", info->isContainer);
2950f4a2713aSLionel Sambuc   }
2951f4a2713aSLionel Sambuc   printf(" | isImplicit: %d\n", info->isImplicit);
2952f4a2713aSLionel Sambuc 
2953f4a2713aSLionel Sambuc   for (i = 0; i != info->numAttributes; ++i) {
2954f4a2713aSLionel Sambuc     const CXIdxAttrInfo *Attr = info->attributes[i];
2955f4a2713aSLionel Sambuc     printf("     <attribute>: ");
2956f4a2713aSLionel Sambuc     PrintCursor(Attr->cursor, NULL);
2957f4a2713aSLionel Sambuc     printf("\n");
2958f4a2713aSLionel Sambuc   }
2959f4a2713aSLionel Sambuc 
2960f4a2713aSLionel Sambuc   if (clang_index_isEntityObjCContainerKind(info->entityInfo->kind)) {
2961f4a2713aSLionel Sambuc     const char *kindName = 0;
2962f4a2713aSLionel Sambuc     CXIdxObjCContainerKind K = clang_index_getObjCContainerDeclInfo(info)->kind;
2963f4a2713aSLionel Sambuc     switch (K) {
2964f4a2713aSLionel Sambuc     case CXIdxObjCContainer_ForwardRef:
2965f4a2713aSLionel Sambuc       kindName = "forward-ref"; break;
2966f4a2713aSLionel Sambuc     case CXIdxObjCContainer_Interface:
2967f4a2713aSLionel Sambuc       kindName = "interface"; break;
2968f4a2713aSLionel Sambuc     case CXIdxObjCContainer_Implementation:
2969f4a2713aSLionel Sambuc       kindName = "implementation"; break;
2970f4a2713aSLionel Sambuc     }
2971f4a2713aSLionel Sambuc     printCheck(index_data);
2972f4a2713aSLionel Sambuc     printf("     <ObjCContainerInfo>: kind: %s\n", kindName);
2973f4a2713aSLionel Sambuc   }
2974f4a2713aSLionel Sambuc 
2975f4a2713aSLionel Sambuc   if ((CatInfo = clang_index_getObjCCategoryDeclInfo(info))) {
2976f4a2713aSLionel Sambuc     printEntityInfo("     <ObjCCategoryInfo>: class", client_data,
2977f4a2713aSLionel Sambuc                     CatInfo->objcClass);
2978f4a2713aSLionel Sambuc     printf(" | cursor: ");
2979f4a2713aSLionel Sambuc     PrintCursor(CatInfo->classCursor, NULL);
2980f4a2713aSLionel Sambuc     printf(" | loc: ");
2981f4a2713aSLionel Sambuc     printCXIndexLoc(CatInfo->classLoc, client_data);
2982f4a2713aSLionel Sambuc     printf("\n");
2983f4a2713aSLionel Sambuc   }
2984f4a2713aSLionel Sambuc 
2985f4a2713aSLionel Sambuc   if ((InterInfo = clang_index_getObjCInterfaceDeclInfo(info))) {
2986f4a2713aSLionel Sambuc     if (InterInfo->superInfo) {
2987f4a2713aSLionel Sambuc       printBaseClassInfo(client_data, InterInfo->superInfo);
2988f4a2713aSLionel Sambuc       printf("\n");
2989f4a2713aSLionel Sambuc     }
2990f4a2713aSLionel Sambuc   }
2991f4a2713aSLionel Sambuc 
2992f4a2713aSLionel Sambuc   if ((ProtoInfo = clang_index_getObjCProtocolRefListInfo(info))) {
2993f4a2713aSLionel Sambuc     printProtocolList(ProtoInfo, client_data);
2994f4a2713aSLionel Sambuc   }
2995f4a2713aSLionel Sambuc 
2996f4a2713aSLionel Sambuc   if ((PropInfo = clang_index_getObjCPropertyDeclInfo(info))) {
2997f4a2713aSLionel Sambuc     if (PropInfo->getter) {
2998f4a2713aSLionel Sambuc       printEntityInfo("     <getter>", client_data, PropInfo->getter);
2999f4a2713aSLionel Sambuc       printf("\n");
3000f4a2713aSLionel Sambuc     }
3001f4a2713aSLionel Sambuc     if (PropInfo->setter) {
3002f4a2713aSLionel Sambuc       printEntityInfo("     <setter>", client_data, PropInfo->setter);
3003f4a2713aSLionel Sambuc       printf("\n");
3004f4a2713aSLionel Sambuc     }
3005f4a2713aSLionel Sambuc   }
3006f4a2713aSLionel Sambuc 
3007f4a2713aSLionel Sambuc   if ((CXXClassInfo = clang_index_getCXXClassDeclInfo(info))) {
3008f4a2713aSLionel Sambuc     for (i = 0; i != CXXClassInfo->numBases; ++i) {
3009f4a2713aSLionel Sambuc       printBaseClassInfo(client_data, CXXClassInfo->bases[i]);
3010f4a2713aSLionel Sambuc       printf("\n");
3011f4a2713aSLionel Sambuc     }
3012f4a2713aSLionel Sambuc   }
3013f4a2713aSLionel Sambuc 
3014f4a2713aSLionel Sambuc   if (info->declAsContainer)
3015*0a6a1f1dSLionel Sambuc     clang_index_setClientContainer(
3016*0a6a1f1dSLionel Sambuc         info->declAsContainer,
3017*0a6a1f1dSLionel Sambuc         makeClientContainer(client_data, info->entityInfo, info->loc));
3018f4a2713aSLionel Sambuc }
3019f4a2713aSLionel Sambuc 
index_indexEntityReference(CXClientData client_data,const CXIdxEntityRefInfo * info)3020f4a2713aSLionel Sambuc static void index_indexEntityReference(CXClientData client_data,
3021f4a2713aSLionel Sambuc                                        const CXIdxEntityRefInfo *info) {
3022*0a6a1f1dSLionel Sambuc   printEntityInfo("[indexEntityReference]", client_data,
3023*0a6a1f1dSLionel Sambuc                   info->referencedEntity);
3024f4a2713aSLionel Sambuc   printf(" | cursor: ");
3025f4a2713aSLionel Sambuc   PrintCursor(info->cursor, NULL);
3026f4a2713aSLionel Sambuc   printf(" | loc: ");
3027f4a2713aSLionel Sambuc   printCXIndexLoc(info->loc, client_data);
3028f4a2713aSLionel Sambuc   printEntityInfo(" | <parent>:", client_data, info->parentEntity);
3029f4a2713aSLionel Sambuc   printf(" | container: ");
3030f4a2713aSLionel Sambuc   printCXIndexContainer(info->container);
3031f4a2713aSLionel Sambuc   printf(" | refkind: ");
3032f4a2713aSLionel Sambuc   switch (info->kind) {
3033f4a2713aSLionel Sambuc   case CXIdxEntityRef_Direct: printf("direct"); break;
3034f4a2713aSLionel Sambuc   case CXIdxEntityRef_Implicit: printf("implicit"); break;
3035f4a2713aSLionel Sambuc   }
3036f4a2713aSLionel Sambuc   printf("\n");
3037f4a2713aSLionel Sambuc }
3038f4a2713aSLionel Sambuc 
index_abortQuery(CXClientData client_data,void * reserved)3039f4a2713aSLionel Sambuc static int index_abortQuery(CXClientData client_data, void *reserved) {
3040f4a2713aSLionel Sambuc   IndexData *index_data;
3041f4a2713aSLionel Sambuc   index_data = (IndexData *)client_data;
3042f4a2713aSLionel Sambuc   return index_data->abort;
3043f4a2713aSLionel Sambuc }
3044f4a2713aSLionel Sambuc 
3045f4a2713aSLionel Sambuc static IndexerCallbacks IndexCB = {
3046f4a2713aSLionel Sambuc   index_abortQuery,
3047f4a2713aSLionel Sambuc   index_diagnostic,
3048f4a2713aSLionel Sambuc   index_enteredMainFile,
3049f4a2713aSLionel Sambuc   index_ppIncludedFile,
3050f4a2713aSLionel Sambuc   index_importedASTFile,
3051f4a2713aSLionel Sambuc   index_startedTranslationUnit,
3052f4a2713aSLionel Sambuc   index_indexDeclaration,
3053f4a2713aSLionel Sambuc   index_indexEntityReference
3054f4a2713aSLionel Sambuc };
3055f4a2713aSLionel Sambuc 
getIndexOptions(void)3056f4a2713aSLionel Sambuc static unsigned getIndexOptions(void) {
3057f4a2713aSLionel Sambuc   unsigned index_opts;
3058f4a2713aSLionel Sambuc   index_opts = 0;
3059f4a2713aSLionel Sambuc   if (getenv("CINDEXTEST_SUPPRESSREFS"))
3060f4a2713aSLionel Sambuc     index_opts |= CXIndexOpt_SuppressRedundantRefs;
3061f4a2713aSLionel Sambuc   if (getenv("CINDEXTEST_INDEXLOCALSYMBOLS"))
3062f4a2713aSLionel Sambuc     index_opts |= CXIndexOpt_IndexFunctionLocalSymbols;
3063f4a2713aSLionel Sambuc   if (!getenv("CINDEXTEST_DISABLE_SKIPPARSEDBODIES"))
3064f4a2713aSLionel Sambuc     index_opts |= CXIndexOpt_SkipParsedBodiesInSession;
3065f4a2713aSLionel Sambuc 
3066f4a2713aSLionel Sambuc   return index_opts;
3067f4a2713aSLionel Sambuc }
3068f4a2713aSLionel Sambuc 
index_compile_args(int num_args,const char ** args,CXIndexAction idxAction,ImportedASTFilesData * importedASTs,const char * check_prefix)3069f4a2713aSLionel Sambuc static int index_compile_args(int num_args, const char **args,
3070f4a2713aSLionel Sambuc                               CXIndexAction idxAction,
3071f4a2713aSLionel Sambuc                               ImportedASTFilesData *importedASTs,
3072f4a2713aSLionel Sambuc                               const char *check_prefix) {
3073f4a2713aSLionel Sambuc   IndexData index_data;
3074f4a2713aSLionel Sambuc   unsigned index_opts;
3075f4a2713aSLionel Sambuc   int result;
3076f4a2713aSLionel Sambuc 
3077f4a2713aSLionel Sambuc   if (num_args == 0) {
3078f4a2713aSLionel Sambuc     fprintf(stderr, "no compiler arguments\n");
3079f4a2713aSLionel Sambuc     return -1;
3080f4a2713aSLionel Sambuc   }
3081f4a2713aSLionel Sambuc 
3082f4a2713aSLionel Sambuc   index_data.check_prefix = check_prefix;
3083f4a2713aSLionel Sambuc   index_data.first_check_printed = 0;
3084f4a2713aSLionel Sambuc   index_data.fail_for_error = 0;
3085f4a2713aSLionel Sambuc   index_data.abort = 0;
3086f4a2713aSLionel Sambuc   index_data.main_filename = "";
3087f4a2713aSLionel Sambuc   index_data.importedASTs = importedASTs;
3088*0a6a1f1dSLionel Sambuc   index_data.strings = NULL;
3089*0a6a1f1dSLionel Sambuc   index_data.TU = NULL;
3090f4a2713aSLionel Sambuc 
3091f4a2713aSLionel Sambuc   index_opts = getIndexOptions();
3092f4a2713aSLionel Sambuc   result = clang_indexSourceFile(idxAction, &index_data,
3093f4a2713aSLionel Sambuc                                  &IndexCB,sizeof(IndexCB), index_opts,
3094f4a2713aSLionel Sambuc                                  0, args, num_args, 0, 0, 0,
3095f4a2713aSLionel Sambuc                                  getDefaultParsingOptions());
3096*0a6a1f1dSLionel Sambuc   if (result != CXError_Success)
3097*0a6a1f1dSLionel Sambuc     describeLibclangFailure(result);
3098*0a6a1f1dSLionel Sambuc 
3099f4a2713aSLionel Sambuc   if (index_data.fail_for_error)
3100f4a2713aSLionel Sambuc     result = -1;
3101f4a2713aSLionel Sambuc 
3102*0a6a1f1dSLionel Sambuc   free_client_data(&index_data);
3103f4a2713aSLionel Sambuc   return result;
3104f4a2713aSLionel Sambuc }
3105f4a2713aSLionel Sambuc 
index_ast_file(const char * ast_file,CXIndex Idx,CXIndexAction idxAction,ImportedASTFilesData * importedASTs,const char * check_prefix)3106f4a2713aSLionel Sambuc static int index_ast_file(const char *ast_file,
3107f4a2713aSLionel Sambuc                           CXIndex Idx,
3108f4a2713aSLionel Sambuc                           CXIndexAction idxAction,
3109f4a2713aSLionel Sambuc                           ImportedASTFilesData *importedASTs,
3110f4a2713aSLionel Sambuc                           const char *check_prefix) {
3111f4a2713aSLionel Sambuc   CXTranslationUnit TU;
3112f4a2713aSLionel Sambuc   IndexData index_data;
3113f4a2713aSLionel Sambuc   unsigned index_opts;
3114f4a2713aSLionel Sambuc   int result;
3115f4a2713aSLionel Sambuc 
3116f4a2713aSLionel Sambuc   if (!CreateTranslationUnit(Idx, ast_file, &TU))
3117f4a2713aSLionel Sambuc     return -1;
3118f4a2713aSLionel Sambuc 
3119f4a2713aSLionel Sambuc   index_data.check_prefix = check_prefix;
3120f4a2713aSLionel Sambuc   index_data.first_check_printed = 0;
3121f4a2713aSLionel Sambuc   index_data.fail_for_error = 0;
3122f4a2713aSLionel Sambuc   index_data.abort = 0;
3123f4a2713aSLionel Sambuc   index_data.main_filename = "";
3124f4a2713aSLionel Sambuc   index_data.importedASTs = importedASTs;
3125*0a6a1f1dSLionel Sambuc   index_data.strings = NULL;
3126*0a6a1f1dSLionel Sambuc   index_data.TU = TU;
3127f4a2713aSLionel Sambuc 
3128f4a2713aSLionel Sambuc   index_opts = getIndexOptions();
3129f4a2713aSLionel Sambuc   result = clang_indexTranslationUnit(idxAction, &index_data,
3130f4a2713aSLionel Sambuc                                       &IndexCB,sizeof(IndexCB),
3131f4a2713aSLionel Sambuc                                       index_opts, TU);
3132f4a2713aSLionel Sambuc   if (index_data.fail_for_error)
3133f4a2713aSLionel Sambuc     result = -1;
3134f4a2713aSLionel Sambuc 
3135f4a2713aSLionel Sambuc   clang_disposeTranslationUnit(TU);
3136*0a6a1f1dSLionel Sambuc   free_client_data(&index_data);
3137f4a2713aSLionel Sambuc   return result;
3138f4a2713aSLionel Sambuc }
3139f4a2713aSLionel Sambuc 
index_file(int argc,const char ** argv,int full)3140f4a2713aSLionel Sambuc static int index_file(int argc, const char **argv, int full) {
3141f4a2713aSLionel Sambuc   const char *check_prefix;
3142f4a2713aSLionel Sambuc   CXIndex Idx;
3143f4a2713aSLionel Sambuc   CXIndexAction idxAction;
3144f4a2713aSLionel Sambuc   ImportedASTFilesData *importedASTs;
3145f4a2713aSLionel Sambuc   int result;
3146f4a2713aSLionel Sambuc 
3147f4a2713aSLionel Sambuc   check_prefix = 0;
3148f4a2713aSLionel Sambuc   if (argc > 0) {
3149f4a2713aSLionel Sambuc     if (strstr(argv[0], "-check-prefix=") == argv[0]) {
3150f4a2713aSLionel Sambuc       check_prefix = argv[0] + strlen("-check-prefix=");
3151f4a2713aSLionel Sambuc       ++argv;
3152f4a2713aSLionel Sambuc       --argc;
3153f4a2713aSLionel Sambuc     }
3154f4a2713aSLionel Sambuc   }
3155f4a2713aSLionel Sambuc 
3156f4a2713aSLionel Sambuc   if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
3157f4a2713aSLionel Sambuc                                 /* displayDiagnostics=*/1))) {
3158f4a2713aSLionel Sambuc     fprintf(stderr, "Could not create Index\n");
3159f4a2713aSLionel Sambuc     return 1;
3160f4a2713aSLionel Sambuc   }
3161f4a2713aSLionel Sambuc   idxAction = clang_IndexAction_create(Idx);
3162f4a2713aSLionel Sambuc   importedASTs = 0;
3163f4a2713aSLionel Sambuc   if (full)
3164f4a2713aSLionel Sambuc     importedASTs = importedASTs_create();
3165f4a2713aSLionel Sambuc 
3166f4a2713aSLionel Sambuc   result = index_compile_args(argc, argv, idxAction, importedASTs, check_prefix);
3167f4a2713aSLionel Sambuc   if (result != 0)
3168f4a2713aSLionel Sambuc     goto finished;
3169f4a2713aSLionel Sambuc 
3170f4a2713aSLionel Sambuc   if (full) {
3171f4a2713aSLionel Sambuc     unsigned i;
3172f4a2713aSLionel Sambuc     for (i = 0; i < importedASTs->num_files && result == 0; ++i) {
3173f4a2713aSLionel Sambuc       result = index_ast_file(importedASTs->filenames[i], Idx, idxAction,
3174f4a2713aSLionel Sambuc                               importedASTs, check_prefix);
3175f4a2713aSLionel Sambuc     }
3176f4a2713aSLionel Sambuc   }
3177f4a2713aSLionel Sambuc 
3178f4a2713aSLionel Sambuc finished:
3179f4a2713aSLionel Sambuc   importedASTs_dispose(importedASTs);
3180f4a2713aSLionel Sambuc   clang_IndexAction_dispose(idxAction);
3181f4a2713aSLionel Sambuc   clang_disposeIndex(Idx);
3182f4a2713aSLionel Sambuc   return result;
3183f4a2713aSLionel Sambuc }
3184f4a2713aSLionel Sambuc 
index_tu(int argc,const char ** argv)3185f4a2713aSLionel Sambuc static int index_tu(int argc, const char **argv) {
3186f4a2713aSLionel Sambuc   const char *check_prefix;
3187f4a2713aSLionel Sambuc   CXIndex Idx;
3188f4a2713aSLionel Sambuc   CXIndexAction idxAction;
3189f4a2713aSLionel Sambuc   int result;
3190f4a2713aSLionel Sambuc 
3191f4a2713aSLionel Sambuc   check_prefix = 0;
3192f4a2713aSLionel Sambuc   if (argc > 0) {
3193f4a2713aSLionel Sambuc     if (strstr(argv[0], "-check-prefix=") == argv[0]) {
3194f4a2713aSLionel Sambuc       check_prefix = argv[0] + strlen("-check-prefix=");
3195f4a2713aSLionel Sambuc       ++argv;
3196f4a2713aSLionel Sambuc       --argc;
3197f4a2713aSLionel Sambuc     }
3198f4a2713aSLionel Sambuc   }
3199f4a2713aSLionel Sambuc 
3200f4a2713aSLionel Sambuc   if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
3201f4a2713aSLionel Sambuc                                 /* displayDiagnostics=*/1))) {
3202f4a2713aSLionel Sambuc     fprintf(stderr, "Could not create Index\n");
3203f4a2713aSLionel Sambuc     return 1;
3204f4a2713aSLionel Sambuc   }
3205f4a2713aSLionel Sambuc   idxAction = clang_IndexAction_create(Idx);
3206f4a2713aSLionel Sambuc 
3207f4a2713aSLionel Sambuc   result = index_ast_file(argv[0], Idx, idxAction,
3208f4a2713aSLionel Sambuc                           /*importedASTs=*/0, check_prefix);
3209f4a2713aSLionel Sambuc 
3210f4a2713aSLionel Sambuc   clang_IndexAction_dispose(idxAction);
3211f4a2713aSLionel Sambuc   clang_disposeIndex(Idx);
3212f4a2713aSLionel Sambuc   return result;
3213f4a2713aSLionel Sambuc }
3214f4a2713aSLionel Sambuc 
index_compile_db(int argc,const char ** argv)3215f4a2713aSLionel Sambuc static int index_compile_db(int argc, const char **argv) {
3216f4a2713aSLionel Sambuc   const char *check_prefix;
3217f4a2713aSLionel Sambuc   CXIndex Idx;
3218f4a2713aSLionel Sambuc   CXIndexAction idxAction;
3219f4a2713aSLionel Sambuc   int errorCode = 0;
3220f4a2713aSLionel Sambuc 
3221f4a2713aSLionel Sambuc   check_prefix = 0;
3222f4a2713aSLionel Sambuc   if (argc > 0) {
3223f4a2713aSLionel Sambuc     if (strstr(argv[0], "-check-prefix=") == argv[0]) {
3224f4a2713aSLionel Sambuc       check_prefix = argv[0] + strlen("-check-prefix=");
3225f4a2713aSLionel Sambuc       ++argv;
3226f4a2713aSLionel Sambuc       --argc;
3227f4a2713aSLionel Sambuc     }
3228f4a2713aSLionel Sambuc   }
3229f4a2713aSLionel Sambuc 
3230f4a2713aSLionel Sambuc   if (argc == 0) {
3231f4a2713aSLionel Sambuc     fprintf(stderr, "no compilation database\n");
3232f4a2713aSLionel Sambuc     return -1;
3233f4a2713aSLionel Sambuc   }
3234f4a2713aSLionel Sambuc 
3235f4a2713aSLionel Sambuc   if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
3236f4a2713aSLionel Sambuc                                 /* displayDiagnostics=*/1))) {
3237f4a2713aSLionel Sambuc     fprintf(stderr, "Could not create Index\n");
3238f4a2713aSLionel Sambuc     return 1;
3239f4a2713aSLionel Sambuc   }
3240f4a2713aSLionel Sambuc   idxAction = clang_IndexAction_create(Idx);
3241f4a2713aSLionel Sambuc 
3242f4a2713aSLionel Sambuc   {
3243f4a2713aSLionel Sambuc     const char *database = argv[0];
3244f4a2713aSLionel Sambuc     CXCompilationDatabase db = 0;
3245f4a2713aSLionel Sambuc     CXCompileCommands CCmds = 0;
3246f4a2713aSLionel Sambuc     CXCompileCommand CCmd;
3247f4a2713aSLionel Sambuc     CXCompilationDatabase_Error ec;
3248f4a2713aSLionel Sambuc     CXString wd;
3249f4a2713aSLionel Sambuc #define MAX_COMPILE_ARGS 512
3250f4a2713aSLionel Sambuc     CXString cxargs[MAX_COMPILE_ARGS];
3251f4a2713aSLionel Sambuc     const char *args[MAX_COMPILE_ARGS];
3252f4a2713aSLionel Sambuc     char *tmp;
3253f4a2713aSLionel Sambuc     unsigned len;
3254f4a2713aSLionel Sambuc     char *buildDir;
3255f4a2713aSLionel Sambuc     int i, a, numCmds, numArgs;
3256f4a2713aSLionel Sambuc 
3257f4a2713aSLionel Sambuc     len = strlen(database);
3258f4a2713aSLionel Sambuc     tmp = (char *) malloc(len+1);
3259f4a2713aSLionel Sambuc     memcpy(tmp, database, len+1);
3260f4a2713aSLionel Sambuc     buildDir = dirname(tmp);
3261f4a2713aSLionel Sambuc 
3262f4a2713aSLionel Sambuc     db = clang_CompilationDatabase_fromDirectory(buildDir, &ec);
3263f4a2713aSLionel Sambuc 
3264f4a2713aSLionel Sambuc     if (db) {
3265f4a2713aSLionel Sambuc 
3266f4a2713aSLionel Sambuc       if (ec!=CXCompilationDatabase_NoError) {
3267f4a2713aSLionel Sambuc         printf("unexpected error %d code while loading compilation database\n", ec);
3268f4a2713aSLionel Sambuc         errorCode = -1;
3269f4a2713aSLionel Sambuc         goto cdb_end;
3270f4a2713aSLionel Sambuc       }
3271f4a2713aSLionel Sambuc 
3272f4a2713aSLionel Sambuc       if (chdir(buildDir) != 0) {
3273f4a2713aSLionel Sambuc         printf("Could not chdir to %s\n", buildDir);
3274f4a2713aSLionel Sambuc         errorCode = -1;
3275f4a2713aSLionel Sambuc         goto cdb_end;
3276f4a2713aSLionel Sambuc       }
3277f4a2713aSLionel Sambuc 
3278f4a2713aSLionel Sambuc       CCmds = clang_CompilationDatabase_getAllCompileCommands(db);
3279f4a2713aSLionel Sambuc       if (!CCmds) {
3280f4a2713aSLionel Sambuc         printf("compilation db is empty\n");
3281f4a2713aSLionel Sambuc         errorCode = -1;
3282f4a2713aSLionel Sambuc         goto cdb_end;
3283f4a2713aSLionel Sambuc       }
3284f4a2713aSLionel Sambuc 
3285f4a2713aSLionel Sambuc       numCmds = clang_CompileCommands_getSize(CCmds);
3286f4a2713aSLionel Sambuc 
3287f4a2713aSLionel Sambuc       if (numCmds==0) {
3288f4a2713aSLionel Sambuc         fprintf(stderr, "should not get an empty compileCommand set\n");
3289f4a2713aSLionel Sambuc         errorCode = -1;
3290f4a2713aSLionel Sambuc         goto cdb_end;
3291f4a2713aSLionel Sambuc       }
3292f4a2713aSLionel Sambuc 
3293f4a2713aSLionel Sambuc       for (i=0; i<numCmds && errorCode == 0; ++i) {
3294f4a2713aSLionel Sambuc         CCmd = clang_CompileCommands_getCommand(CCmds, i);
3295f4a2713aSLionel Sambuc 
3296f4a2713aSLionel Sambuc         wd = clang_CompileCommand_getDirectory(CCmd);
3297f4a2713aSLionel Sambuc         if (chdir(clang_getCString(wd)) != 0) {
3298f4a2713aSLionel Sambuc           printf("Could not chdir to %s\n", clang_getCString(wd));
3299f4a2713aSLionel Sambuc           errorCode = -1;
3300f4a2713aSLionel Sambuc           goto cdb_end;
3301f4a2713aSLionel Sambuc         }
3302f4a2713aSLionel Sambuc         clang_disposeString(wd);
3303f4a2713aSLionel Sambuc 
3304f4a2713aSLionel Sambuc         numArgs = clang_CompileCommand_getNumArgs(CCmd);
3305f4a2713aSLionel Sambuc         if (numArgs > MAX_COMPILE_ARGS){
3306f4a2713aSLionel Sambuc           fprintf(stderr, "got more compile arguments than maximum\n");
3307f4a2713aSLionel Sambuc           errorCode = -1;
3308f4a2713aSLionel Sambuc           goto cdb_end;
3309f4a2713aSLionel Sambuc         }
3310f4a2713aSLionel Sambuc         for (a=0; a<numArgs; ++a) {
3311f4a2713aSLionel Sambuc           cxargs[a] = clang_CompileCommand_getArg(CCmd, a);
3312f4a2713aSLionel Sambuc           args[a] = clang_getCString(cxargs[a]);
3313f4a2713aSLionel Sambuc         }
3314f4a2713aSLionel Sambuc 
3315f4a2713aSLionel Sambuc         errorCode = index_compile_args(numArgs, args, idxAction,
3316f4a2713aSLionel Sambuc                                        /*importedASTs=*/0, check_prefix);
3317f4a2713aSLionel Sambuc 
3318f4a2713aSLionel Sambuc         for (a=0; a<numArgs; ++a)
3319f4a2713aSLionel Sambuc           clang_disposeString(cxargs[a]);
3320f4a2713aSLionel Sambuc       }
3321f4a2713aSLionel Sambuc     } else {
3322f4a2713aSLionel Sambuc       printf("database loading failed with error code %d.\n", ec);
3323f4a2713aSLionel Sambuc       errorCode = -1;
3324f4a2713aSLionel Sambuc     }
3325f4a2713aSLionel Sambuc 
3326f4a2713aSLionel Sambuc   cdb_end:
3327f4a2713aSLionel Sambuc     clang_CompileCommands_dispose(CCmds);
3328f4a2713aSLionel Sambuc     clang_CompilationDatabase_dispose(db);
3329f4a2713aSLionel Sambuc     free(tmp);
3330f4a2713aSLionel Sambuc 
3331f4a2713aSLionel Sambuc   }
3332f4a2713aSLionel Sambuc 
3333f4a2713aSLionel Sambuc   clang_IndexAction_dispose(idxAction);
3334f4a2713aSLionel Sambuc   clang_disposeIndex(Idx);
3335f4a2713aSLionel Sambuc   return errorCode;
3336f4a2713aSLionel Sambuc }
3337f4a2713aSLionel Sambuc 
perform_token_annotation(int argc,const char ** argv)3338f4a2713aSLionel Sambuc int perform_token_annotation(int argc, const char **argv) {
3339f4a2713aSLionel Sambuc   const char *input = argv[1];
3340f4a2713aSLionel Sambuc   char *filename = 0;
3341f4a2713aSLionel Sambuc   unsigned line, second_line;
3342f4a2713aSLionel Sambuc   unsigned column, second_column;
3343f4a2713aSLionel Sambuc   CXIndex CIdx;
3344f4a2713aSLionel Sambuc   CXTranslationUnit TU = 0;
3345f4a2713aSLionel Sambuc   int errorCode;
3346f4a2713aSLionel Sambuc   struct CXUnsavedFile *unsaved_files = 0;
3347f4a2713aSLionel Sambuc   int num_unsaved_files = 0;
3348f4a2713aSLionel Sambuc   CXToken *tokens;
3349f4a2713aSLionel Sambuc   unsigned num_tokens;
3350f4a2713aSLionel Sambuc   CXSourceRange range;
3351f4a2713aSLionel Sambuc   CXSourceLocation startLoc, endLoc;
3352f4a2713aSLionel Sambuc   CXFile file = 0;
3353f4a2713aSLionel Sambuc   CXCursor *cursors = 0;
3354*0a6a1f1dSLionel Sambuc   CXSourceRangeList *skipped_ranges = 0;
3355*0a6a1f1dSLionel Sambuc   enum CXErrorCode Err;
3356f4a2713aSLionel Sambuc   unsigned i;
3357f4a2713aSLionel Sambuc 
3358f4a2713aSLionel Sambuc   input += strlen("-test-annotate-tokens=");
3359f4a2713aSLionel Sambuc   if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
3360f4a2713aSLionel Sambuc                                           &second_line, &second_column)))
3361f4a2713aSLionel Sambuc     return errorCode;
3362f4a2713aSLionel Sambuc 
3363f4a2713aSLionel Sambuc   if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) {
3364f4a2713aSLionel Sambuc     free(filename);
3365f4a2713aSLionel Sambuc     return -1;
3366f4a2713aSLionel Sambuc   }
3367f4a2713aSLionel Sambuc 
3368f4a2713aSLionel Sambuc   CIdx = clang_createIndex(0, 1);
3369*0a6a1f1dSLionel Sambuc   Err = clang_parseTranslationUnit2(CIdx, argv[argc - 1],
3370f4a2713aSLionel Sambuc                                     argv + num_unsaved_files + 2,
3371f4a2713aSLionel Sambuc                                     argc - num_unsaved_files - 3,
3372f4a2713aSLionel Sambuc                                     unsaved_files,
3373f4a2713aSLionel Sambuc                                     num_unsaved_files,
3374*0a6a1f1dSLionel Sambuc                                     getDefaultParsingOptions(), &TU);
3375*0a6a1f1dSLionel Sambuc   if (Err != CXError_Success) {
3376f4a2713aSLionel Sambuc     fprintf(stderr, "unable to parse input\n");
3377*0a6a1f1dSLionel Sambuc     describeLibclangFailure(Err);
3378f4a2713aSLionel Sambuc     clang_disposeIndex(CIdx);
3379f4a2713aSLionel Sambuc     free(filename);
3380f4a2713aSLionel Sambuc     free_remapped_files(unsaved_files, num_unsaved_files);
3381f4a2713aSLionel Sambuc     return -1;
3382f4a2713aSLionel Sambuc   }
3383f4a2713aSLionel Sambuc   errorCode = 0;
3384f4a2713aSLionel Sambuc 
3385f4a2713aSLionel Sambuc   if (checkForErrors(TU) != 0) {
3386f4a2713aSLionel Sambuc     errorCode = -1;
3387f4a2713aSLionel Sambuc     goto teardown;
3388f4a2713aSLionel Sambuc   }
3389f4a2713aSLionel Sambuc 
3390f4a2713aSLionel Sambuc   if (getenv("CINDEXTEST_EDITING")) {
3391f4a2713aSLionel Sambuc     for (i = 0; i < 5; ++i) {
3392*0a6a1f1dSLionel Sambuc       Err = clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
3393*0a6a1f1dSLionel Sambuc                                          clang_defaultReparseOptions(TU));
3394*0a6a1f1dSLionel Sambuc       if (Err != CXError_Success) {
3395f4a2713aSLionel Sambuc         fprintf(stderr, "Unable to reparse translation unit!\n");
3396*0a6a1f1dSLionel Sambuc         describeLibclangFailure(Err);
3397f4a2713aSLionel Sambuc         errorCode = -1;
3398f4a2713aSLionel Sambuc         goto teardown;
3399f4a2713aSLionel Sambuc       }
3400f4a2713aSLionel Sambuc     }
3401f4a2713aSLionel Sambuc   }
3402f4a2713aSLionel Sambuc 
3403f4a2713aSLionel Sambuc   if (checkForErrors(TU) != 0) {
3404f4a2713aSLionel Sambuc     errorCode = -1;
3405f4a2713aSLionel Sambuc     goto teardown;
3406f4a2713aSLionel Sambuc   }
3407f4a2713aSLionel Sambuc 
3408f4a2713aSLionel Sambuc   file = clang_getFile(TU, filename);
3409f4a2713aSLionel Sambuc   if (!file) {
3410f4a2713aSLionel Sambuc     fprintf(stderr, "file %s is not in this translation unit\n", filename);
3411f4a2713aSLionel Sambuc     errorCode = -1;
3412f4a2713aSLionel Sambuc     goto teardown;
3413f4a2713aSLionel Sambuc   }
3414f4a2713aSLionel Sambuc 
3415f4a2713aSLionel Sambuc   startLoc = clang_getLocation(TU, file, line, column);
3416f4a2713aSLionel Sambuc   if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
3417f4a2713aSLionel Sambuc     fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
3418f4a2713aSLionel Sambuc             column);
3419f4a2713aSLionel Sambuc     errorCode = -1;
3420f4a2713aSLionel Sambuc     goto teardown;
3421f4a2713aSLionel Sambuc   }
3422f4a2713aSLionel Sambuc 
3423f4a2713aSLionel Sambuc   endLoc = clang_getLocation(TU, file, second_line, second_column);
3424f4a2713aSLionel Sambuc   if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
3425f4a2713aSLionel Sambuc     fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
3426f4a2713aSLionel Sambuc             second_line, second_column);
3427f4a2713aSLionel Sambuc     errorCode = -1;
3428f4a2713aSLionel Sambuc     goto teardown;
3429f4a2713aSLionel Sambuc   }
3430f4a2713aSLionel Sambuc 
3431f4a2713aSLionel Sambuc   range = clang_getRange(startLoc, endLoc);
3432f4a2713aSLionel Sambuc   clang_tokenize(TU, range, &tokens, &num_tokens);
3433f4a2713aSLionel Sambuc 
3434f4a2713aSLionel Sambuc   if (checkForErrors(TU) != 0) {
3435f4a2713aSLionel Sambuc     errorCode = -1;
3436f4a2713aSLionel Sambuc     goto teardown;
3437f4a2713aSLionel Sambuc   }
3438f4a2713aSLionel Sambuc 
3439f4a2713aSLionel Sambuc   cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
3440f4a2713aSLionel Sambuc   clang_annotateTokens(TU, tokens, num_tokens, cursors);
3441f4a2713aSLionel Sambuc 
3442f4a2713aSLionel Sambuc   if (checkForErrors(TU) != 0) {
3443f4a2713aSLionel Sambuc     errorCode = -1;
3444f4a2713aSLionel Sambuc     goto teardown;
3445f4a2713aSLionel Sambuc   }
3446f4a2713aSLionel Sambuc 
3447*0a6a1f1dSLionel Sambuc   skipped_ranges = clang_getSkippedRanges(TU, file);
3448*0a6a1f1dSLionel Sambuc   for (i = 0; i != skipped_ranges->count; ++i) {
3449*0a6a1f1dSLionel Sambuc     unsigned start_line, start_column, end_line, end_column;
3450*0a6a1f1dSLionel Sambuc     clang_getSpellingLocation(clang_getRangeStart(skipped_ranges->ranges[i]),
3451*0a6a1f1dSLionel Sambuc                               0, &start_line, &start_column, 0);
3452*0a6a1f1dSLionel Sambuc     clang_getSpellingLocation(clang_getRangeEnd(skipped_ranges->ranges[i]),
3453*0a6a1f1dSLionel Sambuc                               0, &end_line, &end_column, 0);
3454*0a6a1f1dSLionel Sambuc     printf("Skipping: ");
3455*0a6a1f1dSLionel Sambuc     PrintExtent(stdout, start_line, start_column, end_line, end_column);
3456*0a6a1f1dSLionel Sambuc     printf("\n");
3457*0a6a1f1dSLionel Sambuc   }
3458*0a6a1f1dSLionel Sambuc   clang_disposeSourceRangeList(skipped_ranges);
3459*0a6a1f1dSLionel Sambuc 
3460f4a2713aSLionel Sambuc   for (i = 0; i != num_tokens; ++i) {
3461f4a2713aSLionel Sambuc     const char *kind = "<unknown>";
3462f4a2713aSLionel Sambuc     CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
3463f4a2713aSLionel Sambuc     CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
3464f4a2713aSLionel Sambuc     unsigned start_line, start_column, end_line, end_column;
3465f4a2713aSLionel Sambuc 
3466f4a2713aSLionel Sambuc     switch (clang_getTokenKind(tokens[i])) {
3467f4a2713aSLionel Sambuc     case CXToken_Punctuation: kind = "Punctuation"; break;
3468f4a2713aSLionel Sambuc     case CXToken_Keyword: kind = "Keyword"; break;
3469f4a2713aSLionel Sambuc     case CXToken_Identifier: kind = "Identifier"; break;
3470f4a2713aSLionel Sambuc     case CXToken_Literal: kind = "Literal"; break;
3471f4a2713aSLionel Sambuc     case CXToken_Comment: kind = "Comment"; break;
3472f4a2713aSLionel Sambuc     }
3473f4a2713aSLionel Sambuc     clang_getSpellingLocation(clang_getRangeStart(extent),
3474f4a2713aSLionel Sambuc                               0, &start_line, &start_column, 0);
3475f4a2713aSLionel Sambuc     clang_getSpellingLocation(clang_getRangeEnd(extent),
3476f4a2713aSLionel Sambuc                               0, &end_line, &end_column, 0);
3477f4a2713aSLionel Sambuc     printf("%s: \"%s\" ", kind, clang_getCString(spelling));
3478f4a2713aSLionel Sambuc     clang_disposeString(spelling);
3479f4a2713aSLionel Sambuc     PrintExtent(stdout, start_line, start_column, end_line, end_column);
3480f4a2713aSLionel Sambuc     if (!clang_isInvalid(cursors[i].kind)) {
3481f4a2713aSLionel Sambuc       printf(" ");
3482f4a2713aSLionel Sambuc       PrintCursor(cursors[i], NULL);
3483f4a2713aSLionel Sambuc     }
3484f4a2713aSLionel Sambuc     printf("\n");
3485f4a2713aSLionel Sambuc   }
3486f4a2713aSLionel Sambuc   free(cursors);
3487f4a2713aSLionel Sambuc   clang_disposeTokens(TU, tokens, num_tokens);
3488f4a2713aSLionel Sambuc 
3489f4a2713aSLionel Sambuc  teardown:
3490f4a2713aSLionel Sambuc   PrintDiagnostics(TU);
3491f4a2713aSLionel Sambuc   clang_disposeTranslationUnit(TU);
3492f4a2713aSLionel Sambuc   clang_disposeIndex(CIdx);
3493f4a2713aSLionel Sambuc   free(filename);
3494f4a2713aSLionel Sambuc   free_remapped_files(unsaved_files, num_unsaved_files);
3495f4a2713aSLionel Sambuc   return errorCode;
3496f4a2713aSLionel Sambuc }
3497f4a2713aSLionel Sambuc 
3498f4a2713aSLionel Sambuc static int
perform_test_compilation_db(const char * database,int argc,const char ** argv)3499f4a2713aSLionel Sambuc perform_test_compilation_db(const char *database, int argc, const char **argv) {
3500f4a2713aSLionel Sambuc   CXCompilationDatabase db;
3501f4a2713aSLionel Sambuc   CXCompileCommands CCmds;
3502f4a2713aSLionel Sambuc   CXCompileCommand CCmd;
3503f4a2713aSLionel Sambuc   CXCompilationDatabase_Error ec;
3504f4a2713aSLionel Sambuc   CXString wd;
3505f4a2713aSLionel Sambuc   CXString arg;
3506f4a2713aSLionel Sambuc   int errorCode = 0;
3507f4a2713aSLionel Sambuc   char *tmp;
3508f4a2713aSLionel Sambuc   unsigned len;
3509f4a2713aSLionel Sambuc   char *buildDir;
3510f4a2713aSLionel Sambuc   int i, j, a, numCmds, numArgs;
3511f4a2713aSLionel Sambuc 
3512f4a2713aSLionel Sambuc   len = strlen(database);
3513f4a2713aSLionel Sambuc   tmp = (char *) malloc(len+1);
3514f4a2713aSLionel Sambuc   memcpy(tmp, database, len+1);
3515f4a2713aSLionel Sambuc   buildDir = dirname(tmp);
3516f4a2713aSLionel Sambuc 
3517f4a2713aSLionel Sambuc   db = clang_CompilationDatabase_fromDirectory(buildDir, &ec);
3518f4a2713aSLionel Sambuc 
3519f4a2713aSLionel Sambuc   if (db) {
3520f4a2713aSLionel Sambuc 
3521f4a2713aSLionel Sambuc     if (ec!=CXCompilationDatabase_NoError) {
3522f4a2713aSLionel Sambuc       printf("unexpected error %d code while loading compilation database\n", ec);
3523f4a2713aSLionel Sambuc       errorCode = -1;
3524f4a2713aSLionel Sambuc       goto cdb_end;
3525f4a2713aSLionel Sambuc     }
3526f4a2713aSLionel Sambuc 
3527f4a2713aSLionel Sambuc     for (i=0; i<argc && errorCode==0; ) {
3528f4a2713aSLionel Sambuc       if (strcmp(argv[i],"lookup")==0){
3529f4a2713aSLionel Sambuc         CCmds = clang_CompilationDatabase_getCompileCommands(db, argv[i+1]);
3530f4a2713aSLionel Sambuc 
3531f4a2713aSLionel Sambuc         if (!CCmds) {
3532f4a2713aSLionel Sambuc           printf("file %s not found in compilation db\n", argv[i+1]);
3533f4a2713aSLionel Sambuc           errorCode = -1;
3534f4a2713aSLionel Sambuc           break;
3535f4a2713aSLionel Sambuc         }
3536f4a2713aSLionel Sambuc 
3537f4a2713aSLionel Sambuc         numCmds = clang_CompileCommands_getSize(CCmds);
3538f4a2713aSLionel Sambuc 
3539f4a2713aSLionel Sambuc         if (numCmds==0) {
3540f4a2713aSLionel Sambuc           fprintf(stderr, "should not get an empty compileCommand set for file"
3541f4a2713aSLionel Sambuc                           " '%s'\n", argv[i+1]);
3542f4a2713aSLionel Sambuc           errorCode = -1;
3543f4a2713aSLionel Sambuc           break;
3544f4a2713aSLionel Sambuc         }
3545f4a2713aSLionel Sambuc 
3546f4a2713aSLionel Sambuc         for (j=0; j<numCmds; ++j) {
3547f4a2713aSLionel Sambuc           CCmd = clang_CompileCommands_getCommand(CCmds, j);
3548f4a2713aSLionel Sambuc 
3549f4a2713aSLionel Sambuc           wd = clang_CompileCommand_getDirectory(CCmd);
3550f4a2713aSLionel Sambuc           printf("workdir:'%s'", clang_getCString(wd));
3551f4a2713aSLionel Sambuc           clang_disposeString(wd);
3552f4a2713aSLionel Sambuc 
3553f4a2713aSLionel Sambuc           printf(" cmdline:'");
3554f4a2713aSLionel Sambuc           numArgs = clang_CompileCommand_getNumArgs(CCmd);
3555f4a2713aSLionel Sambuc           for (a=0; a<numArgs; ++a) {
3556f4a2713aSLionel Sambuc             if (a) printf(" ");
3557f4a2713aSLionel Sambuc             arg = clang_CompileCommand_getArg(CCmd, a);
3558f4a2713aSLionel Sambuc             printf("%s", clang_getCString(arg));
3559f4a2713aSLionel Sambuc             clang_disposeString(arg);
3560f4a2713aSLionel Sambuc           }
3561f4a2713aSLionel Sambuc           printf("'\n");
3562f4a2713aSLionel Sambuc         }
3563f4a2713aSLionel Sambuc 
3564f4a2713aSLionel Sambuc         clang_CompileCommands_dispose(CCmds);
3565f4a2713aSLionel Sambuc 
3566f4a2713aSLionel Sambuc         i += 2;
3567f4a2713aSLionel Sambuc       }
3568f4a2713aSLionel Sambuc     }
3569f4a2713aSLionel Sambuc     clang_CompilationDatabase_dispose(db);
3570f4a2713aSLionel Sambuc   } else {
3571f4a2713aSLionel Sambuc     printf("database loading failed with error code %d.\n", ec);
3572f4a2713aSLionel Sambuc     errorCode = -1;
3573f4a2713aSLionel Sambuc   }
3574f4a2713aSLionel Sambuc 
3575f4a2713aSLionel Sambuc cdb_end:
3576f4a2713aSLionel Sambuc   free(tmp);
3577f4a2713aSLionel Sambuc 
3578f4a2713aSLionel Sambuc   return errorCode;
3579f4a2713aSLionel Sambuc }
3580f4a2713aSLionel Sambuc 
3581f4a2713aSLionel Sambuc /******************************************************************************/
3582f4a2713aSLionel Sambuc /* USR printing.                                                              */
3583f4a2713aSLionel Sambuc /******************************************************************************/
3584f4a2713aSLionel Sambuc 
insufficient_usr(const char * kind,const char * usage)3585f4a2713aSLionel Sambuc static int insufficient_usr(const char *kind, const char *usage) {
3586f4a2713aSLionel Sambuc   fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
3587f4a2713aSLionel Sambuc   return 1;
3588f4a2713aSLionel Sambuc }
3589f4a2713aSLionel Sambuc 
isUSR(const char * s)3590f4a2713aSLionel Sambuc static unsigned isUSR(const char *s) {
3591f4a2713aSLionel Sambuc   return s[0] == 'c' && s[1] == ':';
3592f4a2713aSLionel Sambuc }
3593f4a2713aSLionel Sambuc 
not_usr(const char * s,const char * arg)3594f4a2713aSLionel Sambuc static int not_usr(const char *s, const char *arg) {
3595f4a2713aSLionel Sambuc   fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
3596f4a2713aSLionel Sambuc   return 1;
3597f4a2713aSLionel Sambuc }
3598f4a2713aSLionel Sambuc 
print_usr(CXString usr)3599f4a2713aSLionel Sambuc static void print_usr(CXString usr) {
3600f4a2713aSLionel Sambuc   const char *s = clang_getCString(usr);
3601f4a2713aSLionel Sambuc   printf("%s\n", s);
3602f4a2713aSLionel Sambuc   clang_disposeString(usr);
3603f4a2713aSLionel Sambuc }
3604f4a2713aSLionel Sambuc 
display_usrs()3605f4a2713aSLionel Sambuc static void display_usrs() {
3606f4a2713aSLionel Sambuc   fprintf(stderr, "-print-usrs options:\n"
3607f4a2713aSLionel Sambuc         " ObjCCategory <class name> <category name>\n"
3608f4a2713aSLionel Sambuc         " ObjCClass <class name>\n"
3609f4a2713aSLionel Sambuc         " ObjCIvar <ivar name> <class USR>\n"
3610f4a2713aSLionel Sambuc         " ObjCMethod <selector> [0=class method|1=instance method] "
3611f4a2713aSLionel Sambuc             "<class USR>\n"
3612f4a2713aSLionel Sambuc           " ObjCProperty <property name> <class USR>\n"
3613f4a2713aSLionel Sambuc           " ObjCProtocol <protocol name>\n");
3614f4a2713aSLionel Sambuc }
3615f4a2713aSLionel Sambuc 
print_usrs(const char ** I,const char ** E)3616f4a2713aSLionel Sambuc int print_usrs(const char **I, const char **E) {
3617f4a2713aSLionel Sambuc   while (I != E) {
3618f4a2713aSLionel Sambuc     const char *kind = *I;
3619f4a2713aSLionel Sambuc     unsigned len = strlen(kind);
3620f4a2713aSLionel Sambuc     switch (len) {
3621f4a2713aSLionel Sambuc       case 8:
3622f4a2713aSLionel Sambuc         if (memcmp(kind, "ObjCIvar", 8) == 0) {
3623f4a2713aSLionel Sambuc           if (I + 2 >= E)
3624f4a2713aSLionel Sambuc             return insufficient_usr(kind, "<ivar name> <class USR>");
3625f4a2713aSLionel Sambuc           if (!isUSR(I[2]))
3626f4a2713aSLionel Sambuc             return not_usr("<class USR>", I[2]);
3627f4a2713aSLionel Sambuc           else {
3628f4a2713aSLionel Sambuc             CXString x;
3629f4a2713aSLionel Sambuc             x.data = (void*) I[2];
3630f4a2713aSLionel Sambuc             x.private_flags = 0;
3631f4a2713aSLionel Sambuc             print_usr(clang_constructUSR_ObjCIvar(I[1], x));
3632f4a2713aSLionel Sambuc           }
3633f4a2713aSLionel Sambuc 
3634f4a2713aSLionel Sambuc           I += 3;
3635f4a2713aSLionel Sambuc           continue;
3636f4a2713aSLionel Sambuc         }
3637f4a2713aSLionel Sambuc         break;
3638f4a2713aSLionel Sambuc       case 9:
3639f4a2713aSLionel Sambuc         if (memcmp(kind, "ObjCClass", 9) == 0) {
3640f4a2713aSLionel Sambuc           if (I + 1 >= E)
3641f4a2713aSLionel Sambuc             return insufficient_usr(kind, "<class name>");
3642f4a2713aSLionel Sambuc           print_usr(clang_constructUSR_ObjCClass(I[1]));
3643f4a2713aSLionel Sambuc           I += 2;
3644f4a2713aSLionel Sambuc           continue;
3645f4a2713aSLionel Sambuc         }
3646f4a2713aSLionel Sambuc         break;
3647f4a2713aSLionel Sambuc       case 10:
3648f4a2713aSLionel Sambuc         if (memcmp(kind, "ObjCMethod", 10) == 0) {
3649f4a2713aSLionel Sambuc           if (I + 3 >= E)
3650f4a2713aSLionel Sambuc             return insufficient_usr(kind, "<method selector> "
3651f4a2713aSLionel Sambuc                 "[0=class method|1=instance method] <class USR>");
3652f4a2713aSLionel Sambuc           if (!isUSR(I[3]))
3653f4a2713aSLionel Sambuc             return not_usr("<class USR>", I[3]);
3654f4a2713aSLionel Sambuc           else {
3655f4a2713aSLionel Sambuc             CXString x;
3656f4a2713aSLionel Sambuc             x.data = (void*) I[3];
3657f4a2713aSLionel Sambuc             x.private_flags = 0;
3658f4a2713aSLionel Sambuc             print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
3659f4a2713aSLionel Sambuc           }
3660f4a2713aSLionel Sambuc           I += 4;
3661f4a2713aSLionel Sambuc           continue;
3662f4a2713aSLionel Sambuc         }
3663f4a2713aSLionel Sambuc         break;
3664f4a2713aSLionel Sambuc       case 12:
3665f4a2713aSLionel Sambuc         if (memcmp(kind, "ObjCCategory", 12) == 0) {
3666f4a2713aSLionel Sambuc           if (I + 2 >= E)
3667f4a2713aSLionel Sambuc             return insufficient_usr(kind, "<class name> <category name>");
3668f4a2713aSLionel Sambuc           print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
3669f4a2713aSLionel Sambuc           I += 3;
3670f4a2713aSLionel Sambuc           continue;
3671f4a2713aSLionel Sambuc         }
3672f4a2713aSLionel Sambuc         if (memcmp(kind, "ObjCProtocol", 12) == 0) {
3673f4a2713aSLionel Sambuc           if (I + 1 >= E)
3674f4a2713aSLionel Sambuc             return insufficient_usr(kind, "<protocol name>");
3675f4a2713aSLionel Sambuc           print_usr(clang_constructUSR_ObjCProtocol(I[1]));
3676f4a2713aSLionel Sambuc           I += 2;
3677f4a2713aSLionel Sambuc           continue;
3678f4a2713aSLionel Sambuc         }
3679f4a2713aSLionel Sambuc         if (memcmp(kind, "ObjCProperty", 12) == 0) {
3680f4a2713aSLionel Sambuc           if (I + 2 >= E)
3681f4a2713aSLionel Sambuc             return insufficient_usr(kind, "<property name> <class USR>");
3682f4a2713aSLionel Sambuc           if (!isUSR(I[2]))
3683f4a2713aSLionel Sambuc             return not_usr("<class USR>", I[2]);
3684f4a2713aSLionel Sambuc           else {
3685f4a2713aSLionel Sambuc             CXString x;
3686f4a2713aSLionel Sambuc             x.data = (void*) I[2];
3687f4a2713aSLionel Sambuc             x.private_flags = 0;
3688f4a2713aSLionel Sambuc             print_usr(clang_constructUSR_ObjCProperty(I[1], x));
3689f4a2713aSLionel Sambuc           }
3690f4a2713aSLionel Sambuc           I += 3;
3691f4a2713aSLionel Sambuc           continue;
3692f4a2713aSLionel Sambuc         }
3693f4a2713aSLionel Sambuc         break;
3694f4a2713aSLionel Sambuc       default:
3695f4a2713aSLionel Sambuc         break;
3696f4a2713aSLionel Sambuc     }
3697f4a2713aSLionel Sambuc     break;
3698f4a2713aSLionel Sambuc   }
3699f4a2713aSLionel Sambuc 
3700f4a2713aSLionel Sambuc   if (I != E) {
3701f4a2713aSLionel Sambuc     fprintf(stderr, "Invalid USR kind: %s\n", *I);
3702f4a2713aSLionel Sambuc     display_usrs();
3703f4a2713aSLionel Sambuc     return 1;
3704f4a2713aSLionel Sambuc   }
3705f4a2713aSLionel Sambuc   return 0;
3706f4a2713aSLionel Sambuc }
3707f4a2713aSLionel Sambuc 
print_usrs_file(const char * file_name)3708f4a2713aSLionel Sambuc int print_usrs_file(const char *file_name) {
3709f4a2713aSLionel Sambuc   char line[2048];
3710f4a2713aSLionel Sambuc   const char *args[128];
3711f4a2713aSLionel Sambuc   unsigned numChars = 0;
3712f4a2713aSLionel Sambuc 
3713f4a2713aSLionel Sambuc   FILE *fp = fopen(file_name, "r");
3714f4a2713aSLionel Sambuc   if (!fp) {
3715f4a2713aSLionel Sambuc     fprintf(stderr, "error: cannot open '%s'\n", file_name);
3716f4a2713aSLionel Sambuc     return 1;
3717f4a2713aSLionel Sambuc   }
3718f4a2713aSLionel Sambuc 
3719f4a2713aSLionel Sambuc   /* This code is not really all that safe, but it works fine for testing. */
3720f4a2713aSLionel Sambuc   while (!feof(fp)) {
3721f4a2713aSLionel Sambuc     char c = fgetc(fp);
3722f4a2713aSLionel Sambuc     if (c == '\n') {
3723f4a2713aSLionel Sambuc       unsigned i = 0;
3724f4a2713aSLionel Sambuc       const char *s = 0;
3725f4a2713aSLionel Sambuc 
3726f4a2713aSLionel Sambuc       if (numChars == 0)
3727f4a2713aSLionel Sambuc         continue;
3728f4a2713aSLionel Sambuc 
3729f4a2713aSLionel Sambuc       line[numChars] = '\0';
3730f4a2713aSLionel Sambuc       numChars = 0;
3731f4a2713aSLionel Sambuc 
3732f4a2713aSLionel Sambuc       if (line[0] == '/' && line[1] == '/')
3733f4a2713aSLionel Sambuc         continue;
3734f4a2713aSLionel Sambuc 
3735f4a2713aSLionel Sambuc       s = strtok(line, " ");
3736f4a2713aSLionel Sambuc       while (s) {
3737f4a2713aSLionel Sambuc         args[i] = s;
3738f4a2713aSLionel Sambuc         ++i;
3739f4a2713aSLionel Sambuc         s = strtok(0, " ");
3740f4a2713aSLionel Sambuc       }
3741f4a2713aSLionel Sambuc       if (print_usrs(&args[0], &args[i]))
3742f4a2713aSLionel Sambuc         return 1;
3743f4a2713aSLionel Sambuc     }
3744f4a2713aSLionel Sambuc     else
3745f4a2713aSLionel Sambuc       line[numChars++] = c;
3746f4a2713aSLionel Sambuc   }
3747f4a2713aSLionel Sambuc 
3748f4a2713aSLionel Sambuc   fclose(fp);
3749f4a2713aSLionel Sambuc   return 0;
3750f4a2713aSLionel Sambuc }
3751f4a2713aSLionel Sambuc 
3752f4a2713aSLionel Sambuc /******************************************************************************/
3753f4a2713aSLionel Sambuc /* Command line processing.                                                   */
3754f4a2713aSLionel Sambuc /******************************************************************************/
write_pch_file(const char * filename,int argc,const char * argv[])3755f4a2713aSLionel Sambuc int write_pch_file(const char *filename, int argc, const char *argv[]) {
3756f4a2713aSLionel Sambuc   CXIndex Idx;
3757f4a2713aSLionel Sambuc   CXTranslationUnit TU;
3758f4a2713aSLionel Sambuc   struct CXUnsavedFile *unsaved_files = 0;
3759f4a2713aSLionel Sambuc   int num_unsaved_files = 0;
3760*0a6a1f1dSLionel Sambuc   enum CXErrorCode Err;
3761f4a2713aSLionel Sambuc   int result = 0;
3762f4a2713aSLionel Sambuc 
3763f4a2713aSLionel Sambuc   Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnostics=*/1);
3764f4a2713aSLionel Sambuc 
3765f4a2713aSLionel Sambuc   if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
3766f4a2713aSLionel Sambuc     clang_disposeIndex(Idx);
3767f4a2713aSLionel Sambuc     return -1;
3768f4a2713aSLionel Sambuc   }
3769f4a2713aSLionel Sambuc 
3770*0a6a1f1dSLionel Sambuc   Err = clang_parseTranslationUnit2(
3771*0a6a1f1dSLionel Sambuc       Idx, 0, argv + num_unsaved_files, argc - num_unsaved_files,
3772*0a6a1f1dSLionel Sambuc       unsaved_files, num_unsaved_files,
3773f4a2713aSLionel Sambuc       CXTranslationUnit_Incomplete |
3774f4a2713aSLionel Sambuc           CXTranslationUnit_DetailedPreprocessingRecord |
3775*0a6a1f1dSLionel Sambuc           CXTranslationUnit_ForSerialization,
3776*0a6a1f1dSLionel Sambuc       &TU);
3777*0a6a1f1dSLionel Sambuc   if (Err != CXError_Success) {
3778f4a2713aSLionel Sambuc     fprintf(stderr, "Unable to load translation unit!\n");
3779*0a6a1f1dSLionel Sambuc     describeLibclangFailure(Err);
3780f4a2713aSLionel Sambuc     free_remapped_files(unsaved_files, num_unsaved_files);
3781*0a6a1f1dSLionel Sambuc     clang_disposeTranslationUnit(TU);
3782f4a2713aSLionel Sambuc     clang_disposeIndex(Idx);
3783f4a2713aSLionel Sambuc     return 1;
3784f4a2713aSLionel Sambuc   }
3785f4a2713aSLionel Sambuc 
3786f4a2713aSLionel Sambuc   switch (clang_saveTranslationUnit(TU, filename,
3787f4a2713aSLionel Sambuc                                     clang_defaultSaveOptions(TU))) {
3788f4a2713aSLionel Sambuc   case CXSaveError_None:
3789f4a2713aSLionel Sambuc     break;
3790f4a2713aSLionel Sambuc 
3791f4a2713aSLionel Sambuc   case CXSaveError_TranslationErrors:
3792f4a2713aSLionel Sambuc     fprintf(stderr, "Unable to write PCH file %s: translation errors\n",
3793f4a2713aSLionel Sambuc             filename);
3794f4a2713aSLionel Sambuc     result = 2;
3795f4a2713aSLionel Sambuc     break;
3796f4a2713aSLionel Sambuc 
3797f4a2713aSLionel Sambuc   case CXSaveError_InvalidTU:
3798f4a2713aSLionel Sambuc     fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n",
3799f4a2713aSLionel Sambuc             filename);
3800f4a2713aSLionel Sambuc     result = 3;
3801f4a2713aSLionel Sambuc     break;
3802f4a2713aSLionel Sambuc 
3803f4a2713aSLionel Sambuc   case CXSaveError_Unknown:
3804f4a2713aSLionel Sambuc   default:
3805f4a2713aSLionel Sambuc     fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename);
3806f4a2713aSLionel Sambuc     result = 1;
3807f4a2713aSLionel Sambuc     break;
3808f4a2713aSLionel Sambuc   }
3809f4a2713aSLionel Sambuc 
3810f4a2713aSLionel Sambuc   clang_disposeTranslationUnit(TU);
3811f4a2713aSLionel Sambuc   free_remapped_files(unsaved_files, num_unsaved_files);
3812f4a2713aSLionel Sambuc   clang_disposeIndex(Idx);
3813f4a2713aSLionel Sambuc   return result;
3814f4a2713aSLionel Sambuc }
3815f4a2713aSLionel Sambuc 
3816f4a2713aSLionel Sambuc /******************************************************************************/
3817f4a2713aSLionel Sambuc /* Serialized diagnostics.                                                    */
3818f4a2713aSLionel Sambuc /******************************************************************************/
3819f4a2713aSLionel Sambuc 
getDiagnosticCodeStr(enum CXLoadDiag_Error error)3820f4a2713aSLionel Sambuc static const char *getDiagnosticCodeStr(enum CXLoadDiag_Error error) {
3821f4a2713aSLionel Sambuc   switch (error) {
3822f4a2713aSLionel Sambuc     case CXLoadDiag_CannotLoad: return "Cannot Load File";
3823f4a2713aSLionel Sambuc     case CXLoadDiag_None: break;
3824f4a2713aSLionel Sambuc     case CXLoadDiag_Unknown: return "Unknown";
3825f4a2713aSLionel Sambuc     case CXLoadDiag_InvalidFile: return "Invalid File";
3826f4a2713aSLionel Sambuc   }
3827f4a2713aSLionel Sambuc   return "None";
3828f4a2713aSLionel Sambuc }
3829f4a2713aSLionel Sambuc 
getSeverityString(enum CXDiagnosticSeverity severity)3830f4a2713aSLionel Sambuc static const char *getSeverityString(enum CXDiagnosticSeverity severity) {
3831f4a2713aSLionel Sambuc   switch (severity) {
3832f4a2713aSLionel Sambuc     case CXDiagnostic_Note: return "note";
3833f4a2713aSLionel Sambuc     case CXDiagnostic_Error: return "error";
3834f4a2713aSLionel Sambuc     case CXDiagnostic_Fatal: return "fatal";
3835f4a2713aSLionel Sambuc     case CXDiagnostic_Ignored: return "ignored";
3836f4a2713aSLionel Sambuc     case CXDiagnostic_Warning: return "warning";
3837f4a2713aSLionel Sambuc   }
3838f4a2713aSLionel Sambuc   return "unknown";
3839f4a2713aSLionel Sambuc }
3840f4a2713aSLionel Sambuc 
printIndent(unsigned indent)3841f4a2713aSLionel Sambuc static void printIndent(unsigned indent) {
3842f4a2713aSLionel Sambuc   if (indent == 0)
3843f4a2713aSLionel Sambuc     return;
3844f4a2713aSLionel Sambuc   fprintf(stderr, "+");
3845f4a2713aSLionel Sambuc   --indent;
3846f4a2713aSLionel Sambuc   while (indent > 0) {
3847f4a2713aSLionel Sambuc     fprintf(stderr, "-");
3848f4a2713aSLionel Sambuc     --indent;
3849f4a2713aSLionel Sambuc   }
3850f4a2713aSLionel Sambuc }
3851f4a2713aSLionel Sambuc 
printLocation(CXSourceLocation L)3852f4a2713aSLionel Sambuc static void printLocation(CXSourceLocation L) {
3853f4a2713aSLionel Sambuc   CXFile File;
3854f4a2713aSLionel Sambuc   CXString FileName;
3855f4a2713aSLionel Sambuc   unsigned line, column, offset;
3856f4a2713aSLionel Sambuc 
3857f4a2713aSLionel Sambuc   clang_getExpansionLocation(L, &File, &line, &column, &offset);
3858f4a2713aSLionel Sambuc   FileName = clang_getFileName(File);
3859f4a2713aSLionel Sambuc 
3860f4a2713aSLionel Sambuc   fprintf(stderr, "%s:%d:%d", clang_getCString(FileName), line, column);
3861f4a2713aSLionel Sambuc   clang_disposeString(FileName);
3862f4a2713aSLionel Sambuc }
3863f4a2713aSLionel Sambuc 
printRanges(CXDiagnostic D,unsigned indent)3864f4a2713aSLionel Sambuc static void printRanges(CXDiagnostic D, unsigned indent) {
3865f4a2713aSLionel Sambuc   unsigned i, n = clang_getDiagnosticNumRanges(D);
3866f4a2713aSLionel Sambuc 
3867f4a2713aSLionel Sambuc   for (i = 0; i < n; ++i) {
3868f4a2713aSLionel Sambuc     CXSourceLocation Start, End;
3869f4a2713aSLionel Sambuc     CXSourceRange SR = clang_getDiagnosticRange(D, i);
3870f4a2713aSLionel Sambuc     Start = clang_getRangeStart(SR);
3871f4a2713aSLionel Sambuc     End = clang_getRangeEnd(SR);
3872f4a2713aSLionel Sambuc 
3873f4a2713aSLionel Sambuc     printIndent(indent);
3874f4a2713aSLionel Sambuc     fprintf(stderr, "Range: ");
3875f4a2713aSLionel Sambuc     printLocation(Start);
3876f4a2713aSLionel Sambuc     fprintf(stderr, " ");
3877f4a2713aSLionel Sambuc     printLocation(End);
3878f4a2713aSLionel Sambuc     fprintf(stderr, "\n");
3879f4a2713aSLionel Sambuc   }
3880f4a2713aSLionel Sambuc }
3881f4a2713aSLionel Sambuc 
printFixIts(CXDiagnostic D,unsigned indent)3882f4a2713aSLionel Sambuc static void printFixIts(CXDiagnostic D, unsigned indent) {
3883f4a2713aSLionel Sambuc   unsigned i, n = clang_getDiagnosticNumFixIts(D);
3884f4a2713aSLionel Sambuc   fprintf(stderr, "Number FIXITs = %d\n", n);
3885f4a2713aSLionel Sambuc   for (i = 0 ; i < n; ++i) {
3886f4a2713aSLionel Sambuc     CXSourceRange ReplacementRange;
3887f4a2713aSLionel Sambuc     CXString text;
3888f4a2713aSLionel Sambuc     text = clang_getDiagnosticFixIt(D, i, &ReplacementRange);
3889f4a2713aSLionel Sambuc 
3890f4a2713aSLionel Sambuc     printIndent(indent);
3891f4a2713aSLionel Sambuc     fprintf(stderr, "FIXIT: (");
3892f4a2713aSLionel Sambuc     printLocation(clang_getRangeStart(ReplacementRange));
3893f4a2713aSLionel Sambuc     fprintf(stderr, " - ");
3894f4a2713aSLionel Sambuc     printLocation(clang_getRangeEnd(ReplacementRange));
3895f4a2713aSLionel Sambuc     fprintf(stderr, "): \"%s\"\n", clang_getCString(text));
3896f4a2713aSLionel Sambuc     clang_disposeString(text);
3897f4a2713aSLionel Sambuc   }
3898f4a2713aSLionel Sambuc }
3899f4a2713aSLionel Sambuc 
printDiagnosticSet(CXDiagnosticSet Diags,unsigned indent)3900f4a2713aSLionel Sambuc static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) {
3901f4a2713aSLionel Sambuc   unsigned i, n;
3902f4a2713aSLionel Sambuc 
3903f4a2713aSLionel Sambuc   if (!Diags)
3904f4a2713aSLionel Sambuc     return;
3905f4a2713aSLionel Sambuc 
3906f4a2713aSLionel Sambuc   n = clang_getNumDiagnosticsInSet(Diags);
3907f4a2713aSLionel Sambuc   for (i = 0; i < n; ++i) {
3908f4a2713aSLionel Sambuc     CXSourceLocation DiagLoc;
3909f4a2713aSLionel Sambuc     CXDiagnostic D;
3910f4a2713aSLionel Sambuc     CXFile File;
3911f4a2713aSLionel Sambuc     CXString FileName, DiagSpelling, DiagOption, DiagCat;
3912f4a2713aSLionel Sambuc     unsigned line, column, offset;
3913f4a2713aSLionel Sambuc     const char *DiagOptionStr = 0, *DiagCatStr = 0;
3914f4a2713aSLionel Sambuc 
3915f4a2713aSLionel Sambuc     D = clang_getDiagnosticInSet(Diags, i);
3916f4a2713aSLionel Sambuc     DiagLoc = clang_getDiagnosticLocation(D);
3917f4a2713aSLionel Sambuc     clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset);
3918f4a2713aSLionel Sambuc     FileName = clang_getFileName(File);
3919f4a2713aSLionel Sambuc     DiagSpelling = clang_getDiagnosticSpelling(D);
3920f4a2713aSLionel Sambuc 
3921f4a2713aSLionel Sambuc     printIndent(indent);
3922f4a2713aSLionel Sambuc 
3923f4a2713aSLionel Sambuc     fprintf(stderr, "%s:%d:%d: %s: %s",
3924f4a2713aSLionel Sambuc             clang_getCString(FileName),
3925f4a2713aSLionel Sambuc             line,
3926f4a2713aSLionel Sambuc             column,
3927f4a2713aSLionel Sambuc             getSeverityString(clang_getDiagnosticSeverity(D)),
3928f4a2713aSLionel Sambuc             clang_getCString(DiagSpelling));
3929f4a2713aSLionel Sambuc 
3930f4a2713aSLionel Sambuc     DiagOption = clang_getDiagnosticOption(D, 0);
3931f4a2713aSLionel Sambuc     DiagOptionStr = clang_getCString(DiagOption);
3932f4a2713aSLionel Sambuc     if (DiagOptionStr) {
3933f4a2713aSLionel Sambuc       fprintf(stderr, " [%s]", DiagOptionStr);
3934f4a2713aSLionel Sambuc     }
3935f4a2713aSLionel Sambuc 
3936f4a2713aSLionel Sambuc     DiagCat = clang_getDiagnosticCategoryText(D);
3937f4a2713aSLionel Sambuc     DiagCatStr = clang_getCString(DiagCat);
3938f4a2713aSLionel Sambuc     if (DiagCatStr) {
3939f4a2713aSLionel Sambuc       fprintf(stderr, " [%s]", DiagCatStr);
3940f4a2713aSLionel Sambuc     }
3941f4a2713aSLionel Sambuc 
3942f4a2713aSLionel Sambuc     fprintf(stderr, "\n");
3943f4a2713aSLionel Sambuc 
3944f4a2713aSLionel Sambuc     printRanges(D, indent);
3945f4a2713aSLionel Sambuc     printFixIts(D, indent);
3946f4a2713aSLionel Sambuc 
3947f4a2713aSLionel Sambuc     /* Print subdiagnostics. */
3948f4a2713aSLionel Sambuc     printDiagnosticSet(clang_getChildDiagnostics(D), indent+2);
3949f4a2713aSLionel Sambuc 
3950f4a2713aSLionel Sambuc     clang_disposeString(FileName);
3951f4a2713aSLionel Sambuc     clang_disposeString(DiagSpelling);
3952f4a2713aSLionel Sambuc     clang_disposeString(DiagOption);
3953*0a6a1f1dSLionel Sambuc     clang_disposeString(DiagCat);
3954f4a2713aSLionel Sambuc   }
3955f4a2713aSLionel Sambuc }
3956f4a2713aSLionel Sambuc 
read_diagnostics(const char * filename)3957f4a2713aSLionel Sambuc static int read_diagnostics(const char *filename) {
3958f4a2713aSLionel Sambuc   enum CXLoadDiag_Error error;
3959f4a2713aSLionel Sambuc   CXString errorString;
3960f4a2713aSLionel Sambuc   CXDiagnosticSet Diags = 0;
3961f4a2713aSLionel Sambuc 
3962f4a2713aSLionel Sambuc   Diags = clang_loadDiagnostics(filename, &error, &errorString);
3963f4a2713aSLionel Sambuc   if (!Diags) {
3964f4a2713aSLionel Sambuc     fprintf(stderr, "Trouble deserializing file (%s): %s\n",
3965f4a2713aSLionel Sambuc             getDiagnosticCodeStr(error),
3966f4a2713aSLionel Sambuc             clang_getCString(errorString));
3967f4a2713aSLionel Sambuc     clang_disposeString(errorString);
3968f4a2713aSLionel Sambuc     return 1;
3969f4a2713aSLionel Sambuc   }
3970f4a2713aSLionel Sambuc 
3971f4a2713aSLionel Sambuc   printDiagnosticSet(Diags, 0);
3972f4a2713aSLionel Sambuc   fprintf(stderr, "Number of diagnostics: %d\n",
3973f4a2713aSLionel Sambuc           clang_getNumDiagnosticsInSet(Diags));
3974f4a2713aSLionel Sambuc   clang_disposeDiagnosticSet(Diags);
3975f4a2713aSLionel Sambuc   return 0;
3976f4a2713aSLionel Sambuc }
3977f4a2713aSLionel Sambuc 
perform_print_build_session_timestamp(void)3978*0a6a1f1dSLionel Sambuc static int perform_print_build_session_timestamp(void) {
3979*0a6a1f1dSLionel Sambuc   printf("%lld\n", clang_getBuildSessionTimestamp());
3980*0a6a1f1dSLionel Sambuc   return 0;
3981*0a6a1f1dSLionel Sambuc }
3982*0a6a1f1dSLionel Sambuc 
3983f4a2713aSLionel Sambuc /******************************************************************************/
3984f4a2713aSLionel Sambuc /* Command line processing.                                                   */
3985f4a2713aSLionel Sambuc /******************************************************************************/
3986f4a2713aSLionel Sambuc 
GetVisitor(const char * s)3987f4a2713aSLionel Sambuc static CXCursorVisitor GetVisitor(const char *s) {
3988f4a2713aSLionel Sambuc   if (s[0] == '\0')
3989f4a2713aSLionel Sambuc     return FilteredPrintingVisitor;
3990f4a2713aSLionel Sambuc   if (strcmp(s, "-usrs") == 0)
3991f4a2713aSLionel Sambuc     return USRVisitor;
3992f4a2713aSLionel Sambuc   if (strncmp(s, "-memory-usage", 13) == 0)
3993f4a2713aSLionel Sambuc     return GetVisitor(s + 13);
3994f4a2713aSLionel Sambuc   return NULL;
3995f4a2713aSLionel Sambuc }
3996f4a2713aSLionel Sambuc 
print_usage(void)3997f4a2713aSLionel Sambuc static void print_usage(void) {
3998f4a2713aSLionel Sambuc   fprintf(stderr,
3999f4a2713aSLionel Sambuc     "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
4000f4a2713aSLionel Sambuc     "       c-index-test -code-completion-timing=<site> <compiler arguments>\n"
4001f4a2713aSLionel Sambuc     "       c-index-test -cursor-at=<site> <compiler arguments>\n"
4002f4a2713aSLionel Sambuc     "       c-index-test -file-refs-at=<site> <compiler arguments>\n"
4003f4a2713aSLionel Sambuc     "       c-index-test -file-includes-in=<filename> <compiler arguments>\n");
4004f4a2713aSLionel Sambuc   fprintf(stderr,
4005f4a2713aSLionel Sambuc     "       c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
4006f4a2713aSLionel Sambuc     "       c-index-test -index-file-full [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
4007f4a2713aSLionel Sambuc     "       c-index-test -index-tu [-check-prefix=<FileCheck prefix>] <AST file>\n"
4008f4a2713aSLionel Sambuc     "       c-index-test -index-compile-db [-check-prefix=<FileCheck prefix>] <compilation database>\n"
4009f4a2713aSLionel Sambuc     "       c-index-test -test-file-scan <AST file> <source file> "
4010f4a2713aSLionel Sambuc           "[FileCheck prefix]\n");
4011f4a2713aSLionel Sambuc   fprintf(stderr,
4012f4a2713aSLionel Sambuc     "       c-index-test -test-load-tu <AST file> <symbol filter> "
4013f4a2713aSLionel Sambuc           "[FileCheck prefix]\n"
4014f4a2713aSLionel Sambuc     "       c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
4015f4a2713aSLionel Sambuc            "[FileCheck prefix]\n"
4016f4a2713aSLionel Sambuc     "       c-index-test -test-load-source <symbol filter> {<args>}*\n");
4017f4a2713aSLionel Sambuc   fprintf(stderr,
4018f4a2713aSLionel Sambuc     "       c-index-test -test-load-source-memory-usage "
4019f4a2713aSLionel Sambuc     "<symbol filter> {<args>}*\n"
4020f4a2713aSLionel Sambuc     "       c-index-test -test-load-source-reparse <trials> <symbol filter> "
4021f4a2713aSLionel Sambuc     "          {<args>}*\n"
4022f4a2713aSLionel Sambuc     "       c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
4023f4a2713aSLionel Sambuc     "       c-index-test -test-load-source-usrs-memory-usage "
4024f4a2713aSLionel Sambuc           "<symbol filter> {<args>}*\n"
4025f4a2713aSLionel Sambuc     "       c-index-test -test-annotate-tokens=<range> {<args>}*\n"
4026f4a2713aSLionel Sambuc     "       c-index-test -test-inclusion-stack-source {<args>}*\n"
4027f4a2713aSLionel Sambuc     "       c-index-test -test-inclusion-stack-tu <AST file>\n");
4028f4a2713aSLionel Sambuc   fprintf(stderr,
4029f4a2713aSLionel Sambuc     "       c-index-test -test-print-linkage-source {<args>}*\n"
4030f4a2713aSLionel Sambuc     "       c-index-test -test-print-type {<args>}*\n"
4031f4a2713aSLionel Sambuc     "       c-index-test -test-print-type-size {<args>}*\n"
4032f4a2713aSLionel Sambuc     "       c-index-test -test-print-bitwidth {<args>}*\n"
4033f4a2713aSLionel Sambuc     "       c-index-test -print-usr [<CursorKind> {<args>}]*\n"
4034f4a2713aSLionel Sambuc     "       c-index-test -print-usr-file <file>\n"
4035f4a2713aSLionel Sambuc     "       c-index-test -write-pch <file> <compiler arguments>\n");
4036f4a2713aSLionel Sambuc   fprintf(stderr,
4037f4a2713aSLionel Sambuc     "       c-index-test -compilation-db [lookup <filename>] database\n");
4038f4a2713aSLionel Sambuc   fprintf(stderr,
4039*0a6a1f1dSLionel Sambuc     "       c-index-test -print-build-session-timestamp\n");
4040*0a6a1f1dSLionel Sambuc   fprintf(stderr,
4041f4a2713aSLionel Sambuc     "       c-index-test -read-diagnostics <file>\n\n");
4042f4a2713aSLionel Sambuc   fprintf(stderr,
4043f4a2713aSLionel Sambuc     " <symbol filter> values:\n%s",
4044f4a2713aSLionel Sambuc     "   all - load all symbols, including those from PCH\n"
4045f4a2713aSLionel Sambuc     "   local - load all symbols except those in PCH\n"
4046f4a2713aSLionel Sambuc     "   category - only load ObjC categories (non-PCH)\n"
4047f4a2713aSLionel Sambuc     "   interface - only load ObjC interfaces (non-PCH)\n"
4048f4a2713aSLionel Sambuc     "   protocol - only load ObjC protocols (non-PCH)\n"
4049f4a2713aSLionel Sambuc     "   function - only load functions (non-PCH)\n"
4050f4a2713aSLionel Sambuc     "   typedef - only load typdefs (non-PCH)\n"
4051f4a2713aSLionel Sambuc     "   scan-function - scan function bodies (non-PCH)\n\n");
4052f4a2713aSLionel Sambuc }
4053f4a2713aSLionel Sambuc 
4054f4a2713aSLionel Sambuc /***/
4055f4a2713aSLionel Sambuc 
cindextest_main(int argc,const char ** argv)4056f4a2713aSLionel Sambuc int cindextest_main(int argc, const char **argv) {
4057f4a2713aSLionel Sambuc   clang_enableStackTraces();
4058f4a2713aSLionel Sambuc   if (argc > 2 && strcmp(argv[1], "-read-diagnostics") == 0)
4059f4a2713aSLionel Sambuc       return read_diagnostics(argv[2]);
4060f4a2713aSLionel Sambuc   if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
4061f4a2713aSLionel Sambuc     return perform_code_completion(argc, argv, 0);
4062f4a2713aSLionel Sambuc   if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
4063f4a2713aSLionel Sambuc     return perform_code_completion(argc, argv, 1);
4064f4a2713aSLionel Sambuc   if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
4065f4a2713aSLionel Sambuc     return inspect_cursor_at(argc, argv);
4066f4a2713aSLionel Sambuc   if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1])
4067f4a2713aSLionel Sambuc     return find_file_refs_at(argc, argv);
4068f4a2713aSLionel Sambuc   if (argc > 2 && strstr(argv[1], "-file-includes-in=") == argv[1])
4069f4a2713aSLionel Sambuc     return find_file_includes_in(argc, argv);
4070f4a2713aSLionel Sambuc   if (argc > 2 && strcmp(argv[1], "-index-file") == 0)
4071f4a2713aSLionel Sambuc     return index_file(argc - 2, argv + 2, /*full=*/0);
4072f4a2713aSLionel Sambuc   if (argc > 2 && strcmp(argv[1], "-index-file-full") == 0)
4073f4a2713aSLionel Sambuc     return index_file(argc - 2, argv + 2, /*full=*/1);
4074f4a2713aSLionel Sambuc   if (argc > 2 && strcmp(argv[1], "-index-tu") == 0)
4075f4a2713aSLionel Sambuc     return index_tu(argc - 2, argv + 2);
4076f4a2713aSLionel Sambuc   if (argc > 2 && strcmp(argv[1], "-index-compile-db") == 0)
4077f4a2713aSLionel Sambuc     return index_compile_db(argc - 2, argv + 2);
4078f4a2713aSLionel Sambuc   else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
4079f4a2713aSLionel Sambuc     CXCursorVisitor I = GetVisitor(argv[1] + 13);
4080f4a2713aSLionel Sambuc     if (I)
4081f4a2713aSLionel Sambuc       return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
4082f4a2713aSLionel Sambuc                                   NULL);
4083f4a2713aSLionel Sambuc   }
4084f4a2713aSLionel Sambuc   else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
4085f4a2713aSLionel Sambuc     CXCursorVisitor I = GetVisitor(argv[1] + 25);
4086f4a2713aSLionel Sambuc     if (I) {
4087f4a2713aSLionel Sambuc       int trials = atoi(argv[2]);
4088f4a2713aSLionel Sambuc       return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
4089f4a2713aSLionel Sambuc                                          NULL);
4090f4a2713aSLionel Sambuc     }
4091f4a2713aSLionel Sambuc   }
4092f4a2713aSLionel Sambuc   else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
4093f4a2713aSLionel Sambuc     CXCursorVisitor I = GetVisitor(argv[1] + 17);
4094f4a2713aSLionel Sambuc 
4095f4a2713aSLionel Sambuc     PostVisitTU postVisit = 0;
4096f4a2713aSLionel Sambuc     if (strstr(argv[1], "-memory-usage"))
4097f4a2713aSLionel Sambuc       postVisit = PrintMemoryUsage;
4098f4a2713aSLionel Sambuc 
4099f4a2713aSLionel Sambuc     if (I)
4100f4a2713aSLionel Sambuc       return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
4101f4a2713aSLionel Sambuc                                       postVisit);
4102f4a2713aSLionel Sambuc   }
4103f4a2713aSLionel Sambuc   else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
4104f4a2713aSLionel Sambuc     return perform_file_scan(argv[2], argv[3],
4105f4a2713aSLionel Sambuc                              argc >= 5 ? argv[4] : 0);
4106f4a2713aSLionel Sambuc   else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
4107f4a2713aSLionel Sambuc     return perform_token_annotation(argc, argv);
4108f4a2713aSLionel Sambuc   else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
4109f4a2713aSLionel Sambuc     return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
4110f4a2713aSLionel Sambuc                                     PrintInclusionStack);
4111f4a2713aSLionel Sambuc   else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
4112f4a2713aSLionel Sambuc     return perform_test_load_tu(argv[2], "all", NULL, NULL,
4113f4a2713aSLionel Sambuc                                 PrintInclusionStack);
4114f4a2713aSLionel Sambuc   else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
4115f4a2713aSLionel Sambuc     return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
4116f4a2713aSLionel Sambuc                                     NULL);
4117f4a2713aSLionel Sambuc   else if (argc > 2 && strcmp(argv[1], "-test-print-type") == 0)
4118f4a2713aSLionel Sambuc     return perform_test_load_source(argc - 2, argv + 2, "all",
4119f4a2713aSLionel Sambuc                                     PrintType, 0);
4120f4a2713aSLionel Sambuc   else if (argc > 2 && strcmp(argv[1], "-test-print-type-size") == 0)
4121f4a2713aSLionel Sambuc     return perform_test_load_source(argc - 2, argv + 2, "all",
4122f4a2713aSLionel Sambuc                                     PrintTypeSize, 0);
4123f4a2713aSLionel Sambuc   else if (argc > 2 && strcmp(argv[1], "-test-print-bitwidth") == 0)
4124f4a2713aSLionel Sambuc     return perform_test_load_source(argc - 2, argv + 2, "all",
4125f4a2713aSLionel Sambuc                                     PrintBitWidth, 0);
4126*0a6a1f1dSLionel Sambuc   else if (argc > 2 && strcmp(argv[1], "-test-print-mangle") == 0)
4127*0a6a1f1dSLionel Sambuc     return perform_test_load_tu(argv[2], "all", NULL, PrintMangledName, NULL);
4128f4a2713aSLionel Sambuc   else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
4129f4a2713aSLionel Sambuc     if (argc > 2)
4130f4a2713aSLionel Sambuc       return print_usrs(argv + 2, argv + argc);
4131f4a2713aSLionel Sambuc     else {
4132f4a2713aSLionel Sambuc       display_usrs();
4133f4a2713aSLionel Sambuc       return 1;
4134f4a2713aSLionel Sambuc     }
4135f4a2713aSLionel Sambuc   }
4136f4a2713aSLionel Sambuc   else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
4137f4a2713aSLionel Sambuc     return print_usrs_file(argv[2]);
4138f4a2713aSLionel Sambuc   else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
4139f4a2713aSLionel Sambuc     return write_pch_file(argv[2], argc - 3, argv + 3);
4140f4a2713aSLionel Sambuc   else if (argc > 2 && strcmp(argv[1], "-compilation-db") == 0)
4141f4a2713aSLionel Sambuc     return perform_test_compilation_db(argv[argc-1], argc - 3, argv + 2);
4142*0a6a1f1dSLionel Sambuc   else if (argc == 2 && strcmp(argv[1], "-print-build-session-timestamp") == 0)
4143*0a6a1f1dSLionel Sambuc     return perform_print_build_session_timestamp();
4144f4a2713aSLionel Sambuc 
4145f4a2713aSLionel Sambuc   print_usage();
4146f4a2713aSLionel Sambuc   return 1;
4147f4a2713aSLionel Sambuc }
4148f4a2713aSLionel Sambuc 
4149f4a2713aSLionel Sambuc /***/
4150f4a2713aSLionel Sambuc 
4151f4a2713aSLionel Sambuc /* We intentionally run in a separate thread to ensure we at least minimal
4152f4a2713aSLionel Sambuc  * testing of a multithreaded environment (for example, having a reduced stack
4153f4a2713aSLionel Sambuc  * size). */
4154f4a2713aSLionel Sambuc 
4155f4a2713aSLionel Sambuc typedef struct thread_info {
4156f4a2713aSLionel Sambuc   int argc;
4157f4a2713aSLionel Sambuc   const char **argv;
4158f4a2713aSLionel Sambuc   int result;
4159f4a2713aSLionel Sambuc } thread_info;
thread_runner(void * client_data_v)4160f4a2713aSLionel Sambuc void thread_runner(void *client_data_v) {
4161f4a2713aSLionel Sambuc   thread_info *client_data = client_data_v;
4162f4a2713aSLionel Sambuc   client_data->result = cindextest_main(client_data->argc, client_data->argv);
4163*0a6a1f1dSLionel Sambuc }
4164*0a6a1f1dSLionel Sambuc 
flush_atexit(void)4165*0a6a1f1dSLionel Sambuc static void flush_atexit(void) {
4166*0a6a1f1dSLionel Sambuc   /* stdout, and surprisingly even stderr, are not always flushed on process
4167*0a6a1f1dSLionel Sambuc    * and thread exit, particularly when the system is under heavy load. */
4168*0a6a1f1dSLionel Sambuc   fflush(stdout);
4169*0a6a1f1dSLionel Sambuc   fflush(stderr);
4170f4a2713aSLionel Sambuc }
4171f4a2713aSLionel Sambuc 
main(int argc,const char ** argv)4172f4a2713aSLionel Sambuc int main(int argc, const char **argv) {
4173f4a2713aSLionel Sambuc   thread_info client_data;
4174f4a2713aSLionel Sambuc 
4175*0a6a1f1dSLionel Sambuc   atexit(flush_atexit);
4176*0a6a1f1dSLionel Sambuc 
4177f4a2713aSLionel Sambuc #ifdef CLANG_HAVE_LIBXML
4178f4a2713aSLionel Sambuc   LIBXML_TEST_VERSION
4179f4a2713aSLionel Sambuc #endif
4180f4a2713aSLionel Sambuc 
4181f4a2713aSLionel Sambuc   if (getenv("CINDEXTEST_NOTHREADS"))
4182f4a2713aSLionel Sambuc     return cindextest_main(argc, argv);
4183f4a2713aSLionel Sambuc 
4184f4a2713aSLionel Sambuc   client_data.argc = argc;
4185f4a2713aSLionel Sambuc   client_data.argv = argv;
4186f4a2713aSLionel Sambuc   clang_executeOnThread(thread_runner, &client_data, 0);
4187f4a2713aSLionel Sambuc   return client_data.result;
4188f4a2713aSLionel Sambuc }
4189