2009-09-01 23:55:40 +08:00
|
|
|
/* c-index-test.c */
|
2009-08-28 23:28:48 +08:00
|
|
|
|
|
|
|
#include "clang-c/Index.h"
|
2010-08-26 21:48:20 +08:00
|
|
|
#include <ctype.h>
|
2009-11-07 08:00:49 +08:00
|
|
|
#include <stdlib.h>
|
2009-08-31 08:59:03 +08:00
|
|
|
#include <stdio.h>
|
2009-09-03 23:49:00 +08:00
|
|
|
#include <string.h>
|
2010-01-16 03:40:17 +08:00
|
|
|
#include <assert.h>
|
2009-09-03 23:49:00 +08:00
|
|
|
|
2009-11-18 02:13:31 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
/* Utility functions. */
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2009-10-27 21:42:56 +08:00
|
|
|
#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
|
2009-09-25 04:03:06 +08:00
|
|
|
extern char *basename(const char *);
|
2009-10-27 21:42:56 +08:00
|
|
|
#endif
|
2009-09-25 04:03:06 +08:00
|
|
|
|
2010-07-26 01:39:21 +08:00
|
|
|
/** \brief Return the default parsing options. */
|
2010-07-23 08:33:23 +08:00
|
|
|
static unsigned getDefaultParsingOptions() {
|
|
|
|
unsigned options = CXTranslationUnit_DetailedPreprocessingRecord;
|
|
|
|
|
|
|
|
if (getenv("CINDEXTEST_EDITING"))
|
2010-08-10 06:28:58 +08:00
|
|
|
options |= clang_defaultEditingTranslationUnitOptions();
|
2010-08-14 06:48:40 +08:00
|
|
|
if (getenv("CINDEXTEST_COMPLETION_CACHING"))
|
|
|
|
options |= CXTranslationUnit_CacheCompletionResults;
|
2010-07-23 08:33:23 +08:00
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2010-02-14 16:32:24 +08:00
|
|
|
static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column,
|
|
|
|
unsigned end_line, unsigned end_column) {
|
|
|
|
fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column,
|
2010-02-14 18:02:57 +08:00
|
|
|
end_line, end_column);
|
2010-02-14 16:32:24 +08:00
|
|
|
}
|
|
|
|
|
2009-11-18 03:37:36 +08:00
|
|
|
static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
|
|
|
|
CXTranslationUnit *TU) {
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2010-02-19 02:08:43 +08:00
|
|
|
*TU = clang_createTranslationUnit(Idx, file);
|
2010-07-27 05:44:15 +08:00
|
|
|
if (!*TU) {
|
2009-11-18 03:37:36 +08:00
|
|
|
fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
|
|
|
|
return 0;
|
2010-02-17 08:41:32 +08:00
|
|
|
}
|
2009-11-18 03:37:36 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-01-23 08:14:00 +08:00
|
|
|
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);
|
|
|
|
}
|
2010-08-20 04:50:29 +08:00
|
|
|
free(unsaved_files);
|
2010-01-23 08:14:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2010-01-23 08:14:00 +08:00
|
|
|
/* Count the number of remapped files. */
|
|
|
|
for (arg = start_arg; arg < argc; ++arg) {
|
|
|
|
if (strncmp(argv[arg], "-remap-file=", prefix_len))
|
|
|
|
break;
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2010-01-23 08:14:00 +08:00
|
|
|
++*num_unsaved_files;
|
|
|
|
}
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2010-01-23 08:14:00 +08:00
|
|
|
if (*num_unsaved_files == 0)
|
|
|
|
return 0;
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2010-01-23 08:14:00 +08:00
|
|
|
*unsaved_files
|
2010-08-20 04:50:29 +08:00
|
|
|
= (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
|
|
|
|
*num_unsaved_files);
|
2010-01-23 08:14:00 +08:00
|
|
|
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) {
|
2010-02-17 08:41:32 +08:00
|
|
|
fprintf(stderr,
|
2010-01-23 08:14:00 +08:00
|
|
|
"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;
|
|
|
|
}
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2010-01-23 08:14:00 +08:00
|
|
|
/* Open the file that we're remapping to. */
|
2010-10-12 09:01:43 +08:00
|
|
|
to_file = fopen(semi + 1, "rb");
|
2010-01-23 08:14:00 +08:00
|
|
|
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;
|
|
|
|
}
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2010-01-23 08:14:00 +08:00
|
|
|
/* 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);
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2010-01-23 08:14:00 +08:00
|
|
|
/* 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;
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2010-01-23 08:14:00 +08:00
|
|
|
/* Close the file. */
|
|
|
|
fclose(to_file);
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2010-01-23 08:14:00 +08:00
|
|
|
/* 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;
|
|
|
|
}
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2010-01-23 08:14:00 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-11-18 02:13:31 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
/* Pretty-printing. */
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2010-10-03 06:49:11 +08:00
|
|
|
int want_display_name = 0;
|
|
|
|
|
2009-09-03 23:49:00 +08:00
|
|
|
static void PrintCursor(CXCursor Cursor) {
|
2010-02-17 08:41:32 +08:00
|
|
|
if (clang_isInvalid(Cursor.kind)) {
|
|
|
|
CXString ks = clang_getCursorKindSpelling(Cursor.kind);
|
|
|
|
printf("Invalid Cursor => %s", clang_getCString(ks));
|
|
|
|
clang_disposeString(ks);
|
|
|
|
}
|
2009-09-26 05:32:34 +08:00
|
|
|
else {
|
2010-02-17 08:41:32 +08:00
|
|
|
CXString string, ks;
|
2010-01-19 09:20:04 +08:00
|
|
|
CXCursor Referenced;
|
2010-01-20 05:36:55 +08:00
|
|
|
unsigned line, column;
|
2010-09-02 08:07:54 +08:00
|
|
|
CXCursor SpecializationOf;
|
2010-10-02 04:25:15 +08:00
|
|
|
CXCursor *overridden;
|
|
|
|
unsigned num_overridden;
|
|
|
|
|
2010-02-17 08:41:32 +08:00
|
|
|
ks = clang_getCursorKindSpelling(Cursor.kind);
|
2010-10-03 06:49:11 +08:00
|
|
|
string = want_display_name? clang_getCursorDisplayName(Cursor)
|
|
|
|
: clang_getCursorSpelling(Cursor);
|
2010-02-17 08:41:32 +08:00
|
|
|
printf("%s=%s", clang_getCString(ks),
|
|
|
|
clang_getCString(string));
|
|
|
|
clang_disposeString(ks);
|
2009-11-10 01:45:52 +08:00
|
|
|
clang_disposeString(string);
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2010-01-19 09:20:04 +08:00
|
|
|
Referenced = clang_getCursorReferenced(Cursor);
|
|
|
|
if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
|
2010-09-14 06:52:57 +08:00
|
|
|
if (clang_getCursorKind(Referenced) == CXCursor_OverloadedDeclRef) {
|
|
|
|
unsigned I, N = clang_getNumOverloadedDecls(Referenced);
|
|
|
|
printf("[");
|
|
|
|
for (I = 0; I != N; ++I) {
|
|
|
|
CXCursor Ovl = clang_getOverloadedDecl(Referenced, I);
|
2010-09-14 08:20:32 +08:00
|
|
|
CXSourceLocation Loc;
|
2010-09-14 06:52:57 +08:00
|
|
|
if (I)
|
|
|
|
printf(", ");
|
|
|
|
|
2010-09-14 08:20:32 +08:00
|
|
|
Loc = clang_getCursorLocation(Ovl);
|
2010-11-09 14:24:54 +08:00
|
|
|
clang_getSpellingLocation(Loc, 0, &line, &column, 0);
|
2010-09-14 06:52:57 +08:00
|
|
|
printf("%d:%d", line, column);
|
|
|
|
}
|
|
|
|
printf("]");
|
|
|
|
} else {
|
|
|
|
CXSourceLocation Loc = clang_getCursorLocation(Referenced);
|
2010-11-09 14:24:54 +08:00
|
|
|
clang_getSpellingLocation(Loc, 0, &line, &column, 0);
|
2010-09-14 06:52:57 +08:00
|
|
|
printf(":%d:%d", line, column);
|
|
|
|
}
|
2010-01-19 09:20:04 +08:00
|
|
|
}
|
2010-01-20 03:34:47 +08:00
|
|
|
|
|
|
|
if (clang_isCursorDefinition(Cursor))
|
|
|
|
printf(" (Definition)");
|
2010-08-24 07:00:57 +08:00
|
|
|
|
|
|
|
switch (clang_getCursorAvailability(Cursor)) {
|
|
|
|
case CXAvailability_Available:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CXAvailability_Deprecated:
|
|
|
|
printf(" (deprecated)");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CXAvailability_NotAvailable:
|
|
|
|
printf(" (unavailable)");
|
|
|
|
break;
|
|
|
|
}
|
2010-08-26 09:42:22 +08:00
|
|
|
|
|
|
|
if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
|
|
|
|
CXType T =
|
|
|
|
clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor));
|
|
|
|
CXString S = clang_getTypeKindSpelling(T.kind);
|
|
|
|
printf(" [IBOutletCollection=%s]", clang_getCString(S));
|
|
|
|
clang_disposeString(S);
|
|
|
|
}
|
2010-08-28 05:34:58 +08:00
|
|
|
|
|
|
|
if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
|
|
|
|
enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
|
|
|
|
unsigned isVirtual = clang_isVirtualBase(Cursor);
|
|
|
|
const char *accessStr = 0;
|
|
|
|
|
|
|
|
switch (access) {
|
|
|
|
case CX_CXXInvalidAccessSpecifier:
|
|
|
|
accessStr = "invalid"; break;
|
|
|
|
case CX_CXXPublic:
|
|
|
|
accessStr = "public"; break;
|
|
|
|
case CX_CXXProtected:
|
|
|
|
accessStr = "protected"; break;
|
|
|
|
case CX_CXXPrivate:
|
|
|
|
accessStr = "private"; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf(" [access=%s isVirtual=%s]", accessStr,
|
|
|
|
isVirtual ? "true" : "false");
|
|
|
|
}
|
2010-09-02 08:07:54 +08:00
|
|
|
|
|
|
|
SpecializationOf = clang_getSpecializedCursorTemplate(Cursor);
|
|
|
|
if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
|
|
|
|
CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
|
|
|
|
CXString Name = clang_getCursorSpelling(SpecializationOf);
|
2010-11-09 14:24:54 +08:00
|
|
|
clang_getSpellingLocation(Loc, 0, &line, &column, 0);
|
2010-09-02 08:07:54 +08:00
|
|
|
printf(" [Specialization of %s:%d:%d]",
|
|
|
|
clang_getCString(Name), line, column);
|
|
|
|
clang_disposeString(Name);
|
|
|
|
}
|
2010-10-02 04:25:15 +08:00
|
|
|
|
|
|
|
clang_getOverriddenCursors(Cursor, &overridden, &num_overridden);
|
|
|
|
if (num_overridden) {
|
|
|
|
unsigned I;
|
|
|
|
printf(" [Overrides ");
|
|
|
|
for (I = 0; I != num_overridden; ++I) {
|
|
|
|
CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
|
2010-11-09 14:24:54 +08:00
|
|
|
clang_getSpellingLocation(Loc, 0, &line, &column, 0);
|
2010-10-02 04:25:15 +08:00
|
|
|
if (I)
|
|
|
|
printf(", ");
|
|
|
|
printf("@%d:%d", line, column);
|
|
|
|
}
|
|
|
|
printf("]");
|
|
|
|
clang_disposeOverriddenCursors(overridden);
|
|
|
|
}
|
2010-10-21 06:00:55 +08:00
|
|
|
|
|
|
|
if (Cursor.kind == CXCursor_InclusionDirective) {
|
|
|
|
CXFile File = clang_getIncludedFile(Cursor);
|
|
|
|
CXString Included = clang_getFileName(File);
|
|
|
|
printf(" (%s)", clang_getCString(Included));
|
|
|
|
clang_disposeString(Included);
|
|
|
|
}
|
2009-09-26 05:32:34 +08:00
|
|
|
}
|
2009-09-03 23:49:00 +08:00
|
|
|
}
|
2009-08-31 08:59:03 +08:00
|
|
|
|
2010-02-17 08:41:32 +08:00
|
|
|
static const char* GetCursorSource(CXCursor Cursor) {
|
2010-01-20 05:36:55 +08:00
|
|
|
CXSourceLocation Loc = clang_getCursorLocation(Cursor);
|
2010-02-17 08:41:20 +08:00
|
|
|
CXString source;
|
2010-01-20 05:36:55 +08:00
|
|
|
CXFile file;
|
2010-11-09 14:24:54 +08:00
|
|
|
clang_getSpellingLocation(Loc, &file, 0, 0, 0);
|
2010-01-20 05:36:55 +08:00
|
|
|
source = clang_getFileName(file);
|
2010-02-17 08:41:32 +08:00
|
|
|
if (!clang_getCString(source)) {
|
2010-02-17 08:41:20 +08:00
|
|
|
clang_disposeString(source);
|
|
|
|
return "<invalid loc>";
|
|
|
|
}
|
|
|
|
else {
|
2010-02-17 08:41:32 +08:00
|
|
|
const char *b = basename(clang_getCString(source));
|
2010-02-17 08:41:20 +08:00
|
|
|
clang_disposeString(source);
|
|
|
|
return b;
|
|
|
|
}
|
2009-11-17 13:31:58 +08:00
|
|
|
}
|
|
|
|
|
2010-01-27 01:59:48 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
/* Callbacks. */
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
typedef void (*PostVisitTU)(CXTranslationUnit);
|
|
|
|
|
2010-02-19 02:08:43 +08:00
|
|
|
void PrintDiagnostic(CXDiagnostic Diagnostic) {
|
|
|
|
FILE *out = stderr;
|
2010-01-28 08:27:43 +08:00
|
|
|
CXFile file;
|
2010-02-23 07:17:23 +08:00
|
|
|
CXString Msg;
|
2010-02-19 07:07:20 +08:00
|
|
|
unsigned display_opts = CXDiagnostic_DisplaySourceLocation
|
2010-11-20 00:18:16 +08:00
|
|
|
| CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges
|
|
|
|
| CXDiagnostic_DisplayOption;
|
2010-02-19 07:07:20 +08:00
|
|
|
unsigned i, num_fixits;
|
2010-03-25 10:00:39 +08:00
|
|
|
|
2010-02-19 07:07:20 +08:00
|
|
|
if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
|
2010-01-28 08:27:43 +08:00
|
|
|
return;
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2010-02-23 07:17:23 +08:00
|
|
|
Msg = clang_formatDiagnostic(Diagnostic, display_opts);
|
|
|
|
fprintf(stderr, "%s\n", clang_getCString(Msg));
|
|
|
|
clang_disposeString(Msg);
|
2010-03-25 10:00:39 +08:00
|
|
|
|
2010-11-09 14:24:54 +08:00
|
|
|
clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
|
|
|
|
&file, 0, 0, 0);
|
2010-02-19 07:07:20 +08:00
|
|
|
if (!file)
|
|
|
|
return;
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2010-02-19 07:07:20 +08:00
|
|
|
num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
|
|
|
|
for (i = 0; i != num_fixits; ++i) {
|
2010-02-20 02:16:06 +08:00
|
|
|
CXSourceRange range;
|
|
|
|
CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
|
|
|
|
CXSourceLocation start = clang_getRangeStart(range);
|
|
|
|
CXSourceLocation end = clang_getRangeEnd(range);
|
|
|
|
unsigned start_line, start_column, end_line, end_column;
|
|
|
|
CXFile start_file, end_file;
|
2010-11-09 14:24:54 +08:00
|
|
|
clang_getSpellingLocation(start, &start_file, &start_line,
|
|
|
|
&start_column, 0);
|
|
|
|
clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
|
2010-02-20 02:16:06 +08:00
|
|
|
if (clang_equalLocations(start, end)) {
|
|
|
|
/* Insertion. */
|
|
|
|
if (start_file == file)
|
2010-02-19 07:07:20 +08:00
|
|
|
fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
|
2010-02-20 02:16:06 +08:00
|
|
|
clang_getCString(insertion_text), start_line, start_column);
|
|
|
|
} else if (strcmp(clang_getCString(insertion_text), "") == 0) {
|
|
|
|
/* Removal. */
|
2010-02-19 07:07:20 +08:00
|
|
|
if (start_file == file && end_file == file) {
|
|
|
|
fprintf(out, "FIX-IT: Remove ");
|
|
|
|
PrintExtent(out, start_line, start_column, end_line, end_column);
|
|
|
|
fprintf(out, "\n");
|
2010-02-19 06:27:07 +08:00
|
|
|
}
|
2010-02-20 02:16:06 +08:00
|
|
|
} else {
|
|
|
|
/* Replacement. */
|
2010-02-19 07:07:20 +08:00
|
|
|
if (start_file == end_file) {
|
|
|
|
fprintf(out, "FIX-IT: Replace ");
|
|
|
|
PrintExtent(out, start_line, start_column, end_line, end_column);
|
2010-02-20 02:16:06 +08:00
|
|
|
fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
|
2010-01-29 08:41:11 +08:00
|
|
|
}
|
2010-02-19 07:07:20 +08:00
|
|
|
break;
|
|
|
|
}
|
2010-02-20 02:16:06 +08:00
|
|
|
clang_disposeString(insertion_text);
|
2010-01-29 08:41:11 +08:00
|
|
|
}
|
2010-01-28 08:27:43 +08:00
|
|
|
}
|
|
|
|
|
2010-02-19 02:08:43 +08:00
|
|
|
void PrintDiagnostics(CXTranslationUnit TU) {
|
|
|
|
int i, n = clang_getNumDiagnostics(TU);
|
|
|
|
for (i = 0; i != n; ++i) {
|
|
|
|
CXDiagnostic Diag = clang_getDiagnostic(TU, i);
|
|
|
|
PrintDiagnostic(Diag);
|
|
|
|
clang_disposeDiagnostic(Diag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-18 02:13:31 +08:00
|
|
|
/******************************************************************************/
|
2010-01-21 05:32:04 +08:00
|
|
|
/* Logic for testing traversal. */
|
2009-11-18 02:13:31 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2010-01-06 07:18:49 +08:00
|
|
|
static const char *FileCheckPrefix = "CHECK";
|
|
|
|
|
2010-01-19 08:34:46 +08:00
|
|
|
static void PrintCursorExtent(CXCursor C) {
|
|
|
|
CXSourceRange extent = clang_getCursorExtent(C);
|
2010-01-20 05:36:55 +08:00
|
|
|
CXFile begin_file, end_file;
|
|
|
|
unsigned begin_line, begin_column, end_line, end_column;
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2010-11-09 14:24:54 +08:00
|
|
|
clang_getSpellingLocation(clang_getRangeStart(extent),
|
|
|
|
&begin_file, &begin_line, &begin_column, 0);
|
|
|
|
clang_getSpellingLocation(clang_getRangeEnd(extent),
|
|
|
|
&end_file, &end_line, &end_column, 0);
|
2010-01-20 05:36:55 +08:00
|
|
|
if (!begin_file || !end_file)
|
2010-01-16 09:44:12 +08:00
|
|
|
return;
|
2010-01-20 05:36:55 +08:00
|
|
|
|
2010-02-14 16:32:24 +08:00
|
|
|
printf(" Extent=");
|
|
|
|
PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
|
2010-01-06 07:18:49 +08:00
|
|
|
}
|
|
|
|
|
2010-01-21 05:32:04 +08:00
|
|
|
/* Data used by all of the visitors. */
|
|
|
|
typedef struct {
|
|
|
|
CXTranslationUnit TU;
|
|
|
|
enum CXCursorKind *Filter;
|
|
|
|
} VisitorData;
|
2010-01-06 07:18:49 +08:00
|
|
|
|
2009-12-01 10:35:37 +08:00
|
|
|
|
2010-02-17 08:41:32 +08:00
|
|
|
enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
|
2010-01-21 05:32:04 +08:00
|
|
|
CXCursor Parent,
|
|
|
|
CXClientData ClientData) {
|
|
|
|
VisitorData *Data = (VisitorData *)ClientData;
|
|
|
|
if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
|
2010-01-19 06:46:11 +08:00
|
|
|
CXSourceLocation Loc = clang_getCursorLocation(Cursor);
|
2010-01-20 05:36:55 +08:00
|
|
|
unsigned line, column;
|
2010-11-09 14:24:54 +08:00
|
|
|
clang_getSpellingLocation(Loc, 0, &line, &column, 0);
|
2010-01-06 07:18:49 +08:00
|
|
|
printf("// %s: %s:%d:%d: ", FileCheckPrefix,
|
2010-01-20 05:36:55 +08:00
|
|
|
GetCursorSource(Cursor), line, column);
|
2009-09-03 23:49:00 +08:00
|
|
|
PrintCursor(Cursor);
|
2010-01-19 08:34:46 +08:00
|
|
|
PrintCursorExtent(Cursor);
|
2010-02-17 08:41:32 +08:00
|
|
|
printf("\n");
|
2010-01-21 05:32:04 +08:00
|
|
|
return CXChildVisit_Recurse;
|
2009-12-01 10:35:37 +08:00
|
|
|
}
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2010-01-21 05:32:04 +08:00
|
|
|
return CXChildVisit_Continue;
|
2009-12-01 10:35:37 +08:00
|
|
|
}
|
2009-09-02 21:28:54 +08:00
|
|
|
|
2010-02-17 08:41:32 +08:00
|
|
|
static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
|
2010-01-21 05:32:04 +08:00
|
|
|
CXCursor Parent,
|
|
|
|
CXClientData ClientData) {
|
2009-12-01 10:35:37 +08:00
|
|
|
const char *startBuf, *endBuf;
|
|
|
|
unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
|
|
|
|
CXCursor Ref;
|
2010-01-21 05:32:04 +08:00
|
|
|
VisitorData *Data = (VisitorData *)ClientData;
|
2009-12-01 10:35:37 +08:00
|
|
|
|
2010-01-20 03:34:47 +08:00
|
|
|
if (Cursor.kind != CXCursor_FunctionDecl ||
|
|
|
|
!clang_isCursorDefinition(Cursor))
|
2010-01-21 05:32:04 +08:00
|
|
|
return CXChildVisit_Continue;
|
2009-12-01 10:35:37 +08:00
|
|
|
|
|
|
|
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) {
|
2010-01-19 06:46:11 +08:00
|
|
|
CXSourceLocation Loc;
|
2010-01-20 05:36:55 +08:00
|
|
|
CXFile file;
|
2010-02-17 08:41:20 +08:00
|
|
|
CXString source;
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2009-12-01 10:35:37 +08:00
|
|
|
if (*startBuf == '\n') {
|
|
|
|
startBuf++;
|
|
|
|
curLine++;
|
|
|
|
curColumn = 1;
|
|
|
|
} else if (*startBuf != '\t')
|
|
|
|
curColumn++;
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2010-01-19 06:46:11 +08:00
|
|
|
Loc = clang_getCursorLocation(Cursor);
|
2010-11-09 14:24:54 +08:00
|
|
|
clang_getSpellingLocation(Loc, &file, 0, 0, 0);
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2010-01-20 05:36:55 +08:00
|
|
|
source = clang_getFileName(file);
|
2010-02-17 08:41:32 +08:00
|
|
|
if (clang_getCString(source)) {
|
2010-01-23 05:44:22 +08:00
|
|
|
CXSourceLocation RefLoc
|
|
|
|
= clang_getLocation(Data->TU, file, curLine, curColumn);
|
|
|
|
Ref = clang_getCursor(Data->TU, RefLoc);
|
2010-01-19 06:46:11 +08:00
|
|
|
if (Ref.kind == CXCursor_NoDeclFound) {
|
|
|
|
/* Nothing found here; that's fine. */
|
|
|
|
} else if (Ref.kind != CXCursor_FunctionDecl) {
|
|
|
|
printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
|
|
|
|
curLine, curColumn);
|
|
|
|
PrintCursor(Ref);
|
|
|
|
printf("\n");
|
|
|
|
}
|
2009-09-24 01:52:52 +08:00
|
|
|
}
|
2010-02-17 08:41:20 +08:00
|
|
|
clang_disposeString(source);
|
2009-12-01 10:35:37 +08:00
|
|
|
startBuf++;
|
2009-08-31 22:26:51 +08:00
|
|
|
}
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2010-01-21 05:32:04 +08:00
|
|
|
return CXChildVisit_Continue;
|
2009-08-31 08:59:03 +08:00
|
|
|
}
|
2009-08-28 23:28:48 +08:00
|
|
|
|
2010-01-13 07:34:26 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
/* USR testing. */
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2010-01-21 05:32:04 +08:00
|
|
|
enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
|
|
|
|
CXClientData ClientData) {
|
|
|
|
VisitorData *Data = (VisitorData *)ClientData;
|
|
|
|
if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
|
2010-01-19 04:23:29 +08:00
|
|
|
CXString USR = clang_getCursorUSR(C);
|
2010-04-21 07:15:40 +08:00
|
|
|
const char *cstr = clang_getCString(USR);
|
|
|
|
if (!cstr || cstr[0] == '\0') {
|
2010-01-13 07:34:26 +08:00
|
|
|
clang_disposeString(USR);
|
2010-04-17 05:31:52 +08:00
|
|
|
return CXChildVisit_Recurse;
|
2010-01-13 07:34:26 +08:00
|
|
|
}
|
2010-04-21 07:15:40 +08:00
|
|
|
printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
|
|
|
|
|
2010-01-19 08:34:46 +08:00
|
|
|
PrintCursorExtent(C);
|
2010-01-13 07:34:26 +08:00
|
|
|
printf("\n");
|
|
|
|
clang_disposeString(USR);
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2010-01-21 05:32:04 +08:00
|
|
|
return CXChildVisit_Recurse;
|
2010-02-17 08:41:32 +08:00
|
|
|
}
|
|
|
|
|
2010-01-21 05:32:04 +08:00
|
|
|
return CXChildVisit_Continue;
|
2010-01-13 07:34:26 +08:00
|
|
|
}
|
|
|
|
|
2010-01-27 03:31:51 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
/* Inclusion stack testing. */
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
|
|
|
|
unsigned includeStackLen, CXClientData data) {
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2010-01-27 03:31:51 +08:00
|
|
|
unsigned i;
|
2010-02-17 08:41:20 +08:00
|
|
|
CXString fname;
|
|
|
|
|
|
|
|
fname = clang_getFileName(includedFile);
|
2010-02-17 08:41:32 +08:00
|
|
|
printf("file: %s\nincluded by:\n", clang_getCString(fname));
|
2010-02-17 08:41:20 +08:00
|
|
|
clang_disposeString(fname);
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2010-01-27 03:31:51 +08:00
|
|
|
for (i = 0; i < includeStackLen; ++i) {
|
|
|
|
CXFile includingFile;
|
|
|
|
unsigned line, column;
|
2010-11-09 14:24:54 +08:00
|
|
|
clang_getSpellingLocation(includeStack[i], &includingFile, &line,
|
|
|
|
&column, 0);
|
2010-02-17 08:41:20 +08:00
|
|
|
fname = clang_getFileName(includingFile);
|
2010-02-17 08:41:32 +08:00
|
|
|
printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
|
2010-02-17 08:41:20 +08:00
|
|
|
clang_disposeString(fname);
|
2010-01-27 03:31:51 +08:00
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintInclusionStack(CXTranslationUnit TU) {
|
2010-02-17 08:41:32 +08:00
|
|
|
clang_getInclusions(TU, InclusionVisitor, NULL);
|
2010-01-27 03:31:51 +08:00
|
|
|
}
|
|
|
|
|
2010-03-03 14:37:58 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
/* Linkage testing. */
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
|
|
|
|
CXClientData d) {
|
|
|
|
const char *linkage = 0;
|
|
|
|
|
|
|
|
if (clang_isInvalid(clang_getCursorKind(cursor)))
|
|
|
|
return CXChildVisit_Recurse;
|
|
|
|
|
|
|
|
switch (clang_getCursorLinkage(cursor)) {
|
|
|
|
case CXLinkage_Invalid: break;
|
2010-03-05 03:36:27 +08:00
|
|
|
case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
|
|
|
|
case CXLinkage_Internal: linkage = "Internal"; break;
|
|
|
|
case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
|
|
|
|
case CXLinkage_External: linkage = "External"; break;
|
2010-03-03 14:37:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (linkage) {
|
|
|
|
PrintCursor(cursor);
|
|
|
|
printf("linkage=%s\n", linkage);
|
|
|
|
}
|
|
|
|
|
|
|
|
return CXChildVisit_Recurse;
|
|
|
|
}
|
|
|
|
|
2010-05-15 05:29:26 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
/* Typekind testing. */
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p,
|
|
|
|
CXClientData d) {
|
|
|
|
|
|
|
|
if (!clang_isInvalid(clang_getCursorKind(cursor))) {
|
|
|
|
CXType T = clang_getCursorType(cursor);
|
|
|
|
CXString S = clang_getTypeKindSpelling(T.kind);
|
|
|
|
PrintCursor(cursor);
|
|
|
|
printf(" typekind=%s", clang_getCString(S));
|
2011-01-28 00:27:11 +08:00
|
|
|
if (clang_isConstQualifiedType(T))
|
|
|
|
printf(" const");
|
|
|
|
if (clang_isVolatileQualifiedType(T))
|
|
|
|
printf(" volatile");
|
|
|
|
if (clang_isRestrictQualifiedType(T))
|
|
|
|
printf(" restrict");
|
2010-05-15 05:29:26 +08:00
|
|
|
clang_disposeString(S);
|
2010-06-22 17:29:44 +08:00
|
|
|
/* Print the canonical type if it is different. */
|
2010-06-22 04:15:39 +08:00
|
|
|
{
|
|
|
|
CXType CT = clang_getCanonicalType(T);
|
|
|
|
if (!clang_equalTypes(T, CT)) {
|
|
|
|
CXString CS = clang_getTypeKindSpelling(CT.kind);
|
|
|
|
printf(" [canonical=%s]", clang_getCString(CS));
|
|
|
|
clang_disposeString(CS);
|
|
|
|
}
|
|
|
|
}
|
2010-06-22 17:29:44 +08:00
|
|
|
/* Print the return type if it exists. */
|
2010-06-22 04:15:39 +08:00
|
|
|
{
|
2010-06-22 04:48:56 +08:00
|
|
|
CXType RT = clang_getCursorResultType(cursor);
|
2010-06-22 04:15:39 +08:00
|
|
|
if (RT.kind != CXType_Invalid) {
|
|
|
|
CXString RS = clang_getTypeKindSpelling(RT.kind);
|
|
|
|
printf(" [result=%s]", clang_getCString(RS));
|
|
|
|
clang_disposeString(RS);
|
|
|
|
}
|
|
|
|
}
|
2010-07-30 08:14:11 +08:00
|
|
|
/* Print if this is a non-POD type. */
|
|
|
|
printf(" [isPOD=%d]", clang_isPODType(T));
|
2010-06-22 04:15:39 +08:00
|
|
|
|
2010-05-15 05:29:26 +08:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
return CXChildVisit_Recurse;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-13 07:34:26 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
/* Loading ASTs/source. */
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2009-12-01 10:35:37 +08:00
|
|
|
static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
|
2010-01-13 02:53:15 +08:00
|
|
|
const char *filter, const char *prefix,
|
2010-01-27 01:59:48 +08:00
|
|
|
CXCursorVisitor Visitor,
|
|
|
|
PostVisitTU PV) {
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2010-01-06 07:18:49 +08:00
|
|
|
if (prefix)
|
2010-02-17 08:41:32 +08:00
|
|
|
FileCheckPrefix = prefix;
|
2010-01-27 01:55:33 +08:00
|
|
|
|
|
|
|
if (Visitor) {
|
|
|
|
enum CXCursorKind K = CXCursor_NotImplemented;
|
|
|
|
enum CXCursorKind *ck = &K;
|
|
|
|
VisitorData Data;
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2010-01-27 01:55:33 +08:00
|
|
|
/* Perform some simple filtering. */
|
|
|
|
if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
|
2010-10-03 06:49:11 +08:00
|
|
|
else if (!strcmp(filter, "all-display") ||
|
|
|
|
!strcmp(filter, "local-display")) {
|
|
|
|
ck = NULL;
|
|
|
|
want_display_name = 1;
|
|
|
|
}
|
2010-02-11 04:42:40 +08:00
|
|
|
else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
|
2010-01-27 01:55:33 +08:00
|
|
|
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;
|
|
|
|
}
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2010-01-27 01:55:33 +08:00
|
|
|
Data.TU = TU;
|
|
|
|
Data.Filter = ck;
|
|
|
|
clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
|
|
|
|
}
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2010-01-27 01:59:48 +08:00
|
|
|
if (PV)
|
|
|
|
PV(TU);
|
2010-01-27 01:55:33 +08:00
|
|
|
|
2010-02-19 02:08:43 +08:00
|
|
|
PrintDiagnostics(TU);
|
2009-11-18 02:13:31 +08:00
|
|
|
clang_disposeTranslationUnit(TU);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-01-06 07:18:49 +08:00
|
|
|
int perform_test_load_tu(const char *file, const char *filter,
|
2010-01-27 01:59:48 +08:00
|
|
|
const char *prefix, CXCursorVisitor Visitor,
|
|
|
|
PostVisitTU PV) {
|
2009-12-01 10:35:37 +08:00
|
|
|
CXIndex Idx;
|
|
|
|
CXTranslationUnit TU;
|
2010-02-11 15:41:25 +08:00
|
|
|
int result;
|
2010-02-17 08:41:32 +08:00
|
|
|
Idx = clang_createIndex(/* excludeDeclsFromPCH */
|
2010-02-19 07:07:20 +08:00
|
|
|
!strcmp(filter, "local") ? 1 : 0,
|
|
|
|
/* displayDiagnosics=*/1);
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2010-02-11 15:41:25 +08:00
|
|
|
if (!CreateTranslationUnit(Idx, file, &TU)) {
|
|
|
|
clang_disposeIndex(Idx);
|
2009-12-01 10:35:37 +08:00
|
|
|
return 1;
|
2010-02-11 15:41:25 +08:00
|
|
|
}
|
2009-12-01 10:35:37 +08:00
|
|
|
|
2010-02-11 15:41:25 +08:00
|
|
|
result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
|
|
|
|
clang_disposeIndex(Idx);
|
|
|
|
return result;
|
2009-12-01 10:35:37 +08:00
|
|
|
}
|
|
|
|
|
2010-01-27 01:59:48 +08:00
|
|
|
int perform_test_load_source(int argc, const char **argv,
|
|
|
|
const char *filter, CXCursorVisitor Visitor,
|
|
|
|
PostVisitTU PV) {
|
2009-12-01 10:03:10 +08:00
|
|
|
CXIndex Idx;
|
|
|
|
CXTranslationUnit TU;
|
2010-01-23 08:14:00 +08:00
|
|
|
struct CXUnsavedFile *unsaved_files = 0;
|
|
|
|
int num_unsaved_files = 0;
|
|
|
|
int result;
|
2010-07-20 05:46:24 +08:00
|
|
|
|
2009-12-01 10:03:10 +08:00
|
|
|
Idx = clang_createIndex(/* excludeDeclsFromPCH */
|
2010-10-03 06:49:11 +08:00
|
|
|
(!strcmp(filter, "local") ||
|
|
|
|
!strcmp(filter, "local-display"))? 1 : 0,
|
2011-02-04 07:41:12 +08:00
|
|
|
/* displayDiagnosics=*/0);
|
2009-12-01 10:03:10 +08:00
|
|
|
|
2010-02-11 15:41:25 +08:00
|
|
|
if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
|
|
|
|
clang_disposeIndex(Idx);
|
2010-01-23 08:14:00 +08:00
|
|
|
return -1;
|
2010-02-11 15:41:25 +08:00
|
|
|
}
|
2010-01-23 08:14:00 +08:00
|
|
|
|
2010-02-17 08:41:32 +08:00
|
|
|
TU = clang_createTranslationUnitFromSourceFile(Idx, 0,
|
|
|
|
argc - num_unsaved_files,
|
2010-01-23 08:14:00 +08:00
|
|
|
argv + num_unsaved_files,
|
|
|
|
num_unsaved_files,
|
2010-02-19 02:08:43 +08:00
|
|
|
unsaved_files);
|
2009-12-01 10:03:10 +08:00
|
|
|
if (!TU) {
|
|
|
|
fprintf(stderr, "Unable to load translation unit!\n");
|
2010-07-20 05:46:24 +08:00
|
|
|
free_remapped_files(unsaved_files, num_unsaved_files);
|
2010-02-11 15:41:25 +08:00
|
|
|
clang_disposeIndex(Idx);
|
2009-12-01 10:03:10 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-01-27 01:59:48 +08:00
|
|
|
result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
|
2010-01-23 08:14:00 +08:00
|
|
|
free_remapped_files(unsaved_files, num_unsaved_files);
|
2010-02-11 15:41:25 +08:00
|
|
|
clang_disposeIndex(Idx);
|
2010-01-23 08:14:00 +08:00
|
|
|
return result;
|
2009-12-01 10:03:10 +08:00
|
|
|
}
|
|
|
|
|
2010-07-20 05:46:24 +08:00
|
|
|
int perform_test_reparse_source(int argc, const char **argv, int trials,
|
|
|
|
const char *filter, CXCursorVisitor Visitor,
|
|
|
|
PostVisitTU PV) {
|
|
|
|
CXIndex Idx;
|
|
|
|
CXTranslationUnit TU;
|
|
|
|
struct CXUnsavedFile *unsaved_files = 0;
|
|
|
|
int num_unsaved_files = 0;
|
|
|
|
int result;
|
|
|
|
int trial;
|
|
|
|
|
|
|
|
Idx = clang_createIndex(/* excludeDeclsFromPCH */
|
|
|
|
!strcmp(filter, "local") ? 1 : 0,
|
2011-01-28 02:02:58 +08:00
|
|
|
/* displayDiagnosics=*/0);
|
2010-07-20 05:46:24 +08:00
|
|
|
|
|
|
|
if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
|
|
|
|
clang_disposeIndex(Idx);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-08-19 07:09:16 +08:00
|
|
|
/* Load the initial translation unit -- we do this without honoring remapped
|
|
|
|
* files, so that we have a way to test results after changing the source. */
|
2010-07-23 08:33:23 +08:00
|
|
|
TU = clang_parseTranslationUnit(Idx, 0,
|
|
|
|
argv + num_unsaved_files,
|
|
|
|
argc - num_unsaved_files,
|
2010-08-19 07:09:16 +08:00
|
|
|
0, 0, getDefaultParsingOptions());
|
2010-07-20 05:46:24 +08:00
|
|
|
if (!TU) {
|
|
|
|
fprintf(stderr, "Unable to load translation unit!\n");
|
|
|
|
free_remapped_files(unsaved_files, num_unsaved_files);
|
|
|
|
clang_disposeIndex(Idx);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (trial = 0; trial < trials; ++trial) {
|
2010-08-11 23:58:42 +08:00
|
|
|
if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
|
|
|
|
clang_defaultReparseOptions(TU))) {
|
2010-08-19 07:09:16 +08:00
|
|
|
fprintf(stderr, "Unable to reparse translation unit!\n");
|
2010-07-20 05:46:24 +08:00
|
|
|
clang_disposeTranslationUnit(TU);
|
|
|
|
free_remapped_files(unsaved_files, num_unsaved_files);
|
|
|
|
clang_disposeIndex(Idx);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
|
|
|
|
free_remapped_files(unsaved_files, num_unsaved_files);
|
|
|
|
clang_disposeIndex(Idx);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-11-18 03:37:36 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
/* Logic for testing clang_getCursor(). */
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
static void print_cursor_file_scan(CXCursor cursor,
|
|
|
|
unsigned start_line, unsigned start_col,
|
2009-11-18 10:02:52 +08:00
|
|
|
unsigned end_line, unsigned end_col,
|
|
|
|
const char *prefix) {
|
2010-01-07 09:17:12 +08:00
|
|
|
printf("// %s: ", FileCheckPrefix);
|
2009-11-18 10:02:52 +08:00
|
|
|
if (prefix)
|
|
|
|
printf("-%s", prefix);
|
2010-02-14 16:32:24 +08:00
|
|
|
PrintExtent(stdout, start_line, start_col, end_line, end_col);
|
|
|
|
printf(" ");
|
2009-11-18 03:37:36 +08:00
|
|
|
PrintCursor(cursor);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2009-11-18 10:02:52 +08:00
|
|
|
static int perform_file_scan(const char *ast_file, const char *source_file,
|
|
|
|
const char *prefix) {
|
2009-11-18 03:37:36 +08:00
|
|
|
CXIndex Idx;
|
|
|
|
CXTranslationUnit TU;
|
|
|
|
FILE *fp;
|
2010-02-14 16:32:32 +08:00
|
|
|
CXCursor prevCursor = clang_getNullCursor();
|
2010-01-23 05:44:22 +08:00
|
|
|
CXFile file;
|
2010-02-14 16:32:32 +08:00
|
|
|
unsigned line = 1, col = 1;
|
2010-02-14 16:32:51 +08:00
|
|
|
unsigned start_line = 1, start_col = 1;
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2010-02-19 07:07:20 +08:00
|
|
|
if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
|
|
|
|
/* displayDiagnosics=*/1))) {
|
2009-11-18 03:37:36 +08:00
|
|
|
fprintf(stderr, "Could not create Index\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2009-11-18 03:37:36 +08:00
|
|
|
if (!CreateTranslationUnit(Idx, ast_file, &TU))
|
|
|
|
return 1;
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2009-11-18 03:37:36 +08:00
|
|
|
if ((fp = fopen(source_file, "r")) == NULL) {
|
|
|
|
fprintf(stderr, "Could not open '%s'\n", source_file);
|
|
|
|
return 1;
|
|
|
|
}
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2010-01-23 05:44:22 +08:00
|
|
|
file = clang_getFile(TU, source_file);
|
2010-02-14 16:32:32 +08:00
|
|
|
for (;;) {
|
|
|
|
CXCursor cursor;
|
|
|
|
int c = fgetc(fp);
|
|
|
|
|
|
|
|
if (c == '\n') {
|
|
|
|
++line;
|
|
|
|
col = 1;
|
|
|
|
} else
|
|
|
|
++col;
|
|
|
|
|
|
|
|
/* Check the cursor at this position, and dump the previous one if we have
|
|
|
|
* found something new.
|
|
|
|
*/
|
|
|
|
cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
|
|
|
|
if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
|
|
|
|
prevCursor.kind != CXCursor_InvalidFile) {
|
|
|
|
print_cursor_file_scan(prevCursor, start_line, start_col,
|
2010-02-14 18:02:57 +08:00
|
|
|
line, col, prefix);
|
2010-02-14 16:32:32 +08:00
|
|
|
start_line = line;
|
|
|
|
start_col = col;
|
2009-11-18 04:51:40 +08:00
|
|
|
}
|
2010-02-14 16:32:32 +08:00
|
|
|
if (c == EOF)
|
|
|
|
break;
|
2009-11-18 04:51:40 +08:00
|
|
|
|
2010-02-14 16:32:32 +08:00
|
|
|
prevCursor = cursor;
|
2009-11-18 03:37:36 +08:00
|
|
|
}
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2009-11-18 03:37:36 +08:00
|
|
|
fclose(fp);
|
2011-02-01 06:04:05 +08:00
|
|
|
clang_disposeTranslationUnit(TU);
|
|
|
|
clang_disposeIndex(Idx);
|
2009-11-18 03:37:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-11-18 02:13:31 +08:00
|
|
|
/******************************************************************************/
|
2010-10-12 05:37:58 +08:00
|
|
|
/* Logic for testing clang code completion. */
|
2009-11-18 02:13:31 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2009-11-07 08:00:49 +08:00
|
|
|
/* 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. */
|
2010-02-17 08:41:32 +08:00
|
|
|
int parse_file_line_column(const char *input, char **filename, unsigned *line,
|
2010-01-27 01:06:03 +08:00
|
|
|
unsigned *column, unsigned *second_line,
|
|
|
|
unsigned *second_column) {
|
2009-11-10 02:19:57 +08:00
|
|
|
/* Find the second colon. */
|
2010-01-27 01:06:03 +08:00
|
|
|
const char *last_colon = strrchr(input, ':');
|
|
|
|
unsigned values[4], i;
|
|
|
|
unsigned num_values = (second_line && second_column)? 4 : 2;
|
|
|
|
|
2009-11-07 08:00:49 +08:00
|
|
|
char *endptr = 0;
|
2010-01-27 01:06:03 +08:00
|
|
|
if (!last_colon || last_colon == input) {
|
|
|
|
if (num_values == 4)
|
|
|
|
fprintf(stderr, "could not parse filename:line:column:line:column in "
|
|
|
|
"'%s'\n", input);
|
|
|
|
else
|
|
|
|
fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
|
2009-11-07 08:00:49 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-01-27 01:06:03 +08:00
|
|
|
for (i = 0; i != num_values; ++i) {
|
|
|
|
const char *prev_colon;
|
2009-11-10 02:19:57 +08:00
|
|
|
|
2010-01-27 01:06:03 +08:00
|
|
|
/* Parse the next line or column. */
|
|
|
|
values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
|
|
|
|
if (*endptr != 0 && *endptr != ':') {
|
2010-02-17 08:41:32 +08:00
|
|
|
fprintf(stderr, "could not parse %s in '%s'\n",
|
2010-01-27 01:06:03 +08:00
|
|
|
(i % 2 ? "column" : "line"), input);
|
|
|
|
return 1;
|
|
|
|
}
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2010-01-27 01:06:03 +08:00
|
|
|
if (i + 1 == num_values)
|
|
|
|
break;
|
2009-11-10 02:19:57 +08:00
|
|
|
|
2010-01-27 01:06:03 +08:00
|
|
|
/* Find the previous colon. */
|
|
|
|
prev_colon = last_colon - 1;
|
|
|
|
while (prev_colon != input && *prev_colon != ':')
|
|
|
|
--prev_colon;
|
|
|
|
if (prev_colon == input) {
|
2010-02-17 08:41:32 +08:00
|
|
|
fprintf(stderr, "could not parse %s in '%s'\n",
|
2010-01-27 01:06:03 +08:00
|
|
|
(i % 2 == 0? "column" : "line"), input);
|
2010-02-17 08:41:32 +08:00
|
|
|
return 1;
|
2010-01-27 01:06:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
last_colon = prev_colon;
|
2009-11-07 08:00:49 +08:00
|
|
|
}
|
2010-01-27 01:06:03 +08:00
|
|
|
|
|
|
|
*line = values[0];
|
|
|
|
*column = values[1];
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2010-01-27 01:06:03 +08:00
|
|
|
if (second_line && second_column) {
|
|
|
|
*second_line = values[2];
|
|
|
|
*second_column = values[3];
|
|
|
|
}
|
|
|
|
|
2009-11-10 02:19:57 +08:00
|
|
|
/* Copy the file name. */
|
2010-01-27 01:06:03 +08:00
|
|
|
*filename = (char*)malloc(last_colon - input + 1);
|
|
|
|
memcpy(*filename, input, last_colon - input);
|
|
|
|
(*filename)[last_colon - input] = 0;
|
2009-11-07 08:00:49 +08:00
|
|
|
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";
|
2009-12-19 02:53:37 +08:00
|
|
|
case CXCompletionChunk_ResultType: return "ResultType";
|
Improve code completion by introducing patterns for the various C and
C++ grammatical constructs that show up in top-level (namespace-level)
declarations, member declarations, template declarations, statements,
expressions, conditions, etc. For example, we now provide a pattern
for
static_cast<type>(expr)
when we can have an expression, or
using namespace identifier;
when we can have a using directive.
Also, improves the results of code completion at the beginning of a
top-level declaration. Previously, we would see value names (function
names, global variables, etc.); now we see types, namespace names,
etc., but no values.
llvm-svn: 93134
2010-01-11 07:08:15 +08:00
|
|
|
case CXCompletionChunk_Colon: return "Colon";
|
|
|
|
case CXCompletionChunk_SemiColon: return "SemiColon";
|
|
|
|
case CXCompletionChunk_Equal: return "Equal";
|
|
|
|
case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
|
|
|
|
case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
|
2009-11-07 08:00:49 +08:00
|
|
|
}
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2009-11-07 08:00:49 +08:00
|
|
|
return "Unknown";
|
|
|
|
}
|
|
|
|
|
2009-11-10 00:04:45 +08:00
|
|
|
void print_completion_string(CXCompletionString completion_string, FILE *file) {
|
2009-11-08 02:34:24 +08:00
|
|
|
int I, N;
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2009-11-10 00:04:45 +08:00
|
|
|
N = clang_getNumCompletionChunks(completion_string);
|
2009-11-07 08:00:49 +08:00
|
|
|
for (I = 0; I != N; ++I) {
|
2010-02-17 09:42:24 +08:00
|
|
|
CXString text;
|
|
|
|
const char *cstr;
|
2009-11-07 08:00:49 +08:00
|
|
|
enum CXCompletionChunkKind Kind
|
2009-11-10 00:04:45 +08:00
|
|
|
= clang_getCompletionChunkKind(completion_string, I);
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2009-11-10 00:04:45 +08:00
|
|
|
if (Kind == CXCompletionChunk_Optional) {
|
|
|
|
fprintf(file, "{Optional ");
|
|
|
|
print_completion_string(
|
2010-02-17 08:41:32 +08:00
|
|
|
clang_getCompletionChunkCompletionString(completion_string, I),
|
2009-11-10 00:04:45 +08:00
|
|
|
file);
|
|
|
|
fprintf(file, "}");
|
|
|
|
continue;
|
2010-10-09 04:39:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Kind == CXCompletionChunk_VerticalSpace) {
|
|
|
|
fprintf(file, "{VerticalSpace }");
|
|
|
|
continue;
|
2009-11-10 00:04:45 +08:00
|
|
|
}
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2009-11-10 01:05:28 +08:00
|
|
|
text = clang_getCompletionChunkText(completion_string, I);
|
2010-02-17 09:42:24 +08:00
|
|
|
cstr = clang_getCString(text);
|
2010-02-17 08:41:32 +08:00
|
|
|
fprintf(file, "{%s %s}",
|
2009-11-07 08:00:49 +08:00
|
|
|
clang_getCompletionChunkKindSpelling(Kind),
|
2010-02-17 09:42:24 +08:00
|
|
|
cstr ? cstr : "");
|
|
|
|
clang_disposeString(text);
|
2009-11-07 08:00:49 +08:00
|
|
|
}
|
2010-02-17 09:42:24 +08:00
|
|
|
|
2009-11-10 00:04:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void print_completion_result(CXCompletionResult *completion_result,
|
|
|
|
CXClientData client_data) {
|
|
|
|
FILE *file = (FILE *)client_data;
|
2010-02-17 08:41:32 +08:00
|
|
|
CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
|
|
|
|
|
|
|
|
fprintf(file, "%s:", clang_getCString(ks));
|
|
|
|
clang_disposeString(ks);
|
|
|
|
|
2009-11-10 00:04:45 +08:00
|
|
|
print_completion_string(completion_result->CompletionString, file);
|
2010-08-24 07:00:57 +08:00
|
|
|
fprintf(file, " (%u)",
|
2010-05-27 06:00:08 +08:00
|
|
|
clang_getCompletionPriority(completion_result->CompletionString));
|
2010-08-24 07:00:57 +08:00
|
|
|
switch (clang_getCompletionAvailability(completion_result->CompletionString)){
|
|
|
|
case CXAvailability_Available:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CXAvailability_Deprecated:
|
|
|
|
fprintf(file, " (deprecated)");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CXAvailability_NotAvailable:
|
|
|
|
fprintf(file, " (unavailable)");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fprintf(file, "\n");
|
2009-11-07 08:00:49 +08:00
|
|
|
}
|
|
|
|
|
2010-08-26 21:48:20 +08:00
|
|
|
int my_stricmp(const char *s1, const char *s2) {
|
|
|
|
while (*s1 && *s2) {
|
|
|
|
int c1 = tolower(*s1), c2 = tolower(*s2);
|
|
|
|
if (c1 < c2)
|
|
|
|
return -1;
|
|
|
|
else if (c1 > c2)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
++s1;
|
|
|
|
++s2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*s1)
|
|
|
|
return 1;
|
|
|
|
else if (*s2)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-07-13 02:38:41 +08:00
|
|
|
int perform_code_completion(int argc, const char **argv, int timing_only) {
|
2009-11-07 08:00:49 +08:00
|
|
|
const char *input = argv[1];
|
|
|
|
char *filename = 0;
|
|
|
|
unsigned line;
|
|
|
|
unsigned column;
|
2009-11-08 02:34:24 +08:00
|
|
|
CXIndex CIdx;
|
2009-11-18 02:09:14 +08:00
|
|
|
int errorCode;
|
2009-12-02 17:21:34 +08:00
|
|
|
struct CXUnsavedFile *unsaved_files = 0;
|
|
|
|
int num_unsaved_files = 0;
|
2009-12-19 00:20:58 +08:00
|
|
|
CXCodeCompleteResults *results = 0;
|
2010-10-01 06:26:05 +08:00
|
|
|
CXTranslationUnit TU = 0;
|
2010-10-12 05:37:58 +08:00
|
|
|
unsigned I, Repeats = 1;
|
|
|
|
unsigned completionOptions = clang_defaultCodeCompleteOptions();
|
|
|
|
|
|
|
|
if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
|
|
|
|
completionOptions |= CXCodeComplete_IncludeCodePatterns;
|
2010-08-10 04:45:32 +08:00
|
|
|
|
2010-07-13 02:38:41 +08:00
|
|
|
if (timing_only)
|
|
|
|
input += strlen("-code-completion-timing=");
|
|
|
|
else
|
|
|
|
input += strlen("-code-completion-at=");
|
|
|
|
|
2010-02-17 08:41:32 +08:00
|
|
|
if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
|
2010-01-27 01:06:03 +08:00
|
|
|
0, 0)))
|
2009-11-18 02:09:14 +08:00
|
|
|
return errorCode;
|
2009-11-07 08:00:49 +08:00
|
|
|
|
2009-12-02 17:21:34 +08:00
|
|
|
if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
|
|
|
|
return -1;
|
|
|
|
|
2010-10-12 05:37:58 +08:00
|
|
|
CIdx = clang_createIndex(0, 0);
|
|
|
|
|
|
|
|
if (getenv("CINDEXTEST_EDITING"))
|
|
|
|
Repeats = 5;
|
|
|
|
|
|
|
|
TU = clang_parseTranslationUnit(CIdx, 0,
|
|
|
|
argv + num_unsaved_files + 2,
|
|
|
|
argc - num_unsaved_files - 2,
|
|
|
|
0, 0, getDefaultParsingOptions());
|
|
|
|
if (!TU) {
|
|
|
|
fprintf(stderr, "Unable to load translation unit!\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2010-11-16 07:00:34 +08:00
|
|
|
|
|
|
|
if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) {
|
|
|
|
fprintf(stderr, "Unable to reparse translation init!\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2010-10-12 05:37:58 +08:00
|
|
|
|
|
|
|
for (I = 0; I != Repeats; ++I) {
|
|
|
|
results = clang_codeCompleteAt(TU, filename, line, column,
|
|
|
|
unsaved_files, num_unsaved_files,
|
|
|
|
completionOptions);
|
|
|
|
if (!results) {
|
|
|
|
fprintf(stderr, "Unable to perform code completion!\n");
|
2010-08-20 07:44:06 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2010-10-12 05:37:58 +08:00
|
|
|
if (I != Repeats-1)
|
|
|
|
clang_disposeCodeCompleteResults(results);
|
|
|
|
}
|
2010-01-28 08:56:43 +08:00
|
|
|
|
2009-12-19 00:20:58 +08:00
|
|
|
if (results) {
|
|
|
|
unsigned i, n = results->NumResults;
|
2010-08-26 21:48:20 +08:00
|
|
|
if (!timing_only) {
|
|
|
|
/* Sort the code-completion results based on the typed text. */
|
|
|
|
clang_sortCodeCompletionResults(results->Results, results->NumResults);
|
|
|
|
|
2010-07-13 02:38:41 +08:00
|
|
|
for (i = 0; i != n; ++i)
|
|
|
|
print_completion_result(results->Results + i, stdout);
|
2010-08-26 21:48:20 +08:00
|
|
|
}
|
2010-02-19 02:08:43 +08:00
|
|
|
n = clang_codeCompleteGetNumDiagnostics(results);
|
|
|
|
for (i = 0; i != n; ++i) {
|
|
|
|
CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
|
|
|
|
PrintDiagnostic(diag);
|
|
|
|
clang_disposeDiagnostic(diag);
|
|
|
|
}
|
2009-12-19 00:20:58 +08:00
|
|
|
clang_disposeCodeCompleteResults(results);
|
|
|
|
}
|
2010-08-10 04:45:32 +08:00
|
|
|
clang_disposeTranslationUnit(TU);
|
2009-11-07 08:00:49 +08:00
|
|
|
clang_disposeIndex(CIdx);
|
|
|
|
free(filename);
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2009-12-02 17:21:34 +08:00
|
|
|
free_remapped_files(unsaved_files, num_unsaved_files);
|
|
|
|
|
2009-11-18 02:09:14 +08:00
|
|
|
return 0;
|
2009-11-07 08:00:49 +08:00
|
|
|
}
|
|
|
|
|
2010-01-16 03:40:17 +08:00
|
|
|
typedef struct {
|
|
|
|
char *filename;
|
|
|
|
unsigned line;
|
|
|
|
unsigned column;
|
|
|
|
} CursorSourceLocation;
|
|
|
|
|
|
|
|
int inspect_cursor_at(int argc, const char **argv) {
|
|
|
|
CXIndex CIdx;
|
|
|
|
int errorCode;
|
|
|
|
struct CXUnsavedFile *unsaved_files = 0;
|
|
|
|
int num_unsaved_files = 0;
|
|
|
|
CXTranslationUnit TU;
|
|
|
|
CXCursor Cursor;
|
|
|
|
CursorSourceLocation *Locations = 0;
|
|
|
|
unsigned NumLocations = 0, Loc;
|
2010-11-30 13:52:55 +08:00
|
|
|
unsigned Repeats = 1;
|
2010-11-30 14:04:54 +08:00
|
|
|
unsigned I;
|
2010-11-30 13:52:55 +08:00
|
|
|
|
2010-02-17 08:41:32 +08:00
|
|
|
/* Count the number of locations. */
|
2010-01-16 03:40:17 +08:00
|
|
|
while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
|
|
|
|
++NumLocations;
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2010-01-16 03:40:17 +08:00
|
|
|
/* Parse the locations. */
|
|
|
|
assert(NumLocations > 0 && "Unable to count locations?");
|
|
|
|
Locations = (CursorSourceLocation *)malloc(
|
|
|
|
NumLocations * sizeof(CursorSourceLocation));
|
|
|
|
for (Loc = 0; Loc < NumLocations; ++Loc) {
|
|
|
|
const char *input = argv[Loc + 1] + strlen("-cursor-at=");
|
2010-02-17 08:41:32 +08:00
|
|
|
if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
|
|
|
|
&Locations[Loc].line,
|
2010-01-27 01:06:03 +08:00
|
|
|
&Locations[Loc].column, 0, 0)))
|
2010-01-16 03:40:17 +08:00
|
|
|
return errorCode;
|
|
|
|
}
|
2010-02-17 08:41:32 +08:00
|
|
|
|
|
|
|
if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
|
2010-01-16 03:40:17 +08:00
|
|
|
&num_unsaved_files))
|
|
|
|
return -1;
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2010-11-30 13:52:55 +08:00
|
|
|
if (getenv("CINDEXTEST_EDITING"))
|
|
|
|
Repeats = 5;
|
|
|
|
|
|
|
|
/* Parse the translation unit. When we're testing clang_getCursor() after
|
|
|
|
reparsing, don't remap unsaved files until the second parse. */
|
|
|
|
CIdx = clang_createIndex(1, 1);
|
|
|
|
TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
|
|
|
|
argv + num_unsaved_files + 1 + NumLocations,
|
2010-01-16 03:40:17 +08:00
|
|
|
argc - num_unsaved_files - 2 - NumLocations,
|
2010-11-30 13:52:55 +08:00
|
|
|
unsaved_files,
|
|
|
|
Repeats > 1? 0 : num_unsaved_files,
|
|
|
|
getDefaultParsingOptions());
|
|
|
|
|
2010-01-16 03:40:17 +08:00
|
|
|
if (!TU) {
|
|
|
|
fprintf(stderr, "unable to parse input\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2010-11-30 14:04:54 +08:00
|
|
|
for (I = 0; I != Repeats; ++I) {
|
2010-11-30 13:52:55 +08:00
|
|
|
if (Repeats > 1 &&
|
|
|
|
clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
|
|
|
|
clang_defaultReparseOptions(TU))) {
|
|
|
|
clang_disposeTranslationUnit(TU);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Loc = 0; Loc < NumLocations; ++Loc) {
|
|
|
|
CXFile file = clang_getFile(TU, Locations[Loc].filename);
|
|
|
|
if (!file)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Cursor = clang_getCursor(TU,
|
|
|
|
clang_getLocation(TU, file, Locations[Loc].line,
|
|
|
|
Locations[Loc].column));
|
|
|
|
if (I + 1 == Repeats) {
|
|
|
|
PrintCursor(Cursor);
|
|
|
|
printf("\n");
|
|
|
|
free(Locations[Loc].filename);
|
|
|
|
}
|
|
|
|
}
|
2010-01-16 03:40:17 +08:00
|
|
|
}
|
2010-11-30 13:52:55 +08:00
|
|
|
|
2010-02-19 02:08:43 +08:00
|
|
|
PrintDiagnostics(TU);
|
2010-01-16 03:40:17 +08:00
|
|
|
clang_disposeTranslationUnit(TU);
|
|
|
|
clang_disposeIndex(CIdx);
|
|
|
|
free(Locations);
|
|
|
|
free_remapped_files(unsaved_files, num_unsaved_files);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-01-27 01:06:03 +08:00
|
|
|
int perform_token_annotation(int argc, const char **argv) {
|
|
|
|
const char *input = argv[1];
|
|
|
|
char *filename = 0;
|
|
|
|
unsigned line, second_line;
|
|
|
|
unsigned column, second_column;
|
|
|
|
CXIndex CIdx;
|
|
|
|
CXTranslationUnit TU = 0;
|
|
|
|
int errorCode;
|
|
|
|
struct CXUnsavedFile *unsaved_files = 0;
|
|
|
|
int num_unsaved_files = 0;
|
|
|
|
CXToken *tokens;
|
|
|
|
unsigned num_tokens;
|
|
|
|
CXSourceRange range;
|
|
|
|
CXSourceLocation startLoc, endLoc;
|
|
|
|
CXFile file = 0;
|
|
|
|
CXCursor *cursors = 0;
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
input += strlen("-test-annotate-tokens=");
|
|
|
|
if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
|
|
|
|
&second_line, &second_column)))
|
|
|
|
return errorCode;
|
|
|
|
|
|
|
|
if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
|
|
|
|
return -1;
|
|
|
|
|
2010-02-19 07:07:20 +08:00
|
|
|
CIdx = clang_createIndex(0, 1);
|
2010-01-27 01:06:03 +08:00
|
|
|
TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
|
|
|
|
argc - num_unsaved_files - 3,
|
|
|
|
argv + num_unsaved_files + 2,
|
|
|
|
num_unsaved_files,
|
2010-02-19 02:08:43 +08:00
|
|
|
unsaved_files);
|
2010-01-27 01:06:03 +08:00
|
|
|
if (!TU) {
|
|
|
|
fprintf(stderr, "unable to parse input\n");
|
|
|
|
clang_disposeIndex(CIdx);
|
|
|
|
free(filename);
|
|
|
|
free_remapped_files(unsaved_files, num_unsaved_files);
|
|
|
|
return -1;
|
2010-02-17 08:41:32 +08:00
|
|
|
}
|
2010-01-27 01:06:03 +08:00
|
|
|
errorCode = 0;
|
|
|
|
|
|
|
|
file = clang_getFile(TU, filename);
|
|
|
|
if (!file) {
|
|
|
|
fprintf(stderr, "file %s is not in this translation unit\n", filename);
|
|
|
|
errorCode = -1;
|
|
|
|
goto teardown;
|
|
|
|
}
|
|
|
|
|
|
|
|
startLoc = clang_getLocation(TU, file, line, column);
|
|
|
|
if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
|
2010-02-17 08:41:32 +08:00
|
|
|
fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
|
2010-01-27 01:06:03 +08:00
|
|
|
column);
|
|
|
|
errorCode = -1;
|
2010-02-17 08:41:32 +08:00
|
|
|
goto teardown;
|
2010-01-27 01:06:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
endLoc = clang_getLocation(TU, file, second_line, second_column);
|
|
|
|
if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
|
2010-02-17 08:41:32 +08:00
|
|
|
fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
|
2010-01-27 01:06:03 +08:00
|
|
|
second_line, second_column);
|
|
|
|
errorCode = -1;
|
2010-02-17 08:41:32 +08:00
|
|
|
goto teardown;
|
2010-01-27 01:06:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
range = clang_getRange(startLoc, endLoc);
|
|
|
|
clang_tokenize(TU, range, &tokens, &num_tokens);
|
|
|
|
cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
|
|
|
|
clang_annotateTokens(TU, tokens, num_tokens, cursors);
|
|
|
|
for (i = 0; i != num_tokens; ++i) {
|
|
|
|
const char *kind = "<unknown>";
|
|
|
|
CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
|
|
|
|
CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
|
|
|
|
unsigned start_line, start_column, end_line, end_column;
|
|
|
|
|
|
|
|
switch (clang_getTokenKind(tokens[i])) {
|
|
|
|
case CXToken_Punctuation: kind = "Punctuation"; break;
|
|
|
|
case CXToken_Keyword: kind = "Keyword"; break;
|
|
|
|
case CXToken_Identifier: kind = "Identifier"; break;
|
|
|
|
case CXToken_Literal: kind = "Literal"; break;
|
|
|
|
case CXToken_Comment: kind = "Comment"; break;
|
|
|
|
}
|
2010-11-09 14:24:54 +08:00
|
|
|
clang_getSpellingLocation(clang_getRangeStart(extent),
|
|
|
|
0, &start_line, &start_column, 0);
|
|
|
|
clang_getSpellingLocation(clang_getRangeEnd(extent),
|
|
|
|
0, &end_line, &end_column, 0);
|
2010-02-14 16:32:24 +08:00
|
|
|
printf("%s: \"%s\" ", kind, clang_getCString(spelling));
|
|
|
|
PrintExtent(stdout, start_line, start_column, end_line, end_column);
|
2010-01-27 02:31:56 +08:00
|
|
|
if (!clang_isInvalid(cursors[i].kind)) {
|
|
|
|
printf(" ");
|
|
|
|
PrintCursor(cursors[i]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
2010-01-27 01:06:03 +08:00
|
|
|
}
|
|
|
|
free(cursors);
|
2010-10-21 05:22:15 +08:00
|
|
|
clang_disposeTokens(TU, tokens, num_tokens);
|
2010-01-27 01:06:03 +08:00
|
|
|
|
|
|
|
teardown:
|
2010-02-19 02:08:43 +08:00
|
|
|
PrintDiagnostics(TU);
|
2010-01-27 01:06:03 +08:00
|
|
|
clang_disposeTranslationUnit(TU);
|
|
|
|
clang_disposeIndex(CIdx);
|
|
|
|
free(filename);
|
|
|
|
free_remapped_files(unsaved_files, num_unsaved_files);
|
|
|
|
return errorCode;
|
|
|
|
}
|
|
|
|
|
2010-03-25 10:00:39 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
/* USR printing. */
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
static int insufficient_usr(const char *kind, const char *usage) {
|
|
|
|
fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned isUSR(const char *s) {
|
|
|
|
return s[0] == 'c' && s[1] == ':';
|
|
|
|
}
|
|
|
|
|
|
|
|
static int not_usr(const char *s, const char *arg) {
|
|
|
|
fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_usr(CXString usr) {
|
|
|
|
const char *s = clang_getCString(usr);
|
|
|
|
printf("%s\n", s);
|
|
|
|
clang_disposeString(usr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void display_usrs() {
|
|
|
|
fprintf(stderr, "-print-usrs options:\n"
|
|
|
|
" ObjCCategory <class name> <category name>\n"
|
|
|
|
" ObjCClass <class name>\n"
|
|
|
|
" ObjCIvar <ivar name> <class USR>\n"
|
|
|
|
" ObjCMethod <selector> [0=class method|1=instance method] "
|
|
|
|
"<class USR>\n"
|
|
|
|
" ObjCProperty <property name> <class USR>\n"
|
|
|
|
" ObjCProtocol <protocol name>\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
int print_usrs(const char **I, const char **E) {
|
|
|
|
while (I != E) {
|
|
|
|
const char *kind = *I;
|
|
|
|
unsigned len = strlen(kind);
|
|
|
|
switch (len) {
|
|
|
|
case 8:
|
|
|
|
if (memcmp(kind, "ObjCIvar", 8) == 0) {
|
|
|
|
if (I + 2 >= E)
|
|
|
|
return insufficient_usr(kind, "<ivar name> <class USR>");
|
|
|
|
if (!isUSR(I[2]))
|
|
|
|
return not_usr("<class USR>", I[2]);
|
|
|
|
else {
|
|
|
|
CXString x;
|
2010-11-16 16:15:36 +08:00
|
|
|
x.data = (void*) I[2];
|
2010-11-16 09:56:27 +08:00
|
|
|
x.private_flags = 0;
|
2010-03-25 10:00:39 +08:00
|
|
|
print_usr(clang_constructUSR_ObjCIvar(I[1], x));
|
|
|
|
}
|
|
|
|
|
|
|
|
I += 3;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 9:
|
|
|
|
if (memcmp(kind, "ObjCClass", 9) == 0) {
|
|
|
|
if (I + 1 >= E)
|
|
|
|
return insufficient_usr(kind, "<class name>");
|
|
|
|
print_usr(clang_constructUSR_ObjCClass(I[1]));
|
|
|
|
I += 2;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 10:
|
|
|
|
if (memcmp(kind, "ObjCMethod", 10) == 0) {
|
|
|
|
if (I + 3 >= E)
|
|
|
|
return insufficient_usr(kind, "<method selector> "
|
|
|
|
"[0=class method|1=instance method] <class USR>");
|
|
|
|
if (!isUSR(I[3]))
|
|
|
|
return not_usr("<class USR>", I[3]);
|
|
|
|
else {
|
|
|
|
CXString x;
|
2010-11-16 16:15:36 +08:00
|
|
|
x.data = (void*) I[3];
|
2010-11-16 09:56:27 +08:00
|
|
|
x.private_flags = 0;
|
2010-03-25 10:00:39 +08:00
|
|
|
print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
|
|
|
|
}
|
|
|
|
I += 4;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 12:
|
|
|
|
if (memcmp(kind, "ObjCCategory", 12) == 0) {
|
|
|
|
if (I + 2 >= E)
|
|
|
|
return insufficient_usr(kind, "<class name> <category name>");
|
|
|
|
print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
|
|
|
|
I += 3;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (memcmp(kind, "ObjCProtocol", 12) == 0) {
|
|
|
|
if (I + 1 >= E)
|
|
|
|
return insufficient_usr(kind, "<protocol name>");
|
|
|
|
print_usr(clang_constructUSR_ObjCProtocol(I[1]));
|
|
|
|
I += 2;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (memcmp(kind, "ObjCProperty", 12) == 0) {
|
|
|
|
if (I + 2 >= E)
|
|
|
|
return insufficient_usr(kind, "<property name> <class USR>");
|
|
|
|
if (!isUSR(I[2]))
|
|
|
|
return not_usr("<class USR>", I[2]);
|
|
|
|
else {
|
|
|
|
CXString x;
|
2010-11-16 16:15:36 +08:00
|
|
|
x.data = (void*) I[2];
|
2010-11-16 09:56:27 +08:00
|
|
|
x.private_flags = 0;
|
2010-03-25 10:00:39 +08:00
|
|
|
print_usr(clang_constructUSR_ObjCProperty(I[1], x));
|
|
|
|
}
|
|
|
|
I += 3;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (I != E) {
|
|
|
|
fprintf(stderr, "Invalid USR kind: %s\n", *I);
|
|
|
|
display_usrs();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int print_usrs_file(const char *file_name) {
|
|
|
|
char line[2048];
|
|
|
|
const char *args[128];
|
|
|
|
unsigned numChars = 0;
|
|
|
|
|
|
|
|
FILE *fp = fopen(file_name, "r");
|
|
|
|
if (!fp) {
|
|
|
|
fprintf(stderr, "error: cannot open '%s'\n", file_name);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This code is not really all that safe, but it works fine for testing. */
|
|
|
|
while (!feof(fp)) {
|
|
|
|
char c = fgetc(fp);
|
|
|
|
if (c == '\n') {
|
|
|
|
unsigned i = 0;
|
|
|
|
const char *s = 0;
|
|
|
|
|
|
|
|
if (numChars == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
line[numChars] = '\0';
|
|
|
|
numChars = 0;
|
|
|
|
|
|
|
|
if (line[0] == '/' && line[1] == '/')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
s = strtok(line, " ");
|
|
|
|
while (s) {
|
|
|
|
args[i] = s;
|
|
|
|
++i;
|
|
|
|
s = strtok(0, " ");
|
|
|
|
}
|
|
|
|
if (print_usrs(&args[0], &args[i]))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
line[numChars++] = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-08-13 13:36:37 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
/* Command line processing. */
|
|
|
|
/******************************************************************************/
|
|
|
|
int write_pch_file(const char *filename, int argc, const char *argv[]) {
|
|
|
|
CXIndex Idx;
|
|
|
|
CXTranslationUnit TU;
|
|
|
|
struct CXUnsavedFile *unsaved_files = 0;
|
|
|
|
int num_unsaved_files = 0;
|
|
|
|
|
|
|
|
Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
|
|
|
|
|
|
|
|
if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
|
|
|
|
clang_disposeIndex(Idx);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
TU = clang_parseTranslationUnit(Idx, 0,
|
|
|
|
argv + num_unsaved_files,
|
|
|
|
argc - num_unsaved_files,
|
|
|
|
unsaved_files,
|
|
|
|
num_unsaved_files,
|
|
|
|
CXTranslationUnit_Incomplete);
|
|
|
|
if (!TU) {
|
|
|
|
fprintf(stderr, "Unable to load translation unit!\n");
|
|
|
|
free_remapped_files(unsaved_files, num_unsaved_files);
|
|
|
|
clang_disposeIndex(Idx);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-08-13 23:35:05 +08:00
|
|
|
if (clang_saveTranslationUnit(TU, filename, clang_defaultSaveOptions(TU)))
|
2010-08-13 13:36:37 +08:00
|
|
|
fprintf(stderr, "Unable to write PCH file %s\n", filename);
|
|
|
|
clang_disposeTranslationUnit(TU);
|
|
|
|
free_remapped_files(unsaved_files, num_unsaved_files);
|
|
|
|
clang_disposeIndex(Idx);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-11-18 02:13:31 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
/* Command line processing. */
|
|
|
|
/******************************************************************************/
|
2009-11-18 02:09:14 +08:00
|
|
|
|
2010-01-21 05:32:04 +08:00
|
|
|
static CXCursorVisitor GetVisitor(const char *s) {
|
2010-01-13 07:34:26 +08:00
|
|
|
if (s[0] == '\0')
|
2010-01-21 05:32:04 +08:00
|
|
|
return FilteredPrintingVisitor;
|
2010-01-13 07:34:26 +08:00
|
|
|
if (strcmp(s, "-usrs") == 0)
|
|
|
|
return USRVisitor;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-11-18 02:09:14 +08:00
|
|
|
static void print_usage(void) {
|
|
|
|
fprintf(stderr,
|
2009-11-18 02:13:31 +08:00
|
|
|
"usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
|
2010-07-13 02:38:41 +08:00
|
|
|
" c-index-test -code-completion-timing=<site> <compiler arguments>\n"
|
2010-01-16 03:40:17 +08:00
|
|
|
" c-index-test -cursor-at=<site> <compiler arguments>\n"
|
2009-11-18 10:02:52 +08:00
|
|
|
" c-index-test -test-file-scan <AST file> <source file> "
|
|
|
|
"[FileCheck prefix]\n"
|
2010-01-06 07:18:49 +08:00
|
|
|
" c-index-test -test-load-tu <AST file> <symbol filter> "
|
|
|
|
"[FileCheck prefix]\n"
|
2010-01-13 07:34:26 +08:00
|
|
|
" c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
|
|
|
|
"[FileCheck prefix]\n"
|
2010-07-13 02:38:41 +08:00
|
|
|
" c-index-test -test-load-source <symbol filter> {<args>}*\n");
|
2010-01-16 03:40:17 +08:00
|
|
|
fprintf(stderr,
|
2010-07-20 05:46:24 +08:00
|
|
|
" c-index-test -test-load-source-reparse <trials> <symbol filter> "
|
|
|
|
" {<args>}*\n"
|
2010-07-13 02:38:41 +08:00
|
|
|
" c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
|
2010-01-27 03:31:51 +08:00
|
|
|
" c-index-test -test-annotate-tokens=<range> {<args>}*\n"
|
|
|
|
" c-index-test -test-inclusion-stack-source {<args>}*\n"
|
2010-03-03 14:37:58 +08:00
|
|
|
" c-index-test -test-inclusion-stack-tu <AST file>\n"
|
2010-03-25 10:00:39 +08:00
|
|
|
" c-index-test -test-print-linkage-source {<args>}*\n"
|
2010-05-15 05:29:26 +08:00
|
|
|
" c-index-test -test-print-typekind {<args>}*\n"
|
2010-07-22 14:29:13 +08:00
|
|
|
" c-index-test -print-usr [<CursorKind> {<args>}]*\n");
|
|
|
|
fprintf(stderr,
|
2010-08-13 13:36:37 +08:00
|
|
|
" c-index-test -print-usr-file <file>\n"
|
|
|
|
" c-index-test -write-pch <file> <compiler arguments>\n\n");
|
2010-07-20 22:34:35 +08:00
|
|
|
fprintf(stderr,
|
2010-01-13 07:34:26 +08:00
|
|
|
" <symbol filter> values:\n%s",
|
2009-11-18 02:13:31 +08:00
|
|
|
" 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"
|
2009-12-01 10:35:37 +08:00
|
|
|
" typedef - only load typdefs (non-PCH)\n"
|
|
|
|
" scan-function - scan function bodies (non-PCH)\n\n");
|
2009-11-18 02:09:14 +08:00
|
|
|
}
|
|
|
|
|
2010-10-01 04:39:47 +08:00
|
|
|
/***/
|
|
|
|
|
|
|
|
int cindextest_main(int argc, const char **argv) {
|
2010-02-19 07:07:20 +08:00
|
|
|
clang_enableStackTraces();
|
2009-11-18 02:09:14 +08:00
|
|
|
if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
|
2010-07-13 02:38:41 +08:00
|
|
|
return perform_code_completion(argc, argv, 0);
|
|
|
|
if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
|
|
|
|
return perform_code_completion(argc, argv, 1);
|
2010-01-16 03:40:17 +08:00
|
|
|
if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
|
|
|
|
return inspect_cursor_at(argc, argv);
|
2010-01-13 07:34:26 +08:00
|
|
|
else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
|
2010-01-21 05:32:04 +08:00
|
|
|
CXCursorVisitor I = GetVisitor(argv[1] + 13);
|
2010-01-13 07:34:26 +08:00
|
|
|
if (I)
|
2010-01-27 01:59:48 +08:00
|
|
|
return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
|
|
|
|
NULL);
|
2010-01-13 07:34:26 +08:00
|
|
|
}
|
2010-07-20 05:46:24 +08:00
|
|
|
else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
|
|
|
|
CXCursorVisitor I = GetVisitor(argv[1] + 25);
|
|
|
|
if (I) {
|
|
|
|
int trials = atoi(argv[2]);
|
|
|
|
return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
}
|
2010-01-13 07:34:26 +08:00
|
|
|
else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
|
2010-01-21 05:32:04 +08:00
|
|
|
CXCursorVisitor I = GetVisitor(argv[1] + 17);
|
2010-01-13 07:34:26 +08:00
|
|
|
if (I)
|
2010-01-27 01:59:48 +08:00
|
|
|
return perform_test_load_source(argc - 3, argv + 3, argv[2], I, NULL);
|
2010-01-13 07:34:26 +08:00
|
|
|
}
|
|
|
|
else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
|
2009-11-18 10:02:52 +08:00
|
|
|
return perform_file_scan(argv[2], argv[3],
|
|
|
|
argc >= 5 ? argv[4] : 0);
|
2010-01-27 01:06:03 +08:00
|
|
|
else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
|
|
|
|
return perform_token_annotation(argc, argv);
|
2010-01-27 03:31:51 +08:00
|
|
|
else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
|
|
|
|
return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
|
|
|
|
PrintInclusionStack);
|
|
|
|
else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
|
|
|
|
return perform_test_load_tu(argv[2], "all", NULL, NULL,
|
|
|
|
PrintInclusionStack);
|
2010-03-03 14:37:58 +08:00
|
|
|
else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
|
|
|
|
return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
|
|
|
|
NULL);
|
2010-05-15 05:29:26 +08:00
|
|
|
else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
|
|
|
|
return perform_test_load_source(argc - 2, argv + 2, "all",
|
|
|
|
PrintTypeKind, 0);
|
2010-03-25 10:00:39 +08:00
|
|
|
else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
|
|
|
|
if (argc > 2)
|
|
|
|
return print_usrs(argv + 2, argv + argc);
|
|
|
|
else {
|
|
|
|
display_usrs();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
|
|
|
|
return print_usrs_file(argv[2]);
|
2010-08-13 13:36:37 +08:00
|
|
|
else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
|
|
|
|
return write_pch_file(argv[2], argc - 3, argv + 3);
|
|
|
|
|
2009-11-18 02:09:14 +08:00
|
|
|
print_usage();
|
2009-08-28 23:28:48 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2010-10-01 04:39:47 +08:00
|
|
|
|
|
|
|
/***/
|
|
|
|
|
|
|
|
/* We intentionally run in a separate thread to ensure we at least minimal
|
|
|
|
* testing of a multithreaded environment (for example, having a reduced stack
|
|
|
|
* size). */
|
|
|
|
|
|
|
|
typedef struct thread_info {
|
|
|
|
int argc;
|
|
|
|
const char **argv;
|
|
|
|
int result;
|
|
|
|
} thread_info;
|
2010-11-05 03:11:31 +08:00
|
|
|
void thread_runner(void *client_data_v) {
|
2010-10-01 04:39:47 +08:00
|
|
|
thread_info *client_data = client_data_v;
|
|
|
|
client_data->result = cindextest_main(client_data->argc, client_data->argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, const char **argv) {
|
|
|
|
thread_info client_data;
|
|
|
|
|
2010-10-28 00:00:01 +08:00
|
|
|
if (getenv("CINDEXTEST_NOTHREADS"))
|
|
|
|
return cindextest_main(argc, argv);
|
|
|
|
|
2010-10-01 04:39:47 +08:00
|
|
|
client_data.argc = argc;
|
|
|
|
client_data.argv = argv;
|
2010-11-04 09:26:31 +08:00
|
|
|
clang_executeOnThread(thread_runner, &client_data, 0);
|
2010-10-01 04:39:47 +08:00
|
|
|
return client_data.result;
|
|
|
|
}
|