From: Argyrios Kyrtzidis Date: Sat, 13 Feb 2016 21:46:50 +0000 (+0000) Subject: [AST] Add a print() method in DeclarationName that accepts a PrintingPolicy. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=11d2cfebc8d5225e957c37e76c88b262ff56ba2f;p=clang [AST] Add a print() method in DeclarationName that accepts a PrintingPolicy. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@260833 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/DeclarationName.h b/include/clang/AST/DeclarationName.h index 9482e83e81..2d3cfe27a1 100644 --- a/include/clang/AST/DeclarationName.h +++ b/include/clang/AST/DeclarationName.h @@ -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; }; diff --git a/lib/AST/DeclarationName.cpp b/lib/AST/DeclarationName.cpp index 8322b607a6..f6d045b2da 100644 --- a/lib/AST/DeclarationName.cpp +++ b/lib/AST/DeclarationName.cpp @@ -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()) { + 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()) - 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()) - 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()) - return OS << *Rec->getDecl(); - LangOptions LO; - LO.CPlusPlus = true; - LO.Bool = true; - return OS << Type.getAsString(PrintingPolicy(LO)); + if (const RecordType *Rec = Type->getAs()) { + 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 << ""; + OS << ""; + 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 {