Clang: Return ClangString instead of Utf8String

Utf8String is allocating memory but for many compares it is not needed.
In an inner loop this can be expensive.

Change-Id: I6320823ab7e086008447eea255e52859a7faaad7
Reviewed-by: Ivan Donchevskii <ivan.donchevskii@qt.io>
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
This commit is contained in:
Marco Bubke
2017-06-13 16:04:45 +02:00
parent 02194a5542
commit ae485ac551
5 changed files with 58 additions and 22 deletions

View File

@@ -30,6 +30,7 @@
#include <utf8string.h> #include <utf8string.h>
#include <cstring> #include <cstring>
#include <ostream>
namespace ClangBackEnd { namespace ClangBackEnd {
@@ -85,6 +86,11 @@ public:
return cxString.data == nullptr; return cxString.data == nullptr;
} }
bool hasContent() const
{
return !isNull() && std::strlen(cString()) > 0;
}
friend bool operator==(const ClangString &first, const ClangString &second) friend bool operator==(const ClangString &first, const ClangString &second)
{ {
return std::strcmp(first.cString(), second.cString()) == 0; return std::strcmp(first.cString(), second.cString()) == 0;
@@ -118,6 +124,13 @@ public:
return second == first; return second == first;
} }
friend std::ostream &operator<<(std::ostream &out, const ClangString &string)
{
out << string.cString();
return out;
}
private: private:
CXString cxString; CXString cxString;
}; };

View File

@@ -198,12 +198,12 @@ bool Cursor::isUnexposed() const
return clang_isUnexposed(kind()); return clang_isUnexposed(kind());
} }
Utf8String Cursor::unifiedSymbolResolution() const ClangString Cursor::unifiedSymbolResolution() const
{ {
return ClangString(clang_getCursorUSR(cxCursor)); return ClangString(clang_getCursorUSR(cxCursor));
} }
Utf8String Cursor::mangling() const ClangString Cursor::mangling() const
{ {
return ClangString(clang_Cursor_getMangling(cxCursor)); return ClangString(clang_Cursor_getMangling(cxCursor));
} }
@@ -213,17 +213,17 @@ ClangString Cursor::spelling() const
return ClangString(clang_getCursorSpelling(cxCursor)); return ClangString(clang_getCursorSpelling(cxCursor));
} }
Utf8String Cursor::displayName() const ClangString Cursor::displayName() const
{ {
return ClangString(clang_getCursorDisplayName(cxCursor)); return ClangString(clang_getCursorDisplayName(cxCursor));
} }
Utf8String Cursor::briefComment() const ClangString Cursor::briefComment() const
{ {
return ClangString(clang_Cursor_getBriefCommentText(cxCursor)); return ClangString(clang_Cursor_getBriefCommentText(cxCursor));
} }
Utf8String Cursor::rawComment() const ClangString Cursor::rawComment() const
{ {
return ClangString(clang_Cursor_getRawCommentText(cxCursor)); return ClangString(clang_Cursor_getRawCommentText(cxCursor));
} }
@@ -418,7 +418,7 @@ void PrintTo(const Cursor &cursor, ::std::ostream*os)
auto identifier = cursor.displayName(); auto identifier = cursor.displayName();
if (identifier.hasContent()) { if (identifier.hasContent()) {
*os << "\"" *os << "\""
<< identifier.constData() << identifier
<< "\": "; << "\": ";
} }

View File

@@ -71,12 +71,12 @@ public:
bool hasFinalClassAttribute() const; bool hasFinalClassAttribute() const;
bool isUnexposed() const; bool isUnexposed() const;
Utf8String unifiedSymbolResolution() const; ClangString unifiedSymbolResolution() const;
Utf8String mangling() const; ClangString mangling() const;
ClangString spelling() const; ClangString spelling() const;
Utf8String displayName() const; ClangString displayName() const;
Utf8String briefComment() const; ClangString briefComment() const;
Utf8String rawComment() const; ClangString rawComment() const;
int argumentCount() const; int argumentCount() const;
Type type() const; Type type() const;

View File

@@ -167,4 +167,31 @@ TEST(ClangString, EqualCStringAndClangStringPointer)
ASSERT_TRUE(textIsEqual); ASSERT_TRUE(textIsEqual);
} }
TEST(ClangString, NullStringHasNoContent)
{
ClangString text(CXString{nullptr, 0});
bool hasContent = text.hasContent();
ASSERT_FALSE(hasContent);
}
TEST(ClangString, EmptyStringHasNoContent)
{
ClangString text(CXString{"", 0});
bool hasContent = text.hasContent();
ASSERT_FALSE(hasContent);
}
TEST(ClangString, StringHasNoContent)
{
ClangString text(CXString{"text", 0});
bool hasContent = text.hasContent();
ASSERT_TRUE(hasContent);
}
} }

View File

@@ -57,6 +57,7 @@ using testing::AllOf;
using testing::Not; using testing::Not;
using testing::IsEmpty; using testing::IsEmpty;
using testing::StrEq; using testing::StrEq;
using testing::Eq;
namespace { namespace {
@@ -137,14 +138,14 @@ TEST_F(Cursor, UnifiedSymbolResolution)
{ {
::Cursor cursor; ::Cursor cursor;
ASSERT_TRUE(cursor.unifiedSymbolResolution().isEmpty()); ASSERT_FALSE(cursor.unifiedSymbolResolution().hasContent());
} }
TEST_F(Cursor, GetCursorAtLocation) TEST_F(Cursor, GetCursorAtLocation)
{ {
auto cursor = translationUnit.cursorAt(3, 6); auto cursor = translationUnit.cursorAt(3, 6);
ASSERT_THAT(cursor.unifiedSymbolResolution(), Utf8StringLiteral("c:@F@function#I#")); ASSERT_THAT(cursor.unifiedSymbolResolution(), Eq("c:@F@function#I#"));
} }
TEST_F(Cursor, GetCursoSourceLocation) TEST_F(Cursor, GetCursoSourceLocation)
@@ -166,15 +167,13 @@ TEST_F(Cursor, Mangling)
{ {
auto cursor = translationUnit.cursorAt(3, 6); auto cursor = translationUnit.cursorAt(3, 6);
ASSERT_TRUE(cursor.mangling().hasContent());
ASSERT_THAT(cursor.mangling().isEmpty(), false);
} }
TEST_F(Cursor, Spelling) TEST_F(Cursor, Spelling)
{ {
auto cursor = translationUnit.cursorAt(3, 6); auto cursor = translationUnit.cursorAt(3, 6);
ASSERT_THAT(cursor.spelling().cString(), StrEq("function")); ASSERT_THAT(cursor.spelling().cString(), StrEq("function"));
} }
@@ -182,24 +181,21 @@ TEST_F(Cursor, DisplayName)
{ {
auto cursor = translationUnit.cursorAt(3, 6); auto cursor = translationUnit.cursorAt(3, 6);
ASSERT_THAT(cursor.displayName(), Eq("function(int)"));
ASSERT_THAT(cursor.displayName(), Utf8StringLiteral("function(int)"));
} }
TEST_F(Cursor, BriefComment) TEST_F(Cursor, BriefComment)
{ {
auto cursor = translationUnit.cursorAt(Utf8StringLiteral(TESTDATA_DIR"/cursor.h"), 10, 7); auto cursor = translationUnit.cursorAt(Utf8StringLiteral(TESTDATA_DIR"/cursor.h"), 10, 7);
ASSERT_THAT(cursor.briefComment(), Eq("A brief comment"));
ASSERT_THAT(cursor.briefComment(), Utf8StringLiteral("A brief comment"));
} }
TEST_F(Cursor, DISABLED_ON_WINDOWS(RawComment)) TEST_F(Cursor, DISABLED_ON_WINDOWS(RawComment))
{ {
auto cursor = translationUnit.cursorAt(Utf8StringLiteral(TESTDATA_DIR"/cursor.h"), 10, 7); auto cursor = translationUnit.cursorAt(Utf8StringLiteral(TESTDATA_DIR"/cursor.h"), 10, 7);
ASSERT_THAT(cursor.rawComment(), Eq("/**\n * A brief comment\n */"));
ASSERT_THAT(cursor.rawComment(), Utf8StringLiteral("/**\n * A brief comment\n */"));
} }
TEST_F(Cursor, CommentRange) TEST_F(Cursor, CommentRange)