aboutsummaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2011-02-20 13:06:31 +0000
committerDimitry Andric <dim@FreeBSD.org>2011-02-20 13:06:31 +0000
commitbca07a4524feb4edec581062d631a13116320a24 (patch)
treea9243275843fbeaa590afc07ee888e006b8d54ea /tools
parent998bc5802ecdd65ce3b270f6c69a8ae8557f0a10 (diff)
downloadsrc-bca07a4524feb4edec581062d631a13116320a24.tar.gz
src-bca07a4524feb4edec581062d631a13116320a24.zip
Vendor import of clang trunk r126079:vendor/clang/clang-r126079
Notes
Notes: svn path=/vendor/clang/dist/; revision=218887 svn path=/vendor/clang/clang-r126079/; revision=218888; tag=vendor/clang/clang-r126079
Diffstat (limited to 'tools')
-rw-r--r--tools/Makefile2
-rw-r--r--tools/c-index-test/CMakeLists.txt19
-rw-r--r--tools/c-index-test/Makefile2
-rw-r--r--tools/c-index-test/c-index-test.c293
-rw-r--r--tools/driver/CMakeLists.txt34
-rw-r--r--tools/driver/Makefile14
-rw-r--r--tools/driver/cc1_main.cpp10
-rw-r--r--tools/driver/cc1as_main.cpp47
-rw-r--r--tools/driver/clang_symlink.cmake20
-rw-r--r--tools/driver/driver.cpp56
-rw-r--r--tools/libclang/CIndex.cpp2927
-rw-r--r--tools/libclang/CIndexCXX.cpp5
-rw-r--r--tools/libclang/CIndexCodeCompletion.cpp525
-rw-r--r--tools/libclang/CIndexDiagnostic.cpp174
-rw-r--r--tools/libclang/CIndexDiagnostic.h20
-rw-r--r--tools/libclang/CIndexInclusionStack.cpp5
-rw-r--r--tools/libclang/CIndexUSRs.cpp187
-rw-r--r--tools/libclang/CIndexer.cpp66
-rw-r--r--tools/libclang/CIndexer.h46
-rw-r--r--tools/libclang/CMakeLists.txt10
-rw-r--r--tools/libclang/CXCursor.cpp233
-rw-r--r--tools/libclang/CXCursor.h94
-rw-r--r--tools/libclang/CXString.cpp130
-rw-r--r--tools/libclang/CXString.h53
-rw-r--r--tools/libclang/CXTranslationUnit.h24
-rw-r--r--tools/libclang/CXType.cpp196
-rw-r--r--tools/libclang/CXType.h2
-rw-r--r--tools/libclang/Makefile2
-rw-r--r--tools/libclang/libclang.darwin.exports27
-rw-r--r--tools/libclang/libclang.exports27
-rwxr-xr-xtools/scan-build/ccc-analyzer119
-rwxr-xr-xtools/scan-build/scan-build206
32 files changed, 3648 insertions, 1927 deletions
diff --git a/tools/Makefile b/tools/Makefile
index 0202cc5bbbb0..000293f09c9f 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -12,7 +12,7 @@ DIRS := driver libclang c-index-test
include $(CLANG_LEVEL)/../../Makefile.config
-ifeq ($(OS), $(filter $(OS), Cygwin MingW Minix))
+ifeq ($(OS), $(filter $(OS), Minix))
DIRS := $(filter-out libclang c-index-test, $(DIRS))
endif
diff --git a/tools/c-index-test/CMakeLists.txt b/tools/c-index-test/CMakeLists.txt
index 5cf2cd6ebf34..45ad9e35c6ef 100644
--- a/tools/c-index-test/CMakeLists.txt
+++ b/tools/c-index-test/CMakeLists.txt
@@ -1,23 +1,8 @@
-set(LLVM_NO_RTTI 1)
-
-set( LLVM_USED_LIBS
- libclang
- clangIndex
- clangFrontend
- clangDriver
- clangSerialization
- clangParse
- clangSema
- clangAnalysis
- clangAST
- clangLex
- clangBasic
- )
+set(LLVM_USED_LIBS libclang)
set( LLVM_LINK_COMPONENTS
- bitreader
+ support
mc
- core
)
add_clang_executable(c-index-test
diff --git a/tools/c-index-test/Makefile b/tools/c-index-test/Makefile
index f41aa8098155..3d9849a3a401 100644
--- a/tools/c-index-test/Makefile
+++ b/tools/c-index-test/Makefile
@@ -13,7 +13,7 @@ TOOLNAME = c-index-test
# No plugins, optimize startup time.
TOOL_NO_EXPORTS = 1
-LINK_COMPONENTS := bitreader mc core
+LINK_COMPONENTS := support mc
USEDLIBS = clang.a clangIndex.a clangFrontend.a clangDriver.a \
clangSerialization.a clangParse.a clangSema.a clangAnalysis.a \
clangAST.a clangLex.a clangBasic.a
diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c
index 58eff97ef8f8..d4e567d9e26a 100644
--- a/tools/c-index-test/c-index-test.c
+++ b/tools/c-index-test/c-index-test.c
@@ -109,7 +109,7 @@ int parse_remapped_files(int argc, const char **argv, int start_arg,
}
/* Open the file that we're remapping to. */
- to_file = fopen(semi + 1, "r");
+ to_file = fopen(semi + 1, "rb");
if (!to_file) {
fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
semi + 1);
@@ -156,6 +156,8 @@ int parse_remapped_files(int argc, const char **argv, int start_arg,
/* Pretty-printing. */
/******************************************************************************/
+int want_display_name = 0;
+
static void PrintCursor(CXCursor Cursor) {
if (clang_isInvalid(Cursor.kind)) {
CXString ks = clang_getCursorKindSpelling(Cursor.kind);
@@ -167,9 +169,12 @@ static void PrintCursor(CXCursor Cursor) {
CXCursor Referenced;
unsigned line, column;
CXCursor SpecializationOf;
-
+ CXCursor *overridden;
+ unsigned num_overridden;
+
ks = clang_getCursorKindSpelling(Cursor.kind);
- string = clang_getCursorSpelling(Cursor);
+ string = want_display_name? clang_getCursorDisplayName(Cursor)
+ : clang_getCursorSpelling(Cursor);
printf("%s=%s", clang_getCString(ks),
clang_getCString(string));
clang_disposeString(ks);
@@ -177,9 +182,25 @@ static void PrintCursor(CXCursor Cursor) {
Referenced = clang_getCursorReferenced(Cursor);
if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
- CXSourceLocation Loc = clang_getCursorLocation(Referenced);
- clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
- printf(":%d:%d", line, column);
+ if (clang_getCursorKind(Referenced) == CXCursor_OverloadedDeclRef) {
+ unsigned I, N = clang_getNumOverloadedDecls(Referenced);
+ printf("[");
+ for (I = 0; I != N; ++I) {
+ CXCursor Ovl = clang_getOverloadedDecl(Referenced, I);
+ CXSourceLocation Loc;
+ if (I)
+ printf(", ");
+
+ Loc = clang_getCursorLocation(Ovl);
+ clang_getSpellingLocation(Loc, 0, &line, &column, 0);
+ printf("%d:%d", line, column);
+ }
+ printf("]");
+ } else {
+ CXSourceLocation Loc = clang_getCursorLocation(Referenced);
+ clang_getSpellingLocation(Loc, 0, &line, &column, 0);
+ printf(":%d:%d", line, column);
+ }
}
if (clang_isCursorDefinition(Cursor))
@@ -230,11 +251,33 @@ static void PrintCursor(CXCursor Cursor) {
if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
CXString Name = clang_getCursorSpelling(SpecializationOf);
- clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
+ clang_getSpellingLocation(Loc, 0, &line, &column, 0);
printf(" [Specialization of %s:%d:%d]",
clang_getCString(Name), line, column);
clang_disposeString(Name);
}
+
+ clang_getOverriddenCursors(Cursor, &overridden, &num_overridden);
+ if (num_overridden) {
+ unsigned I;
+ printf(" [Overrides ");
+ for (I = 0; I != num_overridden; ++I) {
+ CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
+ clang_getSpellingLocation(Loc, 0, &line, &column, 0);
+ if (I)
+ printf(", ");
+ printf("@%d:%d", line, column);
+ }
+ printf("]");
+ clang_disposeOverriddenCursors(overridden);
+ }
+
+ if (Cursor.kind == CXCursor_InclusionDirective) {
+ CXFile File = clang_getIncludedFile(Cursor);
+ CXString Included = clang_getFileName(File);
+ printf(" (%s)", clang_getCString(Included));
+ clang_disposeString(Included);
+ }
}
}
@@ -242,7 +285,7 @@ static const char* GetCursorSource(CXCursor Cursor) {
CXSourceLocation Loc = clang_getCursorLocation(Cursor);
CXString source;
CXFile file;
- clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
+ clang_getSpellingLocation(Loc, &file, 0, 0, 0);
source = clang_getFileName(file);
if (!clang_getCString(source)) {
clang_disposeString(source);
@@ -266,7 +309,8 @@ void PrintDiagnostic(CXDiagnostic Diagnostic) {
CXFile file;
CXString Msg;
unsigned display_opts = CXDiagnostic_DisplaySourceLocation
- | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges;
+ | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges
+ | CXDiagnostic_DisplayOption;
unsigned i, num_fixits;
if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
@@ -276,8 +320,8 @@ void PrintDiagnostic(CXDiagnostic Diagnostic) {
fprintf(stderr, "%s\n", clang_getCString(Msg));
clang_disposeString(Msg);
- clang_getInstantiationLocation(clang_getDiagnosticLocation(Diagnostic),
- &file, 0, 0, 0);
+ clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
+ &file, 0, 0, 0);
if (!file)
return;
@@ -289,9 +333,9 @@ void PrintDiagnostic(CXDiagnostic Diagnostic) {
CXSourceLocation end = clang_getRangeEnd(range);
unsigned start_line, start_column, end_line, end_column;
CXFile start_file, end_file;
- clang_getInstantiationLocation(start, &start_file, &start_line,
- &start_column, 0);
- clang_getInstantiationLocation(end, &end_file, &end_line, &end_column, 0);
+ clang_getSpellingLocation(start, &start_file, &start_line,
+ &start_column, 0);
+ clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
if (clang_equalLocations(start, end)) {
/* Insertion. */
if (start_file == file)
@@ -337,10 +381,10 @@ static void PrintCursorExtent(CXCursor C) {
CXFile begin_file, end_file;
unsigned begin_line, begin_column, end_line, end_column;
- clang_getInstantiationLocation(clang_getRangeStart(extent),
- &begin_file, &begin_line, &begin_column, 0);
- clang_getInstantiationLocation(clang_getRangeEnd(extent),
- &end_file, &end_line, &end_column, 0);
+ clang_getSpellingLocation(clang_getRangeStart(extent),
+ &begin_file, &begin_line, &begin_column, 0);
+ clang_getSpellingLocation(clang_getRangeEnd(extent),
+ &end_file, &end_line, &end_column, 0);
if (!begin_file || !end_file)
return;
@@ -362,7 +406,7 @@ enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
CXSourceLocation Loc = clang_getCursorLocation(Cursor);
unsigned line, column;
- clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
+ clang_getSpellingLocation(Loc, 0, &line, &column, 0);
printf("// %s: %s:%d:%d: ", FileCheckPrefix,
GetCursorSource(Cursor), line, column);
PrintCursor(Cursor);
@@ -406,7 +450,7 @@ static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
curColumn++;
Loc = clang_getCursorLocation(Cursor);
- clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
+ clang_getSpellingLocation(Loc, &file, 0, 0, 0);
source = clang_getFileName(file);
if (clang_getCString(source)) {
@@ -472,8 +516,8 @@ void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
for (i = 0; i < includeStackLen; ++i) {
CXFile includingFile;
unsigned line, column;
- clang_getInstantiationLocation(includeStack[i], &includingFile, &line,
- &column, 0);
+ clang_getSpellingLocation(includeStack[i], &includingFile, &line,
+ &column, 0);
fname = clang_getFileName(includingFile);
printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
clang_disposeString(fname);
@@ -524,6 +568,12 @@ static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p,
CXString S = clang_getTypeKindSpelling(T.kind);
PrintCursor(cursor);
printf(" typekind=%s", clang_getCString(S));
+ if (clang_isConstQualifiedType(T))
+ printf(" const");
+ if (clang_isVolatileQualifiedType(T))
+ printf(" volatile");
+ if (clang_isRestrictQualifiedType(T))
+ printf(" restrict");
clang_disposeString(S);
/* Print the canonical type if it is different. */
{
@@ -571,6 +621,11 @@ static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
/* Perform some simple filtering. */
if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
+ else if (!strcmp(filter, "all-display") ||
+ !strcmp(filter, "local-display")) {
+ ck = NULL;
+ want_display_name = 1;
+ }
else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
@@ -619,8 +674,6 @@ int perform_test_load_tu(const char *file, const char *filter,
int perform_test_load_source(int argc, const char **argv,
const char *filter, CXCursorVisitor Visitor,
PostVisitTU PV) {
- const char *UseExternalASTs =
- getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
CXIndex Idx;
CXTranslationUnit TU;
struct CXUnsavedFile *unsaved_files = 0;
@@ -628,11 +681,9 @@ int perform_test_load_source(int argc, const char **argv,
int result;
Idx = clang_createIndex(/* excludeDeclsFromPCH */
- !strcmp(filter, "local") ? 1 : 0,
- /* displayDiagnosics=*/1);
-
- if (UseExternalASTs && strlen(UseExternalASTs))
- clang_setUseExternalASTGeneration(Idx, 1);
+ (!strcmp(filter, "local") ||
+ !strcmp(filter, "local-display"))? 1 : 0,
+ /* displayDiagnosics=*/0);
if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
clang_disposeIndex(Idx);
@@ -660,8 +711,6 @@ int perform_test_load_source(int argc, const char **argv,
int perform_test_reparse_source(int argc, const char **argv, int trials,
const char *filter, CXCursorVisitor Visitor,
PostVisitTU PV) {
- const char *UseExternalASTs =
- getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
CXIndex Idx;
CXTranslationUnit TU;
struct CXUnsavedFile *unsaved_files = 0;
@@ -671,10 +720,7 @@ int perform_test_reparse_source(int argc, const char **argv, int trials,
Idx = clang_createIndex(/* excludeDeclsFromPCH */
!strcmp(filter, "local") ? 1 : 0,
- /* displayDiagnosics=*/1);
-
- if (UseExternalASTs && strlen(UseExternalASTs))
- clang_setUseExternalASTGeneration(Idx, 1);
+ /* displayDiagnosics=*/0);
if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
clang_disposeIndex(Idx);
@@ -781,11 +827,13 @@ static int perform_file_scan(const char *ast_file, const char *source_file,
}
fclose(fp);
+ clang_disposeTranslationUnit(TU);
+ clang_disposeIndex(Idx);
return 0;
}
/******************************************************************************/
-/* Logic for testing clang_codeComplete(). */
+/* Logic for testing clang code completion. */
/******************************************************************************/
/* Parse file:line:column from the input string. Returns 0 on success, non-zero
@@ -897,6 +945,11 @@ void print_completion_string(CXCompletionString completion_string, FILE *file) {
file);
fprintf(file, "}");
continue;
+ }
+
+ if (Kind == CXCompletionChunk_VerticalSpace) {
+ fprintf(file, "{VerticalSpace }");
+ continue;
}
text = clang_getCompletionChunkText(completion_string, I);
@@ -964,7 +1017,12 @@ int perform_code_completion(int argc, const char **argv, int timing_only) {
struct CXUnsavedFile *unsaved_files = 0;
int num_unsaved_files = 0;
CXCodeCompleteResults *results = 0;
- CXTranslationUnit *TU = 0;
+ CXTranslationUnit TU = 0;
+ unsigned I, Repeats = 1;
+ unsigned completionOptions = clang_defaultCodeCompleteOptions();
+
+ if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
+ completionOptions |= CXCodeComplete_IncludeCodePatterns;
if (timing_only)
input += strlen("-code-completion-timing=");
@@ -978,34 +1036,36 @@ int perform_code_completion(int argc, const char **argv, int timing_only) {
if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
return -1;
- CIdx = clang_createIndex(0, 1);
- if (getenv("CINDEXTEST_EDITING")) {
- unsigned I, Repeats = 5;
- TU = clang_parseTranslationUnit(CIdx, 0,
- argv + num_unsaved_files + 2,
- argc - num_unsaved_files - 2,
- 0, 0, getDefaultParsingOptions());
- if (!TU) {
- fprintf(stderr, "Unable to load translation unit!\n");
+ CIdx = clang_createIndex(0, 0);
+
+ if (getenv("CINDEXTEST_EDITING"))
+ Repeats = 5;
+
+ TU = clang_parseTranslationUnit(CIdx, 0,
+ argv + num_unsaved_files + 2,
+ argc - num_unsaved_files - 2,
+ 0, 0, getDefaultParsingOptions());
+ if (!TU) {
+ fprintf(stderr, "Unable to load translation unit!\n");
+ return 1;
+ }
+
+ if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) {
+ fprintf(stderr, "Unable to reparse translation init!\n");
+ return 1;
+ }
+
+ 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");
return 1;
}
- for (I = 0; I != Repeats; ++I) {
- results = clang_codeCompleteAt(TU, filename, line, column,
- unsaved_files, num_unsaved_files,
- clang_defaultCodeCompleteOptions());
- if (!results) {
- fprintf(stderr, "Unable to perform code completion!\n");
- return 1;
- }
- if (I != Repeats-1)
- clang_disposeCodeCompleteResults(results);
- }
- } else
- results = clang_codeComplete(CIdx,
- argv[argc - 1], argc - num_unsaved_files - 3,
- argv + num_unsaved_files + 2,
- num_unsaved_files, unsaved_files,
- filename, line, column);
+ if (I != Repeats-1)
+ clang_disposeCodeCompleteResults(results);
+ }
if (results) {
unsigned i, n = results->NumResults;
@@ -1048,7 +1108,9 @@ int inspect_cursor_at(int argc, const char **argv) {
CXCursor Cursor;
CursorSourceLocation *Locations = 0;
unsigned NumLocations = 0, Loc;
-
+ unsigned Repeats = 1;
+ unsigned I;
+
/* Count the number of locations. */
while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
++NumLocations;
@@ -1069,30 +1131,48 @@ int inspect_cursor_at(int argc, const char **argv) {
&num_unsaved_files))
return -1;
- CIdx = clang_createIndex(0, 1);
- TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 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);
+ TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
+ argv + num_unsaved_files + 1 + NumLocations,
argc - num_unsaved_files - 2 - NumLocations,
- argv + num_unsaved_files + 1 + NumLocations,
- num_unsaved_files,
- unsaved_files);
+ unsaved_files,
+ Repeats > 1? 0 : num_unsaved_files,
+ getDefaultParsingOptions());
+
if (!TU) {
fprintf(stderr, "unable to parse input\n");
return -1;
}
- for (Loc = 0; Loc < NumLocations; ++Loc) {
- CXFile file = clang_getFile(TU, Locations[Loc].filename);
- if (!file)
- continue;
+ for (I = 0; I != Repeats; ++I) {
+ if (Repeats > 1 &&
+ clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
+ clang_defaultReparseOptions(TU))) {
+ clang_disposeTranslationUnit(TU);
+ return 1;
+ }
+
+ for (Loc = 0; Loc < NumLocations; ++Loc) {
+ CXFile file = clang_getFile(TU, Locations[Loc].filename);
+ if (!file)
+ continue;
- Cursor = clang_getCursor(TU,
- clang_getLocation(TU, file, Locations[Loc].line,
- Locations[Loc].column));
- PrintCursor(Cursor);
- printf("\n");
- free(Locations[Loc].filename);
+ Cursor = clang_getCursor(TU,
+ clang_getLocation(TU, file, Locations[Loc].line,
+ Locations[Loc].column));
+ if (I + 1 == Repeats) {
+ PrintCursor(Cursor);
+ printf("\n");
+ free(Locations[Loc].filename);
+ }
+ }
}
-
+
PrintDiagnostics(TU);
clang_disposeTranslationUnit(TU);
clang_disposeIndex(CIdx);
@@ -1182,10 +1262,10 @@ int perform_token_annotation(int argc, const char **argv) {
case CXToken_Literal: kind = "Literal"; break;
case CXToken_Comment: kind = "Comment"; break;
}
- clang_getInstantiationLocation(clang_getRangeStart(extent),
- 0, &start_line, &start_column, 0);
- clang_getInstantiationLocation(clang_getRangeEnd(extent),
- 0, &end_line, &end_column, 0);
+ clang_getSpellingLocation(clang_getRangeStart(extent),
+ 0, &start_line, &start_column, 0);
+ clang_getSpellingLocation(clang_getRangeEnd(extent),
+ 0, &end_line, &end_column, 0);
printf("%s: \"%s\" ", kind, clang_getCString(spelling));
PrintExtent(stdout, start_line, start_column, end_line, end_column);
if (!clang_isInvalid(cursors[i].kind)) {
@@ -1195,6 +1275,7 @@ int perform_token_annotation(int argc, const char **argv) {
printf("\n");
}
free(cursors);
+ clang_disposeTokens(TU, tokens, num_tokens);
teardown:
PrintDiagnostics(TU);
@@ -1253,8 +1334,8 @@ int print_usrs(const char **I, const char **E) {
return not_usr("<class USR>", I[2]);
else {
CXString x;
- x.Spelling = I[2];
- x.MustFreeString = 0;
+ x.data = (void*) I[2];
+ x.private_flags = 0;
print_usr(clang_constructUSR_ObjCIvar(I[1], x));
}
@@ -1280,8 +1361,8 @@ int print_usrs(const char **I, const char **E) {
return not_usr("<class USR>", I[3]);
else {
CXString x;
- x.Spelling = I[3];
- x.MustFreeString = 0;
+ x.data = (void*) I[3];
+ x.private_flags = 0;
print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
}
I += 4;
@@ -1310,8 +1391,8 @@ int print_usrs(const char **I, const char **E) {
return not_usr("<class USR>", I[2]);
else {
CXString x;
- x.Spelling = I[2];
- x.MustFreeString = 0;
+ x.data = (void*) I[2];
+ x.private_flags = 0;
print_usr(clang_constructUSR_ObjCProperty(I[1], x));
}
I += 3;
@@ -1462,7 +1543,9 @@ static void print_usage(void) {
" scan-function - scan function bodies (non-PCH)\n\n");
}
-int main(int argc, const char **argv) {
+/***/
+
+int cindextest_main(int argc, const char **argv) {
clang_enableStackTraces();
if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
return perform_code_completion(argc, argv, 0);
@@ -1522,3 +1605,31 @@ int main(int argc, const char **argv) {
print_usage();
return 1;
}
+
+/***/
+
+/* 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;
+void thread_runner(void *client_data_v) {
+ thread_info *client_data = client_data_v;
+ client_data->result = cindextest_main(client_data->argc, client_data->argv);
+}
+
+int main(int argc, const char **argv) {
+ thread_info client_data;
+
+ if (getenv("CINDEXTEST_NOTHREADS"))
+ return cindextest_main(argc, argv);
+
+ client_data.argc = argc;
+ client_data.argv = argv;
+ clang_executeOnThread(thread_runner, &client_data, 0);
+ return client_data.result;
+}
diff --git a/tools/driver/CMakeLists.txt b/tools/driver/CMakeLists.txt
index ec6e9c6e8026..6dc47d656cf9 100644
--- a/tools/driver/CMakeLists.txt
+++ b/tools/driver/CMakeLists.txt
@@ -1,20 +1,20 @@
-set(LLVM_NO_RTTI 1)
-
set( LLVM_USED_LIBS
- clangFrontendTool
- clangFrontend
- clangDriver
- clangSerialization
- clangCodeGen
- clangParse
- clangSema
- clangChecker
+ clangAST
clangAnalysis
+ clangBasic
+ clangCodeGen
+ clangDriver
+ clangFrontend
+ clangFrontendTool
clangIndex
- clangRewrite
- clangAST
clangLex
- clangBasic
+ clangParse
+ clangRewrite
+ clangSema
+ clangSerialization
+ clangStaticAnalyzerFrontend
+ clangStaticAnalyzerCheckers
+ clangStaticAnalyzerCore
)
set( LLVM_LINK_COMPONENTS
@@ -35,20 +35,22 @@ add_clang_executable(clang
if(UNIX)
set(CLANGXX_LINK_OR_COPY create_symlink)
- set(CLANGXX_DESTDIR $ENV{DESTDIR}/)
else()
set(CLANGXX_LINK_OR_COPY copy)
endif()
# Create the clang++ symlink in the build directory.
+set(clang_pp "${LLVM_BINARY_DIR}/bin/${CMAKE_CFG_INTDIR}/clang++${CMAKE_EXECUTABLE_SUFFIX}")
add_custom_target(clang++ ALL
${CMAKE_COMMAND} -E ${CLANGXX_LINK_OR_COPY}
"${LLVM_BINARY_DIR}/bin/${CMAKE_CFG_INTDIR}/clang${CMAKE_EXECUTABLE_SUFFIX}"
- "${LLVM_BINARY_DIR}/bin/${CMAKE_CFG_INTDIR}/clang++${CMAKE_EXECUTABLE_SUFFIX}"
+ "${clang_pp}"
DEPENDS clang)
+set_property(DIRECTORY APPEND
+ PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${clang_pp})
install(TARGETS clang
RUNTIME DESTINATION bin)
# Create the clang++ symlink at installation time.
-install(CODE "execute_process(COMMAND \"${CMAKE_COMMAND}\" -E ${CLANGXX_LINK_OR_COPY} \"${CMAKE_INSTALL_PREFIX}/bin/clang${CMAKE_EXECUTABLE_SUFFIX}\" \"${CLANGXX_DESTDIR}${CMAKE_INSTALL_PREFIX}/bin/clang++${CMAKE_EXECUTABLE_SUFFIX}\")")
+install(SCRIPT clang_symlink.cmake -DCMAKE_INSTALL_PREFIX=\"${CMAKE_INSTALL_PREFIX}\")
diff --git a/tools/driver/Makefile b/tools/driver/Makefile
index 447f0e4eb06b..d96f9505ffe5 100644
--- a/tools/driver/Makefile
+++ b/tools/driver/Makefile
@@ -17,6 +17,16 @@ else
endif
endif
+# We don't currently expect production Clang builds to be interested in
+# plugins. This is important for startup performance.
+ifdef CLANG_IS_PRODUCTION
+TOOL_NO_EXPORTS := 1
+endif
+
+ifdef CLANG_ORDER_FILE
+TOOL_ORDER_FILE := $(CLANG_ORDER_FILE)
+endif
+
# Include tool version information on OS X.
TOOL_INFO_PLIST := Info.plist
@@ -29,7 +39,9 @@ LINK_COMPONENTS := $(TARGETS_TO_BUILD) asmparser bitreader bitwriter codegen \
ipo selectiondag
USEDLIBS = clangFrontendTool.a clangFrontend.a clangDriver.a \
clangSerialization.a clangCodeGen.a clangParse.a clangSema.a \
- clangChecker.a clangAnalysis.a clangIndex.a clangRewrite.a \
+ clangStaticAnalyzerFrontend.a clangStaticAnalyzerCheckers.a \
+ clangStaticAnalyzerCore.a \
+ clangAnalysis.a clangIndex.a clangRewrite.a \
clangAST.a clangLex.a clangBasic.a
include $(CLANG_LEVEL)/Makefile
diff --git a/tools/driver/cc1_main.cpp b/tools/driver/cc1_main.cpp
index de5e8bf043bf..7fb394fa5b01 100644
--- a/tools/driver/cc1_main.cpp
+++ b/tools/driver/cc1_main.cpp
@@ -24,7 +24,6 @@
#include "clang/Frontend/TextDiagnosticBuffer.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/FrontendTool/Utils.h"
-#include "llvm/LLVMContext.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/ManagedStatic.h"
@@ -116,13 +115,12 @@ static int cc1_test(Diagnostic &Diags,
int cc1_main(const char **ArgBegin, const char **ArgEnd,
const char *Argv0, void *MainAddr) {
llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance());
-
- Clang->setLLVMContext(new llvm::LLVMContext());
+ llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
// Run clang -cc1 test.
if (ArgBegin != ArgEnd && llvm::StringRef(ArgBegin[0]) == "-cc1test") {
- Diagnostic Diags(new TextDiagnosticPrinter(llvm::errs(),
- DiagnosticOptions()));
+ Diagnostic Diags(DiagID, new TextDiagnosticPrinter(llvm::errs(),
+ DiagnosticOptions()));
return cc1_test(Diags, ArgBegin + 1, ArgEnd);
}
@@ -134,7 +132,7 @@ int cc1_main(const char **ArgBegin, const char **ArgEnd,
// Buffer diagnostics from argument parsing so that we can output them using a
// well formed diagnostic object.
TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer;
- Diagnostic Diags(DiagsBuffer);
+ Diagnostic Diags(DiagID, DiagsBuffer);
CompilerInvocation::CreateFromArgs(Clang->getInvocation(), ArgBegin, ArgEnd,
Diags);
diff --git a/tools/driver/cc1as_main.cpp b/tools/driver/cc1as_main.cpp
index 5bce70cb2a1b..1d544f3d3c9d 100644
--- a/tools/driver/cc1as_main.cpp
+++ b/tools/driver/cc1as_main.cpp
@@ -39,12 +39,16 @@
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/Timer.h"
#include "llvm/Support/raw_ostream.h"
-#include "llvm/System/Host.h"
-#include "llvm/System/Path.h"
-#include "llvm/System/Signals.h"
+#include "llvm/Support/Host.h"
+#include "llvm/Support/Path.h"
+#include "llvm/Support/Signals.h"
+#include "llvm/Support/system_error.h"
#include "llvm/Target/TargetAsmBackend.h"
+#include "llvm/Target/TargetAsmInfo.h"
#include "llvm/Target/TargetAsmParser.h"
#include "llvm/Target/TargetData.h"
+#include "llvm/Target/TargetLowering.h"
+#include "llvm/Target/TargetLoweringObjectFile.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetRegistry.h"
#include "llvm/Target/TargetSelect.h"
@@ -97,6 +101,7 @@ struct AssemblerInvocation {
/// @{
unsigned RelaxAll : 1;
+ unsigned NoExecStack : 1;
/// @}
@@ -111,6 +116,7 @@ public:
ShowInst = 0;
ShowEncoding = 0;
RelaxAll = 0;
+ NoExecStack = 0;
}
static void CreateFromArgs(AssemblerInvocation &Res, const char **ArgBegin,
@@ -189,6 +195,7 @@ void AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,
// Assemble Options
Opts.RelaxAll = Args->hasArg(OPT_relax_all);
+ Opts.NoExecStack = Args->hasArg(OPT_no_exec_stack);
}
static formatted_raw_ostream *GetOutputStream(AssemblerInvocation &Opts,
@@ -224,11 +231,13 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts, Diagnostic &Diags) {
return false;
}
- MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(Opts.InputFile, &Error);
- if (Buffer == 0) {
+ OwningPtr<MemoryBuffer> BufferPtr;
+ if (error_code ec = MemoryBuffer::getFileOrSTDIN(Opts.InputFile, BufferPtr)) {
+ Error = ec.message();
Diags.Report(diag::err_fe_error_reading) << Opts.InputFile;
return false;
}
+ MemoryBuffer *Buffer = BufferPtr.take();
SourceMgr SrcMgr;
@@ -242,7 +251,6 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts, Diagnostic &Diags) {
OwningPtr<MCAsmInfo> MAI(TheTarget->createAsmInfo(Opts.Triple));
assert(MAI && "Unable to create target asm info!");
- MCContext Ctx(*MAI);
bool IsBinary = Opts.OutputType == AssemblerInvocation::FT_Obj;
formatted_raw_ostream *Out = GetOutputStream(Opts, Diags, IsBinary);
if (!Out)
@@ -255,16 +263,28 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts, Diagnostic &Diags) {
return false;
}
+ const TargetAsmInfo *tai = new TargetAsmInfo(*TM);
+ MCContext Ctx(*MAI, tai);
+
OwningPtr<MCStreamer> Str;
+ const TargetLoweringObjectFile &TLOF =
+ TM->getTargetLowering()->getObjFileLowering();
+ const_cast<TargetLoweringObjectFile&>(TLOF).Initialize(Ctx, *TM);
+
+ // FIXME: There is a bit of code duplication with addPassesToEmitFile.
if (Opts.OutputType == AssemblerInvocation::FT_Asm) {
MCInstPrinter *IP =
TheTarget->createMCInstPrinter(Opts.OutputAsmVariant, *MAI);
MCCodeEmitter *CE = 0;
- if (Opts.ShowEncoding)
+ TargetAsmBackend *TAB = 0;
+ if (Opts.ShowEncoding) {
CE = TheTarget->createCodeEmitter(*TM, Ctx);
- Str.reset(createAsmStreamer(Ctx, *Out,TM->getTargetData()->isLittleEndian(),
- /*asmverbose*/true, IP, CE, Opts.ShowInst));
+ TAB = TheTarget->createAsmBackend(Opts.Triple);
+ }
+ Str.reset(TheTarget->createAsmStreamer(Ctx, *Out, /*asmverbose*/true,
+ /*useLoc*/ true, IP, CE, TAB,
+ Opts.ShowInst));
} else if (Opts.OutputType == AssemblerInvocation::FT_Null) {
Str.reset(createNullStreamer(Ctx));
} else {
@@ -273,7 +293,9 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts, Diagnostic &Diags) {
MCCodeEmitter *CE = TheTarget->createCodeEmitter(*TM, Ctx);
TargetAsmBackend *TAB = TheTarget->createAsmBackend(Opts.Triple);
Str.reset(TheTarget->createObjectStreamer(Opts.Triple, Ctx, *TAB, *Out,
- CE, Opts.RelaxAll));
+ CE, Opts.RelaxAll,
+ Opts.NoExecStack));
+ Str.get()->InitSections();
}
OwningPtr<MCAsmParser> Parser(createMCAsmParser(*TheTarget, SrcMgr, Ctx,
@@ -325,7 +347,8 @@ int cc1as_main(const char **ArgBegin, const char **ArgEnd,
TextDiagnosticPrinter *DiagClient
= new TextDiagnosticPrinter(errs(), DiagnosticOptions());
DiagClient->setPrefix("clang -cc1as");
- Diagnostic Diags(DiagClient);
+ llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
+ Diagnostic Diags(DiagID, DiagClient);
// Set an error handler, so that any LLVM backend diagnostics go through our
// error handler.
@@ -366,7 +389,7 @@ int cc1as_main(const char **ArgBegin, const char **ArgEnd,
// Execute the invocation, unless there were parsing errors.
bool Success = false;
- if (!Diags.getNumErrors())
+ if (!Diags.hasErrorOccurred())
Success = ExecuteAssembler(Asm, Diags);
// If any timers were active but haven't been destroyed yet, print their
diff --git a/tools/driver/clang_symlink.cmake b/tools/driver/clang_symlink.cmake
new file mode 100644
index 000000000000..40a74824cf2b
--- /dev/null
+++ b/tools/driver/clang_symlink.cmake
@@ -0,0 +1,20 @@
+# We need to execute this script at installation time because the
+# DESTDIR environment variable may be unset at configuration time.
+# See PR8397.
+
+if(UNIX)
+ set(CLANGXX_LINK_OR_COPY create_symlink)
+ set(CLANGXX_DESTDIR $ENV{DESTDIR})
+else()
+ set(CLANGXX_LINK_OR_COPY copy)
+endif()
+
+set(bindir "${CLANGXX_DESTDIR}${CMAKE_INSTALL_PREFIX}/bin/")
+set(clang "clang${CMAKE_EXECUTABLE_SUFFIX}")
+set(clangxx "clang++${CMAKE_EXECUTABLE_SUFFIX}")
+
+message("Creating clang++ executable based on ${clang}")
+
+execute_process(
+ COMMAND "${CMAKE_COMMAND}" -E ${CLANGXX_LINK_OR_COPY} "${clang}" "${clangxx}"
+ WORKING_DIRECTORY "${bindir}")
diff --git a/tools/driver/driver.cpp b/tools/driver/driver.cpp
index c058ece0dc9b..0b5d2c97a4e7 100644
--- a/tools/driver/driver.cpp
+++ b/tools/driver/driver.cpp
@@ -23,16 +23,19 @@
#include "llvm/ADT/OwningPtr.h"
#include "llvm/Config/config.h"
#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/FileSystem.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/PrettyStackTrace.h"
#include "llvm/Support/Regex.h"
#include "llvm/Support/Timer.h"
#include "llvm/Support/raw_ostream.h"
-#include "llvm/System/Host.h"
-#include "llvm/System/Path.h"
-#include "llvm/System/Program.h"
-#include "llvm/System/Signals.h"
+#include "llvm/Support/Host.h"
+#include "llvm/Support/Path.h"
+#include "llvm/Support/Program.h"
+#include "llvm/Support/Signals.h"
+#include "llvm/Support/system_error.h"
+#include <cctype>
using namespace clang;
using namespace clang::driver;
@@ -181,8 +184,8 @@ static void ExpandArgsFromBuf(const char *Arg,
llvm::SmallVectorImpl<const char*> &ArgVector,
std::set<std::string> &SavedStrings) {
const char *FName = Arg + 1;
- llvm::MemoryBuffer *MemBuf = llvm::MemoryBuffer::getFile(FName);
- if (!MemBuf) {
+ llvm::OwningPtr<llvm::MemoryBuffer> MemBuf;
+ if (llvm::MemoryBuffer::getFile(FName, MemBuf)) {
ArgVector.push_back(SaveStringInSet(SavedStrings, Arg));
return;
}
@@ -233,7 +236,6 @@ static void ExpandArgsFromBuf(const char *Arg,
}
CurArg.push_back(*P);
}
- delete MemBuf;
}
static void ExpandArgv(int argc, const char **argv,
@@ -287,8 +289,9 @@ int main(int argc_, const char **argv_) {
TextDiagnosticPrinter *DiagClient
= new TextDiagnosticPrinter(llvm::errs(), DiagnosticOptions());
- DiagClient->setPrefix(Path.getBasename());
- Diagnostic Diags(DiagClient);
+ DiagClient->setPrefix(llvm::sys::path::stem(Path.str()));
+ llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
+ Diagnostic Diags(DiagID, DiagClient);
#ifdef CLANG_IS_PRODUCTION
const bool IsProduction = true;
@@ -308,19 +311,22 @@ int main(int argc_, const char **argv_) {
// Attempt to find the original path used to invoke the driver, to determine
// the installed path. We do this manually, because we want to support that
// path being a symlink.
- llvm::sys::Path InstalledPath(argv[0]);
-
- // Do a PATH lookup, if there are no directory components.
- if (InstalledPath.getLast() == InstalledPath.str()) {
- llvm::sys::Path Tmp =
- llvm::sys::Program::FindProgramByName(InstalledPath.getLast());
- if (!Tmp.empty())
- InstalledPath = Tmp;
+ {
+ llvm::SmallString<128> InstalledPath(argv[0]);
+
+ // Do a PATH lookup, if there are no directory components.
+ if (llvm::sys::path::filename(InstalledPath) == InstalledPath) {
+ llvm::sys::Path Tmp = llvm::sys::Program::FindProgramByName(
+ llvm::sys::path::filename(InstalledPath.str()));
+ if (!Tmp.empty())
+ InstalledPath = Tmp.str();
+ }
+ llvm::sys::fs::make_absolute(InstalledPath);
+ InstalledPath = llvm::sys::path::parent_path(InstalledPath);
+ bool exists;
+ if (!llvm::sys::fs::exists(InstalledPath.str(), exists) && exists)
+ TheDriver.setInstalledDir(InstalledPath);
}
- InstalledPath.makeAbsolute();
- InstalledPath.eraseComponent();
- if (InstalledPath.exists())
- TheDriver.setInstalledDir(InstalledPath.str());
// Check for ".*++" or ".*++-[^-]*" to determine if we are a C++
// compiler. This matches things like "c++", "clang++", and "clang++-1.1".
@@ -330,11 +336,10 @@ int main(int argc_, const char **argv_) {
//
// We use *argv instead of argv[0] to work around a bogus g++ warning.
const char *progname = argv_[0];
- std::string ProgName(llvm::sys::Path(progname).getBasename());
+ std::string ProgName(llvm::sys::path::stem(progname));
if (llvm::StringRef(ProgName).endswith("++") ||
llvm::StringRef(ProgName).rsplit('-').first.endswith("++")) {
TheDriver.CCCIsCXX = true;
- TheDriver.CCCGenericGCCName = "g++";
}
// Handle CC_PRINT_OPTIONS and CC_PRINT_OPTIONS_FILE.
@@ -342,6 +347,11 @@ int main(int argc_, const char **argv_) {
if (TheDriver.CCPrintOptions)
TheDriver.CCPrintOptionsFilename = ::getenv("CC_PRINT_OPTIONS_FILE");
+ // Handle CC_PRINT_HEADERS and CC_PRINT_HEADERS_FILE.
+ TheDriver.CCPrintHeaders = !!::getenv("CC_PRINT_HEADERS");
+ if (TheDriver.CCPrintHeaders)
+ TheDriver.CCPrintHeadersFilename = ::getenv("CC_PRINT_HEADERS_FILE");
+
// Handle QA_OVERRIDE_GCC3_OPTIONS and CCC_ADD_ARGS, used for editing a
// command line behind the scenes.
if (const char *OverrideStr = ::getenv("QA_OVERRIDE_GCC3_OPTIONS")) {
diff --git a/tools/libclang/CIndex.cpp b/tools/libclang/CIndex.cpp
index 5117f2c16424..e2882953c080 100644
--- a/tools/libclang/CIndex.cpp
+++ b/tools/libclang/CIndex.cpp
@@ -14,6 +14,8 @@
#include "CIndexer.h"
#include "CXCursor.h"
+#include "CXTranslationUnit.h"
+#include "CXString.h"
#include "CXType.h"
#include "CXSourceLocation.h"
#include "CIndexDiagnostic.h"
@@ -30,102 +32,32 @@
#include "clang/Lex/Lexer.h"
#include "clang/Lex/PreprocessingRecord.h"
#include "clang/Lex/Preprocessor.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/Optional.h"
+#include "clang/Analysis/Support/SaveAndRestore.h"
#include "llvm/Support/CrashRecoveryContext.h"
+#include "llvm/Support/PrettyStackTrace.h"
#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/Timer.h"
-#include "llvm/System/Program.h"
-#include "llvm/System/Signals.h"
-
-// Needed to define L_TMPNAM on some systems.
-#include <cstdio>
+#include "llvm/Support/Mutex.h"
+#include "llvm/Support/Program.h"
+#include "llvm/Support/Signals.h"
+#include "llvm/Support/Threading.h"
+#include "llvm/Support/Compiler.h"
using namespace clang;
using namespace clang::cxcursor;
using namespace clang::cxstring;
-//===----------------------------------------------------------------------===//
-// Crash Reporting.
-//===----------------------------------------------------------------------===//
-
-#ifdef USE_CRASHTRACER
-#include "clang/Analysis/Support/SaveAndRestore.h"
-// Integrate with crash reporter.
-static const char *__crashreporter_info__ = 0;
-asm(".desc ___crashreporter_info__, 0x10");
-#define NUM_CRASH_STRINGS 32
-static unsigned crashtracer_counter = 0;
-static unsigned crashtracer_counter_id[NUM_CRASH_STRINGS] = { 0 };
-static const char *crashtracer_strings[NUM_CRASH_STRINGS] = { 0 };
-static const char *agg_crashtracer_strings[NUM_CRASH_STRINGS] = { 0 };
-
-static unsigned SetCrashTracerInfo(const char *str,
- llvm::SmallString<1024> &AggStr) {
-
- unsigned slot = 0;
- while (crashtracer_strings[slot]) {
- if (++slot == NUM_CRASH_STRINGS)
- slot = 0;
- }
- crashtracer_strings[slot] = str;
- crashtracer_counter_id[slot] = ++crashtracer_counter;
-
- // We need to create an aggregate string because multiple threads
- // may be in this method at one time. The crash reporter string
- // will attempt to overapproximate the set of in-flight invocations
- // of this function. Race conditions can still cause this goal
- // to not be achieved.
- {
- llvm::raw_svector_ostream Out(AggStr);
- for (unsigned i = 0; i < NUM_CRASH_STRINGS; ++i)
- if (crashtracer_strings[i]) Out << crashtracer_strings[i] << '\n';
- }
- __crashreporter_info__ = agg_crashtracer_strings[slot] = AggStr.c_str();
- return slot;
-}
-
-static void ResetCrashTracerInfo(unsigned slot) {
- unsigned max_slot = 0;
- unsigned max_value = 0;
-
- crashtracer_strings[slot] = agg_crashtracer_strings[slot] = 0;
-
- for (unsigned i = 0 ; i < NUM_CRASH_STRINGS; ++i)
- if (agg_crashtracer_strings[i] &&
- crashtracer_counter_id[i] > max_value) {
- max_slot = i;
- max_value = crashtracer_counter_id[i];
- }
-
- __crashreporter_info__ = agg_crashtracer_strings[max_slot];
-}
-
-namespace {
-class ArgsCrashTracerInfo {
- llvm::SmallString<1024> CrashString;
- llvm::SmallString<1024> AggregateString;
- unsigned crashtracerSlot;
-public:
- ArgsCrashTracerInfo(llvm::SmallVectorImpl<const char*> &Args)
- : crashtracerSlot(0)
- {
- {
- llvm::raw_svector_ostream Out(CrashString);
- Out << "ClangCIndex [" << getClangFullVersion() << "]"
- << "[createTranslationUnitFromSourceFile]: clang";
- for (llvm::SmallVectorImpl<const char*>::iterator I=Args.begin(),
- E=Args.end(); I!=E; ++I)
- Out << ' ' << *I;
- }
- crashtracerSlot = SetCrashTracerInfo(CrashString.c_str(),
- AggregateString);
- }
-
- ~ArgsCrashTracerInfo() {
- ResetCrashTracerInfo(crashtracerSlot);
- }
-};
+static CXTranslationUnit MakeCXTranslationUnit(ASTUnit *TU) {
+ if (!TU)
+ return 0;
+ CXTranslationUnit D = new CXTranslationUnitImpl();
+ D->TUData = TU;
+ D->StringPool = createCXStringPool();
+ return D;
}
-#endif
/// \brief The result of comparing two source ranges.
enum RangeComparisonResult {
@@ -181,8 +113,9 @@ CXSourceRange cxloc::translateSourceRange(const SourceManager &SM,
const CharSourceRange &R) {
// We want the last character in this location, so we will adjust the
// location accordingly.
- // FIXME: How do do this with a macro instantiation location?
SourceLocation EndLoc = R.getEnd();
+ if (EndLoc.isValid() && EndLoc.isMacroID())
+ EndLoc = SM.getSpellingLoc(EndLoc);
if (R.isTokenRange() && !EndLoc.isInvalid() && EndLoc.isFileID()) {
unsigned Length = Lexer::MeasureTokenLength(EndLoc, SM, LangOpts);
EndLoc = EndLoc.getFileLocWithOffset(Length);
@@ -199,14 +132,41 @@ CXSourceRange cxloc::translateSourceRange(const SourceManager &SM,
//===----------------------------------------------------------------------===//
namespace {
+
+class VisitorJob {
+public:
+ enum Kind { DeclVisitKind, StmtVisitKind, MemberExprPartsKind,
+ TypeLocVisitKind, OverloadExprPartsKind,
+ DeclRefExprPartsKind, LabelRefVisitKind,
+ ExplicitTemplateArgsVisitKind,
+ NestedNameSpecifierVisitKind,
+ DeclarationNameInfoVisitKind,
+ MemberRefVisitKind, SizeOfPackExprPartsKind };
+protected:
+ void *data[3];
+ CXCursor parent;
+ Kind K;
+ VisitorJob(CXCursor C, Kind k, void *d1, void *d2 = 0, void *d3 = 0)
+ : parent(C), K(k) {
+ data[0] = d1;
+ data[1] = d2;
+ data[2] = d3;
+ }
+public:
+ Kind getKind() const { return K; }
+ const CXCursor &getParent() const { return parent; }
+ static bool classof(VisitorJob *VJ) { return true; }
+};
+
+typedef llvm::SmallVector<VisitorJob, 10> VisitorWorkList;
// Cursor visitor.
class CursorVisitor : public DeclVisitor<CursorVisitor, bool>,
- public TypeLocVisitor<CursorVisitor, bool>,
- public StmtVisitor<CursorVisitor, bool>
+ public TypeLocVisitor<CursorVisitor, bool>
{
/// \brief The translation unit we are traversing.
- ASTUnit *TU;
+ CXTranslationUnit TU;
+ ASTUnit *AU;
/// \brief The parent cursor whose children we are traversing.
CXCursor Parent;
@@ -230,9 +190,17 @@ class CursorVisitor : public DeclVisitor<CursorVisitor, bool>,
/// its search.
SourceRange RegionOfInterest;
+ // FIXME: Eventually remove. This part of a hack to support proper
+ // iteration over all Decls contained lexically within an ObjC container.
+ DeclContext::decl_iterator *DI_current;
+ DeclContext::decl_iterator DE_current;
+
+ // Cache of pre-allocated worklists for data-recursion walk of Stmts.
+ llvm::SmallVector<VisitorWorkList*, 5> WorkListFreeList;
+ llvm::SmallVector<VisitorWorkList*, 5> WorkListCache;
+
using DeclVisitor<CursorVisitor, bool>::Visit;
using TypeLocVisitor<CursorVisitor, bool>::Visit;
- using StmtVisitor<CursorVisitor, bool>::Visit;
/// \brief Determine whether this particular source range comes before, comes
/// after, or overlaps the region of interest.
@@ -262,11 +230,14 @@ class CursorVisitor : public DeclVisitor<CursorVisitor, bool>,
};
public:
- CursorVisitor(ASTUnit *TU, CXCursorVisitor Visitor, CXClientData ClientData,
+ CursorVisitor(CXTranslationUnit TU, CXCursorVisitor Visitor,
+ CXClientData ClientData,
unsigned MaxPCHLevel,
SourceRange RegionOfInterest = SourceRange())
- : TU(TU), Visitor(Visitor), ClientData(ClientData),
- MaxPCHLevel(MaxPCHLevel), RegionOfInterest(RegionOfInterest)
+ : TU(TU), AU(static_cast<ASTUnit*>(TU->TUData)),
+ Visitor(Visitor), ClientData(ClientData),
+ MaxPCHLevel(MaxPCHLevel), RegionOfInterest(RegionOfInterest),
+ DI_current(0)
{
Parent.kind = CXCursor_NoDeclFound;
Parent.data[0] = 0;
@@ -275,6 +246,17 @@ public:
StmtParent = 0;
}
+ ~CursorVisitor() {
+ // Free the pre-allocated worklists for data-recursion.
+ for (llvm::SmallVectorImpl<VisitorWorkList*>::iterator
+ I = WorkListCache.begin(), E = WorkListCache.end(); I != E; ++I) {
+ delete *I;
+ }
+ }
+
+ ASTUnit *getASTUnit() const { return static_cast<ASTUnit*>(TU->TUData); }
+ CXTranslationUnit getTU() const { return TU; }
+
bool Visit(CXCursor Cursor, bool CheckedRegionOfInterest = false);
std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
@@ -286,6 +268,7 @@ public:
bool VisitAttributes(Decl *D);
bool VisitBlockDecl(BlockDecl *B);
bool VisitCXXRecordDecl(CXXRecordDecl *D);
+ llvm::Optional<bool> shouldVisitCursor(CXCursor C);
bool VisitDeclContext(DeclContext *DC);
bool VisitTranslationUnitDecl(TranslationUnitDecl *D);
bool VisitTypedefDecl(TypedefDecl *D);
@@ -312,11 +295,10 @@ public:
bool VisitObjCImplDecl(ObjCImplDecl *D);
bool VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
bool VisitObjCImplementationDecl(ObjCImplementationDecl *D);
- // FIXME: ObjCPropertyDecl requires TypeSourceInfo, getter/setter locations,
- // etc.
// FIXME: ObjCCompatibleAliasDecl requires aliased-class locations.
bool VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
bool VisitObjCClassDecl(ObjCClassDecl *D);
+ bool VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PD);
bool VisitLinkageSpecDecl(LinkageSpecDecl *D);
bool VisitNamespaceDecl(NamespaceDecl *D);
bool VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
@@ -344,6 +326,7 @@ public:
bool VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL);
bool VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL);
bool VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL);
+ bool VisitParenTypeLoc(ParenTypeLoc TL);
bool VisitPointerTypeLoc(PointerTypeLoc TL);
bool VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL);
bool VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL);
@@ -355,55 +338,24 @@ public:
// FIXME: Implement visitors here when the unimplemented TypeLocs get
// implemented
bool VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL);
+ bool VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL);
bool VisitTypeOfTypeLoc(TypeOfTypeLoc TL);
- // Statement visitors
- bool VisitStmt(Stmt *S);
- bool VisitDeclStmt(DeclStmt *S);
- // FIXME: LabelStmt label?
- bool VisitIfStmt(IfStmt *S);
- bool VisitSwitchStmt(SwitchStmt *S);
- bool VisitCaseStmt(CaseStmt *S);
- bool VisitWhileStmt(WhileStmt *S);
- bool VisitForStmt(ForStmt *S);
-// bool VisitSwitchCase(SwitchCase *S);
-
- // Expression visitors
- bool VisitDeclRefExpr(DeclRefExpr *E);
- bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E);
- bool VisitBlockExpr(BlockExpr *B);
- bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
- bool VisitExplicitCastExpr(ExplicitCastExpr *E);
- bool VisitObjCMessageExpr(ObjCMessageExpr *E);
- bool VisitObjCEncodeExpr(ObjCEncodeExpr *E);
- bool VisitOffsetOfExpr(OffsetOfExpr *E);
- bool VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
- bool VisitMemberExpr(MemberExpr *E);
- // FIXME: AddrLabelExpr (once we have cursors for labels)
- bool VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
- bool VisitVAArgExpr(VAArgExpr *E);
- // FIXME: InitListExpr (for the designators)
- // FIXME: DesignatedInitExpr
- bool VisitCXXTypeidExpr(CXXTypeidExpr *E);
- bool VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { return false; }
- // FIXME: CXXTemporaryObjectExpr has poor source-location information.
- // FIXME: CXXScalarValueInitExpr has poor source-location information.
- // FIXME: CXXNewExpr has poor source-location information
- bool VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E);
- // FIXME: UnaryTypeTraitExpr has poor source-location information.
- bool VisitOverloadExpr(OverloadExpr *E);
- bool VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E);
- // FIXME: CXXUnresolvedConstructExpr has poor source-location information.
- bool VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E);
- bool VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E);
+ // Data-recursive visitor functions.
+ bool IsInRegionOfInterest(CXCursor C);
+ bool RunVisitorWorkList(VisitorWorkList &WL);
+ void EnqueueWorkList(VisitorWorkList &WL, Stmt *S);
+ LLVM_ATTRIBUTE_NOINLINE bool Visit(Stmt *S);
};
} // end anonymous namespace
static SourceRange getRawCursorExtent(CXCursor C);
+static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr);
+
RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) {
- return RangeCompare(TU->getSourceManager(), R, RegionOfInterest);
+ return RangeCompare(AU->getSourceManager(), R, RegionOfInterest);
}
/// \brief Visit the given cursor and, if requested by the visitor,
@@ -455,18 +407,39 @@ bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) {
std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
CursorVisitor::getPreprocessedEntities() {
PreprocessingRecord &PPRec
- = *TU->getPreprocessor().getPreprocessingRecord();
+ = *AU->getPreprocessor().getPreprocessingRecord();
bool OnlyLocalDecls
- = !TU->isMainFileAST() && TU->getOnlyLocalDecls();
+ = !AU->isMainFileAST() && AU->getOnlyLocalDecls();
+
+ if (OnlyLocalDecls && RegionOfInterest.isValid()) {
+ // If we would only look at local declarations but we have a region of
+ // interest, check whether that region of interest is in the main file.
+ // If not, we should traverse all declarations.
+ // FIXME: My kingdom for a proper binary search approach to finding
+ // cursors!
+ std::pair<FileID, unsigned> Location
+ = AU->getSourceManager().getDecomposedInstantiationLoc(
+ RegionOfInterest.getBegin());
+ if (Location.first != AU->getSourceManager().getMainFileID())
+ OnlyLocalDecls = false;
+ }
+
+ PreprocessingRecord::iterator StartEntity, EndEntity;
+ if (OnlyLocalDecls) {
+ StartEntity = AU->pp_entity_begin();
+ EndEntity = AU->pp_entity_end();
+ } else {
+ StartEntity = PPRec.begin();
+ EndEntity = PPRec.end();
+ }
// There is no region of interest; we have to walk everything.
if (RegionOfInterest.isInvalid())
- return std::make_pair(PPRec.begin(OnlyLocalDecls),
- PPRec.end(OnlyLocalDecls));
+ return std::make_pair(StartEntity, EndEntity);
// Find the file in which the region of interest lands.
- SourceManager &SM = TU->getSourceManager();
+ SourceManager &SM = AU->getSourceManager();
std::pair<FileID, unsigned> Begin
= SM.getDecomposedInstantiationLoc(RegionOfInterest.getBegin());
std::pair<FileID, unsigned> End
@@ -474,18 +447,16 @@ CursorVisitor::getPreprocessedEntities() {
// The region of interest spans files; we have to walk everything.
if (Begin.first != End.first)
- return std::make_pair(PPRec.begin(OnlyLocalDecls),
- PPRec.end(OnlyLocalDecls));
+ return std::make_pair(StartEntity, EndEntity);
ASTUnit::PreprocessedEntitiesByFileMap &ByFileMap
- = TU->getPreprocessedEntitiesByFile();
+ = AU->getPreprocessedEntitiesByFile();
if (ByFileMap.empty()) {
// Build the mapping from files to sets of preprocessed entities.
- for (PreprocessingRecord::iterator E = PPRec.begin(OnlyLocalDecls),
- EEnd = PPRec.end(OnlyLocalDecls);
- E != EEnd; ++E) {
+ for (PreprocessingRecord::iterator E = StartEntity; E != EndEntity; ++E) {
std::pair<FileID, unsigned> P
= SM.getDecomposedInstantiationLoc((*E)->getSourceRange().getBegin());
+
ByFileMap[P.first].push_back(*E);
}
}
@@ -495,7 +466,7 @@ CursorVisitor::getPreprocessedEntities() {
}
/// \brief Visit the children of the given cursor.
-///
+///
/// \returns true if the visitation should be aborted, false if it
/// should continue.
bool CursorVisitor::VisitChildren(CXCursor Cursor) {
@@ -520,13 +491,14 @@ bool CursorVisitor::VisitChildren(CXCursor Cursor) {
return Visit(getCursorExpr(Cursor));
if (clang_isTranslationUnit(Cursor.kind)) {
- ASTUnit *CXXUnit = getCursorASTUnit(Cursor);
+ CXTranslationUnit tu = getCursorTU(Cursor);
+ ASTUnit *CXXUnit = static_cast<ASTUnit*>(tu->TUData);
if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() &&
RegionOfInterest.isInvalid()) {
for (ASTUnit::top_level_iterator TL = CXXUnit->top_level_begin(),
TLEnd = CXXUnit->top_level_end();
TL != TLEnd; ++TL) {
- if (Visit(MakeCXCursor(*TL, CXXUnit), true))
+ if (Visit(MakeCXCursor(*TL, tu), true))
return true;
}
} else if (VisitDeclContext(
@@ -540,14 +512,21 @@ bool CursorVisitor::VisitChildren(CXCursor Cursor) {
PreprocessingRecord::iterator E, EEnd;
for (llvm::tie(E, EEnd) = getPreprocessedEntities(); E != EEnd; ++E) {
if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
- if (Visit(MakeMacroInstantiationCursor(MI, CXXUnit)))
+ if (Visit(MakeMacroInstantiationCursor(MI, tu)))
return true;
continue;
}
if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
- if (Visit(MakeMacroDefinitionCursor(MD, CXXUnit)))
+ if (Visit(MakeMacroDefinitionCursor(MD, tu)))
+ return true;
+
+ continue;
+ }
+
+ if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
+ if (Visit(MakeInclusionDirectiveCursor(ID, tu)))
return true;
continue;
@@ -571,40 +550,50 @@ bool CursorVisitor::VisitBlockDecl(BlockDecl *B) {
return false;
}
+llvm::Optional<bool> CursorVisitor::shouldVisitCursor(CXCursor Cursor) {
+ if (RegionOfInterest.isValid()) {
+ SourceRange Range = getFullCursorExtent(Cursor, AU->getSourceManager());
+ if (Range.isInvalid())
+ return llvm::Optional<bool>();
+
+ switch (CompareRegionOfInterest(Range)) {
+ case RangeBefore:
+ // This declaration comes before the region of interest; skip it.
+ return llvm::Optional<bool>();
+
+ case RangeAfter:
+ // This declaration comes after the region of interest; we're done.
+ return false;
+
+ case RangeOverlap:
+ // This declaration overlaps the region of interest; visit it.
+ break;
+ }
+ }
+ return true;
+}
+
bool CursorVisitor::VisitDeclContext(DeclContext *DC) {
- for (DeclContext::decl_iterator
- I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) {
+ DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
+ // FIXME: Eventually remove. This part of a hack to support proper
+ // iteration over all Decls contained lexically within an ObjC container.
+ SaveAndRestore<DeclContext::decl_iterator*> DI_saved(DI_current, &I);
+ SaveAndRestore<DeclContext::decl_iterator> DE_saved(DE_current, E);
+
+ for ( ; I != E; ++I) {
Decl *D = *I;
if (D->getLexicalDeclContext() != DC)
continue;
-
CXCursor Cursor = MakeCXCursor(D, TU);
-
- if (RegionOfInterest.isValid()) {
- SourceRange Range = getRawCursorExtent(Cursor);
- if (Range.isInvalid())
- continue;
-
- switch (CompareRegionOfInterest(Range)) {
- case RangeBefore:
- // This declaration comes before the region of interest; skip it.
- continue;
-
- case RangeAfter:
- // This declaration comes after the region of interest; we're done.
- return false;
-
- case RangeOverlap:
- // This declaration overlaps the region of interest; visit it.
- break;
- }
- }
-
+ const llvm::Optional<bool> &V = shouldVisitCursor(Cursor);
+ if (!V.hasValue())
+ continue;
+ if (!V.getValue())
+ return false;
if (Visit(Cursor, true))
return true;
}
-
return false;
}
@@ -699,11 +688,26 @@ bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) {
return false;
}
+/// \brief Compare two base or member initializers based on their source order.
+static int CompareCXXCtorInitializers(const void* Xp, const void *Yp) {
+ CXXCtorInitializer const * const *X
+ = static_cast<CXXCtorInitializer const * const *>(Xp);
+ CXXCtorInitializer const * const *Y
+ = static_cast<CXXCtorInitializer const * const *>(Yp);
+
+ if ((*X)->getSourceOrder() < (*Y)->getSourceOrder())
+ return -1;
+ else if ((*X)->getSourceOrder() > (*Y)->getSourceOrder())
+ return 1;
+ else
+ return 0;
+}
+
bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) {
if (TypeSourceInfo *TSInfo = ND->getTypeSourceInfo()) {
// Visit the function declaration's syntactic components in the order
// written. This requires a bit of work.
- TypeLoc TL = TSInfo->getTypeLoc();
+ TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens();
FunctionTypeLoc *FTL = dyn_cast<FunctionTypeLoc>(&TL);
// If we have a function declared directly (without the use of a typedef),
@@ -731,9 +735,45 @@ bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) {
// FIXME: Attributes?
}
- if (ND->isThisDeclarationADefinition() &&
- Visit(MakeCXCursor(ND->getBody(), StmtParent, TU)))
- return true;
+ if (ND->isThisDeclarationADefinition()) {
+ if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(ND)) {
+ // Find the initializers that were written in the source.
+ llvm::SmallVector<CXXCtorInitializer *, 4> WrittenInits;
+ for (CXXConstructorDecl::init_iterator I = Constructor->init_begin(),
+ IEnd = Constructor->init_end();
+ I != IEnd; ++I) {
+ if (!(*I)->isWritten())
+ continue;
+
+ WrittenInits.push_back(*I);
+ }
+
+ // Sort the initializers in source order
+ llvm::array_pod_sort(WrittenInits.begin(), WrittenInits.end(),
+ &CompareCXXCtorInitializers);
+
+ // Visit the initializers in source order
+ for (unsigned I = 0, N = WrittenInits.size(); I != N; ++I) {
+ CXXCtorInitializer *Init = WrittenInits[I];
+ if (Init->isAnyMemberInitializer()) {
+ if (Visit(MakeCursorMemberRef(Init->getAnyMember(),
+ Init->getMemberLocation(), TU)))
+ return true;
+ } else if (TypeSourceInfo *BaseInfo = Init->getBaseClassInfo()) {
+ if (Visit(BaseInfo->getTypeLoc()))
+ return true;
+ }
+
+ // Visit the initializer value.
+ if (Expr *Initializer = Init->getInit())
+ if (Visit(MakeCXCursor(Initializer, ND, TU)))
+ return true;
+ }
+ }
+
+ if (Visit(MakeCXCursor(ND->getBody(), StmtParent, TU)))
+ return true;
+ }
return false;
}
@@ -817,8 +857,84 @@ bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) {
return false;
}
+namespace {
+ struct ContainerDeclsSort {
+ SourceManager &SM;
+ ContainerDeclsSort(SourceManager &sm) : SM(sm) {}
+ bool operator()(Decl *A, Decl *B) {
+ SourceLocation L_A = A->getLocStart();
+ SourceLocation L_B = B->getLocStart();
+ assert(L_A.isValid() && L_B.isValid());
+ return SM.isBeforeInTranslationUnit(L_A, L_B);
+ }
+ };
+}
+
bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) {
- return VisitDeclContext(D);
+ // FIXME: Eventually convert back to just 'VisitDeclContext()'. Essentially
+ // an @implementation can lexically contain Decls that are not properly
+ // nested in the AST. When we identify such cases, we need to retrofit
+ // this nesting here.
+ if (!DI_current)
+ return VisitDeclContext(D);
+
+ // Scan the Decls that immediately come after the container
+ // in the current DeclContext. If any fall within the
+ // container's lexical region, stash them into a vector
+ // for later processing.
+ llvm::SmallVector<Decl *, 24> DeclsInContainer;
+ SourceLocation EndLoc = D->getSourceRange().getEnd();
+ SourceManager &SM = AU->getSourceManager();
+ if (EndLoc.isValid()) {
+ DeclContext::decl_iterator next = *DI_current;
+ while (++next != DE_current) {
+ Decl *D_next = *next;
+ if (!D_next)
+ break;
+ SourceLocation L = D_next->getLocStart();
+ if (!L.isValid())
+ break;
+ if (SM.isBeforeInTranslationUnit(L, EndLoc)) {
+ *DI_current = next;
+ DeclsInContainer.push_back(D_next);
+ continue;
+ }
+ break;
+ }
+ }
+
+ // The common case.
+ if (DeclsInContainer.empty())
+ return VisitDeclContext(D);
+
+ // Get all the Decls in the DeclContext, and sort them with the
+ // additional ones we've collected. Then visit them.
+ for (DeclContext::decl_iterator I = D->decls_begin(), E = D->decls_end();
+ I!=E; ++I) {
+ Decl *subDecl = *I;
+ if (!subDecl || subDecl->getLexicalDeclContext() != D ||
+ subDecl->getLocStart().isInvalid())
+ continue;
+ DeclsInContainer.push_back(subDecl);
+ }
+
+ // Now sort the Decls so that they appear in lexical order.
+ std::sort(DeclsInContainer.begin(), DeclsInContainer.end(),
+ ContainerDeclsSort(SM));
+
+ // Now visit the decls.
+ for (llvm::SmallVectorImpl<Decl*>::iterator I = DeclsInContainer.begin(),
+ E = DeclsInContainer.end(); I != E; ++I) {
+ CXCursor Cursor = MakeCXCursor(*I, TU);
+ const llvm::Optional<bool> &V = shouldVisitCursor(Cursor);
+ if (!V.hasValue())
+ continue;
+ if (!V.getValue())
+ return false;
+ if (Visit(Cursor, true))
+ return true;
+ }
+ return false;
}
bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
@@ -846,7 +962,7 @@ bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
}
bool CursorVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *PD) {
- if (Visit(PD->getTypeSourceInfo()->getTypeLoc()))
+ if (PD->getTypeSourceInfo() && Visit(PD->getTypeSourceInfo()->getTypeLoc()))
return true;
// FIXME: This implements a workaround with @property declarations also being
@@ -870,12 +986,12 @@ bool CursorVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *PD) {
// Visit synthesized methods since they will be skipped when visiting
// the @interface.
if (ObjCMethodDecl *MD = prevDecl->getGetterMethodDecl())
- if (MD->isSynthesized())
+ if (MD->isSynthesized() && MD->getLexicalDeclContext() == CDecl)
if (Visit(MakeCXCursor(MD, TU)))
return true;
if (ObjCMethodDecl *MD = prevDecl->getSetterMethodDecl())
- if (MD->isSynthesized())
+ if (MD->isSynthesized() && MD->getLexicalDeclContext() == CDecl)
if (Visit(MakeCXCursor(MD, TU)))
return true;
@@ -945,6 +1061,13 @@ bool CursorVisitor::VisitObjCClassDecl(ObjCClassDecl *D) {
return false;
}
+bool CursorVisitor::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PD) {
+ if (ObjCIvarDecl *Ivar = PD->getPropertyIvarDecl())
+ return Visit(MakeCursorMemberRef(Ivar, PD->getPropertyIvarDeclLoc(), TU));
+
+ return false;
+}
+
bool CursorVisitor::VisitNamespaceDecl(NamespaceDecl *D) {
return VisitDeclContext(D);
}
@@ -965,10 +1088,9 @@ bool CursorVisitor::VisitUsingDecl(UsingDecl *D) {
if (VisitNestedNameSpecifier(Qualifier, D->getNestedNameRange()))
return true;
- // FIXME: Provide a multi-reference of some kind for all of the declarations
- // that the using declaration refers to. We don't have this kind of cursor
- // yet.
-
+ if (Visit(MakeCursorOverloadedDeclRef(D, D->getLocation(), TU)))
+ return true;
+
return VisitDeclarationNameInfo(D->getNameInfo());
}
@@ -1050,7 +1172,7 @@ bool CursorVisitor::VisitNestedNameSpecifier(NestedNameSpecifier *NNS,
// If the type has a form where we know that the beginning of the source
// range matches up with a reference cursor. Visit the appropriate reference
// cursor.
- Type *T = NNS->getAsType();
+ const Type *T = NNS->getAsType();
if (const TypedefType *Typedef = dyn_cast<TypedefType>(T))
return Visit(MakeCursorTypeRef(Typedef->getDecl(), Range.getBegin(), TU));
if (const TagType *Tag = dyn_cast<TagType>(T))
@@ -1091,8 +1213,10 @@ bool CursorVisitor::VisitTemplateName(TemplateName Name, SourceLocation Loc) {
return Visit(MakeCursorTemplateRef(Name.getAsTemplateDecl(), Loc, TU));
case TemplateName::OverloadedTemplate:
- // FIXME: We need a way to return multiple lookup results in a single
- // cursor.
+ // Visit the overloaded template set.
+ if (Visit(MakeCursorOverloadedDeclRef(Name, Loc, TU)))
+ return true;
+
return false;
case TemplateName::DependentTemplate:
@@ -1104,6 +1228,11 @@ bool CursorVisitor::VisitTemplateName(TemplateName Name, SourceLocation Loc) {
return Visit(MakeCursorTemplateRef(
Name.getAsQualifiedTemplateName()->getDecl(),
Loc, TU));
+
+ case TemplateName::SubstTemplateTemplateParmPack:
+ return Visit(MakeCursorTemplateRef(
+ Name.getAsSubstTemplateTemplateParmPack()->getParameterPack(),
+ Loc, TU));
}
return false;
@@ -1113,12 +1242,9 @@ bool CursorVisitor::VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL) {
switch (TAL.getArgument().getKind()) {
case TemplateArgument::Null:
case TemplateArgument::Integral:
- return false;
-
case TemplateArgument::Pack:
- // FIXME: Implement when variadic templates come along.
return false;
-
+
case TemplateArgument::Type:
if (TypeSourceInfo *TSInfo = TAL.getTypeSourceInfo())
return Visit(TSInfo->getTypeLoc());
@@ -1135,7 +1261,8 @@ bool CursorVisitor::VisitTemplateArgumentLoc(const TemplateArgumentLoc &TAL) {
return false;
case TemplateArgument::Template:
- return VisitTemplateName(TAL.getArgument().getAsTemplate(),
+ case TemplateArgument::TemplateExpansion:
+ return VisitTemplateName(TAL.getArgument().getAsTemplateOrTemplatePattern(),
TAL.getTemplateNameLoc());
}
@@ -1151,7 +1278,7 @@ bool CursorVisitor::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
}
bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
- ASTContext &Context = TU->getASTContext();
+ ASTContext &Context = AU->getASTContext();
// Some builtin types (such as Objective-C's "id", "sel", and
// "Class") have associated declarations. Create cursors for those.
@@ -1170,7 +1297,8 @@ bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
case BuiltinType::UInt128:
case BuiltinType::Char_S:
case BuiltinType::SChar:
- case BuiltinType::WChar:
+ case BuiltinType::WChar_U:
+ case BuiltinType::WChar_S:
case BuiltinType::Short:
case BuiltinType::Int:
case BuiltinType::Long:
@@ -1184,9 +1312,6 @@ bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
case BuiltinType::Dependent:
break;
- case BuiltinType::UndeducedAuto: // FIXME: Deserves a cursor?
- break;
-
case BuiltinType::ObjCId:
VisitType = Context.getObjCIdType();
break;
@@ -1222,7 +1347,7 @@ bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) {
}
bool CursorVisitor::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
- // FIXME: We can't visit the template template parameter, but there's
+ // FIXME: We can't visit the template type parameter, because there's
// no context information with which we can match up the depth/index in the
// type to the appropriate
return false;
@@ -1252,6 +1377,10 @@ bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
return Visit(TL.getPointeeLoc());
}
+bool CursorVisitor::VisitParenTypeLoc(ParenTypeLoc TL) {
+ return Visit(TL.getInnerLoc());
+}
+
bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) {
return Visit(TL.getPointeeLoc());
}
@@ -1321,418 +1450,702 @@ bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
return false;
}
-bool CursorVisitor::VisitStmt(Stmt *S) {
- for (Stmt::child_iterator Child = S->child_begin(), ChildEnd = S->child_end();
- Child != ChildEnd; ++Child) {
- if (Stmt *C = *Child)
- if (Visit(MakeCXCursor(C, StmtParent, TU)))
- return true;
- }
-
- return false;
+bool CursorVisitor::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
+ return Visit(TL.getPatternLoc());
}
-bool CursorVisitor::VisitCaseStmt(CaseStmt *S) {
- // Specially handle CaseStmts because they can be nested, e.g.:
- //
- // case 1:
- // case 2:
- //
- // In this case the second CaseStmt is the child of the first. Walking
- // these recursively can blow out the stack.
- CXCursor Cursor = MakeCXCursor(S, StmtParent, TU);
- while (true) {
- // Set the Parent field to Cursor, then back to its old value once we're
- // done.
- SetParentRAII SetParent(Parent, StmtParent, Cursor);
-
- if (Stmt *LHS = S->getLHS())
- if (Visit(MakeCXCursor(LHS, StmtParent, TU)))
- return true;
- if (Stmt *RHS = S->getRHS())
- if (Visit(MakeCXCursor(RHS, StmtParent, TU)))
+bool CursorVisitor::VisitCXXRecordDecl(CXXRecordDecl *D) {
+ if (D->isDefinition()) {
+ for (CXXRecordDecl::base_class_iterator I = D->bases_begin(),
+ E = D->bases_end(); I != E; ++I) {
+ if (Visit(cxcursor::MakeCursorCXXBaseSpecifier(I, TU)))
return true;
- if (Stmt *SubStmt = S->getSubStmt()) {
- if (!isa<CaseStmt>(SubStmt))
- return Visit(MakeCXCursor(SubStmt, StmtParent, TU));
-
- // Specially handle 'CaseStmt' so that we don't blow out the stack.
- CaseStmt *CS = cast<CaseStmt>(SubStmt);
- Cursor = MakeCXCursor(CS, StmtParent, TU);
- if (RegionOfInterest.isValid()) {
- SourceRange Range = CS->getSourceRange();
- if (Range.isInvalid() || CompareRegionOfInterest(Range))
- return false;
- }
-
- switch (Visitor(Cursor, Parent, ClientData)) {
- case CXChildVisit_Break: return true;
- case CXChildVisit_Continue: return false;
- case CXChildVisit_Recurse:
- // Perform tail-recursion manually.
- S = CS;
- continue;
- }
}
- return false;
- }
-}
-
-bool CursorVisitor::VisitDeclStmt(DeclStmt *S) {
- for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
- D != DEnd; ++D) {
- if (*D && Visit(MakeCXCursor(*D, TU)))
- return true;
}
- return false;
+ return VisitTagDecl(D);
}
-bool CursorVisitor::VisitIfStmt(IfStmt *S) {
- if (VarDecl *Var = S->getConditionVariable()) {
- if (Visit(MakeCXCursor(Var, TU)))
- return true;
- }
-
- if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
- return true;
- if (S->getThen() && Visit(MakeCXCursor(S->getThen(), StmtParent, TU)))
- return true;
- if (S->getElse() && Visit(MakeCXCursor(S->getElse(), StmtParent, TU)))
- return true;
+bool CursorVisitor::VisitAttributes(Decl *D) {
+ for (AttrVec::const_iterator i = D->attr_begin(), e = D->attr_end();
+ i != e; ++i)
+ if (Visit(MakeCXCursor(*i, D, TU)))
+ return true;
return false;
}
-bool CursorVisitor::VisitSwitchStmt(SwitchStmt *S) {
- if (VarDecl *Var = S->getConditionVariable()) {
- if (Visit(MakeCXCursor(Var, TU)))
- return true;
- }
+//===----------------------------------------------------------------------===//
+// Data-recursive visitor methods.
+//===----------------------------------------------------------------------===//
- if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
- return true;
- if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
- return true;
+namespace {
+#define DEF_JOB(NAME, DATA, KIND)\
+class NAME : public VisitorJob {\
+public:\
+ NAME(DATA *d, CXCursor parent) : VisitorJob(parent, VisitorJob::KIND, d) {} \
+ static bool classof(const VisitorJob *VJ) { return VJ->getKind() == KIND; }\
+ DATA *get() const { return static_cast<DATA*>(data[0]); }\
+};
- return false;
-}
+DEF_JOB(StmtVisit, Stmt, StmtVisitKind)
+DEF_JOB(MemberExprParts, MemberExpr, MemberExprPartsKind)
+DEF_JOB(DeclRefExprParts, DeclRefExpr, DeclRefExprPartsKind)
+DEF_JOB(OverloadExprParts, OverloadExpr, OverloadExprPartsKind)
+DEF_JOB(ExplicitTemplateArgsVisit, ExplicitTemplateArgumentList,
+ ExplicitTemplateArgsVisitKind)
+DEF_JOB(SizeOfPackExprParts, SizeOfPackExpr, SizeOfPackExprPartsKind)
+#undef DEF_JOB
-bool CursorVisitor::VisitWhileStmt(WhileStmt *S) {
- if (VarDecl *Var = S->getConditionVariable()) {
- if (Visit(MakeCXCursor(Var, TU)))
- return true;
+class DeclVisit : public VisitorJob {
+public:
+ DeclVisit(Decl *d, CXCursor parent, bool isFirst) :
+ VisitorJob(parent, VisitorJob::DeclVisitKind,
+ d, isFirst ? (void*) 1 : (void*) 0) {}
+ static bool classof(const VisitorJob *VJ) {
+ return VJ->getKind() == DeclVisitKind;
}
+ Decl *get() const { return static_cast<Decl*>(data[0]); }
+ bool isFirst() const { return data[1] ? true : false; }
+};
+class TypeLocVisit : public VisitorJob {
+public:
+ TypeLocVisit(TypeLoc tl, CXCursor parent) :
+ VisitorJob(parent, VisitorJob::TypeLocVisitKind,
+ tl.getType().getAsOpaquePtr(), tl.getOpaqueData()) {}
- if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
- return true;
- if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
- return true;
-
- return false;
-}
-
-bool CursorVisitor::VisitForStmt(ForStmt *S) {
- if (S->getInit() && Visit(MakeCXCursor(S->getInit(), StmtParent, TU)))
- return true;
- if (VarDecl *Var = S->getConditionVariable()) {
- if (Visit(MakeCXCursor(Var, TU)))
- return true;
+ static bool classof(const VisitorJob *VJ) {
+ return VJ->getKind() == TypeLocVisitKind;
}
- if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
- return true;
- if (S->getInc() && Visit(MakeCXCursor(S->getInc(), StmtParent, TU)))
- return true;
- if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
- return true;
-
- return false;
-}
+ TypeLoc get() const {
+ QualType T = QualType::getFromOpaquePtr(data[0]);
+ return TypeLoc(T, data[1]);
+ }
+};
-bool CursorVisitor::VisitDeclRefExpr(DeclRefExpr *E) {
- // Visit nested-name-specifier, if present.
- if (NestedNameSpecifier *Qualifier = E->getQualifier())
- if (VisitNestedNameSpecifier(Qualifier, E->getQualifierRange()))
- return true;
-
- // Visit declaration name.
- if (VisitDeclarationNameInfo(E->getNameInfo()))
- return true;
+class LabelRefVisit : public VisitorJob {
+public:
+ LabelRefVisit(LabelDecl *LD, SourceLocation labelLoc, CXCursor parent)
+ : VisitorJob(parent, VisitorJob::LabelRefVisitKind, LD,
+ labelLoc.getPtrEncoding()) {}
- // Visit explicitly-specified template arguments.
- if (E->hasExplicitTemplateArgs()) {
- ExplicitTemplateArgumentList &Args = E->getExplicitTemplateArgs();
- for (TemplateArgumentLoc *Arg = Args.getTemplateArgs(),
- *ArgEnd = Arg + Args.NumTemplateArgs;
- Arg != ArgEnd; ++Arg)
- if (VisitTemplateArgumentLoc(*Arg))
- return true;
+ static bool classof(const VisitorJob *VJ) {
+ return VJ->getKind() == VisitorJob::LabelRefVisitKind;
+ }
+ LabelDecl *get() const { return static_cast<LabelDecl*>(data[0]); }
+ SourceLocation getLoc() const {
+ return SourceLocation::getFromPtrEncoding(data[1]); }
+};
+class NestedNameSpecifierVisit : public VisitorJob {
+public:
+ NestedNameSpecifierVisit(NestedNameSpecifier *NS, SourceRange R,
+ CXCursor parent)
+ : VisitorJob(parent, VisitorJob::NestedNameSpecifierVisitKind,
+ NS, R.getBegin().getPtrEncoding(),
+ R.getEnd().getPtrEncoding()) {}
+ static bool classof(const VisitorJob *VJ) {
+ return VJ->getKind() == VisitorJob::NestedNameSpecifierVisitKind;
+ }
+ NestedNameSpecifier *get() const {
+ return static_cast<NestedNameSpecifier*>(data[0]);
+ }
+ SourceRange getSourceRange() const {
+ SourceLocation A =
+ SourceLocation::getFromRawEncoding((unsigned)(uintptr_t) data[1]);
+ SourceLocation B =
+ SourceLocation::getFromRawEncoding((unsigned)(uintptr_t) data[2]);
+ return SourceRange(A, B);
+ }
+};
+class DeclarationNameInfoVisit : public VisitorJob {
+public:
+ DeclarationNameInfoVisit(Stmt *S, CXCursor parent)
+ : VisitorJob(parent, VisitorJob::DeclarationNameInfoVisitKind, S) {}
+ static bool classof(const VisitorJob *VJ) {
+ return VJ->getKind() == VisitorJob::DeclarationNameInfoVisitKind;
}
+ DeclarationNameInfo get() const {
+ Stmt *S = static_cast<Stmt*>(data[0]);
+ switch (S->getStmtClass()) {
+ default:
+ llvm_unreachable("Unhandled Stmt");
+ case Stmt::CXXDependentScopeMemberExprClass:
+ return cast<CXXDependentScopeMemberExpr>(S)->getMemberNameInfo();
+ case Stmt::DependentScopeDeclRefExprClass:
+ return cast<DependentScopeDeclRefExpr>(S)->getNameInfo();
+ }
+ }
+};
+class MemberRefVisit : public VisitorJob {
+public:
+ MemberRefVisit(FieldDecl *D, SourceLocation L, CXCursor parent)
+ : VisitorJob(parent, VisitorJob::MemberRefVisitKind, D,
+ L.getPtrEncoding()) {}
+ static bool classof(const VisitorJob *VJ) {
+ return VJ->getKind() == VisitorJob::MemberRefVisitKind;
+ }
+ FieldDecl *get() const {
+ return static_cast<FieldDecl*>(data[0]);
+ }
+ SourceLocation getLoc() const {
+ return SourceLocation::getFromRawEncoding((unsigned)(uintptr_t) data[1]);
+ }
+};
+class EnqueueVisitor : public StmtVisitor<EnqueueVisitor, void> {
+ VisitorWorkList &WL;
+ CXCursor Parent;
+public:
+ EnqueueVisitor(VisitorWorkList &wl, CXCursor parent)
+ : WL(wl), Parent(parent) {}
+
+ void VisitAddrLabelExpr(AddrLabelExpr *E);
+ void VisitBlockExpr(BlockExpr *B);
+ void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
+ void VisitCompoundStmt(CompoundStmt *S);
+ void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { /* Do nothing. */ }
+ void VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E);
+ void VisitCXXNewExpr(CXXNewExpr *E);
+ void VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
+ void VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E);
+ void VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E);
+ void VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E);
+ void VisitCXXTypeidExpr(CXXTypeidExpr *E);
+ void VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E);
+ void VisitCXXUuidofExpr(CXXUuidofExpr *E);
+ void VisitDeclRefExpr(DeclRefExpr *D);
+ void VisitDeclStmt(DeclStmt *S);
+ void VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E);
+ void VisitDesignatedInitExpr(DesignatedInitExpr *E);
+ void VisitExplicitCastExpr(ExplicitCastExpr *E);
+ void VisitForStmt(ForStmt *FS);
+ void VisitGotoStmt(GotoStmt *GS);
+ void VisitIfStmt(IfStmt *If);
+ void VisitInitListExpr(InitListExpr *IE);
+ void VisitMemberExpr(MemberExpr *M);
+ void VisitOffsetOfExpr(OffsetOfExpr *E);
+ void VisitObjCEncodeExpr(ObjCEncodeExpr *E);
+ void VisitObjCMessageExpr(ObjCMessageExpr *M);
+ void VisitOverloadExpr(OverloadExpr *E);
+ void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
+ void VisitStmt(Stmt *S);
+ void VisitSwitchStmt(SwitchStmt *S);
+ void VisitWhileStmt(WhileStmt *W);
+ void VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E);
+ void VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E);
+ void VisitUnresolvedMemberExpr(UnresolvedMemberExpr *U);
+ void VisitVAArgExpr(VAArgExpr *E);
+ void VisitSizeOfPackExpr(SizeOfPackExpr *E);
- return false;
+private:
+ void AddDeclarationNameInfo(Stmt *S);
+ void AddNestedNameSpecifier(NestedNameSpecifier *NS, SourceRange R);
+ void AddExplicitTemplateArgs(const ExplicitTemplateArgumentList *A);
+ void AddMemberRef(FieldDecl *D, SourceLocation L);
+ void AddStmt(Stmt *S);
+ void AddDecl(Decl *D, bool isFirst = true);
+ void AddTypeLoc(TypeSourceInfo *TI);
+ void EnqueueChildren(Stmt *S);
+};
+} // end anonyous namespace
+
+void EnqueueVisitor::AddDeclarationNameInfo(Stmt *S) {
+ // 'S' should always be non-null, since it comes from the
+ // statement we are visiting.
+ WL.push_back(DeclarationNameInfoVisit(S, Parent));
+}
+void EnqueueVisitor::AddNestedNameSpecifier(NestedNameSpecifier *N,
+ SourceRange R) {
+ if (N)
+ WL.push_back(NestedNameSpecifierVisit(N, R, Parent));
+}
+void EnqueueVisitor::AddStmt(Stmt *S) {
+ if (S)
+ WL.push_back(StmtVisit(S, Parent));
+}
+void EnqueueVisitor::AddDecl(Decl *D, bool isFirst) {
+ if (D)
+ WL.push_back(DeclVisit(D, Parent, isFirst));
+}
+void EnqueueVisitor::
+ AddExplicitTemplateArgs(const ExplicitTemplateArgumentList *A) {
+ if (A)
+ WL.push_back(ExplicitTemplateArgsVisit(
+ const_cast<ExplicitTemplateArgumentList*>(A), Parent));
+}
+void EnqueueVisitor::AddMemberRef(FieldDecl *D, SourceLocation L) {
+ if (D)
+ WL.push_back(MemberRefVisit(D, L, Parent));
+}
+void EnqueueVisitor::AddTypeLoc(TypeSourceInfo *TI) {
+ if (TI)
+ WL.push_back(TypeLocVisit(TI->getTypeLoc(), Parent));
+ }
+void EnqueueVisitor::EnqueueChildren(Stmt *S) {
+ unsigned size = WL.size();
+ for (Stmt::child_range Child = S->children(); Child; ++Child) {
+ AddStmt(*Child);
+ }
+ if (size == WL.size())
+ return;
+ // Now reverse the entries we just added. This will match the DFS
+ // ordering performed by the worklist.
+ VisitorWorkList::iterator I = WL.begin() + size, E = WL.end();
+ std::reverse(I, E);
}
-
-bool CursorVisitor::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) {
- if (Visit(MakeCXCursor(E->getArg(0), StmtParent, TU)))
- return true;
-
- if (Visit(MakeCXCursor(E->getCallee(), StmtParent, TU)))
- return true;
-
- for (unsigned I = 1, N = E->getNumArgs(); I != N; ++I)
- if (Visit(MakeCXCursor(E->getArg(I), StmtParent, TU)))
- return true;
-
- return false;
+void EnqueueVisitor::VisitAddrLabelExpr(AddrLabelExpr *E) {
+ WL.push_back(LabelRefVisit(E->getLabel(), E->getLabelLoc(), Parent));
}
-
-bool CursorVisitor::VisitCXXRecordDecl(CXXRecordDecl *D) {
- if (D->isDefinition()) {
- for (CXXRecordDecl::base_class_iterator I = D->bases_begin(),
- E = D->bases_end(); I != E; ++I) {
- if (Visit(cxcursor::MakeCursorCXXBaseSpecifier(I, TU)))
- return true;
- }
+void EnqueueVisitor::VisitBlockExpr(BlockExpr *B) {
+ AddDecl(B->getBlockDecl());
+}
+void EnqueueVisitor::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
+ EnqueueChildren(E);
+ AddTypeLoc(E->getTypeSourceInfo());
+}
+void EnqueueVisitor::VisitCompoundStmt(CompoundStmt *S) {
+ for (CompoundStmt::reverse_body_iterator I = S->body_rbegin(),
+ E = S->body_rend(); I != E; ++I) {
+ AddStmt(*I);
}
-
- return VisitTagDecl(D);
}
-
-
-bool CursorVisitor::VisitBlockExpr(BlockExpr *B) {
- return Visit(B->getBlockDecl());
+void EnqueueVisitor::
+VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E) {
+ AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs());
+ AddDeclarationNameInfo(E);
+ if (NestedNameSpecifier *Qualifier = E->getQualifier())
+ AddNestedNameSpecifier(Qualifier, E->getQualifierRange());
+ if (!E->isImplicitAccess())
+ AddStmt(E->getBase());
+}
+void EnqueueVisitor::VisitCXXNewExpr(CXXNewExpr *E) {
+ // Enqueue the initializer or constructor arguments.
+ for (unsigned I = E->getNumConstructorArgs(); I > 0; --I)
+ AddStmt(E->getConstructorArg(I-1));
+ // Enqueue the array size, if any.
+ AddStmt(E->getArraySize());
+ // Enqueue the allocated type.
+ AddTypeLoc(E->getAllocatedTypeSourceInfo());
+ // Enqueue the placement arguments.
+ for (unsigned I = E->getNumPlacementArgs(); I > 0; --I)
+ AddStmt(E->getPlacementArg(I-1));
+}
+void EnqueueVisitor::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *CE) {
+ for (unsigned I = CE->getNumArgs(); I > 1 /* Yes, this is 1 */; --I)
+ AddStmt(CE->getArg(I-1));
+ AddStmt(CE->getCallee());
+ AddStmt(CE->getArg(0));
+}
+void EnqueueVisitor::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
+ // Visit the name of the type being destroyed.
+ AddTypeLoc(E->getDestroyedTypeInfo());
+ // Visit the scope type that looks disturbingly like the nested-name-specifier
+ // but isn't.
+ AddTypeLoc(E->getScopeTypeInfo());
+ // Visit the nested-name-specifier.
+ if (NestedNameSpecifier *Qualifier = E->getQualifier())
+ AddNestedNameSpecifier(Qualifier, E->getQualifierRange());
+ // Visit base expression.
+ AddStmt(E->getBase());
}
-
-bool CursorVisitor::VisitOffsetOfExpr(OffsetOfExpr *E) {
- // FIXME: Visit fields as well?
- if (Visit(E->getTypeSourceInfo()->getTypeLoc()))
- return true;
-
- return VisitExpr(E);
+void EnqueueVisitor::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
+ AddTypeLoc(E->getTypeSourceInfo());
+}
+void EnqueueVisitor::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) {
+ EnqueueChildren(E);
+ AddTypeLoc(E->getTypeSourceInfo());
+}
+void EnqueueVisitor::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
+ EnqueueChildren(E);
+ if (E->isTypeOperand())
+ AddTypeLoc(E->getTypeOperandSourceInfo());
}
-bool CursorVisitor::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
- if (E->isArgumentType()) {
- if (TypeSourceInfo *TSInfo = E->getArgumentTypeInfo())
- return Visit(TSInfo->getTypeLoc());
-
- return false;
+void EnqueueVisitor::VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr
+ *E) {
+ EnqueueChildren(E);
+ AddTypeLoc(E->getTypeSourceInfo());
+}
+void EnqueueVisitor::VisitCXXUuidofExpr(CXXUuidofExpr *E) {
+ EnqueueChildren(E);
+ if (E->isTypeOperand())
+ AddTypeLoc(E->getTypeOperandSourceInfo());
+}
+void EnqueueVisitor::VisitDeclRefExpr(DeclRefExpr *DR) {
+ if (DR->hasExplicitTemplateArgs()) {
+ AddExplicitTemplateArgs(&DR->getExplicitTemplateArgs());
}
-
- return VisitExpr(E);
+ WL.push_back(DeclRefExprParts(DR, Parent));
}
-
-bool CursorVisitor::VisitMemberExpr(MemberExpr *E) {
- // Visit the base expression.
- if (Visit(MakeCXCursor(E->getBase(), StmtParent, TU)))
- return true;
-
- // Visit the nested-name-specifier
+void EnqueueVisitor::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
+ AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs());
+ AddDeclarationNameInfo(E);
if (NestedNameSpecifier *Qualifier = E->getQualifier())
- if (VisitNestedNameSpecifier(Qualifier, E->getQualifierRange()))
- return true;
+ AddNestedNameSpecifier(Qualifier, E->getQualifierRange());
+}
+void EnqueueVisitor::VisitDeclStmt(DeclStmt *S) {
+ unsigned size = WL.size();
+ bool isFirst = true;
+ for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
+ D != DEnd; ++D) {
+ AddDecl(*D, isFirst);
+ isFirst = false;
+ }
+ if (size == WL.size())
+ return;
+ // Now reverse the entries we just added. This will match the DFS
+ // ordering performed by the worklist.
+ VisitorWorkList::iterator I = WL.begin() + size, E = WL.end();
+ std::reverse(I, E);
+}
+void EnqueueVisitor::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
+ AddStmt(E->getInit());
+ typedef DesignatedInitExpr::Designator Designator;
+ for (DesignatedInitExpr::reverse_designators_iterator
+ D = E->designators_rbegin(), DEnd = E->designators_rend();
+ D != DEnd; ++D) {
+ if (D->isFieldDesignator()) {
+ if (FieldDecl *Field = D->getField())
+ AddMemberRef(Field, D->getFieldLoc());
+ continue;
+ }
+ if (D->isArrayDesignator()) {
+ AddStmt(E->getArrayIndex(*D));
+ continue;
+ }
+ assert(D->isArrayRangeDesignator() && "Unknown designator kind");
+ AddStmt(E->getArrayRangeEnd(*D));
+ AddStmt(E->getArrayRangeStart(*D));
+ }
+}
+void EnqueueVisitor::VisitExplicitCastExpr(ExplicitCastExpr *E) {
+ EnqueueChildren(E);
+ AddTypeLoc(E->getTypeInfoAsWritten());
+}
+void EnqueueVisitor::VisitForStmt(ForStmt *FS) {
+ AddStmt(FS->getBody());
+ AddStmt(FS->getInc());
+ AddStmt(FS->getCond());
+ AddDecl(FS->getConditionVariable());
+ AddStmt(FS->getInit());
+}
+void EnqueueVisitor::VisitGotoStmt(GotoStmt *GS) {
+ WL.push_back(LabelRefVisit(GS->getLabel(), GS->getLabelLoc(), Parent));
+}
+void EnqueueVisitor::VisitIfStmt(IfStmt *If) {
+ AddStmt(If->getElse());
+ AddStmt(If->getThen());
+ AddStmt(If->getCond());
+ AddDecl(If->getConditionVariable());
+}
+void EnqueueVisitor::VisitInitListExpr(InitListExpr *IE) {
+ // We care about the syntactic form of the initializer list, only.
+ if (InitListExpr *Syntactic = IE->getSyntacticForm())
+ IE = Syntactic;
+ EnqueueChildren(IE);
+}
+void EnqueueVisitor::VisitMemberExpr(MemberExpr *M) {
+ WL.push_back(MemberExprParts(M, Parent));
- // Visit the declaration name.
- if (VisitDeclarationNameInfo(E->getMemberNameInfo()))
- return true;
+ // If the base of the member access expression is an implicit 'this', don't
+ // visit it.
+ // FIXME: If we ever want to show these implicit accesses, this will be
+ // unfortunate. However, clang_getCursor() relies on this behavior.
+ if (CXXThisExpr *This
+ = llvm::dyn_cast<CXXThisExpr>(M->getBase()->IgnoreParenImpCasts()))
+ if (This->isImplicit())
+ return;
- // Visit the explicitly-specified template arguments, if any.
- if (E->hasExplicitTemplateArgs()) {
- for (const TemplateArgumentLoc *Arg = E->getTemplateArgs(),
- *ArgEnd = Arg + E->getNumTemplateArgs();
- Arg != ArgEnd;
- ++Arg) {
- if (VisitTemplateArgumentLoc(*Arg))
- return true;
+ AddStmt(M->getBase());
+}
+void EnqueueVisitor::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
+ AddTypeLoc(E->getEncodedTypeSourceInfo());
+}
+void EnqueueVisitor::VisitObjCMessageExpr(ObjCMessageExpr *M) {
+ EnqueueChildren(M);
+ AddTypeLoc(M->getClassReceiverTypeInfo());
+}
+void EnqueueVisitor::VisitOffsetOfExpr(OffsetOfExpr *E) {
+ // Visit the components of the offsetof expression.
+ for (unsigned N = E->getNumComponents(), I = N; I > 0; --I) {
+ typedef OffsetOfExpr::OffsetOfNode OffsetOfNode;
+ const OffsetOfNode &Node = E->getComponent(I-1);
+ switch (Node.getKind()) {
+ case OffsetOfNode::Array:
+ AddStmt(E->getIndexExpr(Node.getArrayExprIndex()));
+ break;
+ case OffsetOfNode::Field:
+ AddMemberRef(Node.getField(), Node.getRange().getEnd());
+ break;
+ case OffsetOfNode::Identifier:
+ case OffsetOfNode::Base:
+ continue;
}
}
-
- return false;
+ // Visit the type into which we're computing the offset.
+ AddTypeLoc(E->getTypeSourceInfo());
}
-
-bool CursorVisitor::VisitExplicitCastExpr(ExplicitCastExpr *E) {
- if (TypeSourceInfo *TSInfo = E->getTypeInfoAsWritten())
- if (Visit(TSInfo->getTypeLoc()))
- return true;
-
- return VisitCastExpr(E);
+void EnqueueVisitor::VisitOverloadExpr(OverloadExpr *E) {
+ AddExplicitTemplateArgs(E->getOptionalExplicitTemplateArgs());
+ WL.push_back(OverloadExprParts(E, Parent));
}
-
-bool CursorVisitor::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
- if (TypeSourceInfo *TSInfo = E->getTypeSourceInfo())
- if (Visit(TSInfo->getTypeLoc()))
- return true;
-
- return VisitExpr(E);
+void EnqueueVisitor::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
+ EnqueueChildren(E);
+ if (E->isArgumentType())
+ AddTypeLoc(E->getArgumentTypeInfo());
+}
+void EnqueueVisitor::VisitStmt(Stmt *S) {
+ EnqueueChildren(S);
+}
+void EnqueueVisitor::VisitSwitchStmt(SwitchStmt *S) {
+ AddStmt(S->getBody());
+ AddStmt(S->getCond());
+ AddDecl(S->getConditionVariable());
}
-bool CursorVisitor::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
- return Visit(E->getArgTInfo1()->getTypeLoc()) ||
- Visit(E->getArgTInfo2()->getTypeLoc());
+void EnqueueVisitor::VisitWhileStmt(WhileStmt *W) {
+ AddStmt(W->getBody());
+ AddStmt(W->getCond());
+ AddDecl(W->getConditionVariable());
+}
+void EnqueueVisitor::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
+ AddTypeLoc(E->getQueriedTypeSourceInfo());
}
-bool CursorVisitor::VisitVAArgExpr(VAArgExpr *E) {
- if (Visit(E->getWrittenTypeInfo()->getTypeLoc()))
- return true;
-
- return Visit(MakeCXCursor(E->getSubExpr(), StmtParent, TU));
+void EnqueueVisitor::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) {
+ AddTypeLoc(E->getRhsTypeSourceInfo());
+ AddTypeLoc(E->getLhsTypeSourceInfo());
}
-bool CursorVisitor::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
- if (E->isTypeOperand()) {
- if (TypeSourceInfo *TSInfo = E->getTypeOperandSourceInfo())
- return Visit(TSInfo->getTypeLoc());
-
- return false;
- }
-
- return VisitExpr(E);
+void EnqueueVisitor::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *U) {
+ VisitOverloadExpr(U);
+ if (!U->isImplicitAccess())
+ AddStmt(U->getBase());
+}
+void EnqueueVisitor::VisitVAArgExpr(VAArgExpr *E) {
+ AddStmt(E->getSubExpr());
+ AddTypeLoc(E->getWrittenTypeInfo());
+}
+void EnqueueVisitor::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
+ WL.push_back(SizeOfPackExprParts(E, Parent));
}
-bool CursorVisitor::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
- // Visit base expression.
- if (Visit(MakeCXCursor(E->getBase(), StmtParent, TU)))
- return true;
-
- // Visit the nested-name-specifier.
- if (NestedNameSpecifier *Qualifier = E->getQualifier())
- if (VisitNestedNameSpecifier(Qualifier, E->getQualifierRange()))
- return true;
-
- // Visit the scope type that looks disturbingly like the nested-name-specifier
- // but isn't.
- if (TypeSourceInfo *TSInfo = E->getScopeTypeInfo())
- if (Visit(TSInfo->getTypeLoc()))
- return true;
-
- // Visit the name of the type being destroyed.
- if (TypeSourceInfo *TSInfo = E->getDestroyedTypeInfo())
- if (Visit(TSInfo->getTypeLoc()))
- return true;
-
- return false;
+void CursorVisitor::EnqueueWorkList(VisitorWorkList &WL, Stmt *S) {
+ EnqueueVisitor(WL, MakeCXCursor(S, StmtParent, TU)).Visit(S);
}
-bool CursorVisitor::VisitOverloadExpr(OverloadExpr *E) {
- // Visit the nested-name-specifier.
- if (NestedNameSpecifier *Qualifier = E->getQualifier())
- if (VisitNestedNameSpecifier(Qualifier, E->getQualifierRange()))
- return true;
-
- // Visit the declaration name.
- if (VisitDeclarationNameInfo(E->getNameInfo()))
- return true;
-
- // Visit the explicitly-specified template arguments.
- if (const ExplicitTemplateArgumentList *ArgList
- = E->getOptionalExplicitTemplateArgs()) {
- for (const TemplateArgumentLoc *Arg = ArgList->getTemplateArgs(),
- *ArgEnd = Arg + ArgList->NumTemplateArgs;
- Arg != ArgEnd; ++Arg) {
- if (VisitTemplateArgumentLoc(*Arg))
- return true;
- }
+bool CursorVisitor::IsInRegionOfInterest(CXCursor C) {
+ if (RegionOfInterest.isValid()) {
+ SourceRange Range = getRawCursorExtent(C);
+ if (Range.isInvalid() || CompareRegionOfInterest(Range))
+ return false;
}
-
- // FIXME: We don't have a way to visit all of the declarations referenced
- // here.
- return false;
+ return true;
}
-bool CursorVisitor::VisitDependentScopeDeclRefExpr(
- DependentScopeDeclRefExpr *E) {
- // Visit the nested-name-specifier.
- if (NestedNameSpecifier *Qualifier = E->getQualifier())
- if (VisitNestedNameSpecifier(Qualifier, E->getQualifierRange()))
- return true;
+bool CursorVisitor::RunVisitorWorkList(VisitorWorkList &WL) {
+ while (!WL.empty()) {
+ // Dequeue the worklist item.
+ VisitorJob LI = WL.back();
+ WL.pop_back();
- // Visit the declaration name.
- if (VisitDeclarationNameInfo(E->getNameInfo()))
- return true;
+ // Set the Parent field, then back to its old value once we're done.
+ SetParentRAII SetParent(Parent, StmtParent, LI.getParent());
+
+ switch (LI.getKind()) {
+ case VisitorJob::DeclVisitKind: {
+ Decl *D = cast<DeclVisit>(&LI)->get();
+ if (!D)
+ continue;
- // Visit the explicitly-specified template arguments.
- if (const ExplicitTemplateArgumentList *ArgList
- = E->getOptionalExplicitTemplateArgs()) {
- for (const TemplateArgumentLoc *Arg = ArgList->getTemplateArgs(),
- *ArgEnd = Arg + ArgList->NumTemplateArgs;
- Arg != ArgEnd; ++Arg) {
- if (VisitTemplateArgumentLoc(*Arg))
- return true;
- }
- }
+ // For now, perform default visitation for Decls.
+ if (Visit(MakeCXCursor(D, TU, cast<DeclVisit>(&LI)->isFirst())))
+ return true;
- return false;
-}
+ continue;
+ }
+ case VisitorJob::ExplicitTemplateArgsVisitKind: {
+ const ExplicitTemplateArgumentList *ArgList =
+ cast<ExplicitTemplateArgsVisit>(&LI)->get();
+ for (const TemplateArgumentLoc *Arg = ArgList->getTemplateArgs(),
+ *ArgEnd = Arg + ArgList->NumTemplateArgs;
+ Arg != ArgEnd; ++Arg) {
+ if (VisitTemplateArgumentLoc(*Arg))
+ return true;
+ }
+ continue;
+ }
+ case VisitorJob::TypeLocVisitKind: {
+ // Perform default visitation for TypeLocs.
+ if (Visit(cast<TypeLocVisit>(&LI)->get()))
+ return true;
+ continue;
+ }
+ case VisitorJob::LabelRefVisitKind: {
+ LabelDecl *LS = cast<LabelRefVisit>(&LI)->get();
+ if (Visit(MakeCursorLabelRef(LS->getStmt(),
+ cast<LabelRefVisit>(&LI)->getLoc(),
+ TU)))
+ return true;
+ continue;
+ }
+ case VisitorJob::NestedNameSpecifierVisitKind: {
+ NestedNameSpecifierVisit *V = cast<NestedNameSpecifierVisit>(&LI);
+ if (VisitNestedNameSpecifier(V->get(), V->getSourceRange()))
+ return true;
+ continue;
+ }
+ case VisitorJob::DeclarationNameInfoVisitKind: {
+ if (VisitDeclarationNameInfo(cast<DeclarationNameInfoVisit>(&LI)
+ ->get()))
+ return true;
+ continue;
+ }
+ case VisitorJob::MemberRefVisitKind: {
+ MemberRefVisit *V = cast<MemberRefVisit>(&LI);
+ if (Visit(MakeCursorMemberRef(V->get(), V->getLoc(), TU)))
+ return true;
+ continue;
+ }
+ case VisitorJob::StmtVisitKind: {
+ Stmt *S = cast<StmtVisit>(&LI)->get();
+ if (!S)
+ continue;
-bool CursorVisitor::VisitCXXDependentScopeMemberExpr(
- CXXDependentScopeMemberExpr *E) {
- // Visit the base expression, if there is one.
- if (!E->isImplicitAccess() &&
- Visit(MakeCXCursor(E->getBase(), StmtParent, TU)))
- return true;
-
- // Visit the nested-name-specifier.
- if (NestedNameSpecifier *Qualifier = E->getQualifier())
- if (VisitNestedNameSpecifier(Qualifier, E->getQualifierRange()))
- return true;
-
- // Visit the declaration name.
- if (VisitDeclarationNameInfo(E->getMemberNameInfo()))
- return true;
-
- // Visit the explicitly-specified template arguments.
- if (const ExplicitTemplateArgumentList *ArgList
- = E->getOptionalExplicitTemplateArgs()) {
- for (const TemplateArgumentLoc *Arg = ArgList->getTemplateArgs(),
- *ArgEnd = Arg + ArgList->NumTemplateArgs;
- Arg != ArgEnd; ++Arg) {
- if (VisitTemplateArgumentLoc(*Arg))
- return true;
+ // Update the current cursor.
+ CXCursor Cursor = MakeCXCursor(S, StmtParent, TU);
+ if (!IsInRegionOfInterest(Cursor))
+ continue;
+ switch (Visitor(Cursor, Parent, ClientData)) {
+ case CXChildVisit_Break: return true;
+ case CXChildVisit_Continue: break;
+ case CXChildVisit_Recurse:
+ EnqueueWorkList(WL, S);
+ break;
+ }
+ continue;
+ }
+ case VisitorJob::MemberExprPartsKind: {
+ // Handle the other pieces in the MemberExpr besides the base.
+ MemberExpr *M = cast<MemberExprParts>(&LI)->get();
+
+ // Visit the nested-name-specifier
+ if (NestedNameSpecifier *Qualifier = M->getQualifier())
+ if (VisitNestedNameSpecifier(Qualifier, M->getQualifierRange()))
+ return true;
+
+ // Visit the declaration name.
+ if (VisitDeclarationNameInfo(M->getMemberNameInfo()))
+ return true;
+
+ // Visit the explicitly-specified template arguments, if any.
+ if (M->hasExplicitTemplateArgs()) {
+ for (const TemplateArgumentLoc *Arg = M->getTemplateArgs(),
+ *ArgEnd = Arg + M->getNumTemplateArgs();
+ Arg != ArgEnd; ++Arg) {
+ if (VisitTemplateArgumentLoc(*Arg))
+ return true;
+ }
+ }
+ continue;
+ }
+ case VisitorJob::DeclRefExprPartsKind: {
+ DeclRefExpr *DR = cast<DeclRefExprParts>(&LI)->get();
+ // Visit nested-name-specifier, if present.
+ if (NestedNameSpecifier *Qualifier = DR->getQualifier())
+ if (VisitNestedNameSpecifier(Qualifier, DR->getQualifierRange()))
+ return true;
+ // Visit declaration name.
+ if (VisitDeclarationNameInfo(DR->getNameInfo()))
+ return true;
+ continue;
+ }
+ case VisitorJob::OverloadExprPartsKind: {
+ OverloadExpr *O = cast<OverloadExprParts>(&LI)->get();
+ // Visit the nested-name-specifier.
+ if (NestedNameSpecifier *Qualifier = O->getQualifier())
+ if (VisitNestedNameSpecifier(Qualifier, O->getQualifierRange()))
+ return true;
+ // Visit the declaration name.
+ if (VisitDeclarationNameInfo(O->getNameInfo()))
+ return true;
+ // Visit the overloaded declaration reference.
+ if (Visit(MakeCursorOverloadedDeclRef(O, TU)))
+ return true;
+ continue;
+ }
+ case VisitorJob::SizeOfPackExprPartsKind: {
+ SizeOfPackExpr *E = cast<SizeOfPackExprParts>(&LI)->get();
+ NamedDecl *Pack = E->getPack();
+ if (isa<TemplateTypeParmDecl>(Pack)) {
+ if (Visit(MakeCursorTypeRef(cast<TemplateTypeParmDecl>(Pack),
+ E->getPackLoc(), TU)))
+ return true;
+
+ continue;
+ }
+
+ if (isa<TemplateTemplateParmDecl>(Pack)) {
+ if (Visit(MakeCursorTemplateRef(cast<TemplateTemplateParmDecl>(Pack),
+ E->getPackLoc(), TU)))
+ return true;
+
+ continue;
+ }
+
+ // Non-type template parameter packs and function parameter packs are
+ // treated like DeclRefExpr cursors.
+ continue;
+ }
}
}
-
return false;
}
-bool CursorVisitor::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
- // Visit the base expression, if there is one.
- if (!E->isImplicitAccess() &&
- Visit(MakeCXCursor(E->getBase(), StmtParent, TU)))
- return true;
-
- return VisitOverloadExpr(E);
-}
-
-bool CursorVisitor::VisitObjCMessageExpr(ObjCMessageExpr *E) {
- if (TypeSourceInfo *TSInfo = E->getClassReceiverTypeInfo())
- if (Visit(TSInfo->getTypeLoc()))
- return true;
-
- return VisitExpr(E);
-}
-
-bool CursorVisitor::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
- return Visit(E->getEncodedTypeSourceInfo()->getTypeLoc());
+bool CursorVisitor::Visit(Stmt *S) {
+ VisitorWorkList *WL = 0;
+ if (!WorkListFreeList.empty()) {
+ WL = WorkListFreeList.back();
+ WL->clear();
+ WorkListFreeList.pop_back();
+ }
+ else {
+ WL = new VisitorWorkList();
+ WorkListCache.push_back(WL);
+ }
+ EnqueueWorkList(*WL, S);
+ bool result = RunVisitorWorkList(*WL);
+ WorkListFreeList.push_back(WL);
+ return result;
}
+//===----------------------------------------------------------------------===//
+// Misc. API hooks.
+//===----------------------------------------------------------------------===//
-bool CursorVisitor::VisitAttributes(Decl *D) {
- for (AttrVec::const_iterator i = D->attr_begin(), e = D->attr_end();
- i != e; ++i)
- if (Visit(MakeCXCursor(*i, D, TU)))
- return true;
-
- return false;
-}
+static llvm::sys::Mutex EnableMultithreadingMutex;
+static bool EnabledMultithreading;
extern "C" {
CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
int displayDiagnostics) {
+ // Disable pretty stack trace functionality, which will otherwise be a very
+ // poor citizen of the world and set up all sorts of signal handlers.
+ llvm::DisablePrettyStackTrace = true;
+
// We use crash recovery to make some of our APIs more reliable, implicitly
// enable it.
llvm::CrashRecoveryContext::Enable();
+ // Enable support for multithreading in LLVM.
+ {
+ llvm::sys::ScopedLock L(EnableMultithreadingMutex);
+ if (!EnabledMultithreading) {
+ llvm::llvm_start_multithreaded();
+ EnabledMultithreading = true;
+ }
+ }
+
CIndexer *CIdxr = new CIndexer();
if (excludeDeclarationsFromPCH)
CIdxr->setOnlyLocalDecls();
@@ -1744,15 +2157,6 @@ CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
void clang_disposeIndex(CXIndex CIdx) {
if (CIdx)
delete static_cast<CIndexer *>(CIdx);
- if (getenv("LIBCLANG_TIMING"))
- llvm::TimerGroup::printAll(llvm::errs());
-}
-
-void clang_setUseExternalASTGeneration(CXIndex CIdx, int value) {
- if (CIdx) {
- CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
- CXXIdx->setUseExternalASTGeneration(value);
- }
}
CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
@@ -1761,15 +2165,20 @@ CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
return 0;
CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
+ FileSystemOptions FileSystemOpts;
+ FileSystemOpts.WorkingDir = CXXIdx->getWorkingDirectory();
llvm::IntrusiveRefCntPtr<Diagnostic> Diags;
- return ASTUnit::LoadFromASTFile(ast_filename, Diags,
+ ASTUnit *TU = ASTUnit::LoadFromASTFile(ast_filename, Diags, FileSystemOpts,
CXXIdx->getOnlyLocalDecls(),
0, 0, true);
+ return MakeCXTranslationUnit(TU);
}
unsigned clang_defaultEditingTranslationUnitOptions() {
- return CXTranslationUnit_PrecompiledPreamble;
+ return CXTranslationUnit_PrecompiledPreamble |
+ CXTranslationUnit_CacheCompletionResults |
+ CXTranslationUnit_CXXPrecompiledPreamble;
}
CXTranslationUnit
@@ -1817,11 +2226,16 @@ static void clang_parseTranslationUnit_Impl(void *UserData) {
= ((options & CXTranslationUnit_Incomplete) == 0);
bool CacheCodeCompetionResults
= options & CXTranslationUnit_CacheCompletionResults;
+ bool CXXPrecompilePreamble
+ = options & CXTranslationUnit_CXXPrecompiledPreamble;
+ bool CXXChainedPCH
+ = options & CXTranslationUnit_CXXChainedPCH;
// Configure the diagnostics.
DiagnosticOptions DiagOpts;
llvm::IntrusiveRefCntPtr<Diagnostic> Diags;
- Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0);
+ Diags = CompilerInstance::createDiagnostics(DiagOpts, num_command_line_args,
+ command_line_args);
llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles;
for (unsigned I = 0; I != num_unsaved_files; ++I) {
@@ -1832,239 +2246,90 @@ static void clang_parseTranslationUnit_Impl(void *UserData) {
Buffer));
}
- if (!CXXIdx->getUseExternalASTGeneration()) {
- llvm::SmallVector<const char *, 16> Args;
-
- // The 'source_filename' argument is optional. If the caller does not
- // specify it then it is assumed that the source file is specified
- // in the actual argument list.
- if (source_filename)
- Args.push_back(source_filename);
-
- // Since the Clang C library is primarily used by batch tools dealing with
- // (often very broken) source code, where spell-checking can have a
- // significant negative impact on performance (particularly when
- // precompiled headers are involved), we disable it by default.
- // Note that we place this argument early in the list, so that it can be
- // overridden by the caller with "-fspell-checking".
- Args.push_back("-fno-spell-checking");
-
- Args.insert(Args.end(), command_line_args,
- command_line_args + num_command_line_args);
-
- // Do we need the detailed preprocessing record?
- if (options & CXTranslationUnit_DetailedPreprocessingRecord) {
- Args.push_back("-Xclang");
- Args.push_back("-detailed-preprocessing-record");
- }
-
- unsigned NumErrors = Diags->getNumErrors();
-
-#ifdef USE_CRASHTRACER
- ArgsCrashTracerInfo ACTI(Args);
-#endif
-
- llvm::OwningPtr<ASTUnit> Unit(
- ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(),
- Diags,
- CXXIdx->getClangResourcesPath(),
- CXXIdx->getOnlyLocalDecls(),
- RemappedFiles.data(),
- RemappedFiles.size(),
- /*CaptureDiagnostics=*/true,
- PrecompilePreamble,
- CompleteTranslationUnit,
- CacheCodeCompetionResults));
-
- if (NumErrors != Diags->getNumErrors()) {
- // Make sure to check that 'Unit' is non-NULL.
- if (CXXIdx->getDisplayDiagnostics() && Unit.get()) {
- for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(),
- DEnd = Unit->stored_diag_end();
- D != DEnd; ++D) {
- CXStoredDiagnostic Diag(*D, Unit->getASTContext().getLangOptions());
- CXString Msg = clang_formatDiagnostic(&Diag,
- clang_defaultDiagnosticDisplayOptions());
- fprintf(stderr, "%s\n", clang_getCString(Msg));
- clang_disposeString(Msg);
- }
-#ifdef LLVM_ON_WIN32
- // On Windows, force a flush, since there may be multiple copies of
- // stderr and stdout in the file system, all with different buffers
- // but writing to the same device.
- fflush(stderr);
-#endif
- }
- }
-
- PTUI->result = Unit.take();
- return;
- }
-
- // Build up the arguments for invoking 'clang'.
- std::vector<const char *> argv;
-
- // First add the complete path to the 'clang' executable.
- llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
- argv.push_back(ClangPath.c_str());
-
- // Add the '-emit-ast' option as our execution mode for 'clang'.
- argv.push_back("-emit-ast");
+ llvm::SmallVector<const char *, 16> Args;
// The 'source_filename' argument is optional. If the caller does not
// specify it then it is assumed that the source file is specified
// in the actual argument list.
if (source_filename)
- argv.push_back(source_filename);
-
- // Generate a temporary name for the AST file.
- argv.push_back("-o");
- char astTmpFile[L_tmpnam];
- argv.push_back(tmpnam(astTmpFile));
+ Args.push_back(source_filename);
// Since the Clang C library is primarily used by batch tools dealing with
// (often very broken) source code, where spell-checking can have a
// significant negative impact on performance (particularly when
// precompiled headers are involved), we disable it by default.
- // Note that we place this argument early in the list, so that it can be
- // overridden by the caller with "-fspell-checking".
- argv.push_back("-fno-spell-checking");
-
- // Remap any unsaved files to temporary files.
- std::vector<llvm::sys::Path> TemporaryFiles;
- std::vector<std::string> RemapArgs;
- if (RemapFiles(num_unsaved_files, unsaved_files, RemapArgs, TemporaryFiles))
- return;
-
- // The pointers into the elements of RemapArgs are stable because we
- // won't be adding anything to RemapArgs after this point.
- for (unsigned i = 0, e = RemapArgs.size(); i != e; ++i)
- argv.push_back(RemapArgs[i].c_str());
-
- // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'.
- for (int i = 0; i < num_command_line_args; ++i)
- if (const char *arg = command_line_args[i]) {
- if (strcmp(arg, "-o") == 0) {
- ++i; // Also skip the matching argument.
- continue;
- }
- if (strcmp(arg, "-emit-ast") == 0 ||
- strcmp(arg, "-c") == 0 ||
- strcmp(arg, "-fsyntax-only") == 0) {
- continue;
- }
-
- // Keep the argument.
- argv.push_back(arg);
+ // Only do this if we haven't found a spell-checking-related argument.
+ bool FoundSpellCheckingArgument = false;
+ for (int I = 0; I != num_command_line_args; ++I) {
+ if (strcmp(command_line_args[I], "-fno-spell-checking") == 0 ||
+ strcmp(command_line_args[I], "-fspell-checking") == 0) {
+ FoundSpellCheckingArgument = true;
+ break;
}
-
- // Generate a temporary name for the diagnostics file.
- char tmpFileResults[L_tmpnam];
- char *tmpResultsFileName = tmpnam(tmpFileResults);
- llvm::sys::Path DiagnosticsFile(tmpResultsFileName);
- TemporaryFiles.push_back(DiagnosticsFile);
- argv.push_back("-fdiagnostics-binary");
+ }
+ if (!FoundSpellCheckingArgument)
+ Args.push_back("-fno-spell-checking");
+
+ Args.insert(Args.end(), command_line_args,
+ command_line_args + num_command_line_args);
// Do we need the detailed preprocessing record?
if (options & CXTranslationUnit_DetailedPreprocessingRecord) {
- argv.push_back("-Xclang");
- argv.push_back("-detailed-preprocessing-record");
- }
-
- // Add the null terminator.
- argv.push_back(NULL);
-
- // Invoke 'clang'.
- llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null
- // on Unix or NUL (Windows).
- std::string ErrMsg;
- const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DiagnosticsFile,
- NULL };
- llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL,
- /* redirects */ &Redirects[0],
- /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg);
-
- if (!ErrMsg.empty()) {
- std::string AllArgs;
- for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
- I != E; ++I) {
- AllArgs += ' ';
- if (*I)
- AllArgs += *I;
- }
-
- Diags->Report(diag::err_fe_invoking) << AllArgs << ErrMsg;
- }
-
- ASTUnit *ATU = ASTUnit::LoadFromASTFile(astTmpFile, Diags,
- CXXIdx->getOnlyLocalDecls(),
- RemappedFiles.data(),
- RemappedFiles.size(),
- /*CaptureDiagnostics=*/true);
- if (ATU) {
- LoadSerializedDiagnostics(DiagnosticsFile,
- num_unsaved_files, unsaved_files,
- ATU->getFileManager(),
- ATU->getSourceManager(),
- ATU->getStoredDiagnostics());
- } else if (CXXIdx->getDisplayDiagnostics()) {
- // We failed to load the ASTUnit, but we can still deserialize the
- // diagnostics and emit them.
- FileManager FileMgr;
- Diagnostic Diag;
- SourceManager SourceMgr(Diag);
- // FIXME: Faked LangOpts!
- LangOptions LangOpts;
- llvm::SmallVector<StoredDiagnostic, 4> Diags;
- LoadSerializedDiagnostics(DiagnosticsFile,
- num_unsaved_files, unsaved_files,
- FileMgr, SourceMgr, Diags);
- for (llvm::SmallVector<StoredDiagnostic, 4>::iterator D = Diags.begin(),
- DEnd = Diags.end();
- D != DEnd; ++D) {
- CXStoredDiagnostic Diag(*D, LangOpts);
- CXString Msg = clang_formatDiagnostic(&Diag,
- clang_defaultDiagnosticDisplayOptions());
- fprintf(stderr, "%s\n", clang_getCString(Msg));
- clang_disposeString(Msg);
- }
-
-#ifdef LLVM_ON_WIN32
- // On Windows, force a flush, since there may be multiple copies of
- // stderr and stdout in the file system, all with different buffers
- // but writing to the same device.
- fflush(stderr);
-#endif
- }
-
- if (ATU) {
- // Make the translation unit responsible for destroying all temporary files.
- for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i)
- ATU->addTemporaryFile(TemporaryFiles[i]);
- ATU->addTemporaryFile(llvm::sys::Path(ATU->getASTFileName()));
- } else {
- // Destroy all of the temporary files now; they can't be referenced any
- // longer.
- llvm::sys::Path(astTmpFile).eraseFromDisk();
- for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i)
- TemporaryFiles[i].eraseFromDisk();
+ Args.push_back("-Xclang");
+ Args.push_back("-detailed-preprocessing-record");
}
- PTUI->result = ATU;
+ unsigned NumErrors = Diags->getClient()->getNumErrors();
+ llvm::OwningPtr<ASTUnit> Unit(
+ ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(),
+ Diags,
+ CXXIdx->getClangResourcesPath(),
+ CXXIdx->getOnlyLocalDecls(),
+ /*CaptureDiagnostics=*/true,
+ RemappedFiles.data(),
+ RemappedFiles.size(),
+ PrecompilePreamble,
+ CompleteTranslationUnit,
+ CacheCodeCompetionResults,
+ CXXPrecompilePreamble,
+ CXXChainedPCH));
+
+ if (NumErrors != Diags->getClient()->getNumErrors()) {
+ // Make sure to check that 'Unit' is non-NULL.
+ if (CXXIdx->getDisplayDiagnostics() && Unit.get()) {
+ for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(),
+ DEnd = Unit->stored_diag_end();
+ D != DEnd; ++D) {
+ CXStoredDiagnostic Diag(*D, Unit->getASTContext().getLangOptions());
+ CXString Msg = clang_formatDiagnostic(&Diag,
+ clang_defaultDiagnosticDisplayOptions());
+ fprintf(stderr, "%s\n", clang_getCString(Msg));
+ clang_disposeString(Msg);
+ }
+#ifdef LLVM_ON_WIN32
+ // On Windows, force a flush, since there may be multiple copies of
+ // stderr and stdout in the file system, all with different buffers
+ // but writing to the same device.
+ fflush(stderr);
+#endif
+ }
+ }
+
+ PTUI->result = MakeCXTranslationUnit(Unit.take());
}
CXTranslationUnit clang_parseTranslationUnit(CXIndex CIdx,
const char *source_filename,
const char * const *command_line_args,
int num_command_line_args,
- struct CXUnsavedFile *unsaved_files,
+ struct CXUnsavedFile *unsaved_files,
unsigned num_unsaved_files,
unsigned options) {
ParseTranslationUnitInfo PTUI = { CIdx, source_filename, command_line_args,
- num_command_line_args, unsaved_files, num_unsaved_files,
- options, 0 };
+ num_command_line_args, unsaved_files,
+ num_unsaved_files, options, 0 };
llvm::CrashRecoveryContext CRC;
- if (!CRC.RunSafely(clang_parseTranslationUnit_Impl, &PTUI)) {
+ if (!RunSafely(CRC, clang_parseTranslationUnit_Impl, &PTUI)) {
fprintf(stderr, "libclang: crash detected during parsing: {\n");
fprintf(stderr, " 'source_filename' : '%s'\n", source_filename);
fprintf(stderr, " 'command_line_args' : [");
@@ -2100,17 +2365,19 @@ int clang_saveTranslationUnit(CXTranslationUnit TU, const char *FileName,
if (!TU)
return 1;
- return static_cast<ASTUnit *>(TU)->Save(FileName);
+ return static_cast<ASTUnit *>(TU->TUData)->Save(FileName);
}
void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
if (CTUnit) {
// If the translation unit has been marked as unsafe to free, just discard
// it.
- if (static_cast<ASTUnit *>(CTUnit)->isUnsafeToFree())
+ if (static_cast<ASTUnit *>(CTUnit->TUData)->isUnsafeToFree())
return;
- delete static_cast<ASTUnit *>(CTUnit);
+ delete static_cast<ASTUnit *>(CTUnit->TUData);
+ disposeCXStringPool(CTUnit->StringPool);
+ delete CTUnit;
}
}
@@ -2125,6 +2392,7 @@ struct ReparseTranslationUnitInfo {
unsigned options;
int result;
};
+
static void clang_reparseTranslationUnit_Impl(void *UserData) {
ReparseTranslationUnitInfo *RTUI =
static_cast<ReparseTranslationUnitInfo*>(UserData);
@@ -2137,6 +2405,9 @@ static void clang_reparseTranslationUnit_Impl(void *UserData) {
if (!TU)
return;
+
+ ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
+ ASTUnit::ConcurrencyCheck Check(*CXXUnit);
llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles;
for (unsigned I = 0; I != num_unsaved_files; ++I) {
@@ -2147,10 +2418,10 @@ static void clang_reparseTranslationUnit_Impl(void *UserData) {
Buffer));
}
- if (!static_cast<ASTUnit *>(TU)->Reparse(RemappedFiles.data(),
- RemappedFiles.size()))
- RTUI->result = 0;
+ if (!CXXUnit->Reparse(RemappedFiles.data(), RemappedFiles.size()))
+ RTUI->result = 0;
}
+
int clang_reparseTranslationUnit(CXTranslationUnit TU,
unsigned num_unsaved_files,
struct CXUnsavedFile *unsaved_files,
@@ -2159,12 +2430,13 @@ int clang_reparseTranslationUnit(CXTranslationUnit TU,
options, 0 };
llvm::CrashRecoveryContext CRC;
- if (!CRC.RunSafely(clang_reparseTranslationUnit_Impl, &RTUI)) {
+ if (!RunSafely(CRC, clang_reparseTranslationUnit_Impl, &RTUI)) {
fprintf(stderr, "libclang: crash detected during reparsing\n");
- static_cast<ASTUnit *>(TU)->setUnsafeToFree(true);
+ static_cast<ASTUnit *>(TU->TUData)->setUnsafeToFree(true);
return 1;
}
+
return RTUI.result;
}
@@ -2173,7 +2445,7 @@ CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
if (!CTUnit)
return createCXString("");
- ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
+ ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit->TUData);
return createCXString(CXXUnit->getOriginalSourceFileName(), true);
}
@@ -2207,11 +2479,42 @@ CXSourceLocation clang_getLocation(CXTranslationUnit tu,
if (!tu || !file)
return clang_getNullLocation();
- ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu);
+ bool Logging = ::getenv("LIBCLANG_LOGGING");
+ ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData);
+ const FileEntry *File = static_cast<const FileEntry *>(file);
SourceLocation SLoc
+ = CXXUnit->getSourceManager().getLocation(File, line, column);
+ if (SLoc.isInvalid()) {
+ if (Logging)
+ llvm::errs() << "clang_getLocation(\"" << File->getName()
+ << "\", " << line << ", " << column << ") = invalid\n";
+ return clang_getNullLocation();
+ }
+
+ if (Logging)
+ llvm::errs() << "clang_getLocation(\"" << File->getName()
+ << "\", " << line << ", " << column << ") = "
+ << SLoc.getRawEncoding() << "\n";
+
+ return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc);
+}
+
+CXSourceLocation clang_getLocationForOffset(CXTranslationUnit tu,
+ CXFile file,
+ unsigned offset) {
+ if (!tu || !file)
+ return clang_getNullLocation();
+
+ ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData);
+ SourceLocation Start
= CXXUnit->getSourceManager().getLocation(
static_cast<const FileEntry *>(file),
- line, column);
+ 1, 1);
+ if (Start.isInvalid()) return clang_getNullLocation();
+
+ SourceLocation SLoc = Start.getFileLocWithOffset(offset);
+
+ if (SLoc.isInvalid()) return clang_getNullLocation();
return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc);
}
@@ -2264,6 +2567,51 @@ void clang_getInstantiationLocation(CXSourceLocation location,
*offset = SM.getDecomposedLoc(InstLoc).second;
}
+void clang_getSpellingLocation(CXSourceLocation location,
+ CXFile *file,
+ unsigned *line,
+ unsigned *column,
+ unsigned *offset) {
+ SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
+
+ if (!location.ptr_data[0] || Loc.isInvalid()) {
+ if (file)
+ *file = 0;
+ if (line)
+ *line = 0;
+ if (column)
+ *column = 0;
+ if (offset)
+ *offset = 0;
+ return;
+ }
+
+ const SourceManager &SM =
+ *static_cast<const SourceManager*>(location.ptr_data[0]);
+ SourceLocation SpellLoc = Loc;
+ if (SpellLoc.isMacroID()) {
+ SourceLocation SimpleSpellingLoc = SM.getImmediateSpellingLoc(SpellLoc);
+ if (SimpleSpellingLoc.isFileID() &&
+ SM.getFileEntryForID(SM.getDecomposedLoc(SimpleSpellingLoc).first))
+ SpellLoc = SimpleSpellingLoc;
+ else
+ SpellLoc = SM.getInstantiationLoc(SpellLoc);
+ }
+
+ std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(SpellLoc);
+ FileID FID = LocInfo.first;
+ unsigned FileOffset = LocInfo.second;
+
+ if (file)
+ *file = (void *)SM.getFileEntryForID(FID);
+ if (line)
+ *line = SM.getLineNumber(FID, FileOffset);
+ if (column)
+ *column = SM.getColumnNumber(FID, FileOffset);
+ if (offset)
+ *offset = FileOffset;
+}
+
CXSourceLocation clang_getRangeStart(CXSourceRange range) {
CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] },
range.begin_int_data };
@@ -2285,7 +2633,7 @@ CXSourceLocation clang_getRangeEnd(CXSourceRange range) {
extern "C" {
CXString clang_getFileName(CXFile SFile) {
if (!SFile)
- return createCXString(NULL);
+ return createCXString((const char*)NULL);
FileEntry *FEnt = static_cast<FileEntry *>(SFile);
return createCXString(FEnt->getName());
@@ -2303,11 +2651,10 @@ CXFile clang_getFile(CXTranslationUnit tu, const char *file_name) {
if (!tu)
return 0;
- ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu);
+ ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu->TUData);
FileManager &FMgr = CXXUnit->getFileManager();
- const FileEntry *File = FMgr.getFile(file_name, file_name+strlen(file_name));
- return const_cast<FileEntry *>(File);
+ return const_cast<FileEntry *>(FMgr.getFile(file_name));
}
} // end: extern "C"
@@ -2317,20 +2664,38 @@ CXFile clang_getFile(CXTranslationUnit tu, const char *file_name) {
//===----------------------------------------------------------------------===//
static Decl *getDeclFromExpr(Stmt *E) {
+ if (CastExpr *CE = dyn_cast<CastExpr>(E))
+ return getDeclFromExpr(CE->getSubExpr());
+
if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
return RefExpr->getDecl();
+ if (BlockDeclRefExpr *RefExpr = dyn_cast<BlockDeclRefExpr>(E))
+ return RefExpr->getDecl();
if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
return ME->getMemberDecl();
if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
return RE->getDecl();
-
+ if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(E))
+ return PRE->isExplicitProperty() ? PRE->getExplicitProperty() : 0;
+
if (CallExpr *CE = dyn_cast<CallExpr>(E))
return getDeclFromExpr(CE->getCallee());
- if (CastExpr *CE = dyn_cast<CastExpr>(E))
- return getDeclFromExpr(CE->getSubExpr());
+ if (CXXConstructExpr *CE = llvm::dyn_cast<CXXConstructExpr>(E))
+ if (!CE->isElidable())
+ return CE->getConstructor();
if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
return OME->getMethodDecl();
+ if (ObjCProtocolExpr *PE = dyn_cast<ObjCProtocolExpr>(E))
+ return PE->getProtocol();
+ if (SubstNonTypeTemplateParmPackExpr *NTTP
+ = dyn_cast<SubstNonTypeTemplateParmPackExpr>(E))
+ return NTTP->getParameterPack();
+ if (SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E))
+ if (isa<NonTypeTemplateParmDecl>(SizeOfPack->getPack()) ||
+ isa<ParmVarDecl>(SizeOfPack->getPack()))
+ return SizeOfPack->getPack();
+
return 0;
}
@@ -2339,10 +2704,15 @@ static SourceLocation getLocationFromExpr(Expr *E) {
return /*FIXME:*/Msg->getLeftLoc();
if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
return DRE->getLocation();
+ if (BlockDeclRefExpr *RefExpr = dyn_cast<BlockDeclRefExpr>(E))
+ return RefExpr->getLocation();
if (MemberExpr *Member = dyn_cast<MemberExpr>(E))
return Member->getMemberLoc();
if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E))
return Ivar->getLocation();
+ if (SizeOfPackExpr *SizeOfPack = dyn_cast<SizeOfPackExpr>(E))
+ return SizeOfPack->getPackLoc();
+
return E->getLocStart();
}
@@ -2351,18 +2721,58 @@ extern "C" {
unsigned clang_visitChildren(CXCursor parent,
CXCursorVisitor visitor,
CXClientData client_data) {
- ASTUnit *CXXUnit = getCursorASTUnit(parent);
-
- CursorVisitor CursorVis(CXXUnit, visitor, client_data,
- CXXUnit->getMaxPCHLevel());
+ CursorVisitor CursorVis(getCursorTU(parent), visitor, client_data,
+ getCursorASTUnit(parent)->getMaxPCHLevel());
return CursorVis.VisitChildren(parent);
}
+#ifndef __has_feature
+#define __has_feature(x) 0
+#endif
+#if __has_feature(blocks)
+typedef enum CXChildVisitResult
+ (^CXCursorVisitorBlock)(CXCursor cursor, CXCursor parent);
+
+static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent,
+ CXClientData client_data) {
+ CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data;
+ return block(cursor, parent);
+}
+#else
+// If we are compiled with a compiler that doesn't have native blocks support,
+// define and call the block manually, so the
+typedef struct _CXChildVisitResult
+{
+ void *isa;
+ int flags;
+ int reserved;
+ enum CXChildVisitResult(*invoke)(struct _CXChildVisitResult*, CXCursor,
+ CXCursor);
+} *CXCursorVisitorBlock;
+
+static enum CXChildVisitResult visitWithBlock(CXCursor cursor, CXCursor parent,
+ CXClientData client_data) {
+ CXCursorVisitorBlock block = (CXCursorVisitorBlock)client_data;
+ return block->invoke(block, cursor, parent);
+}
+#endif
+
+
+unsigned clang_visitChildrenWithBlock(CXCursor parent,
+ CXCursorVisitorBlock block) {
+ return clang_visitChildren(parent, visitWithBlock, block);
+}
+
static CXString getDeclSpelling(Decl *D) {
NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D);
- if (!ND)
+ if (!ND) {
+ if (ObjCPropertyImplDecl *PropImpl =llvm::dyn_cast<ObjCPropertyImplDecl>(D))
+ if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl())
+ return createCXString(Property->getIdentifier()->getName());
+
return createCXString("");
-
+ }
+
if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
return createCXString(OMD->getSelector().getAsString());
@@ -2384,7 +2794,8 @@ static CXString getDeclSpelling(Decl *D) {
CXString clang_getCursorSpelling(CXCursor C) {
if (clang_isTranslationUnit(C.kind))
- return clang_getTranslationUnitSpelling(C.data[2]);
+ return clang_getTranslationUnitSpelling(
+ static_cast<CXTranslationUnit>(C.data[2]));
if (clang_isReference(C.kind)) {
switch (C.kind) {
@@ -2426,6 +2837,36 @@ CXString clang_getCursorSpelling(CXCursor C) {
return createCXString(NS->getNameAsString());
}
+ case CXCursor_MemberRef: {
+ FieldDecl *Field = getCursorMemberRef(C).first;
+ assert(Field && "Missing member decl");
+
+ return createCXString(Field->getNameAsString());
+ }
+
+ case CXCursor_LabelRef: {
+ LabelStmt *Label = getCursorLabelRef(C).first;
+ assert(Label && "Missing label");
+
+ return createCXString(Label->getName());
+ }
+
+ case CXCursor_OverloadedDeclRef: {
+ OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first;
+ if (Decl *D = Storage.dyn_cast<Decl *>()) {
+ if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
+ return createCXString(ND->getNameAsString());
+ return createCXString("");
+ }
+ if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>())
+ return createCXString(E->getName().getAsString());
+ OverloadedTemplateStorage *Ovl
+ = Storage.get<OverloadedTemplateStorage*>();
+ if (Ovl->size() == 0)
+ return createCXString("");
+ return createCXString((*Ovl->begin())->getNameAsString());
+ }
+
default:
return createCXString("<not implemented>");
}
@@ -2438,6 +2879,14 @@ CXString clang_getCursorSpelling(CXCursor C) {
return createCXString("");
}
+ if (clang_isStatement(C.kind)) {
+ Stmt *S = getCursorStmt(C);
+ if (LabelStmt *Label = dyn_cast_or_null<LabelStmt>(S))
+ return createCXString(Label->getName());
+
+ return createCXString("");
+ }
+
if (C.kind == CXCursor_MacroInstantiation)
return createCXString(getCursorMacroInstantiation(C)->getName()
->getNameStart());
@@ -2446,12 +2895,99 @@ CXString clang_getCursorSpelling(CXCursor C) {
return createCXString(getCursorMacroDefinition(C)->getName()
->getNameStart());
+ if (C.kind == CXCursor_InclusionDirective)
+ return createCXString(getCursorInclusionDirective(C)->getFileName());
+
if (clang_isDeclaration(C.kind))
return getDeclSpelling(getCursorDecl(C));
return createCXString("");
}
+CXString clang_getCursorDisplayName(CXCursor C) {
+ if (!clang_isDeclaration(C.kind))
+ return clang_getCursorSpelling(C);
+
+ Decl *D = getCursorDecl(C);
+ if (!D)
+ return createCXString("");
+
+ PrintingPolicy &Policy = getCursorContext(C).PrintingPolicy;
+ if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D))
+ D = FunTmpl->getTemplatedDecl();
+
+ if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
+ llvm::SmallString<64> Str;
+ llvm::raw_svector_ostream OS(Str);
+ OS << Function->getNameAsString();
+ if (Function->getPrimaryTemplate())
+ OS << "<>";
+ OS << "(";
+ for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) {
+ if (I)
+ OS << ", ";
+ OS << Function->getParamDecl(I)->getType().getAsString(Policy);
+ }
+
+ if (Function->isVariadic()) {
+ if (Function->getNumParams())
+ OS << ", ";
+ OS << "...";
+ }
+ OS << ")";
+ return createCXString(OS.str());
+ }
+
+ if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) {
+ llvm::SmallString<64> Str;
+ llvm::raw_svector_ostream OS(Str);
+ OS << ClassTemplate->getNameAsString();
+ OS << "<";
+ TemplateParameterList *Params = ClassTemplate->getTemplateParameters();
+ for (unsigned I = 0, N = Params->size(); I != N; ++I) {
+ if (I)
+ OS << ", ";
+
+ NamedDecl *Param = Params->getParam(I);
+ if (Param->getIdentifier()) {
+ OS << Param->getIdentifier()->getName();
+ continue;
+ }
+
+ // There is no parameter name, which makes this tricky. Try to come up
+ // with something useful that isn't too long.
+ if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
+ OS << (TTP->wasDeclaredWithTypename()? "typename" : "class");
+ else if (NonTypeTemplateParmDecl *NTTP
+ = dyn_cast<NonTypeTemplateParmDecl>(Param))
+ OS << NTTP->getType().getAsString(Policy);
+ else
+ OS << "template<...> class";
+ }
+
+ OS << ">";
+ return createCXString(OS.str());
+ }
+
+ if (ClassTemplateSpecializationDecl *ClassSpec
+ = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
+ // If the type was explicitly written, use that.
+ if (TypeSourceInfo *TSInfo = ClassSpec->getTypeAsWritten())
+ return createCXString(TSInfo->getType().getAsString(Policy));
+
+ llvm::SmallString<64> Str;
+ llvm::raw_svector_ostream OS(Str);
+ OS << ClassSpec->getNameAsString();
+ OS << TemplateSpecializationType::PrintTemplateArgumentList(
+ ClassSpec->getTemplateArgs().data(),
+ ClassSpec->getTemplateArgs().size(),
+ Policy);
+ return createCXString(OS.str());
+ }
+
+ return clang_getCursorSpelling(C);
+}
+
CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) {
switch (Kind) {
case CXCursor_FunctionDecl:
@@ -2508,6 +3044,12 @@ CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) {
return createCXString("TemplateRef");
case CXCursor_NamespaceRef:
return createCXString("NamespaceRef");
+ case CXCursor_MemberRef:
+ return createCXString("MemberRef");
+ case CXCursor_LabelRef:
+ return createCXString("LabelRef");
+ case CXCursor_OverloadedDeclRef:
+ return createCXString("OverloadedDeclRef");
case CXCursor_UnexposedExpr:
return createCXString("UnexposedExpr");
case CXCursor_BlockExpr:
@@ -2522,6 +3064,8 @@ CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) {
return createCXString("ObjCMessageExpr");
case CXCursor_UnexposedStmt:
return createCXString("UnexposedStmt");
+ case CXCursor_LabelStmt:
+ return createCXString("LabelStmt");
case CXCursor_InvalidFile:
return createCXString("InvalidFile");
case CXCursor_InvalidCode:
@@ -2546,6 +3090,8 @@ CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) {
return createCXString("macro definition");
case CXCursor_MacroInstantiation:
return createCXString("macro instantiation");
+ case CXCursor_InclusionDirective:
+ return createCXString("inclusion directive");
case CXCursor_Namespace:
return createCXString("Namespace");
case CXCursor_LinkageSpec:
@@ -2579,13 +3125,28 @@ CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) {
}
llvm_unreachable("Unhandled CXCursorKind");
- return createCXString(NULL);
+ return createCXString((const char*) 0);
}
enum CXChildVisitResult GetCursorVisitor(CXCursor cursor,
CXCursor parent,
CXClientData client_data) {
CXCursor *BestCursor = static_cast<CXCursor *>(client_data);
+
+ // If our current best cursor is the construction of a temporary object,
+ // don't replace that cursor with a type reference, because we want
+ // clang_getCursor() to point at the constructor.
+ if (clang_isExpression(BestCursor->kind) &&
+ isa<CXXTemporaryObjectExpr>(getCursorExpr(*BestCursor)) &&
+ cursor.kind == CXCursor_TypeRef)
+ return CXChildVisit_Recurse;
+
+ // Don't override a preprocessing cursor with another preprocessing
+ // cursor; we want the outermost preprocessing cursor.
+ if (clang_isPreprocessing(cursor.kind) &&
+ clang_isPreprocessing(BestCursor->kind))
+ return CXChildVisit_Recurse;
+
*BestCursor = cursor;
return CXChildVisit_Recurse;
}
@@ -2594,7 +3155,7 @@ CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) {
if (!TU)
return clang_getNullCursor();
- ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
+ ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
ASTUnit::ConcurrencyCheck Check(*CXXUnit);
// Translate the given source location to make it point at the beginning of
@@ -2606,6 +3167,7 @@ CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) {
if (SLoc.isInvalid())
return clang_getNullCursor();
+ bool Logging = getenv("LIBCLANG_LOGGING");
SLoc = Lexer::GetBeginningOfToken(SLoc, CXXUnit->getSourceManager(),
CXXUnit->getASTContext().getLangOptions());
@@ -2614,11 +3176,58 @@ CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) {
// FIXME: Would be great to have a "hint" cursor, then walk from that
// hint cursor upward until we find a cursor whose source range encloses
// the region of interest, rather than starting from the translation unit.
- CXCursor Parent = clang_getTranslationUnitCursor(CXXUnit);
- CursorVisitor CursorVis(CXXUnit, GetCursorVisitor, &Result,
+ CXCursor Parent = clang_getTranslationUnitCursor(TU);
+ CursorVisitor CursorVis(TU, GetCursorVisitor, &Result,
Decl::MaxPCHLevel, SourceLocation(SLoc));
CursorVis.VisitChildren(Parent);
}
+
+ if (Logging) {
+ CXFile SearchFile;
+ unsigned SearchLine, SearchColumn;
+ CXFile ResultFile;
+ unsigned ResultLine, ResultColumn;
+ CXString SearchFileName, ResultFileName, KindSpelling, USR;
+ const char *IsDef = clang_isCursorDefinition(Result)? " (Definition)" : "";
+ CXSourceLocation ResultLoc = clang_getCursorLocation(Result);
+
+ clang_getInstantiationLocation(Loc, &SearchFile, &SearchLine, &SearchColumn,
+ 0);
+ clang_getInstantiationLocation(ResultLoc, &ResultFile, &ResultLine,
+ &ResultColumn, 0);
+ SearchFileName = clang_getFileName(SearchFile);
+ ResultFileName = clang_getFileName(ResultFile);
+ KindSpelling = clang_getCursorKindSpelling(Result.kind);
+ USR = clang_getCursorUSR(Result);
+ fprintf(stderr, "clang_getCursor(%s:%d:%d) = %s(%s:%d:%d):%s%s\n",
+ clang_getCString(SearchFileName), SearchLine, SearchColumn,
+ clang_getCString(KindSpelling),
+ clang_getCString(ResultFileName), ResultLine, ResultColumn,
+ clang_getCString(USR), IsDef);
+ clang_disposeString(SearchFileName);
+ clang_disposeString(ResultFileName);
+ clang_disposeString(KindSpelling);
+ clang_disposeString(USR);
+
+ CXCursor Definition = clang_getCursorDefinition(Result);
+ if (!clang_equalCursors(Definition, clang_getNullCursor())) {
+ CXSourceLocation DefinitionLoc = clang_getCursorLocation(Definition);
+ CXString DefinitionKindSpelling
+ = clang_getCursorKindSpelling(Definition.kind);
+ CXFile DefinitionFile;
+ unsigned DefinitionLine, DefinitionColumn;
+ clang_getInstantiationLocation(DefinitionLoc, &DefinitionFile,
+ &DefinitionLine, &DefinitionColumn, 0);
+ CXString DefinitionFileName = clang_getFileName(DefinitionFile);
+ fprintf(stderr, " -> %s(%s:%d:%d)\n",
+ clang_getCString(DefinitionKindSpelling),
+ clang_getCString(DefinitionFileName),
+ DefinitionLine, DefinitionColumn);
+ clang_disposeString(DefinitionFileName);
+ clang_disposeString(DefinitionKindSpelling);
+ }
+ }
+
return Result;
}
@@ -2630,6 +3239,15 @@ unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
return X == Y;
}
+unsigned clang_hashCursor(CXCursor C) {
+ unsigned Index = 0;
+ if (clang_isExpression(C.kind) || clang_isStatement(C.kind))
+ Index = 1;
+
+ return llvm::DenseMapInfo<std::pair<unsigned, void*> >::getHashValue(
+ std::make_pair(C.kind, C.data[Index]));
+}
+
unsigned clang_isInvalid(enum CXCursorKind K) {
return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
}
@@ -2710,11 +3328,33 @@ CXSourceLocation clang_getCursorLocation(CXCursor C) {
return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
}
+ case CXCursor_MemberRef: {
+ std::pair<FieldDecl *, SourceLocation> P = getCursorMemberRef(C);
+ return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
+ }
+
case CXCursor_CXXBaseSpecifier: {
- // FIXME: Figure out what location to return for a CXXBaseSpecifier.
- return clang_getNullLocation();
+ CXXBaseSpecifier *BaseSpec = getCursorCXXBaseSpecifier(C);
+ if (!BaseSpec)
+ return clang_getNullLocation();
+
+ if (TypeSourceInfo *TSInfo = BaseSpec->getTypeSourceInfo())
+ return cxloc::translateSourceLocation(getCursorContext(C),
+ TSInfo->getTypeLoc().getBeginLoc());
+
+ return cxloc::translateSourceLocation(getCursorContext(C),
+ BaseSpec->getSourceRange().getBegin());
}
+ case CXCursor_LabelRef: {
+ std::pair<LabelStmt *, SourceLocation> P = getCursorLabelRef(C);
+ return cxloc::translateSourceLocation(getCursorContext(C), P.second);
+ }
+
+ case CXCursor_OverloadedDeclRef:
+ return cxloc::translateSourceLocation(getCursorContext(C),
+ getCursorOverloadedDeclRef(C).second);
+
default:
// FIXME: Need a way to enumerate all non-reference cases.
llvm_unreachable("Missed a reference kind");
@@ -2725,6 +3365,10 @@ CXSourceLocation clang_getCursorLocation(CXCursor C) {
return cxloc::translateSourceLocation(getCursorContext(C),
getLocationFromExpr(getCursorExpr(C)));
+ if (clang_isStatement(C.kind))
+ return cxloc::translateSourceLocation(getCursorContext(C),
+ getCursorStmt(C)->getLocStart());
+
if (C.kind == CXCursor_PreprocessingDirective) {
SourceLocation L = cxcursor::getCursorPreprocessingDirective(C).getBegin();
return cxloc::translateSourceLocation(getCursorContext(C), L);
@@ -2740,7 +3384,13 @@ CXSourceLocation clang_getCursorLocation(CXCursor C) {
SourceLocation L = cxcursor::getCursorMacroDefinition(C)->getLocation();
return cxloc::translateSourceLocation(getCursorContext(C), L);
}
-
+
+ if (C.kind == CXCursor_InclusionDirective) {
+ SourceLocation L
+ = cxcursor::getCursorInclusionDirective(C)->getSourceRange().getBegin();
+ return cxloc::translateSourceLocation(getCursorContext(C), L);
+ }
+
if (C.kind < CXCursor_FirstDecl || C.kind > CXCursor_LastDecl)
return clang_getNullLocation();
@@ -2748,6 +3398,16 @@ CXSourceLocation clang_getCursorLocation(CXCursor C) {
SourceLocation Loc = D->getLocation();
if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D))
Loc = Class->getClassLoc();
+ // FIXME: Multiple variables declared in a single declaration
+ // currently lack the information needed to correctly determine their
+ // ranges when accounting for the type-specifier. We use context
+ // stored in the CXCursor to determine if the VarDecl is in a DeclGroup,
+ // and if so, whether it is the first decl.
+ if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
+ if (!cxcursor::isFirstInDeclGroup(C))
+ Loc = VD->getLocation();
+ }
+
return cxloc::translateSourceLocation(getCursorContext(C), Loc);
}
@@ -2773,10 +3433,18 @@ static SourceRange getRawCursorExtent(CXCursor C) {
case CXCursor_NamespaceRef:
return getCursorNamespaceRef(C).second;
-
+
+ case CXCursor_MemberRef:
+ return getCursorMemberRef(C).second;
+
case CXCursor_CXXBaseSpecifier:
- // FIXME: Figure out what source range to use for a CXBaseSpecifier.
- return SourceRange();
+ return getCursorCXXBaseSpecifier(C)->getSourceRange();
+
+ case CXCursor_LabelRef:
+ return getCursorLabelRef(C).second;
+
+ case CXCursor_OverloadedDeclRef:
+ return getCursorOverloadedDeclRef(C).second;
default:
// FIXME: Need a way to enumerate all non-reference cases.
@@ -2798,13 +3466,60 @@ static SourceRange getRawCursorExtent(CXCursor C) {
if (C.kind == CXCursor_MacroDefinition)
return cxcursor::getCursorMacroDefinition(C)->getSourceRange();
-
- if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl)
- return getCursorDecl(C)->getSourceRange();
+ if (C.kind == CXCursor_InclusionDirective)
+ return cxcursor::getCursorInclusionDirective(C)->getSourceRange();
+
+ if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) {
+ Decl *D = cxcursor::getCursorDecl(C);
+ SourceRange R = D->getSourceRange();
+ // FIXME: Multiple variables declared in a single declaration
+ // currently lack the information needed to correctly determine their
+ // ranges when accounting for the type-specifier. We use context
+ // stored in the CXCursor to determine if the VarDecl is in a DeclGroup,
+ // and if so, whether it is the first decl.
+ if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
+ if (!cxcursor::isFirstInDeclGroup(C))
+ R.setBegin(VD->getLocation());
+ }
+ return R;
+ }
return SourceRange();
}
+/// \brief Retrieves the "raw" cursor extent, which is then extended to include
+/// the decl-specifier-seq for declarations.
+static SourceRange getFullCursorExtent(CXCursor C, SourceManager &SrcMgr) {
+ if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) {
+ Decl *D = cxcursor::getCursorDecl(C);
+ SourceRange R = D->getSourceRange();
+
+ if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) {
+ if (TypeSourceInfo *TI = DD->getTypeSourceInfo()) {
+ TypeLoc TL = TI->getTypeLoc();
+ SourceLocation TLoc = TL.getSourceRange().getBegin();
+ if (TLoc.isValid() && R.getBegin().isValid() &&
+ SrcMgr.isBeforeInTranslationUnit(TLoc, R.getBegin()))
+ R.setBegin(TLoc);
+ }
+
+ // FIXME: Multiple variables declared in a single declaration
+ // currently lack the information needed to correctly determine their
+ // ranges when accounting for the type-specifier. We use context
+ // stored in the CXCursor to determine if the VarDecl is in a DeclGroup,
+ // and if so, whether it is the first decl.
+ if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
+ if (!cxcursor::isFirstInDeclGroup(C))
+ R.setBegin(VD->getLocation());
+ }
+ }
+
+ return R;
+ }
+
+ return getRawCursorExtent(C);
+}
+
extern "C" {
CXSourceRange clang_getCursorExtent(CXCursor C) {
@@ -2819,20 +3534,46 @@ CXCursor clang_getCursorReferenced(CXCursor C) {
if (clang_isInvalid(C.kind))
return clang_getNullCursor();
- ASTUnit *CXXUnit = getCursorASTUnit(C);
- if (clang_isDeclaration(C.kind))
+ CXTranslationUnit tu = getCursorTU(C);
+ if (clang_isDeclaration(C.kind)) {
+ Decl *D = getCursorDecl(C);
+ if (UsingDecl *Using = dyn_cast<UsingDecl>(D))
+ return MakeCursorOverloadedDeclRef(Using, D->getLocation(), tu);
+ if (ObjCClassDecl *Classes = dyn_cast<ObjCClassDecl>(D))
+ return MakeCursorOverloadedDeclRef(Classes, D->getLocation(), tu);
+ if (ObjCForwardProtocolDecl *Protocols
+ = dyn_cast<ObjCForwardProtocolDecl>(D))
+ return MakeCursorOverloadedDeclRef(Protocols, D->getLocation(), tu);
+ if (ObjCPropertyImplDecl *PropImpl =llvm::dyn_cast<ObjCPropertyImplDecl>(D))
+ if (ObjCPropertyDecl *Property = PropImpl->getPropertyDecl())
+ return MakeCXCursor(Property, tu);
+
return C;
-
+ }
+
if (clang_isExpression(C.kind)) {
- Decl *D = getDeclFromExpr(getCursorExpr(C));
+ Expr *E = getCursorExpr(C);
+ Decl *D = getDeclFromExpr(E);
if (D)
- return MakeCXCursor(D, CXXUnit);
+ return MakeCXCursor(D, tu);
+
+ if (OverloadExpr *Ovl = dyn_cast_or_null<OverloadExpr>(E))
+ return MakeCursorOverloadedDeclRef(Ovl, tu);
+
return clang_getNullCursor();
}
+ if (clang_isStatement(C.kind)) {
+ Stmt *S = getCursorStmt(C);
+ if (GotoStmt *Goto = dyn_cast_or_null<GotoStmt>(S))
+ return MakeCXCursor(Goto->getLabel()->getStmt(), getCursorDecl(C), tu);
+
+ return clang_getNullCursor();
+ }
+
if (C.kind == CXCursor_MacroInstantiation) {
if (MacroDefinition *Def = getCursorMacroInstantiation(C)->getDefinition())
- return MakeMacroDefinitionCursor(Def, CXXUnit);
+ return MakeMacroDefinitionCursor(Def, tu);
}
if (!clang_isReference(C.kind))
@@ -2840,29 +3581,43 @@ CXCursor clang_getCursorReferenced(CXCursor C) {
switch (C.kind) {
case CXCursor_ObjCSuperClassRef:
- return MakeCXCursor(getCursorObjCSuperClassRef(C).first, CXXUnit);
+ return MakeCXCursor(getCursorObjCSuperClassRef(C).first, tu);
case CXCursor_ObjCProtocolRef: {
- return MakeCXCursor(getCursorObjCProtocolRef(C).first, CXXUnit);
+ return MakeCXCursor(getCursorObjCProtocolRef(C).first, tu);
case CXCursor_ObjCClassRef:
- return MakeCXCursor(getCursorObjCClassRef(C).first, CXXUnit);
+ return MakeCXCursor(getCursorObjCClassRef(C).first, tu );
case CXCursor_TypeRef:
- return MakeCXCursor(getCursorTypeRef(C).first, CXXUnit);
+ return MakeCXCursor(getCursorTypeRef(C).first, tu );
case CXCursor_TemplateRef:
- return MakeCXCursor(getCursorTemplateRef(C).first, CXXUnit);
+ return MakeCXCursor(getCursorTemplateRef(C).first, tu );
case CXCursor_NamespaceRef:
- return MakeCXCursor(getCursorNamespaceRef(C).first, CXXUnit);
+ return MakeCXCursor(getCursorNamespaceRef(C).first, tu );
+
+ case CXCursor_MemberRef:
+ return MakeCXCursor(getCursorMemberRef(C).first, tu );
case CXCursor_CXXBaseSpecifier: {
CXXBaseSpecifier *B = cxcursor::getCursorCXXBaseSpecifier(C);
return clang_getTypeDeclaration(cxtype::MakeCXType(B->getType(),
- CXXUnit));
+ tu ));
}
+ case CXCursor_LabelRef:
+ // FIXME: We end up faking the "parent" declaration here because we
+ // don't want to make CXCursor larger.
+ return MakeCXCursor(getCursorLabelRef(C).first,
+ static_cast<ASTUnit*>(tu->TUData)->getASTContext()
+ .getTranslationUnitDecl(),
+ tu);
+
+ case CXCursor_OverloadedDeclRef:
+ return C;
+
default:
// We would prefer to enumerate all non-reference cursor kinds here.
llvm_unreachable("Unhandled reference cursor kind");
@@ -2877,7 +3632,7 @@ CXCursor clang_getCursorDefinition(CXCursor C) {
if (clang_isInvalid(C.kind))
return clang_getNullCursor();
- ASTUnit *CXXUnit = getCursorASTUnit(C);
+ CXTranslationUnit TU = getCursorTU(C);
bool WasReference = false;
if (clang_isReference(C.kind) || clang_isExpression(C.kind)) {
@@ -2903,6 +3658,7 @@ CXCursor clang_getCursorDefinition(CXCursor C) {
case Decl::TemplateTypeParm:
case Decl::EnumConstant:
case Decl::Field:
+ case Decl::IndirectField:
case Decl::ObjCIvar:
case Decl::ObjCAtDefsField:
case Decl::ImplicitParam:
@@ -2917,6 +3673,7 @@ CXCursor clang_getCursorDefinition(CXCursor C) {
case Decl::FileScopeAsm:
case Decl::StaticAssert:
case Decl::Block:
+ case Decl::Label: // FIXME: Is this right??
return C;
// Declaration kinds that don't make any sense here, but are
@@ -2931,10 +3688,10 @@ CXCursor clang_getCursorDefinition(CXCursor C) {
case Decl::UsingDirective:
return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(),
- CXXUnit);
+ TU);
case Decl::NamespaceAlias:
- return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), CXXUnit);
+ return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), TU);
case Decl::Enum:
case Decl::Record:
@@ -2942,7 +3699,7 @@ CXCursor clang_getCursorDefinition(CXCursor C) {
case Decl::ClassTemplateSpecialization:
case Decl::ClassTemplatePartialSpecialization:
if (TagDecl *Def = cast<TagDecl>(D)->getDefinition())
- return MakeCXCursor(Def, CXXUnit);
+ return MakeCXCursor(Def, TU);
return clang_getNullCursor();
case Decl::Function:
@@ -2952,21 +3709,21 @@ CXCursor clang_getCursorDefinition(CXCursor C) {
case Decl::CXXConversion: {
const FunctionDecl *Def = 0;
if (cast<FunctionDecl>(D)->getBody(Def))
- return MakeCXCursor(const_cast<FunctionDecl *>(Def), CXXUnit);
+ return MakeCXCursor(const_cast<FunctionDecl *>(Def), TU);
return clang_getNullCursor();
}
case Decl::Var: {
// Ask the variable if it has a definition.
if (VarDecl *Def = cast<VarDecl>(D)->getDefinition())
- return MakeCXCursor(Def, CXXUnit);
+ return MakeCXCursor(Def, TU);
return clang_getNullCursor();
}
case Decl::FunctionTemplate: {
const FunctionDecl *Def = 0;
if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def))
- return MakeCXCursor(Def->getDescribedFunctionTemplate(), CXXUnit);
+ return MakeCXCursor(Def->getDescribedFunctionTemplate(), TU);
return clang_getNullCursor();
}
@@ -2974,32 +3731,18 @@ CXCursor clang_getCursorDefinition(CXCursor C) {
if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl()
->getDefinition())
return MakeCXCursor(cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(),
- CXXUnit);
+ TU);
return clang_getNullCursor();
}
- case Decl::Using: {
- UsingDecl *Using = cast<UsingDecl>(D);
- CXCursor Def = clang_getNullCursor();
- for (UsingDecl::shadow_iterator S = Using->shadow_begin(),
- SEnd = Using->shadow_end();
- S != SEnd; ++S) {
- if (Def != clang_getNullCursor()) {
- // FIXME: We have no way to return multiple results.
- return clang_getNullCursor();
- }
-
- Def = clang_getCursorDefinition(MakeCXCursor((*S)->getTargetDecl(),
- CXXUnit));
- }
-
- return Def;
- }
+ case Decl::Using:
+ return MakeCursorOverloadedDeclRef(cast<UsingDecl>(D),
+ D->getLocation(), TU);
case Decl::UsingShadow:
return clang_getCursorDefinition(
MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(),
- CXXUnit));
+ TU));
case Decl::ObjCMethod: {
ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D);
@@ -3015,7 +3758,7 @@ CXCursor clang_getCursorDefinition(CXCursor C) {
if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(),
Method->isInstanceMethod()))
if (Def->isThisDeclarationADefinition())
- return MakeCXCursor(Def, CXXUnit);
+ return MakeCXCursor(Def, TU);
return clang_getNullCursor();
}
@@ -3023,7 +3766,7 @@ CXCursor clang_getCursorDefinition(CXCursor C) {
case Decl::ObjCCategory:
if (ObjCCategoryImplDecl *Impl
= cast<ObjCCategoryDecl>(D)->getImplementation())
- return MakeCXCursor(Impl, CXXUnit);
+ return MakeCXCursor(Impl, TU);
return clang_getNullCursor();
case Decl::ObjCProtocol:
@@ -3042,7 +3785,7 @@ CXCursor clang_getCursorDefinition(CXCursor C) {
return C;
} else if (ObjCImplementationDecl *Impl
= cast<ObjCInterfaceDecl>(D)->getImplementation())
- return MakeCXCursor(Impl, CXXUnit);
+ return MakeCXCursor(Impl, TU);
return clang_getNullCursor();
case Decl::ObjCProperty:
@@ -3054,42 +3797,26 @@ CXCursor clang_getCursorDefinition(CXCursor C) {
if (ObjCInterfaceDecl *Class
= cast<ObjCCompatibleAliasDecl>(D)->getClassInterface())
if (!Class->isForwardDecl())
- return MakeCXCursor(Class, CXXUnit);
-
- return clang_getNullCursor();
-
- case Decl::ObjCForwardProtocol: {
- ObjCForwardProtocolDecl *Forward = cast<ObjCForwardProtocolDecl>(D);
- if (Forward->protocol_size() == 1)
- return clang_getCursorDefinition(
- MakeCXCursor(*Forward->protocol_begin(),
- CXXUnit));
+ return MakeCXCursor(Class, TU);
- // FIXME: Cannot return multiple definitions.
return clang_getNullCursor();
- }
- case Decl::ObjCClass: {
- ObjCClassDecl *Class = cast<ObjCClassDecl>(D);
- if (Class->size() == 1) {
- ObjCInterfaceDecl *IFace = Class->begin()->getInterface();
- if (!IFace->isForwardDecl())
- return MakeCXCursor(IFace, CXXUnit);
- return clang_getNullCursor();
- }
+ case Decl::ObjCForwardProtocol:
+ return MakeCursorOverloadedDeclRef(cast<ObjCForwardProtocolDecl>(D),
+ D->getLocation(), TU);
- // FIXME: Cannot return multiple definitions.
- return clang_getNullCursor();
- }
+ case Decl::ObjCClass:
+ return MakeCursorOverloadedDeclRef(cast<ObjCClassDecl>(D), D->getLocation(),
+ TU);
case Decl::Friend:
if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl())
- return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit));
+ return clang_getCursorDefinition(MakeCXCursor(Friend, TU));
return clang_getNullCursor();
case Decl::FriendTemplate:
if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl())
- return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit));
+ return clang_getCursorDefinition(MakeCXCursor(Friend, TU));
return clang_getNullCursor();
}
@@ -3103,6 +3830,72 @@ unsigned clang_isCursorDefinition(CXCursor C) {
return clang_getCursorDefinition(C) == C;
}
+CXCursor clang_getCanonicalCursor(CXCursor C) {
+ if (!clang_isDeclaration(C.kind))
+ return C;
+
+ if (Decl *D = getCursorDecl(C))
+ return MakeCXCursor(D->getCanonicalDecl(), getCursorTU(C));
+
+ return C;
+}
+
+unsigned clang_getNumOverloadedDecls(CXCursor C) {
+ if (C.kind != CXCursor_OverloadedDeclRef)
+ return 0;
+
+ OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(C).first;
+ if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>())
+ return E->getNumDecls();
+
+ if (OverloadedTemplateStorage *S
+ = Storage.dyn_cast<OverloadedTemplateStorage*>())
+ return S->size();
+
+ Decl *D = Storage.get<Decl*>();
+ if (UsingDecl *Using = dyn_cast<UsingDecl>(D))
+ return Using->shadow_size();
+ if (ObjCClassDecl *Classes = dyn_cast<ObjCClassDecl>(D))
+ return Classes->size();
+ if (ObjCForwardProtocolDecl *Protocols =dyn_cast<ObjCForwardProtocolDecl>(D))
+ return Protocols->protocol_size();
+
+ return 0;
+}
+
+CXCursor clang_getOverloadedDecl(CXCursor cursor, unsigned index) {
+ if (cursor.kind != CXCursor_OverloadedDeclRef)
+ return clang_getNullCursor();
+
+ if (index >= clang_getNumOverloadedDecls(cursor))
+ return clang_getNullCursor();
+
+ CXTranslationUnit TU = getCursorTU(cursor);
+ OverloadedDeclRefStorage Storage = getCursorOverloadedDeclRef(cursor).first;
+ if (OverloadExpr *E = Storage.dyn_cast<OverloadExpr *>())
+ return MakeCXCursor(E->decls_begin()[index], TU);
+
+ if (OverloadedTemplateStorage *S
+ = Storage.dyn_cast<OverloadedTemplateStorage*>())
+ return MakeCXCursor(S->begin()[index], TU);
+
+ Decl *D = Storage.get<Decl*>();
+ if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) {
+ // FIXME: This is, unfortunately, linear time.
+ UsingDecl::shadow_iterator Pos = Using->shadow_begin();
+ std::advance(Pos, index);
+ return MakeCXCursor(cast<UsingShadowDecl>(*Pos)->getTargetDecl(), TU);
+ }
+
+ if (ObjCClassDecl *Classes = dyn_cast<ObjCClassDecl>(D))
+ return MakeCXCursor(Classes->begin()[index].getInterface(), TU);
+
+ if (ObjCForwardProtocolDecl *Protocols = dyn_cast<ObjCForwardProtocolDecl>(D))
+ return MakeCXCursor(Protocols->protocol_begin()[index], TU);
+
+ return clang_getNullCursor();
+}
+
void clang_getDefinitionSpellingAndExtent(CXCursor C,
const char **startBuf,
const char **endBuf,
@@ -3128,6 +3921,11 @@ void clang_enableStackTraces(void) {
llvm::sys::PrintStackTraceOnErrorSignal();
}
+void clang_executeOnThread(void (*fn)(void*), void *user_data,
+ unsigned stack_size) {
+ llvm::llvm_execute_on_thread(fn, user_data, stack_size);
+}
+
} // end: extern "C"
//===----------------------------------------------------------------------===//
@@ -3169,7 +3967,7 @@ CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) {
// We have to find the starting buffer pointer the hard way, by
// deconstructing the source location.
- ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
+ ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
if (!CXXUnit)
return createCXString("");
@@ -3186,7 +3984,7 @@ CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) {
}
CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) {
- ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
+ ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
if (!CXXUnit)
return clang_getNullLocation();
@@ -3195,7 +3993,7 @@ CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) {
}
CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) {
- ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
+ ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
if (!CXXUnit)
return clang_getNullRange();
@@ -3210,7 +4008,7 @@ void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range,
if (NumTokens)
*NumTokens = 0;
- ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
+ ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
if (!CXXUnit || !Tokens || !NumTokens)
return;
@@ -3246,6 +4044,7 @@ void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range,
const char *EffectiveBufferEnd = Buffer.data() + EndLocInfo.second;
llvm::SmallVector<CXToken, 32> CXTokens;
Token Tok;
+ bool previousWasAt = false;
do {
// Lex the next token
Lex.LexFromRawLexer(Tok);
@@ -3264,27 +4063,18 @@ void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range,
if (Tok.isLiteral()) {
CXTok.int_data[0] = CXToken_Literal;
CXTok.ptr_data = (void *)Tok.getLiteralData();
- } else if (Tok.is(tok::identifier)) {
+ } else if (Tok.is(tok::raw_identifier)) {
// Lookup the identifier to determine whether we have a keyword.
- std::pair<FileID, unsigned> LocInfo
- = SourceMgr.getDecomposedLoc(Tok.getLocation());
- bool Invalid = false;
- llvm::StringRef Buf
- = CXXUnit->getSourceManager().getBufferData(LocInfo.first, &Invalid);
- if (Invalid)
- return;
-
- const char *StartPos = Buf.data() + LocInfo.second;
IdentifierInfo *II
- = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok, StartPos);
+ = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok);
- if (II->getObjCKeywordID() != tok::objc_not_keyword) {
+ if ((II->getObjCKeywordID() != tok::objc_not_keyword) && previousWasAt) {
CXTok.int_data[0] = CXToken_Keyword;
}
else {
- CXTok.int_data[0] = II->getTokenID() == tok::identifier?
- CXToken_Identifier
- : CXToken_Keyword;
+ CXTok.int_data[0] = Tok.is(tok::identifier)
+ ? CXToken_Identifier
+ : CXToken_Keyword;
}
CXTok.ptr_data = II;
} else if (Tok.is(tok::comment)) {
@@ -3295,6 +4085,7 @@ void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range,
CXTok.ptr_data = 0;
}
CXTokens.push_back(CXTok);
+ previousWasAt = Tok.is(tok::at);
} while (Lex.getBufferLocation() <= EffectiveBufferEnd);
if (CXTokens.empty())
@@ -3327,6 +4118,7 @@ class AnnotateTokensWorker {
CXCursor *Cursors;
unsigned NumTokens;
unsigned TokIdx;
+ unsigned PreprocessingTokIdx;
CursorVisitor AnnotateVis;
SourceManager &SrcMgr;
@@ -3340,16 +4132,20 @@ class AnnotateTokensWorker {
public:
AnnotateTokensWorker(AnnotateTokensData &annotated,
CXToken *tokens, CXCursor *cursors, unsigned numTokens,
- ASTUnit *CXXUnit, SourceRange RegionOfInterest)
+ CXTranslationUnit tu, SourceRange RegionOfInterest)
: Annotated(annotated), Tokens(tokens), Cursors(cursors),
- NumTokens(numTokens), TokIdx(0),
- AnnotateVis(CXXUnit, AnnotateTokensVisitor, this,
+ NumTokens(numTokens), TokIdx(0), PreprocessingTokIdx(0),
+ AnnotateVis(tu,
+ AnnotateTokensVisitor, this,
Decl::MaxPCHLevel, RegionOfInterest),
- SrcMgr(CXXUnit->getSourceManager()) {}
+ SrcMgr(static_cast<ASTUnit*>(tu->TUData)->getSourceManager()) {}
void VisitChildren(CXCursor C) { AnnotateVis.VisitChildren(C); }
enum CXChildVisitResult Visit(CXCursor cursor, CXCursor parent);
void AnnotateTokens(CXCursor parent);
+ void AnnotateTokens() {
+ AnnotateTokens(clang_getTranslationUnitCursor(AnnotateVis.getTU()));
+ }
};
}
@@ -3360,7 +4156,9 @@ void AnnotateTokensWorker::AnnotateTokens(CXCursor parent) {
for (unsigned I = 0 ; I < TokIdx ; ++I) {
AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]);
- if (Pos != Annotated.end())
+ if (Pos != Annotated.end() &&
+ (clang_isInvalid(Cursors[I].kind) ||
+ Pos->second.kind != CXCursor_PreprocessingDirective))
Cursors[I] = Pos->second;
}
@@ -3376,16 +4174,65 @@ void AnnotateTokensWorker::AnnotateTokens(CXCursor parent) {
}
enum CXChildVisitResult
-AnnotateTokensWorker::Visit(CXCursor cursor, CXCursor parent) {
+AnnotateTokensWorker::Visit(CXCursor cursor, CXCursor parent) {
CXSourceLocation Loc = clang_getCursorLocation(cursor);
- // We can always annotate a preprocessing directive/macro instantiation.
- if (clang_isPreprocessing(cursor.kind)) {
- Annotated[Loc.int_data] = cursor;
+ SourceRange cursorRange = getRawCursorExtent(cursor);
+ if (cursorRange.isInvalid())
+ return CXChildVisit_Recurse;
+
+ if (clang_isPreprocessing(cursor.kind)) {
+ // For macro instantiations, just note where the beginning of the macro
+ // instantiation occurs.
+ if (cursor.kind == CXCursor_MacroInstantiation) {
+ Annotated[Loc.int_data] = cursor;
+ return CXChildVisit_Recurse;
+ }
+
+ // Items in the preprocessing record are kept separate from items in
+ // declarations, so we keep a separate token index.
+ unsigned SavedTokIdx = TokIdx;
+ TokIdx = PreprocessingTokIdx;
+
+ // Skip tokens up until we catch up to the beginning of the preprocessing
+ // entry.
+ while (MoreTokens()) {
+ const unsigned I = NextToken();
+ SourceLocation TokLoc = GetTokenLoc(I);
+ switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) {
+ case RangeBefore:
+ AdvanceToken();
+ continue;
+ case RangeAfter:
+ case RangeOverlap:
+ break;
+ }
+ break;
+ }
+
+ // Look at all of the tokens within this range.
+ while (MoreTokens()) {
+ const unsigned I = NextToken();
+ SourceLocation TokLoc = GetTokenLoc(I);
+ switch (LocationCompare(SrcMgr, TokLoc, cursorRange)) {
+ case RangeBefore:
+ assert(0 && "Infeasible");
+ case RangeAfter:
+ break;
+ case RangeOverlap:
+ Cursors[I] = cursor;
+ AdvanceToken();
+ continue;
+ }
+ break;
+ }
+
+ // Save the preprocessing token index; restore the non-preprocessing
+ // token index.
+ PreprocessingTokIdx = TokIdx;
+ TokIdx = SavedTokIdx;
return CXChildVisit_Recurse;
}
- SourceRange cursorRange = getRawCursorExtent(cursor);
-
if (cursorRange.isInvalid())
return CXChildVisit_Continue;
@@ -3405,13 +4252,13 @@ AnnotateTokensWorker::Visit(CXCursor cursor, CXCursor parent) {
if (TypeSourceInfo *TI = DD->getTypeSourceInfo()) {
TypeLoc TL = TI->getTypeLoc();
SourceLocation TLoc = TL.getSourceRange().getBegin();
- if (TLoc.isValid() &&
+ if (TLoc.isValid() && L.isValid() &&
SrcMgr.isBeforeInTranslationUnit(TLoc, L))
cursorRange.setBegin(TLoc);
}
}
}
-
+
// If the location of the cursor occurs within a macro instantiation, record
// the spelling location of the cursor in our annotation map. We can then
// paper over the token labelings during a post-processing step to try and
@@ -3423,7 +4270,7 @@ AnnotateTokensWorker::Visit(CXCursor cursor, CXCursor parent) {
// directive. Here we assume that the default construction of CXCursor
// results in CXCursor.kind being an initialized value (i.e., 0). If
// this isn't the case, we can fix by doing lookup + insertion.
-
+
CXCursor &oldC = Annotated[rawEncoding];
if (!clang_isPreprocessing(oldC.kind))
oldC = cursor;
@@ -3485,6 +4332,7 @@ AnnotateTokensWorker::Visit(CXCursor cursor, CXCursor parent) {
for (unsigned I = BeforeChildren; I != AfterChildren; ++I) {
if (!clang_isInvalid(clang_getCursorKind(Cursors[I])))
break;
+
Cursors[I] = cursor;
}
// Scan the tokens that are at the end of the cursor, but are not captured
@@ -3502,6 +4350,11 @@ static enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor,
return static_cast<AnnotateTokensWorker*>(client_data)->Visit(cursor, parent);
}
+// This gets run a separate thread to avoid stack blowout.
+static void runAnnotateTokensWorker(void *UserData) {
+ ((AnnotateTokensWorker*)UserData)->AnnotateTokens();
+}
+
extern "C" {
void clang_annotateTokens(CXTranslationUnit TU,
@@ -3511,14 +4364,14 @@ void clang_annotateTokens(CXTranslationUnit TU,
if (NumTokens == 0 || !Tokens || !Cursors)
return;
- ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
- if (!CXXUnit) {
- // Any token we don't specifically annotate will have a NULL cursor.
- const CXCursor &C = clang_getNullCursor();
- for (unsigned I = 0; I != NumTokens; ++I)
- Cursors[I] = C;
+ // Any token we don't specifically annotate will have a NULL cursor.
+ CXCursor C = clang_getNullCursor();
+ for (unsigned I = 0; I != NumTokens; ++I)
+ Cursors[I] = C;
+
+ ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
+ if (!CXXUnit)
return;
- }
ASTUnit::ConcurrencyCheck Check(*CXXUnit);
@@ -3562,9 +4415,9 @@ void clang_annotateTokens(CXTranslationUnit TU,
reprocess:
if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
// We have found a preprocessing directive. Gobble it up so that we
- // don't see it while preprocessing these tokens later, but keep track of
- // all of the token locations inside this preprocessing directive so that
- // we can annotate them appropriately.
+ // don't see it while preprocessing these tokens later, but keep track
+ // of all of the token locations inside this preprocessing directive so
+ // that we can annotate them appropriately.
//
// FIXME: Some simple tests here could identify macro definitions and
// #undefs, to provide specific cursor kinds for those.
@@ -3578,7 +4431,7 @@ void clang_annotateTokens(CXTranslationUnit TU,
CXCursor Cursor
= MakePreprocessingDirectiveCursor(SourceRange(Locations.front(),
Locations.back()),
- CXXUnit);
+ TU);
for (unsigned I = 0, N = Locations.size(); I != N; ++I) {
Annotated[Locations[I].getRawEncoding()] = Cursor;
}
@@ -3597,8 +4450,19 @@ void clang_annotateTokens(CXTranslationUnit TU,
// Annotate all of the source locations in the region of interest that map to
// a specific cursor.
AnnotateTokensWorker W(Annotated, Tokens, Cursors, NumTokens,
- CXXUnit, RegionOfInterest);
- W.AnnotateTokens(clang_getTranslationUnitCursor(CXXUnit));
+ TU, RegionOfInterest);
+
+ // Run the worker within a CrashRecoveryContext.
+ // FIXME: We use a ridiculous stack size here because the data-recursion
+ // algorithm uses a large stack frame than the non-data recursive version,
+ // and AnnotationTokensWorker currently transforms the data-recursion
+ // algorithm back into a traditional recursion by explicitly calling
+ // VisitChildren(). We will need to remove this explicit recursive call.
+ llvm::CrashRecoveryContext CRC;
+ if (!RunSafely(CRC, runAnnotateTokensWorker, &W,
+ GetSafetyThreadStackSize() * 2)) {
+ fprintf(stderr, "libclang: crash detected while annotating tokens\n");
+ }
}
} // end: extern "C"
@@ -3698,6 +4562,181 @@ CXLanguageKind clang_getCursorLanguage(CXCursor cursor) {
return CXLanguage_Invalid;
}
+
+ /// \brief If the given cursor is the "templated" declaration
+ /// descibing a class or function template, return the class or
+ /// function template.
+static Decl *maybeGetTemplateCursor(Decl *D) {
+ if (!D)
+ return 0;
+
+ if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
+ if (FunctionTemplateDecl *FunTmpl = FD->getDescribedFunctionTemplate())
+ return FunTmpl;
+
+ if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D))
+ if (ClassTemplateDecl *ClassTmpl = RD->getDescribedClassTemplate())
+ return ClassTmpl;
+
+ return D;
+}
+
+CXCursor clang_getCursorSemanticParent(CXCursor cursor) {
+ if (clang_isDeclaration(cursor.kind)) {
+ if (Decl *D = getCursorDecl(cursor)) {
+ DeclContext *DC = D->getDeclContext();
+ if (!DC)
+ return clang_getNullCursor();
+
+ return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)),
+ getCursorTU(cursor));
+ }
+ }
+
+ if (clang_isStatement(cursor.kind) || clang_isExpression(cursor.kind)) {
+ if (Decl *D = getCursorDecl(cursor))
+ return MakeCXCursor(D, getCursorTU(cursor));
+ }
+
+ return clang_getNullCursor();
+}
+
+CXCursor clang_getCursorLexicalParent(CXCursor cursor) {
+ if (clang_isDeclaration(cursor.kind)) {
+ if (Decl *D = getCursorDecl(cursor)) {
+ DeclContext *DC = D->getLexicalDeclContext();
+ if (!DC)
+ return clang_getNullCursor();
+
+ return MakeCXCursor(maybeGetTemplateCursor(cast<Decl>(DC)),
+ getCursorTU(cursor));
+ }
+ }
+
+ // FIXME: Note that we can't easily compute the lexical context of a
+ // statement or expression, so we return nothing.
+ return clang_getNullCursor();
+}
+
+static void CollectOverriddenMethods(DeclContext *Ctx,
+ ObjCMethodDecl *Method,
+ llvm::SmallVectorImpl<ObjCMethodDecl *> &Methods) {
+ if (!Ctx)
+ return;
+
+ // If we have a class or category implementation, jump straight to the
+ // interface.
+ if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(Ctx))
+ return CollectOverriddenMethods(Impl->getClassInterface(), Method, Methods);
+
+ ObjCContainerDecl *Container = dyn_cast<ObjCContainerDecl>(Ctx);
+ if (!Container)
+ return;
+
+ // Check whether we have a matching method at this level.
+ if (ObjCMethodDecl *Overridden = Container->getMethod(Method->getSelector(),
+ Method->isInstanceMethod()))
+ if (Method != Overridden) {
+ // We found an override at this level; there is no need to look
+ // into other protocols or categories.
+ Methods.push_back(Overridden);
+ return;
+ }
+
+ if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
+ for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
+ PEnd = Protocol->protocol_end();
+ P != PEnd; ++P)
+ CollectOverriddenMethods(*P, Method, Methods);
+ }
+
+ if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
+ for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
+ PEnd = Category->protocol_end();
+ P != PEnd; ++P)
+ CollectOverriddenMethods(*P, Method, Methods);
+ }
+
+ if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
+ for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
+ PEnd = Interface->protocol_end();
+ P != PEnd; ++P)
+ CollectOverriddenMethods(*P, Method, Methods);
+
+ for (ObjCCategoryDecl *Category = Interface->getCategoryList();
+ Category; Category = Category->getNextClassCategory())
+ CollectOverriddenMethods(Category, Method, Methods);
+
+ // We only look into the superclass if we haven't found anything yet.
+ if (Methods.empty())
+ if (ObjCInterfaceDecl *Super = Interface->getSuperClass())
+ return CollectOverriddenMethods(Super, Method, Methods);
+ }
+}
+
+void clang_getOverriddenCursors(CXCursor cursor,
+ CXCursor **overridden,
+ unsigned *num_overridden) {
+ if (overridden)
+ *overridden = 0;
+ if (num_overridden)
+ *num_overridden = 0;
+ if (!overridden || !num_overridden)
+ return;
+
+ if (!clang_isDeclaration(cursor.kind))
+ return;
+
+ Decl *D = getCursorDecl(cursor);
+ if (!D)
+ return;
+
+ // Handle C++ member functions.
+ CXTranslationUnit TU = getCursorTU(cursor);
+ if (CXXMethodDecl *CXXMethod = dyn_cast<CXXMethodDecl>(D)) {
+ *num_overridden = CXXMethod->size_overridden_methods();
+ if (!*num_overridden)
+ return;
+
+ *overridden = new CXCursor [*num_overridden];
+ unsigned I = 0;
+ for (CXXMethodDecl::method_iterator
+ M = CXXMethod->begin_overridden_methods(),
+ MEnd = CXXMethod->end_overridden_methods();
+ M != MEnd; (void)++M, ++I)
+ (*overridden)[I] = MakeCXCursor(const_cast<CXXMethodDecl*>(*M), TU);
+ return;
+ }
+
+ ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D);
+ if (!Method)
+ return;
+
+ // Handle Objective-C methods.
+ llvm::SmallVector<ObjCMethodDecl *, 4> Methods;
+ CollectOverriddenMethods(Method->getDeclContext(), Method, Methods);
+
+ if (Methods.empty())
+ return;
+
+ *num_overridden = Methods.size();
+ *overridden = new CXCursor [Methods.size()];
+ for (unsigned I = 0, N = Methods.size(); I != N; ++I)
+ (*overridden)[I] = MakeCXCursor(Methods[I], TU);
+}
+
+void clang_disposeOverriddenCursors(CXCursor *overridden) {
+ delete [] overridden;
+}
+
+CXFile clang_getIncludedFile(CXCursor cursor) {
+ if (cursor.kind != CXCursor_InclusionDirective)
+ return 0;
+
+ InclusionDirective *ID = getCursorInclusionDirective(cursor);
+ return (void *)ID->getFile();
+}
+
} // end: extern "C"
@@ -3728,63 +4767,43 @@ unsigned clang_CXXMethod_isStatic(CXCursor C) {
extern "C" {
CXType clang_getIBOutletCollectionType(CXCursor C) {
if (C.kind != CXCursor_IBOutletCollectionAttr)
- return cxtype::MakeCXType(QualType(), cxcursor::getCursorASTUnit(C));
+ return cxtype::MakeCXType(QualType(), cxcursor::getCursorTU(C));
IBOutletCollectionAttr *A =
cast<IBOutletCollectionAttr>(cxcursor::getCursorAttr(C));
- return cxtype::MakeCXType(A->getInterface(), cxcursor::getCursorASTUnit(C));
+ return cxtype::MakeCXType(A->getInterface(), cxcursor::getCursorTU(C));
}
} // end: extern "C"
//===----------------------------------------------------------------------===//
-// CXString Operations.
+// Misc. utility functions.
//===----------------------------------------------------------------------===//
-extern "C" {
-const char *clang_getCString(CXString string) {
- return string.Spelling;
-}
+/// Default to using an 8 MB stack size on "safety" threads.
+static unsigned SafetyStackThreadSize = 8 << 20;
-void clang_disposeString(CXString string) {
- if (string.MustFreeString && string.Spelling)
- free((void*)string.Spelling);
-}
+namespace clang {
-} // end: extern "C"
+bool RunSafely(llvm::CrashRecoveryContext &CRC,
+ void (*Fn)(void*), void *UserData,
+ unsigned Size) {
+ if (!Size)
+ Size = GetSafetyThreadStackSize();
+ if (Size)
+ return CRC.RunSafelyOnThread(Fn, UserData, Size);
+ return CRC.RunSafely(Fn, UserData);
+}
-namespace clang { namespace cxstring {
-CXString createCXString(const char *String, bool DupString){
- CXString Str;
- if (DupString) {
- Str.Spelling = strdup(String);
- Str.MustFreeString = 1;
- } else {
- Str.Spelling = String;
- Str.MustFreeString = 0;
- }
- return Str;
+unsigned GetSafetyThreadStackSize() {
+ return SafetyStackThreadSize;
}
-CXString createCXString(llvm::StringRef String, bool DupString) {
- CXString Result;
- if (DupString || (!String.empty() && String.data()[String.size()] != 0)) {
- char *Spelling = (char *)malloc(String.size() + 1);
- memmove(Spelling, String.data(), String.size());
- Spelling[String.size()] = 0;
- Result.Spelling = Spelling;
- Result.MustFreeString = 1;
- } else {
- Result.Spelling = String.data();
- Result.MustFreeString = 0;
- }
- return Result;
+void SetSafetyThreadStackSize(unsigned Value) {
+ SafetyStackThreadSize = Value;
}
-}}
-//===----------------------------------------------------------------------===//
-// Misc. utility functions.
-//===----------------------------------------------------------------------===//
+}
extern "C" {
diff --git a/tools/libclang/CIndexCXX.cpp b/tools/libclang/CIndexCXX.cpp
index 3ade5195d8f8..0f49f65c2b80 100644
--- a/tools/libclang/CIndexCXX.cpp
+++ b/tools/libclang/CIndexCXX.cpp
@@ -18,7 +18,6 @@
#include "clang/AST/DeclTemplate.h"
using namespace clang;
-using namespace clang::cxstring;
using namespace clang::cxcursor;
extern "C" {
@@ -56,7 +55,7 @@ enum CXCursorKind clang_getTemplateCursorKind(CXCursor C) {
if (TemplateDecl *Template
= dyn_cast_or_null<TemplateDecl>(getCursorDecl(C)))
return MakeCXCursor(Template->getTemplatedDecl(),
- getCursorASTUnit(C)).kind;
+ static_cast<CXTranslationUnit>(C.data[2])).kind;
break;
case CXCursor_ClassTemplatePartialSpecialization:
@@ -118,7 +117,7 @@ CXCursor clang_getSpecializedCursorTemplate(CXCursor C) {
if (!Template)
return clang_getNullCursor();
- return MakeCXCursor(Template, getCursorASTUnit(C));
+ return MakeCXCursor(Template, static_cast<CXTranslationUnit>(C.data[2]));
}
} // end extern "C"
diff --git a/tools/libclang/CIndexCodeCompletion.cpp b/tools/libclang/CIndexCodeCompletion.cpp
index d591c5defb2e..292719bebdae 100644
--- a/tools/libclang/CIndexCodeCompletion.cpp
+++ b/tools/libclang/CIndexCodeCompletion.cpp
@@ -13,6 +13,8 @@
//===----------------------------------------------------------------------===//
#include "CIndexer.h"
+#include "CXTranslationUnit.h"
+#include "CXString.h"
#include "CIndexDiagnostic.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Basic/FileManager.h"
@@ -22,11 +24,12 @@
#include "clang/Sema/CodeCompleteConsumer.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h"
+#include "llvm/Support/Atomic.h"
#include "llvm/Support/CrashRecoveryContext.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Timer.h"
#include "llvm/Support/raw_ostream.h"
-#include "llvm/System/Program.h"
+#include "llvm/Support/Program.h"
#include <cstdlib>
#include <cstdio>
@@ -42,31 +45,12 @@
using namespace clang;
using namespace clang::cxstring;
-namespace {
- /// \brief Stored representation of a completion string.
- ///
- /// This is the representation behind a CXCompletionString.
- class CXStoredCodeCompletionString : public CodeCompletionString {
- unsigned Priority;
- CXAvailabilityKind Availability;
-
- public:
- CXStoredCodeCompletionString(unsigned Priority,
- CXAvailabilityKind Availability)
- : Priority(Priority), Availability(Availability) { }
-
- unsigned getPriority() const { return Priority; }
- CXAvailabilityKind getAvailability() const { return Availability; }
- };
-}
-
extern "C" {
enum CXCompletionChunkKind
clang_getCompletionChunkKind(CXCompletionString completion_string,
unsigned chunk_number) {
- CXStoredCodeCompletionString *CCStr
- = (CXStoredCodeCompletionString *)completion_string;
+ CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
if (!CCStr || chunk_number >= CCStr->size())
return CXCompletionChunk_Text;
@@ -121,10 +105,9 @@ clang_getCompletionChunkKind(CXCompletionString completion_string,
CXString clang_getCompletionChunkText(CXCompletionString completion_string,
unsigned chunk_number) {
- CXStoredCodeCompletionString *CCStr
- = (CXStoredCodeCompletionString *)completion_string;
+ CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
if (!CCStr || chunk_number >= CCStr->size())
- return createCXString(0);
+ return createCXString((const char*)0);
switch ((*CCStr)[chunk_number].Kind) {
case CodeCompletionString::CK_TypedText:
@@ -146,11 +129,8 @@ CXString clang_getCompletionChunkText(CXCompletionString completion_string,
case CodeCompletionString::CK_SemiColon:
case CodeCompletionString::CK_Equal:
case CodeCompletionString::CK_HorizontalSpace:
- return createCXString((*CCStr)[chunk_number].Text, false);
-
case CodeCompletionString::CK_VerticalSpace:
- // FIXME: Temporary hack until we figure out how to handle vertical space.
- return createCXString(" ");
+ return createCXString((*CCStr)[chunk_number].Text, false);
case CodeCompletionString::CK_Optional:
// Note: treated as an empty text block.
@@ -158,15 +138,14 @@ CXString clang_getCompletionChunkText(CXCompletionString completion_string,
}
// Should be unreachable, but let's be careful.
- return createCXString(0);
+ return createCXString((const char*)0);
}
CXCompletionString
clang_getCompletionChunkCompletionString(CXCompletionString completion_string,
unsigned chunk_number) {
- CXStoredCodeCompletionString *CCStr
- = (CXStoredCodeCompletionString *)completion_string;
+ CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
if (!CCStr || chunk_number >= CCStr->size())
return 0;
@@ -203,32 +182,20 @@ clang_getCompletionChunkCompletionString(CXCompletionString completion_string,
}
unsigned clang_getNumCompletionChunks(CXCompletionString completion_string) {
- CXStoredCodeCompletionString *CCStr
- = (CXStoredCodeCompletionString *)completion_string;
+ CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
return CCStr? CCStr->size() : 0;
}
unsigned clang_getCompletionPriority(CXCompletionString completion_string) {
- CXStoredCodeCompletionString *CCStr
- = (CXStoredCodeCompletionString *)completion_string;
+ CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
return CCStr? CCStr->getPriority() : unsigned(CCP_Unlikely);
}
enum CXAvailabilityKind
clang_getCompletionAvailability(CXCompletionString completion_string) {
- CXStoredCodeCompletionString *CCStr
- = (CXStoredCodeCompletionString *)completion_string;
- return CCStr? CCStr->getAvailability() : CXAvailability_Available;
-}
-
-static bool ReadUnsigned(const char *&Memory, const char *MemoryEnd,
- unsigned &Value) {
- if (Memory + sizeof(unsigned) > MemoryEnd)
- return true;
-
- memmove(&Value, Memory, sizeof(unsigned));
- Memory += sizeof(unsigned);
- return false;
+ CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
+ return CCStr? static_cast<CXAvailabilityKind>(CCStr->getAvailability())
+ : CXAvailability_Available;
}
/// \brief The CXCodeCompleteResults structure we allocate internally;
@@ -246,345 +213,119 @@ struct AllocatedCXCodeCompleteResults : public CXCodeCompleteResults {
/// \brief Language options used to adjust source locations.
LangOptions LangOpts;
- /// \brief Source manager, used for diagnostics.
- SourceManager SourceMgr;
-
+ FileSystemOptions FileSystemOpts;
+
/// \brief File manager, used for diagnostics.
FileManager FileMgr;
+
+ /// \brief Source manager, used for diagnostics.
+ SourceManager SourceMgr;
/// \brief Temporary files that should be removed once we have finished
/// with the code-completion results.
std::vector<llvm::sys::Path> TemporaryFiles;
- /// \brief Temporary buffers that will be deleted once we have finished with the code-completion results.
+ /// \brief Temporary buffers that will be deleted once we have finished with
+ /// the code-completion results.
llvm::SmallVector<const llvm::MemoryBuffer *, 1> TemporaryBuffers;
+
+ /// \brief Allocator used to store globally cached code-completion results.
+ llvm::IntrusiveRefCntPtr<clang::GlobalCodeCompletionAllocator>
+ CachedCompletionAllocator;
+
+ /// \brief Allocator used to store code completion results.
+ clang::CodeCompletionAllocator CodeCompletionAllocator;
};
+/// \brief Tracks the number of code-completion result objects that are
+/// currently active.
+///
+/// Used for debugging purposes only.
+static llvm::sys::cas_flag CodeCompletionResultObjects;
+
AllocatedCXCodeCompleteResults::AllocatedCXCodeCompleteResults()
- : CXCodeCompleteResults(), Diag(new Diagnostic), SourceMgr(*Diag) { }
+ : CXCodeCompleteResults(),
+ Diag(new Diagnostic(
+ llvm::IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs))),
+ FileMgr(FileSystemOpts),
+ SourceMgr(*Diag, FileMgr) {
+ if (getenv("LIBCLANG_OBJTRACKING")) {
+ llvm::sys::AtomicIncrement(&CodeCompletionResultObjects);
+ fprintf(stderr, "+++ %d completion results\n", CodeCompletionResultObjects);
+ }
+}
AllocatedCXCodeCompleteResults::~AllocatedCXCodeCompleteResults() {
- for (unsigned I = 0, N = NumResults; I != N; ++I)
- delete (CXStoredCodeCompletionString *)Results[I].CompletionString;
delete [] Results;
for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I)
TemporaryFiles[I].eraseFromDisk();
for (unsigned I = 0, N = TemporaryBuffers.size(); I != N; ++I)
delete TemporaryBuffers[I];
-}
-
-CXCodeCompleteResults *clang_codeComplete(CXIndex CIdx,
- const char *source_filename,
- int num_command_line_args,
- const char * const *command_line_args,
- unsigned num_unsaved_files,
- struct CXUnsavedFile *unsaved_files,
- const char *complete_filename,
- unsigned complete_line,
- unsigned complete_column) {
-#ifdef UDP_CODE_COMPLETION_LOGGER
-#ifdef UDP_CODE_COMPLETION_LOGGER_PORT
- const llvm::TimeRecord &StartTime = llvm::TimeRecord::getCurrentTime();
-#endif
-#endif
-
- bool EnableLogging = getenv("LIBCLANG_CODE_COMPLETION_LOGGING") != 0;
-
- llvm::OwningPtr<llvm::NamedRegionTimer> CCTimer;
- if (getenv("LIBCLANG_TIMING")) {
- llvm::SmallString<128> TimerName;
- llvm::raw_svector_ostream TimerNameOut(TimerName);
- TimerNameOut << "Code completion (out-of-process) @ " << complete_filename
- << ":" << complete_line << ":" << complete_column;
- CCTimer.reset(new llvm::NamedRegionTimer(TimerNameOut.str()));
- }
-
- // The indexer, which is mainly used to determine where diagnostics go.
- CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
-
- // Configure the diagnostics.
- DiagnosticOptions DiagOpts;
- llvm::IntrusiveRefCntPtr<Diagnostic> Diags;
- Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0);
-
- // The set of temporary files that we've built.
- std::vector<llvm::sys::Path> TemporaryFiles;
-
- // Build up the arguments for invoking 'clang'.
- std::vector<const char *> argv;
-
- // First add the complete path to the 'clang' executable.
- llvm::sys::Path ClangPath = CXXIdx->getClangPath();
- argv.push_back(ClangPath.c_str());
-
- // Always use Clang C++ support.
- argv.push_back("-ccc-clang-cxx");
-
- // Add the '-fsyntax-only' argument so that we only perform a basic
- // syntax check of the code.
- argv.push_back("-fsyntax-only");
-
- // Add the appropriate '-code-completion-at=file:line:column' argument
- // to perform code completion, with an "-Xclang" preceding it.
- std::string code_complete_at;
- code_complete_at += complete_filename;
- code_complete_at += ":";
- code_complete_at += llvm::utostr(complete_line);
- code_complete_at += ":";
- code_complete_at += llvm::utostr(complete_column);
- argv.push_back("-Xclang");
- argv.push_back("-code-completion-at");
- argv.push_back("-Xclang");
- argv.push_back(code_complete_at.c_str());
- argv.push_back("-Xclang");
- argv.push_back("-no-code-completion-debug-printer");
- argv.push_back("-Xclang");
- argv.push_back("-code-completion-macros");
- argv.push_back("-fdiagnostics-binary");
-
- // Remap any unsaved files to temporary files.
- std::vector<std::string> RemapArgs;
- if (RemapFiles(num_unsaved_files, unsaved_files, RemapArgs, TemporaryFiles))
- return 0;
-
- // The pointers into the elements of RemapArgs are stable because we
- // won't be adding anything to RemapArgs after this point.
- for (unsigned i = 0, e = RemapArgs.size(); i != e; ++i)
- argv.push_back(RemapArgs[i].c_str());
-
- // Add the source file name (FIXME: later, we'll want to build temporary
- // file from the buffer, or just feed the source text via standard input).
- if (source_filename)
- argv.push_back(source_filename);
-
- // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'.
- for (int i = 0; i < num_command_line_args; ++i)
- if (const char *arg = command_line_args[i]) {
- if (strcmp(arg, "-o") == 0) {
- ++i; // Also skip the matching argument.
- continue;
- }
- if (strcmp(arg, "-emit-ast") == 0 ||
- strcmp(arg, "-c") == 0 ||
- strcmp(arg, "-fsyntax-only") == 0) {
- continue;
- }
-
- // Keep the argument.
- argv.push_back(arg);
- }
-
- if (EnableLogging) {
- std::string Log = ClangPath.str();
- for (unsigned I = 0, N = argv.size(); I != N; ++I) {
- Log += ' ';
- Log += argv[I];
- }
- fprintf(stderr, "libclang (Code Completion): %s\n", Log.c_str());
- }
-
- // Add the null terminator.
- argv.push_back(NULL);
-
- // Generate a temporary name for the code-completion results file.
- char tmpFile[L_tmpnam];
- char *tmpFileName = tmpnam(tmpFile);
- llvm::sys::Path ResultsFile(tmpFileName);
- TemporaryFiles.push_back(ResultsFile);
-
- // Generate a temporary name for the diagnostics file.
- char tmpFileResults[L_tmpnam];
- char *tmpResultsFileName = tmpnam(tmpFileResults);
- llvm::sys::Path DiagnosticsFile(tmpResultsFileName);
- TemporaryFiles.push_back(DiagnosticsFile);
-
-
-
- // Invoke 'clang'.
- llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null
- // on Unix or NUL (Windows).
- std::string ErrMsg;
- const llvm::sys::Path *Redirects[] = { &DevNull, &ResultsFile,
- &DiagnosticsFile, 0 };
- llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL,
- /* redirects */ &Redirects[0],
- /* secondsToWait */ 0,
- /* memoryLimits */ 0, &ErrMsg);
-
- if (!ErrMsg.empty()) {
- std::string AllArgs;
- for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
- I != E; ++I) {
- AllArgs += ' ';
- if (*I)
- AllArgs += *I;
- }
-
- Diags->Report(diag::err_fe_invoking) << AllArgs << ErrMsg;
- }
-
- // Parse the resulting source file to find code-completion results.
- using llvm::MemoryBuffer;
- using llvm::StringRef;
- AllocatedCXCodeCompleteResults *Results = new AllocatedCXCodeCompleteResults;
- Results->Results = 0;
- Results->NumResults = 0;
- // FIXME: Set Results->LangOpts!
- if (MemoryBuffer *F = MemoryBuffer::getFile(ResultsFile.c_str())) {
- llvm::SmallVector<CXCompletionResult, 4> CompletionResults;
- StringRef Buffer = F->getBuffer();
- for (const char *Str = Buffer.data(), *StrEnd = Str + Buffer.size();
- Str < StrEnd;) {
- unsigned KindValue;
- if (ReadUnsigned(Str, StrEnd, KindValue))
- break;
-
- unsigned Priority;
- if (ReadUnsigned(Str, StrEnd, Priority))
- break;
-
- unsigned Availability;
- if (ReadUnsigned(Str, StrEnd, Availability))
- break;
-
- CXStoredCodeCompletionString *CCStr
- = new CXStoredCodeCompletionString(Priority,
- (CXAvailabilityKind)Availability);
- if (!CCStr->Deserialize(Str, StrEnd)) {
- delete CCStr;
- continue;
- }
-
- if (!CCStr->empty()) {
- // Vend the code-completion result to the caller.
- CXCompletionResult Result;
- Result.CursorKind = (CXCursorKind)KindValue;
- Result.CompletionString = CCStr;
- CompletionResults.push_back(Result);
- }
- };
-
- // Allocate the results.
- Results->Results = new CXCompletionResult [CompletionResults.size()];
- Results->NumResults = CompletionResults.size();
- memcpy(Results->Results, CompletionResults.data(),
- CompletionResults.size() * sizeof(CXCompletionResult));
- Results->TemporaryBuffers.push_back(F);
- }
-
- LoadSerializedDiagnostics(DiagnosticsFile, num_unsaved_files, unsaved_files,
- Results->FileMgr, Results->SourceMgr,
- Results->Diagnostics);
-
- // Make sure we delete temporary files when the code-completion results are
- // destroyed.
- Results->TemporaryFiles.swap(TemporaryFiles);
-
-#ifdef UDP_CODE_COMPLETION_LOGGER
-#ifdef UDP_CODE_COMPLETION_LOGGER_PORT
- const llvm::TimeRecord &EndTime = llvm::TimeRecord::getCurrentTime();
- llvm::SmallString<256> LogResult;
- llvm::raw_svector_ostream os(LogResult);
-
- // Figure out the language and whether or not it uses PCH.
- const char *lang = 0;
- bool usesPCH = false;
- for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
- I != E; ++I) {
- if (*I == 0)
- continue;
- if (strcmp(*I, "-x") == 0) {
- if (I + 1 != E) {
- lang = *(++I);
- continue;
- }
- }
- else if (strcmp(*I, "-include") == 0) {
- if (I+1 != E) {
- const char *arg = *(++I);
- llvm::SmallString<512> pchName;
- {
- llvm::raw_svector_ostream os(pchName);
- os << arg << ".pth";
- }
- pchName.push_back('\0');
- struct stat stat_results;
- if (stat(pchName.data(), &stat_results) == 0)
- usesPCH = true;
- continue;
- }
- }
- }
-
- os << "{ ";
- os << "\"wall\": " << (EndTime.getWallTime() - StartTime.getWallTime());
- os << ", \"numRes\": " << Results->NumResults;
- os << ", \"diags\": " << Results->Diagnostics.size();
- os << ", \"pch\": " << (usesPCH ? "true" : "false");
- os << ", \"lang\": \"" << (lang ? lang : "<unknown>") << '"';
- const char *name = getlogin();
- os << ", \"user\": \"" << (name ? name : "unknown") << '"';
- os << ", \"clangVer\": \"" << getClangFullVersion() << '"';
- os << " }";
-
- llvm::StringRef res = os.str();
- if (res.size() > 0) {
- do {
- // Setup the UDP socket.
- struct sockaddr_in servaddr;
- bzero(&servaddr, sizeof(servaddr));
- servaddr.sin_family = AF_INET;
- servaddr.sin_port = htons(UDP_CODE_COMPLETION_LOGGER_PORT);
- if (inet_pton(AF_INET, UDP_CODE_COMPLETION_LOGGER,
- &servaddr.sin_addr) <= 0)
- break;
-
- int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
- if (sockfd < 0)
- break;
-
- sendto(sockfd, res.data(), res.size(), 0,
- (struct sockaddr *)&servaddr, sizeof(servaddr));
- close(sockfd);
- }
- while (false);
- }
-#endif
-#endif
- clang_sortCodeCompletionResults(Results->Results, Results->NumResults);
- return Results;
+ if (getenv("LIBCLANG_OBJTRACKING")) {
+ llvm::sys::AtomicDecrement(&CodeCompletionResultObjects);
+ fprintf(stderr, "--- %d completion results\n", CodeCompletionResultObjects);
+ }
}
-
+
} // end extern "C"
namespace {
class CaptureCompletionResults : public CodeCompleteConsumer {
AllocatedCXCodeCompleteResults &AllocatedResults;
-
+ llvm::SmallVector<CXCompletionResult, 16> StoredResults;
public:
- explicit CaptureCompletionResults(AllocatedCXCodeCompleteResults &Results)
+ CaptureCompletionResults(AllocatedCXCodeCompleteResults &Results)
: CodeCompleteConsumer(true, false, true, false),
AllocatedResults(Results) { }
-
+ ~CaptureCompletionResults() { Finish(); }
+
virtual void ProcessCodeCompleteResults(Sema &S,
CodeCompletionContext Context,
CodeCompletionResult *Results,
unsigned NumResults) {
- AllocatedResults.Results = new CXCompletionResult [NumResults];
- AllocatedResults.NumResults = NumResults;
+ StoredResults.reserve(StoredResults.size() + NumResults);
for (unsigned I = 0; I != NumResults; ++I) {
- CXStoredCodeCompletionString *StoredCompletion
- = new CXStoredCodeCompletionString(Results[I].Priority,
- Results[I].Availability);
- (void)Results[I].CreateCodeCompletionString(S, StoredCompletion);
- AllocatedResults.Results[I].CursorKind = Results[I].CursorKind;
- AllocatedResults.Results[I].CompletionString = StoredCompletion;
+ CodeCompletionString *StoredCompletion
+ = Results[I].CreateCodeCompletionString(S,
+ AllocatedResults.CodeCompletionAllocator);
+
+ CXCompletionResult R;
+ R.CursorKind = Results[I].CursorKind;
+ R.CompletionString = StoredCompletion;
+ StoredResults.push_back(R);
+ }
+ }
+
+ virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
+ OverloadCandidate *Candidates,
+ unsigned NumCandidates) {
+ StoredResults.reserve(StoredResults.size() + NumCandidates);
+ for (unsigned I = 0; I != NumCandidates; ++I) {
+ CodeCompletionString *StoredCompletion
+ = Candidates[I].CreateSignatureString(CurrentArg, S,
+ AllocatedResults.CodeCompletionAllocator);
+
+ CXCompletionResult R;
+ R.CursorKind = CXCursor_NotImplemented;
+ R.CompletionString = StoredCompletion;
+ StoredResults.push_back(R);
}
}
- // FIXME: Add ProcessOverloadCandidates?
+ virtual CodeCompletionAllocator &getAllocator() {
+ return AllocatedResults.CodeCompletionAllocator;
+ }
+
+ private:
+ void Finish() {
+ AllocatedResults.Results = new CXCompletionResult [StoredResults.size()];
+ AllocatedResults.NumResults = StoredResults.size();
+ std::memcpy(AllocatedResults.Results, StoredResults.data(),
+ StoredResults.size() * sizeof(CXCompletionResult));
+ StoredResults.clear();
+ }
};
}
@@ -618,10 +359,12 @@ void clang_codeCompleteAt_Impl(void *UserData) {
bool EnableLogging = getenv("LIBCLANG_CODE_COMPLETION_LOGGING") != 0;
- ASTUnit *AST = static_cast<ASTUnit *>(TU);
+ ASTUnit *AST = static_cast<ASTUnit *>(TU->TUData);
if (!AST)
return;
+ ASTUnit::ConcurrencyCheck Check(*AST);
+
// Perform the remapping of source files.
llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles;
for (unsigned I = 0; I != num_unsaved_files; ++I) {
@@ -640,7 +383,7 @@ void clang_codeCompleteAt_Impl(void *UserData) {
AllocatedCXCodeCompleteResults *Results = new AllocatedCXCodeCompleteResults;
Results->Results = 0;
Results->NumResults = 0;
-
+
// Create a code-completion consumer to capture the results.
CaptureCompletionResults Capture(*Results);
@@ -653,6 +396,12 @@ void clang_codeCompleteAt_Impl(void *UserData) {
*Results->Diag, Results->LangOpts, Results->SourceMgr,
Results->FileMgr, Results->Diagnostics,
Results->TemporaryBuffers);
+
+ // Keep a reference to the allocator used for cached global completions, so
+ // that we can be sure that the memory used by our code completion strings
+ // doesn't get freed due to subsequent reparses (while the code completion
+ // results are still active).
+ Results->CachedCompletionAllocator = AST->getCachedCompletionAllocator();
@@ -742,9 +491,9 @@ CXCodeCompleteResults *clang_codeCompleteAt(CXTranslationUnit TU,
options, 0 };
llvm::CrashRecoveryContext CRC;
- if (!CRC.RunSafely(clang_codeCompleteAt_Impl, &CCAI)) {
+ if (!RunSafely(CRC, clang_codeCompleteAt_Impl, &CCAI)) {
fprintf(stderr, "libclang: crash detected in code completion\n");
- static_cast<ASTUnit *>(TU)->setUnsafeToFree(true);
+ static_cast<ASTUnit *>(TU->TUData)->setUnsafeToFree(true);
return 0;
}
@@ -788,28 +537,74 @@ clang_codeCompleteGetDiagnostic(CXCodeCompleteResults *ResultsIn,
} // end extern "C"
+/// \brief Simple utility function that appends a \p New string to the given
+/// \p Old string, using the \p Buffer for storage.
+///
+/// \param Old The string to which we are appending. This parameter will be
+/// updated to reflect the complete string.
+///
+///
+/// \param New The string to append to \p Old.
+///
+/// \param Buffer A buffer that stores the actual, concatenated string. It will
+/// be used if the old string is already-non-empty.
+static void AppendToString(llvm::StringRef &Old, llvm::StringRef New,
+ llvm::SmallString<256> &Buffer) {
+ if (Old.empty()) {
+ Old = New;
+ return;
+ }
+
+ if (Buffer.empty())
+ Buffer.append(Old.begin(), Old.end());
+ Buffer.append(New.begin(), New.end());
+ Old = Buffer.str();
+}
+
+/// \brief Get the typed-text blocks from the given code-completion string
+/// and return them as a single string.
+///
+/// \param String The code-completion string whose typed-text blocks will be
+/// concatenated.
+///
+/// \param Buffer A buffer used for storage of the completed name.
+static llvm::StringRef GetTypedName(CodeCompletionString *String,
+ llvm::SmallString<256> &Buffer) {
+ llvm::StringRef Result;
+ for (CodeCompletionString::iterator C = String->begin(), CEnd = String->end();
+ C != CEnd; ++C) {
+ if (C->Kind == CodeCompletionString::CK_TypedText)
+ AppendToString(Result, C->Text, Buffer);
+ }
+
+ return Result;
+}
+
namespace {
struct OrderCompletionResults {
bool operator()(const CXCompletionResult &XR,
const CXCompletionResult &YR) const {
- CXStoredCodeCompletionString *X
- = (CXStoredCodeCompletionString *)XR.CompletionString;
- CXStoredCodeCompletionString *Y
- = (CXStoredCodeCompletionString *)YR.CompletionString;
+ CodeCompletionString *X
+ = (CodeCompletionString *)XR.CompletionString;
+ CodeCompletionString *Y
+ = (CodeCompletionString *)YR.CompletionString;
- const char *XText = X->getTypedText();
- const char *YText = Y->getTypedText();
- if (!XText || !YText)
- return XText != 0;
+ llvm::SmallString<256> XBuffer;
+ llvm::StringRef XText = GetTypedName(X, XBuffer);
+ llvm::SmallString<256> YBuffer;
+ llvm::StringRef YText = GetTypedName(Y, YBuffer);
- int result = llvm::StringRef(XText).compare_lower(YText);
+ if (XText.empty() || YText.empty())
+ return !XText.empty();
+
+ int result = XText.compare_lower(YText);
if (result < 0)
return true;
if (result > 0)
return false;
- result = llvm::StringRef(XText).compare(YText);
- return result;
+ result = XText.compare(YText);
+ return result < 0;
}
};
}
diff --git a/tools/libclang/CIndexDiagnostic.cpp b/tools/libclang/CIndexDiagnostic.cpp
index 531992efebce..fa3b1cec7fb3 100644
--- a/tools/libclang/CIndexDiagnostic.cpp
+++ b/tools/libclang/CIndexDiagnostic.cpp
@@ -12,7 +12,9 @@
\*===----------------------------------------------------------------------===*/
#include "CIndexDiagnostic.h"
#include "CIndexer.h"
+#include "CXTranslationUnit.h"
#include "CXSourceLocation.h"
+#include "CXString.h"
#include "clang/Frontend/ASTUnit.h"
#include "clang/Frontend/FrontendDiagnostic.h"
@@ -32,12 +34,12 @@ using namespace llvm;
extern "C" {
unsigned clang_getNumDiagnostics(CXTranslationUnit Unit) {
- ASTUnit *CXXUnit = static_cast<ASTUnit *>(Unit);
+ ASTUnit *CXXUnit = static_cast<ASTUnit *>(Unit->TUData);
return CXXUnit? CXXUnit->stored_diag_size() : 0;
}
CXDiagnostic clang_getDiagnostic(CXTranslationUnit Unit, unsigned Index) {
- ASTUnit *CXXUnit = static_cast<ASTUnit *>(Unit);
+ ASTUnit *CXXUnit = static_cast<ASTUnit *>(Unit->TUData);
if (!CXXUnit || Index >= CXXUnit->stored_diag_size())
return 0;
@@ -56,10 +58,6 @@ CXString clang_formatDiagnostic(CXDiagnostic Diagnostic, unsigned Options) {
CXDiagnosticSeverity Severity = clang_getDiagnosticSeverity(Diagnostic);
- // Ignore diagnostics that should be ignored.
- if (Severity == CXDiagnostic_Ignored)
- return createCXString("");
-
llvm::SmallString<256> Str;
llvm::raw_svector_ostream Out(Str);
@@ -68,8 +66,8 @@ CXString clang_formatDiagnostic(CXDiagnostic Diagnostic, unsigned Options) {
// and source ranges.
CXFile File;
unsigned Line, Column;
- clang_getInstantiationLocation(clang_getDiagnosticLocation(Diagnostic),
- &File, &Line, &Column, 0);
+ clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
+ &File, &Line, &Column, 0);
if (File) {
CXString FName = clang_getFileName(File);
Out << clang_getCString(FName) << ":" << Line << ":";
@@ -85,11 +83,11 @@ CXString clang_formatDiagnostic(CXDiagnostic Diagnostic, unsigned Options) {
CXSourceRange Range = clang_getDiagnosticRange(Diagnostic, I);
unsigned StartLine, StartColumn, EndLine, EndColumn;
- clang_getInstantiationLocation(clang_getRangeStart(Range),
- &StartFile, &StartLine, &StartColumn,
- 0);
- clang_getInstantiationLocation(clang_getRangeEnd(Range),
- &EndFile, &EndLine, &EndColumn, 0);
+ clang_getSpellingLocation(clang_getRangeStart(Range),
+ &StartFile, &StartLine, &StartColumn,
+ 0);
+ clang_getSpellingLocation(clang_getRangeEnd(Range),
+ &EndFile, &EndLine, &EndColumn, 0);
if (StartFile != EndFile || StartFile != File)
continue;
@@ -101,9 +99,9 @@ CXString clang_formatDiagnostic(CXDiagnostic Diagnostic, unsigned Options) {
if (PrintedRange)
Out << ":";
}
+
+ Out << " ";
}
-
- Out << " ";
}
/* Print warning/error/etc. */
@@ -121,11 +119,61 @@ CXString clang_formatDiagnostic(CXDiagnostic Diagnostic, unsigned Options) {
else
Out << "<no diagnostic text>";
clang_disposeString(Text);
+
+ if (Options & (CXDiagnostic_DisplayOption | CXDiagnostic_DisplayCategoryId |
+ CXDiagnostic_DisplayCategoryName)) {
+ bool NeedBracket = true;
+ bool NeedComma = false;
+
+ if (Options & CXDiagnostic_DisplayOption) {
+ CXString OptionName = clang_getDiagnosticOption(Diagnostic, 0);
+ if (const char *OptionText = clang_getCString(OptionName)) {
+ if (OptionText[0]) {
+ Out << " [" << OptionText;
+ NeedBracket = false;
+ NeedComma = true;
+ }
+ }
+ clang_disposeString(OptionName);
+ }
+
+ if (Options & (CXDiagnostic_DisplayCategoryId |
+ CXDiagnostic_DisplayCategoryName)) {
+ if (unsigned CategoryID = clang_getDiagnosticCategory(Diagnostic)) {
+ if (Options & CXDiagnostic_DisplayCategoryId) {
+ if (NeedBracket)
+ Out << " [";
+ if (NeedComma)
+ Out << ", ";
+ Out << CategoryID;
+ NeedBracket = false;
+ NeedComma = true;
+ }
+
+ if (Options & CXDiagnostic_DisplayCategoryName) {
+ CXString CategoryName = clang_getDiagnosticCategoryName(CategoryID);
+ if (NeedBracket)
+ Out << " [";
+ if (NeedComma)
+ Out << ", ";
+ Out << clang_getCString(CategoryName);
+ NeedBracket = false;
+ NeedComma = true;
+ clang_disposeString(CategoryName);
+ }
+ }
+ }
+
+ if (!NeedBracket)
+ Out << "]";
+ }
+
return createCXString(Out.str(), true);
}
unsigned clang_defaultDiagnosticDisplayOptions() {
- return CXDiagnostic_DisplaySourceLocation | CXDiagnostic_DisplayColumn;
+ return CXDiagnostic_DisplaySourceLocation | CXDiagnostic_DisplayColumn |
+ CXDiagnostic_DisplayOption;
}
enum CXDiagnosticSeverity clang_getDiagnosticSeverity(CXDiagnostic Diag) {
@@ -163,6 +211,47 @@ CXString clang_getDiagnosticSpelling(CXDiagnostic Diag) {
return createCXString(StoredDiag->Diag.getMessage(), false);
}
+CXString clang_getDiagnosticOption(CXDiagnostic Diag, CXString *Disable) {
+ if (Disable)
+ *Disable = createCXString("");
+
+ CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
+ if (!StoredDiag)
+ return createCXString("");
+
+ unsigned ID = StoredDiag->Diag.getID();
+ if (const char *Option = DiagnosticIDs::getWarningOptionForDiag(ID)) {
+ if (Disable)
+ *Disable = createCXString((llvm::Twine("-Wno-") + Option).str());
+ return createCXString((llvm::Twine("-W") + Option).str());
+ }
+
+ if (ID == diag::fatal_too_many_errors) {
+ if (Disable)
+ *Disable = createCXString("-ferror-limit=0");
+ return createCXString("-ferror-limit=");
+ }
+
+ bool EnabledByDefault;
+ if (DiagnosticIDs::isBuiltinExtensionDiag(ID, EnabledByDefault) &&
+ !EnabledByDefault)
+ return createCXString("-pedantic");
+
+ return createCXString("");
+}
+
+unsigned clang_getDiagnosticCategory(CXDiagnostic Diag) {
+ CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
+ if (!StoredDiag)
+ return 0;
+
+ return DiagnosticIDs::getCategoryNumberForDiag(StoredDiag->Diag.getID());
+}
+
+CXString clang_getDiagnosticCategoryName(unsigned Category) {
+ return createCXString(DiagnosticIDs::getCategoryNameFromID(Category));
+}
+
unsigned clang_getDiagnosticNumRanges(CXDiagnostic Diag) {
CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
if (!StoredDiag || StoredDiag->Diag.getLocation().isInvalid())
@@ -217,56 +306,3 @@ CXString clang_getDiagnosticFixIt(CXDiagnostic Diagnostic, unsigned FixIt,
}
} // end extern "C"
-
-void clang::LoadSerializedDiagnostics(const llvm::sys::Path &DiagnosticsPath,
- unsigned num_unsaved_files,
- struct CXUnsavedFile *unsaved_files,
- FileManager &FileMgr,
- SourceManager &SourceMgr,
- SmallVectorImpl<StoredDiagnostic> &Diags) {
- using llvm::MemoryBuffer;
- using llvm::StringRef;
- MemoryBuffer *F = MemoryBuffer::getFile(DiagnosticsPath.c_str());
- if (!F)
- return;
-
- // Enter the unsaved files into the file manager.
- for (unsigned I = 0; I != num_unsaved_files; ++I) {
- const FileEntry *File = FileMgr.getVirtualFile(unsaved_files[I].Filename,
- unsaved_files[I].Length,
- 0);
- if (!File) {
- // FIXME: Hard to localize when we have no diagnostics engine!
- Diags.push_back(StoredDiagnostic(Diagnostic::Fatal,
- (Twine("could not remap from missing file ") +
- unsaved_files[I].Filename).str()));
- delete F;
- return;
- }
-
- MemoryBuffer *Buffer
- = MemoryBuffer::getMemBuffer(unsaved_files[I].Contents,
- unsaved_files[I].Contents + unsaved_files[I].Length);
- if (!Buffer) {
- delete F;
- return;
- }
-
- SourceMgr.overrideFileContents(File, Buffer);
- SourceMgr.createFileID(File, SourceLocation(), SrcMgr::C_User);
- }
-
- // Parse the diagnostics, emitting them one by one until we've
- // exhausted the data.
- StringRef Buffer = F->getBuffer();
- const char *Memory = Buffer.data(), *MemoryEnd = Memory + Buffer.size();
- while (Memory != MemoryEnd) {
- StoredDiagnostic Stored = StoredDiagnostic::Deserialize(FileMgr, SourceMgr,
- Memory, MemoryEnd);
- if (!Stored)
- break;
-
- Diags.push_back(Stored);
- }
- delete F;
-}
diff --git a/tools/libclang/CIndexDiagnostic.h b/tools/libclang/CIndexDiagnostic.h
index 919c21cfdbe4..0d935fae660f 100644
--- a/tools/libclang/CIndexDiagnostic.h
+++ b/tools/libclang/CIndexDiagnostic.h
@@ -13,21 +13,10 @@
#ifndef LLVM_CLANG_CINDEX_DIAGNOSTIC_H
#define LLVM_CLANG_CINDEX_DIAGNOSTIC_H
-struct CXUnsavedFile;
-
-namespace llvm {
-template<typename T> class SmallVectorImpl;
-namespace sys { class Path; }
-}
-
namespace clang {
-class Diagnostic;
-class FileManager;
class LangOptions;
-class Preprocessor;
class StoredDiagnostic;
-class SourceManager;
/// \brief The storage behind a CXDiagnostic
struct CXStoredDiagnostic {
@@ -38,15 +27,6 @@ struct CXStoredDiagnostic {
const LangOptions &LangOpts)
: Diag(Diag), LangOpts(LangOpts) { }
};
-
-/// \brief Given the path to a file that contains binary, serialized
-/// diagnostics produced by Clang, load those diagnostics.
-void LoadSerializedDiagnostics(const llvm::sys::Path &DiagnosticsPath,
- unsigned num_unsaved_files,
- struct CXUnsavedFile *unsaved_files,
- FileManager &FileMgr,
- SourceManager &SourceMgr,
- llvm::SmallVectorImpl<StoredDiagnostic> &Diags);
} // end namespace clang
diff --git a/tools/libclang/CIndexInclusionStack.cpp b/tools/libclang/CIndexInclusionStack.cpp
index e86323956f91..e0f4d42defdb 100644
--- a/tools/libclang/CIndexInclusionStack.cpp
+++ b/tools/libclang/CIndexInclusionStack.cpp
@@ -13,6 +13,7 @@
//===----------------------------------------------------------------------===//
#include "CIndexer.h"
+#include "CXTranslationUnit.h"
#include "CXSourceLocation.h"
#include "clang/AST/DeclVisitor.h"
#include "clang/Frontend/ASTUnit.h"
@@ -24,7 +25,7 @@ extern "C" {
void clang_getInclusions(CXTranslationUnit TU, CXInclusionVisitor CB,
CXClientData clientData) {
- ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
+ ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
SourceManager &SM = CXXUnit->getSourceManager();
ASTContext &Ctx = CXXUnit->getASTContext();
@@ -55,7 +56,7 @@ void clang_getInclusions(CXTranslationUnit TU, CXInclusionVisitor CB,
while (L.isValid()) {
PresumedLoc PLoc = SM.getPresumedLoc(L);
InclusionStack.push_back(cxloc::translateSourceLocation(Ctx, L));
- L = PLoc.getIncludeLoc();
+ L = PLoc.isValid()? PLoc.getIncludeLoc() : SourceLocation();
}
// Callback to the client.
diff --git a/tools/libclang/CIndexUSRs.cpp b/tools/libclang/CIndexUSRs.cpp
index 8f3dacfad218..e74d1d4deb3f 100644
--- a/tools/libclang/CIndexUSRs.cpp
+++ b/tools/libclang/CIndexUSRs.cpp
@@ -13,6 +13,7 @@
#include "CIndexer.h"
#include "CXCursor.h"
+#include "CXString.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/AST/DeclVisitor.h"
#include "clang/Frontend/ASTUnit.h"
@@ -29,17 +30,23 @@ using namespace clang::cxstring;
namespace {
class USRGenerator : public DeclVisitor<USRGenerator> {
- llvm::SmallString<1024> Buf;
+ llvm::OwningPtr<llvm::SmallString<128> > OwnedBuf;
+ llvm::SmallVectorImpl<char> &Buf;
llvm::raw_svector_ostream Out;
bool IgnoreResults;
ASTUnit *AU;
bool generatedLoc;
+
+ llvm::DenseMap<const Type *, unsigned> TypeSubstitutions;
+
public:
- USRGenerator(const CXCursor *C = 0)
- : Out(Buf),
- IgnoreResults(false),
- AU(C ? cxcursor::getCursorASTUnit(*C) : 0),
- generatedLoc(false)
+ USRGenerator(const CXCursor *C = 0, llvm::SmallVectorImpl<char> *extBuf = 0)
+ : OwnedBuf(extBuf ? 0 : new llvm::SmallString<128>()),
+ Buf(extBuf ? *extBuf : *OwnedBuf.get()),
+ Out(Buf),
+ IgnoreResults(false),
+ AU(C ? cxcursor::getCursorASTUnit(*C) : 0),
+ generatedLoc(false)
{
// Add the USR space prefix.
Out << "c:";
@@ -276,20 +283,20 @@ void USRGenerator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
}
void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) {
- Decl *container = cast<Decl>(D->getDeclContext());
-
- // The USR for a method declared in a class extension is based on
- // the ObjCInterfaceDecl, not the ObjCCategoryDecl.
- do {
- if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(container))
- if (CD->IsClassExtension()) {
- Visit(CD->getClassInterface());
- break;
- }
- Visit(cast<Decl>(D->getDeclContext()));
+ DeclContext *container = D->getDeclContext();
+ if (ObjCProtocolDecl *pd = dyn_cast<ObjCProtocolDecl>(container)) {
+ Visit(pd);
+ }
+ else {
+ // The USR for a method declared in a class extension or category is based on
+ // the ObjCInterfaceDecl, not the ObjCCategoryDecl.
+ ObjCInterfaceDecl *ID = D->getClassInterface();
+ if (!ID) {
+ IgnoreResults = true;
+ return;
+ }
+ Visit(ID);
}
- while (false);
-
// Ideally we would use 'GenObjCMethod', but this is such a hot path
// for Objective-C code that we don't want to use
// DeclarationName::getAsString().
@@ -474,8 +481,7 @@ bool USRGenerator::GenLoc(const Decl *D) {
const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(L);
const FileEntry *FE = SM.getFileEntryForID(Decomposed.first);
if (FE) {
- llvm::sys::Path P(FE->getName());
- Out << P.getLast();
+ Out << llvm::sys::path::filename(FE->getName());
}
else {
// This case really isn't interesting.
@@ -510,32 +516,11 @@ void USRGenerator::VisitType(QualType T) {
// Mangle in ObjC GC qualifiers?
- if (const PointerType *PT = T->getAs<PointerType>()) {
- Out << '*';
- T = PT->getPointeeType();
- continue;
- }
- if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
- Out << '&';
- T = RT->getPointeeType();
- continue;
- }
- if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) {
- Out << 'F';
- VisitType(FT->getResultType());
- for (FunctionProtoType::arg_type_iterator
- I = FT->arg_type_begin(), E = FT->arg_type_end(); I!=E; ++I) {
- VisitType(*I);
- }
- if (FT->isVariadic())
- Out << '.';
- return;
- }
- if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
- Out << 'B';
- T = BT->getPointeeType();
- continue;
+ if (const PackExpansionType *Expansion = T->getAs<PackExpansionType>()) {
+ Out << 'P';
+ T = Expansion->getPattern();
}
+
if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
unsigned char c = '\0';
switch (BT->getKind()) {
@@ -563,7 +548,8 @@ void USRGenerator::VisitType(QualType T) {
case BuiltinType::Char_S:
case BuiltinType::SChar:
c = 'C'; break;
- case BuiltinType::WChar:
+ case BuiltinType::WChar_S:
+ case BuiltinType::WChar_U:
c = 'W'; break;
case BuiltinType::Short:
c = 'S'; break;
@@ -585,7 +571,6 @@ void USRGenerator::VisitType(QualType T) {
c = 'n'; break;
case BuiltinType::Overload:
case BuiltinType::Dependent:
- case BuiltinType::UndeducedAuto:
IgnoreResults = true;
return;
case BuiltinType::ObjCId:
@@ -598,6 +583,46 @@ void USRGenerator::VisitType(QualType T) {
Out << c;
return;
}
+
+ // If we have already seen this (non-built-in) type, use a substitution
+ // encoding.
+ llvm::DenseMap<const Type *, unsigned>::iterator Substitution
+ = TypeSubstitutions.find(T.getTypePtr());
+ if (Substitution != TypeSubstitutions.end()) {
+ Out << 'S' << Substitution->second << '_';
+ return;
+ } else {
+ // Record this as a substitution.
+ unsigned Number = TypeSubstitutions.size();
+ TypeSubstitutions[T.getTypePtr()] = Number;
+ }
+
+ if (const PointerType *PT = T->getAs<PointerType>()) {
+ Out << '*';
+ T = PT->getPointeeType();
+ continue;
+ }
+ if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
+ Out << '&';
+ T = RT->getPointeeType();
+ continue;
+ }
+ if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) {
+ Out << 'F';
+ VisitType(FT->getResultType());
+ for (FunctionProtoType::arg_type_iterator
+ I = FT->arg_type_begin(), E = FT->arg_type_end(); I!=E; ++I) {
+ VisitType(*I);
+ }
+ if (FT->isVariadic())
+ Out << '.';
+ return;
+ }
+ if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
+ Out << 'B';
+ T = BT->getPointeeType();
+ continue;
+ }
if (const ComplexType *CT = T->getAs<ComplexType>()) {
Out << '<';
T = CT->getElementType();
@@ -638,17 +663,23 @@ void USRGenerator::VisitTemplateParameterList(
P != PEnd; ++P) {
Out << '#';
if (isa<TemplateTypeParmDecl>(*P)) {
+ if (cast<TemplateTypeParmDecl>(*P)->isParameterPack())
+ Out<< 'p';
Out << 'T';
continue;
}
if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
+ if (NTTP->isParameterPack())
+ Out << 'p';
Out << 'N';
VisitType(NTTP->getType());
continue;
}
TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
+ if (TTP->isParameterPack())
+ Out << 'p';
Out << 't';
VisitTemplateParameterList(TTP->getTemplateParameters());
}
@@ -675,11 +706,15 @@ void USRGenerator::VisitTemplateArgument(const TemplateArgument &Arg) {
break;
case TemplateArgument::Declaration:
- Visit(Arg.getAsDecl());
+ if (Decl *D = Arg.getAsDecl())
+ Visit(D);
break;
+ case TemplateArgument::TemplateExpansion:
+ Out << 'P'; // pack expansion of...
+ // Fall through
case TemplateArgument::Template:
- VisitTemplateName(Arg.getAsTemplate());
+ VisitTemplateName(Arg.getAsTemplateOrTemplatePattern());
break;
case TemplateArgument::Expression:
@@ -687,7 +722,10 @@ void USRGenerator::VisitTemplateArgument(const TemplateArgument &Arg) {
break;
case TemplateArgument::Pack:
- // FIXME: Variadic templates
+ Out << 'p' << Arg.pack_size();
+ for (TemplateArgument::pack_iterator P = Arg.pack_begin(), PEnd = Arg.pack_end();
+ P != PEnd; ++P)
+ VisitTemplateArgument(*P);
break;
case TemplateArgument::Type:
@@ -768,19 +806,27 @@ static CXString getDeclCursorUSR(const CXCursor &C) {
break;
}
- USRGenerator UG(&C);
- UG->Visit(D);
-
- if (UG->ignoreResults())
+ CXTranslationUnit TU = cxcursor::getCursorTU(C);
+ if (!TU)
return createCXString("");
-#if 0
- // For development testing.
- assert(UG.str().size() > 2);
-#endif
+ CXStringBuf *buf = cxstring::getCXStringBuf(TU);
+ if (!buf)
+ return createCXString("");
+
+ {
+ USRGenerator UG(&C, &buf->Data);
+ UG->Visit(D);
- // Return a copy of the string that must be disposed by the caller.
- return createCXString(UG.str(), true);
+ if (UG->ignoreResults()) {
+ disposeCXStringBuf(buf);
+ return createCXString("");
+ }
+ }
+ // Return the C-string, but don't make a copy since it is already in
+ // the string buffer.
+ buf->Data.push_back('\0');
+ return createCXString(buf);
}
extern "C" {
@@ -792,10 +838,21 @@ CXString clang_getCursorUSR(CXCursor C) {
return getDeclCursorUSR(C);
if (K == CXCursor_MacroDefinition) {
- USRGenerator UG(&C);
- UG << "macro@"
- << cxcursor::getCursorMacroDefinition(C)->getName()->getNameStart();
- return createCXString(UG.str(), true);
+ CXTranslationUnit TU = cxcursor::getCursorTU(C);
+ if (!TU)
+ return createCXString("");
+
+ CXStringBuf *buf = cxstring::getCXStringBuf(TU);
+ if (!buf)
+ return createCXString("");
+
+ {
+ USRGenerator UG(&C, &buf->Data);
+ UG << "macro@"
+ << cxcursor::getCursorMacroDefinition(C)->getName()->getNameStart();
+ }
+ buf->Data.push_back('\0');
+ return createCXString(buf);
}
return createCXString("");
diff --git a/tools/libclang/CIndexer.cpp b/tools/libclang/CIndexer.cpp
index cdf6c61a09e2..992d76a2efe9 100644
--- a/tools/libclang/CIndexer.cpp
+++ b/tools/libclang/CIndexer.cpp
@@ -25,12 +25,17 @@
#include "llvm/Support/Compiler.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"
-#include "llvm/System/Program.h"
+#include "llvm/Support/Program.h"
#include <cstdio>
#include <vector>
#include <sstream>
+#ifdef __CYGWIN__
+#include <sys/cygwin.h>
+#define LLVM_ON_WIN32 1
+#endif
+
#ifdef LLVM_ON_WIN32
#include <windows.h>
#else
@@ -39,12 +44,12 @@
using namespace clang;
-const llvm::sys::Path& CIndexer::getClangPath() {
+std::string CIndexer::getClangResourcesPath() {
// Did we already compute the path?
- if (!ClangPath.empty())
- return ClangPath;
-
- // Find the location where this library lives (libCIndex.dylib).
+ if (!ResourcesPath.empty())
+ return ResourcesPath.str();
+
+ // Find the location where this library lives (libclang.dylib).
#ifdef LLVM_ON_WIN32
MEMORY_BASIC_INFORMATION mbi;
char path[MAX_PATH];
@@ -52,46 +57,32 @@ const llvm::sys::Path& CIndexer::getClangPath() {
sizeof(mbi));
GetModuleFileNameA((HINSTANCE)mbi.AllocationBase, path, MAX_PATH);
- llvm::sys::Path CIndexPath(path);
+#ifdef __CYGWIN__
+ char w32path[MAX_PATH];
+ strcpy(w32path, path);
+ cygwin_conv_to_full_posix_path(w32path, path);
+#endif
- CIndexPath.eraseComponent();
- CIndexPath.appendComponent("clang");
- CIndexPath.appendSuffix("exe");
- CIndexPath.makeAbsolute();
+ llvm::sys::Path LibClangPath(path);
+ LibClangPath.eraseComponent();
#else
// This silly cast below avoids a C++ warning.
Dl_info info;
if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0)
assert(0 && "Call to dladdr() failed");
-
- llvm::sys::Path CIndexPath(info.dli_fname);
-
+
+ llvm::sys::Path LibClangPath(info.dli_fname);
+
// We now have the CIndex directory, locate clang relative to it.
- CIndexPath.eraseComponent();
- CIndexPath.appendComponent("..");
- CIndexPath.appendComponent("bin");
- CIndexPath.appendComponent("clang");
+ LibClangPath.eraseComponent();
#endif
+
+ LibClangPath.appendComponent("clang");
+ LibClangPath.appendComponent(CLANG_VERSION_STRING);
// Cache our result.
- ClangPath = CIndexPath;
- return ClangPath;
-}
-
-std::string CIndexer::getClangResourcesPath() {
- llvm::sys::Path P = getClangPath();
-
- if (!P.empty()) {
- P.eraseComponent(); // Remove /clang from foo/bin/clang
- P.eraseComponent(); // Remove /bin from foo/bin
-
- // Get foo/lib/clang/<version>/include
- P.appendComponent("lib");
- P.appendComponent("clang");
- P.appendComponent(CLANG_VERSION_STRING);
- }
-
- return P.str();
+ ResourcesPath = LibClangPath;
+ return LibClangPath.str();
}
static llvm::sys::Path GetTemporaryPath() {
@@ -127,7 +118,8 @@ bool clang::RemapFiles(unsigned num_unsaved_files,
return true;
std::string ErrorInfo;
- llvm::raw_fd_ostream OS(SavedFile.c_str(), ErrorInfo);
+ llvm::raw_fd_ostream OS(SavedFile.c_str(), ErrorInfo,
+ llvm::raw_fd_ostream::F_Binary);
if (!ErrorInfo.empty())
return true;
diff --git a/tools/libclang/CIndexer.h b/tools/libclang/CIndexer.h
index 31bf779ea4a5..b40891a02f6c 100644
--- a/tools/libclang/CIndexer.h
+++ b/tools/libclang/CIndexer.h
@@ -1,4 +1,4 @@
-//===- CIndexer.h - Clang-C Source Indexing Library -----------------------===//
+//===- CIndexer.h - Clang-C Source Indexing Library -------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
@@ -17,27 +17,22 @@
#include "clang-c/Index.h"
#include "llvm/ADT/StringRef.h"
-#include "llvm/System/Path.h"
+#include "llvm/Support/Path.h"
#include <vector>
-namespace clang {
-namespace cxstring {
- CXString createCXString(const char *String, bool DupString = false);
- CXString createCXString(llvm::StringRef String, bool DupString = true);
-}
+namespace llvm {
+ class CrashRecoveryContext;
}
class CIndexer {
- bool UseExternalASTGeneration;
bool OnlyLocalDecls;
bool DisplayDiagnostics;
- llvm::sys::Path ClangPath;
-
+ llvm::sys::Path ResourcesPath;
+ std::string WorkingDir;
+
public:
- CIndexer()
- : UseExternalASTGeneration(false), OnlyLocalDecls(false),
- DisplayDiagnostics(false) { }
+ CIndexer() : OnlyLocalDecls(false), DisplayDiagnostics(false) { }
/// \brief Whether we only want to see "local" declarations (that did not
/// come from a previous precompiled header). If false, we want to see all
@@ -50,16 +45,11 @@ public:
DisplayDiagnostics = Display;
}
- bool getUseExternalASTGeneration() const { return UseExternalASTGeneration; }
- void setUseExternalASTGeneration(bool Value) {
- UseExternalASTGeneration = Value;
- }
-
- /// \brief Get the path of the clang binary.
- const llvm::sys::Path& getClangPath();
-
/// \brief Get the path of the clang resource files.
std::string getClangResourcesPath();
+
+ const std::string &getWorkingDirectory() const { return WorkingDir; }
+ void setWorkingDirectory(const std::string &Dir) { WorkingDir = Dir; }
};
namespace clang {
@@ -73,6 +63,20 @@ namespace clang {
struct CXUnsavedFile *unsaved_files,
std::vector<std::string> &RemapArgs,
std::vector<llvm::sys::Path> &TemporaryFiles);
+
+ /// \brief Return the current size to request for "safety".
+ unsigned GetSafetyThreadStackSize();
+
+ /// \brief Set the current size to request for "safety" (or 0, if safety
+ /// threads should not be used).
+ void SetSafetyThreadStackSize(unsigned Value);
+
+ /// \brief Execution the given code "safely", using crash recovery or safety
+ /// threads when possible.
+ ///
+ /// \return False if a crash was detected.
+ bool RunSafely(llvm::CrashRecoveryContext &CRC,
+ void (*Fn)(void*), void *UserData, unsigned Size = 0);
}
#endif
diff --git a/tools/libclang/CMakeLists.txt b/tools/libclang/CMakeLists.txt
index 29ef574fff6a..da72f5a02d01 100644
--- a/tools/libclang/CMakeLists.txt
+++ b/tools/libclang/CMakeLists.txt
@@ -1,22 +1,18 @@
set(SHARED_LIBRARY TRUE)
-set(LLVM_NO_RTTI 1)
-
set(LLVM_USED_LIBS
clangFrontend
clangDriver
clangSerialization
- clangParse
+ clangIndex
clangSema
- clangAnalysis
clangAST
clangLex
clangBasic)
set( LLVM_LINK_COMPONENTS
- bitreader
+ support
mc
- core
)
add_clang_library(libclang
@@ -28,6 +24,7 @@ add_clang_library(libclang
CIndexUSRs.cpp
CIndexer.cpp
CXCursor.cpp
+ CXString.cpp
CXType.cpp
../../include/clang-c/Index.h
)
@@ -60,5 +57,6 @@ endif(MSVC)
set_target_properties(libclang
PROPERTIES
+ PREFIX "" # Otherwise we get liblibclang.so
LINKER_LANGUAGE CXX
DEFINE_SYMBOL _CINDEX_LIB_)
diff --git a/tools/libclang/CXCursor.cpp b/tools/libclang/CXCursor.cpp
index a8fd049c9965..dd22a97ab19b 100644
--- a/tools/libclang/CXCursor.cpp
+++ b/tools/libclang/CXCursor.cpp
@@ -13,15 +13,20 @@
//
//===----------------------------------------------------------------------===//
+#include "CXTranslationUnit.h"
#include "CXCursor.h"
+#include "CXString.h"
#include "clang/Frontend/ASTUnit.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/Expr.h"
+#include "clang/AST/ExprCXX.h"
+#include "clang-c/Index.h"
#include "llvm/Support/ErrorHandling.h"
using namespace clang;
+using namespace cxcursor;
CXCursor cxcursor::MakeCXCursorInvalid(CXCursorKind K) {
assert(K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid);
@@ -41,19 +46,24 @@ static CXCursorKind GetCursorKind(const Attr *A) {
return CXCursor_UnexposedAttr;
}
-CXCursor cxcursor::MakeCXCursor(const Attr *A, Decl *Parent, ASTUnit *TU) {
+CXCursor cxcursor::MakeCXCursor(const Attr *A, Decl *Parent,
+ CXTranslationUnit TU) {
assert(A && Parent && TU && "Invalid arguments!");
CXCursor C = { GetCursorKind(A), { Parent, (void*)A, TU } };
return C;
}
-CXCursor cxcursor::MakeCXCursor(Decl *D, ASTUnit *TU) {
+CXCursor cxcursor::MakeCXCursor(Decl *D, CXTranslationUnit TU,
+ bool FirstInDeclGroup) {
assert(D && TU && "Invalid arguments!");
- CXCursor C = { getCursorKindForDecl(D), { D, 0, TU } };
+ CXCursor C = { getCursorKindForDecl(D),
+ { D, (void*)(intptr_t) (FirstInDeclGroup ? 1 : 0), TU }
+ };
return C;
}
-CXCursor cxcursor::MakeCXCursor(Stmt *S, Decl *Parent, ASTUnit *TU) {
+CXCursor cxcursor::MakeCXCursor(Stmt *S, Decl *Parent,
+ CXTranslationUnit TU) {
assert(S && TU && "Invalid arguments!");
CXCursorKind K = CXCursor_NotImplemented;
@@ -65,7 +75,6 @@ CXCursor cxcursor::MakeCXCursor(Stmt *S, Decl *Parent, ASTUnit *TU) {
case Stmt::CompoundStmtClass:
case Stmt::CaseStmtClass:
case Stmt::DefaultStmtClass:
- case Stmt::LabelStmtClass:
case Stmt::IfStmtClass:
case Stmt::SwitchStmtClass:
case Stmt::WhileStmtClass:
@@ -77,7 +86,6 @@ CXCursor cxcursor::MakeCXCursor(Stmt *S, Decl *Parent, ASTUnit *TU) {
case Stmt::BreakStmtClass:
case Stmt::ReturnStmtClass:
case Stmt::DeclStmtClass:
- case Stmt::SwitchCaseClass:
case Stmt::AsmStmtClass:
case Stmt::ObjCAtTryStmtClass:
case Stmt::ObjCAtCatchStmtClass:
@@ -90,6 +98,10 @@ CXCursor cxcursor::MakeCXCursor(Stmt *S, Decl *Parent, ASTUnit *TU) {
K = CXCursor_UnexposedStmt;
break;
+ case Stmt::LabelStmtClass:
+ K = CXCursor_LabelStmt;
+ break;
+
case Stmt::PredefinedExprClass:
case Stmt::IntegerLiteralClass:
case Stmt::FloatingLiteralClass:
@@ -104,6 +116,7 @@ CXCursor cxcursor::MakeCXCursor(Stmt *S, Decl *Parent, ASTUnit *TU) {
case Stmt::BinaryOperatorClass:
case Stmt::CompoundAssignOperatorClass:
case Stmt::ConditionalOperatorClass:
+ case Stmt::BinaryConditionalOperatorClass:
case Stmt::ImplicitCastExprClass:
case Stmt::CStyleCastExprClass:
case Stmt::CompoundLiteralExprClass:
@@ -115,7 +128,6 @@ CXCursor cxcursor::MakeCXCursor(Stmt *S, Decl *Parent, ASTUnit *TU) {
case Stmt::VAArgExprClass:
case Stmt::AddrLabelExprClass:
case Stmt::StmtExprClass:
- case Stmt::TypesCompatibleExprClass:
case Stmt::ChooseExprClass:
case Stmt::GNUNullExprClass:
case Stmt::CXXStaticCastExprClass:
@@ -124,6 +136,7 @@ CXCursor cxcursor::MakeCXCursor(Stmt *S, Decl *Parent, ASTUnit *TU) {
case Stmt::CXXConstCastExprClass:
case Stmt::CXXFunctionalCastExprClass:
case Stmt::CXXTypeidExprClass:
+ case Stmt::CXXUuidofExprClass:
case Stmt::CXXBoolLiteralExprClass:
case Stmt::CXXNullPtrLiteralExprClass:
case Stmt::CXXThisExprClass:
@@ -135,25 +148,30 @@ CXCursor cxcursor::MakeCXCursor(Stmt *S, Decl *Parent, ASTUnit *TU) {
case Stmt::CXXPseudoDestructorExprClass:
case Stmt::UnresolvedLookupExprClass:
case Stmt::UnaryTypeTraitExprClass:
+ case Stmt::BinaryTypeTraitExprClass:
case Stmt::DependentScopeDeclRefExprClass:
case Stmt::CXXBindTemporaryExprClass:
- case Stmt::CXXExprWithTemporariesClass:
+ case Stmt::ExprWithCleanupsClass:
case Stmt::CXXUnresolvedConstructExprClass:
case Stmt::CXXDependentScopeMemberExprClass:
case Stmt::UnresolvedMemberExprClass:
+ case Stmt::CXXNoexceptExprClass:
case Stmt::ObjCStringLiteralClass:
case Stmt::ObjCEncodeExprClass:
case Stmt::ObjCSelectorExprClass:
case Stmt::ObjCProtocolExprClass:
- case Stmt::ObjCImplicitSetterGetterRefExprClass:
- case Stmt::ObjCSuperExprClass:
case Stmt::ObjCIsaExprClass:
case Stmt::ShuffleVectorExprClass:
case Stmt::BlockExprClass:
+ case Stmt::OpaqueValueExprClass:
+ case Stmt::PackExpansionExprClass:
+ case Stmt::SizeOfPackExprClass:
K = CXCursor_UnexposedExpr;
break;
+
case Stmt::DeclRefExprClass:
case Stmt::BlockDeclRefExprClass:
+ case Stmt::SubstNonTypeTemplateParmPackExprClass:
// FIXME: UnresolvedLookupExpr?
// FIXME: DependentScopeDeclRefExpr?
K = CXCursor_DeclRefExpr;
@@ -170,10 +188,10 @@ CXCursor cxcursor::MakeCXCursor(Stmt *S, Decl *Parent, ASTUnit *TU) {
case Stmt::CallExprClass:
case Stmt::CXXOperatorCallExprClass:
case Stmt::CXXMemberCallExprClass:
+ case Stmt::CUDAKernelCallExprClass:
case Stmt::CXXConstructExprClass:
case Stmt::CXXTemporaryObjectExprClass:
// FIXME: CXXUnresolvedConstructExpr
- // FIXME: ObjCImplicitSetterGetterRefExpr?
K = CXCursor_CallExpr;
break;
@@ -188,7 +206,7 @@ CXCursor cxcursor::MakeCXCursor(Stmt *S, Decl *Parent, ASTUnit *TU) {
CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
SourceLocation Loc,
- ASTUnit *TU) {
+ CXTranslationUnit TU) {
assert(Super && TU && "Invalid arguments!");
void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
CXCursor C = { CXCursor_ObjCSuperClassRef, { Super, RawLoc, TU } };
@@ -205,7 +223,7 @@ cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
CXCursor cxcursor::MakeCursorObjCProtocolRef(ObjCProtocolDecl *Super,
SourceLocation Loc,
- ASTUnit *TU) {
+ CXTranslationUnit TU) {
assert(Super && TU && "Invalid arguments!");
void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
CXCursor C = { CXCursor_ObjCProtocolRef, { Super, RawLoc, TU } };
@@ -222,7 +240,7 @@ cxcursor::getCursorObjCProtocolRef(CXCursor C) {
CXCursor cxcursor::MakeCursorObjCClassRef(ObjCInterfaceDecl *Class,
SourceLocation Loc,
- ASTUnit *TU) {
+ CXTranslationUnit TU) {
// 'Class' can be null for invalid code.
if (!Class)
return MakeCXCursorInvalid(CXCursor_InvalidCode);
@@ -241,7 +259,7 @@ cxcursor::getCursorObjCClassRef(CXCursor C) {
}
CXCursor cxcursor::MakeCursorTypeRef(TypeDecl *Type, SourceLocation Loc,
- ASTUnit *TU) {
+ CXTranslationUnit TU) {
assert(Type && TU && "Invalid arguments!");
void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
CXCursor C = { CXCursor_TypeRef, { Type, RawLoc, TU } };
@@ -257,7 +275,8 @@ cxcursor::getCursorTypeRef(CXCursor C) {
}
CXCursor cxcursor::MakeCursorTemplateRef(TemplateDecl *Template,
- SourceLocation Loc, ASTUnit *TU) {
+ SourceLocation Loc,
+ CXTranslationUnit TU) {
assert(Template && TU && "Invalid arguments!");
void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
CXCursor C = { CXCursor_TemplateRef, { Template, RawLoc, TU } };
@@ -273,7 +292,7 @@ cxcursor::getCursorTemplateRef(CXCursor C) {
}
CXCursor cxcursor::MakeCursorNamespaceRef(NamedDecl *NS, SourceLocation Loc,
- ASTUnit *TU) {
+ CXTranslationUnit TU) {
assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
"Invalid arguments!");
@@ -290,7 +309,25 @@ cxcursor::getCursorNamespaceRef(CXCursor C) {
reinterpret_cast<uintptr_t>(C.data[1])));
}
-CXCursor cxcursor::MakeCursorCXXBaseSpecifier(CXXBaseSpecifier *B, ASTUnit *TU){
+CXCursor cxcursor::MakeCursorMemberRef(FieldDecl *Field, SourceLocation Loc,
+ CXTranslationUnit TU) {
+
+ assert(Field && TU && "Invalid arguments!");
+ void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
+ CXCursor C = { CXCursor_MemberRef, { Field, RawLoc, TU } };
+ return C;
+}
+
+std::pair<FieldDecl *, SourceLocation>
+cxcursor::getCursorMemberRef(CXCursor C) {
+ assert(C.kind == CXCursor_MemberRef);
+ return std::make_pair(static_cast<FieldDecl *>(C.data[0]),
+ SourceLocation::getFromRawEncoding(
+ reinterpret_cast<uintptr_t>(C.data[1])));
+}
+
+CXCursor cxcursor::MakeCursorCXXBaseSpecifier(CXXBaseSpecifier *B,
+ CXTranslationUnit TU){
CXCursor C = { CXCursor_CXXBaseSpecifier, { B, 0, TU } };
return C;
}
@@ -301,7 +338,7 @@ CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
}
CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
- ASTUnit *TU) {
+ CXTranslationUnit TU) {
CXCursor C = { CXCursor_PreprocessingDirective,
{ reinterpret_cast<void *>(Range.getBegin().getRawEncoding()),
reinterpret_cast<void *>(Range.getEnd().getRawEncoding()),
@@ -318,7 +355,8 @@ SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
reinterpret_cast<uintptr_t> (C.data[1])));
}
-CXCursor cxcursor::MakeMacroDefinitionCursor(MacroDefinition *MI, ASTUnit *TU) {
+CXCursor cxcursor::MakeMacroDefinitionCursor(MacroDefinition *MI,
+ CXTranslationUnit TU) {
CXCursor C = { CXCursor_MacroDefinition, { MI, 0, TU } };
return C;
}
@@ -329,7 +367,7 @@ MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) {
}
CXCursor cxcursor::MakeMacroInstantiationCursor(MacroInstantiation *MI,
- ASTUnit *TU) {
+ CXTranslationUnit TU) {
CXCursor C = { CXCursor_MacroInstantiation, { MI, 0, TU } };
return C;
}
@@ -339,6 +377,80 @@ MacroInstantiation *cxcursor::getCursorMacroInstantiation(CXCursor C) {
return static_cast<MacroInstantiation *>(C.data[0]);
}
+CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
+ CXTranslationUnit TU) {
+ CXCursor C = { CXCursor_InclusionDirective, { ID, 0, TU } };
+ return C;
+}
+
+InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
+ assert(C.kind == CXCursor_InclusionDirective);
+ return static_cast<InclusionDirective *>(C.data[0]);
+}
+
+CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
+ CXTranslationUnit TU) {
+
+ assert(Label && TU && "Invalid arguments!");
+ void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
+ CXCursor C = { CXCursor_LabelRef, { Label, RawLoc, TU } };
+ return C;
+}
+
+std::pair<LabelStmt*, SourceLocation>
+cxcursor::getCursorLabelRef(CXCursor C) {
+ assert(C.kind == CXCursor_LabelRef);
+ return std::make_pair(static_cast<LabelStmt *>(C.data[0]),
+ SourceLocation::getFromRawEncoding(
+ reinterpret_cast<uintptr_t>(C.data[1])));
+}
+
+CXCursor cxcursor::MakeCursorOverloadedDeclRef(OverloadExpr *E,
+ CXTranslationUnit TU) {
+ assert(E && TU && "Invalid arguments!");
+ OverloadedDeclRefStorage Storage(E);
+ void *RawLoc = reinterpret_cast<void *>(E->getNameLoc().getRawEncoding());
+ CXCursor C = {
+ CXCursor_OverloadedDeclRef,
+ { Storage.getOpaqueValue(), RawLoc, TU }
+ };
+ return C;
+}
+
+CXCursor cxcursor::MakeCursorOverloadedDeclRef(Decl *D,
+ SourceLocation Loc,
+ CXTranslationUnit TU) {
+ assert(D && TU && "Invalid arguments!");
+ void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
+ OverloadedDeclRefStorage Storage(D);
+ CXCursor C = {
+ CXCursor_OverloadedDeclRef,
+ { Storage.getOpaqueValue(), RawLoc, TU }
+ };
+ return C;
+}
+
+CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
+ SourceLocation Loc,
+ CXTranslationUnit TU) {
+ assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
+ void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
+ OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
+ CXCursor C = {
+ CXCursor_OverloadedDeclRef,
+ { Storage.getOpaqueValue(), RawLoc, TU }
+ };
+ return C;
+}
+
+std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
+cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
+ assert(C.kind == CXCursor_OverloadedDeclRef);
+ return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(C.data[0]),
+ SourceLocation::getFromRawEncoding(
+ reinterpret_cast<uintptr_t>(C.data[1])));
+}
+
Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
return (Decl *)Cursor.data[0];
}
@@ -365,10 +477,87 @@ ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
}
ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
- return static_cast<ASTUnit *>(Cursor.data[2]);
+ return static_cast<ASTUnit *>(static_cast<CXTranslationUnit>(Cursor.data[2])
+ ->TUData);
+}
+
+CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
+ return static_cast<CXTranslationUnit>(Cursor.data[2]);
}
bool cxcursor::operator==(CXCursor X, CXCursor Y) {
return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
X.data[2] == Y.data[2];
}
+
+// FIXME: Remove once we can model DeclGroups and their appropriate ranges
+// properly in the ASTs.
+bool cxcursor::isFirstInDeclGroup(CXCursor C) {
+ assert(clang_isDeclaration(C.kind));
+ return ((uintptr_t) (C.data[1])) != 0;
+}
+
+//===----------------------------------------------------------------------===//
+// CXCursorSet.
+//===----------------------------------------------------------------------===//
+
+typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
+
+static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
+ return (CXCursorSet) setImpl;
+}
+static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
+ return (CXCursorSet_Impl*) set;
+}
+namespace llvm {
+template<> struct DenseMapInfo<CXCursor> {
+public:
+ static inline CXCursor getEmptyKey() {
+ return MakeCXCursorInvalid(CXCursor_InvalidFile);
+ }
+ static inline CXCursor getTombstoneKey() {
+ return MakeCXCursorInvalid(CXCursor_NoDeclFound);
+ }
+ static inline unsigned getHashValue(const CXCursor &cursor) {
+ return llvm::DenseMapInfo<std::pair<void*,void*> >
+ ::getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
+ }
+ static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
+ return x.kind == y.kind &&
+ x.data[0] == y.data[0] &&
+ x.data[1] == y.data[1];
+ }
+};
+}
+
+extern "C" {
+CXCursorSet clang_createCXCursorSet() {
+ return packCXCursorSet(new CXCursorSet_Impl());
+}
+
+void clang_disposeCXCursorSet(CXCursorSet set) {
+ delete unpackCXCursorSet(set);
+}
+
+unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
+ CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
+ if (!setImpl)
+ return 0;
+ return setImpl->find(cursor) == setImpl->end();
+}
+
+unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
+ // Do not insert invalid cursors into the set.
+ if (cursor.kind >= CXCursor_FirstInvalid &&
+ cursor.kind <= CXCursor_LastInvalid)
+ return 1;
+
+ CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
+ if (!setImpl)
+ return 1;
+ unsigned &entry = (*setImpl)[cursor];
+ unsigned flag = entry == 0 ? 1 : 0;
+ entry = 1;
+ return flag;
+}
+} // end: extern "C"
diff --git a/tools/libclang/CXCursor.h b/tools/libclang/CXCursor.h
index a5f111edc1d8..11f2500fb144 100644
--- a/tools/libclang/CXCursor.h
+++ b/tools/libclang/CXCursor.h
@@ -16,6 +16,7 @@
#include "clang-c/Index.h"
#include "clang/Basic/SourceLocation.h"
+#include "llvm/ADT/PointerUnion.h"
#include <utility>
namespace clang {
@@ -26,26 +27,35 @@ class Attr;
class CXXBaseSpecifier;
class Decl;
class Expr;
+class FieldDecl;
+class InclusionDirective;
+class LabelStmt;
class MacroDefinition;
class MacroInstantiation;
class NamedDecl;
class ObjCInterfaceDecl;
class ObjCProtocolDecl;
+class OverloadedTemplateStorage;
+class OverloadExpr;
class Stmt;
class TemplateDecl;
+class TemplateName;
class TypeDecl;
-
+
namespace cxcursor {
-CXCursor MakeCXCursor(const clang::Attr *A, clang::Decl *Parent, ASTUnit *TU);
-CXCursor MakeCXCursor(clang::Decl *D, ASTUnit *TU);
-CXCursor MakeCXCursor(clang::Stmt *S, clang::Decl *Parent, ASTUnit *TU);
+CXCursor MakeCXCursor(const clang::Attr *A, clang::Decl *Parent,
+ CXTranslationUnit TU);
+CXCursor MakeCXCursor(clang::Decl *D, CXTranslationUnit TU,
+ bool FirstInDeclGroup = true);
+CXCursor MakeCXCursor(clang::Stmt *S, clang::Decl *Parent,
+ CXTranslationUnit TU);
CXCursor MakeCXCursorInvalid(CXCursorKind K);
/// \brief Create an Objective-C superclass reference at the given location.
CXCursor MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
SourceLocation Loc,
- ASTUnit *TU);
+ CXTranslationUnit TU);
/// \brief Unpack an ObjCSuperClassRef cursor into the interface it references
/// and optionally the location where the reference occurred.
@@ -54,7 +64,7 @@ std::pair<ObjCInterfaceDecl *, SourceLocation>
/// \brief Create an Objective-C protocol reference at the given location.
CXCursor MakeCursorObjCProtocolRef(ObjCProtocolDecl *Proto, SourceLocation Loc,
- ASTUnit *TU);
+ CXTranslationUnit TU);
/// \brief Unpack an ObjCProtocolRef cursor into the protocol it references
/// and optionally the location where the reference occurred.
@@ -63,7 +73,7 @@ std::pair<ObjCProtocolDecl *, SourceLocation>
/// \brief Create an Objective-C class reference at the given location.
CXCursor MakeCursorObjCClassRef(ObjCInterfaceDecl *Class, SourceLocation Loc,
- ASTUnit *TU);
+ CXTranslationUnit TU);
/// \brief Unpack an ObjCClassRef cursor into the class it references
/// and optionally the location where the reference occurred.
@@ -71,7 +81,8 @@ std::pair<ObjCInterfaceDecl *, SourceLocation>
getCursorObjCClassRef(CXCursor C);
/// \brief Create a type reference at the given location.
-CXCursor MakeCursorTypeRef(TypeDecl *Type, SourceLocation Loc, ASTUnit *TU);
+CXCursor MakeCursorTypeRef(TypeDecl *Type, SourceLocation Loc,
+ CXTranslationUnit TU);
/// \brief Unpack a TypeRef cursor into the class it references
/// and optionally the location where the reference occurred.
@@ -79,7 +90,7 @@ std::pair<TypeDecl *, SourceLocation> getCursorTypeRef(CXCursor C);
/// \brief Create a reference to a template at the given location.
CXCursor MakeCursorTemplateRef(TemplateDecl *Template, SourceLocation Loc,
- ASTUnit *TU);
+ CXTranslationUnit TU);
/// \brief Unpack a TemplateRef cursor into the template it references and
/// the location where the reference occurred.
@@ -87,38 +98,88 @@ std::pair<TemplateDecl *, SourceLocation> getCursorTemplateRef(CXCursor C);
/// \brief Create a reference to a namespace or namespace alias at the given
/// location.
-CXCursor MakeCursorNamespaceRef(NamedDecl *NS, SourceLocation Loc, ASTUnit *TU);
+CXCursor MakeCursorNamespaceRef(NamedDecl *NS, SourceLocation Loc,
+ CXTranslationUnit TU);
/// \brief Unpack a NamespaceRef cursor into the namespace or namespace alias
/// it references and the location where the reference occurred.
std::pair<NamedDecl *, SourceLocation> getCursorNamespaceRef(CXCursor C);
+/// \brief Create a reference to a field at the given location.
+CXCursor MakeCursorMemberRef(FieldDecl *Field, SourceLocation Loc,
+ CXTranslationUnit TU);
+
+/// \brief Unpack a MemberRef cursor into the field it references and the
+/// location where the reference occurred.
+std::pair<FieldDecl *, SourceLocation> getCursorMemberRef(CXCursor C);
+
/// \brief Create a CXX base specifier cursor.
-CXCursor MakeCursorCXXBaseSpecifier(CXXBaseSpecifier *B, ASTUnit *TU);
+CXCursor MakeCursorCXXBaseSpecifier(CXXBaseSpecifier *B,
+ CXTranslationUnit TU);
/// \brief Unpack a CXXBaseSpecifier cursor into a CXXBaseSpecifier.
CXXBaseSpecifier *getCursorCXXBaseSpecifier(CXCursor C);
/// \brief Create a preprocessing directive cursor.
-CXCursor MakePreprocessingDirectiveCursor(SourceRange Range, ASTUnit *TU);
+CXCursor MakePreprocessingDirectiveCursor(SourceRange Range,
+ CXTranslationUnit TU);
/// \brief Unpack a given preprocessing directive to retrieve its source range.
SourceRange getCursorPreprocessingDirective(CXCursor C);
/// \brief Create a macro definition cursor.
-CXCursor MakeMacroDefinitionCursor(MacroDefinition *, ASTUnit *TU);
+CXCursor MakeMacroDefinitionCursor(MacroDefinition *, CXTranslationUnit TU);
/// \brief Unpack a given macro definition cursor to retrieve its
/// source range.
MacroDefinition *getCursorMacroDefinition(CXCursor C);
/// \brief Create a macro instantiation cursor.
-CXCursor MakeMacroInstantiationCursor(MacroInstantiation *, ASTUnit *TU);
+CXCursor MakeMacroInstantiationCursor(MacroInstantiation *,
+ CXTranslationUnit TU);
/// \brief Unpack a given macro instantiation cursor to retrieve its
/// source range.
MacroInstantiation *getCursorMacroInstantiation(CXCursor C);
+/// \brief Create an inclusion directive cursor.
+CXCursor MakeInclusionDirectiveCursor(InclusionDirective *,
+ CXTranslationUnit TU);
+
+/// \brief Unpack a given inclusion directive cursor to retrieve its
+/// source range.
+InclusionDirective *getCursorInclusionDirective(CXCursor C);
+
+/// \brief Create a label reference at the given location.
+CXCursor MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
+ CXTranslationUnit TU);
+
+/// \brief Unpack a label reference into the label statement it refers to and
+/// the location of the reference.
+std::pair<LabelStmt *, SourceLocation> getCursorLabelRef(CXCursor C);
+
+/// \brief Create a overloaded declaration reference cursor for an expression.
+CXCursor MakeCursorOverloadedDeclRef(OverloadExpr *E, CXTranslationUnit TU);
+
+/// \brief Create a overloaded declaration reference cursor for a declaration.
+CXCursor MakeCursorOverloadedDeclRef(Decl *D, SourceLocation Location,
+ CXTranslationUnit TU);
+
+/// \brief Create a overloaded declaration reference cursor for a template name.
+CXCursor MakeCursorOverloadedDeclRef(TemplateName Template,
+ SourceLocation Location,
+ CXTranslationUnit TU);
+
+/// \brief Internal storage for an overloaded declaration reference cursor;
+typedef llvm::PointerUnion3<OverloadExpr *, Decl *,
+ OverloadedTemplateStorage *>
+ OverloadedDeclRefStorage;
+
+/// \brief Unpack an overloaded declaration reference into an expression,
+/// declaration, or template name along with the source location.
+std::pair<OverloadedDeclRefStorage, SourceLocation>
+ getCursorOverloadedDeclRef(CXCursor C);
+
Decl *getCursorDecl(CXCursor Cursor);
Expr *getCursorExpr(CXCursor Cursor);
Stmt *getCursorStmt(CXCursor Cursor);
@@ -126,6 +187,7 @@ Attr *getCursorAttr(CXCursor Cursor);
ASTContext &getCursorContext(CXCursor Cursor);
ASTUnit *getCursorASTUnit(CXCursor Cursor);
+CXTranslationUnit getCursorTU(CXCursor Cursor);
bool operator==(CXCursor X, CXCursor Y);
@@ -133,6 +195,10 @@ inline bool operator!=(CXCursor X, CXCursor Y) {
return !(X == Y);
}
+/// \brief Return true if the cursor represents a declaration that is the
+/// first in a declaration group.
+bool isFirstInDeclGroup(CXCursor C);
+
}} // end namespace: clang::cxcursor
#endif
diff --git a/tools/libclang/CXString.cpp b/tools/libclang/CXString.cpp
new file mode 100644
index 000000000000..f2a6b091ece4
--- /dev/null
+++ b/tools/libclang/CXString.cpp
@@ -0,0 +1,130 @@
+//===- CXString.cpp - Routines for manipulating CXStrings -----------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines routines for manipulating CXStrings. It should be the
+// only file that has internal knowledge of the encoding of the data in
+// CXStrings.
+//
+//===----------------------------------------------------------------------===//
+
+#include "CXString.h"
+#include "CXTranslationUnit.h"
+#include "clang/Frontend/ASTUnit.h"
+#include "clang-c/Index.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/Support/ErrorHandling.h"
+
+using namespace clang;
+using namespace clang::cxstring;
+
+enum CXStringFlag { CXS_Unmanaged, CXS_Malloc, CXS_StringBuf };
+
+//===----------------------------------------------------------------------===//
+// Basic generation of CXStrings.
+//===----------------------------------------------------------------------===//
+
+CXString cxstring::createCXString(const char *String, bool DupString){
+ CXString Str;
+ if (DupString) {
+ Str.data = strdup(String);
+ Str.private_flags = (unsigned) CXS_Malloc;
+ } else {
+ Str.data = (void*)String;
+ Str.private_flags = (unsigned) CXS_Unmanaged;
+ }
+ return Str;
+}
+
+CXString cxstring::createCXString(llvm::StringRef String, bool DupString) {
+ CXString Result;
+ if (DupString || (!String.empty() && String.data()[String.size()] != 0)) {
+ char *Spelling = (char *)malloc(String.size() + 1);
+ memmove(Spelling, String.data(), String.size());
+ Spelling[String.size()] = 0;
+ Result.data = Spelling;
+ Result.private_flags = (unsigned) CXS_Malloc;
+ } else {
+ Result.data = (void*) String.data();
+ Result.private_flags = (unsigned) CXS_Unmanaged;
+ }
+ return Result;
+}
+
+CXString cxstring::createCXString(CXStringBuf *buf) {
+ CXString Str;
+ Str.data = buf;
+ Str.private_flags = (unsigned) CXS_StringBuf;
+ return Str;
+}
+
+
+//===----------------------------------------------------------------------===//
+// String pools.
+//===----------------------------------------------------------------------===//
+
+
+typedef std::vector<CXStringBuf *> CXStringPool;
+
+void *cxstring::createCXStringPool() {
+ return new CXStringPool();
+}
+
+void cxstring::disposeCXStringPool(void *p) {
+ CXStringPool *pool = static_cast<CXStringPool*>(p);
+ if (pool) {
+ for (CXStringPool::iterator I = pool->begin(), E = pool->end();
+ I != E; ++I) {
+ delete *I;
+ }
+ delete pool;
+ }
+}
+
+CXStringBuf *cxstring::getCXStringBuf(CXTranslationUnit TU) {
+ CXStringPool *pool = static_cast<CXStringPool*>(TU->StringPool);
+ if (pool->empty())
+ return new CXStringBuf(TU);
+ CXStringBuf *buf = pool->back();
+ buf->Data.clear();
+ pool->pop_back();
+ return buf;
+}
+
+void cxstring::disposeCXStringBuf(CXStringBuf *buf) {
+ if (buf)
+ static_cast<CXStringPool*>(buf->TU->StringPool)->push_back(buf);
+}
+
+//===----------------------------------------------------------------------===//
+// libClang public APIs.
+//===----------------------------------------------------------------------===//
+
+extern "C" {
+const char *clang_getCString(CXString string) {
+ if (string.private_flags == (unsigned) CXS_StringBuf) {
+ return ((CXStringBuf*)string.data)->Data.data();
+ }
+ return (const char*) string.data;
+}
+
+void clang_disposeString(CXString string) {
+ switch ((CXStringFlag) string.private_flags) {
+ case CXS_Unmanaged:
+ break;
+ case CXS_Malloc:
+ if (string.data)
+ free((void*)string.data);
+ break;
+ case CXS_StringBuf:
+ disposeCXStringBuf((CXStringBuf *) string.data);
+ break;
+ }
+}
+} // end: extern "C"
+
diff --git a/tools/libclang/CXString.h b/tools/libclang/CXString.h
new file mode 100644
index 000000000000..f03a6b290384
--- /dev/null
+++ b/tools/libclang/CXString.h
@@ -0,0 +1,53 @@
+//===- CXString.h - Routines for manipulating CXStrings -------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines routines for manipulating CXStrings.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_CXSTRING_H
+#define LLVM_CLANG_CXSTRING_H
+
+#include "clang-c/Index.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/SmallString.h"
+
+namespace clang {
+namespace cxstring {
+
+struct CXStringBuf {
+ llvm::SmallString<128> Data;
+ CXTranslationUnit TU;
+ CXStringBuf(CXTranslationUnit tu) : TU(tu) {}
+};
+
+/// \brief Create a CXString object from a C string.
+CXString createCXString(const char *String, bool DupString = false);
+
+/// \brief Create a CXString object from a StringRef.
+CXString createCXString(llvm::StringRef String, bool DupString = true);
+
+/// \brief Create a CXString object that is backed by a string buffer.
+CXString createCXString(CXStringBuf *buf);
+
+/// \brief Create an opaque string pool used for fast geneneration of strings.
+void *createCXStringPool();
+
+/// \brief Dispose of a string pool.
+void disposeCXStringPool(void *pool);
+
+CXStringBuf *getCXStringBuf(CXTranslationUnit TU);
+
+void disposeCXStringBuf(CXStringBuf *buf);
+
+}
+}
+
+#endif
+
diff --git a/tools/libclang/CXTranslationUnit.h b/tools/libclang/CXTranslationUnit.h
new file mode 100644
index 000000000000..6df85b7d4dc9
--- /dev/null
+++ b/tools/libclang/CXTranslationUnit.h
@@ -0,0 +1,24 @@
+//===- CXTranslationUnit.h - Routines for manipulating CXTranslationUnits -===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines routines for manipulating CXTranslationUnits.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_CXTRANSLATIONUNIT_H
+#define LLVM_CLANG_CXTRANSLATIONUNIT_H
+
+extern "C" {
+struct CXTranslationUnitImpl {
+ void *TUData;
+ void *StringPool;
+};
+}
+
+#endif
diff --git a/tools/libclang/CXType.cpp b/tools/libclang/CXType.cpp
index aa173cae32c0..93326724de84 100644
--- a/tools/libclang/CXType.cpp
+++ b/tools/libclang/CXType.cpp
@@ -12,12 +12,15 @@
//===--------------------------------------------------------------------===//
#include "CIndexer.h"
+#include "CXTranslationUnit.h"
#include "CXCursor.h"
+#include "CXString.h"
#include "CXType.h"
#include "clang/AST/Expr.h"
#include "clang/AST/Type.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclObjC.h"
+#include "clang/AST/DeclTemplate.h"
#include "clang/Frontend/ASTUnit.h"
using namespace clang;
@@ -38,7 +41,8 @@ static CXTypeKind GetBuiltinTypeKind(const BuiltinType *BT) {
BTCASE(UInt128);
BTCASE(Char_S);
BTCASE(SChar);
- BTCASE(WChar);
+ case BuiltinType::WChar_S: return CXType_WChar;
+ case BuiltinType::WChar_U: return CXType_WChar;
BTCASE(Short);
BTCASE(Int);
BTCASE(Long);
@@ -60,7 +64,7 @@ static CXTypeKind GetBuiltinTypeKind(const BuiltinType *BT) {
}
static CXTypeKind GetTypeKind(QualType T) {
- Type *TP = T.getTypePtr();
+ const Type *TP = T.getTypePtrOrNull();
if (!TP)
return CXType_Invalid;
@@ -87,7 +91,7 @@ static CXTypeKind GetTypeKind(QualType T) {
}
-CXType cxtype::MakeCXType(QualType T, ASTUnit *TU) {
+CXType cxtype::MakeCXType(QualType T, CXTranslationUnit TU) {
CXTypeKind TK = GetTypeKind(T);
CXType CT = { TK, { TK == CXType_Invalid ? 0 : T.getAsOpaquePtr(), TU }};
return CT;
@@ -99,37 +103,73 @@ static inline QualType GetQualType(CXType CT) {
return QualType::getFromOpaquePtr(CT.data[0]);
}
-static inline ASTUnit* GetASTU(CXType CT) {
- return static_cast<ASTUnit*>(CT.data[1]);
+static inline CXTranslationUnit GetTU(CXType CT) {
+ return static_cast<CXTranslationUnit>(CT.data[1]);
}
extern "C" {
CXType clang_getCursorType(CXCursor C) {
- ASTUnit *AU = cxcursor::getCursorASTUnit(C);
-
+ using namespace cxcursor;
+
+ CXTranslationUnit TU = cxcursor::getCursorTU(C);
+ ASTContext &Context = static_cast<ASTUnit *>(TU->TUData)->getASTContext();
if (clang_isExpression(C.kind)) {
QualType T = cxcursor::getCursorExpr(C)->getType();
- return MakeCXType(T, AU);
+ return MakeCXType(T, TU);
}
if (clang_isDeclaration(C.kind)) {
Decl *D = cxcursor::getCursorDecl(C);
if (TypeDecl *TD = dyn_cast<TypeDecl>(D))
- return MakeCXType(QualType(TD->getTypeForDecl(), 0), AU);
+ return MakeCXType(Context.getTypeDeclType(TD), TU);
if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D))
- return MakeCXType(QualType(ID->getTypeForDecl(), 0), AU);
+ return MakeCXType(Context.getObjCInterfaceType(ID), TU);
if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
- return MakeCXType(VD->getType(), AU);
+ return MakeCXType(VD->getType(), TU);
if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
- return MakeCXType(PD->getType(), AU);
+ return MakeCXType(PD->getType(), TU);
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
- return MakeCXType(FD->getType(), AU);
- return MakeCXType(QualType(), AU);
+ return MakeCXType(FD->getType(), TU);
+ return MakeCXType(QualType(), TU);
+ }
+
+ if (clang_isReference(C.kind)) {
+ switch (C.kind) {
+ case CXCursor_ObjCSuperClassRef: {
+ QualType T
+ = Context.getObjCInterfaceType(getCursorObjCSuperClassRef(C).first);
+ return MakeCXType(T, TU);
+ }
+
+ case CXCursor_ObjCClassRef: {
+ QualType T = Context.getObjCInterfaceType(getCursorObjCClassRef(C).first);
+ return MakeCXType(T, TU);
+ }
+
+ case CXCursor_TypeRef: {
+ QualType T = Context.getTypeDeclType(getCursorTypeRef(C).first);
+ return MakeCXType(T, TU);
+
+ }
+
+ case CXCursor_CXXBaseSpecifier:
+ return cxtype::MakeCXType(getCursorCXXBaseSpecifier(C)->getType(), TU);
+
+ case CXCursor_ObjCProtocolRef:
+ case CXCursor_TemplateRef:
+ case CXCursor_NamespaceRef:
+ case CXCursor_MemberRef:
+ case CXCursor_OverloadedDeclRef:
+ default:
+ break;
+ }
+
+ return MakeCXType(QualType(), TU);
}
- return MakeCXType(QualType(), AU);
+ return MakeCXType(QualType(), TU);
}
CXType clang_getCanonicalType(CXType CT) {
@@ -137,20 +177,36 @@ CXType clang_getCanonicalType(CXType CT) {
return CT;
QualType T = GetQualType(CT);
+ CXTranslationUnit TU = GetTU(CT);
if (T.isNull())
- return MakeCXType(QualType(), GetASTU(CT));
+ return MakeCXType(QualType(), GetTU(CT));
+
+ ASTUnit *AU = static_cast<ASTUnit*>(TU->TUData);
+ return MakeCXType(AU->getASTContext().getCanonicalType(T), TU);
+}
+
+unsigned clang_isConstQualifiedType(CXType CT) {
+ QualType T = GetQualType(CT);
+ return T.isLocalConstQualified();
+}
+
+unsigned clang_isVolatileQualifiedType(CXType CT) {
+ QualType T = GetQualType(CT);
+ return T.isLocalVolatileQualified();
+}
- ASTUnit *AU = GetASTU(CT);
- return MakeCXType(AU->getASTContext().getCanonicalType(T), AU);
+unsigned clang_isRestrictQualifiedType(CXType CT) {
+ QualType T = GetQualType(CT);
+ return T.isLocalRestrictQualified();
}
CXType clang_getPointeeType(CXType CT) {
QualType T = GetQualType(CT);
- Type *TP = T.getTypePtr();
+ const Type *TP = T.getTypePtrOrNull();
if (!TP)
- return MakeCXType(QualType(), GetASTU(CT));
+ return MakeCXType(QualType(), GetTU(CT));
switch (TP->getTypeClass()) {
case Type::Pointer:
@@ -170,7 +226,7 @@ CXType clang_getPointeeType(CXType CT) {
T = QualType();
break;
}
- return MakeCXType(T, GetASTU(CT));
+ return MakeCXType(T, GetTU(CT));
}
CXCursor clang_getTypeDeclaration(CXType CT) {
@@ -178,35 +234,54 @@ CXCursor clang_getTypeDeclaration(CXType CT) {
return cxcursor::MakeCXCursorInvalid(CXCursor_NoDeclFound);
QualType T = GetQualType(CT);
- Type *TP = T.getTypePtr();
+ const Type *TP = T.getTypePtrOrNull();
if (!TP)
return cxcursor::MakeCXCursorInvalid(CXCursor_NoDeclFound);
Decl *D = 0;
+try_again:
switch (TP->getTypeClass()) {
- case Type::Typedef:
- D = cast<TypedefType>(TP)->getDecl();
- break;
- case Type::ObjCObject:
- D = cast<ObjCObjectType>(TP)->getInterface();
- break;
- case Type::ObjCInterface:
- D = cast<ObjCInterfaceType>(TP)->getDecl();
- break;
- case Type::Record:
- case Type::Enum:
- D = cast<TagType>(TP)->getDecl();
- break;
- default:
- break;
+ case Type::Typedef:
+ D = cast<TypedefType>(TP)->getDecl();
+ break;
+ case Type::ObjCObject:
+ D = cast<ObjCObjectType>(TP)->getInterface();
+ break;
+ case Type::ObjCInterface:
+ D = cast<ObjCInterfaceType>(TP)->getDecl();
+ break;
+ case Type::Record:
+ case Type::Enum:
+ D = cast<TagType>(TP)->getDecl();
+ break;
+ case Type::TemplateSpecialization:
+ if (const RecordType *Record = TP->getAs<RecordType>())
+ D = Record->getDecl();
+ else
+ D = cast<TemplateSpecializationType>(TP)->getTemplateName()
+ .getAsTemplateDecl();
+ break;
+
+ case Type::InjectedClassName:
+ D = cast<InjectedClassNameType>(TP)->getDecl();
+ break;
+
+ // FIXME: Template type parameters!
+
+ case Type::Elaborated:
+ TP = cast<ElaboratedType>(TP)->getNamedType().getTypePtrOrNull();
+ goto try_again;
+
+ default:
+ break;
}
if (!D)
return cxcursor::MakeCXCursorInvalid(CXCursor_NoDeclFound);
- return cxcursor::MakeCXCursor(D, GetASTU(CT));
+ return cxcursor::MakeCXCursor(D, GetTU(CT));
}
CXString clang_getTypeKindSpelling(enum CXTypeKind K) {
@@ -228,7 +303,7 @@ CXString clang_getTypeKindSpelling(enum CXTypeKind K) {
TKIND(UInt128);
TKIND(Char_S);
TKIND(SChar);
- TKIND(WChar);
+ case CXType_WChar: s = "WChar"; break;
TKIND(Short);
TKIND(Int);
TKIND(Long);
@@ -266,32 +341,61 @@ unsigned clang_equalTypes(CXType A, CXType B) {
CXType clang_getResultType(CXType X) {
QualType T = GetQualType(X);
- if (!T.getTypePtr())
- return MakeCXType(QualType(), GetASTU(X));
+ if (!T.getTypePtrOrNull())
+ return MakeCXType(QualType(), GetTU(X));
if (const FunctionType *FD = T->getAs<FunctionType>())
- return MakeCXType(FD->getResultType(), GetASTU(X));
+ return MakeCXType(FD->getResultType(), GetTU(X));
- return MakeCXType(QualType(), GetASTU(X));
+ return MakeCXType(QualType(), GetTU(X));
}
CXType clang_getCursorResultType(CXCursor C) {
if (clang_isDeclaration(C.kind)) {
Decl *D = cxcursor::getCursorDecl(C);
if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
- return MakeCXType(MD->getResultType(), cxcursor::getCursorASTUnit(C));
+ return MakeCXType(MD->getResultType(), cxcursor::getCursorTU(C));
return clang_getResultType(clang_getCursorType(C));
}
- return MakeCXType(QualType(), cxcursor::getCursorASTUnit(C));
+ return MakeCXType(QualType(), cxcursor::getCursorTU(C));
}
unsigned clang_isPODType(CXType X) {
QualType T = GetQualType(X);
- if (!T.getTypePtr())
+ if (!T.getTypePtrOrNull())
return 0;
return T->isPODType() ? 1 : 0;
}
+CXString clang_getDeclObjCTypeEncoding(CXCursor C) {
+ if ((C.kind < CXCursor_FirstDecl) || (C.kind > CXCursor_LastDecl))
+ return cxstring::createCXString("");
+
+ Decl *D = static_cast<Decl*>(C.data[0]);
+ CXTranslationUnit TU = static_cast<CXTranslationUnit>(C.data[2]);
+ ASTUnit *AU = static_cast<ASTUnit*>(TU->TUData);
+ ASTContext &Ctx = AU->getASTContext();
+ std::string encoding;
+
+ if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D))
+ Ctx.getObjCEncodingForMethodDecl(OMD, encoding);
+ else if (ObjCPropertyDecl *OPD = dyn_cast<ObjCPropertyDecl>(D))
+ Ctx.getObjCEncodingForPropertyDecl(OPD, NULL, encoding);
+ else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
+ Ctx.getObjCEncodingForFunctionDecl(FD, encoding);
+ else {
+ QualType Ty;
+ if (TypeDecl *TD = dyn_cast<TypeDecl>(D))
+ Ty = Ctx.getTypeDeclType(TD);
+ if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
+ Ty = VD->getType();
+ else return cxstring::createCXString("?");
+ Ctx.getObjCEncodingForType(Ty, encoding);
+ }
+
+ return cxstring::createCXString(encoding);
+}
+
} // end: extern "C"
diff --git a/tools/libclang/CXType.h b/tools/libclang/CXType.h
index 94151ed6b98b..7660bebf041b 100644
--- a/tools/libclang/CXType.h
+++ b/tools/libclang/CXType.h
@@ -23,7 +23,7 @@ class ASTUnit;
namespace cxtype {
-CXType MakeCXType(QualType T, ASTUnit *TU);
+CXType MakeCXType(QualType T, CXTranslationUnit TU);
}} // end namespace clang::cxtype
#endif
diff --git a/tools/libclang/Makefile b/tools/libclang/Makefile
index 6d2a13cfc0b4..e684652aa4ca 100644
--- a/tools/libclang/Makefile
+++ b/tools/libclang/Makefile
@@ -15,7 +15,7 @@ EXPORTED_SYMBOL_FILE = $(PROJ_SRC_DIR)/libclang.exports
LINK_LIBS_IN_SHARED = 1
SHARED_LIBRARY = 1
-LINK_COMPONENTS := bitreader mc core
+LINK_COMPONENTS := support mc
USEDLIBS = clangFrontend.a clangDriver.a clangSerialization.a clangParse.a \
clangSema.a clangAnalysis.a clangAST.a clangLex.a clangBasic.a
diff --git a/tools/libclang/libclang.darwin.exports b/tools/libclang/libclang.darwin.exports
index d1b45a248674..7614544ca3bc 100644
--- a/tools/libclang/libclang.darwin.exports
+++ b/tools/libclang/libclang.darwin.exports
@@ -1,6 +1,7 @@
+_clang_CXCursorSet_contains
+_clang_CXCursorSet_insert
_clang_CXXMethod_isStatic
_clang_annotateTokens
-_clang_codeComplete
_clang_codeCompleteAt
_clang_codeCompleteGetDiagnostic
_clang_codeCompleteGetNumDiagnostics
@@ -10,6 +11,7 @@ _clang_constructUSR_ObjCIvar
_clang_constructUSR_ObjCMethod
_clang_constructUSR_ObjCProperty
_clang_constructUSR_ObjCProtocol
+_clang_createCXCursorSet
_clang_createIndex
_clang_createTranslationUnit
_clang_createTranslationUnitFromSourceFile
@@ -18,9 +20,11 @@ _clang_defaultDiagnosticDisplayOptions
_clang_defaultEditingTranslationUnitOptions
_clang_defaultReparseOptions
_clang_defaultSaveOptions
+_clang_disposeCXCursorSet
_clang_disposeCodeCompleteResults
_clang_disposeDiagnostic
_clang_disposeIndex
+_clang_disposeOverriddenCursors
_clang_disposeString
_clang_disposeTokens
_clang_disposeTranslationUnit
@@ -28,9 +32,11 @@ _clang_enableStackTraces
_clang_equalCursors
_clang_equalLocations
_clang_equalTypes
+_clang_executeOnThread
_clang_formatDiagnostic
_clang_getCString
_clang_getCXXAccessSpecifier
+_clang_getCanonicalCursor
_clang_getCanonicalType
_clang_getClangVersion
_clang_getCompletionAvailability
@@ -41,23 +47,30 @@ _clang_getCompletionPriority
_clang_getCursor
_clang_getCursorAvailability
_clang_getCursorDefinition
+_clang_getCursorDisplayName
_clang_getCursorExtent
_clang_getCursorKind
_clang_getCursorKindSpelling
_clang_getCursorLanguage
+_clang_getCursorLexicalParent
_clang_getCursorLinkage
_clang_getCursorLocation
_clang_getCursorReferenced
_clang_getCursorResultType
+_clang_getCursorSemanticParent
_clang_getCursorSpelling
_clang_getCursorType
_clang_getCursorUSR
+_clang_getDeclObjCTypeEncoding
_clang_getDefinitionSpellingAndExtent
_clang_getDiagnostic
+_clang_getDiagnosticCategory
+_clang_getDiagnosticCategoryName
_clang_getDiagnosticFixIt
_clang_getDiagnosticLocation
_clang_getDiagnosticNumFixIts
_clang_getDiagnosticNumRanges
+_clang_getDiagnosticOption
_clang_getDiagnosticRange
_clang_getDiagnosticSeverity
_clang_getDiagnosticSpelling
@@ -65,20 +78,26 @@ _clang_getFile
_clang_getFileName
_clang_getFileTime
_clang_getIBOutletCollectionType
+_clang_getIncludedFile
_clang_getInclusions
_clang_getInstantiationLocation
_clang_getLocation
+_clang_getLocationForOffset
_clang_getNullCursor
_clang_getNullLocation
_clang_getNullRange
_clang_getNumCompletionChunks
_clang_getNumDiagnostics
+_clang_getNumOverloadedDecls
+_clang_getOverloadedDecl
+_clang_getOverriddenCursors
_clang_getPointeeType
_clang_getRange
_clang_getRangeEnd
_clang_getRangeStart
_clang_getResultType
_clang_getSpecializedCursorTemplate
+_clang_getSpellingLocation
_clang_getTemplateCursorKind
_clang_getTokenExtent
_clang_getTokenKind
@@ -88,21 +107,25 @@ _clang_getTranslationUnitCursor
_clang_getTranslationUnitSpelling
_clang_getTypeDeclaration
_clang_getTypeKindSpelling
+_clang_hashCursor
_clang_isCursorDefinition
+_clang_isConstQualifiedType
_clang_isDeclaration
_clang_isExpression
_clang_isInvalid
_clang_isPODType
_clang_isPreprocessing
_clang_isReference
+_clang_isRestrictQualifiedType
_clang_isStatement
_clang_isTranslationUnit
_clang_isUnexposed
_clang_isVirtualBase
+_clang_isVolatileQualifiedType
_clang_parseTranslationUnit
_clang_reparseTranslationUnit
_clang_saveTranslationUnit
-_clang_setUseExternalASTGeneration
_clang_sortCodeCompletionResults
_clang_tokenize
_clang_visitChildren
+_clang_visitChildrenWithBlock
diff --git a/tools/libclang/libclang.exports b/tools/libclang/libclang.exports
index 0ea6993b218c..c2f0587b9ae8 100644
--- a/tools/libclang/libclang.exports
+++ b/tools/libclang/libclang.exports
@@ -1,6 +1,7 @@
+clang_CXCursorSet_contains
+clang_CXCursorSet_insert
clang_CXXMethod_isStatic
clang_annotateTokens
-clang_codeComplete
clang_codeCompleteAt
clang_codeCompleteGetDiagnostic
clang_codeCompleteGetNumDiagnostics
@@ -10,6 +11,7 @@ clang_constructUSR_ObjCIvar
clang_constructUSR_ObjCMethod
clang_constructUSR_ObjCProperty
clang_constructUSR_ObjCProtocol
+clang_createCXCursorSet
clang_createIndex
clang_createTranslationUnit
clang_createTranslationUnitFromSourceFile
@@ -18,9 +20,11 @@ clang_defaultDiagnosticDisplayOptions
clang_defaultEditingTranslationUnitOptions
clang_defaultReparseOptions
clang_defaultSaveOptions
+clang_disposeCXCursorSet
clang_disposeCodeCompleteResults
clang_disposeDiagnostic
clang_disposeIndex
+clang_disposeOverriddenCursors
clang_disposeString
clang_disposeTokens
clang_disposeTranslationUnit
@@ -28,9 +32,11 @@ clang_enableStackTraces
clang_equalCursors
clang_equalLocations
clang_equalTypes
+clang_executeOnThread
clang_formatDiagnostic
clang_getCString
clang_getCXXAccessSpecifier
+clang_getCanonicalCursor
clang_getCanonicalType
clang_getClangVersion
clang_getCompletionAvailability
@@ -41,23 +47,30 @@ clang_getCompletionPriority
clang_getCursor
clang_getCursorAvailability
clang_getCursorDefinition
+clang_getCursorDisplayName
clang_getCursorExtent
clang_getCursorKind
clang_getCursorKindSpelling
clang_getCursorLanguage
+clang_getCursorLexicalParent
clang_getCursorLinkage
clang_getCursorLocation
clang_getCursorReferenced
clang_getCursorResultType
+clang_getCursorSemanticParent
clang_getCursorSpelling
clang_getCursorType
clang_getCursorUSR
+clang_getDeclObjCTypeEncoding
clang_getDefinitionSpellingAndExtent
clang_getDiagnostic
+clang_getDiagnosticCategory
+clang_getDiagnosticCategoryName
clang_getDiagnosticFixIt
clang_getDiagnosticLocation
clang_getDiagnosticNumFixIts
clang_getDiagnosticNumRanges
+clang_getDiagnosticOption
clang_getDiagnosticRange
clang_getDiagnosticSeverity
clang_getDiagnosticSpelling
@@ -65,20 +78,26 @@ clang_getFile
clang_getFileName
clang_getFileTime
clang_getIBOutletCollectionType
+clang_getIncludedFile
clang_getInclusions
clang_getInstantiationLocation
clang_getLocation
+clang_getLocationForOffset
clang_getNullCursor
clang_getNullLocation
clang_getNullRange
clang_getNumCompletionChunks
clang_getNumDiagnostics
+clang_getNumOverloadedDecls
+clang_getOverloadedDecl
+clang_getOverriddenCursors
clang_getPointeeType
clang_getRange
clang_getRangeEnd
clang_getRangeStart
clang_getResultType
clang_getSpecializedCursorTemplate
+clang_getSpellingLocation
clang_getTemplateCursorKind
clang_getTokenExtent
clang_getTokenKind
@@ -88,6 +107,8 @@ clang_getTranslationUnitCursor
clang_getTranslationUnitSpelling
clang_getTypeDeclaration
clang_getTypeKindSpelling
+clang_hashCursor
+clang_isConstQualifiedType
clang_isCursorDefinition
clang_isDeclaration
clang_isExpression
@@ -95,14 +116,16 @@ clang_isInvalid
clang_isPODType
clang_isPreprocessing
clang_isReference
+clang_isRestrictQualifiedType
clang_isStatement
clang_isTranslationUnit
clang_isUnexposed
clang_isVirtualBase
+clang_isVolatileQualifiedType
clang_parseTranslationUnit
clang_reparseTranslationUnit
clang_saveTranslationUnit
-clang_setUseExternalASTGeneration
clang_sortCodeCompletionResults
clang_tokenize
clang_visitChildren
+clang_visitChildrenWithBlock
diff --git a/tools/scan-build/ccc-analyzer b/tools/scan-build/ccc-analyzer
index c182a686202f..5601387d794c 100755
--- a/tools/scan-build/ccc-analyzer
+++ b/tools/scan-build/ccc-analyzer
@@ -115,6 +115,7 @@ sub ProcessClangFailure {
##----------------------------------------------------------------------------##
sub GetCCArgs {
+ my $mode = shift;
my $Args = shift;
pipe (FROM_CHILD, TO_PARENT);
@@ -123,7 +124,7 @@ sub GetCCArgs {
close FROM_CHILD;
open(STDOUT,">&", \*TO_PARENT);
open(STDERR,">&", \*TO_PARENT);
- exec $Clang, "-###", "-fsyntax-only", @$Args;
+ exec $Clang, "-###", $mode, @$Args;
}
close(TO_PARENT);
my $line;
@@ -137,7 +138,7 @@ sub GetCCArgs {
die "could not find clang line\n" if (!defined $line);
# Strip the newline and initial whitspace
- chomp $line;
+ chomp $line;
$line =~ s/^\s+//;
my @items = quotewords('\s+', 0, $line);
my $cmd = shift @items;
@@ -147,70 +148,72 @@ sub GetCCArgs {
sub Analyze {
my ($Clang, $Args, $AnalyzeArgs, $Lang, $Output, $Verbose, $HtmlDir,
- $file, $Analyses) = @_;
-
- $Args = GetCCArgs($Args);
+ $file) = @_;
- my $RunAnalyzer = 0;
my $Cmd;
my @CmdArgs;
my @CmdArgsSansAnalyses;
-
+
if ($Lang =~ /header/) {
exit 0 if (!defined ($Output));
$Cmd = 'cp';
- push @CmdArgs,$file;
+ push @CmdArgs, $file;
# Remove the PCH extension.
$Output =~ s/[.]gch$//;
- push @CmdArgs,$Output;
- @CmdArgsSansAnalyses = @CmdArgs;
+ push @CmdArgs, $Output;
+ @CmdArgsSansAnalyses = @CmdArgs;
}
else {
$Cmd = $Clang;
- push @CmdArgs, "-cc1";
- push @CmdArgs,'-DIBOutlet=__attribute__((iboutlet))';
- push @CmdArgs, @$Args;
- @CmdArgsSansAnalyses = @CmdArgs;
- push @CmdArgs,'-analyze';
- push @CmdArgs,"-analyzer-display-progress";
- push @CmdArgs,"-analyzer-eagerly-assume";
- push @CmdArgs,"-analyzer-opt-analyze-nested-blocks";
- push @CmdArgs,(split /\s/,$Analyses);
-
- if (defined $ENV{"CCC_EXPERIMENTAL_CHECKS"}) {
- push @CmdArgs,"-analyzer-experimental-internal-checks";
- push @CmdArgs,"-analyzer-experimental-checks";
+ if ($Lang eq "objective-c" || $Lang eq "objective-c++") {
+ push @$Args,'-DIBOutlet=__attribute__((iboutlet))';
+ push @$Args,'-DIBOutletCollection(ClassName)=__attribute__((iboutletcollection)))';
+ push @$Args,'-DIBAction=void)__attribute__((ibaction)';
}
-
- $RunAnalyzer = 1;
- }
-
- # Add the analysis arguments passed down from scan-build.
- foreach my $Arg (@$AnalyzeArgs) {
- push @CmdArgs, $Arg;
- }
-
- my @PrintArgs;
- my $dir;
- if ($RunAnalyzer) {
+ # Create arguments for doing regular parsing.
+ my $SyntaxArgs = GetCCArgs("-fsyntax-only", $Args);
+ @CmdArgsSansAnalyses = @CmdArgs;
+ push @CmdArgsSansAnalyses, @$SyntaxArgs;
+
+ # Create arguments for doing static analysis.
if (defined $ResultFile) {
- push @CmdArgs,'-o';
- push @CmdArgs, $ResultFile;
+ push @$Args,'-o';
+ push @$Args, $ResultFile;
}
elsif (defined $HtmlDir) {
- push @CmdArgs,'-o';
- push @CmdArgs, $HtmlDir;
+ push @$Args,'-o';
+ push @$Args, $HtmlDir;
+ }
+ push @$Args,"-Xclang";
+ push @$Args,"-analyzer-display-progress";
+
+ foreach my $arg (@$AnalyzeArgs) {
+ push @$Args, "-Xclang";
+ push @$Args, $arg;
+ }
+
+ # Display Ubiviz graph?
+ if (defined $ENV{'CCC_UBI'}) {
+ push @$Args, "-Xclang";
+ push @$Args,"-analyzer-viz-egraph-ubigraph";
}
+
+ my $AnalysisArgs = GetCCArgs("--analyze", $Args);
+ push @CmdArgs, @$AnalysisArgs;
}
-
+
+ my @PrintArgs;
+ my $dir;
+
if ($Verbose) {
$dir = getcwd();
print STDERR "\n[LOCATION]: $dir\n";
push @PrintArgs,"'$Cmd'";
- foreach my $arg (@CmdArgs) { push @PrintArgs,"\'$arg\'"; }
+ foreach my $arg (@CmdArgs) {
+ push @PrintArgs,"\'$arg\'";
+ }
}
-
if ($Verbose == 1) {
# We MUST print to stderr. Some clients use the stdout output of
# gcc for various purposes.
@@ -220,11 +223,7 @@ sub Analyze {
elsif ($Verbose == 2) {
print STDERR "#SHELL (cd '$dir' && @PrintArgs)\n";
}
-
- if (defined $ENV{'CCC_UBI'}) {
- push @CmdArgs,"--analyzer-viz-egraph-ubigraph";
- }
-
+
# Capture the STDERR of clang and send it to a temporary file.
# Capture the STDOUT of clang and reroute it to ccc-analyzer's STDERR.
# We save the output file in the 'crashes' directory if clang encounters
@@ -237,13 +236,13 @@ sub Analyze {
open(STDERR,">&", \*TO_PARENT);
exec $Cmd, @CmdArgs;
}
-
+
close TO_PARENT;
my ($ofh, $ofile) = tempfile("clang_output_XXXXXX", DIR => $HtmlDir);
while (<FROM_CHILD>) {
print $ofh $_;
- print STDERR $_;
+ print STDERR $_;
}
waitpid($pid,0);
@@ -266,11 +265,11 @@ sub Analyze {
# Check if there were any unhandled attributes.
if (open(CHILD, $ofile)) {
my %attributes_not_handled;
-
+
# Don't flag warnings about the following attributes that we
# know are currently not supported by Clang.
$attributes_not_handled{"cdecl"} = 1;
-
+
my $ppfile;
while (<CHILD>) {
next if (! /warning: '([^\']+)' attribute ignored/);
@@ -406,19 +405,19 @@ my %Uniqued;
# Forward arguments to gcc.
my $Status = system($Compiler,@ARGV);
+if (defined $ENV{'CCC_ANALYZER_LOG'}) {
+ print "$Compiler @ARGV\n";
+}
if ($Status) { exit($Status >> 8); }
# Get the analysis options.
my $Analyses = $ENV{'CCC_ANALYZER_ANALYSIS'};
-if (!defined($Analyses)) { $Analyses = '-analyzer-check-objc-mem'; }
# Get the store model.
my $StoreModel = $ENV{'CCC_ANALYZER_STORE_MODEL'};
-if (!defined $StoreModel) { $StoreModel = "region"; }
# Get the constraints engine.
my $ConstraintsModel = $ENV{'CCC_ANALYZER_CONSTRAINTS_MODEL'};
-if (!defined $ConstraintsModel) { $ConstraintsModel = "range"; }
# Get the output format.
my $OutputFormat = $ENV{'CCC_ANALYZER_OUTPUT_FORMAT'};
@@ -625,6 +624,10 @@ if ($Action eq 'compile' or $Action eq 'link') {
if (defined $ConstraintsModel) {
push @AnalyzeArgs, "-analyzer-constraints=$ConstraintsModel";
}
+
+# if (defined $Analyses) {
+# push @AnalyzeArgs, split '\s+', $Analyses;
+# }
if (defined $OutputFormat) {
push @AnalyzeArgs, "-analyzer-output=" . $OutputFormat;
@@ -637,8 +640,8 @@ if ($Action eq 'compile' or $Action eq 'link') {
}
}
- push @CmdArgs,@CompileOpts;
- push @CmdArgs,$file;
+ push @CmdArgs, @CompileOpts;
+ push @CmdArgs, $file;
if (scalar @Archs) {
foreach my $arch (@Archs) {
@@ -647,12 +650,12 @@ if ($Action eq 'compile' or $Action eq 'link') {
push @NewArgs, $arch;
push @NewArgs, @CmdArgs;
Analyze($Clang, \@NewArgs, \@AnalyzeArgs, $FileLang, $Output,
- $Verbose, $HtmlDir, $file, $Analyses);
+ $Verbose, $HtmlDir, $file);
}
}
else {
Analyze($Clang, \@CmdArgs, \@AnalyzeArgs, $FileLang, $Output,
- $Verbose, $HtmlDir, $file, $Analyses);
+ $Verbose, $HtmlDir, $file);
}
}
}
diff --git a/tools/scan-build/scan-build b/tools/scan-build/scan-build
index 8a7afbb3eaee..80585b1f7c7e 100755
--- a/tools/scan-build/scan-build
+++ b/tools/scan-build/scan-build
@@ -99,30 +99,6 @@ else {
}
my $ClangCXX = $Clang . "++";
-my %AvailableAnalyses;
-
-# Query clang for analysis options.
-open(PIPE, "-|", $Clang, "-cc1", "-help") or
- DieDiag("Cannot execute '$Clang'\n");
-
-while(<PIPE>) {
- if (/(-analyzer-check-[^\s]+)/) {
- $AvailableAnalyses{$1} = 1;
- next;
- }
-}
-close (PIPE);
-
-my %AnalysesDefaultEnabled = (
- '-analyzer-check-dead-stores' => 1,
- '-analyzer-check-objc-mem' => 1,
- '-analyzer-check-objc-methodsigs' => 1,
- # Do not enable the missing -dealloc check by default.
- # '-analyzer-check-objc-missing-dealloc' => 1,
- '-analyzer-check-objc-unused-ivars' => 1,
- '-analyzer-check-security-syntactic' => 1
-);
-
##----------------------------------------------------------------------------##
# GetHTMLRunDir - Construct an HTML directory name for the current sub-run.
##----------------------------------------------------------------------------##
@@ -304,6 +280,38 @@ sub UpdateInFilePath {
}
##----------------------------------------------------------------------------##
+# AddStatLine - Decode and insert a statistics line into the database.
+##----------------------------------------------------------------------------##
+
+sub AddStatLine {
+ my $Line = shift;
+ my $Stats = shift;
+
+ print $Line . "\n";
+
+ my $Regex = qr/(.*?)\ :\ (.*?)\ ->\ Total\ CFGBlocks:\ (\d+)\ \|\ Unreachable
+ \ CFGBlocks:\ (\d+)\ \|\ Aborted\ Block:\ (yes|no)\ \|\ Empty\ WorkList:
+ \ (yes|no)/x;
+
+ if ($Line !~ $Regex) {
+ return;
+ }
+
+ # Create a hash of the interesting fields
+ my $Row = {
+ Filename => $1,
+ Function => $2,
+ Total => $3,
+ Unreachable => $4,
+ Aborted => $5,
+ Empty => $6
+ };
+
+ # Add them to the stats array
+ push @$Stats, $Row;
+}
+
+##----------------------------------------------------------------------------##
# ScanFile - Scan a report file for various identifying attributes.
##----------------------------------------------------------------------------##
@@ -317,6 +325,7 @@ sub ScanFile {
my $Index = shift;
my $Dir = shift;
my $FName = shift;
+ my $Stats = shift;
# Compute a digest for the report file. Determine if we have already
# scanned a file that looks just like it.
@@ -337,11 +346,12 @@ sub ScanFile {
# Scan the report file for tags.
open(IN, "$Dir/$FName") or DieDiag("Cannot open '$Dir/$FName'\n");
- my $BugType = "";
- my $BugFile = "";
- my $BugCategory;
- my $BugPathLength = 1;
- my $BugLine = 0;
+ my $BugType = "";
+ my $BugFile = "";
+ my $BugCategory = "";
+ my $BugDescription = "";
+ my $BugPathLength = 1;
+ my $BugLine = 0;
while (<IN>) {
last if (/<!-- BUGMETAEND -->/);
@@ -362,6 +372,9 @@ sub ScanFile {
elsif (/<!-- BUGCATEGORY (.*) -->$/) {
$BugCategory = $1;
}
+ elsif (/<!-- BUGDESC (.*) -->$/) {
+ $BugDescription = $1;
+ }
}
close(IN);
@@ -369,7 +382,13 @@ sub ScanFile {
if (!defined $BugCategory) {
$BugCategory = "Other";
}
-
+
+ # Don't add internal statistics to the bug reports
+ if ($BugCategory =~ /statistics/i) {
+ AddStatLine($BugDescription, $Stats);
+ return;
+ }
+
push @$Index,[ $FName, $BugCategory, $BugType, $BugFile, $BugLine,
$BugPathLength ];
}
@@ -404,13 +423,61 @@ sub CopyFiles {
}
##----------------------------------------------------------------------------##
+# CalcStats - Calculates visitation statistics and returns the string.
+##----------------------------------------------------------------------------##
+
+sub CalcStats {
+ my $Stats = shift;
+
+ my $TotalBlocks = 0;
+ my $UnreachedBlocks = 0;
+ my $TotalFunctions = scalar(@$Stats);
+ my $BlockAborted = 0;
+ my $WorkListAborted = 0;
+ my $Aborted = 0;
+
+ # Calculate the unique files
+ my $FilesHash = {};
+
+ foreach my $Row (@$Stats) {
+ $FilesHash->{$Row->{Filename}} = 1;
+ $TotalBlocks += $Row->{Total};
+ $UnreachedBlocks += $Row->{Unreachable};
+ $BlockAborted++ if $Row->{Aborted} eq 'yes';
+ $WorkListAborted++ if $Row->{Empty} eq 'no';
+ $Aborted++ if $Row->{Aborted} eq 'yes' || $Row->{Empty} eq 'no';
+ }
+
+ my $TotalFiles = scalar(keys(%$FilesHash));
+
+ # Calculations
+ my $PercentAborted = sprintf("%.2f", $Aborted / $TotalFunctions * 100);
+ my $PercentBlockAborted = sprintf("%.2f", $BlockAborted / $TotalFunctions
+ * 100);
+ my $PercentWorkListAborted = sprintf("%.2f", $WorkListAborted /
+ $TotalFunctions * 100);
+ my $PercentBlocksUnreached = sprintf("%.2f", $UnreachedBlocks / $TotalBlocks
+ * 100);
+
+ my $StatsString = "Analyzed $TotalBlocks blocks in $TotalFunctions functions"
+ . " in $TotalFiles files\n"
+ . "$Aborted functions aborted early ($PercentAborted%)\n"
+ . "$BlockAborted had aborted blocks ($PercentBlockAborted%)\n"
+ . "$WorkListAborted had unfinished worklists ($PercentWorkListAborted%)\n"
+ . "$UnreachedBlocks blocks were never reached ($PercentBlocksUnreached%)\n";
+
+ return $StatsString;
+}
+
+##----------------------------------------------------------------------------##
# Postprocess - Postprocess the results of an analysis scan.
##----------------------------------------------------------------------------##
sub Postprocess {
- my $Dir = shift;
- my $BaseDir = shift;
+ my $Dir = shift;
+ my $BaseDir = shift;
+ my $AnalyzerStats = shift;
die "No directory specified." if (!defined $Dir);
@@ -430,8 +497,9 @@ sub Postprocess {
}
# Scan each report file and build an index.
- my @Index;
- foreach my $file (@files) { ScanFile(\@Index, $Dir, $file); }
+ my @Index;
+ my @Stats;
+ foreach my $file (@files) { ScanFile(\@Index, $Dir, $file, \@Stats); }
# Scan the failures directory and use the information in the .info files
# to update the common prefix directory.
@@ -745,6 +813,9 @@ ENDTEXT
system("chmod", "755", $Dir);
if (defined $BaseDir) { system("chmod", "755", $BaseDir); }
+ # Print statistics
+ print CalcStats(\@Stats) if $AnalyzerStats;
+
my $Num = scalar(@Index);
Diag("$Num bugs found.\n");
if ($Num > 0 && -r "$Dir/index.html") {
@@ -793,6 +864,7 @@ sub RunBuildCommand {
if ($Cmd =~ /(.*\/?gcc[^\/]*$)/ or
$Cmd =~ /(.*\/?cc[^\/]*$)/ or
$Cmd =~ /(.*\/?llvm-gcc[^\/]*$)/ or
+ $Cmd =~ /(.*\/?clang$)/ or
$Cmd =~ /(.*\/?ccc-analyzer[^\/]*$)/) {
if (!($Cmd =~ /ccc-analyzer/) and !defined $ENV{"CCC_CC"}) {
@@ -805,6 +877,7 @@ sub RunBuildCommand {
elsif ($Cmd =~ /(.*\/?g\+\+[^\/]*$)/ or
$Cmd =~ /(.*\/?c\+\+[^\/]*$)/ or
$Cmd =~ /(.*\/?llvm-g\+\+[^\/]*$)/ or
+ $Cmd =~ /(.*\/?clang\+\+$)/ or
$Cmd =~ /(.*\/?c\+\+-analyzer[^\/]*$)/) {
if (!($Cmd =~ /c\+\+-analyzer/) and !defined $ENV{"CCC_CXX"}) {
$ENV{"CCC_CXX"} = $1;
@@ -838,9 +911,6 @@ sub RunBuildCommand {
}
}
- # Disable distributed builds for xcodebuild.
- AddIfNotPresent($Args,"-nodistribute");
-
# Disable PCH files until clang supports them.
AddIfNotPresent($Args,"GCC_PRECOMPILE_PREFIX_HEADER=NO");
@@ -873,8 +943,6 @@ OPTIONS:
-analyze-headers - Also analyze functions in #included files.
- --experimental-checks - Enable experimental checks that are currently in heavy testing
-
-o - Target directory for HTML report files. Subdirectories
will be created as needed to represent separate "runs" of
the analyzer. If this option is not specified, a directory
@@ -930,21 +998,15 @@ ADVANCED OPTIONS:
-no-failure-reports - Do not create a 'failures' subdirectory that includes
analyzer crash reports and preprocessed source files.
-AVAILABLE ANALYSES (multiple analyses may be specified):
+ -stats - Generates visitation statistics for the project being analyzed.
+ -maxloop N - specifiy the number of times a block can be visited before giving
+ up. Default is 3. Increase for more comprehensive coverage at a
+ cost of speed.
ENDTEXT
- foreach my $Analysis (sort keys %AvailableAnalyses) {
- if (defined $AnalysesDefaultEnabled{$Analysis}) {
- print " (+)";
- }
- else {
- print " ";
- }
-
- print " $Analysis\n";
- }
-
+# FIXME: Print out available analyesis.
+
print <<ENDTEXT
NOTE: "(+)" indicates that an analysis is enabled by default unless one
@@ -1004,6 +1066,8 @@ my @AnalysesToRun;
my $StoreModel;
my $ConstraintsModel;
my $OutputFormat = "html";
+my $AnalyzerStats = 0;
+my $MaxLoop = 0;
if (!@ARGV) {
DisplayHelp();
@@ -1027,12 +1091,6 @@ while (@ARGV) {
next;
}
- if (defined $AvailableAnalyses{$arg}) {
- shift @ARGV;
- push @AnalysesToRun, $arg;
- next;
- }
-
if ($arg eq "-o") {
shift @ARGV;
@@ -1068,13 +1126,7 @@ while (@ARGV) {
$IgnoreErrors = 1;
next;
}
-
- if ($arg eq "--experimental-checks") {
- shift @ARGV;
- $ENV{"CCC_EXPERIMENTAL_CHECKS"} = 1;
- next;
- }
-
+
if ($arg =~ /^--use-cc(=(.+))?$/) {
shift @ARGV;
my $cc;
@@ -1156,6 +1208,16 @@ while (@ARGV) {
$ENV{"CCC_REPORT_FAILURES"} = 0;
next;
}
+ if ($arg eq "-stats") {
+ shift @ARGV;
+ $AnalyzerStats = 1;
+ next;
+ }
+ if ($arg eq "-maxloop") {
+ shift @ARGV;
+ $MaxLoop = shift @ARGV;
+ next;
+ }
DieDiag("unrecognized option '$arg'\n") if ($arg =~ /^-/);
@@ -1210,16 +1272,18 @@ if ($Verbose >= 3) {
$ENV{'CCC_ANALYZER_LOG'} = 1;
}
-if (scalar(@AnalysesToRun) == 0) {
- foreach my $key (keys %AnalysesDefaultEnabled) {
- push @AnalysesToRun,$key;
- }
-}
-
if ($AnalyzeHeaders) {
push @AnalysesToRun,"-analyzer-opt-analyze-headers";
}
+if ($AnalyzerStats) {
+ push @AnalysesToRun, '-analyzer-stats';
+}
+
+if ($MaxLoop > 0) {
+ push @AnalysesToRun, '-analyzer-max-loop ' . $MaxLoop;
+}
+
$ENV{'CCC_ANALYZER_ANALYSIS'} = join ' ',@AnalysesToRun;
if (defined $StoreModel) {
@@ -1244,7 +1308,7 @@ if (defined $OutputFormat) {
}
elsif ($OutputFormat =~ /html/) {
# Postprocess the HTML directory.
- my $NumBugs = Postprocess($HtmlDir, $BaseDir);
+ my $NumBugs = Postprocess($HtmlDir, $BaseDir, $AnalyzerStats);
if ($ViewResults and -r "$HtmlDir/index.html") {
Diag "Analysis run complete.\n";