aboutsummaryrefslogtreecommitdiff
path: root/tools/c-index-test/c-index-test.c
blob: 7300585a1d8836408b8a7401ad4bf4430754c913 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/* c-index-test.c */

#include "clang-c/Index.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/******************************************************************************/
/* Utility functions.                                                         */
/******************************************************************************/

#ifdef _MSC_VER
char *basename(const char* path)
{
    char* base1 = (char*)strrchr(path, '/');
    char* base2 = (char*)strrchr(path, '\\');
    if (base1 && base2)
        return((base1 > base2) ? base1 + 1 : base2 + 1);
    else if (base1)
        return(base1 + 1);
    else if (base2)
        return(base2 + 1);

    return((char*)path);
}
#else
extern char *basename(const char *);
#endif

static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
                                      CXTranslationUnit *TU) {
  
  *TU = clang_createTranslationUnit(Idx, file);
  if (!TU) {
    fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
    return 0;
  }  
  return 1;
}

/******************************************************************************/
/* Pretty-printing.                                                           */
/******************************************************************************/

static void PrintCursor(CXCursor Cursor) {
  if (clang_isInvalid(Cursor.kind))
    printf("Invalid Cursor => %s", clang_getCursorKindSpelling(Cursor.kind));
  else {
    CXDecl DeclReferenced;
    CXString string;
    string = clang_getCursorSpelling(Cursor);
    printf("%s=%s", clang_getCursorKindSpelling(Cursor.kind),
                      clang_getCString(string));
    clang_disposeString(string);
    DeclReferenced = clang_getCursorDecl(Cursor);
    if (DeclReferenced)
      printf(":%d:%d", clang_getDeclLine(DeclReferenced),
                       clang_getDeclColumn(DeclReferenced));
  }
}

static const char* GetCursorSource(CXCursor Cursor) {  
  const char *source = clang_getCursorSource(Cursor);
  if (!source)
    return "<invalid loc>";  
  return basename(source);
}

/******************************************************************************/
/* Logic for testing clang_loadTranslationUnit().                             */
/******************************************************************************/

static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter) {
  if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
    CXString string;
    printf("// CHECK: %s:%d:%d: ", GetCursorSource(Cursor),
                                 clang_getCursorLine(Cursor),
                                 clang_getCursorColumn(Cursor));
    PrintCursor(Cursor);
    string = clang_getDeclSpelling(Dcl);
    printf(" [Context=%s]\n", clang_getCString(string));
    clang_disposeString(string);
  }
}

static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor,
                                   CXClientData Filter) {
  if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
    CXString string;
    printf("// CHECK: %s:%d:%d: ", GetCursorSource(Cursor),
                                 clang_getCursorLine(Cursor),
                                 clang_getCursorColumn(Cursor));
    PrintCursor(Cursor);
    string = clang_getTranslationUnitSpelling(Unit);
    printf(" [Context=%s]\n",
          basename(clang_getCString(string)));
    clang_disposeString(string);

    clang_loadDeclaration(Cursor.decl, DeclVisitor, 0);
  }
}

static void FunctionScanVisitor(CXTranslationUnit Unit, CXCursor Cursor,
                                CXClientData Filter) {
  const char *startBuf, *endBuf;
  unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
  CXCursor Ref;

  if (Cursor.kind != CXCursor_FunctionDefn)
    return;

  clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
                                       &startLine, &startColumn,
                                       &endLine, &endColumn);
  /* Probe the entire body, looking for both decls and refs. */
  curLine = startLine;
  curColumn = startColumn;

  while (startBuf < endBuf) {
    if (*startBuf == '\n') {
      startBuf++;
      curLine++;
      curColumn = 1;
    } else if (*startBuf != '\t')
      curColumn++;
          
    Ref = clang_getCursor(Unit, clang_getCursorSource(Cursor),
                          curLine, curColumn);
    if (Ref.kind == CXCursor_NoDeclFound) {
      /* Nothing found here; that's fine. */
    } else if (Ref.kind != CXCursor_FunctionDecl) {
      CXString string;
      printf("// CHECK: %s:%d:%d: ", GetCursorSource(Ref),
             curLine, curColumn);
      PrintCursor(Ref);
      string = clang_getDeclSpelling(Ref.decl);
      printf(" [Context:%s]\n", clang_getCString(string));
      clang_disposeString(string);
    }
    startBuf++;
  }
}

static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
                             const char *filter) {
  enum CXCursorKind K = CXCursor_NotImplemented;
  CXTranslationUnitIterator Visitor = TranslationUnitVisitor;
  enum CXCursorKind *ck = &K;

  /* Perform some simple filtering. */
  if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
  else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
  else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
  else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
  else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
  else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
  else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
  else {
    fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
    return 1;
  }
  
  clang_loadTranslationUnit(TU, Visitor, ck);
  clang_disposeTranslationUnit(TU);
  return 0;
}

int perform_test_load_tu(const char *file, const char *filter) {
  CXIndex Idx;
  CXTranslationUnit TU;
  Idx = clang_createIndex(/* excludeDeclsFromPCH */ 
                          !strcmp(filter, "local") ? 1 : 0, 
                          /* displayDiagnostics */ 1);
  
  if (!CreateTranslationUnit(Idx, file, &TU))
    return 1;

  return perform_test_load(Idx, TU, filter);
}

int perform_test_load_source(int argc, const char **argv, const char *filter) {
  const char *UseExternalASTs =
    getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
  CXIndex Idx;
  CXTranslationUnit TU;
  Idx = clang_createIndex(/* excludeDeclsFromPCH */
                          !strcmp(filter, "local") ? 1 : 0,
                          /* displayDiagnostics */ 1);

  if (UseExternalASTs && strlen(UseExternalASTs))
    clang_setUseExternalASTGeneration(Idx, 1);

  TU = clang_createTranslationUnitFromSourceFile(Idx, 0, argc, argv);
  if (!TU) {
    fprintf(stderr, "Unable to load translation unit!\n");
    return 1;
  }

  return perform_test_load(Idx, TU, filter);
}

/******************************************************************************/
/* Logic for testing clang_getCursor().                                       */
/******************************************************************************/

static void print_cursor_file_scan(CXCursor cursor,
                                   unsigned start_line, unsigned start_col,
                                   unsigned end_line, unsigned end_col,
                                   const char *prefix) {
  printf("// CHECK");
  if (prefix)
    printf("-%s", prefix);
  printf("{start_line=%d start_col=%d end_line=%d end_col=%d} ",
          start_line, start_col, end_line, end_col);
  PrintCursor(cursor);
  printf("\n");
}

static int perform_file_scan(const char *ast_file, const char *source_file,
                             const char *prefix) {
  CXIndex Idx;
  CXTranslationUnit TU;
  FILE *fp;
  unsigned line;
  CXCursor prevCursor;
  unsigned printed;
  unsigned start_line, start_col, last_line, last_col;
  size_t i;
  
  if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
                                /* displayDiagnostics */ 1))) {
    fprintf(stderr, "Could not create Index\n");
    return 1;
  }
  
  if (!CreateTranslationUnit(Idx, ast_file, &TU))
    return 1;
  
  if ((fp = fopen(source_file, "r")) == NULL) {
    fprintf(stderr, "Could not open '%s'\n", source_file);
    return 1;
  }
  
  line = 0;
  prevCursor = clang_getNullCursor();
  printed = 0;
  start_line = last_line = 1;
  start_col = last_col = 1;
  
  while (!feof(fp)) {
    size_t len = 0;
    int c;

    while ((c = fgetc(fp)) != EOF) {
      len++;
      if (c == '\n')
        break;
    }

    ++line;
    
    for (i = 0; i < len ; ++i) {
      CXCursor cursor;
      cursor = clang_getCursor(TU, source_file, line, i+1);

      if (!clang_equalCursors(cursor, prevCursor) &&
          prevCursor.kind != CXCursor_InvalidFile) {
        print_cursor_file_scan(prevCursor, start_line, start_col,
                               last_line, last_col, prefix);
        printed = 1;
        start_line = line;
        start_col = (unsigned) i+1;
      }
      else {
        printed = 0;
      }
      
      prevCursor = cursor;
      last_line = line;
      last_col = (unsigned) i+1;
    }    
  }
  
  if (!printed && prevCursor.kind != CXCursor_InvalidFile) {
    print_cursor_file_scan(prevCursor, start_line, start_col,
                           last_line, last_col, prefix);
  }  
  
  fclose(fp);
  return 0;
}

/******************************************************************************/
/* Logic for testing clang_codeComplete().                                    */
/******************************************************************************/

/* Parse file:line:column from the input string. Returns 0 on success, non-zero
   on failure. If successful, the pointer *filename will contain newly-allocated
   memory (that will be owned by the caller) to store the file name. */
int parse_file_line_column(const char *input, char **filename, unsigned *line, 
                           unsigned *column) {
  /* Find the second colon. */
  const char *second_colon = strrchr(input, ':'), *first_colon;
  char *endptr = 0;
  if (!second_colon || second_colon == input) {
    fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
    return 1;
  }

  /* Parse the column number. */
  *column = strtol(second_colon + 1, &endptr, 10);
  if (*endptr != 0) {
    fprintf(stderr, "could not parse column in '%s'\n", input);
    return 1;
  }

  /* Find the first colon. */
  first_colon = second_colon - 1;
  while (first_colon != input && *first_colon != ':')
    --first_colon;
  if (first_colon == input) {
    fprintf(stderr, "could not parse line in '%s'\n", input);
    return 1;    
  }

  /* Parse the line number. */
  *line = strtol(first_colon + 1, &endptr, 10);
  if (*endptr != ':') {
    fprintf(stderr, "could not parse line in '%s'\n", input);
    return 1;
  }
  
  /* Copy the file name. */
  *filename = (char*)malloc(first_colon - input + 1);
  memcpy(*filename, input, first_colon - input);
  (*filename)[first_colon - input] = 0;
  return 0;
}

const char *
clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
  switch (Kind) {
  case CXCompletionChunk_Optional: return "Optional";
  case CXCompletionChunk_TypedText: return "TypedText";
  case CXCompletionChunk_Text: return "Text";
  case CXCompletionChunk_Placeholder: return "Placeholder";
  case CXCompletionChunk_Informative: return "Informative";
  case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
  case CXCompletionChunk_LeftParen: return "LeftParen";
  case CXCompletionChunk_RightParen: return "RightParen";
  case CXCompletionChunk_LeftBracket: return "LeftBracket";
  case CXCompletionChunk_RightBracket: return "RightBracket";
  case CXCompletionChunk_LeftBrace: return "LeftBrace";
  case CXCompletionChunk_RightBrace: return "RightBrace";
  case CXCompletionChunk_LeftAngle: return "LeftAngle";
  case CXCompletionChunk_RightAngle: return "RightAngle";
  case CXCompletionChunk_Comma: return "Comma";
  }
  
  return "Unknown";
}

void print_completion_string(CXCompletionString completion_string, FILE *file) {
  int I, N;
  
  N = clang_getNumCompletionChunks(completion_string);
  for (I = 0; I != N; ++I) {
    const char *text = 0;
    enum CXCompletionChunkKind Kind
      = clang_getCompletionChunkKind(completion_string, I);
    
    if (Kind == CXCompletionChunk_Optional) {
      fprintf(file, "{Optional ");
      print_completion_string(
                clang_getCompletionChunkCompletionString(completion_string, I), 
                              file);
      fprintf(file, "}");
      continue;
    }
    
    text = clang_getCompletionChunkText(completion_string, I);
    fprintf(file, "{%s %s}", 
            clang_getCompletionChunkKindSpelling(Kind),
            text? text : "");
  }
}

void print_completion_result(CXCompletionResult *completion_result,
                             CXClientData client_data) {
  FILE *file = (FILE *)client_data;
  fprintf(file, "%s:", 
          clang_getCursorKindSpelling(completion_result->CursorKind));
  print_completion_string(completion_result->CompletionString, file);
  fprintf(file, "\n");
}

void free_remapped_files(struct CXUnsavedFile *unsaved_files,
                         int num_unsaved_files) {
  int i;
  for (i = 0; i != num_unsaved_files; ++i) {
    free((char *)unsaved_files[i].Filename);
    free((char *)unsaved_files[i].Contents);
  }
}

int parse_remapped_files(int argc, const char **argv, int start_arg,
                         struct CXUnsavedFile **unsaved_files,
                          int *num_unsaved_files) {
  int i;
  int arg;
  int prefix_len = strlen("-remap-file=");
  *unsaved_files = 0;
  *num_unsaved_files = 0;

  /* Count the number of remapped files. */
  for (arg = start_arg; arg < argc; ++arg) {
    if (strncmp(argv[arg], "-remap-file=", prefix_len))
      break;

    ++*num_unsaved_files;
  }

  if (*num_unsaved_files == 0)
    return 0;

  *unsaved_files
    = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) * 
                                     *num_unsaved_files);
  for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
    struct CXUnsavedFile *unsaved = *unsaved_files + i;
    const char *arg_string = argv[arg] + prefix_len;
    int filename_len;
    char *filename;
    char *contents;
    FILE *to_file;
    const char *semi = strchr(arg_string, ';');
    if (!semi) {
      fprintf(stderr, 
              "error: -remap-file=from;to argument is missing semicolon\n");
      free_remapped_files(*unsaved_files, i);
      *unsaved_files = 0;
      *num_unsaved_files = 0;
      return -1;
    }

    /* Open the file that we're remapping to. */
    to_file = fopen(semi + 1, "r");
    if (!to_file) {
      fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
              semi + 1);
      free_remapped_files(*unsaved_files, i);
      *unsaved_files = 0;
      *num_unsaved_files = 0;
      return -1;
    }

    /* Determine the length of the file we're remapping to. */
    fseek(to_file, 0, SEEK_END);
    unsaved->Length = ftell(to_file);
    fseek(to_file, 0, SEEK_SET);
    
    /* Read the contents of the file we're remapping to. */
    contents = (char *)malloc(unsaved->Length + 1);
    fread(contents, 1, unsaved->Length, to_file);
    contents[unsaved->Length] = 0;
    unsaved->Contents = contents;

    /* Close the file. */
    fclose(to_file);
    
    /* Copy the file name that we're remapping from. */
    filename_len = semi - arg_string;
    filename = (char *)malloc(filename_len + 1);
    memcpy(filename, arg_string, filename_len);
    filename[filename_len] = 0;
    unsaved->Filename = filename;
  }

  return 0;
}

int perform_code_completion(int argc, const char **argv) {
  const char *input = argv[1];
  char *filename = 0;
  unsigned line;
  unsigned column;
  CXIndex CIdx;
  int errorCode;
  struct CXUnsavedFile *unsaved_files = 0;
  int num_unsaved_files = 0;

  input += strlen("-code-completion-at=");
  if ((errorCode = parse_file_line_column(input, &filename, &line, &column)))
    return errorCode;

  if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
    return -1;

  CIdx = clang_createIndex(0, 0);
  clang_codeComplete(CIdx, argv[argc - 1], argc - num_unsaved_files - 3, 
                     argv + num_unsaved_files + 2, 
                     num_unsaved_files, unsaved_files,
                     filename, line, column, &print_completion_result, stdout);
  clang_disposeIndex(CIdx);
  free(filename);
  
  free_remapped_files(unsaved_files, num_unsaved_files);

  return 0;
}

/******************************************************************************/
/* Command line processing.                                                   */
/******************************************************************************/

static void print_usage(void) {
  fprintf(stderr,
    "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
    "       c-index-test -test-file-scan <AST file> <source file> "
          "[FileCheck prefix]\n"
    "       c-index-test -test-load-tu <AST file> <symbol filter>\n\n"
    "       c-index-test -test-load-source <symbol filter> {<args>}*\n\n"
    " <symbol filter> options for -test-load-tu and -test-load-source:\n%s",
    "   all - load all symbols, including those from PCH\n"
    "   local - load all symbols except those in PCH\n"
    "   category - only load ObjC categories (non-PCH)\n"
    "   interface - only load ObjC interfaces (non-PCH)\n"
    "   protocol - only load ObjC protocols (non-PCH)\n"
    "   function - only load functions (non-PCH)\n"
    "   typedef - only load typdefs (non-PCH)\n"
    "   scan-function - scan function bodies (non-PCH)\n\n");
}

int main(int argc, const char **argv) {
  if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
    return perform_code_completion(argc, argv);
  if (argc == 4 && strcmp(argv[1], "-test-load-tu") == 0)
    return perform_test_load_tu(argv[2], argv[3]);
  if (argc >= 4 && strcmp(argv[1], "-test-load-source") == 0)
    return perform_test_load_source(argc - 3, argv + 3, argv[2]);
  if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
    return perform_file_scan(argv[2], argv[3],
                             argc >= 5 ? argv[4] : 0);

  print_usage();
  return 1;
}