/* c-index-test.c */
#include "clang-c/Index.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.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;
}
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);
if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
(feof(to_file) ? "EOF" : "error"), semi + 1);
fclose(to_file);
free_remapped_files(*unsaved_files, i);
*unsaved_files = 0;
*num_unsaved_files = 0;
return -1;
}
contents[unsaved->Length] = 0;
unsaved->Contents = contents;
/* Close the file. */
fclose(to_file);
/* Copy the file name that we're remapping from. */
filename_len = semi - arg_string;
filename = (char *)malloc