[cfe-commits] r103225 - in /cfe/trunk: test/Index/usrs.cpp tools/libclang/CIndexUSRs.cpp

Ted Kremenek kremenek at apple.com
Thu May 6 18:04:32 PDT 2010


Author: kremenek
Date: Thu May  6 20:04:32 2010
New Revision: 103225

URL: http://llvm.org/viewvc/llvm-project?rev=103225&view=rev
Log:
Add initial USR support for mangling in the types of C++ functions and methods.

Modified:
    cfe/trunk/test/Index/usrs.cpp
    cfe/trunk/tools/libclang/CIndexUSRs.cpp

Modified: cfe/trunk/test/Index/usrs.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/usrs.cpp?rev=103225&r1=103224&r2=103225&view=diff
==============================================================================
--- cfe/trunk/test/Index/usrs.cpp (original)
+++ cfe/trunk/test/Index/usrs.cpp Thu May  6 20:04:32 2010
@@ -47,21 +47,25 @@
   };
 }}
 
+extern "C" {
+  void rez(int a, int b);
+}
+
 // RUN: c-index-test -test-load-source-usrs all %s | FileCheck %s
 // CHECK: usrs.cpp c:@N at foo Extent=[1:11 - 4:2]
 // CHECK: usrs.cpp c:@N at foo@x Extent=[2:3 - 2:8]
-// CHECK: usrs.cpp c:@N at foo@F at bar Extent=[3:8 - 3:18]
-// CHECK: usrs.cpp c:usrs.cpp at 3:12 at N@foo at F@bar at z Extent=[3:12 - 3:17]
+// CHECK: usrs.cpp c:@N at foo@F at bar#I Extent=[3:8 - 3:18]
+// CHECK: usrs.cpp c:usrs.cpp at 3:12 at N@foo at F@bar#I at z Extent=[3:12 - 3:17]
 // CHECK: usrs.cpp c:@N at bar Extent=[5:11 - 8:2]
 // CHECK: usrs.cpp c:usrs.cpp at 6:15 at N@bar at T@QType Extent=[6:15 - 6:20]
-// CHECK: usrs.cpp c:@N at bar@F at bar Extent=[7:8 - 7:20]
-// CHECK: usrs.cpp c:usrs.cpp at 7:12 at N@bar at F@bar at z Extent=[7:12 - 7:19]
+// CHECK: usrs.cpp c:@N at bar@F at bar#I Extent=[7:8 - 7:20]
+// CHECK: usrs.cpp c:usrs.cpp at 7:12 at N@bar at F@bar#I at z Extent=[7:12 - 7:19]
 // CHECK: usrs.cpp c:@C at ClsA Extent=[10:1 - 14:2]
 // CHECK: usrs.cpp c:@C at ClsA@FI at a Extent=[12:7 - 12:8]
 // CHECK: usrs.cpp c:@C at ClsA@FI at b Extent=[12:10 - 12:11]
-// CHECK: usrs.cpp c:@C at ClsA@F at ClsA Extent=[13:3 - 13:37]
-// CHECK: usrs.cpp c:usrs.cpp at 13:8 at C@ClsA at F@ClsA at A Extent=[13:8 - 13:13]
-// CHECK: usrs.cpp c:usrs.cpp at 13:15 at C@ClsA at F@ClsA at B Extent=[13:15 - 13:20]
+// CHECK: usrs.cpp c:@C at ClsA@F at ClsA#I#I Extent=[13:3 - 13:37]
+// CHECK: usrs.cpp c:usrs.cpp at 13:8 at C@ClsA at F@ClsA#I#I at A Extent=[13:8 - 13:13]
+// CHECK: usrs.cpp c:usrs.cpp at 13:15 at C@ClsA at F@ClsA#I#I at B Extent=[13:15 - 13:20]
 // CHECK: usrs.cpp c:@N at foo Extent=[16:11 - 22:2]
 // CHECK: usrs.cpp c:@N at foo@C at ClsB Extent=[17:3 - 21:4]
 // CHECK: usrs.cpp c:@N at foo@C at ClsB@F at ClsB Extent=[19:5 - 19:27]
@@ -73,19 +77,20 @@
 // CHECK: usrs.cpp c:@N at foo Extent=[35:11 - 40:2]
 // CHECK: usrs.cpp c:@N at foo@N at taz Extent=[35:27 - 39:2]
 // CHECK: usrs.cpp c:@N at foo@N at taz@x Extent=[36:3 - 36:8]
-// CHECK: usrs.cpp c:usrs.cpp at 37:21 at N@foo at N@taz at F@add Extent=[37:21 - 37:56]
-// CHECK: usrs.cpp c:usrs.cpp at 37:25 at N@foo at N@taz at F@add at a Extent=[37:25 - 37:30]
-// CHECK: usrs.cpp c:usrs.cpp at 37:32 at N@foo at N@taz at F@add at b Extent=[37:32 - 37:37]
-// CHECK: usrs.cpp c:@N at foo@N at taz@F at sub Extent=[38:8 - 38:25]
-// CHECK: usrs.cpp c:usrs.cpp at 38:12 at N@foo at N@taz at F@sub at a Extent=[38:12 - 38:17]
-// CHECK: usrs.cpp c:usrs.cpp at 38:19 at N@foo at N@taz at F@sub at b Extent=[38:19 - 38:24]
+// CHECK: usrs.cpp c:usrs.cpp at 37:21 at N@foo at N@taz at F@add#I#I Extent=[37:21 - 37:56]
+// CHECK: usrs.cpp c:usrs.cpp at 37:25 at N@foo at N@taz at F@add#I#I at a Extent=[37:25 - 37:30]
+// CHECK: usrs.cpp c:usrs.cpp at 37:32 at N@foo at N@taz at F@add#I#I at b Extent=[37:32 - 37:37]
+// CHECK: usrs.cpp c:@N at foo@N at taz@F at sub#I#I Extent=[38:8 - 38:25]
+// CHECK: usrs.cpp c:usrs.cpp at 38:12 at N@foo at N@taz at F@sub#I#I at a Extent=[38:12 - 38:17]
+// CHECK: usrs.cpp c:usrs.cpp at 38:19 at N@foo at N@taz at F@sub#I#I at b Extent=[38:19 - 38:24]
 // CHECK: usrs.cpp c:@N at foo Extent=[42:11 - 48:3]
 // CHECK: usrs.cpp c:@N at foo@N at taz Extent=[42:27 - 48:2]
 // CHECK: usrs.cpp c:@N at foo@N at taz@C at ClsD Extent=[43:3 - 47:4]
-// CHECK: usrs.cpp c:@N at foo@N at taz@C at ClsD@F at operator= Extent=[45:11 - 45:52]
-// CHECK: usrs.cpp c:usrs.cpp at 45:21 at N@foo at N@taz at C@ClsD at F@operator=@x Extent=[45:21 - 45:26]
-// CHECK: usrs.cpp c:@N at foo@N at taz@C at ClsD@F at operator= Extent=[46:11 - 46:61]
-// CHECK: usrs.cpp c:usrs.cpp at 46:21 at N@foo at N@taz at C@ClsD at F@operator=@x Extent=[46:21 - 46:29]
-
-
+// CHECK: usrs.cpp c:@N at foo@N at taz@C at ClsD@F at operator=#I Extent=[45:11 - 45:52]
+// CHECK: usrs.cpp c:usrs.cpp at 45:21 at N@foo at N@taz at C@ClsD at F@operator=#I at x Extent=[45:21 - 45:26]
+// CHECK: usrs.cpp c:@N at foo@N at taz@C at ClsD@F at operator=#d Extent=[46:11 - 46:61]
+// CHECK: usrs.cpp c:usrs.cpp at 46:21 at N@foo at N@taz at C@ClsD at F@operator=#d at x Extent=[46:21 - 46:29]
+// CHECK: usrs.cpp c:@F at rez Extent=[51:8 - 51:25]
+// CHECK: usrs.cpp c:usrs.cpp at 51:12 at F@rez at a Extent=[51:12 - 51:17]
+// CHECK: usrs.cpp c:usrs.cpp at 51:19 at F@rez at b Extent=[51:19 - 51:24]
 

Modified: cfe/trunk/tools/libclang/CIndexUSRs.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndexUSRs.cpp?rev=103225&r1=103224&r2=103225&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndexUSRs.cpp (original)
+++ cfe/trunk/tools/libclang/CIndexUSRs.cpp Thu May  6 20:04:32 2010
@@ -53,6 +53,10 @@
   void VisitTagDecl(TagDecl *D);
   void VisitTypedefDecl(TypedefDecl *D);
   void VisitVarDecl(VarDecl *D);
+  void VisitLinkageSpecDecl(LinkageSpecDecl *D) {
+    IgnoreResults = true;
+    return;
+  }
 
   /// Generate the string component containing the location of the
   ///  declaration.
@@ -82,6 +86,8 @@
   void GenObjCProperty(llvm::StringRef prop);
   /// Generate a USR for an Objective-C protocol.
   void GenObjCProtocol(llvm::StringRef prot);
+
+  void VisitType(QualType T);
 };
 
 class StringUSRGenerator {
@@ -147,6 +153,20 @@
 
   VisitDeclContext(D->getDeclContext());
   Out << "@F@" << D->getNameAsString();
+
+  ASTContext &Ctx = AU->getASTContext();
+  if (!Ctx.getLangOptions().CPlusPlus || D->isExternC())
+    return;
+
+  // Mangle in type information for the arguments.
+  for (FunctionDecl::param_iterator I = D->param_begin(), E = D->param_end();
+       I != E; ++I) {
+    Out << '#';
+    if (ParmVarDecl *PD = *I)
+      VisitType(PD->getType());
+  }
+  if (D->isVariadic())
+    Out << '.';
 }
 
 void USRGenerator::VisitNamedDecl(NamedDecl *D) {
@@ -274,7 +294,7 @@
   // translation units.
   if (ShouldGenerateLocation(D) && GenLoc(D))
     return;
-  
+
   D = D->getCanonicalDecl();
   VisitDeclContext(D->getDeclContext());
 
@@ -336,10 +356,126 @@
   Out << '@'
       << SM.getLineNumber(Decomposed.first, Decomposed.second) << ':'
       << SM.getColumnNumber(Decomposed.first, Decomposed.second);
-  
+
   return IgnoreResults;
 }
 
+void USRGenerator::VisitType(QualType T) {
+  // This method mangles in USR information for types.  It can possibly
+  // just reuse the naming-mangling logic used by codegen, although the
+  // requirements for USRs might not be the same.
+  do {
+    T = T.getTypePtr()->getCanonicalTypeInternal();
+    Qualifiers Q = T.getQualifiers();
+    if (Q.hasConst())
+      Out << '1';
+    if (Q.hasVolatile())
+      Out << '2';
+    if (Q.hasRestrict())
+      Out << '3';
+
+    // Mangle in ObjC GC qualifiers?
+
+    if (const PointerType *PT = T->getAs<PointerType>()) {
+      Out << '*';
+      T = PT->getPointeeType();
+      continue;
+    }
+    if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
+      Out << '&';
+      T = RT->getPointeeType();
+      continue;
+    }
+    if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) {
+      Out << 'F';
+      VisitType(FT->getResultType());
+      for (FunctionProtoType::arg_type_iterator
+            I = FT->arg_type_begin(), E = FT->arg_type_end(); I!=E; ++I) {
+        VisitType(*I);
+      }
+      if (FT->isVariadic())
+        Out << '.';
+      return;
+    }
+    if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
+      Out << 'B';
+      T = BT->getPointeeType();
+      continue;
+    }
+    if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
+      unsigned char c = '\0';
+      switch (BT->getKind()) {
+        case BuiltinType::Void:
+          c = 'v'; break;
+        case BuiltinType::Bool:
+          c = 'b'; break;
+        case BuiltinType::Char_U:
+        case BuiltinType::UChar:
+          c = 'c'; break;
+        case BuiltinType::Char16:
+          c = 'q'; break;
+        case BuiltinType::Char32:
+          c = 'w'; break;
+        case BuiltinType::UShort:
+          c = 's'; break;
+        case BuiltinType::UInt:
+          c = 'i'; break;
+        case BuiltinType::ULong:
+          c = 'l'; break;
+        case BuiltinType::ULongLong:
+          c = 'k'; break;
+        case BuiltinType::UInt128:
+          c = 'j'; break;
+        case BuiltinType::Char_S:
+        case BuiltinType::SChar:
+          c = 'C'; break;
+        case BuiltinType::WChar:
+          c = 'W'; break;
+        case BuiltinType::Short:
+          c = 'S'; break;
+        case BuiltinType::Int:
+          c = 'I'; break;
+        case BuiltinType::Long:
+          c = 'L'; break;
+        case BuiltinType::LongLong:
+          c = 'K'; break;
+        case BuiltinType::Int128:
+          c = 'J'; break;
+        case BuiltinType::Float:
+          c = 'f'; break;
+        case BuiltinType::Double:
+          c = 'd'; break;
+        case BuiltinType::LongDouble:
+          c = 'D'; break;
+        case BuiltinType::NullPtr:
+          c = 'n'; break;
+        case BuiltinType::Overload:
+        case BuiltinType::Dependent:
+        case BuiltinType::UndeducedAuto:
+          IgnoreResults = true;
+          return;
+        case BuiltinType::ObjCId:
+          c = 'o'; break;
+        case BuiltinType::ObjCClass:
+          c = 'O'; break;
+        case BuiltinType::ObjCSel:
+          c = 'e'; break;
+      }
+      Out << c;
+      return;
+    }
+    if (const ComplexType *CT = T->getAs<ComplexType>()) {
+      Out << '<';
+      T = CT->getElementType();
+      continue;
+    }
+
+    // Unhandled type.
+    Out << ' ';
+    break;
+  } while (true);
+}
+
 //===----------------------------------------------------------------------===//
 // General purpose USR generation methods.
 //===----------------------------------------------------------------------===//





More information about the cfe-commits mailing list