[clang] 8dcb629 - [clang][ExtractAPI] Fix naming of typedef'd anonymous enums

Daniel Grumberg via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 16 03:01:10 PST 2022


Author: Daniel Grumberg
Date: 2022-12-16T11:01:03Z
New Revision: 8dcb629aa4ccfdd18fb700cabb45fd74fcd291c8

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

LOG: [clang][ExtractAPI] Fix naming of typedef'd anonymous enums

Anonymous enums that are typedef'd should take on the name of the typedef.

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

Added: 
    

Modified: 
    clang/lib/ExtractAPI/ExtractAPIVisitor.cpp
    clang/test/ExtractAPI/typedef_anonymous_record.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/ExtractAPI/ExtractAPIVisitor.cpp b/clang/lib/ExtractAPI/ExtractAPIVisitor.cpp
index 38d95aaa6d5eb..24260cf89383d 100644
--- a/clang/lib/ExtractAPI/ExtractAPIVisitor.cpp
+++ b/clang/lib/ExtractAPI/ExtractAPIVisitor.cpp
@@ -29,6 +29,7 @@
 #include "clang/ExtractAPI/DeclarationFragments.h"
 #include "clang/Frontend/ASTConsumers.h"
 #include "clang/Frontend/FrontendOptions.h"
+#include "llvm/Support/raw_ostream.h"
 
 using namespace clang;
 using namespace extractapi;
@@ -167,11 +168,16 @@ bool ExtractAPIVisitor::VisitEnumDecl(const EnumDecl *Decl) {
   if (!LocationChecker(Decl->getLocation()))
     return true;
 
+  SmallString<128> QualifiedNameBuffer;
   // Collect symbol information.
-  std::string NameString = Decl->getQualifiedNameAsString();
-  StringRef Name(NameString);
+  StringRef Name = Decl->getName();
   if (Name.empty())
     Name = getTypedefName(Decl);
+  if (Name.empty()) {
+    llvm::raw_svector_ostream OS(QualifiedNameBuffer);
+    Decl->printQualifiedName(OS);
+    Name = QualifiedNameBuffer.str();
+  }
 
   StringRef USR = API.recordUSR(Decl);
   PresumedLoc Loc =

diff  --git a/clang/test/ExtractAPI/typedef_anonymous_record.c b/clang/test/ExtractAPI/typedef_anonymous_record.c
index e3e34769321f1..1bd93b92ede81 100644
--- a/clang/test/ExtractAPI/typedef_anonymous_record.c
+++ b/clang/test/ExtractAPI/typedef_anonymous_record.c
@@ -2,21 +2,22 @@
 // RUN: split-file %s %t
 // RUN: sed -e "s at INPUT_DIR@%{/t:regex_replacement}@g" \
 // RUN: %t/reference.output.json.in >> %t/reference.output.json
-// RUN: %clang -extract-api --product-name=TypedefChain -target arm64-apple-macosx \
-// RUN: -x objective-c-header %t/input.h -o %t/output.json | FileCheck -allow-empty %s
+// RUN: %clang_cc1 -extract-api --product-name=TypedefChain -triple arm64-apple-macosx \
+// RUN:   -x c-header %t/input.h -o %t/output.json -verify
 
 // Generator version is not consistent across test runs, normalize it.
 // RUN: sed -e "s@\"generator\": \".*\"@\"generator\": \"?\"@g" \
 // RUN: %t/output.json >> %t/output-normalized.json
 // RUN: 
diff  %t/reference.output.json %t/output-normalized.json
 
-// CHECK-NOT: error:
-// CHECK-NOT: warning:
-
 //--- input.h
 typedef struct { } MyStruct;
 typedef MyStruct MyStructStruct;
 typedef MyStructStruct MyStructStructStruct;
+typedef enum { Case } MyEnum;
+typedef MyEnum MyEnumEnum;
+typedef MyEnumEnum MyEnumEnumEnum;
+// expected-no-diagnostics
 
 //--- reference.output.json.in
 {
@@ -43,8 +44,110 @@ typedef MyStructStruct MyStructStructStruct;
       "vendor": "apple"
     }
   },
-  "relationships": [],
+  "relationships": [
+    {
+      "kind": "memberOf",
+      "source": "c:@EA at MyEnum@Case",
+      "target": "c:@EA at MyEnum",
+      "targetFallback": "MyEnum"
+    }
+  ],
   "symbols": [
+    {
+      "accessLevel": "public",
+      "declarationFragments": [
+        {
+          "kind": "keyword",
+          "spelling": "typedef"
+        },
+        {
+          "kind": "text",
+          "spelling": " "
+        },
+        {
+          "kind": "keyword",
+          "spelling": "enum"
+        },
+        {
+          "kind": "text",
+          "spelling": " "
+        },
+        {
+          "kind": "identifier",
+          "spelling": "MyEnum"
+        }
+      ],
+      "identifier": {
+        "interfaceLanguage": "c",
+        "precise": "c:@EA at MyEnum"
+      },
+      "kind": {
+        "displayName": "Enumeration",
+        "identifier": "c.enum"
+      },
+      "location": {
+        "position": {
+          "character": 9,
+          "line": 4
+        },
+        "uri": "file://INPUT_DIR/input.h"
+      },
+      "names": {
+        "navigator": [
+          {
+            "kind": "identifier",
+            "spelling": "MyEnum"
+          }
+        ],
+        "title": "MyEnum"
+      },
+      "pathComponents": [
+        "MyEnum"
+      ]
+    },
+    {
+      "accessLevel": "public",
+      "declarationFragments": [
+        {
+          "kind": "identifier",
+          "spelling": "Case"
+        }
+      ],
+      "identifier": {
+        "interfaceLanguage": "c",
+        "precise": "c:@EA at MyEnum@Case"
+      },
+      "kind": {
+        "displayName": "Enumeration Case",
+        "identifier": "c.enum.case"
+      },
+      "location": {
+        "position": {
+          "character": 16,
+          "line": 4
+        },
+        "uri": "file://INPUT_DIR/input.h"
+      },
+      "names": {
+        "navigator": [
+          {
+            "kind": "identifier",
+            "spelling": "Case"
+          }
+        ],
+        "subHeading": [
+          {
+            "kind": "identifier",
+            "spelling": "Case"
+          }
+        ],
+        "title": "Case"
+      },
+      "pathComponents": [
+        "MyEnum",
+        "Case"
+      ]
+    },
     {
       "accessLevel": "public",
       "declarationFragments": [
@@ -70,12 +173,12 @@ typedef MyStructStruct MyStructStructStruct;
         }
       ],
       "identifier": {
-        "interfaceLanguage": "objective-c",
+        "interfaceLanguage": "c",
         "precise": "c:@SA at MyStruct"
       },
       "kind": {
         "displayName": "Structure",
-        "identifier": "objective-c.struct"
+        "identifier": "c.struct"
       },
       "location": {
         "position": {
@@ -123,12 +226,12 @@ typedef MyStructStruct MyStructStructStruct;
         }
       ],
       "identifier": {
-        "interfaceLanguage": "objective-c",
+        "interfaceLanguage": "c",
         "precise": "c:input.h at T@MyStructStruct"
       },
       "kind": {
         "displayName": "Type Alias",
-        "identifier": "objective-c.typealias"
+        "identifier": "c.typealias"
       },
       "location": {
         "position": {
@@ -183,12 +286,12 @@ typedef MyStructStruct MyStructStructStruct;
         }
       ],
       "identifier": {
-        "interfaceLanguage": "objective-c",
+        "interfaceLanguage": "c",
         "precise": "c:input.h at T@MyStructStructStruct"
       },
       "kind": {
         "displayName": "Type Alias",
-        "identifier": "objective-c.typealias"
+        "identifier": "c.typealias"
       },
       "location": {
         "position": {
@@ -216,6 +319,126 @@ typedef MyStructStruct MyStructStructStruct;
         "MyStructStructStruct"
       ],
       "type": "c:input.h at T@MyStructStruct"
+    },
+    {
+      "accessLevel": "public",
+      "declarationFragments": [
+        {
+          "kind": "keyword",
+          "spelling": "typedef"
+        },
+        {
+          "kind": "text",
+          "spelling": " "
+        },
+        {
+          "kind": "typeIdentifier",
+          "preciseIdentifier": "c:@EA at MyEnum",
+          "spelling": "MyEnum"
+        },
+        {
+          "kind": "text",
+          "spelling": " "
+        },
+        {
+          "kind": "identifier",
+          "spelling": "MyEnumEnum"
+        }
+      ],
+      "identifier": {
+        "interfaceLanguage": "c",
+        "precise": "c:input.h at T@MyEnumEnum"
+      },
+      "kind": {
+        "displayName": "Type Alias",
+        "identifier": "c.typealias"
+      },
+      "location": {
+        "position": {
+          "character": 16,
+          "line": 5
+        },
+        "uri": "file://INPUT_DIR/input.h"
+      },
+      "names": {
+        "navigator": [
+          {
+            "kind": "identifier",
+            "spelling": "MyEnumEnum"
+          }
+        ],
+        "subHeading": [
+          {
+            "kind": "identifier",
+            "spelling": "MyEnumEnum"
+          }
+        ],
+        "title": "MyEnumEnum"
+      },
+      "pathComponents": [
+        "MyEnumEnum"
+      ],
+      "type": "c:@EA at MyEnum"
+    },
+    {
+      "accessLevel": "public",
+      "declarationFragments": [
+        {
+          "kind": "keyword",
+          "spelling": "typedef"
+        },
+        {
+          "kind": "text",
+          "spelling": " "
+        },
+        {
+          "kind": "typeIdentifier",
+          "preciseIdentifier": "c:input.h at T@MyEnumEnum",
+          "spelling": "MyEnumEnum"
+        },
+        {
+          "kind": "text",
+          "spelling": " "
+        },
+        {
+          "kind": "identifier",
+          "spelling": "MyEnumEnumEnum"
+        }
+      ],
+      "identifier": {
+        "interfaceLanguage": "c",
+        "precise": "c:input.h at T@MyEnumEnumEnum"
+      },
+      "kind": {
+        "displayName": "Type Alias",
+        "identifier": "c.typealias"
+      },
+      "location": {
+        "position": {
+          "character": 20,
+          "line": 6
+        },
+        "uri": "file://INPUT_DIR/input.h"
+      },
+      "names": {
+        "navigator": [
+          {
+            "kind": "identifier",
+            "spelling": "MyEnumEnumEnum"
+          }
+        ],
+        "subHeading": [
+          {
+            "kind": "identifier",
+            "spelling": "MyEnumEnumEnum"
+          }
+        ],
+        "title": "MyEnumEnumEnum"
+      },
+      "pathComponents": [
+        "MyEnumEnumEnum"
+      ],
+      "type": "c:input.h at T@MyEnumEnum"
     }
   ]
 }


        


More information about the cfe-commits mailing list