]> granicus.if.org Git - clang/commitdiff
[AST] Add a print() method in DeclarationName that accepts a PrintingPolicy.
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Sat, 13 Feb 2016 21:46:50 +0000 (21:46 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Sat, 13 Feb 2016 21:46:50 +0000 (21:46 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@260833 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/DeclarationName.h
lib/AST/DeclarationName.cpp

index 9482e83e81dc3c6285ed0da2e630fc947bbf574e..2d3cfe27a16595948ab129441b114a53de469e25 100644 (file)
@@ -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;
 };
 
index 8322b607a6cb6fcb85cc8842d34edd5a67a6b8a5..f6d045b2dab20262bd779524b10f08105f06eda7 100644 (file)
@@ -133,36 +133,43 @@ int DeclarationName::compare(DeclarationName LHS, DeclarationName RHS) {
   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, DeclarationName N) {
     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 {