[clang] e7d9917 - Expose QualType::getNonReferenceType in libclang

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 2 06:55:10 PDT 2022


Author: Luca Di Sera
Date: 2022-09-02T09:54:10-04:00
New Revision: e7d9917a60dca60da05d0114de9858ed7acb015c

URL: https://github.com/llvm/llvm-project/commit/e7d9917a60dca60da05d0114de9858ed7acb015c
DIFF: https://github.com/llvm/llvm-project/commit/e7d9917a60dca60da05d0114de9858ed7acb015c.diff

LOG: Expose QualType::getNonReferenceType in libclang

The method is now wrapped by clang_getNonReferenceType.

A declaration for clang_getNonReferenceType was added to clang-c/Index.h
to expose it to user of the library.

An implementation for clang_getNonReferenceType was introduced in
CXType.cpp, wrapping the equivalent method of the underlying QualType of
a CXType.

An export symbol for the new function was added to libclang.map under
the LLVM_16 version entry.

A test was added to LibclangTest.cpp that tests the removal of
ref-qualifiers for some CXTypes.

The release-notes for the clang project was updated to include a
notification of the new addition under the "libclang" section.

Differential Revision: https://reviews.llvm.org/D133195

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/include/clang-c/Index.h
    clang/tools/libclang/CXType.cpp
    clang/tools/libclang/libclang.map
    clang/unittests/libclang/LibclangTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 2a92f65cc04fc..41371fe0e04f2 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -289,6 +289,8 @@ libclang
 
 - Introduced the new function `clang_getUnqualifiedType`, which mimics
   the behavior of `QualType::getUnqualifiedType` for `CXType`.
+- Introduced the new function `clang_getNonReferenceType`, which mimics
+  the behavior of `QualType::getNonReferenceType` for `CXType`.
 
 Static Analyzer
 ---------------

diff  --git a/clang/include/clang-c/Index.h b/clang/include/clang-c/Index.h
index 41efc57823d3c..c0dbad869114b 100644
--- a/clang/include/clang-c/Index.h
+++ b/clang/include/clang-c/Index.h
@@ -3797,6 +3797,17 @@ CINDEX_LINKAGE CXType clang_getPointeeType(CXType T);
  */
 CINDEX_LINKAGE CXType clang_getUnqualifiedType(CXType CT);
 
+/**
+ * For reference types (e.g., "const int&"), returns the type that the
+ * reference refers to (e.g "const int").
+ *
+ * Otherwise, returns the type itself.
+ *
+ * A type that has kind \c CXType_LValueReference or
+ * \c CXType_RValueReference is a reference type.
+ */
+CINDEX_LINKAGE CXType clang_getNonReferenceType(CXType CT);
+
 /**
  * Return the cursor for the declaration of the given type.
  */

diff  --git a/clang/tools/libclang/CXType.cpp b/clang/tools/libclang/CXType.cpp
index e50a471b94098..a62163c64d097 100644
--- a/clang/tools/libclang/CXType.cpp
+++ b/clang/tools/libclang/CXType.cpp
@@ -488,6 +488,10 @@ CXType clang_getUnqualifiedType(CXType CT) {
   return MakeCXType(GetQualType(CT).getUnqualifiedType(), GetTU(CT));
 }
 
+CXType clang_getNonReferenceType(CXType CT) {
+  return MakeCXType(GetQualType(CT).getNonReferenceType(), GetTU(CT));
+}
+
 CXCursor clang_getTypeDeclaration(CXType CT) {
   if (CT.kind == CXType_Invalid)
     return cxcursor::MakeCXCursorInvalid(CXCursor_NoDeclFound);

diff  --git a/clang/tools/libclang/libclang.map b/clang/tools/libclang/libclang.map
index 9ed81722fa341..911292a81c27b 100644
--- a/clang/tools/libclang/libclang.map
+++ b/clang/tools/libclang/libclang.map
@@ -408,6 +408,7 @@ LLVM_13 {
 LLVM_16 {
   global:
     clang_getUnqualifiedType;
+    clang_getNonReferenceType;
 };
 
 # Example of how to add a new symbol version entry.  If you do add a new symbol

diff  --git a/clang/unittests/libclang/LibclangTest.cpp b/clang/unittests/libclang/LibclangTest.cpp
index e6084859ade71..d020fa9af61c5 100644
--- a/clang/unittests/libclang/LibclangTest.cpp
+++ b/clang/unittests/libclang/LibclangTest.cpp
@@ -891,6 +891,46 @@ TEST_F(LibclangParseTest, clang_getUnqualifiedTypeRemovesQualifiers) {
   });
 }
 
+TEST_F(LibclangParseTest, clang_getNonReferenceTypeRemovesRefQualifiers) {
+  std::string Header = "header.h";
+  WriteFile(Header, "void foo1(int&);\n"
+                    "void foo2(int&&);\n");
+
+  auto is_ref_qualified = [](CXType type) -> bool {
+    return (type.kind == CXType_LValueReference) ||
+           (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) {
+    if (clang_getCursorKind(cursor) == CXCursor_FunctionDecl) {
+      CXType arg_type = clang_getArgType(clang_getCursorType(cursor), 0);
+      EXPECT_TRUE(is_ref_qualified(arg_type))
+          << "Input data '" << from_CXString(clang_getCursorSpelling(cursor))
+          << "' first argument does not have a ref-qualified type.";
+
+      CXType non_reference_arg_type = clang_getNonReferenceType(arg_type);
+      EXPECT_FALSE(is_ref_qualified(non_reference_arg_type))
+          << "The type '" << from_CXString(clang_getTypeSpelling(arg_type))
+          << "' ref-qualifier was not removed after a call to "
+             "clang_getNonReferenceType.";
+    }
+
+    return CXChildVisit_Continue;
+  });
+}
+
 class LibclangRewriteTest : public LibclangParseTest {
 public:
   CXRewriter Rew = nullptr;


        


More information about the cfe-commits mailing list