kiloalphaindia created this revision.
Herald added a subscriber: arphaman.
Herald added a project: All.
kiloalphaindia requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D135555

Files:
  clang/tools/libclang/CIndex.cpp
  clang/unittests/libclang/LibclangTest.cpp
  clang/unittests/libclang/TestUtils.h

Index: clang/unittests/libclang/TestUtils.h
===================================================================
--- clang/unittests/libclang/TestUtils.h
+++ clang/unittests/libclang/TestUtils.h
@@ -82,6 +82,12 @@
         &TraverseStateless<std::reference_wrapper<const F>>,
         &FunctorRef);
   }
+  static std::string from_CXString(CXString cx_string) {
+    std::string string{clang_getCString(cx_string)};
+    clang_disposeString(cx_string);
+    return string;
+  };
+
 private:
   template<typename TState>
   static CXChildVisitResult TraverseStateless(CXCursor cx, CXCursor parent,
@@ -91,4 +97,4 @@
   }
 };
 
-#endif // LLVM_CLANG_TEST_TESTUTILS_H
\ No newline at end of file
+#endif // LLVM_CLANG_TEST_TESTUTILS_H
Index: clang/unittests/libclang/LibclangTest.cpp
===================================================================
--- clang/unittests/libclang/LibclangTest.cpp
+++ clang/unittests/libclang/LibclangTest.cpp
@@ -863,18 +863,10 @@
            clang_isRestrictQualifiedType(type);
   };
 
-  auto from_CXString = [](CXString cx_string) -> std::string {
-    std::string string{clang_getCString(cx_string)};
-
-    clang_disposeString(cx_string);
-
-    return string;
-  };
-
   ClangTU = clang_parseTranslationUnit(Index, Header.c_str(), nullptr, 0,
                                        nullptr, 0, TUFlags);
 
-  Traverse([&is_qualified, &from_CXString](CXCursor cursor, CXCursor) {
+  Traverse([&is_qualified](CXCursor cursor, CXCursor) {
     if (clang_getCursorKind(cursor) == CXCursor_FunctionDecl) {
       CXType arg_type = clang_getArgType(clang_getCursorType(cursor), 0);
       EXPECT_TRUE(is_qualified(arg_type))
@@ -901,19 +893,11 @@
            (type.kind == CXType_RValueReference);
   };
 
-  auto from_CXString = [](CXString cx_string) -> std::string {
-    std::string string{clang_getCString(cx_string)};
-
-    clang_disposeString(cx_string);
-
-    return string;
-  };
-
   const char *Args[] = {"-xc++"};
   ClangTU = clang_parseTranslationUnit(Index, Header.c_str(), Args, 1, nullptr,
                                        0, TUFlags);
 
-  Traverse([&is_ref_qualified, &from_CXString](CXCursor cursor, CXCursor) {
+  Traverse([&is_ref_qualified](CXCursor cursor, CXCursor) {
     if (clang_getCursorKind(cursor) == CXCursor_FunctionDecl) {
       CXType arg_type = clang_getArgType(clang_getCursorType(cursor), 0);
       EXPECT_TRUE(is_ref_qualified(arg_type))
@@ -931,6 +915,37 @@
   });
 }
 
+TEST_F(LibclangParseTest, VisitUsingTypeLoc) {
+  const char testSource[] = R"cpp(
+namespace ns1 {
+class Class1
+{
+    void fun();
+};
+}
+
+using ns1::Class1;
+
+void Class1::fun() {}
+)cpp";
+  std::string fileName = "main.cpp";
+  WriteFile(fileName, testSource);
+  const char *Args[] = {"-xc++"};
+  ClangTU = clang_parseTranslationUnit(Index, fileName.c_str(), Args, 1,
+                                       nullptr, 0, TUFlags);
+
+  llvm::Optional<CXCursor> typeRefCsr;
+  Traverse([&](CXCursor cursor, CXCursor parent) -> CXChildVisitResult {
+    if (cursor.kind == CXCursor_TypeRef) {
+      typeRefCsr.emplace(cursor);
+    }
+    return CXChildVisit_Recurse;
+  });
+  ASSERT_TRUE(typeRefCsr.has_value());
+  EXPECT_EQ(from_CXString(clang_getCursorSpelling(*typeRefCsr)),
+            "class ns1::Class1");
+}
+
 class LibclangRewriteTest : public LibclangParseTest {
 public:
   CXRewriter Rew = nullptr;
Index: clang/tools/libclang/CIndex.cpp
===================================================================
--- clang/tools/libclang/CIndex.cpp
+++ clang/tools/libclang/CIndex.cpp
@@ -1747,7 +1747,13 @@
   return Visit(TL.getPointeeLoc());
 }
 
-bool CursorVisitor::VisitUsingTypeLoc(UsingTypeLoc TL) { return false; }
+bool CursorVisitor::VisitUsingTypeLoc(UsingTypeLoc TL) {
+  auto *underlyingDecl = TL.getUnderlyingType()->getAsTagDecl();
+  if (underlyingDecl) {
+    return Visit(MakeCursorTypeRef(underlyingDecl, TL.getNameLoc(), TU));
+  }
+  return false;
+}
 
 bool CursorVisitor::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
   return Visit(TL.getModifiedLoc());
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to