diff --git a/src/tools/clangbackend/ipcsource/clangstring.h b/src/tools/clangbackend/ipcsource/clangstring.h index a4c0f0a5838..09049153abb 100644 --- a/src/tools/clangbackend/ipcsource/clangstring.h +++ b/src/tools/clangbackend/ipcsource/clangstring.h @@ -30,6 +30,7 @@ #include #include +#include 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; }; diff --git a/src/tools/clangbackend/ipcsource/cursor.cpp b/src/tools/clangbackend/ipcsource/cursor.cpp index 6367955fe4c..c9f8dd5ab81 100644 --- a/src/tools/clangbackend/ipcsource/cursor.cpp +++ b/src/tools/clangbackend/ipcsource/cursor.cpp @@ -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 << "\": "; } diff --git a/src/tools/clangbackend/ipcsource/cursor.h b/src/tools/clangbackend/ipcsource/cursor.h index e1420f596da..234ad900f9e 100644 --- a/src/tools/clangbackend/ipcsource/cursor.h +++ b/src/tools/clangbackend/ipcsource/cursor.h @@ -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; diff --git a/tests/unit/unittest/clangstring-test.cpp b/tests/unit/unittest/clangstring-test.cpp index 4db02510a6c..bea70a7ab06 100644 --- a/tests/unit/unittest/clangstring-test.cpp +++ b/tests/unit/unittest/clangstring-test.cpp @@ -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); +} + } diff --git a/tests/unit/unittest/cursor-test.cpp b/tests/unit/unittest/cursor-test.cpp index 78913d7c20d..2ddf2022601 100644 --- a/tests/unit/unittest/cursor-test.cpp +++ b/tests/unit/unittest/cursor-test.cpp @@ -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)