2009-09-01 23:55:40 +08:00
|
|
|
/* c-index-test.c */
|
2009-08-28 23:28:48 +08:00
|
|
|
|
2014-06-04 11:28:55 +08:00
|
|
|
#include "clang/Config/config.h"
|
2009-08-28 23:28:48 +08:00
|
|
|
#include "clang-c/Index.h"
|
2012-06-30 19:27:57 +08:00
|
|
|
#include "clang-c/CXCompilationDatabase.h"
|
Add an option to allow Clang verify source files for a module only once during
the build
When Clang loads the module, it verifies the user source files that the module
was built from. If any file was changed, the module is rebuilt. There are two
problems with this:
1. correctness: we don't verify system files (there are too many of them, and
stat'ing all of them would take a lot of time);
2. performance: the same module file is verified again and again during a
single build.
This change allows the build system to optimize source file verification. The
idea is based on the fact that while the project is being built, the source
files don't change. This allows us to verify the module only once during a
single build session. The build system passes a flag,
-fbuild-session-timestamp=, to inform Clang of the time when the build started.
The build system also requests to enable this feature by passing
-fmodules-validate-once-per-build-session. If these flags are not passed, the
behavior is not changed. When Clang verifies the module the first time, it
writes out a timestamp file. Then, when Clang loads the module the second
time, it finds a timestamp file, so it can compare the verification timestamp
of the module with the time when the build started. If the verification
timestamp is too old, the module is verified again, and the timestamp file is
updated.
llvm-svn: 201224
2014-02-12 18:33:14 +08:00
|
|
|
#include "clang-c/BuildSystem.h"
|
2014-04-28 10:39:27 +08:00
|
|
|
#include "clang-c/Documentation.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
|
|
|
|
2012-08-08 01:54:38 +08:00
|
|
|
#ifdef CLANG_HAVE_LIBXML
|
|
|
|
#include <libxml/parser.h>
|
|
|
|
#include <libxml/relaxng.h>
|
|
|
|
#include <libxml/xmlerror.h>
|
|
|
|
#endif
|
|
|
|
|
2012-12-06 05:53:37 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
# include <direct.h>
|
|
|
|
#else
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
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);
|
|
|
|
}
|
2012-06-30 19:27:57 +08:00
|
|
|
char *dirname(char* path)
|
|
|
|
{
|
|
|
|
char* base1 = (char*)strrchr(path, '/');
|
|
|
|
char* base2 = (char*)strrchr(path, '\\');
|
|
|
|
if (base1 && base2)
|
|
|
|
if (base1 > base2)
|
|
|
|
*base1 = 0;
|
|
|
|
else
|
|
|
|
*base2 = 0;
|
|
|
|
else if (base1)
|
2012-06-30 19:47:18 +08:00
|
|
|
*base1 = 0;
|
2012-06-30 19:27:57 +08:00
|
|
|
else if (base2)
|
2012-06-30 19:47:18 +08:00
|
|
|
*base2 = 0;
|
2012-06-30 19:27:57 +08:00
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
2009-10-27 21:42:56 +08:00
|
|
|
#else
|
2009-09-25 04:03:06 +08:00
|
|
|
extern char *basename(const char *);
|
2012-06-30 19:27:57 +08:00
|
|
|
extern char *dirname(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;
|
2011-11-03 10:20:25 +08:00
|
|
|
if (getenv("CINDEXTEST_COMPLETION_NO_CACHING"))
|
|
|
|
options &= ~CXTranslationUnit_CacheCompletionResults;
|
2012-04-12 18:11:59 +08:00
|
|
|
if (getenv("CINDEXTEST_SKIP_FUNCTION_BODIES"))
|
|
|
|
options |= CXTranslationUnit_SkipFunctionBodies;
|
2012-07-03 01:35:10 +08:00
|
|
|
if (getenv("CINDEXTEST_COMPLETION_BRIEF_COMMENTS"))
|
|
|
|
options |= CXTranslationUnit_IncludeBriefCommentsInCodeCompletion;
|
2010-07-23 08:33:23 +08:00
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2014-02-17 19:54:08 +08:00
|
|
|
/** \brief Returns 0 in case of success, non-zero in case of a failure. */
|
2011-11-14 06:08:33 +08:00
|
|
|
static int checkForErrors(CXTranslationUnit TU);
|
|
|
|
|
2014-02-13 03:12:37 +08:00
|
|
|
static void describeLibclangFailure(enum CXErrorCode Err) {
|
|
|
|
switch (Err) {
|
|
|
|
case CXError_Success:
|
|
|
|
fprintf(stderr, "Success\n");
|
|
|
|
return;
|
|
|
|
|
|
|
|
case CXError_Failure:
|
|
|
|
fprintf(stderr, "Failure (no details available)\n");
|
|
|
|
return;
|
|
|
|
|
|
|
|
case CXError_Crashed:
|
|
|
|
fprintf(stderr, "Failure: libclang crashed\n");
|
|
|
|
return;
|
|
|
|
|
|
|
|
case CXError_InvalidArguments:
|
|
|
|
fprintf(stderr, "Failure: invalid arguments passed to a libclang routine\n");
|
|
|
|
return;
|
|
|
|
|
|
|
|
case CXError_ASTReadError:
|
|
|
|
fprintf(stderr, "Failure: AST deserialization error occurred\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2014-02-13 03:12:37 +08:00
|
|
|
enum CXErrorCode Err = clang_createTranslationUnit2(Idx, file, TU);
|
|
|
|
if (Err != CXError_Success) {
|
2009-11-18 03:37:36 +08:00
|
|
|
fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
|
2014-02-13 03:12:37 +08:00
|
|
|
describeLibclangFailure(Err);
|
|
|
|
*TU = 0;
|
2009-11-18 03:37:36 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-12-05 16:19:23 +08:00
|
|
|
static int parse_remapped_files_with_opt(const char *opt_name,
|
|
|
|
int argc, const char **argv,
|
|
|
|
int start_arg,
|
|
|
|
struct CXUnsavedFile **unsaved_files,
|
|
|
|
int *num_unsaved_files) {
|
2010-01-23 08:14:00 +08:00
|
|
|
int i;
|
|
|
|
int arg;
|
2013-12-05 16:19:23 +08:00
|
|
|
int prefix_len = strlen(opt_name);
|
|
|
|
int arg_indices[20];
|
2010-01-23 08:14:00 +08:00
|
|
|
*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) {
|
2013-12-05 16:19:23 +08:00
|
|
|
if (strncmp(argv[arg], opt_name, prefix_len))
|
|
|
|
continue;
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2013-12-05 16:19:23 +08:00
|
|
|
assert(*num_unsaved_files < (int)(sizeof(arg_indices)/sizeof(int)));
|
|
|
|
arg_indices[*num_unsaved_files] = arg;
|
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);
|
2013-12-05 16:19:23 +08:00
|
|
|
for (i = 0; i != *num_unsaved_files; ++i) {
|
2010-01-23 08:14:00 +08:00
|
|
|
struct CXUnsavedFile *unsaved = *unsaved_files + i;
|
2013-12-05 16:19:23 +08:00
|
|
|
const char *arg_string = argv[arg_indices[i]] + prefix_len;
|
2010-01-23 08:14:00 +08:00
|
|
|
int filename_len;
|
|
|
|
char *filename;
|
|
|
|
char *contents;
|
|
|
|
FILE *to_file;
|
2013-12-06 04:13:27 +08:00
|
|
|
const char *sep = strchr(arg_string, ',');
|
|
|
|
if (!sep) {
|
2010-02-17 08:41:32 +08:00
|
|
|
fprintf(stderr,
|
2013-12-06 04:13:27 +08:00
|
|
|
"error: %sfrom:to argument is missing comma\n", opt_name);
|
2010-01-23 08:14:00 +08:00
|
|
|
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. */
|
2013-12-06 04:13:27 +08:00
|
|
|
to_file = fopen(sep + 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",
|
2013-12-06 04:13:27 +08:00
|
|
|
sep + 1);
|
2010-01-23 08:14:00 +08:00
|
|
|
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",
|
2013-12-06 04:13:27 +08:00
|
|
|
(feof(to_file) ? "EOF" : "error"), sep + 1);
|
2010-01-23 08:14:00 +08:00
|
|
|
fclose(to_file);
|
|
|
|
free_remapped_files(*unsaved_files, i);
|
2012-07-05 16:20:49 +08:00
|
|
|
free(contents);
|
2010-01-23 08:14:00 +08:00
|
|
|
*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. */
|
2013-12-06 04:13:27 +08:00
|
|
|
filename_len = sep - arg_string;
|
2010-01-23 08:14:00 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-12-05 16:19:23 +08:00
|
|
|
static int parse_remapped_files(int argc, const char **argv, int start_arg,
|
|
|
|
struct CXUnsavedFile **unsaved_files,
|
|
|
|
int *num_unsaved_files) {
|
|
|
|
return parse_remapped_files_with_opt("-remap-file=", argc, argv, start_arg,
|
|
|
|
unsaved_files, num_unsaved_files);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_remapped_files_with_try(int try_idx,
|
|
|
|
int argc, const char **argv,
|
|
|
|
int start_arg,
|
|
|
|
struct CXUnsavedFile **unsaved_files,
|
|
|
|
int *num_unsaved_files) {
|
|
|
|
struct CXUnsavedFile *unsaved_files_no_try_idx;
|
|
|
|
int num_unsaved_files_no_try_idx;
|
|
|
|
struct CXUnsavedFile *unsaved_files_try_idx;
|
|
|
|
int num_unsaved_files_try_idx;
|
|
|
|
int ret;
|
|
|
|
char opt_name[32];
|
|
|
|
|
|
|
|
ret = parse_remapped_files(argc, argv, start_arg,
|
|
|
|
&unsaved_files_no_try_idx, &num_unsaved_files_no_try_idx);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
sprintf(opt_name, "-remap-file-%d=", try_idx);
|
|
|
|
ret = parse_remapped_files_with_opt(opt_name, argc, argv, start_arg,
|
|
|
|
&unsaved_files_try_idx, &num_unsaved_files_try_idx);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2015-08-04 11:53:04 +08:00
|
|
|
if (num_unsaved_files_no_try_idx == 0) {
|
|
|
|
*unsaved_files = unsaved_files_try_idx;
|
|
|
|
*num_unsaved_files = num_unsaved_files_try_idx;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (num_unsaved_files_try_idx == 0) {
|
|
|
|
*unsaved_files = unsaved_files_no_try_idx;
|
|
|
|
*num_unsaved_files = num_unsaved_files_no_try_idx;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-05 16:19:23 +08:00
|
|
|
*num_unsaved_files = num_unsaved_files_no_try_idx + num_unsaved_files_try_idx;
|
|
|
|
*unsaved_files
|
|
|
|
= (struct CXUnsavedFile *)realloc(unsaved_files_no_try_idx,
|
|
|
|
sizeof(struct CXUnsavedFile) *
|
|
|
|
*num_unsaved_files);
|
|
|
|
memcpy(*unsaved_files + num_unsaved_files_no_try_idx,
|
|
|
|
unsaved_files_try_idx, sizeof(struct CXUnsavedFile) *
|
|
|
|
num_unsaved_files_try_idx);
|
|
|
|
free(unsaved_files_try_idx);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-08 01:54:38 +08:00
|
|
|
static const char *parse_comments_schema(int argc, const char **argv) {
|
|
|
|
const char *CommentsSchemaArg = "-comments-xml-schema=";
|
|
|
|
const char *CommentSchemaFile = NULL;
|
|
|
|
|
|
|
|
if (argc == 0)
|
|
|
|
return CommentSchemaFile;
|
|
|
|
|
|
|
|
if (!strncmp(argv[0], CommentsSchemaArg, strlen(CommentsSchemaArg)))
|
|
|
|
CommentSchemaFile = argv[0] + strlen(CommentsSchemaArg);
|
|
|
|
|
|
|
|
return CommentSchemaFile;
|
|
|
|
}
|
|
|
|
|
2009-11-18 02:13:31 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
/* Pretty-printing. */
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2012-07-21 05:34:34 +08:00
|
|
|
static const char *FileCheckPrefix = "CHECK";
|
|
|
|
|
|
|
|
static void PrintCString(const char *CStr) {
|
2012-06-27 04:39:18 +08:00
|
|
|
if (CStr != NULL && CStr[0] != '\0') {
|
|
|
|
for ( ; *CStr; ++CStr) {
|
|
|
|
const char C = *CStr;
|
|
|
|
switch (C) {
|
|
|
|
case '\n': printf("\\n"); break;
|
|
|
|
case '\r': printf("\\r"); break;
|
|
|
|
case '\t': printf("\\t"); break;
|
|
|
|
case '\v': printf("\\v"); break;
|
|
|
|
case '\f': printf("\\f"); break;
|
|
|
|
default: putchar(C); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-07-21 05:34:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void PrintCStringWithPrefix(const char *Prefix, const char *CStr) {
|
|
|
|
printf(" %s=[", Prefix);
|
|
|
|
PrintCString(CStr);
|
2012-06-27 04:39:18 +08:00
|
|
|
printf("]");
|
|
|
|
}
|
|
|
|
|
2012-07-21 05:34:34 +08:00
|
|
|
static void PrintCXStringAndDispose(CXString Str) {
|
|
|
|
PrintCString(clang_getCString(Str));
|
|
|
|
clang_disposeString(Str);
|
|
|
|
}
|
|
|
|
|
2012-08-08 01:54:38 +08:00
|
|
|
static void PrintCXStringWithPrefix(const char *Prefix, CXString Str) {
|
|
|
|
PrintCStringWithPrefix(Prefix, clang_getCString(Str));
|
|
|
|
}
|
|
|
|
|
2012-07-21 05:34:34 +08:00
|
|
|
static void PrintCXStringWithPrefixAndDispose(const char *Prefix,
|
|
|
|
CXString Str) {
|
|
|
|
PrintCStringWithPrefix(Prefix, clang_getCString(Str));
|
|
|
|
clang_disposeString(Str);
|
|
|
|
}
|
|
|
|
|
2011-07-26 01:48:11 +08:00
|
|
|
static void PrintRange(CXSourceRange R, const char *str) {
|
|
|
|
CXFile begin_file, end_file;
|
|
|
|
unsigned begin_line, begin_column, end_line, end_column;
|
|
|
|
|
|
|
|
clang_getSpellingLocation(clang_getRangeStart(R),
|
|
|
|
&begin_file, &begin_line, &begin_column, 0);
|
|
|
|
clang_getSpellingLocation(clang_getRangeEnd(R),
|
|
|
|
&end_file, &end_line, &end_column, 0);
|
|
|
|
if (!begin_file || !end_file)
|
|
|
|
return;
|
|
|
|
|
2012-03-31 04:58:35 +08:00
|
|
|
if (str)
|
|
|
|
printf(" %s=", str);
|
2011-07-26 01:48:11 +08:00
|
|
|
PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
|
|
|
|
}
|
|
|
|
|
2010-10-03 06:49:11 +08:00
|
|
|
int want_display_name = 0;
|
|
|
|
|
2012-05-08 08:14:45 +08:00
|
|
|
static void printVersion(const char *Prefix, CXVersion Version) {
|
|
|
|
if (Version.Major < 0)
|
|
|
|
return;
|
|
|
|
printf("%s%d", Prefix, Version.Major);
|
|
|
|
|
|
|
|
if (Version.Minor < 0)
|
|
|
|
return;
|
|
|
|
printf(".%d", Version.Minor);
|
|
|
|
|
|
|
|
if (Version.Subminor < 0)
|
|
|
|
return;
|
|
|
|
printf(".%d", Version.Subminor);
|
|
|
|
}
|
|
|
|
|
2012-07-21 05:34:34 +08:00
|
|
|
struct CommentASTDumpingContext {
|
|
|
|
int IndentLevel;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void DumpCXCommentInternal(struct CommentASTDumpingContext *Ctx,
|
|
|
|
CXComment Comment) {
|
2012-07-21 06:00:35 +08:00
|
|
|
unsigned i;
|
|
|
|
unsigned e;
|
|
|
|
enum CXCommentKind Kind = clang_Comment_getKind(Comment);
|
|
|
|
|
2012-07-21 05:34:34 +08:00
|
|
|
Ctx->IndentLevel++;
|
2012-07-21 06:00:35 +08:00
|
|
|
for (i = 0, e = Ctx->IndentLevel; i != e; ++i)
|
2012-07-21 05:34:34 +08:00
|
|
|
printf(" ");
|
|
|
|
|
|
|
|
printf("(");
|
|
|
|
switch (Kind) {
|
|
|
|
case CXComment_Null:
|
|
|
|
printf("CXComment_Null");
|
|
|
|
break;
|
|
|
|
case CXComment_Text:
|
|
|
|
printf("CXComment_Text");
|
|
|
|
PrintCXStringWithPrefixAndDispose("Text",
|
|
|
|
clang_TextComment_getText(Comment));
|
|
|
|
if (clang_Comment_isWhitespace(Comment))
|
|
|
|
printf(" IsWhitespace");
|
|
|
|
if (clang_InlineContentComment_hasTrailingNewline(Comment))
|
|
|
|
printf(" HasTrailingNewline");
|
|
|
|
break;
|
|
|
|
case CXComment_InlineCommand:
|
|
|
|
printf("CXComment_InlineCommand");
|
|
|
|
PrintCXStringWithPrefixAndDispose(
|
|
|
|
"CommandName",
|
|
|
|
clang_InlineCommandComment_getCommandName(Comment));
|
2012-07-24 00:43:01 +08:00
|
|
|
switch (clang_InlineCommandComment_getRenderKind(Comment)) {
|
|
|
|
case CXCommentInlineCommandRenderKind_Normal:
|
|
|
|
printf(" RenderNormal");
|
|
|
|
break;
|
|
|
|
case CXCommentInlineCommandRenderKind_Bold:
|
|
|
|
printf(" RenderBold");
|
|
|
|
break;
|
|
|
|
case CXCommentInlineCommandRenderKind_Monospaced:
|
|
|
|
printf(" RenderMonospaced");
|
|
|
|
break;
|
|
|
|
case CXCommentInlineCommandRenderKind_Emphasized:
|
|
|
|
printf(" RenderEmphasized");
|
|
|
|
break;
|
|
|
|
}
|
2012-07-21 06:00:35 +08:00
|
|
|
for (i = 0, e = clang_InlineCommandComment_getNumArgs(Comment);
|
2012-07-21 05:34:34 +08:00
|
|
|
i != e; ++i) {
|
|
|
|
printf(" Arg[%u]=", i);
|
|
|
|
PrintCXStringAndDispose(
|
|
|
|
clang_InlineCommandComment_getArgText(Comment, i));
|
|
|
|
}
|
|
|
|
if (clang_InlineContentComment_hasTrailingNewline(Comment))
|
|
|
|
printf(" HasTrailingNewline");
|
|
|
|
break;
|
2012-07-21 06:00:35 +08:00
|
|
|
case CXComment_HTMLStartTag: {
|
|
|
|
unsigned NumAttrs;
|
2012-07-21 05:34:34 +08:00
|
|
|
printf("CXComment_HTMLStartTag");
|
|
|
|
PrintCXStringWithPrefixAndDispose(
|
|
|
|
"Name",
|
|
|
|
clang_HTMLTagComment_getTagName(Comment));
|
2012-07-21 06:00:35 +08:00
|
|
|
NumAttrs = clang_HTMLStartTag_getNumAttrs(Comment);
|
2012-07-21 05:34:34 +08:00
|
|
|
if (NumAttrs != 0) {
|
|
|
|
printf(" Attrs:");
|
2012-07-21 06:00:35 +08:00
|
|
|
for (i = 0; i != NumAttrs; ++i) {
|
2012-07-21 05:34:34 +08:00
|
|
|
printf(" ");
|
|
|
|
PrintCXStringAndDispose(clang_HTMLStartTag_getAttrName(Comment, i));
|
|
|
|
printf("=");
|
|
|
|
PrintCXStringAndDispose(clang_HTMLStartTag_getAttrValue(Comment, i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (clang_HTMLStartTagComment_isSelfClosing(Comment))
|
|
|
|
printf(" SelfClosing");
|
|
|
|
if (clang_InlineContentComment_hasTrailingNewline(Comment))
|
|
|
|
printf(" HasTrailingNewline");
|
|
|
|
break;
|
2012-07-21 06:00:35 +08:00
|
|
|
}
|
2012-07-21 05:34:34 +08:00
|
|
|
case CXComment_HTMLEndTag:
|
|
|
|
printf("CXComment_HTMLEndTag");
|
|
|
|
PrintCXStringWithPrefixAndDispose(
|
|
|
|
"Name",
|
|
|
|
clang_HTMLTagComment_getTagName(Comment));
|
|
|
|
if (clang_InlineContentComment_hasTrailingNewline(Comment))
|
|
|
|
printf(" HasTrailingNewline");
|
|
|
|
break;
|
|
|
|
case CXComment_Paragraph:
|
|
|
|
printf("CXComment_Paragraph");
|
|
|
|
if (clang_Comment_isWhitespace(Comment))
|
|
|
|
printf(" IsWhitespace");
|
|
|
|
break;
|
|
|
|
case CXComment_BlockCommand:
|
|
|
|
printf("CXComment_BlockCommand");
|
|
|
|
PrintCXStringWithPrefixAndDispose(
|
|
|
|
"CommandName",
|
|
|
|
clang_BlockCommandComment_getCommandName(Comment));
|
2012-07-21 06:00:35 +08:00
|
|
|
for (i = 0, e = clang_BlockCommandComment_getNumArgs(Comment);
|
2012-07-21 05:34:34 +08:00
|
|
|
i != e; ++i) {
|
|
|
|
printf(" Arg[%u]=", i);
|
|
|
|
PrintCXStringAndDispose(
|
|
|
|
clang_BlockCommandComment_getArgText(Comment, i));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CXComment_ParamCommand:
|
|
|
|
printf("CXComment_ParamCommand");
|
|
|
|
switch (clang_ParamCommandComment_getDirection(Comment)) {
|
|
|
|
case CXCommentParamPassDirection_In:
|
|
|
|
printf(" in");
|
|
|
|
break;
|
|
|
|
case CXCommentParamPassDirection_Out:
|
|
|
|
printf(" out");
|
|
|
|
break;
|
|
|
|
case CXCommentParamPassDirection_InOut:
|
|
|
|
printf(" in,out");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (clang_ParamCommandComment_isDirectionExplicit(Comment))
|
|
|
|
printf(" explicitly");
|
|
|
|
else
|
|
|
|
printf(" implicitly");
|
|
|
|
PrintCXStringWithPrefixAndDispose(
|
|
|
|
"ParamName",
|
|
|
|
clang_ParamCommandComment_getParamName(Comment));
|
|
|
|
if (clang_ParamCommandComment_isParamIndexValid(Comment))
|
|
|
|
printf(" ParamIndex=%u", clang_ParamCommandComment_getParamIndex(Comment));
|
|
|
|
else
|
|
|
|
printf(" ParamIndex=Invalid");
|
|
|
|
break;
|
2012-08-01 06:37:06 +08:00
|
|
|
case CXComment_TParamCommand:
|
|
|
|
printf("CXComment_TParamCommand");
|
|
|
|
PrintCXStringWithPrefixAndDispose(
|
|
|
|
"ParamName",
|
|
|
|
clang_TParamCommandComment_getParamName(Comment));
|
|
|
|
if (clang_TParamCommandComment_isParamPositionValid(Comment)) {
|
|
|
|
printf(" ParamPosition={");
|
|
|
|
for (i = 0, e = clang_TParamCommandComment_getDepth(Comment);
|
|
|
|
i != e; ++i) {
|
|
|
|
printf("%u", clang_TParamCommandComment_getIndex(Comment, i));
|
|
|
|
if (i != e - 1)
|
|
|
|
printf(", ");
|
|
|
|
}
|
|
|
|
printf("}");
|
|
|
|
} else
|
|
|
|
printf(" ParamPosition=Invalid");
|
|
|
|
break;
|
2012-07-21 05:34:34 +08:00
|
|
|
case CXComment_VerbatimBlockCommand:
|
|
|
|
printf("CXComment_VerbatimBlockCommand");
|
|
|
|
PrintCXStringWithPrefixAndDispose(
|
|
|
|
"CommandName",
|
|
|
|
clang_BlockCommandComment_getCommandName(Comment));
|
|
|
|
break;
|
|
|
|
case CXComment_VerbatimBlockLine:
|
|
|
|
printf("CXComment_VerbatimBlockLine");
|
|
|
|
PrintCXStringWithPrefixAndDispose(
|
|
|
|
"Text",
|
|
|
|
clang_VerbatimBlockLineComment_getText(Comment));
|
|
|
|
break;
|
|
|
|
case CXComment_VerbatimLine:
|
|
|
|
printf("CXComment_VerbatimLine");
|
|
|
|
PrintCXStringWithPrefixAndDispose(
|
|
|
|
"Text",
|
|
|
|
clang_VerbatimLineComment_getText(Comment));
|
|
|
|
break;
|
|
|
|
case CXComment_FullComment:
|
|
|
|
printf("CXComment_FullComment");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (Kind != CXComment_Null) {
|
|
|
|
const unsigned NumChildren = clang_Comment_getNumChildren(Comment);
|
2012-07-21 06:00:35 +08:00
|
|
|
unsigned i;
|
|
|
|
for (i = 0; i != NumChildren; ++i) {
|
2012-07-21 05:34:34 +08:00
|
|
|
printf("\n// %s: ", FileCheckPrefix);
|
|
|
|
DumpCXCommentInternal(Ctx, clang_Comment_getChild(Comment, i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf(")");
|
|
|
|
Ctx->IndentLevel--;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DumpCXComment(CXComment Comment) {
|
|
|
|
struct CommentASTDumpingContext Ctx;
|
|
|
|
Ctx.IndentLevel = 1;
|
|
|
|
printf("\n// %s: CommentAST=[\n// %s:", FileCheckPrefix, FileCheckPrefix);
|
|
|
|
DumpCXCommentInternal(&Ctx, Comment);
|
|
|
|
printf("]");
|
|
|
|
}
|
|
|
|
|
2014-05-03 07:30:59 +08:00
|
|
|
static void ValidateCommentXML(const char *Str, const char *CommentSchemaFile) {
|
2012-08-08 01:54:38 +08:00
|
|
|
#ifdef CLANG_HAVE_LIBXML
|
|
|
|
xmlRelaxNGParserCtxtPtr RNGParser;
|
|
|
|
xmlRelaxNGPtr Schema;
|
|
|
|
xmlDocPtr Doc;
|
|
|
|
xmlRelaxNGValidCtxtPtr ValidationCtxt;
|
|
|
|
int status;
|
|
|
|
|
2014-05-03 07:30:59 +08:00
|
|
|
if (!CommentSchemaFile)
|
2012-08-08 01:54:38 +08:00
|
|
|
return;
|
|
|
|
|
2014-05-03 07:30:59 +08:00
|
|
|
RNGParser = xmlRelaxNGNewParserCtxt(CommentSchemaFile);
|
|
|
|
if (!RNGParser) {
|
2012-08-08 01:54:38 +08:00
|
|
|
printf(" libXMLError");
|
|
|
|
return;
|
|
|
|
}
|
2014-05-03 07:30:59 +08:00
|
|
|
Schema = xmlRelaxNGParse(RNGParser);
|
2012-08-08 01:54:38 +08:00
|
|
|
|
|
|
|
Doc = xmlParseDoc((const xmlChar *) Str);
|
|
|
|
|
|
|
|
if (!Doc) {
|
|
|
|
xmlErrorPtr Error = xmlGetLastError();
|
|
|
|
printf(" CommentXMLInvalid [not well-formed XML: %s]", Error->message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-03 07:30:59 +08:00
|
|
|
ValidationCtxt = xmlRelaxNGNewValidCtxt(Schema);
|
2012-08-08 01:54:38 +08:00
|
|
|
status = xmlRelaxNGValidateDoc(ValidationCtxt, Doc);
|
|
|
|
if (!status)
|
|
|
|
printf(" CommentXMLValid");
|
|
|
|
else if (status > 0) {
|
|
|
|
xmlErrorPtr Error = xmlGetLastError();
|
|
|
|
printf(" CommentXMLInvalid [not vaild XML: %s]", Error->message);
|
|
|
|
} else
|
|
|
|
printf(" libXMLError");
|
|
|
|
|
|
|
|
xmlRelaxNGFreeValidCtxt(ValidationCtxt);
|
|
|
|
xmlFreeDoc(Doc);
|
2014-05-03 07:30:59 +08:00
|
|
|
xmlRelaxNGFree(Schema);
|
|
|
|
xmlRelaxNGFreeParserCtxt(RNGParser);
|
2012-08-08 01:54:38 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-09-11 04:32:42 +08:00
|
|
|
static void PrintCursorComments(CXCursor Cursor,
|
2014-05-03 07:30:59 +08:00
|
|
|
const char *CommentSchemaFile) {
|
2012-07-21 05:34:34 +08:00
|
|
|
{
|
|
|
|
CXString RawComment;
|
|
|
|
const char *RawCommentCString;
|
|
|
|
CXString BriefComment;
|
|
|
|
const char *BriefCommentCString;
|
|
|
|
|
|
|
|
RawComment = clang_Cursor_getRawCommentText(Cursor);
|
|
|
|
RawCommentCString = clang_getCString(RawComment);
|
|
|
|
if (RawCommentCString != NULL && RawCommentCString[0] != '\0') {
|
|
|
|
PrintCStringWithPrefix("RawComment", RawCommentCString);
|
|
|
|
PrintRange(clang_Cursor_getCommentRange(Cursor), "RawCommentRange");
|
|
|
|
|
|
|
|
BriefComment = clang_Cursor_getBriefCommentText(Cursor);
|
|
|
|
BriefCommentCString = clang_getCString(BriefComment);
|
|
|
|
if (BriefCommentCString != NULL && BriefCommentCString[0] != '\0')
|
|
|
|
PrintCStringWithPrefix("BriefComment", BriefCommentCString);
|
|
|
|
clang_disposeString(BriefComment);
|
|
|
|
}
|
|
|
|
clang_disposeString(RawComment);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2013-07-23 04:58:30 +08:00
|
|
|
CXComment Comment = clang_Cursor_getParsedComment(Cursor);
|
2012-07-21 05:34:34 +08:00
|
|
|
if (clang_Comment_getKind(Comment) != CXComment_Null) {
|
|
|
|
PrintCXStringWithPrefixAndDispose("FullCommentAsHTML",
|
|
|
|
clang_FullComment_getAsHTML(Comment));
|
2012-08-08 01:54:38 +08:00
|
|
|
{
|
|
|
|
CXString XML;
|
2012-09-11 04:32:42 +08:00
|
|
|
XML = clang_FullComment_getAsXML(Comment);
|
2012-08-08 01:54:38 +08:00
|
|
|
PrintCXStringWithPrefix("FullCommentAsXML", XML);
|
2014-05-03 07:30:59 +08:00
|
|
|
ValidateCommentXML(clang_getCString(XML), CommentSchemaFile);
|
2012-08-08 01:54:38 +08:00
|
|
|
clang_disposeString(XML);
|
|
|
|
}
|
|
|
|
|
2012-07-21 05:34:34 +08:00
|
|
|
DumpCXComment(Comment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-23 07:15:52 +08:00
|
|
|
typedef struct {
|
|
|
|
unsigned line;
|
|
|
|
unsigned col;
|
|
|
|
} LineCol;
|
|
|
|
|
|
|
|
static int lineCol_cmp(const void *p1, const void *p2) {
|
|
|
|
const LineCol *lhs = p1;
|
|
|
|
const LineCol *rhs = p2;
|
|
|
|
if (lhs->line != rhs->line)
|
|
|
|
return (int)lhs->line - (int)rhs->line;
|
|
|
|
return (int)lhs->col - (int)rhs->col;
|
|
|
|
}
|
|
|
|
|
2014-05-03 07:30:59 +08:00
|
|
|
static void PrintCursor(CXCursor Cursor, const char *CommentSchemaFile) {
|
2011-10-06 15:00:54 +08:00
|
|
|
CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor);
|
2010-02-17 08:41:32 +08:00
|
|
|
if (clang_isInvalid(Cursor.kind)) {
|
2013-07-23 04:58:30 +08:00
|
|
|
CXString ks = clang_getCursorKindSpelling(Cursor.kind);
|
2010-02-17 08:41:32 +08:00
|
|
|
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;
|
2011-07-26 01:48:11 +08:00
|
|
|
unsigned RefNameRangeNr;
|
|
|
|
CXSourceRange CursorExtent;
|
|
|
|
CXSourceRange RefNameRange;
|
2012-05-08 08:14:45 +08:00
|
|
|
int AlwaysUnavailable;
|
|
|
|
int AlwaysDeprecated;
|
|
|
|
CXString UnavailableMessage;
|
|
|
|
CXString DeprecatedMessage;
|
|
|
|
CXPlatformAvailability PlatformAvailability[2];
|
|
|
|
int NumPlatformAvailability;
|
|
|
|
int I;
|
2012-06-20 08:34:58 +08:00
|
|
|
|
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) {
|
2013-07-23 04:58:30 +08:00
|
|
|
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 {
|
2013-07-23 04:58:30 +08:00
|
|
|
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;
|
2011-10-06 15:27:49 +08:00
|
|
|
|
|
|
|
case CXAvailability_NotAccessible:
|
|
|
|
printf(" (inaccessible)");
|
|
|
|
break;
|
2010-08-24 07:00:57 +08:00
|
|
|
}
|
2010-08-26 09:42:22 +08:00
|
|
|
|
2012-05-08 08:14:45 +08:00
|
|
|
NumPlatformAvailability
|
|
|
|
= clang_getCursorPlatformAvailability(Cursor,
|
|
|
|
&AlwaysDeprecated,
|
|
|
|
&DeprecatedMessage,
|
|
|
|
&AlwaysUnavailable,
|
|
|
|
&UnavailableMessage,
|
|
|
|
PlatformAvailability, 2);
|
|
|
|
if (AlwaysUnavailable) {
|
|
|
|
printf(" (always unavailable: \"%s\")",
|
|
|
|
clang_getCString(UnavailableMessage));
|
|
|
|
} else if (AlwaysDeprecated) {
|
|
|
|
printf(" (always deprecated: \"%s\")",
|
|
|
|
clang_getCString(DeprecatedMessage));
|
|
|
|
} else {
|
|
|
|
for (I = 0; I != NumPlatformAvailability; ++I) {
|
|
|
|
if (I >= 2)
|
|
|
|
break;
|
|
|
|
|
|
|
|
printf(" (%s", clang_getCString(PlatformAvailability[I].Platform));
|
|
|
|
if (PlatformAvailability[I].Unavailable)
|
|
|
|
printf(", unavailable");
|
|
|
|
else {
|
|
|
|
printVersion(", introduced=", PlatformAvailability[I].Introduced);
|
|
|
|
printVersion(", deprecated=", PlatformAvailability[I].Deprecated);
|
|
|
|
printVersion(", obsoleted=", PlatformAvailability[I].Obsoleted);
|
|
|
|
}
|
|
|
|
if (clang_getCString(PlatformAvailability[I].Message)[0])
|
|
|
|
printf(", message=\"%s\"",
|
|
|
|
clang_getCString(PlatformAvailability[I].Message));
|
|
|
|
printf(")");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (I = 0; I != NumPlatformAvailability; ++I) {
|
|
|
|
if (I >= 2)
|
|
|
|
break;
|
|
|
|
clang_disposeCXPlatformAvailability(PlatformAvailability + I);
|
|
|
|
}
|
|
|
|
|
|
|
|
clang_disposeString(DeprecatedMessage);
|
|
|
|
clang_disposeString(UnavailableMessage);
|
|
|
|
|
2011-05-13 23:54:42 +08:00
|
|
|
if (clang_CXXMethod_isStatic(Cursor))
|
|
|
|
printf(" (static)");
|
|
|
|
if (clang_CXXMethod_isVirtual(Cursor))
|
|
|
|
printf(" (virtual)");
|
2014-04-07 22:59:13 +08:00
|
|
|
if (clang_CXXMethod_isConst(Cursor))
|
|
|
|
printf(" (const)");
|
2013-05-18 02:38:35 +08:00
|
|
|
if (clang_CXXMethod_isPureVirtual(Cursor))
|
|
|
|
printf(" (pure)");
|
2013-04-19 07:53:05 +08:00
|
|
|
if (clang_Cursor_isVariadic(Cursor))
|
|
|
|
printf(" (variadic)");
|
2013-07-06 04:44:37 +08:00
|
|
|
if (clang_Cursor_isObjCOptional(Cursor))
|
|
|
|
printf(" (@optional)");
|
|
|
|
|
2010-08-26 09:42:22 +08:00
|
|
|
if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
|
2013-07-23 04:58:30 +08:00
|
|
|
CXType T =
|
|
|
|
clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor));
|
|
|
|
CXString S = clang_getTypeKindSpelling(T.kind);
|
2010-08-26 09:42:22 +08:00
|
|
|
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");
|
|
|
|
}
|
2014-10-11 04:01:05 +08:00
|
|
|
|
2010-09-02 08:07:54 +08:00
|
|
|
SpecializationOf = clang_getSpecializedCursorTemplate(Cursor);
|
|
|
|
if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
|
2013-07-23 04:58:30 +08:00
|
|
|
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);
|
2014-10-11 04:01:05 +08:00
|
|
|
printf(" [Specialization of %s:%d:%d]",
|
2010-09-02 08:07:54 +08:00
|
|
|
clang_getCString(Name), line, column);
|
|
|
|
clang_disposeString(Name);
|
2014-10-11 04:01:05 +08:00
|
|
|
|
|
|
|
if (Cursor.kind == CXCursor_FunctionDecl) {
|
|
|
|
/* Collect the template parameter kinds from the base template. */
|
|
|
|
unsigned NumTemplateArgs = clang_Cursor_getNumTemplateArguments(Cursor);
|
|
|
|
unsigned I;
|
|
|
|
for (I = 0; I < NumTemplateArgs; I++) {
|
|
|
|
enum CXTemplateArgumentKind TAK =
|
|
|
|
clang_Cursor_getTemplateArgumentKind(Cursor, I);
|
|
|
|
switch(TAK) {
|
|
|
|
case CXTemplateArgumentKind_Type:
|
|
|
|
{
|
|
|
|
CXType T = clang_Cursor_getTemplateArgumentType(Cursor, I);
|
|
|
|
CXString S = clang_getTypeSpelling(T);
|
|
|
|
printf(" [Template arg %d: kind: %d, type: %s]",
|
|
|
|
I, TAK, clang_getCString(S));
|
|
|
|
clang_disposeString(S);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CXTemplateArgumentKind_Integral:
|
2015-05-14 14:53:31 +08:00
|
|
|
printf(" [Template arg %d: kind: %d, intval: %lld]",
|
2014-10-11 04:01:05 +08:00
|
|
|
I, TAK, clang_Cursor_getTemplateArgumentValue(Cursor, I));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf(" [Template arg %d: kind: %d]\n", I, TAK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-09-02 08:07:54 +08:00
|
|
|
}
|
2010-10-02 04:25:15 +08:00
|
|
|
|
|
|
|
clang_getOverriddenCursors(Cursor, &overridden, &num_overridden);
|
|
|
|
if (num_overridden) {
|
|
|
|
unsigned I;
|
2012-08-23 07:15:52 +08:00
|
|
|
LineCol lineCols[50];
|
|
|
|
assert(num_overridden <= 50);
|
2010-10-02 04:25:15 +08:00
|
|
|
printf(" [Overrides ");
|
|
|
|
for (I = 0; I != num_overridden; ++I) {
|
2013-07-23 04:58:30 +08:00
|
|
|
CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
|
2010-11-09 14:24:54 +08:00
|
|
|
clang_getSpellingLocation(Loc, 0, &line, &column, 0);
|
2012-08-23 07:15:52 +08:00
|
|
|
lineCols[I].line = line;
|
|
|
|
lineCols[I].col = column;
|
|
|
|
}
|
2012-08-30 08:45:32 +08:00
|
|
|
/* Make the order of the override list deterministic. */
|
2012-08-23 07:15:52 +08:00
|
|
|
qsort(lineCols, num_overridden, sizeof(LineCol), lineCol_cmp);
|
|
|
|
for (I = 0; I != num_overridden; ++I) {
|
2010-10-02 04:25:15 +08:00
|
|
|
if (I)
|
|
|
|
printf(", ");
|
2012-08-23 07:15:52 +08:00
|
|
|
printf("@%d:%d", lineCols[I].line, lineCols[I].col);
|
2010-10-02 04:25:15 +08:00
|
|
|
}
|
|
|
|
printf("]");
|
|
|
|
clang_disposeOverriddenCursors(overridden);
|
|
|
|
}
|
2010-10-21 06:00:55 +08:00
|
|
|
|
|
|
|
if (Cursor.kind == CXCursor_InclusionDirective) {
|
2013-07-23 04:58:30 +08:00
|
|
|
CXFile File = clang_getIncludedFile(Cursor);
|
|
|
|
CXString Included = clang_getFileName(File);
|
2010-10-21 06:00:55 +08:00
|
|
|
printf(" (%s)", clang_getCString(Included));
|
|
|
|
clang_disposeString(Included);
|
2011-05-04 08:14:37 +08:00
|
|
|
|
|
|
|
if (clang_isFileMultipleIncludeGuarded(TU, File))
|
|
|
|
printf(" [multi-include guarded]");
|
2010-10-21 06:00:55 +08:00
|
|
|
}
|
2011-07-26 01:48:11 +08:00
|
|
|
|
|
|
|
CursorExtent = clang_getCursorExtent(Cursor);
|
|
|
|
RefNameRange = clang_getCursorReferenceNameRange(Cursor,
|
|
|
|
CXNameRange_WantQualifier
|
|
|
|
| CXNameRange_WantSinglePiece
|
|
|
|
| CXNameRange_WantTemplateArgs,
|
|
|
|
0);
|
|
|
|
if (!clang_equalRanges(CursorExtent, RefNameRange))
|
|
|
|
PrintRange(RefNameRange, "SingleRefName");
|
|
|
|
|
|
|
|
for (RefNameRangeNr = 0; 1; RefNameRangeNr++) {
|
|
|
|
RefNameRange = clang_getCursorReferenceNameRange(Cursor,
|
|
|
|
CXNameRange_WantQualifier
|
|
|
|
| CXNameRange_WantTemplateArgs,
|
|
|
|
RefNameRangeNr);
|
|
|
|
if (clang_equalRanges(clang_getNullRange(), RefNameRange))
|
|
|
|
break;
|
|
|
|
if (!clang_equalRanges(CursorExtent, RefNameRange))
|
|
|
|
PrintRange(RefNameRange, "RefName");
|
|
|
|
}
|
2012-06-20 08:34:58 +08:00
|
|
|
|
2014-05-03 07:30:59 +08:00
|
|
|
PrintCursorComments(Cursor, CommentSchemaFile);
|
2013-04-19 06:15:49 +08:00
|
|
|
|
|
|
|
{
|
|
|
|
unsigned PropAttrs = clang_Cursor_getObjCPropertyAttributes(Cursor, 0);
|
|
|
|
if (PropAttrs != CXObjCPropertyAttr_noattr) {
|
|
|
|
printf(" [");
|
|
|
|
#define PRINT_PROP_ATTR(A) \
|
|
|
|
if (PropAttrs & CXObjCPropertyAttr_##A) printf(#A ",")
|
|
|
|
PRINT_PROP_ATTR(readonly);
|
|
|
|
PRINT_PROP_ATTR(getter);
|
|
|
|
PRINT_PROP_ATTR(assign);
|
|
|
|
PRINT_PROP_ATTR(readwrite);
|
|
|
|
PRINT_PROP_ATTR(retain);
|
|
|
|
PRINT_PROP_ATTR(copy);
|
|
|
|
PRINT_PROP_ATTR(nonatomic);
|
|
|
|
PRINT_PROP_ATTR(setter);
|
|
|
|
PRINT_PROP_ATTR(atomic);
|
|
|
|
PRINT_PROP_ATTR(weak);
|
|
|
|
PRINT_PROP_ATTR(strong);
|
|
|
|
PRINT_PROP_ATTR(unsafe_unretained);
|
|
|
|
printf("]");
|
|
|
|
}
|
|
|
|
}
|
2013-04-19 07:29:12 +08:00
|
|
|
|
|
|
|
{
|
|
|
|
unsigned QT = clang_Cursor_getObjCDeclQualifiers(Cursor);
|
|
|
|
if (QT != CXObjCDeclQualifier_None) {
|
|
|
|
printf(" [");
|
|
|
|
#define PRINT_OBJC_QUAL(A) \
|
|
|
|
if (QT & CXObjCDeclQualifier_##A) printf(#A ",")
|
|
|
|
PRINT_OBJC_QUAL(In);
|
|
|
|
PRINT_OBJC_QUAL(Inout);
|
|
|
|
PRINT_OBJC_QUAL(Out);
|
|
|
|
PRINT_OBJC_QUAL(Bycopy);
|
|
|
|
PRINT_OBJC_QUAL(Byref);
|
|
|
|
PRINT_OBJC_QUAL(Oneway);
|
|
|
|
printf("]");
|
|
|
|
}
|
|
|
|
}
|
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) {
|
2013-07-23 04:58:30 +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;
|
2011-11-03 10:20:36 +08:00
|
|
|
clang_getExpansionLocation(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);
|
2012-03-21 04:49:45 +08:00
|
|
|
fprintf(stderr, "Number FIX-ITs = %d\n", num_fixits);
|
2010-02-19 07:07:20 +08:00
|
|
|
for (i = 0; i != num_fixits; ++i) {
|
2010-02-20 02:16:06 +08:00
|
|
|
CXSourceRange range;
|
2013-07-23 04:58:30 +08:00
|
|
|
CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
|
|
|
|
CXSourceLocation start = clang_getRangeStart(range);
|
|
|
|
CXSourceLocation end = clang_getRangeEnd(range);
|
2010-02-20 02:16:06 +08:00
|
|
|
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
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2012-02-14 10:46:03 +08:00
|
|
|
void PrintDiagnosticSet(CXDiagnosticSet Set) {
|
|
|
|
int i = 0, n = clang_getNumDiagnosticsInSet(Set);
|
|
|
|
for ( ; i != n ; ++i) {
|
|
|
|
CXDiagnostic Diag = clang_getDiagnosticInSet(Set, i);
|
|
|
|
CXDiagnosticSet ChildDiags = clang_getChildDiagnostics(Diag);
|
2010-02-19 02:08:43 +08:00
|
|
|
PrintDiagnostic(Diag);
|
2012-02-14 10:46:03 +08:00
|
|
|
if (ChildDiags)
|
|
|
|
PrintDiagnosticSet(ChildDiags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintDiagnostics(CXTranslationUnit TU) {
|
|
|
|
CXDiagnosticSet TUSet = clang_getDiagnosticSetFromTU(TU);
|
|
|
|
PrintDiagnosticSet(TUSet);
|
|
|
|
clang_disposeDiagnosticSet(TUSet);
|
2010-02-19 02:08:43 +08:00
|
|
|
}
|
|
|
|
|
2011-04-19 06:47:10 +08:00
|
|
|
void PrintMemoryUsage(CXTranslationUnit TU) {
|
2011-08-30 00:37:29 +08:00
|
|
|
unsigned long total = 0;
|
2011-04-19 07:42:53 +08:00
|
|
|
unsigned i = 0;
|
2013-07-23 04:58:30 +08:00
|
|
|
CXTUResourceUsage usage = clang_getCXTUResourceUsage(TU);
|
2011-04-19 07:33:22 +08:00
|
|
|
fprintf(stderr, "Memory usage:\n");
|
2011-04-19 07:42:53 +08:00
|
|
|
for (i = 0 ; i != usage.numEntries; ++i) {
|
2011-04-21 00:41:07 +08:00
|
|
|
const char *name = clang_getTUResourceUsageName(usage.entries[i].kind);
|
2011-04-19 06:47:10 +08:00
|
|
|
unsigned long amount = usage.entries[i].amount;
|
|
|
|
total += amount;
|
2011-04-19 07:42:53 +08:00
|
|
|
fprintf(stderr, " %s : %ld bytes (%f MBytes)\n", name, amount,
|
2011-04-19 06:47:10 +08:00
|
|
|
((double) amount)/(1024*1024));
|
|
|
|
}
|
2011-04-19 07:42:53 +08:00
|
|
|
fprintf(stderr, " TOTAL = %ld bytes (%f MBytes)\n", total,
|
2011-04-19 06:47:10 +08:00
|
|
|
((double) total)/(1024*1024));
|
2011-04-21 00:41:07 +08:00
|
|
|
clang_disposeCXTUResourceUsage(usage);
|
2011-04-19 06:47:10 +08:00
|
|
|
}
|
|
|
|
|
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-19 08:34:46 +08:00
|
|
|
static void PrintCursorExtent(CXCursor C) {
|
2013-07-23 04:58:30 +08:00
|
|
|
CXSourceRange extent = clang_getCursorExtent(C);
|
|
|
|
PrintRange(extent, "Extent");
|
2010-01-06 07:18:49 +08:00
|
|
|
}
|
|
|
|
|
2012-08-08 01:54:38 +08:00
|
|
|
/* Data used by the visitors. */
|
|
|
|
typedef struct {
|
2010-01-21 05:32:04 +08:00
|
|
|
CXTranslationUnit TU;
|
|
|
|
enum CXCursorKind *Filter;
|
2014-05-03 07:30:59 +08:00
|
|
|
const char *CommentSchemaFile;
|
2010-01-21 05:32:04 +08:00
|
|
|
} 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)) {
|
2013-07-23 04:58:30 +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);
|
2014-05-03 07:30:59 +08:00
|
|
|
PrintCursor(Cursor, Data->CommentSchemaFile);
|
2010-01-19 08:34:46 +08:00
|
|
|
PrintCursorExtent(Cursor);
|
2013-04-12 01:02:10 +08:00
|
|
|
if (clang_isDeclaration(Cursor.kind)) {
|
|
|
|
enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
|
|
|
|
const char *accessStr = 0;
|
|
|
|
|
|
|
|
switch (access) {
|
|
|
|
case CX_CXXInvalidAccessSpecifier: break;
|
|
|
|
case CX_CXXPublic:
|
|
|
|
accessStr = "public"; break;
|
|
|
|
case CX_CXXProtected:
|
|
|
|
accessStr = "protected"; break;
|
|
|
|
case CX_CXXPrivate:
|
|
|
|
accessStr = "private"; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (accessStr)
|
|
|
|
printf(" [access=%s]", accessStr);
|
|
|
|
}
|
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)) {
|
2013-07-23 04:58:30 +08:00
|
|
|
CXSourceLocation RefLoc
|
|
|
|
= clang_getLocation(Data->TU, file, curLine, curColumn);
|
2010-01-23 05:44:22 +08:00
|
|
|
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);
|
2014-05-03 07:30:59 +08:00
|
|
|
PrintCursor(Ref, Data->CommentSchemaFile);
|
2010-01-19 06:46:11 +08:00
|
|
|
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)) {
|
2013-07-23 04:58:30 +08:00
|
|
|
CXString USR = clang_getCursorUSR(C);
|
|
|
|
const char *cstr = clang_getCString(USR);
|
2010-04-21 07:15:40 +08:00
|
|
|
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) {
|
2012-08-08 01:54:38 +08:00
|
|
|
PrintCursor(cursor, NULL);
|
2010-03-03 14:37:58 +08:00
|
|
|
printf("linkage=%s\n", linkage);
|
|
|
|
}
|
|
|
|
|
|
|
|
return CXChildVisit_Recurse;
|
|
|
|
}
|
|
|
|
|
2010-05-15 05:29:26 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
/* Typekind testing. */
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2013-02-16 05:15:49 +08:00
|
|
|
static void PrintTypeAndTypeKind(CXType T, const char *Format) {
|
|
|
|
CXString TypeSpelling, TypeKindSpelling;
|
|
|
|
|
|
|
|
TypeSpelling = clang_getTypeSpelling(T);
|
|
|
|
TypeKindSpelling = clang_getTypeKindSpelling(T.kind);
|
|
|
|
printf(Format,
|
|
|
|
clang_getCString(TypeSpelling),
|
|
|
|
clang_getCString(TypeKindSpelling));
|
|
|
|
clang_disposeString(TypeSpelling);
|
|
|
|
clang_disposeString(TypeKindSpelling);
|
|
|
|
}
|
|
|
|
|
2015-04-14 00:55:04 +08:00
|
|
|
static enum CXVisitorResult FieldVisitor(CXCursor C,
|
|
|
|
CXClientData client_data) {
|
|
|
|
(*(int *) client_data)+=1;
|
|
|
|
return CXVisit_Continue;
|
|
|
|
}
|
|
|
|
|
2013-02-16 05:15:49 +08:00
|
|
|
static enum CXChildVisitResult PrintType(CXCursor cursor, CXCursor p,
|
|
|
|
CXClientData d) {
|
2010-05-15 05:29:26 +08:00
|
|
|
if (!clang_isInvalid(clang_getCursorKind(cursor))) {
|
2013-07-23 04:58:30 +08:00
|
|
|
CXType T = clang_getCursorType(cursor);
|
2013-10-12 03:58:38 +08:00
|
|
|
enum CXRefQualifierKind RQ = clang_Type_getCXXRefQualifier(T);
|
2012-08-08 01:54:38 +08:00
|
|
|
PrintCursor(cursor, NULL);
|
2013-02-16 05:15:49 +08:00
|
|
|
PrintTypeAndTypeKind(T, " [type=%s] [typekind=%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");
|
2013-10-12 03:58:38 +08:00
|
|
|
if (RQ == CXRefQualifier_LValue)
|
|
|
|
printf(" lvalue-ref-qualifier");
|
|
|
|
if (RQ == CXRefQualifier_RValue)
|
|
|
|
printf(" rvalue-ref-qualifier");
|
2010-06-22 17:29:44 +08:00
|
|
|
/* Print the canonical type if it is different. */
|
2010-06-22 04:15:39 +08:00
|
|
|
{
|
2013-07-23 04:58:30 +08:00
|
|
|
CXType CT = clang_getCanonicalType(T);
|
2010-06-22 04:15:39 +08:00
|
|
|
if (!clang_equalTypes(T, CT)) {
|
2013-02-16 05:15:49 +08:00
|
|
|
PrintTypeAndTypeKind(CT, " [canonicaltype=%s] [canonicaltypekind=%s]");
|
2010-06-22 04:15:39 +08:00
|
|
|
}
|
|
|
|
}
|
2010-06-22 17:29:44 +08:00
|
|
|
/* Print the return type if it exists. */
|
2010-06-22 04:15:39 +08:00
|
|
|
{
|
2013-07-23 04:58:30 +08:00
|
|
|
CXType RT = clang_getCursorResultType(cursor);
|
2010-06-22 04:15:39 +08:00
|
|
|
if (RT.kind != CXType_Invalid) {
|
2013-02-16 05:15:49 +08:00
|
|
|
PrintTypeAndTypeKind(RT, " [resulttype=%s] [resulttypekind=%s]");
|
2010-06-22 04:15:39 +08:00
|
|
|
}
|
|
|
|
}
|
2012-04-12 03:32:19 +08:00
|
|
|
/* Print the argument types if they exist. */
|
|
|
|
{
|
2014-02-28 00:05:05 +08:00
|
|
|
int NumArgs = clang_Cursor_getNumArguments(cursor);
|
|
|
|
if (NumArgs != -1 && NumArgs != 0) {
|
2012-04-12 03:54:09 +08:00
|
|
|
int i;
|
2012-04-12 03:32:19 +08:00
|
|
|
printf(" [args=");
|
2014-02-28 00:05:05 +08:00
|
|
|
for (i = 0; i < NumArgs; ++i) {
|
2013-07-23 04:58:30 +08:00
|
|
|
CXType T = clang_getCursorType(clang_Cursor_getArgument(cursor, i));
|
2012-04-12 03:32:19 +08:00
|
|
|
if (T.kind != CXType_Invalid) {
|
2013-02-16 05:15:49 +08:00
|
|
|
PrintTypeAndTypeKind(T, " [%s] [%s]");
|
2012-04-12 03:32:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("]");
|
|
|
|
}
|
|
|
|
}
|
2014-02-28 00:05:05 +08:00
|
|
|
/* Print the template argument types if they exist. */
|
|
|
|
{
|
|
|
|
int NumTArgs = clang_Type_getNumTemplateArguments(T);
|
|
|
|
if (NumTArgs != -1 && NumTArgs != 0) {
|
|
|
|
int i;
|
|
|
|
printf(" [templateargs/%d=", NumTArgs);
|
|
|
|
for (i = 0; i < NumTArgs; ++i) {
|
|
|
|
CXType TArg = clang_Type_getTemplateArgumentAsType(T, i);
|
|
|
|
if (TArg.kind != CXType_Invalid) {
|
|
|
|
PrintTypeAndTypeKind(TArg, " [type=%s] [typekind=%s]");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("]");
|
|
|
|
}
|
|
|
|
}
|
2010-07-30 08:14:11 +08:00
|
|
|
/* Print if this is a non-POD type. */
|
|
|
|
printf(" [isPOD=%d]", clang_isPODType(T));
|
2014-04-10 03:16:08 +08:00
|
|
|
/* Print the pointee type. */
|
|
|
|
{
|
|
|
|
CXType PT = clang_getPointeeType(T);
|
|
|
|
if (PT.kind != CXType_Invalid) {
|
|
|
|
PrintTypeAndTypeKind(PT, " [pointeetype=%s] [pointeekind=%s]");
|
|
|
|
}
|
|
|
|
}
|
2015-04-14 00:55:04 +08:00
|
|
|
/* Print the number of fields if they exist. */
|
|
|
|
{
|
|
|
|
int numFields = 0;
|
|
|
|
if (clang_Type_visitFields(T, FieldVisitor, &numFields)){
|
|
|
|
if (numFields != 0) {
|
|
|
|
printf(" [nbFields=%d]", numFields);
|
|
|
|
}
|
|
|
|
/* Print if it is an anonymous record. */
|
|
|
|
{
|
|
|
|
unsigned isAnon = clang_Cursor_isAnonymous(cursor);
|
|
|
|
if (isAnon != 0) {
|
|
|
|
printf(" [isAnon=%d]", isAnon);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-06-22 04:15:39 +08:00
|
|
|
|
2010-05-15 05:29:26 +08:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
return CXChildVisit_Recurse;
|
|
|
|
}
|
|
|
|
|
2013-04-11 09:20:11 +08:00
|
|
|
static enum CXChildVisitResult PrintTypeSize(CXCursor cursor, CXCursor p,
|
|
|
|
CXClientData d) {
|
|
|
|
CXType T;
|
|
|
|
enum CXCursorKind K = clang_getCursorKind(cursor);
|
|
|
|
if (clang_isInvalid(K))
|
|
|
|
return CXChildVisit_Recurse;
|
|
|
|
T = clang_getCursorType(cursor);
|
|
|
|
PrintCursor(cursor, NULL);
|
|
|
|
PrintTypeAndTypeKind(T, " [type=%s] [typekind=%s]");
|
|
|
|
/* Print the type sizeof if applicable. */
|
|
|
|
{
|
|
|
|
long long Size = clang_Type_getSizeOf(T);
|
|
|
|
if (Size >= 0 || Size < -1 ) {
|
2015-05-14 14:53:31 +08:00
|
|
|
printf(" [sizeof=%lld]", Size);
|
2013-04-11 09:20:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Print the type alignof if applicable. */
|
|
|
|
{
|
|
|
|
long long Align = clang_Type_getAlignOf(T);
|
|
|
|
if (Align >= 0 || Align < -1) {
|
2015-05-14 14:53:31 +08:00
|
|
|
printf(" [alignof=%lld]", Align);
|
2013-04-11 09:20:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Print the record field offset if applicable. */
|
|
|
|
{
|
2014-04-24 12:14:12 +08:00
|
|
|
CXString FieldSpelling = clang_getCursorSpelling(cursor);
|
|
|
|
const char *FieldName = clang_getCString(FieldSpelling);
|
2015-04-14 00:55:04 +08:00
|
|
|
/* recurse to get the first parent record that is not anonymous. */
|
|
|
|
CXCursor Parent, Record;
|
|
|
|
unsigned RecordIsAnonymous = 0;
|
2014-04-24 12:14:12 +08:00
|
|
|
if (clang_getCursorKind(cursor) == CXCursor_FieldDecl) {
|
2015-04-14 00:55:04 +08:00
|
|
|
Record = Parent = p;
|
2013-04-11 09:20:11 +08:00
|
|
|
do {
|
2015-04-14 00:55:04 +08:00
|
|
|
Record = Parent;
|
|
|
|
Parent = clang_getCursorSemanticParent(Record);
|
|
|
|
RecordIsAnonymous = clang_Cursor_isAnonymous(Record);
|
|
|
|
/* Recurse as long as the parent is a CXType_Record and the Record
|
|
|
|
is anonymous */
|
|
|
|
} while ( clang_getCursorType(Parent).kind == CXType_Record &&
|
|
|
|
RecordIsAnonymous > 0);
|
2013-04-11 09:20:11 +08:00
|
|
|
{
|
2015-04-14 00:55:04 +08:00
|
|
|
long long Offset = clang_Type_getOffsetOf(clang_getCursorType(Record),
|
2013-04-11 09:20:11 +08:00
|
|
|
FieldName);
|
2015-04-14 00:55:04 +08:00
|
|
|
long long Offset2 = clang_Cursor_getOffsetOfField(cursor);
|
|
|
|
if (Offset == Offset2){
|
2015-05-14 14:53:31 +08:00
|
|
|
printf(" [offsetof=%lld]", Offset);
|
2015-04-14 00:55:04 +08:00
|
|
|
} else {
|
|
|
|
/* Offsets will be different in anonymous records. */
|
2015-05-14 14:53:31 +08:00
|
|
|
printf(" [offsetof=%lld/%lld]", Offset, Offset2);
|
2015-04-14 00:55:04 +08:00
|
|
|
}
|
2013-04-11 09:20:11 +08:00
|
|
|
}
|
|
|
|
}
|
2014-04-24 12:14:12 +08:00
|
|
|
clang_disposeString(FieldSpelling);
|
2013-04-11 09:20:11 +08:00
|
|
|
}
|
|
|
|
/* Print if its a bitfield */
|
|
|
|
{
|
|
|
|
int IsBitfield = clang_Cursor_isBitField(cursor);
|
|
|
|
if (IsBitfield)
|
|
|
|
printf(" [BitFieldSize=%d]", clang_getFieldDeclBitWidth(cursor));
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
return CXChildVisit_Recurse;
|
|
|
|
}
|
|
|
|
|
2014-08-01 02:04:56 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
/* Mangling testing. */
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
static enum CXChildVisitResult PrintMangledName(CXCursor cursor, CXCursor p,
|
|
|
|
CXClientData d) {
|
2015-10-08 08:01:20 +08:00
|
|
|
if (clang_isUnexposed(clang_getCursorKind(cursor)))
|
|
|
|
return CXChildVisit_Recurse;
|
2014-08-01 02:04:56 +08:00
|
|
|
CXString MangledName;
|
|
|
|
PrintCursor(cursor, NULL);
|
|
|
|
MangledName = clang_Cursor_getMangling(cursor);
|
|
|
|
printf(" [mangled=%s]\n", clang_getCString(MangledName));
|
2014-08-01 20:55:44 +08:00
|
|
|
clang_disposeString(MangledName);
|
2014-08-01 02:04:56 +08:00
|
|
|
return CXChildVisit_Continue;
|
|
|
|
}
|
|
|
|
|
2012-12-04 23:13:46 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
/* Bitwidth testing. */
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
static enum CXChildVisitResult PrintBitWidth(CXCursor cursor, CXCursor p,
|
|
|
|
CXClientData d) {
|
2012-12-04 23:32:03 +08:00
|
|
|
int Bitwidth;
|
2012-12-04 23:13:46 +08:00
|
|
|
if (clang_getCursorKind(cursor) != CXCursor_FieldDecl)
|
|
|
|
return CXChildVisit_Recurse;
|
|
|
|
|
2012-12-04 23:32:03 +08:00
|
|
|
Bitwidth = clang_getFieldDeclBitWidth(cursor);
|
2012-12-04 23:13:46 +08:00
|
|
|
if (Bitwidth >= 0) {
|
|
|
|
PrintCursor(cursor, NULL);
|
|
|
|
printf(" bitwidth=%d\n", Bitwidth);
|
|
|
|
}
|
|
|
|
|
|
|
|
return CXChildVisit_Recurse;
|
|
|
|
}
|
2010-05-15 05:29:26 +08:00
|
|
|
|
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,
|
2012-08-08 01:54:38 +08:00
|
|
|
PostVisitTU PV,
|
|
|
|
const char *CommentSchemaFile) {
|
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;
|
2014-05-03 07:30:59 +08:00
|
|
|
Data.CommentSchemaFile = CommentSchemaFile;
|
2010-01-27 01:55:33 +08:00
|
|
|
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);
|
2011-11-14 07:39:14 +08:00
|
|
|
if (checkForErrors(TU) != 0) {
|
|
|
|
clang_disposeTranslationUnit(TU);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
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,
|
2013-03-02 05:41:22 +08:00
|
|
|
/* displayDiagnostics=*/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
|
|
|
|
2012-08-08 01:54:38 +08:00
|
|
|
result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV, NULL);
|
2010-02-11 15:41:25 +08:00
|
|
|
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;
|
2012-08-08 01:54:38 +08:00
|
|
|
const char *CommentSchemaFile;
|
2010-01-23 08:14:00 +08:00
|
|
|
struct CXUnsavedFile *unsaved_files = 0;
|
|
|
|
int num_unsaved_files = 0;
|
2014-02-13 03:12:37 +08:00
|
|
|
enum CXErrorCode Err;
|
2010-01-23 08:14:00 +08:00
|
|
|
int result;
|
2014-02-13 03:12:37 +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,
|
2013-04-10 04:29:24 +08:00
|
|
|
/* displayDiagnostics=*/1);
|
2009-12-01 10:03:10 +08:00
|
|
|
|
2012-08-08 01:54:38 +08:00
|
|
|
if ((CommentSchemaFile = parse_comments_schema(argc, argv))) {
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2014-02-13 03:12:37 +08:00
|
|
|
Err = clang_parseTranslationUnit2(Idx, 0,
|
|
|
|
argv + num_unsaved_files,
|
|
|
|
argc - num_unsaved_files,
|
|
|
|
unsaved_files, num_unsaved_files,
|
|
|
|
getDefaultParsingOptions(), &TU);
|
|
|
|
if (Err != CXError_Success) {
|
2009-12-01 10:03:10 +08:00
|
|
|
fprintf(stderr, "Unable to load translation unit!\n");
|
2014-02-13 03:12:37 +08:00
|
|
|
describeLibclangFailure(Err);
|
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;
|
|
|
|
}
|
|
|
|
|
2012-08-08 01:54:38 +08:00
|
|
|
result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV,
|
|
|
|
CommentSchemaFile);
|
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;
|
2013-12-05 16:19:23 +08:00
|
|
|
int compiler_arg_idx = 0;
|
2014-02-13 03:12:37 +08:00
|
|
|
enum CXErrorCode Err;
|
2013-12-05 16:19:23 +08:00
|
|
|
int result, i;
|
2010-07-20 05:46:24 +08:00
|
|
|
int trial;
|
2011-09-13 02:09:31 +08:00
|
|
|
int remap_after_trial = 0;
|
|
|
|
char *endptr = 0;
|
2010-07-20 05:46:24 +08:00
|
|
|
|
|
|
|
Idx = clang_createIndex(/* excludeDeclsFromPCH */
|
|
|
|
!strcmp(filter, "local") ? 1 : 0,
|
2013-04-10 04:29:24 +08:00
|
|
|
/* displayDiagnostics=*/1);
|
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;
|
|
|
|
}
|
2013-12-05 16:19:23 +08:00
|
|
|
|
|
|
|
for (i = 0; i < argc; ++i) {
|
|
|
|
if (strcmp(argv[i], "--") == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i < argc)
|
|
|
|
compiler_arg_idx = i+1;
|
|
|
|
if (num_unsaved_files > compiler_arg_idx)
|
|
|
|
compiler_arg_idx = num_unsaved_files;
|
2010-07-20 05:46:24 +08:00
|
|
|
|
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. */
|
2014-02-13 03:12:37 +08:00
|
|
|
Err = clang_parseTranslationUnit2(Idx, 0,
|
|
|
|
argv + compiler_arg_idx,
|
|
|
|
argc - compiler_arg_idx,
|
|
|
|
0, 0, getDefaultParsingOptions(), &TU);
|
|
|
|
if (Err != CXError_Success) {
|
2010-07-20 05:46:24 +08:00
|
|
|
fprintf(stderr, "Unable to load translation unit!\n");
|
2014-02-13 03:12:37 +08:00
|
|
|
describeLibclangFailure(Err);
|
2010-07-20 05:46:24 +08:00
|
|
|
free_remapped_files(unsaved_files, num_unsaved_files);
|
|
|
|
clang_disposeIndex(Idx);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-11-14 06:08:33 +08:00
|
|
|
if (checkForErrors(TU) != 0)
|
|
|
|
return -1;
|
|
|
|
|
2011-09-13 02:09:31 +08:00
|
|
|
if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) {
|
|
|
|
remap_after_trial =
|
|
|
|
strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10);
|
|
|
|
}
|
|
|
|
|
2010-07-20 05:46:24 +08:00
|
|
|
for (trial = 0; trial < trials; ++trial) {
|
2013-12-05 16:19:23 +08:00
|
|
|
free_remapped_files(unsaved_files, num_unsaved_files);
|
|
|
|
if (parse_remapped_files_with_try(trial, argc, argv, 0,
|
|
|
|
&unsaved_files, &num_unsaved_files)) {
|
|
|
|
clang_disposeTranslationUnit(TU);
|
|
|
|
clang_disposeIndex(Idx);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-02-13 03:12:37 +08:00
|
|
|
Err = clang_reparseTranslationUnit(
|
|
|
|
TU,
|
|
|
|
trial >= remap_after_trial ? num_unsaved_files : 0,
|
|
|
|
trial >= remap_after_trial ? unsaved_files : 0,
|
|
|
|
clang_defaultReparseOptions(TU));
|
|
|
|
if (Err != CXError_Success) {
|
2010-08-19 07:09:16 +08:00
|
|
|
fprintf(stderr, "Unable to reparse translation unit!\n");
|
2014-02-13 03:12:37 +08:00
|
|
|
describeLibclangFailure(Err);
|
2010-07-20 05:46:24 +08:00
|
|
|
clang_disposeTranslationUnit(TU);
|
|
|
|
free_remapped_files(unsaved_files, num_unsaved_files);
|
|
|
|
clang_disposeIndex(Idx);
|
|
|
|
return -1;
|
|
|
|
}
|
2011-11-14 06:08:33 +08:00
|
|
|
|
|
|
|
if (checkForErrors(TU) != 0)
|
|
|
|
return -1;
|
2010-07-20 05:46:24 +08:00
|
|
|
}
|
|
|
|
|
2012-08-08 01:54:38 +08:00
|
|
|
result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV, NULL);
|
2011-11-14 06:08:33 +08:00
|
|
|
|
2010-07-20 05:46:24 +08:00
|
|
|
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(). */
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2011-05-04 08:14:37 +08:00
|
|
|
static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor,
|
2009-11-18 03:37:36 +08:00
|
|
|
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(" ");
|
2012-08-08 01:54:38 +08:00
|
|
|
PrintCursor(cursor, NULL);
|
2009-11-18 03:37:36 +08:00
|
|
|
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;
|
2013-07-23 04:58:30 +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,
|
2013-03-02 05:41:22 +08:00
|
|
|
/* displayDiagnostics=*/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);
|
2014-08-18 23:18:56 +08:00
|
|
|
clang_disposeTranslationUnit(TU);
|
2009-11-18 03:37:36 +08:00
|
|
|
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) {
|
2011-05-04 08:14:37 +08:00
|
|
|
print_cursor_file_scan(TU, 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";
|
|
|
|
}
|
|
|
|
|
2011-10-29 06:54:36 +08:00
|
|
|
static int checkForErrors(CXTranslationUnit TU) {
|
|
|
|
unsigned Num, i;
|
|
|
|
CXDiagnostic Diag;
|
|
|
|
CXString DiagStr;
|
|
|
|
|
|
|
|
if (!getenv("CINDEXTEST_FAILONERROR"))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
Num = clang_getNumDiagnostics(TU);
|
|
|
|
for (i = 0; i != Num; ++i) {
|
|
|
|
Diag = clang_getDiagnostic(TU, i);
|
|
|
|
if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) {
|
|
|
|
DiagStr = clang_formatDiagnostic(Diag,
|
|
|
|
clang_defaultDiagnosticDisplayOptions());
|
|
|
|
fprintf(stderr, "%s\n", clang_getCString(DiagStr));
|
|
|
|
clang_disposeString(DiagStr);
|
|
|
|
clang_disposeDiagnostic(Diag);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
clang_disposeDiagnostic(Diag);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-08 05:05:22 +08:00
|
|
|
static 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
|
|
|
}
|
|
|
|
|
2014-05-08 05:05:22 +08:00
|
|
|
static void print_completion_result(CXCompletionResult *completion_result,
|
2014-05-08 05:09:42 +08:00
|
|
|
FILE *file) {
|
2013-07-23 04:58:30 +08:00
|
|
|
CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
|
2011-10-14 23:31:08 +08:00
|
|
|
unsigned annotationCount;
|
2012-03-28 07:34:16 +08:00
|
|
|
enum CXCursorKind ParentKind;
|
|
|
|
CXString ParentName;
|
2012-07-03 01:35:10 +08:00
|
|
|
CXString BriefComment;
|
|
|
|
const char *BriefCommentCString;
|
2012-03-28 07:34:16 +08:00
|
|
|
|
2010-02-17 08:41:32 +08:00
|
|
|
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;
|
2011-10-06 15:27:49 +08:00
|
|
|
|
|
|
|
case CXAvailability_NotAccessible:
|
|
|
|
fprintf(file, " (inaccessible)");
|
|
|
|
break;
|
2010-08-24 07:00:57 +08:00
|
|
|
}
|
2011-10-14 23:31:08 +08:00
|
|
|
|
|
|
|
annotationCount = clang_getCompletionNumAnnotations(
|
|
|
|
completion_result->CompletionString);
|
|
|
|
if (annotationCount) {
|
|
|
|
unsigned i;
|
|
|
|
fprintf(file, " (");
|
|
|
|
for (i = 0; i < annotationCount; ++i) {
|
|
|
|
if (i != 0)
|
|
|
|
fprintf(file, ", ");
|
|
|
|
fprintf(file, "\"%s\"",
|
|
|
|
clang_getCString(clang_getCompletionAnnotation(
|
|
|
|
completion_result->CompletionString, i)));
|
|
|
|
}
|
|
|
|
fprintf(file, ")");
|
|
|
|
}
|
|
|
|
|
2012-03-28 07:34:16 +08:00
|
|
|
if (!getenv("CINDEXTEST_NO_COMPLETION_PARENTS")) {
|
|
|
|
ParentName = clang_getCompletionParent(completion_result->CompletionString,
|
|
|
|
&ParentKind);
|
|
|
|
if (ParentKind != CXCursor_NotImplemented) {
|
2013-07-23 04:58:30 +08:00
|
|
|
CXString KindSpelling = clang_getCursorKindSpelling(ParentKind);
|
2012-03-28 07:34:16 +08:00
|
|
|
fprintf(file, " (parent: %s '%s')",
|
|
|
|
clang_getCString(KindSpelling),
|
|
|
|
clang_getCString(ParentName));
|
|
|
|
clang_disposeString(KindSpelling);
|
|
|
|
}
|
|
|
|
clang_disposeString(ParentName);
|
|
|
|
}
|
2012-07-03 01:35:10 +08:00
|
|
|
|
|
|
|
BriefComment = clang_getCompletionBriefComment(
|
|
|
|
completion_result->CompletionString);
|
|
|
|
BriefCommentCString = clang_getCString(BriefComment);
|
|
|
|
if (BriefCommentCString && *BriefCommentCString != '\0') {
|
|
|
|
fprintf(file, "(brief comment: %s)", BriefCommentCString);
|
|
|
|
}
|
|
|
|
clang_disposeString(BriefComment);
|
2012-03-28 07:34:16 +08:00
|
|
|
|
2010-08-24 07:00:57 +08:00
|
|
|
fprintf(file, "\n");
|
2009-11-07 08:00:49 +08:00
|
|
|
}
|
|
|
|
|
2011-07-08 00:03:39 +08:00
|
|
|
void print_completion_contexts(unsigned long long contexts, FILE *file) {
|
|
|
|
fprintf(file, "Completion contexts:\n");
|
|
|
|
if (contexts == CXCompletionContext_Unknown) {
|
|
|
|
fprintf(file, "Unknown\n");
|
|
|
|
}
|
|
|
|
if (contexts & CXCompletionContext_AnyType) {
|
|
|
|
fprintf(file, "Any type\n");
|
|
|
|
}
|
|
|
|
if (contexts & CXCompletionContext_AnyValue) {
|
|
|
|
fprintf(file, "Any value\n");
|
|
|
|
}
|
|
|
|
if (contexts & CXCompletionContext_ObjCObjectValue) {
|
|
|
|
fprintf(file, "Objective-C object value\n");
|
|
|
|
}
|
|
|
|
if (contexts & CXCompletionContext_ObjCSelectorValue) {
|
|
|
|
fprintf(file, "Objective-C selector value\n");
|
|
|
|
}
|
|
|
|
if (contexts & CXCompletionContext_CXXClassTypeValue) {
|
|
|
|
fprintf(file, "C++ class type value\n");
|
|
|
|
}
|
|
|
|
if (contexts & CXCompletionContext_DotMemberAccess) {
|
|
|
|
fprintf(file, "Dot member access\n");
|
|
|
|
}
|
|
|
|
if (contexts & CXCompletionContext_ArrowMemberAccess) {
|
|
|
|
fprintf(file, "Arrow member access\n");
|
|
|
|
}
|
|
|
|
if (contexts & CXCompletionContext_ObjCPropertyAccess) {
|
|
|
|
fprintf(file, "Objective-C property access\n");
|
|
|
|
}
|
|
|
|
if (contexts & CXCompletionContext_EnumTag) {
|
|
|
|
fprintf(file, "Enum tag\n");
|
|
|
|
}
|
|
|
|
if (contexts & CXCompletionContext_UnionTag) {
|
|
|
|
fprintf(file, "Union tag\n");
|
|
|
|
}
|
|
|
|
if (contexts & CXCompletionContext_StructTag) {
|
|
|
|
fprintf(file, "Struct tag\n");
|
|
|
|
}
|
|
|
|
if (contexts & CXCompletionContext_ClassTag) {
|
|
|
|
fprintf(file, "Class name\n");
|
|
|
|
}
|
|
|
|
if (contexts & CXCompletionContext_Namespace) {
|
|
|
|
fprintf(file, "Namespace or namespace alias\n");
|
|
|
|
}
|
|
|
|
if (contexts & CXCompletionContext_NestedNameSpecifier) {
|
|
|
|
fprintf(file, "Nested name specifier\n");
|
|
|
|
}
|
|
|
|
if (contexts & CXCompletionContext_ObjCInterface) {
|
|
|
|
fprintf(file, "Objective-C interface\n");
|
|
|
|
}
|
|
|
|
if (contexts & CXCompletionContext_ObjCProtocol) {
|
|
|
|
fprintf(file, "Objective-C protocol\n");
|
|
|
|
}
|
|
|
|
if (contexts & CXCompletionContext_ObjCCategory) {
|
|
|
|
fprintf(file, "Objective-C category\n");
|
|
|
|
}
|
|
|
|
if (contexts & CXCompletionContext_ObjCInstanceMessage) {
|
|
|
|
fprintf(file, "Objective-C instance method\n");
|
|
|
|
}
|
|
|
|
if (contexts & CXCompletionContext_ObjCClassMessage) {
|
|
|
|
fprintf(file, "Objective-C class method\n");
|
|
|
|
}
|
|
|
|
if (contexts & CXCompletionContext_ObjCSelectorName) {
|
|
|
|
fprintf(file, "Objective-C selector name\n");
|
|
|
|
}
|
|
|
|
if (contexts & CXCompletionContext_MacroName) {
|
|
|
|
fprintf(file, "Macro name\n");
|
|
|
|
}
|
|
|
|
if (contexts & CXCompletionContext_NaturalLanguage) {
|
|
|
|
fprintf(file, "Natural language\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-26 21:48:20 +08:00
|
|
|
int my_stricmp(const char *s1, const char *s2) {
|
|
|
|
while (*s1 && *s2) {
|
2011-03-09 11:02:28 +08:00
|
|
|
int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
|
2010-08-26 21:48:20 +08:00
|
|
|
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;
|
2014-02-13 03:12:37 +08:00
|
|
|
enum CXErrorCode Err;
|
|
|
|
CXTranslationUnit TU;
|
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;
|
2012-07-03 01:35:10 +08:00
|
|
|
if (getenv("CINDEXTEST_COMPLETION_BRIEF_COMMENTS"))
|
|
|
|
completionOptions |= CXCodeComplete_IncludeBriefComments;
|
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;
|
2014-02-13 03:12:37 +08:00
|
|
|
|
|
|
|
Err = clang_parseTranslationUnit2(CIdx, 0,
|
|
|
|
argv + num_unsaved_files + 2,
|
|
|
|
argc - num_unsaved_files - 2,
|
|
|
|
0, 0, getDefaultParsingOptions(), &TU);
|
|
|
|
if (Err != CXError_Success) {
|
2010-10-12 05:37:58 +08:00
|
|
|
fprintf(stderr, "Unable to load translation unit!\n");
|
2014-02-13 03:12:37 +08:00
|
|
|
describeLibclangFailure(Err);
|
2010-10-12 05:37:58 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2010-11-16 07:00:34 +08:00
|
|
|
|
2014-02-13 03:12:37 +08:00
|
|
|
Err = clang_reparseTranslationUnit(TU, 0, 0,
|
|
|
|
clang_defaultReparseOptions(TU));
|
|
|
|
|
|
|
|
if (Err != CXError_Success) {
|
2015-06-19 00:41:51 +08:00
|
|
|
fprintf(stderr, "Unable to reparse translation unit!\n");
|
2014-02-13 03:12:37 +08:00
|
|
|
describeLibclangFailure(Err);
|
|
|
|
clang_disposeTranslationUnit(TU);
|
2010-11-16 07:00:34 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2014-02-13 03:12:37 +08:00
|
|
|
|
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) {
|
2011-07-21 09:05:26 +08:00
|
|
|
unsigned i, n = results->NumResults, containerIsIncomplete = 0;
|
2011-07-08 00:03:39 +08:00
|
|
|
unsigned long long contexts;
|
2011-07-21 09:05:26 +08:00
|
|
|
enum CXCursorKind containerKind;
|
2011-07-26 23:24:30 +08:00
|
|
|
CXString objCSelector;
|
|
|
|
const char *selectorString;
|
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);
|
|
|
|
}
|
2011-07-08 00:03:39 +08:00
|
|
|
|
|
|
|
contexts = clang_codeCompleteGetContexts(results);
|
|
|
|
print_completion_contexts(contexts, stdout);
|
|
|
|
|
2011-07-26 23:24:30 +08:00
|
|
|
containerKind = clang_codeCompleteGetContainerKind(results,
|
|
|
|
&containerIsIncomplete);
|
2011-07-21 09:05:26 +08:00
|
|
|
|
|
|
|
if (containerKind != CXCursor_InvalidCode) {
|
|
|
|
/* We have found a container */
|
|
|
|
CXString containerUSR, containerKindSpelling;
|
|
|
|
containerKindSpelling = clang_getCursorKindSpelling(containerKind);
|
|
|
|
printf("Container Kind: %s\n", clang_getCString(containerKindSpelling));
|
|
|
|
clang_disposeString(containerKindSpelling);
|
|
|
|
|
|
|
|
if (containerIsIncomplete) {
|
|
|
|
printf("Container is incomplete\n");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf("Container is complete\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
containerUSR = clang_codeCompleteGetContainerUSR(results);
|
|
|
|
printf("Container USR: %s\n", clang_getCString(containerUSR));
|
|
|
|
clang_disposeString(containerUSR);
|
|
|
|
}
|
|
|
|
|
2011-07-26 23:24:30 +08:00
|
|
|
objCSelector = clang_codeCompleteGetObjCSelector(results);
|
|
|
|
selectorString = clang_getCString(objCSelector);
|
|
|
|
if (selectorString && strlen(selectorString) > 0) {
|
|
|
|
printf("Objective-C selector: %s\n", selectorString);
|
|
|
|
}
|
|
|
|
clang_disposeString(objCSelector);
|
|
|
|
|
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;
|
|
|
|
|
2011-10-06 15:00:54 +08:00
|
|
|
static int inspect_cursor_at(int argc, const char **argv) {
|
2010-01-16 03:40:17 +08:00
|
|
|
CXIndex CIdx;
|
|
|
|
int errorCode;
|
|
|
|
struct CXUnsavedFile *unsaved_files = 0;
|
|
|
|
int num_unsaved_files = 0;
|
2014-02-13 03:12:37 +08:00
|
|
|
enum CXErrorCode Err;
|
2010-01-16 03:40:17 +08:00
|
|
|
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);
|
2014-02-13 03:12:37 +08:00
|
|
|
Err = clang_parseTranslationUnit2(CIdx, argv[argc - 1],
|
|
|
|
argv + num_unsaved_files + 1 + NumLocations,
|
|
|
|
argc - num_unsaved_files - 2 - NumLocations,
|
|
|
|
unsaved_files,
|
|
|
|
Repeats > 1? 0 : num_unsaved_files,
|
|
|
|
getDefaultParsingOptions(), &TU);
|
|
|
|
if (Err != CXError_Success) {
|
2010-01-16 03:40:17 +08:00
|
|
|
fprintf(stderr, "unable to parse input\n");
|
2014-02-13 03:12:37 +08:00
|
|
|
describeLibclangFailure(Err);
|
2010-01-16 03:40:17 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2010-02-17 08:41:32 +08:00
|
|
|
|
2011-10-29 06:54:36 +08:00
|
|
|
if (checkForErrors(TU) != 0)
|
|
|
|
return -1;
|
|
|
|
|
2010-11-30 14:04:54 +08:00
|
|
|
for (I = 0; I != Repeats; ++I) {
|
2014-02-13 03:12:37 +08:00
|
|
|
if (Repeats > 1) {
|
|
|
|
Err = clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
|
|
|
|
clang_defaultReparseOptions(TU));
|
|
|
|
if (Err != CXError_Success) {
|
|
|
|
describeLibclangFailure(Err);
|
|
|
|
clang_disposeTranslationUnit(TU);
|
|
|
|
return 1;
|
|
|
|
}
|
2010-11-30 13:52:55 +08:00
|
|
|
}
|
2011-10-29 06:54:36 +08:00
|
|
|
|
|
|
|
if (checkForErrors(TU) != 0)
|
|
|
|
return -1;
|
2010-11-30 13:52:55 +08:00
|
|
|
|
|
|
|
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));
|
2011-10-29 06:54:36 +08:00
|
|
|
|
|
|
|
if (checkForErrors(TU) != 0)
|
|
|
|
return -1;
|
|
|
|
|
2010-11-30 13:52:55 +08:00
|
|
|
if (I + 1 == Repeats) {
|
2013-07-23 04:58:30 +08:00
|
|
|
CXCompletionString completionString = clang_getCursorCompletionString(
|
|
|
|
Cursor);
|
|
|
|
CXSourceLocation CursorLoc = clang_getCursorLocation(Cursor);
|
2012-03-30 08:19:05 +08:00
|
|
|
CXString Spelling;
|
|
|
|
const char *cspell;
|
|
|
|
unsigned line, column;
|
|
|
|
clang_getSpellingLocation(CursorLoc, 0, &line, &column, 0);
|
|
|
|
printf("%d:%d ", line, column);
|
2012-08-08 01:54:38 +08:00
|
|
|
PrintCursor(Cursor, NULL);
|
2012-03-30 08:19:05 +08:00
|
|
|
PrintCursorExtent(Cursor);
|
|
|
|
Spelling = clang_getCursorSpelling(Cursor);
|
|
|
|
cspell = clang_getCString(Spelling);
|
2012-03-31 04:58:35 +08:00
|
|
|
if (cspell && strlen(cspell) != 0) {
|
|
|
|
unsigned pieceIndex;
|
|
|
|
printf(" Spelling=%s (", cspell);
|
|
|
|
for (pieceIndex = 0; ; ++pieceIndex) {
|
2013-07-23 04:58:30 +08:00
|
|
|
CXSourceRange range =
|
|
|
|
clang_Cursor_getSpellingNameRange(Cursor, pieceIndex, 0);
|
2012-03-31 04:58:35 +08:00
|
|
|
if (clang_Range_isNull(range))
|
|
|
|
break;
|
|
|
|
PrintRange(range, 0);
|
|
|
|
}
|
|
|
|
printf(")");
|
|
|
|
}
|
2012-03-30 08:19:05 +08:00
|
|
|
clang_disposeString(Spelling);
|
2012-03-31 06:15:48 +08:00
|
|
|
if (clang_Cursor_getObjCSelectorIndex(Cursor) != -1)
|
2014-05-08 05:05:22 +08:00
|
|
|
printf(" Selector index=%d",
|
|
|
|
clang_Cursor_getObjCSelectorIndex(Cursor));
|
2012-07-03 07:54:36 +08:00
|
|
|
if (clang_Cursor_isDynamicCall(Cursor))
|
|
|
|
printf(" Dynamic-call");
|
2012-11-01 10:01:34 +08:00
|
|
|
if (Cursor.kind == CXCursor_ObjCMessageExpr) {
|
2013-07-23 04:58:30 +08:00
|
|
|
CXType T = clang_Cursor_getReceiverType(Cursor);
|
|
|
|
CXString S = clang_getTypeKindSpelling(T.kind);
|
2012-11-01 10:01:34 +08:00
|
|
|
printf(" Receiver-type=%s", clang_getCString(S));
|
|
|
|
clang_disposeString(S);
|
|
|
|
}
|
2012-07-03 07:54:36 +08:00
|
|
|
|
2012-10-05 08:22:37 +08:00
|
|
|
{
|
|
|
|
CXModule mod = clang_Cursor_getModule(Cursor);
|
2013-04-27 06:47:49 +08:00
|
|
|
CXFile astFile;
|
|
|
|
CXString name, astFilename;
|
2012-10-05 08:22:37 +08:00
|
|
|
unsigned i, numHeaders;
|
|
|
|
if (mod) {
|
2013-04-27 06:47:49 +08:00
|
|
|
astFile = clang_Module_getASTFile(mod);
|
|
|
|
astFilename = clang_getFileName(astFile);
|
2012-10-05 08:22:37 +08:00
|
|
|
name = clang_Module_getFullName(mod);
|
2013-03-14 05:13:43 +08:00
|
|
|
numHeaders = clang_Module_getNumTopLevelHeaders(TU, mod);
|
2014-05-15 12:44:25 +08:00
|
|
|
printf(" ModuleName=%s (%s) system=%d Headers(%d):",
|
2013-04-27 06:47:49 +08:00
|
|
|
clang_getCString(name), clang_getCString(astFilename),
|
2014-05-15 12:44:25 +08:00
|
|
|
clang_Module_isSystem(mod), numHeaders);
|
2012-10-05 08:22:37 +08:00
|
|
|
clang_disposeString(name);
|
2013-04-27 06:47:49 +08:00
|
|
|
clang_disposeString(astFilename);
|
2012-10-05 08:22:37 +08:00
|
|
|
for (i = 0; i < numHeaders; ++i) {
|
2013-07-23 04:58:30 +08:00
|
|
|
CXFile file = clang_Module_getTopLevelHeader(TU, mod, i);
|
|
|
|
CXString filename = clang_getFileName(file);
|
2012-10-05 08:22:37 +08:00
|
|
|
printf("\n%s", clang_getCString(filename));
|
|
|
|
clang_disposeString(filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-05 04:04:59 +08:00
|
|
|
if (completionString != NULL) {
|
|
|
|
printf("\nCompletion string: ");
|
|
|
|
print_completion_string(completionString, stdout);
|
|
|
|
}
|
2010-11-30 13:52:55 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-10-06 15:00:54 +08:00
|
|
|
static enum CXVisitorResult findFileRefsVisit(void *context,
|
|
|
|
CXCursor cursor, CXSourceRange range) {
|
|
|
|
if (clang_Range_isNull(range))
|
|
|
|
return CXVisit_Continue;
|
|
|
|
|
2012-08-08 01:54:38 +08:00
|
|
|
PrintCursor(cursor, NULL);
|
2011-10-06 15:00:54 +08:00
|
|
|
PrintRange(range, "");
|
|
|
|
printf("\n");
|
|
|
|
return CXVisit_Continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int find_file_refs_at(int argc, const char **argv) {
|
|
|
|
CXIndex CIdx;
|
|
|
|
int errorCode;
|
|
|
|
struct CXUnsavedFile *unsaved_files = 0;
|
|
|
|
int num_unsaved_files = 0;
|
2014-02-13 03:12:37 +08:00
|
|
|
enum CXErrorCode Err;
|
2011-10-06 15:00:54 +08:00
|
|
|
CXTranslationUnit TU;
|
|
|
|
CXCursor Cursor;
|
|
|
|
CursorSourceLocation *Locations = 0;
|
|
|
|
unsigned NumLocations = 0, Loc;
|
|
|
|
unsigned Repeats = 1;
|
|
|
|
unsigned I;
|
|
|
|
|
|
|
|
/* Count the number of locations. */
|
|
|
|
while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1])
|
|
|
|
++NumLocations;
|
|
|
|
|
|
|
|
/* 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("-file-refs-at=");
|
|
|
|
if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
|
|
|
|
&Locations[Loc].line,
|
|
|
|
&Locations[Loc].column, 0, 0)))
|
|
|
|
return errorCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
|
|
|
|
&num_unsaved_files))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
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);
|
2014-02-13 03:12:37 +08:00
|
|
|
Err = clang_parseTranslationUnit2(CIdx, argv[argc - 1],
|
|
|
|
argv + num_unsaved_files + 1 + NumLocations,
|
|
|
|
argc - num_unsaved_files - 2 - NumLocations,
|
|
|
|
unsaved_files,
|
|
|
|
Repeats > 1? 0 : num_unsaved_files,
|
|
|
|
getDefaultParsingOptions(), &TU);
|
|
|
|
if (Err != CXError_Success) {
|
2011-10-06 15:00:54 +08:00
|
|
|
fprintf(stderr, "unable to parse input\n");
|
2014-02-13 03:12:37 +08:00
|
|
|
describeLibclangFailure(Err);
|
|
|
|
clang_disposeTranslationUnit(TU);
|
2011-10-06 15:00:54 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-10-29 06:54:36 +08:00
|
|
|
if (checkForErrors(TU) != 0)
|
|
|
|
return -1;
|
|
|
|
|
2011-10-06 15:00:54 +08:00
|
|
|
for (I = 0; I != Repeats; ++I) {
|
2014-02-13 03:12:37 +08:00
|
|
|
if (Repeats > 1) {
|
|
|
|
Err = clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
|
|
|
|
clang_defaultReparseOptions(TU));
|
|
|
|
if (Err != CXError_Success) {
|
|
|
|
describeLibclangFailure(Err);
|
|
|
|
clang_disposeTranslationUnit(TU);
|
|
|
|
return 1;
|
|
|
|
}
|
2011-10-06 15:00:54 +08:00
|
|
|
}
|
2011-10-29 06:54:36 +08:00
|
|
|
|
|
|
|
if (checkForErrors(TU) != 0)
|
|
|
|
return -1;
|
2011-10-06 15:00:54 +08:00
|
|
|
|
|
|
|
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));
|
2011-10-29 06:54:36 +08:00
|
|
|
|
|
|
|
if (checkForErrors(TU) != 0)
|
|
|
|
return -1;
|
|
|
|
|
2011-10-06 15:00:54 +08:00
|
|
|
if (I + 1 == Repeats) {
|
2011-10-06 19:38:08 +08:00
|
|
|
CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit };
|
2012-08-08 01:54:38 +08:00
|
|
|
PrintCursor(Cursor, NULL);
|
2011-10-06 15:00:54 +08:00
|
|
|
printf("\n");
|
|
|
|
clang_findReferencesInFile(Cursor, file, visitor);
|
|
|
|
free(Locations[Loc].filename);
|
2011-10-29 06:54:36 +08:00
|
|
|
|
|
|
|
if (checkForErrors(TU) != 0)
|
|
|
|
return -1;
|
2011-10-06 15:00:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PrintDiagnostics(TU);
|
|
|
|
clang_disposeTranslationUnit(TU);
|
|
|
|
clang_disposeIndex(CIdx);
|
|
|
|
free(Locations);
|
|
|
|
free_remapped_files(unsaved_files, num_unsaved_files);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-03-08 10:32:34 +08:00
|
|
|
static enum CXVisitorResult findFileIncludesVisit(void *context,
|
|
|
|
CXCursor cursor, CXSourceRange range) {
|
|
|
|
PrintCursor(cursor, NULL);
|
|
|
|
PrintRange(range, "");
|
|
|
|
printf("\n");
|
|
|
|
return CXVisit_Continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int find_file_includes_in(int argc, const char **argv) {
|
|
|
|
CXIndex CIdx;
|
|
|
|
struct CXUnsavedFile *unsaved_files = 0;
|
|
|
|
int num_unsaved_files = 0;
|
2014-02-13 03:12:37 +08:00
|
|
|
enum CXErrorCode Err;
|
2013-03-08 10:32:34 +08:00
|
|
|
CXTranslationUnit TU;
|
|
|
|
const char **Filenames = 0;
|
|
|
|
unsigned NumFilenames = 0;
|
|
|
|
unsigned Repeats = 1;
|
|
|
|
unsigned I, FI;
|
|
|
|
|
|
|
|
/* Count the number of locations. */
|
|
|
|
while (strstr(argv[NumFilenames+1], "-file-includes-in=") == argv[NumFilenames+1])
|
|
|
|
++NumFilenames;
|
|
|
|
|
|
|
|
/* Parse the locations. */
|
|
|
|
assert(NumFilenames > 0 && "Unable to count filenames?");
|
|
|
|
Filenames = (const char **)malloc(NumFilenames * sizeof(const char *));
|
|
|
|
for (I = 0; I < NumFilenames; ++I) {
|
|
|
|
const char *input = argv[I + 1] + strlen("-file-includes-in=");
|
|
|
|
/* Copy the file name. */
|
|
|
|
Filenames[I] = input;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parse_remapped_files(argc, argv, NumFilenames + 1, &unsaved_files,
|
|
|
|
&num_unsaved_files))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (getenv("CINDEXTEST_EDITING"))
|
|
|
|
Repeats = 2;
|
|
|
|
|
|
|
|
/* 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);
|
2014-02-13 03:12:37 +08:00
|
|
|
Err = clang_parseTranslationUnit2(
|
|
|
|
CIdx, argv[argc - 1],
|
|
|
|
argv + num_unsaved_files + 1 + NumFilenames,
|
|
|
|
argc - num_unsaved_files - 2 - NumFilenames,
|
|
|
|
unsaved_files,
|
|
|
|
Repeats > 1 ? 0 : num_unsaved_files, getDefaultParsingOptions(), &TU);
|
|
|
|
|
|
|
|
if (Err != CXError_Success) {
|
2013-03-08 10:32:34 +08:00
|
|
|
fprintf(stderr, "unable to parse input\n");
|
2014-02-13 03:12:37 +08:00
|
|
|
describeLibclangFailure(Err);
|
|
|
|
clang_disposeTranslationUnit(TU);
|
2013-03-08 10:32:34 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (checkForErrors(TU) != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (I = 0; I != Repeats; ++I) {
|
2014-02-13 03:12:37 +08:00
|
|
|
if (Repeats > 1) {
|
|
|
|
Err = clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
|
|
|
|
clang_defaultReparseOptions(TU));
|
|
|
|
if (Err != CXError_Success) {
|
|
|
|
describeLibclangFailure(Err);
|
|
|
|
clang_disposeTranslationUnit(TU);
|
|
|
|
return 1;
|
|
|
|
}
|
2013-03-08 10:32:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (checkForErrors(TU) != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (FI = 0; FI < NumFilenames; ++FI) {
|
|
|
|
CXFile file = clang_getFile(TU, Filenames[FI]);
|
|
|
|
if (!file)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (checkForErrors(TU) != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (I + 1 == Repeats) {
|
|
|
|
CXCursorAndRangeVisitor visitor = { 0, findFileIncludesVisit };
|
|
|
|
clang_findIncludesInFile(TU, file, visitor);
|
|
|
|
|
|
|
|
if (checkForErrors(TU) != 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PrintDiagnostics(TU);
|
|
|
|
clang_disposeTranslationUnit(TU);
|
|
|
|
clang_disposeIndex(CIdx);
|
2013-03-12 00:03:17 +08:00
|
|
|
free((void *)Filenames);
|
2013-03-08 10:32:34 +08:00
|
|
|
free_remapped_files(unsaved_files, num_unsaved_files);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-25 02:29:15 +08:00
|
|
|
#define MAX_IMPORTED_ASTFILES 200
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
char **filenames;
|
|
|
|
unsigned num_files;
|
|
|
|
} ImportedASTFilesData;
|
|
|
|
|
|
|
|
static ImportedASTFilesData *importedASTs_create() {
|
|
|
|
ImportedASTFilesData *p;
|
|
|
|
p = malloc(sizeof(ImportedASTFilesData));
|
|
|
|
p->filenames = malloc(MAX_IMPORTED_ASTFILES * sizeof(const char *));
|
|
|
|
p->num_files = 0;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void importedASTs_dispose(ImportedASTFilesData *p) {
|
|
|
|
unsigned i;
|
|
|
|
if (!p)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < p->num_files; ++i)
|
|
|
|
free(p->filenames[i]);
|
|
|
|
free(p->filenames);
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void importedASTS_insert(ImportedASTFilesData *p, const char *file) {
|
|
|
|
unsigned i;
|
|
|
|
assert(p && file);
|
|
|
|
for (i = 0; i < p->num_files; ++i)
|
|
|
|
if (strcmp(file, p->filenames[i]) == 0)
|
|
|
|
return;
|
|
|
|
assert(p->num_files + 1 < MAX_IMPORTED_ASTFILES);
|
|
|
|
p->filenames[p->num_files++] = strdup(file);
|
|
|
|
}
|
|
|
|
|
2014-05-08 05:09:42 +08:00
|
|
|
typedef struct IndexDataStringList_ {
|
|
|
|
struct IndexDataStringList_ *next;
|
|
|
|
char data[1]; /* Dynamically sized. */
|
|
|
|
} IndexDataStringList;
|
|
|
|
|
2011-10-18 03:48:19 +08:00
|
|
|
typedef struct {
|
|
|
|
const char *check_prefix;
|
|
|
|
int first_check_printed;
|
2011-10-29 06:54:36 +08:00
|
|
|
int fail_for_error;
|
2011-11-28 12:56:00 +08:00
|
|
|
int abort;
|
2012-03-16 02:07:22 +08:00
|
|
|
const char *main_filename;
|
2012-10-25 02:29:15 +08:00
|
|
|
ImportedASTFilesData *importedASTs;
|
2014-05-08 05:09:42 +08:00
|
|
|
IndexDataStringList *strings;
|
2014-05-15 07:14:37 +08:00
|
|
|
CXTranslationUnit TU;
|
2011-10-18 03:48:19 +08:00
|
|
|
} IndexData;
|
|
|
|
|
2014-05-08 05:09:42 +08:00
|
|
|
static void free_client_data(IndexData *index_data) {
|
|
|
|
IndexDataStringList *node = index_data->strings;
|
|
|
|
while (node) {
|
|
|
|
IndexDataStringList *next = node->next;
|
|
|
|
free(node);
|
|
|
|
node = next;
|
|
|
|
}
|
|
|
|
index_data->strings = NULL;
|
|
|
|
}
|
|
|
|
|
2011-10-18 03:48:19 +08:00
|
|
|
static void printCheck(IndexData *data) {
|
|
|
|
if (data->check_prefix) {
|
|
|
|
if (data->first_check_printed) {
|
|
|
|
printf("// %s-NEXT: ", data->check_prefix);
|
|
|
|
} else {
|
|
|
|
printf("// %s : ", data->check_prefix);
|
|
|
|
data->first_check_printed = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-11 08:23:36 +08:00
|
|
|
static void printCXIndexFile(CXIdxClientFile file) {
|
2013-07-23 04:58:30 +08:00
|
|
|
CXString filename = clang_getFileName((CXFile)file);
|
2011-10-18 03:48:19 +08:00
|
|
|
printf("%s", clang_getCString(filename));
|
|
|
|
clang_disposeString(filename);
|
|
|
|
}
|
|
|
|
|
2012-03-16 02:07:22 +08:00
|
|
|
static void printCXIndexLoc(CXIdxLoc loc, CXClientData client_data) {
|
|
|
|
IndexData *index_data;
|
2011-10-18 03:48:19 +08:00
|
|
|
CXString filename;
|
2012-03-16 02:07:22 +08:00
|
|
|
const char *cname;
|
2011-11-11 08:23:36 +08:00
|
|
|
CXIdxClientFile file;
|
2011-10-18 03:48:19 +08:00
|
|
|
unsigned line, column;
|
2012-03-16 02:07:22 +08:00
|
|
|
int isMainFile;
|
2011-10-18 03:48:19 +08:00
|
|
|
|
2012-03-16 02:07:22 +08:00
|
|
|
index_data = (IndexData *)client_data;
|
2011-10-18 03:48:19 +08:00
|
|
|
clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
|
|
|
|
if (line == 0) {
|
2012-10-12 03:00:44 +08:00
|
|
|
printf("<invalid>");
|
2011-10-18 03:48:19 +08:00
|
|
|
return;
|
|
|
|
}
|
2011-12-14 02:47:35 +08:00
|
|
|
if (!file) {
|
|
|
|
printf("<no idxfile>");
|
|
|
|
return;
|
|
|
|
}
|
2011-10-18 03:48:19 +08:00
|
|
|
filename = clang_getFileName((CXFile)file);
|
|
|
|
cname = clang_getCString(filename);
|
2012-03-16 02:07:22 +08:00
|
|
|
if (strcmp(cname, index_data->main_filename) == 0)
|
|
|
|
isMainFile = 1;
|
|
|
|
else
|
|
|
|
isMainFile = 0;
|
|
|
|
clang_disposeString(filename);
|
|
|
|
|
|
|
|
if (!isMainFile) {
|
2011-10-18 03:48:19 +08:00
|
|
|
printCXIndexFile(file);
|
|
|
|
printf(":");
|
|
|
|
}
|
|
|
|
printf("%d:%d", line, column);
|
|
|
|
}
|
|
|
|
|
2012-03-16 02:07:22 +08:00
|
|
|
static unsigned digitCount(unsigned val) {
|
|
|
|
unsigned c = 1;
|
|
|
|
while (1) {
|
|
|
|
if (val < 10)
|
|
|
|
return c;
|
|
|
|
++c;
|
|
|
|
val /= 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-08 05:09:42 +08:00
|
|
|
static CXIdxClientContainer makeClientContainer(CXClientData *client_data,
|
|
|
|
const CXIdxEntityInfo *info,
|
2011-11-12 10:16:30 +08:00
|
|
|
CXIdxLoc loc) {
|
2014-05-08 05:09:42 +08:00
|
|
|
IndexData *index_data;
|
|
|
|
IndexDataStringList *node;
|
2011-10-18 03:48:19 +08:00
|
|
|
const char *name;
|
|
|
|
char *newStr;
|
2011-11-11 08:23:36 +08:00
|
|
|
CXIdxClientFile file;
|
2011-10-18 03:48:19 +08:00
|
|
|
unsigned line, column;
|
|
|
|
|
2011-11-11 08:23:36 +08:00
|
|
|
name = info->name;
|
2011-10-18 03:48:19 +08:00
|
|
|
if (!name)
|
|
|
|
name = "<anon-tag>";
|
|
|
|
|
|
|
|
clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
|
2014-05-08 05:09:42 +08:00
|
|
|
|
|
|
|
node =
|
|
|
|
(IndexDataStringList *)malloc(sizeof(IndexDataStringList) + strlen(name) +
|
|
|
|
digitCount(line) + digitCount(column) + 2);
|
|
|
|
newStr = node->data;
|
2011-10-18 03:48:19 +08:00
|
|
|
sprintf(newStr, "%s:%d:%d", name, line, column);
|
2014-05-08 05:09:42 +08:00
|
|
|
|
|
|
|
/* Remember string so it can be freed later. */
|
|
|
|
index_data = (IndexData *)client_data;
|
|
|
|
node->next = index_data->strings;
|
|
|
|
index_data->strings = node;
|
|
|
|
|
2011-11-12 10:16:30 +08:00
|
|
|
return (CXIdxClientContainer)newStr;
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
2011-11-22 15:24:51 +08:00
|
|
|
static void printCXIndexContainer(const CXIdxContainerInfo *info) {
|
|
|
|
CXIdxClientContainer container;
|
|
|
|
container = clang_index_getClientContainer(info);
|
2011-11-16 10:35:05 +08:00
|
|
|
if (!container)
|
|
|
|
printf("[<<NULL>>]");
|
|
|
|
else
|
|
|
|
printf("[%s]", (const char *)container);
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
2011-11-11 08:23:36 +08:00
|
|
|
static const char *getEntityKindString(CXIdxEntityKind kind) {
|
|
|
|
switch (kind) {
|
|
|
|
case CXIdxEntity_Unexposed: return "<<UNEXPOSED>>";
|
|
|
|
case CXIdxEntity_Typedef: return "typedef";
|
|
|
|
case CXIdxEntity_Function: return "function";
|
|
|
|
case CXIdxEntity_Variable: return "variable";
|
|
|
|
case CXIdxEntity_Field: return "field";
|
|
|
|
case CXIdxEntity_EnumConstant: return "enumerator";
|
|
|
|
case CXIdxEntity_ObjCClass: return "objc-class";
|
|
|
|
case CXIdxEntity_ObjCProtocol: return "objc-protocol";
|
|
|
|
case CXIdxEntity_ObjCCategory: return "objc-category";
|
2011-11-15 06:39:19 +08:00
|
|
|
case CXIdxEntity_ObjCInstanceMethod: return "objc-instance-method";
|
|
|
|
case CXIdxEntity_ObjCClassMethod: return "objc-class-method";
|
2011-11-11 08:23:36 +08:00
|
|
|
case CXIdxEntity_ObjCProperty: return "objc-property";
|
|
|
|
case CXIdxEntity_ObjCIvar: return "objc-ivar";
|
|
|
|
case CXIdxEntity_Enum: return "enum";
|
|
|
|
case CXIdxEntity_Struct: return "struct";
|
|
|
|
case CXIdxEntity_Union: return "union";
|
|
|
|
case CXIdxEntity_CXXClass: return "c++-class";
|
2011-11-22 15:24:51 +08:00
|
|
|
case CXIdxEntity_CXXNamespace: return "namespace";
|
|
|
|
case CXIdxEntity_CXXNamespaceAlias: return "namespace-alias";
|
|
|
|
case CXIdxEntity_CXXStaticVariable: return "c++-static-var";
|
|
|
|
case CXIdxEntity_CXXStaticMethod: return "c++-static-method";
|
|
|
|
case CXIdxEntity_CXXInstanceMethod: return "c++-instance-method";
|
|
|
|
case CXIdxEntity_CXXConstructor: return "constructor";
|
|
|
|
case CXIdxEntity_CXXDestructor: return "destructor";
|
|
|
|
case CXIdxEntity_CXXConversionFunction: return "conversion-func";
|
|
|
|
case CXIdxEntity_CXXTypeAlias: return "type-alias";
|
2012-09-01 05:55:26 +08:00
|
|
|
case CXIdxEntity_CXXInterface: return "c++-__interface";
|
2011-11-22 15:24:51 +08:00
|
|
|
}
|
|
|
|
assert(0 && "Garbage entity kind");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *getEntityTemplateKindString(CXIdxEntityCXXTemplateKind kind) {
|
|
|
|
switch (kind) {
|
|
|
|
case CXIdxEntity_NonTemplate: return "";
|
|
|
|
case CXIdxEntity_Template: return "-template";
|
|
|
|
case CXIdxEntity_TemplatePartialSpecialization:
|
|
|
|
return "-template-partial-spec";
|
|
|
|
case CXIdxEntity_TemplateSpecialization: return "-template-spec";
|
2011-11-11 08:23:36 +08:00
|
|
|
}
|
2011-11-12 10:16:30 +08:00
|
|
|
assert(0 && "Garbage entity kind");
|
|
|
|
return 0;
|
2011-11-11 08:23:36 +08:00
|
|
|
}
|
|
|
|
|
2011-12-08 04:44:12 +08:00
|
|
|
static const char *getEntityLanguageString(CXIdxEntityLanguage kind) {
|
|
|
|
switch (kind) {
|
|
|
|
case CXIdxEntityLang_None: return "<none>";
|
|
|
|
case CXIdxEntityLang_C: return "C";
|
|
|
|
case CXIdxEntityLang_ObjC: return "ObjC";
|
|
|
|
case CXIdxEntityLang_CXX: return "C++";
|
|
|
|
}
|
|
|
|
assert(0 && "Garbage language kind");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-11 08:23:36 +08:00
|
|
|
static void printEntityInfo(const char *cb,
|
|
|
|
CXClientData client_data,
|
2011-11-12 10:16:30 +08:00
|
|
|
const CXIdxEntityInfo *info) {
|
2011-10-18 03:48:19 +08:00
|
|
|
const char *name;
|
|
|
|
IndexData *index_data;
|
2011-12-15 08:05:00 +08:00
|
|
|
unsigned i;
|
2011-10-18 03:48:19 +08:00
|
|
|
index_data = (IndexData *)client_data;
|
|
|
|
printCheck(index_data);
|
|
|
|
|
2011-11-16 10:34:59 +08:00
|
|
|
if (!info) {
|
|
|
|
printf("%s: <<NULL>>", cb);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-11-11 08:23:36 +08:00
|
|
|
name = info->name;
|
2011-10-18 03:48:19 +08:00
|
|
|
if (!name)
|
|
|
|
name = "<anon-tag>";
|
|
|
|
|
2011-11-22 15:24:51 +08:00
|
|
|
printf("%s: kind: %s%s", cb, getEntityKindString(info->kind),
|
|
|
|
getEntityTemplateKindString(info->templateKind));
|
2011-11-11 08:23:36 +08:00
|
|
|
printf(" | name: %s", name);
|
|
|
|
printf(" | USR: %s", info->USR);
|
2011-12-14 02:47:35 +08:00
|
|
|
printf(" | lang: %s", getEntityLanguageString(info->lang));
|
2011-12-15 08:05:00 +08:00
|
|
|
|
|
|
|
for (i = 0; i != info->numAttributes; ++i) {
|
|
|
|
const CXIdxAttrInfo *Attr = info->attributes[i];
|
|
|
|
printf(" <attribute>: ");
|
2012-08-08 01:54:38 +08:00
|
|
|
PrintCursor(Attr->cursor, NULL);
|
2011-12-15 08:05:00 +08:00
|
|
|
}
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
2011-12-08 04:44:15 +08:00
|
|
|
static void printBaseClassInfo(CXClientData client_data,
|
|
|
|
const CXIdxBaseClassInfo *info) {
|
|
|
|
printEntityInfo(" <base>", client_data, info->base);
|
|
|
|
printf(" | cursor: ");
|
2012-08-08 01:54:38 +08:00
|
|
|
PrintCursor(info->cursor, NULL);
|
2011-12-08 04:44:15 +08:00
|
|
|
printf(" | loc: ");
|
2012-03-16 02:07:22 +08:00
|
|
|
printCXIndexLoc(info->loc, client_data);
|
2011-12-08 04:44:15 +08:00
|
|
|
}
|
|
|
|
|
2011-11-15 06:39:19 +08:00
|
|
|
static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo,
|
|
|
|
CXClientData client_data) {
|
|
|
|
unsigned i;
|
|
|
|
for (i = 0; i < ProtoInfo->numProtocols; ++i) {
|
|
|
|
printEntityInfo(" <protocol>", client_data,
|
|
|
|
ProtoInfo->protocols[i]->protocol);
|
|
|
|
printf(" | cursor: ");
|
2012-08-08 01:54:38 +08:00
|
|
|
PrintCursor(ProtoInfo->protocols[i]->cursor, NULL);
|
2011-11-15 06:39:19 +08:00
|
|
|
printf(" | loc: ");
|
2012-03-16 02:07:22 +08:00
|
|
|
printCXIndexLoc(ProtoInfo->protocols[i]->loc, client_data);
|
2011-11-15 06:39:19 +08:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-18 03:48:19 +08:00
|
|
|
static void index_diagnostic(CXClientData client_data,
|
2011-12-01 10:42:50 +08:00
|
|
|
CXDiagnosticSet diagSet, void *reserved) {
|
2011-10-18 03:48:19 +08:00
|
|
|
CXString str;
|
|
|
|
const char *cstr;
|
2011-12-01 10:42:50 +08:00
|
|
|
unsigned numDiags, i;
|
|
|
|
CXDiagnostic diag;
|
2011-10-18 03:48:19 +08:00
|
|
|
IndexData *index_data;
|
|
|
|
index_data = (IndexData *)client_data;
|
|
|
|
printCheck(index_data);
|
|
|
|
|
2011-12-01 10:42:50 +08:00
|
|
|
numDiags = clang_getNumDiagnosticsInSet(diagSet);
|
|
|
|
for (i = 0; i != numDiags; ++i) {
|
|
|
|
diag = clang_getDiagnosticInSet(diagSet, i);
|
|
|
|
str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions());
|
|
|
|
cstr = clang_getCString(str);
|
|
|
|
printf("[diagnostic]: %s\n", cstr);
|
|
|
|
clang_disposeString(str);
|
|
|
|
|
|
|
|
if (getenv("CINDEXTEST_FAILONERROR") &&
|
|
|
|
clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) {
|
|
|
|
index_data->fail_for_error = 1;
|
|
|
|
}
|
2011-10-29 06:54:36 +08:00
|
|
|
}
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
2011-11-11 08:23:36 +08:00
|
|
|
static CXIdxClientFile index_enteredMainFile(CXClientData client_data,
|
|
|
|
CXFile file, void *reserved) {
|
|
|
|
IndexData *index_data;
|
2012-03-16 02:48:52 +08:00
|
|
|
CXString filename;
|
|
|
|
|
2011-11-11 08:23:36 +08:00
|
|
|
index_data = (IndexData *)client_data;
|
|
|
|
printCheck(index_data);
|
|
|
|
|
2012-03-16 02:48:52 +08:00
|
|
|
filename = clang_getFileName(file);
|
2012-03-16 02:07:22 +08:00
|
|
|
index_data->main_filename = clang_getCString(filename);
|
|
|
|
clang_disposeString(filename);
|
|
|
|
|
2011-11-11 08:23:36 +08:00
|
|
|
printf("[enteredMainFile]: ");
|
|
|
|
printCXIndexFile((CXIdxClientFile)file);
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
return (CXIdxClientFile)file;
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
2011-11-11 08:23:36 +08:00
|
|
|
static CXIdxClientFile index_ppIncludedFile(CXClientData client_data,
|
2011-11-12 10:16:30 +08:00
|
|
|
const CXIdxIncludedFileInfo *info) {
|
2011-10-18 03:48:19 +08:00
|
|
|
IndexData *index_data;
|
2014-05-15 07:14:37 +08:00
|
|
|
CXModule Mod;
|
2011-10-18 03:48:19 +08:00
|
|
|
index_data = (IndexData *)client_data;
|
|
|
|
printCheck(index_data);
|
|
|
|
|
2011-11-05 12:03:35 +08:00
|
|
|
printf("[ppIncludedFile]: ");
|
2011-11-11 08:23:36 +08:00
|
|
|
printCXIndexFile((CXIdxClientFile)info->file);
|
2011-10-18 03:48:19 +08:00
|
|
|
printf(" | name: \"%s\"", info->filename);
|
|
|
|
printf(" | hash loc: ");
|
2012-03-16 02:07:22 +08:00
|
|
|
printCXIndexLoc(info->hashLoc, client_data);
|
2014-05-15 07:14:37 +08:00
|
|
|
printf(" | isImport: %d | isAngled: %d | isModule: %d",
|
2012-10-18 08:17:05 +08:00
|
|
|
info->isImport, info->isAngled, info->isModuleImport);
|
2014-05-15 07:14:37 +08:00
|
|
|
|
|
|
|
Mod = clang_getModuleForFile(index_data->TU, (CXFile)info->file);
|
|
|
|
if (Mod) {
|
|
|
|
CXString str = clang_Module_getFullName(Mod);
|
|
|
|
const char *cstr = clang_getCString(str);
|
|
|
|
printf(" | module: %s", cstr);
|
|
|
|
clang_disposeString(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
2011-11-11 08:23:36 +08:00
|
|
|
|
|
|
|
return (CXIdxClientFile)info->file;
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
2012-10-03 00:10:38 +08:00
|
|
|
static CXIdxClientFile index_importedASTFile(CXClientData client_data,
|
|
|
|
const CXIdxImportedASTFileInfo *info) {
|
|
|
|
IndexData *index_data;
|
|
|
|
index_data = (IndexData *)client_data;
|
|
|
|
printCheck(index_data);
|
|
|
|
|
2012-10-25 02:29:15 +08:00
|
|
|
if (index_data->importedASTs) {
|
2013-07-23 04:58:30 +08:00
|
|
|
CXString filename = clang_getFileName(info->file);
|
2012-10-25 02:29:15 +08:00
|
|
|
importedASTS_insert(index_data->importedASTs, clang_getCString(filename));
|
|
|
|
clang_disposeString(filename);
|
|
|
|
}
|
|
|
|
|
2012-10-03 00:10:38 +08:00
|
|
|
printf("[importedASTFile]: ");
|
|
|
|
printCXIndexFile((CXIdxClientFile)info->file);
|
2012-10-05 08:22:40 +08:00
|
|
|
if (info->module) {
|
2013-07-23 04:58:30 +08:00
|
|
|
CXString name = clang_Module_getFullName(info->module);
|
2012-10-05 08:22:40 +08:00
|
|
|
printf(" | loc: ");
|
|
|
|
printCXIndexLoc(info->loc, client_data);
|
|
|
|
printf(" | name: \"%s\"", clang_getCString(name));
|
|
|
|
printf(" | isImplicit: %d\n", info->isImplicit);
|
|
|
|
clang_disposeString(name);
|
2012-10-12 00:05:00 +08:00
|
|
|
} else {
|
2012-10-12 22:25:52 +08:00
|
|
|
/* PCH file, the rest are not relevant. */
|
2012-10-12 00:05:00 +08:00
|
|
|
printf("\n");
|
2012-10-05 08:22:40 +08:00
|
|
|
}
|
2012-10-03 00:10:38 +08:00
|
|
|
|
|
|
|
return (CXIdxClientFile)info->file;
|
|
|
|
}
|
|
|
|
|
2014-05-08 05:05:22 +08:00
|
|
|
static CXIdxClientContainer
|
|
|
|
index_startedTranslationUnit(CXClientData client_data, void *reserved) {
|
2011-10-18 03:48:19 +08:00
|
|
|
IndexData *index_data;
|
|
|
|
index_data = (IndexData *)client_data;
|
|
|
|
printCheck(index_data);
|
|
|
|
|
2011-11-05 12:03:35 +08:00
|
|
|
printf("[startedTranslationUnit]\n");
|
2011-11-11 08:23:36 +08:00
|
|
|
return (CXIdxClientContainer)"TU";
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
2011-11-12 10:16:30 +08:00
|
|
|
static void index_indexDeclaration(CXClientData client_data,
|
2011-11-22 15:24:51 +08:00
|
|
|
const CXIdxDeclInfo *info) {
|
2011-11-11 08:23:36 +08:00
|
|
|
IndexData *index_data;
|
2011-11-12 10:16:30 +08:00
|
|
|
const CXIdxObjCCategoryDeclInfo *CatInfo;
|
|
|
|
const CXIdxObjCInterfaceDeclInfo *InterInfo;
|
2011-11-15 06:39:19 +08:00
|
|
|
const CXIdxObjCProtocolRefListInfo *ProtoInfo;
|
2012-02-29 01:50:33 +08:00
|
|
|
const CXIdxObjCPropertyDeclInfo *PropInfo;
|
2011-12-08 04:44:15 +08:00
|
|
|
const CXIdxCXXClassDeclInfo *CXXClassInfo;
|
2011-11-18 08:26:51 +08:00
|
|
|
unsigned i;
|
2011-11-11 08:23:36 +08:00
|
|
|
index_data = (IndexData *)client_data;
|
2011-10-18 03:48:19 +08:00
|
|
|
|
2011-11-11 08:23:36 +08:00
|
|
|
printEntityInfo("[indexDeclaration]", client_data, info->entityInfo);
|
|
|
|
printf(" | cursor: ");
|
2012-08-08 01:54:38 +08:00
|
|
|
PrintCursor(info->cursor, NULL);
|
2011-11-11 08:23:36 +08:00
|
|
|
printf(" | loc: ");
|
2012-03-16 02:07:22 +08:00
|
|
|
printCXIndexLoc(info->loc, client_data);
|
2011-12-08 04:44:19 +08:00
|
|
|
printf(" | semantic-container: ");
|
|
|
|
printCXIndexContainer(info->semanticContainer);
|
|
|
|
printf(" | lexical-container: ");
|
|
|
|
printCXIndexContainer(info->lexicalContainer);
|
2011-11-11 08:23:36 +08:00
|
|
|
printf(" | isRedecl: %d", info->isRedeclaration);
|
2011-11-15 06:39:19 +08:00
|
|
|
printf(" | isDef: %d", info->isDefinition);
|
2012-12-07 03:41:16 +08:00
|
|
|
if (info->flags & CXIdxDeclFlag_Skipped) {
|
|
|
|
assert(!info->isContainer);
|
|
|
|
printf(" | isContainer: skipped");
|
|
|
|
} else {
|
|
|
|
printf(" | isContainer: %d", info->isContainer);
|
|
|
|
}
|
2011-11-15 06:39:19 +08:00
|
|
|
printf(" | isImplicit: %d\n", info->isImplicit);
|
2011-10-18 03:48:19 +08:00
|
|
|
|
2011-11-18 08:26:51 +08:00
|
|
|
for (i = 0; i != info->numAttributes; ++i) {
|
2011-11-18 08:51:03 +08:00
|
|
|
const CXIdxAttrInfo *Attr = info->attributes[i];
|
2011-11-18 08:26:51 +08:00
|
|
|
printf(" <attribute>: ");
|
2012-08-08 01:54:38 +08:00
|
|
|
PrintCursor(Attr->cursor, NULL);
|
2011-11-18 08:26:51 +08:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2011-11-11 08:23:36 +08:00
|
|
|
if (clang_index_isEntityObjCContainerKind(info->entityInfo->kind)) {
|
|
|
|
const char *kindName = 0;
|
|
|
|
CXIdxObjCContainerKind K = clang_index_getObjCContainerDeclInfo(info)->kind;
|
|
|
|
switch (K) {
|
|
|
|
case CXIdxObjCContainer_ForwardRef:
|
|
|
|
kindName = "forward-ref"; break;
|
|
|
|
case CXIdxObjCContainer_Interface:
|
|
|
|
kindName = "interface"; break;
|
|
|
|
case CXIdxObjCContainer_Implementation:
|
|
|
|
kindName = "implementation"; break;
|
|
|
|
}
|
|
|
|
printCheck(index_data);
|
|
|
|
printf(" <ObjCContainerInfo>: kind: %s\n", kindName);
|
|
|
|
}
|
2011-10-18 03:48:19 +08:00
|
|
|
|
2011-11-12 10:16:30 +08:00
|
|
|
if ((CatInfo = clang_index_getObjCCategoryDeclInfo(info))) {
|
2011-11-11 08:23:36 +08:00
|
|
|
printEntityInfo(" <ObjCCategoryInfo>: class", client_data,
|
|
|
|
CatInfo->objcClass);
|
2011-11-15 14:20:16 +08:00
|
|
|
printf(" | cursor: ");
|
2012-08-08 01:54:38 +08:00
|
|
|
PrintCursor(CatInfo->classCursor, NULL);
|
2011-11-15 14:20:16 +08:00
|
|
|
printf(" | loc: ");
|
2012-03-16 02:07:22 +08:00
|
|
|
printCXIndexLoc(CatInfo->classLoc, client_data);
|
2011-11-11 08:23:36 +08:00
|
|
|
printf("\n");
|
|
|
|
}
|
2011-10-18 03:48:19 +08:00
|
|
|
|
2011-11-12 10:16:30 +08:00
|
|
|
if ((InterInfo = clang_index_getObjCInterfaceDeclInfo(info))) {
|
|
|
|
if (InterInfo->superInfo) {
|
2011-12-08 04:44:15 +08:00
|
|
|
printBaseClassInfo(client_data, InterInfo->superInfo);
|
2011-11-12 10:16:30 +08:00
|
|
|
printf("\n");
|
|
|
|
}
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
2011-11-15 06:39:19 +08:00
|
|
|
if ((ProtoInfo = clang_index_getObjCProtocolRefListInfo(info))) {
|
|
|
|
printProtocolList(ProtoInfo, client_data);
|
2011-11-12 10:16:30 +08:00
|
|
|
}
|
2011-10-18 03:48:19 +08:00
|
|
|
|
2012-02-29 01:50:33 +08:00
|
|
|
if ((PropInfo = clang_index_getObjCPropertyDeclInfo(info))) {
|
|
|
|
if (PropInfo->getter) {
|
|
|
|
printEntityInfo(" <getter>", client_data, PropInfo->getter);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
if (PropInfo->setter) {
|
|
|
|
printEntityInfo(" <setter>", client_data, PropInfo->setter);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-08 04:44:15 +08:00
|
|
|
if ((CXXClassInfo = clang_index_getCXXClassDeclInfo(info))) {
|
|
|
|
for (i = 0; i != CXXClassInfo->numBases; ++i) {
|
|
|
|
printBaseClassInfo(client_data, CXXClassInfo->bases[i]);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-22 15:24:51 +08:00
|
|
|
if (info->declAsContainer)
|
2014-05-08 05:05:22 +08:00
|
|
|
clang_index_setClientContainer(
|
|
|
|
info->declAsContainer,
|
2014-05-08 05:09:42 +08:00
|
|
|
makeClientContainer(client_data, info->entityInfo, info->loc));
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void index_indexEntityReference(CXClientData client_data,
|
2011-11-12 10:16:30 +08:00
|
|
|
const CXIdxEntityRefInfo *info) {
|
2014-05-08 05:05:22 +08:00
|
|
|
printEntityInfo("[indexEntityReference]", client_data,
|
|
|
|
info->referencedEntity);
|
2011-10-18 03:48:19 +08:00
|
|
|
printf(" | cursor: ");
|
2012-08-08 01:54:38 +08:00
|
|
|
PrintCursor(info->cursor, NULL);
|
2011-10-18 03:48:19 +08:00
|
|
|
printf(" | loc: ");
|
2012-03-16 02:07:22 +08:00
|
|
|
printCXIndexLoc(info->loc, client_data);
|
2011-11-11 08:23:36 +08:00
|
|
|
printEntityInfo(" | <parent>:", client_data, info->parentEntity);
|
2011-10-18 03:48:19 +08:00
|
|
|
printf(" | container: ");
|
|
|
|
printCXIndexContainer(info->container);
|
2011-11-15 06:39:19 +08:00
|
|
|
printf(" | refkind: ");
|
2011-10-18 23:50:50 +08:00
|
|
|
switch (info->kind) {
|
|
|
|
case CXIdxEntityRef_Direct: printf("direct"); break;
|
2011-11-18 08:26:51 +08:00
|
|
|
case CXIdxEntityRef_Implicit: printf("implicit"); break;
|
2011-10-18 23:50:50 +08:00
|
|
|
}
|
2011-10-18 03:48:19 +08:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2011-11-28 12:56:00 +08:00
|
|
|
static int index_abortQuery(CXClientData client_data, void *reserved) {
|
|
|
|
IndexData *index_data;
|
|
|
|
index_data = (IndexData *)client_data;
|
|
|
|
return index_data->abort;
|
|
|
|
}
|
|
|
|
|
2011-10-18 03:48:19 +08:00
|
|
|
static IndexerCallbacks IndexCB = {
|
2011-11-28 12:56:00 +08:00
|
|
|
index_abortQuery,
|
2011-10-18 03:48:19 +08:00
|
|
|
index_diagnostic,
|
2011-11-11 08:23:36 +08:00
|
|
|
index_enteredMainFile,
|
2011-10-18 03:48:19 +08:00
|
|
|
index_ppIncludedFile,
|
2012-10-03 00:10:38 +08:00
|
|
|
index_importedASTFile,
|
2011-10-18 03:48:19 +08:00
|
|
|
index_startedTranslationUnit,
|
2011-11-11 08:23:36 +08:00
|
|
|
index_indexDeclaration,
|
2011-10-18 03:48:19 +08:00
|
|
|
index_indexEntityReference
|
|
|
|
};
|
|
|
|
|
2012-01-14 08:11:49 +08:00
|
|
|
static unsigned getIndexOptions(void) {
|
|
|
|
unsigned index_opts;
|
|
|
|
index_opts = 0;
|
|
|
|
if (getenv("CINDEXTEST_SUPPRESSREFS"))
|
|
|
|
index_opts |= CXIndexOpt_SuppressRedundantRefs;
|
|
|
|
if (getenv("CINDEXTEST_INDEXLOCALSYMBOLS"))
|
|
|
|
index_opts |= CXIndexOpt_IndexFunctionLocalSymbols;
|
2012-12-07 03:41:16 +08:00
|
|
|
if (!getenv("CINDEXTEST_DISABLE_SKIPPARSEDBODIES"))
|
|
|
|
index_opts |= CXIndexOpt_SkipParsedBodiesInSession;
|
2012-01-14 08:11:49 +08:00
|
|
|
|
|
|
|
return index_opts;
|
|
|
|
}
|
|
|
|
|
2012-12-06 05:53:37 +08:00
|
|
|
static int index_compile_args(int num_args, const char **args,
|
|
|
|
CXIndexAction idxAction,
|
|
|
|
ImportedASTFilesData *importedASTs,
|
|
|
|
const char *check_prefix) {
|
|
|
|
IndexData index_data;
|
|
|
|
unsigned index_opts;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
if (num_args == 0) {
|
|
|
|
fprintf(stderr, "no compiler arguments\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
index_data.check_prefix = check_prefix;
|
|
|
|
index_data.first_check_printed = 0;
|
|
|
|
index_data.fail_for_error = 0;
|
|
|
|
index_data.abort = 0;
|
|
|
|
index_data.main_filename = "";
|
|
|
|
index_data.importedASTs = importedASTs;
|
2014-05-08 05:09:42 +08:00
|
|
|
index_data.strings = NULL;
|
2014-05-15 07:14:37 +08:00
|
|
|
index_data.TU = NULL;
|
2012-12-06 05:53:37 +08:00
|
|
|
|
|
|
|
index_opts = getIndexOptions();
|
|
|
|
result = clang_indexSourceFile(idxAction, &index_data,
|
|
|
|
&IndexCB,sizeof(IndexCB), index_opts,
|
|
|
|
0, args, num_args, 0, 0, 0,
|
|
|
|
getDefaultParsingOptions());
|
2014-02-13 03:12:37 +08:00
|
|
|
if (result != CXError_Success)
|
|
|
|
describeLibclangFailure(result);
|
|
|
|
|
2012-12-06 05:53:37 +08:00
|
|
|
if (index_data.fail_for_error)
|
|
|
|
result = -1;
|
|
|
|
|
2014-05-08 05:09:42 +08:00
|
|
|
free_client_data(&index_data);
|
2012-12-06 05:53:37 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int index_ast_file(const char *ast_file,
|
|
|
|
CXIndex Idx,
|
|
|
|
CXIndexAction idxAction,
|
|
|
|
ImportedASTFilesData *importedASTs,
|
|
|
|
const char *check_prefix) {
|
|
|
|
CXTranslationUnit TU;
|
|
|
|
IndexData index_data;
|
|
|
|
unsigned index_opts;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
if (!CreateTranslationUnit(Idx, ast_file, &TU))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
index_data.check_prefix = check_prefix;
|
|
|
|
index_data.first_check_printed = 0;
|
|
|
|
index_data.fail_for_error = 0;
|
|
|
|
index_data.abort = 0;
|
|
|
|
index_data.main_filename = "";
|
|
|
|
index_data.importedASTs = importedASTs;
|
2014-05-08 05:09:42 +08:00
|
|
|
index_data.strings = NULL;
|
2014-05-15 07:14:37 +08:00
|
|
|
index_data.TU = TU;
|
2012-12-06 05:53:37 +08:00
|
|
|
|
|
|
|
index_opts = getIndexOptions();
|
|
|
|
result = clang_indexTranslationUnit(idxAction, &index_data,
|
|
|
|
&IndexCB,sizeof(IndexCB),
|
|
|
|
index_opts, TU);
|
|
|
|
if (index_data.fail_for_error)
|
|
|
|
result = -1;
|
|
|
|
|
|
|
|
clang_disposeTranslationUnit(TU);
|
2014-05-08 05:09:42 +08:00
|
|
|
free_client_data(&index_data);
|
2012-12-06 05:53:37 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-10-25 02:29:15 +08:00
|
|
|
static int index_file(int argc, const char **argv, int full) {
|
2011-10-18 03:48:19 +08:00
|
|
|
const char *check_prefix;
|
2011-11-22 15:24:51 +08:00
|
|
|
CXIndex Idx;
|
|
|
|
CXIndexAction idxAction;
|
2012-12-06 05:53:37 +08:00
|
|
|
ImportedASTFilesData *importedASTs;
|
2011-10-29 06:54:36 +08:00
|
|
|
int result;
|
2011-10-18 03:48:19 +08:00
|
|
|
|
|
|
|
check_prefix = 0;
|
|
|
|
if (argc > 0) {
|
|
|
|
if (strstr(argv[0], "-check-prefix=") == argv[0]) {
|
|
|
|
check_prefix = argv[0] + strlen("-check-prefix=");
|
|
|
|
++argv;
|
|
|
|
--argc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-22 15:24:51 +08:00
|
|
|
if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
|
2013-03-02 05:41:22 +08:00
|
|
|
/* displayDiagnostics=*/1))) {
|
2011-11-22 15:24:51 +08:00
|
|
|
fprintf(stderr, "Could not create Index\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2012-12-06 05:53:37 +08:00
|
|
|
idxAction = clang_IndexAction_create(Idx);
|
|
|
|
importedASTs = 0;
|
2012-10-25 02:29:15 +08:00
|
|
|
if (full)
|
2012-12-06 05:53:37 +08:00
|
|
|
importedASTs = importedASTs_create();
|
|
|
|
|
|
|
|
result = index_compile_args(argc, argv, idxAction, importedASTs, check_prefix);
|
|
|
|
if (result != 0)
|
|
|
|
goto finished;
|
2011-10-29 06:54:36 +08:00
|
|
|
|
2012-10-25 02:29:15 +08:00
|
|
|
if (full) {
|
|
|
|
unsigned i;
|
2012-12-06 05:53:37 +08:00
|
|
|
for (i = 0; i < importedASTs->num_files && result == 0; ++i) {
|
|
|
|
result = index_ast_file(importedASTs->filenames[i], Idx, idxAction,
|
|
|
|
importedASTs, check_prefix);
|
2012-10-25 02:29:15 +08:00
|
|
|
}
|
|
|
|
}
|
2011-11-15 14:20:16 +08:00
|
|
|
|
2012-10-25 02:29:15 +08:00
|
|
|
finished:
|
2012-12-06 05:53:37 +08:00
|
|
|
importedASTs_dispose(importedASTs);
|
2011-11-22 15:24:51 +08:00
|
|
|
clang_IndexAction_dispose(idxAction);
|
|
|
|
clang_disposeIndex(Idx);
|
2011-11-15 14:20:16 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int index_tu(int argc, const char **argv) {
|
2012-12-06 05:53:37 +08:00
|
|
|
const char *check_prefix;
|
2011-11-15 14:20:16 +08:00
|
|
|
CXIndex Idx;
|
2011-11-22 15:24:51 +08:00
|
|
|
CXIndexAction idxAction;
|
2011-11-15 14:20:16 +08:00
|
|
|
int result;
|
|
|
|
|
|
|
|
check_prefix = 0;
|
|
|
|
if (argc > 0) {
|
|
|
|
if (strstr(argv[0], "-check-prefix=") == argv[0]) {
|
|
|
|
check_prefix = argv[0] + strlen("-check-prefix=");
|
|
|
|
++argv;
|
|
|
|
--argc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-06 05:53:37 +08:00
|
|
|
if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
|
2013-03-02 05:41:22 +08:00
|
|
|
/* displayDiagnostics=*/1))) {
|
2012-12-06 05:53:37 +08:00
|
|
|
fprintf(stderr, "Could not create Index\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
idxAction = clang_IndexAction_create(Idx);
|
|
|
|
|
|
|
|
result = index_ast_file(argv[0], Idx, idxAction,
|
|
|
|
/*importedASTs=*/0, check_prefix);
|
|
|
|
|
|
|
|
clang_IndexAction_dispose(idxAction);
|
|
|
|
clang_disposeIndex(Idx);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int index_compile_db(int argc, const char **argv) {
|
|
|
|
const char *check_prefix;
|
|
|
|
CXIndex Idx;
|
|
|
|
CXIndexAction idxAction;
|
|
|
|
int errorCode = 0;
|
|
|
|
|
|
|
|
check_prefix = 0;
|
|
|
|
if (argc > 0) {
|
|
|
|
if (strstr(argv[0], "-check-prefix=") == argv[0]) {
|
|
|
|
check_prefix = argv[0] + strlen("-check-prefix=");
|
|
|
|
++argv;
|
|
|
|
--argc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-15 14:20:16 +08:00
|
|
|
if (argc == 0) {
|
2012-12-06 05:53:37 +08:00
|
|
|
fprintf(stderr, "no compilation database\n");
|
2011-11-15 14:20:16 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
|
2013-03-02 05:41:22 +08:00
|
|
|
/* displayDiagnostics=*/1))) {
|
2011-11-15 14:20:16 +08:00
|
|
|
fprintf(stderr, "Could not create Index\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2012-12-06 05:53:37 +08:00
|
|
|
idxAction = clang_IndexAction_create(Idx);
|
2011-11-15 14:20:16 +08:00
|
|
|
|
2012-12-06 05:53:37 +08:00
|
|
|
{
|
|
|
|
const char *database = argv[0];
|
|
|
|
CXCompilationDatabase db = 0;
|
|
|
|
CXCompileCommands CCmds = 0;
|
|
|
|
CXCompileCommand CCmd;
|
|
|
|
CXCompilationDatabase_Error ec;
|
|
|
|
CXString wd;
|
|
|
|
#define MAX_COMPILE_ARGS 512
|
|
|
|
CXString cxargs[MAX_COMPILE_ARGS];
|
|
|
|
const char *args[MAX_COMPILE_ARGS];
|
|
|
|
char *tmp;
|
|
|
|
unsigned len;
|
|
|
|
char *buildDir;
|
|
|
|
int i, a, numCmds, numArgs;
|
|
|
|
|
|
|
|
len = strlen(database);
|
|
|
|
tmp = (char *) malloc(len+1);
|
|
|
|
memcpy(tmp, database, len+1);
|
|
|
|
buildDir = dirname(tmp);
|
|
|
|
|
|
|
|
db = clang_CompilationDatabase_fromDirectory(buildDir, &ec);
|
|
|
|
|
|
|
|
if (db) {
|
|
|
|
|
|
|
|
if (ec!=CXCompilationDatabase_NoError) {
|
|
|
|
printf("unexpected error %d code while loading compilation database\n", ec);
|
|
|
|
errorCode = -1;
|
|
|
|
goto cdb_end;
|
|
|
|
}
|
2011-11-15 14:20:16 +08:00
|
|
|
|
2012-12-18 04:19:56 +08:00
|
|
|
if (chdir(buildDir) != 0) {
|
|
|
|
printf("Could not chdir to %s\n", buildDir);
|
|
|
|
errorCode = -1;
|
|
|
|
goto cdb_end;
|
|
|
|
}
|
2011-11-15 14:20:16 +08:00
|
|
|
|
2012-12-18 04:19:56 +08:00
|
|
|
CCmds = clang_CompilationDatabase_getAllCompileCommands(db);
|
2012-12-06 05:53:37 +08:00
|
|
|
if (!CCmds) {
|
|
|
|
printf("compilation db is empty\n");
|
|
|
|
errorCode = -1;
|
|
|
|
goto cdb_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
numCmds = clang_CompileCommands_getSize(CCmds);
|
|
|
|
|
|
|
|
if (numCmds==0) {
|
|
|
|
fprintf(stderr, "should not get an empty compileCommand set\n");
|
|
|
|
errorCode = -1;
|
|
|
|
goto cdb_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i=0; i<numCmds && errorCode == 0; ++i) {
|
|
|
|
CCmd = clang_CompileCommands_getCommand(CCmds, i);
|
|
|
|
|
|
|
|
wd = clang_CompileCommand_getDirectory(CCmd);
|
2012-12-18 04:19:56 +08:00
|
|
|
if (chdir(clang_getCString(wd)) != 0) {
|
|
|
|
printf("Could not chdir to %s\n", clang_getCString(wd));
|
|
|
|
errorCode = -1;
|
|
|
|
goto cdb_end;
|
|
|
|
}
|
2012-12-06 05:53:37 +08:00
|
|
|
clang_disposeString(wd);
|
|
|
|
|
|
|
|
numArgs = clang_CompileCommand_getNumArgs(CCmd);
|
|
|
|
if (numArgs > MAX_COMPILE_ARGS){
|
|
|
|
fprintf(stderr, "got more compile arguments than maximum\n");
|
|
|
|
errorCode = -1;
|
|
|
|
goto cdb_end;
|
|
|
|
}
|
|
|
|
for (a=0; a<numArgs; ++a) {
|
|
|
|
cxargs[a] = clang_CompileCommand_getArg(CCmd, a);
|
|
|
|
args[a] = clang_getCString(cxargs[a]);
|
|
|
|
}
|
|
|
|
|
|
|
|
errorCode = index_compile_args(numArgs, args, idxAction,
|
|
|
|
/*importedASTs=*/0, check_prefix);
|
|
|
|
|
|
|
|
for (a=0; a<numArgs; ++a)
|
|
|
|
clang_disposeString(cxargs[a]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
printf("database loading failed with error code %d.\n", ec);
|
|
|
|
errorCode = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cdb_end:
|
|
|
|
clang_CompileCommands_dispose(CCmds);
|
|
|
|
clang_CompilationDatabase_dispose(db);
|
|
|
|
free(tmp);
|
|
|
|
|
|
|
|
}
|
2011-10-18 03:48:19 +08:00
|
|
|
|
2011-11-22 15:24:51 +08:00
|
|
|
clang_IndexAction_dispose(idxAction);
|
|
|
|
clang_disposeIndex(Idx);
|
2012-12-06 05:53:37 +08:00
|
|
|
return errorCode;
|
2011-10-18 03:48:19 +08:00
|
|
|
}
|
|
|
|
|
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;
|
2013-12-07 02:55:45 +08:00
|
|
|
CXSourceRangeList *skipped_ranges = 0;
|
2014-02-13 03:12:37 +08:00
|
|
|
enum CXErrorCode Err;
|
2010-01-27 01:06:03 +08:00
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
input += strlen("-test-annotate-tokens=");
|
|
|
|
if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
|
|
|
|
&second_line, &second_column)))
|
|
|
|
return errorCode;
|
|
|
|
|
2012-07-05 16:20:49 +08:00
|
|
|
if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) {
|
|
|
|
free(filename);
|
2010-01-27 01:06:03 +08:00
|
|
|
return -1;
|
2012-07-05 16:20:49 +08:00
|
|
|
}
|
2010-01-27 01:06:03 +08:00
|
|
|
|
2010-02-19 07:07:20 +08:00
|
|
|
CIdx = clang_createIndex(0, 1);
|
2014-02-13 03:12:37 +08:00
|
|
|
Err = clang_parseTranslationUnit2(CIdx, argv[argc - 1],
|
|
|
|
argv + num_unsaved_files + 2,
|
|
|
|
argc - num_unsaved_files - 3,
|
|
|
|
unsaved_files,
|
|
|
|
num_unsaved_files,
|
|
|
|
getDefaultParsingOptions(), &TU);
|
|
|
|
if (Err != CXError_Success) {
|
2010-01-27 01:06:03 +08:00
|
|
|
fprintf(stderr, "unable to parse input\n");
|
2014-02-13 03:12:37 +08:00
|
|
|
describeLibclangFailure(Err);
|
2010-01-27 01:06:03 +08:00
|
|
|
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;
|
|
|
|
|
2012-07-05 16:20:49 +08:00
|
|
|
if (checkForErrors(TU) != 0) {
|
|
|
|
errorCode = -1;
|
|
|
|
goto teardown;
|
|
|
|
}
|
2011-10-29 06:54:36 +08:00
|
|
|
|
2011-09-26 16:01:41 +08:00
|
|
|
if (getenv("CINDEXTEST_EDITING")) {
|
|
|
|
for (i = 0; i < 5; ++i) {
|
2014-02-13 03:12:37 +08:00
|
|
|
Err = clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
|
|
|
|
clang_defaultReparseOptions(TU));
|
|
|
|
if (Err != CXError_Success) {
|
2011-09-26 16:01:41 +08:00
|
|
|
fprintf(stderr, "Unable to reparse translation unit!\n");
|
2014-02-13 03:12:37 +08:00
|
|
|
describeLibclangFailure(Err);
|
2011-09-26 16:01:41 +08:00
|
|
|
errorCode = -1;
|
|
|
|
goto teardown;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-29 06:54:36 +08:00
|
|
|
if (checkForErrors(TU) != 0) {
|
|
|
|
errorCode = -1;
|
|
|
|
goto teardown;
|
|
|
|
}
|
|
|
|
|
2010-01-27 01:06:03 +08:00
|
|
|
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);
|
2011-10-29 06:54:36 +08:00
|
|
|
|
|
|
|
if (checkForErrors(TU) != 0) {
|
|
|
|
errorCode = -1;
|
|
|
|
goto teardown;
|
|
|
|
}
|
|
|
|
|
2010-01-27 01:06:03 +08:00
|
|
|
cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
|
|
|
|
clang_annotateTokens(TU, tokens, num_tokens, cursors);
|
2011-10-29 06:54:36 +08:00
|
|
|
|
|
|
|
if (checkForErrors(TU) != 0) {
|
|
|
|
errorCode = -1;
|
|
|
|
goto teardown;
|
|
|
|
}
|
|
|
|
|
2013-12-05 16:19:32 +08:00
|
|
|
skipped_ranges = clang_getSkippedRanges(TU, file);
|
|
|
|
for (i = 0; i != skipped_ranges->count; ++i) {
|
|
|
|
unsigned start_line, start_column, end_line, end_column;
|
|
|
|
clang_getSpellingLocation(clang_getRangeStart(skipped_ranges->ranges[i]),
|
|
|
|
0, &start_line, &start_column, 0);
|
|
|
|
clang_getSpellingLocation(clang_getRangeEnd(skipped_ranges->ranges[i]),
|
|
|
|
0, &end_line, &end_column, 0);
|
|
|
|
printf("Skipping: ");
|
|
|
|
PrintExtent(stdout, start_line, start_column, end_line, end_column);
|
|
|
|
printf("\n");
|
|
|
|
}
|
2013-12-07 02:55:45 +08:00
|
|
|
clang_disposeSourceRangeList(skipped_ranges);
|
2013-12-05 16:19:32 +08:00
|
|
|
|
2010-01-27 01:06:03 +08:00
|
|
|
for (i = 0; i != num_tokens; ++i) {
|
|
|
|
const char *kind = "<unknown>";
|
2013-07-23 04:58:30 +08:00
|
|
|
CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
|
|
|
|
CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
|
2010-01-27 01:06:03 +08:00
|
|
|
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));
|
2012-04-14 17:11:51 +08:00
|
|
|
clang_disposeString(spelling);
|
2010-02-14 16:32:24 +08:00
|
|
|
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(" ");
|
2012-08-08 01:54:38 +08:00
|
|
|
PrintCursor(cursors[i], NULL);
|
2010-01-27 02:31:56 +08:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-06-30 19:27:57 +08:00
|
|
|
static int
|
|
|
|
perform_test_compilation_db(const char *database, int argc, const char **argv) {
|
|
|
|
CXCompilationDatabase db;
|
|
|
|
CXCompileCommands CCmds;
|
|
|
|
CXCompileCommand CCmd;
|
|
|
|
CXCompilationDatabase_Error ec;
|
|
|
|
CXString wd;
|
|
|
|
CXString arg;
|
|
|
|
int errorCode = 0;
|
|
|
|
char *tmp;
|
|
|
|
unsigned len;
|
|
|
|
char *buildDir;
|
|
|
|
int i, j, a, numCmds, numArgs;
|
|
|
|
|
|
|
|
len = strlen(database);
|
|
|
|
tmp = (char *) malloc(len+1);
|
|
|
|
memcpy(tmp, database, len+1);
|
|
|
|
buildDir = dirname(tmp);
|
|
|
|
|
2012-07-04 04:38:12 +08:00
|
|
|
db = clang_CompilationDatabase_fromDirectory(buildDir, &ec);
|
2012-06-30 19:27:57 +08:00
|
|
|
|
|
|
|
if (db) {
|
|
|
|
|
|
|
|
if (ec!=CXCompilationDatabase_NoError) {
|
|
|
|
printf("unexpected error %d code while loading compilation database\n", ec);
|
|
|
|
errorCode = -1;
|
|
|
|
goto cdb_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i=0; i<argc && errorCode==0; ) {
|
|
|
|
if (strcmp(argv[i],"lookup")==0){
|
2012-07-04 04:38:12 +08:00
|
|
|
CCmds = clang_CompilationDatabase_getCompileCommands(db, argv[i+1]);
|
2012-06-30 19:27:57 +08:00
|
|
|
|
|
|
|
if (!CCmds) {
|
|
|
|
printf("file %s not found in compilation db\n", argv[i+1]);
|
|
|
|
errorCode = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-07-04 04:38:12 +08:00
|
|
|
numCmds = clang_CompileCommands_getSize(CCmds);
|
2012-06-30 19:27:57 +08:00
|
|
|
|
|
|
|
if (numCmds==0) {
|
|
|
|
fprintf(stderr, "should not get an empty compileCommand set for file"
|
|
|
|
" '%s'\n", argv[i+1]);
|
|
|
|
errorCode = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j=0; j<numCmds; ++j) {
|
2012-07-04 04:38:12 +08:00
|
|
|
CCmd = clang_CompileCommands_getCommand(CCmds, j);
|
2012-06-30 19:27:57 +08:00
|
|
|
|
2012-07-04 04:38:12 +08:00
|
|
|
wd = clang_CompileCommand_getDirectory(CCmd);
|
2012-06-30 19:27:57 +08:00
|
|
|
printf("workdir:'%s'", clang_getCString(wd));
|
|
|
|
clang_disposeString(wd);
|
|
|
|
|
|
|
|
printf(" cmdline:'");
|
2012-07-04 04:38:12 +08:00
|
|
|
numArgs = clang_CompileCommand_getNumArgs(CCmd);
|
2012-06-30 19:27:57 +08:00
|
|
|
for (a=0; a<numArgs; ++a) {
|
|
|
|
if (a) printf(" ");
|
2012-07-04 04:38:12 +08:00
|
|
|
arg = clang_CompileCommand_getArg(CCmd, a);
|
2012-06-30 19:27:57 +08:00
|
|
|
printf("%s", clang_getCString(arg));
|
|
|
|
clang_disposeString(arg);
|
|
|
|
}
|
|
|
|
printf("'\n");
|
|
|
|
}
|
|
|
|
|
2012-07-04 04:38:12 +08:00
|
|
|
clang_CompileCommands_dispose(CCmds);
|
2012-06-30 19:27:57 +08:00
|
|
|
|
|
|
|
i += 2;
|
|
|
|
}
|
|
|
|
}
|
2012-07-04 04:38:12 +08:00
|
|
|
clang_CompilationDatabase_dispose(db);
|
2012-06-30 19:27:57 +08:00
|
|
|
} else {
|
|
|
|
printf("database loading failed with error code %d.\n", ec);
|
|
|
|
errorCode = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cdb_end:
|
|
|
|
free(tmp);
|
|
|
|
|
|
|
|
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;
|
2014-02-13 03:12:37 +08:00
|
|
|
enum CXErrorCode Err;
|
2011-07-07 06:09:44 +08:00
|
|
|
int result = 0;
|
2010-08-13 13:36:37 +08:00
|
|
|
|
2013-03-02 05:41:22 +08:00
|
|
|
Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnostics=*/1);
|
2010-08-13 13:36:37 +08:00
|
|
|
|
|
|
|
if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
|
|
|
|
clang_disposeIndex(Idx);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-02-13 03:12:37 +08:00
|
|
|
|
|
|
|
Err = clang_parseTranslationUnit2(
|
|
|
|
Idx, 0, argv + num_unsaved_files, argc - num_unsaved_files,
|
|
|
|
unsaved_files, num_unsaved_files,
|
|
|
|
CXTranslationUnit_Incomplete |
|
|
|
|
CXTranslationUnit_DetailedPreprocessingRecord |
|
|
|
|
CXTranslationUnit_ForSerialization,
|
|
|
|
&TU);
|
|
|
|
if (Err != CXError_Success) {
|
2010-08-13 13:36:37 +08:00
|
|
|
fprintf(stderr, "Unable to load translation unit!\n");
|
2014-02-13 03:12:37 +08:00
|
|
|
describeLibclangFailure(Err);
|
2010-08-13 13:36:37 +08:00
|
|
|
free_remapped_files(unsaved_files, num_unsaved_files);
|
2014-02-13 03:12:37 +08:00
|
|
|
clang_disposeTranslationUnit(TU);
|
2010-08-13 13:36:37 +08:00
|
|
|
clang_disposeIndex(Idx);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-07-07 00:43:36 +08:00
|
|
|
switch (clang_saveTranslationUnit(TU, filename,
|
|
|
|
clang_defaultSaveOptions(TU))) {
|
|
|
|
case CXSaveError_None:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CXSaveError_TranslationErrors:
|
|
|
|
fprintf(stderr, "Unable to write PCH file %s: translation errors\n",
|
|
|
|
filename);
|
|
|
|
result = 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CXSaveError_InvalidTU:
|
|
|
|
fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n",
|
|
|
|
filename);
|
|
|
|
result = 3;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CXSaveError_Unknown:
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename);
|
|
|
|
result = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-08-13 13:36:37 +08:00
|
|
|
clang_disposeTranslationUnit(TU);
|
|
|
|
free_remapped_files(unsaved_files, num_unsaved_files);
|
|
|
|
clang_disposeIndex(Idx);
|
2011-07-07 00:43:36 +08:00
|
|
|
return result;
|
2010-08-13 13:36:37 +08:00
|
|
|
}
|
|
|
|
|
2011-11-10 16:43:12 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
/* Serialized diagnostics. */
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
static const char *getDiagnosticCodeStr(enum CXLoadDiag_Error error) {
|
|
|
|
switch (error) {
|
|
|
|
case CXLoadDiag_CannotLoad: return "Cannot Load File";
|
|
|
|
case CXLoadDiag_None: break;
|
|
|
|
case CXLoadDiag_Unknown: return "Unknown";
|
|
|
|
case CXLoadDiag_InvalidFile: return "Invalid File";
|
|
|
|
}
|
|
|
|
return "None";
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *getSeverityString(enum CXDiagnosticSeverity severity) {
|
|
|
|
switch (severity) {
|
|
|
|
case CXDiagnostic_Note: return "note";
|
|
|
|
case CXDiagnostic_Error: return "error";
|
|
|
|
case CXDiagnostic_Fatal: return "fatal";
|
|
|
|
case CXDiagnostic_Ignored: return "ignored";
|
|
|
|
case CXDiagnostic_Warning: return "warning";
|
|
|
|
}
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
|
|
|
|
static void printIndent(unsigned indent) {
|
2011-11-11 08:46:43 +08:00
|
|
|
if (indent == 0)
|
|
|
|
return;
|
|
|
|
fprintf(stderr, "+");
|
|
|
|
--indent;
|
2011-11-10 16:43:12 +08:00
|
|
|
while (indent > 0) {
|
2011-11-11 08:46:43 +08:00
|
|
|
fprintf(stderr, "-");
|
2011-11-10 16:43:12 +08:00
|
|
|
--indent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void printLocation(CXSourceLocation L) {
|
|
|
|
CXFile File;
|
|
|
|
CXString FileName;
|
|
|
|
unsigned line, column, offset;
|
|
|
|
|
|
|
|
clang_getExpansionLocation(L, &File, &line, &column, &offset);
|
|
|
|
FileName = clang_getFileName(File);
|
|
|
|
|
|
|
|
fprintf(stderr, "%s:%d:%d", clang_getCString(FileName), line, column);
|
|
|
|
clang_disposeString(FileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void printRanges(CXDiagnostic D, unsigned indent) {
|
|
|
|
unsigned i, n = clang_getDiagnosticNumRanges(D);
|
|
|
|
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
CXSourceLocation Start, End;
|
2013-07-23 04:58:30 +08:00
|
|
|
CXSourceRange SR = clang_getDiagnosticRange(D, i);
|
2011-11-10 16:43:12 +08:00
|
|
|
Start = clang_getRangeStart(SR);
|
|
|
|
End = clang_getRangeEnd(SR);
|
|
|
|
|
|
|
|
printIndent(indent);
|
|
|
|
fprintf(stderr, "Range: ");
|
|
|
|
printLocation(Start);
|
|
|
|
fprintf(stderr, " ");
|
|
|
|
printLocation(End);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void printFixIts(CXDiagnostic D, unsigned indent) {
|
|
|
|
unsigned i, n = clang_getDiagnosticNumFixIts(D);
|
2012-03-21 04:49:45 +08:00
|
|
|
fprintf(stderr, "Number FIXITs = %d\n", n);
|
2011-11-10 16:43:12 +08:00
|
|
|
for (i = 0 ; i < n; ++i) {
|
|
|
|
CXSourceRange ReplacementRange;
|
|
|
|
CXString text;
|
|
|
|
text = clang_getDiagnosticFixIt(D, i, &ReplacementRange);
|
|
|
|
|
|
|
|
printIndent(indent);
|
|
|
|
fprintf(stderr, "FIXIT: (");
|
|
|
|
printLocation(clang_getRangeStart(ReplacementRange));
|
|
|
|
fprintf(stderr, " - ");
|
|
|
|
printLocation(clang_getRangeEnd(ReplacementRange));
|
|
|
|
fprintf(stderr, "): \"%s\"\n", clang_getCString(text));
|
|
|
|
clang_disposeString(text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) {
|
2011-11-10 17:30:15 +08:00
|
|
|
unsigned i, n;
|
|
|
|
|
2011-11-10 16:43:12 +08:00
|
|
|
if (!Diags)
|
|
|
|
return;
|
|
|
|
|
2011-11-10 17:30:15 +08:00
|
|
|
n = clang_getNumDiagnosticsInSet(Diags);
|
2011-11-10 16:43:12 +08:00
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
CXSourceLocation DiagLoc;
|
|
|
|
CXDiagnostic D;
|
|
|
|
CXFile File;
|
2012-04-12 08:03:31 +08:00
|
|
|
CXString FileName, DiagSpelling, DiagOption, DiagCat;
|
2011-11-10 16:43:12 +08:00
|
|
|
unsigned line, column, offset;
|
2012-04-12 08:03:31 +08:00
|
|
|
const char *DiagOptionStr = 0, *DiagCatStr = 0;
|
2011-11-10 16:43:12 +08:00
|
|
|
|
|
|
|
D = clang_getDiagnosticInSet(Diags, i);
|
|
|
|
DiagLoc = clang_getDiagnosticLocation(D);
|
|
|
|
clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset);
|
|
|
|
FileName = clang_getFileName(File);
|
|
|
|
DiagSpelling = clang_getDiagnosticSpelling(D);
|
|
|
|
|
|
|
|
printIndent(indent);
|
|
|
|
|
|
|
|
fprintf(stderr, "%s:%d:%d: %s: %s",
|
|
|
|
clang_getCString(FileName),
|
|
|
|
line,
|
|
|
|
column,
|
|
|
|
getSeverityString(clang_getDiagnosticSeverity(D)),
|
|
|
|
clang_getCString(DiagSpelling));
|
|
|
|
|
|
|
|
DiagOption = clang_getDiagnosticOption(D, 0);
|
|
|
|
DiagOptionStr = clang_getCString(DiagOption);
|
|
|
|
if (DiagOptionStr) {
|
|
|
|
fprintf(stderr, " [%s]", DiagOptionStr);
|
|
|
|
}
|
|
|
|
|
2012-04-12 08:03:31 +08:00
|
|
|
DiagCat = clang_getDiagnosticCategoryText(D);
|
|
|
|
DiagCatStr = clang_getCString(DiagCat);
|
|
|
|
if (DiagCatStr) {
|
|
|
|
fprintf(stderr, " [%s]", DiagCatStr);
|
|
|
|
}
|
|
|
|
|
2011-11-10 16:43:12 +08:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
|
|
|
printRanges(D, indent);
|
|
|
|
printFixIts(D, indent);
|
|
|
|
|
2011-11-10 18:07:57 +08:00
|
|
|
/* Print subdiagnostics. */
|
2011-11-10 16:43:12 +08:00
|
|
|
printDiagnosticSet(clang_getChildDiagnostics(D), indent+2);
|
|
|
|
|
|
|
|
clang_disposeString(FileName);
|
|
|
|
clang_disposeString(DiagSpelling);
|
|
|
|
clang_disposeString(DiagOption);
|
2014-05-12 01:16:59 +08:00
|
|
|
clang_disposeString(DiagCat);
|
2011-11-10 16:43:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int read_diagnostics(const char *filename) {
|
|
|
|
enum CXLoadDiag_Error error;
|
|
|
|
CXString errorString;
|
|
|
|
CXDiagnosticSet Diags = 0;
|
|
|
|
|
|
|
|
Diags = clang_loadDiagnostics(filename, &error, &errorString);
|
|
|
|
if (!Diags) {
|
|
|
|
fprintf(stderr, "Trouble deserializing file (%s): %s\n",
|
|
|
|
getDiagnosticCodeStr(error),
|
|
|
|
clang_getCString(errorString));
|
|
|
|
clang_disposeString(errorString);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
printDiagnosticSet(Diags, 0);
|
2011-11-11 08:46:43 +08:00
|
|
|
fprintf(stderr, "Number of diagnostics: %d\n",
|
|
|
|
clang_getNumDiagnosticsInSet(Diags));
|
2011-11-10 16:43:12 +08:00
|
|
|
clang_disposeDiagnosticSet(Diags);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
Add an option to allow Clang verify source files for a module only once during
the build
When Clang loads the module, it verifies the user source files that the module
was built from. If any file was changed, the module is rebuilt. There are two
problems with this:
1. correctness: we don't verify system files (there are too many of them, and
stat'ing all of them would take a lot of time);
2. performance: the same module file is verified again and again during a
single build.
This change allows the build system to optimize source file verification. The
idea is based on the fact that while the project is being built, the source
files don't change. This allows us to verify the module only once during a
single build session. The build system passes a flag,
-fbuild-session-timestamp=, to inform Clang of the time when the build started.
The build system also requests to enable this feature by passing
-fmodules-validate-once-per-build-session. If these flags are not passed, the
behavior is not changed. When Clang verifies the module the first time, it
writes out a timestamp file. Then, when Clang loads the module the second
time, it finds a timestamp file, so it can compare the verification timestamp
of the module with the time when the build started. If the verification
timestamp is too old, the module is verified again, and the timestamp file is
updated.
llvm-svn: 201224
2014-02-12 18:33:14 +08:00
|
|
|
static int perform_print_build_session_timestamp(void) {
|
2015-05-14 14:53:31 +08:00
|
|
|
printf("%lld\n", clang_getBuildSessionTimestamp());
|
Add an option to allow Clang verify source files for a module only once during
the build
When Clang loads the module, it verifies the user source files that the module
was built from. If any file was changed, the module is rebuilt. There are two
problems with this:
1. correctness: we don't verify system files (there are too many of them, and
stat'ing all of them would take a lot of time);
2. performance: the same module file is verified again and again during a
single build.
This change allows the build system to optimize source file verification. The
idea is based on the fact that while the project is being built, the source
files don't change. This allows us to verify the module only once during a
single build session. The build system passes a flag,
-fbuild-session-timestamp=, to inform Clang of the time when the build started.
The build system also requests to enable this feature by passing
-fmodules-validate-once-per-build-session. If these flags are not passed, the
behavior is not changed. When Clang verifies the module the first time, it
writes out a timestamp file. Then, when Clang loads the module the second
time, it finds a timestamp file, so it can compare the verification timestamp
of the module with the time when the build started. If the verification
timestamp is too old, the module is verified again, and the timestamp file is
updated.
llvm-svn: 201224
2014-02-12 18:33:14 +08:00
|
|
|
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;
|
2011-04-19 06:47:10 +08:00
|
|
|
if (strncmp(s, "-memory-usage", 13) == 0)
|
|
|
|
return GetVisitor(s + 13);
|
2010-01-13 07:34:26 +08:00
|
|
|
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"
|
2013-03-08 10:32:34 +08:00
|
|
|
" c-index-test -file-refs-at=<site> <compiler arguments>\n"
|
|
|
|
" c-index-test -file-includes-in=<filename> <compiler arguments>\n");
|
2012-10-25 06:52:04 +08:00
|
|
|
fprintf(stderr,
|
2011-10-18 03:48:19 +08:00
|
|
|
" c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
|
2012-10-25 02:29:15 +08:00
|
|
|
" c-index-test -index-file-full [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
|
2011-11-15 14:20:16 +08:00
|
|
|
" c-index-test -index-tu [-check-prefix=<FileCheck prefix>] <AST file>\n"
|
2012-12-06 05:53:37 +08:00
|
|
|
" c-index-test -index-compile-db [-check-prefix=<FileCheck prefix>] <compilation database>\n"
|
2009-11-18 10:02:52 +08:00
|
|
|
" c-index-test -test-file-scan <AST file> <source file> "
|
2011-10-06 19:38:08 +08:00
|
|
|
"[FileCheck prefix]\n");
|
|
|
|
fprintf(stderr,
|
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,
|
2011-04-19 06:47:10 +08:00
|
|
|
" c-index-test -test-load-source-memory-usage "
|
|
|
|
"<symbol filter> {<args>}*\n"
|
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"
|
2011-04-19 06:47:10 +08:00
|
|
|
" c-index-test -test-load-source-usrs-memory-usage "
|
|
|
|
"<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"
|
2011-04-19 07:42:53 +08:00
|
|
|
" c-index-test -test-inclusion-stack-tu <AST file>\n");
|
2010-07-22 14:29:13 +08:00
|
|
|
fprintf(stderr,
|
2011-04-19 07:42:53 +08:00
|
|
|
" c-index-test -test-print-linkage-source {<args>}*\n"
|
2013-02-16 05:15:49 +08:00
|
|
|
" c-index-test -test-print-type {<args>}*\n"
|
2013-04-11 09:20:11 +08:00
|
|
|
" c-index-test -test-print-type-size {<args>}*\n"
|
2012-12-04 23:13:46 +08:00
|
|
|
" c-index-test -test-print-bitwidth {<args>}*\n"
|
2011-04-19 06:47:10 +08:00
|
|
|
" c-index-test -print-usr [<CursorKind> {<args>}]*\n"
|
2010-08-13 13:36:37 +08:00
|
|
|
" c-index-test -print-usr-file <file>\n"
|
2011-11-10 16:43:12 +08:00
|
|
|
" c-index-test -write-pch <file> <compiler arguments>\n");
|
2012-06-30 19:27:57 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
" c-index-test -compilation-db [lookup <filename>] database\n");
|
Add an option to allow Clang verify source files for a module only once during
the build
When Clang loads the module, it verifies the user source files that the module
was built from. If any file was changed, the module is rebuilt. There are two
problems with this:
1. correctness: we don't verify system files (there are too many of them, and
stat'ing all of them would take a lot of time);
2. performance: the same module file is verified again and again during a
single build.
This change allows the build system to optimize source file verification. The
idea is based on the fact that while the project is being built, the source
files don't change. This allows us to verify the module only once during a
single build session. The build system passes a flag,
-fbuild-session-timestamp=, to inform Clang of the time when the build started.
The build system also requests to enable this feature by passing
-fmodules-validate-once-per-build-session. If these flags are not passed, the
behavior is not changed. When Clang verifies the module the first time, it
writes out a timestamp file. Then, when Clang loads the module the second
time, it finds a timestamp file, so it can compare the verification timestamp
of the module with the time when the build started. If the verification
timestamp is too old, the module is verified again, and the timestamp file is
updated.
llvm-svn: 201224
2014-02-12 18:33:14 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
" c-index-test -print-build-session-timestamp\n");
|
2011-11-10 16:43:12 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
" c-index-test -read-diagnostics <file>\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();
|
2011-11-10 16:43:12 +08:00
|
|
|
if (argc > 2 && strcmp(argv[1], "-read-diagnostics") == 0)
|
|
|
|
return read_diagnostics(argv[2]);
|
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);
|
2011-10-06 15:00:54 +08:00
|
|
|
if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1])
|
|
|
|
return find_file_refs_at(argc, argv);
|
2013-03-08 10:32:34 +08:00
|
|
|
if (argc > 2 && strstr(argv[1], "-file-includes-in=") == argv[1])
|
|
|
|
return find_file_includes_in(argc, argv);
|
2011-10-18 03:48:19 +08:00
|
|
|
if (argc > 2 && strcmp(argv[1], "-index-file") == 0)
|
2012-10-25 02:29:15 +08:00
|
|
|
return index_file(argc - 2, argv + 2, /*full=*/0);
|
|
|
|
if (argc > 2 && strcmp(argv[1], "-index-file-full") == 0)
|
|
|
|
return index_file(argc - 2, argv + 2, /*full=*/1);
|
2011-11-15 14:20:16 +08:00
|
|
|
if (argc > 2 && strcmp(argv[1], "-index-tu") == 0)
|
|
|
|
return index_tu(argc - 2, argv + 2);
|
2012-12-06 05:53:37 +08:00
|
|
|
if (argc > 2 && strcmp(argv[1], "-index-compile-db") == 0)
|
|
|
|
return index_compile_db(argc - 2, argv + 2);
|
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);
|
2011-04-19 06:47:10 +08:00
|
|
|
|
|
|
|
PostVisitTU postVisit = 0;
|
|
|
|
if (strstr(argv[1], "-memory-usage"))
|
|
|
|
postVisit = PrintMemoryUsage;
|
|
|
|
|
2010-01-13 07:34:26 +08:00
|
|
|
if (I)
|
2011-04-19 06:47:10 +08:00
|
|
|
return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
|
|
|
|
postVisit);
|
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);
|
2013-02-16 05:15:49 +08:00
|
|
|
else if (argc > 2 && strcmp(argv[1], "-test-print-type") == 0)
|
2010-05-15 05:29:26 +08:00
|
|
|
return perform_test_load_source(argc - 2, argv + 2, "all",
|
2013-02-16 05:15:49 +08:00
|
|
|
PrintType, 0);
|
2013-04-11 09:20:11 +08:00
|
|
|
else if (argc > 2 && strcmp(argv[1], "-test-print-type-size") == 0)
|
|
|
|
return perform_test_load_source(argc - 2, argv + 2, "all",
|
|
|
|
PrintTypeSize, 0);
|
2012-12-04 23:13:46 +08:00
|
|
|
else if (argc > 2 && strcmp(argv[1], "-test-print-bitwidth") == 0)
|
|
|
|
return perform_test_load_source(argc - 2, argv + 2, "all",
|
|
|
|
PrintBitWidth, 0);
|
2014-08-01 02:04:56 +08:00
|
|
|
else if (argc > 2 && strcmp(argv[1], "-test-print-mangle") == 0)
|
|
|
|
return perform_test_load_tu(argv[2], "all", NULL, PrintMangledName, NULL);
|
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);
|
2012-06-30 19:27:57 +08:00
|
|
|
else if (argc > 2 && strcmp(argv[1], "-compilation-db") == 0)
|
|
|
|
return perform_test_compilation_db(argv[argc-1], argc - 3, argv + 2);
|
Add an option to allow Clang verify source files for a module only once during
the build
When Clang loads the module, it verifies the user source files that the module
was built from. If any file was changed, the module is rebuilt. There are two
problems with this:
1. correctness: we don't verify system files (there are too many of them, and
stat'ing all of them would take a lot of time);
2. performance: the same module file is verified again and again during a
single build.
This change allows the build system to optimize source file verification. The
idea is based on the fact that while the project is being built, the source
files don't change. This allows us to verify the module only once during a
single build session. The build system passes a flag,
-fbuild-session-timestamp=, to inform Clang of the time when the build started.
The build system also requests to enable this feature by passing
-fmodules-validate-once-per-build-session. If these flags are not passed, the
behavior is not changed. When Clang verifies the module the first time, it
writes out a timestamp file. Then, when Clang loads the module the second
time, it finds a timestamp file, so it can compare the verification timestamp
of the module with the time when the build started. If the verification
timestamp is too old, the module is verified again, and the timestamp file is
updated.
llvm-svn: 201224
2014-02-12 18:33:14 +08:00
|
|
|
else if (argc == 2 && strcmp(argv[1], "-print-build-session-timestamp") == 0)
|
|
|
|
return perform_print_build_session_timestamp();
|
2012-06-30 19:27:57 +08:00
|
|
|
|
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);
|
2014-06-05 08:13:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void flush_atexit(void) {
|
2014-06-06 19:04:46 +08:00
|
|
|
/* stdout, and surprisingly even stderr, are not always flushed on process
|
|
|
|
* and thread exit, particularly when the system is under heavy load. */
|
2014-06-05 08:13:43 +08:00
|
|
|
fflush(stdout);
|
|
|
|
fflush(stderr);
|
2010-10-01 04:39:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, const char **argv) {
|
2012-08-10 18:06:13 +08:00
|
|
|
thread_info client_data;
|
|
|
|
|
2014-06-05 08:13:43 +08:00
|
|
|
atexit(flush_atexit);
|
|
|
|
|
2012-08-08 01:54:38 +08:00
|
|
|
#ifdef CLANG_HAVE_LIBXML
|
|
|
|
LIBXML_TEST_VERSION
|
|
|
|
#endif
|
|
|
|
|
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;
|
|
|
|
}
|