Commit ae485ac5 authored by Marco Bubke's avatar Marco Bubke

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's avatarIvan Donchevskii <ivan.donchevskii@qt.io>
Reviewed-by: Nikolai Kosjar's avatarNikolai Kosjar <nikolai.kosjar@qt.io>
parent 02194a55
......@@ -30,6 +30,7 @@
#include <utf8string.h>
#include <cstring>
#include <ostream>
namespace ClangBackEnd {
......@@ -85,6 +86,11 @@ public:
return cxString.data == nullptr;
}
bool hasContent() const
{
return !isNull() && std::strlen(cString()) > 0;
}
friend bool operator==(const ClangString &first, const ClangString &second)
{
return std::strcmp(first.cString(), second.cString()) == 0;
......@@ -118,6 +124,13 @@ public:
return second == first;
}
friend std::ostream &operator<<(std::ostream &out, const ClangString &string)
{
out << string.cString();
return out;
}
private:
CXString cxString;
};
......
......@@ -198,12 +198,12 @@ bool Cursor::isUnexposed() const
return clang_isUnexposed(kind());
}
Utf8String Cursor::unifiedSymbolResolution() const
ClangString Cursor::unifiedSymbolResolution() const
{
return ClangString(clang_getCursorUSR(cxCursor));
}
Utf8String Cursor::mangling() const
ClangString Cursor::mangling() const
{
return ClangString(clang_Cursor_getMangling(cxCursor));
}
......@@ -213,17 +213,17 @@ ClangString Cursor::spelling() const
return ClangString(clang_getCursorSpelling(cxCursor));
}
Utf8String Cursor::displayName() const
ClangString Cursor::displayName() const
{
return ClangString(clang_getCursorDisplayName(cxCursor));
}
Utf8String Cursor::briefComment() const
ClangString Cursor::briefComment() const
{
return ClangString(clang_Cursor_getBriefCommentText(cxCursor));
}
Utf8String Cursor::rawComment() const
ClangString Cursor::rawComment() const
{
return ClangString(clang_Cursor_getRawCommentText(cxCursor));
}
......@@ -418,7 +418,7 @@ void PrintTo(const Cursor &cursor, ::std::ostream*os)
auto identifier = cursor.displayName();
if (identifier.hasContent()) {
*os << "\""
<< identifier.constData()
<< identifier
<< "\": ";
}
......
......@@ -71,12 +71,12 @@ public:
bool hasFinalClassAttribute() const;
bool isUnexposed() const;
Utf8String unifiedSymbolResolution() const;
Utf8String mangling() const;
ClangString unifiedSymbolResolution() const;
ClangString mangling() const;
ClangString spelling() const;
Utf8String displayName() const;
Utf8String briefComment() const;
Utf8String rawComment() const;
ClangString displayName() const;
ClangString briefComment() const;
ClangString rawComment() const;
int argumentCount() const;
Type type() const;
......
......@@ -167,4 +167,31 @@ TEST(ClangString, EqualCStringAndClangStringPointer)
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);
}
}
......@@ -57,6 +57,7 @@ using testing::AllOf;
using testing::Not;
using testing::IsEmpty;
using testing::StrEq;
using testing::Eq;
namespace {
......@@ -137,14 +138,14 @@ TEST_F(Cursor, UnifiedSymbolResolution)
{
::Cursor cursor;
ASSERT_TRUE(cursor.unifiedSymbolResolution().isEmpty());
ASSERT_FALSE(cursor.unifiedSymbolResolution().hasContent());
}
TEST_F(Cursor, GetCursorAtLocation)
{
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)
......@@ -166,15 +167,13 @@ TEST_F(Cursor, Mangling)
{
auto cursor = translationUnit.cursorAt(3, 6);
ASSERT_THAT(cursor.mangling().isEmpty(), false);
ASSERT_TRUE(cursor.mangling().hasContent());
}
TEST_F(Cursor, Spelling)
{
auto cursor = translationUnit.cursorAt(3, 6);
ASSERT_THAT(cursor.spelling().cString(), StrEq("function"));
}
......@@ -182,24 +181,21 @@ TEST_F(Cursor, DisplayName)
{
auto cursor = translationUnit.cursorAt(3, 6);
ASSERT_THAT(cursor.displayName(), Utf8StringLiteral("function(int)"));
ASSERT_THAT(cursor.displayName(), Eq("function(int)"));
}
TEST_F(Cursor, BriefComment)
{
auto cursor = translationUnit.cursorAt(Utf8StringLiteral(TESTDATA_DIR"/cursor.h"), 10, 7);
ASSERT_THAT(cursor.briefComment(), Utf8StringLiteral("A brief comment"));
ASSERT_THAT(cursor.briefComment(), Eq("A brief comment"));
}
TEST_F(Cursor, DISABLED_ON_WINDOWS(RawComment))
{
auto cursor = translationUnit.cursorAt(Utf8StringLiteral(TESTDATA_DIR"/cursor.h"), 10, 7);
ASSERT_THAT(cursor.rawComment(), Utf8StringLiteral("/**\n * A brief comment\n */"));
ASSERT_THAT(cursor.rawComment(), Eq("/**\n * A brief comment\n */"));
}
TEST_F(Cursor, CommentRange)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment