[clang] f62ad15 - NFC: Add a simple test for introspection call formatting

Stephen Kelly via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 15 15:46:13 PDT 2021


Author: Stephen Kelly
Date: 2021-04-15T23:45:54+01:00
New Revision: f62ad15cd7df0ca7681e0dbb894ee1c1d2465c51

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

LOG: NFC: Add a simple test for introspection call formatting

Added: 
    

Modified: 
    clang/unittests/Introspection/IntrospectionTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/unittests/Introspection/IntrospectionTest.cpp b/clang/unittests/Introspection/IntrospectionTest.cpp
index be58945c9a8d..ad21748f11f8 100644
--- a/clang/unittests/Introspection/IntrospectionTest.cpp
+++ b/clang/unittests/Introspection/IntrospectionTest.cpp
@@ -45,6 +45,43 @@ FormatExpected(const MapType &Accessors) {
 
 #define STRING_LOCATION_PAIR(INSTANCE, LOC) Pair(#LOC, INSTANCE->LOC)
 
+/**
+  A test formatter for a hypothetical language which needs
+  neither casts nor '->'.
+*/
+class LocationCallFormatterSimple {
+public:
+  static void print(const LocationCall &Call, llvm::raw_ostream &OS) {
+    if (Call.isCast()) {
+      if (const LocationCall *On = Call.on())
+        print(*On, OS);
+      return;
+    }
+    if (const LocationCall *On = Call.on()) {
+      print(*On, OS);
+      OS << '.';
+    }
+    OS << Call.name();
+    if (Call.args().empty()) {
+      OS << "()";
+      return;
+    }
+    OS << '(' << Call.args().front();
+    for (const std::string &Arg : Call.args().drop_front()) {
+      OS << ", " << Arg;
+    }
+    OS << ')';
+  }
+
+  static std::string format(const LocationCall &Call) {
+    std::string Result;
+    llvm::raw_string_ostream OS(Result);
+    print(Call, OS);
+    OS.flush();
+    return Result;
+  }
+};
+
 TEST(Introspection, SourceLocations_CallContainer) {
   SourceLocationMap slm;
   SharedLocationCall Prefix;
@@ -70,6 +107,24 @@ TEST(Introspection, SourceLocations_CallChainFormatting) {
             "getTypeLoc().getSourceRange()");
 }
 
+TEST(Introspection, SourceLocations_Formatter) {
+  SharedLocationCall Prefix;
+  auto chainedCall = llvm::makeIntrusiveRefCnt<LocationCall>(
+      llvm::makeIntrusiveRefCnt<LocationCall>(
+          llvm::makeIntrusiveRefCnt<LocationCall>(
+              llvm::makeIntrusiveRefCnt<LocationCall>(
+                  Prefix, "getTypeSourceInfo", LocationCall::ReturnsPointer),
+              "getTypeLoc"),
+          "getAs<clang::TypeSpecTypeLoc>", LocationCall::IsCast),
+      "getNameLoc");
+
+  EXPECT_EQ("getTypeSourceInfo()->getTypeLoc().getAs<clang::TypeSpecTypeLoc>()."
+            "getNameLoc()",
+            LocationCallFormatterCpp::format(*chainedCall));
+  EXPECT_EQ("getTypeSourceInfo().getTypeLoc().getNameLoc()",
+            LocationCallFormatterSimple::format(*chainedCall));
+}
+
 TEST(Introspection, SourceLocations_Stmt) {
   if (!NodeIntrospection::hasIntrospectionSupport())
     return;


        


More information about the cfe-commits mailing list