Skip to content

Commit

Permalink
Make CursorInfos pointers. This seems to speed things up a little
Browse files Browse the repository at this point in the history
bit. Not entirely sure why. Maybe they get copied a lot.
  • Loading branch information
Andersbakken committed Feb 18, 2014
1 parent 15d9a43 commit d10fde4
Show file tree
Hide file tree
Showing 16 changed files with 242 additions and 185 deletions.
111 changes: 61 additions & 50 deletions src/ClangIndexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,14 +646,16 @@ void ClangIndexer::handleReference(const CXCursor &cursor, CXCursorKind kind, co
if (!reffedLoc.isValid())
return;

CursorInfo &refInfo = mData->symbols[reffedLoc];
if (!refInfo.symbolLength && !handleCursor(ref, refKind, reffedLoc))
std::shared_ptr<CursorInfo> &refInfo = mData->symbols[reffedLoc];
if (!refInfo && !handleCursor(ref, refKind, reffedLoc))
return;

refInfo.references.insert(location);
refInfo->references.insert(location);

CursorInfo &info = mData->symbols[location];
info.targets.insert(reffedLoc);
std::shared_ptr<CursorInfo> &info = mData->symbols[location];
if (!info)
info.reset(new CursorInfo);
info->targets.insert(reffedLoc);

// We need the new cursor to replace the symbolLength. This is important
// in the following case:
Expand All @@ -670,29 +672,29 @@ void ClangIndexer::handleReference(const CXCursor &cursor, CXCursorKind kind, co
// The !isCursor is var decls and field decls where we set up a target even
// if they're not considered references

if (!RTags::isCursor(info.kind) && (!info.symbolLength || info.bestTarget(mData->symbols).kind == refKind)) {
if (!RTags::isCursor(info->kind) && (!info->symbolLength || info->bestTarget(mData->symbols)->kind == refKind)) {
CXSourceRange range = clang_getCursorExtent(cursor);
CXSourceLocation rangeStart = clang_getRangeStart(range);
CXSourceLocation rangeEnd = clang_getRangeEnd(range);
unsigned startLine, startColumn, endLine, endColumn;
clang_getPresumedLocation(rangeStart, 0, &startLine, &startColumn);
clang_getPresumedLocation(rangeEnd, 0, &endLine, &endColumn);
info.startLine = startLine;
info.startColumn = startColumn;
info.endLine = endLine;
info.endColumn = endColumn;
info.definition = false;
info.kind = kind;
info->startLine = startLine;
info->startColumn = startColumn;
info->endLine = endLine;
info->endColumn = endColumn;
info->definition = false;
info->kind = kind;
if (isOperator) {
unsigned start, end;
clang_getSpellingLocation(rangeStart, 0, 0, 0, &start);
clang_getSpellingLocation(rangeEnd, 0, 0, 0, &end);
info.symbolLength = end - start;
info->symbolLength = end - start;
} else {
info.symbolLength = refInfo.symbolLength;
info->symbolLength = refInfo->symbolLength;
}
info.symbolName = refInfo.symbolName;
info.type = clang_getCursorType(cursor).kind;
info->symbolName = refInfo->symbolName;
info->type = clang_getCursorType(cursor).kind;
}

Set<Location> &val = mData->references[location];
Expand All @@ -709,10 +711,12 @@ void ClangIndexer::addOverriddenCursors(const CXCursor& cursor, const Location&
return;
for (unsigned i=0; i<count; ++i) {
Location loc = createLocation(overridden[i]);
CursorInfo &o = mData->symbols[loc];
std::shared_ptr<CursorInfo> &o = mData->symbols[loc];
if (!o)
o.reset(new CursorInfo);

//error() << "adding overridden (1) " << location << " to " << o;
o.references.insert(location);
o->references.insert(location);
List<CursorInfo*>::const_iterator inf = infos.begin();
const List<CursorInfo*>::const_iterator infend = infos.end();
while (inf != infend) {
Expand All @@ -721,7 +725,7 @@ void ClangIndexer::addOverriddenCursors(const CXCursor& cursor, const Location&
++inf;
}

infos.append(&o);
infos.append(o.get());
addOverriddenCursors(overridden[i], loc, infos);
infos.removeLast();
}
Expand All @@ -744,12 +748,14 @@ void ClangIndexer::handleInclude(const CXCursor &cursor, CXCursorKind kind, cons
mData->symbolNames[(include + path)].insert(location);
mData->symbolNames[(include + path.fileName())].insert(location);
}
CursorInfo &info = mData->symbols[location];
info.targets.insert(refLoc);
info.kind = cursor.kind;
info.definition = false;
info.symbolName = "#include " + RTags::eatString(clang_getCursorDisplayName(cursor));
info.symbolLength = info.symbolName.size() + 2;
std::shared_ptr<CursorInfo> &info = mData->symbols[location];
if (!info)
info.reset(new CursorInfo);
info->targets.insert(refLoc);
info->kind = cursor.kind;
info->definition = false;
info->symbolName = "#include " + RTags::eatString(clang_getCursorDisplayName(cursor));
info->symbolLength = info->symbolName.size() + 2;
// this fails for things like:
// # include <foobar.h>
}
Expand All @@ -759,8 +765,11 @@ void ClangIndexer::handleInclude(const CXCursor &cursor, CXCursorKind kind, cons
bool ClangIndexer::handleCursor(const CXCursor &cursor, CXCursorKind kind, const Location &location)
{
// error() << "Got a cursor" << cursor;
CursorInfo &info = mData->symbols[location];
if (!info.symbolLength || !RTags::isCursor(info.kind)) {
std::shared_ptr<CursorInfo> &info = mData->symbols[location];
if (!info)
info.reset(new CursorInfo);

if (!info->symbolLength || !RTags::isCursor(info->kind)) {
// if (mLogFile) {
// String out;
// Log(&out) << cursor << a;
Expand All @@ -769,9 +778,9 @@ bool ClangIndexer::handleCursor(const CXCursor &cursor, CXCursorKind kind, const
// }
CXStringScope name = clang_getCursorSpelling(cursor);
const char *cstr = name.data();
info.symbolLength = cstr ? strlen(cstr) : 0;
info.type = clang_getCursorType(cursor).kind;
if (!info.symbolLength) {
info->symbolLength = cstr ? strlen(cstr) : 0;
info->type = clang_getCursorType(cursor).kind;
if (!info->symbolLength) {
// this is for these constructs:
// typedef struct {
// int a;
Expand All @@ -781,23 +790,23 @@ bool ClangIndexer::handleCursor(const CXCursor &cursor, CXCursorKind kind, const

switch (kind) {
case CXCursor_ClassDecl:
info.symbolLength = 5;
info.symbolName = "class";
info->symbolLength = 5;
info->symbolName = "class";
break;
case CXCursor_UnionDecl:
info.symbolLength = 5;
info.symbolName = "union";
info->symbolLength = 5;
info->symbolName = "union";
break;
case CXCursor_StructDecl:
info.symbolLength = 6;
info.symbolName = "struct";
info->symbolLength = 6;
info->symbolName = "struct";
break;
default:
mData->symbols.remove(location);
return false;
}
} else {
info.symbolName = addNamePermutations(cursor, location);
info->symbolName = addNamePermutations(cursor, location);
}

CXSourceRange range = clang_getCursorExtent(cursor);
Expand All @@ -806,21 +815,21 @@ bool ClangIndexer::handleCursor(const CXCursor &cursor, CXCursorKind kind, const
unsigned startLine, startColumn, endLine, endColumn;
clang_getPresumedLocation(rangeStart, 0, &startLine, &startColumn);
clang_getPresumedLocation(rangeEnd, 0, &endLine, &endColumn);
info.startLine = startLine;
info.startColumn = startColumn;
info.endLine = endLine;
info.endColumn = endColumn;
info->startLine = startLine;
info->startColumn = startColumn;
info->endLine = endLine;
info->endColumn = endColumn;

if (kind == CXCursor_EnumConstantDecl) {
#if CINDEX_VERSION_MINOR > 1
info.enumValue = clang_getEnumConstantDeclValue(cursor);
info->enumValue = clang_getEnumConstantDeclValue(cursor);
#else
info.definition = 1;
info->definition = 1;
#endif
} else {
info.definition = clang_isCursorDefinition(cursor);
info->definition = clang_isCursorDefinition(cursor);
}
info.kind = kind;
info->kind = kind;
// apparently some function decls will give a different usr for
// their definition and their declaration. Using the canonical
// cursor's usr allows us to join them. Check JSClassRelease in
Expand All @@ -829,21 +838,23 @@ bool ClangIndexer::handleCursor(const CXCursor &cursor, CXCursorKind kind, const
if (!usr.isEmpty())
mData->usrMap[usr].insert(location);

switch (info.kind) {
switch (info->kind) {
case CXCursor_Constructor:
case CXCursor_Destructor: {
Location parentLocation = createLocation(clang_getCursorSemanticParent(cursor));
// consider doing this for only declaration/inline definition since
// declaration and definition should know of one another
if (parentLocation.isValid()) {
CursorInfo &parent = mData->symbols[parentLocation];
parent.references.insert(location);
info.references.insert(parentLocation);
std::shared_ptr<CursorInfo> &parent = mData->symbols[parentLocation];
if (!parent)
parent.reset(new CursorInfo);
parent->references.insert(location);
info->references.insert(parentLocation);
}
break; }
case CXCursor_CXXMethod: {
List<CursorInfo*> infos;
infos.append(&info);
infos.append(info.get());
addOverriddenCursors(cursor, location, infos);
break; }
default:
Expand Down
17 changes: 10 additions & 7 deletions src/Cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
#ifndef Cpp_h
#define Cpp_h

#include <Source.h>
#include <rct/String.h>
#include "CursorInfo.h"
#include "RTags.h"
#include "Source.h"
#include <rct/List.h>
#include <rct/Serializer.h>
#include "RTags.h"
#include <rct/String.h>

struct Cpp
{
Expand Down Expand Up @@ -67,15 +68,17 @@ template <> inline Deserializer &operator>>(Deserializer &s, Cpp::Diagnostic &d)

template <> inline Serializer &operator<<(Serializer &s, const Cpp &c)
{
s << c.preprocessed << c.time << c.flags << c.diagnostics << c.macroCursors
<< c.macroNames << c.visited << c.preprocessDuration;
s << c.preprocessed << c.time << c.flags << c.diagnostics;
CursorInfo::serialize(s, c.macroCursors);
s << c.macroNames << c.visited << c.preprocessDuration;
return s;
}

template <> inline Deserializer &operator>>(Deserializer &s, Cpp &c)
{
s >> c.preprocessed >> c.time >> c.flags >> c.diagnostics >> c.macroCursors
>> c.macroNames >> c.visited >> c.preprocessDuration;
s >> c.preprocessed >> c.time >> c.flags >> c.diagnostics;
CursorInfo::deserialize(s, c.macroCursors);
s >> c.macroNames >> c.visited >> c.preprocessDuration;
return s;
}

Expand Down
Loading

0 comments on commit d10fde4

Please sign in to comment.