r260833 - [AST] Add a print() method in DeclarationName that accepts a PrintingPolicy.

Argyrios Kyrtzidis via cfe-commits cfe-commits at lists.llvm.org
Sat Feb 13 13:46:51 PST 2016


Author: akirtzidis
Date: Sat Feb 13 15:46:50 2016
New Revision: 260833

URL: http://llvm.org/viewvc/llvm-project?rev=260833&view=rev
Log:
[AST] Add a print() method in DeclarationName that accepts a PrintingPolicy.

Modified:
    cfe/trunk/include/clang/AST/DeclarationName.h
    cfe/trunk/lib/AST/DeclarationName.cpp

Modified: cfe/trunk/include/clang/AST/DeclarationName.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclarationName.h?rev=260833&r1=260832&r2=260833&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/DeclarationName.h (original)
+++ cfe/trunk/include/clang/AST/DeclarationName.h Sat Feb 13 15:46:50 2016
@@ -30,6 +30,7 @@ namespace clang {
   class IdentifierInfo;
   class MultiKeywordSelector;
   enum OverloadedOperatorKind : int;
+  struct PrintingPolicy;
   class QualType;
   class Type;
   class TypeSourceInfo;
@@ -302,7 +303,9 @@ public:
   }
 
   static int compare(DeclarationName LHS, DeclarationName RHS);
-  
+
+  void print(raw_ostream &OS, const PrintingPolicy &Policy);
+
   void dump() const;
 };
 

Modified: cfe/trunk/lib/AST/DeclarationName.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclarationName.cpp?rev=260833&r1=260832&r2=260833&view=diff
==============================================================================
--- cfe/trunk/lib/AST/DeclarationName.cpp (original)
+++ cfe/trunk/lib/AST/DeclarationName.cpp Sat Feb 13 15:46:50 2016
@@ -133,36 +133,43 @@ int DeclarationName::compare(Declaration
   llvm_unreachable("Invalid DeclarationName Kind!");
 }
 
-raw_ostream &operator<<(raw_ostream &OS, DeclarationName N) {
+static void printCXXConstructorDestructorName(QualType ClassType,
+                                              raw_ostream &OS,
+                                              const PrintingPolicy &Policy) {
+  if (const RecordType *ClassRec = ClassType->getAs<RecordType>()) {
+    OS << *ClassRec->getDecl();
+    return;
+  }
+  if (!Policy.LangOpts.CPlusPlus) {
+    // Passed policy is the default one from operator <<, use a C++ policy.
+    LangOptions LO;
+    LO.CPlusPlus = true;
+    ClassType.print(OS, PrintingPolicy(LO));
+  } else {
+    ClassType.print(OS, Policy);
+  }
+}
+
+void DeclarationName::print(raw_ostream &OS, const PrintingPolicy &Policy) {
+  DeclarationName &N = *this;
   switch (N.getNameKind()) {
   case DeclarationName::Identifier:
     if (const IdentifierInfo *II = N.getAsIdentifierInfo())
       OS << II->getName();
-    return OS;
+    return;
 
   case DeclarationName::ObjCZeroArgSelector:
   case DeclarationName::ObjCOneArgSelector:
   case DeclarationName::ObjCMultiArgSelector:
     N.getObjCSelector().print(OS);
-    return OS;
+    return;
 
-  case DeclarationName::CXXConstructorName: {
-    QualType ClassType = N.getCXXNameType();
-    if (const RecordType *ClassRec = ClassType->getAs<RecordType>())
-      return OS << *ClassRec->getDecl();
-    LangOptions LO;
-    LO.CPlusPlus = true;
-    return OS << ClassType.getAsString(PrintingPolicy(LO));
-  }
+  case DeclarationName::CXXConstructorName:
+    return printCXXConstructorDestructorName(N.getCXXNameType(), OS, Policy);
 
   case DeclarationName::CXXDestructorName: {
     OS << '~';
-    QualType Type = N.getCXXNameType();
-    if (const RecordType *Rec = Type->getAs<RecordType>())
-      return OS << *Rec->getDecl();
-    LangOptions LO;
-    LO.CPlusPlus = true;
-    return OS << Type.getAsString(PrintingPolicy(LO));
+    return printCXXConstructorDestructorName(N.getCXXNameType(), OS, Policy);
   }
 
   case DeclarationName::CXXOperatorName: {
@@ -178,29 +185,46 @@ raw_ostream &operator<<(raw_ostream &OS,
     OS << "operator";
     if (OpName[0] >= 'a' && OpName[0] <= 'z')
       OS << ' ';
-    return OS << OpName;
+    OS << OpName;
+    return;
   }
 
   case DeclarationName::CXXLiteralOperatorName:
-    return OS << "operator\"\"" << N.getCXXLiteralIdentifier()->getName();
+    OS << "operator\"\"" << N.getCXXLiteralIdentifier()->getName();
+    return;
 
   case DeclarationName::CXXConversionFunctionName: {
     OS << "operator ";
     QualType Type = N.getCXXNameType();
-    if (const RecordType *Rec = Type->getAs<RecordType>())
-      return OS << *Rec->getDecl();
-    LangOptions LO;
-    LO.CPlusPlus = true;
-    LO.Bool = true;
-    return OS << Type.getAsString(PrintingPolicy(LO));
+    if (const RecordType *Rec = Type->getAs<RecordType>()) {
+      OS << *Rec->getDecl();
+      return;
+    }
+    if (!Policy.LangOpts.CPlusPlus) {
+      // Passed policy is the default one from operator <<, use a C++ policy.
+      LangOptions LO;
+      LO.CPlusPlus = true;
+      LO.Bool = true;
+      Type.print(OS, PrintingPolicy(LO));
+    } else {
+      Type.print(OS, Policy);
+    }
+    return;
   }
   case DeclarationName::CXXUsingDirective:
-    return OS << "<using-directive>";
+    OS << "<using-directive>";
+    return;
   }
 
   llvm_unreachable("Unexpected declaration name kind");
 }
 
+raw_ostream &operator<<(raw_ostream &OS, DeclarationName N) {
+  LangOptions LO;
+  N.print(OS, PrintingPolicy(LO));
+  return OS;
+}
+
 } // end namespace clang
 
 DeclarationName::NameKind DeclarationName::getNameKind() const {




More information about the cfe-commits mailing list