#include "clang/AST/ASTDiagnostic.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/ASTLambda.h"
#include "clang/AST/Attr.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/AST/ExprCXX.h"
#include "clang/AST/TemplateBase.h"
#include "clang/AST/Type.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/raw_ostream.h"
using namespace clang;
QualType clang::desugarForDiagnostic(ASTContext &Context, QualType QT,
bool &ShouldAKA) {
QualifierCollector QC;
while (true) {
const Type *Ty = QC.strip(QT);
if (const ElaboratedType *ET = dyn_cast<ElaboratedType>(Ty)) {
QT = ET->desugar();
continue;
}
if (const UsingType *UT = dyn_cast<UsingType>(Ty)) {
QT = UT->desugar();
continue;
}
if (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
QT = PT->desugar();
continue;
}
if (const MacroQualifiedType *MDT = dyn_cast<MacroQualifiedType>(Ty)) {
QT = MDT->desugar();
continue;
}
if (const SubstTemplateTypeParmType *ST =
dyn_cast<SubstTemplateTypeParmType>(Ty)) {
QT = ST->desugar();
continue;
}
if (const AttributedType *AT = dyn_cast<AttributedType>(Ty)) {
QT = AT->desugar();
continue;
}
if (const AdjustedType *AT = dyn_cast<AdjustedType>(Ty)) {
QT = AT->desugar();
continue;
}
if (const AutoType *AT = dyn_cast<AutoType>(Ty)) {
if (!AT->isSugared())
break;
QT = AT->desugar();
continue;
}
if (const FunctionType *FT = dyn_cast<FunctionType>(Ty)) {
bool DesugarReturn = false;
QualType SugarRT = FT->getReturnType();
QualType RT = desugarForDiagnostic(Context, SugarRT, DesugarReturn);
if (auto nullability = AttributedType::stripOuterNullability(SugarRT)) {
RT = Context.getAttributedType(
AttributedType::getNullabilityAttrKind(*nullability), RT, RT);
}
bool DesugarArgument = false;
SmallVector<QualType, 4> Args;
const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT);
if (FPT) {
for (QualType SugarPT : FPT->param_types()) {
QualType PT = desugarForDiagnostic(Context, SugarPT, DesugarArgument);
if (auto nullability =
AttributedType::stripOuterNullability(SugarPT)) {
PT = Context.getAttributedType(
AttributedType::getNullabilityAttrKind(*nullability), PT, PT);
}
Args.push_back(PT);
}
}
if (DesugarReturn || DesugarArgument) {
ShouldAKA = true;
QT = FPT ? Context.getFunctionType(RT, Args, FPT->getExtProtoInfo())
: Context.getFunctionNoProtoType(RT, FT->getExtInfo());
break;
}
}
if (const TemplateSpecializationType *TST =
dyn_cast<TemplateSpecializationType>(Ty)) {
if (!TST->isTypeAlias()) {
bool DesugarArgument = false;
SmallVector<TemplateArgument, 4> Args;
for (unsigned I = 0, N = TST->getNumArgs(); I != N; ++I) {
const TemplateArgument &Arg = TST->getArg(I);
if (Arg.getKind() == TemplateArgument::Type)
Args.push_back(desugarForDiagnostic(Context, Arg.getAsType(),
DesugarArgument));
else
Args.push_back(Arg);
}
if (DesugarArgument) {
ShouldAKA = true;
QT = Context.getTemplateSpecializationType(
TST->getTemplateName(), Args, QT);
}
break;
}
}
if (const auto *AT = dyn_cast<ArrayType>(Ty)) {
QualType ElementTy =
desugarForDiagnostic(Context, AT->getElementType(), ShouldAKA);
if (const auto *CAT = dyn_cast<ConstantArrayType>(AT))
QT = Context.getConstantArrayType(
ElementTy, CAT->getSize(), CAT->getSizeExpr(),
CAT->getSizeModifier(), CAT->getIndexTypeCVRQualifiers());
else if (const auto *VAT = dyn_cast<VariableArrayType>(AT))
QT = Context.getVariableArrayType(
ElementTy, VAT->getSizeExpr(), VAT->getSizeModifier(),
VAT->getIndexTypeCVRQualifiers(), VAT->getBracketsRange());
else if (const auto *DSAT = dyn_cast<DependentSizedArrayType>(AT))
QT = Context.getDependentSizedArrayType(
ElementTy, DSAT->getSizeExpr(), DSAT->getSizeModifier(),
DSAT->getIndexTypeCVRQualifiers(), DSAT->getBracketsRange());
else if (const auto *IAT = dyn_cast<IncompleteArrayType>(AT))
QT = Context.getIncompleteArrayType(ElementTy, IAT->getSizeModifier(),
IAT->getIndexTypeCVRQualifiers());
else
llvm_unreachable("Unhandled array type");
break;
}
if (QualType(Ty,0) == Context.getObjCIdType() ||
QualType(Ty,0) == Context.getObjCClassType() ||
QualType(Ty,0) == Context.getObjCSelType() ||
QualType(Ty,0) == Context.getObjCProtoType())
break;
if (QualType(Ty, 0) == Context.getBuiltinVaListType() ||
QualType(Ty, 0) == Context.getBuiltinMSVaListType())
break;
QualType Underlying;
bool IsSugar = false;
switch (Ty->getTypeClass()) {
#define ABSTRACT_TYPE(Class, Base)
#define TYPE(Class, Base) \
case Type::Class: { \
const Class##Type *CTy = cast<Class##Type>(Ty); \
if (CTy->isSugared()) { \
IsSugar = true; \
Underlying = CTy->desugar(); \
} \
break; \
}
#include "clang/AST/TypeNodes.inc"
}
if (!IsSugar)
break;
if (isa<VectorType>(Underlying))
break;
if (const TagType *UTT = Underlying->getAs<TagType>())
if (const TypedefType *QTT = dyn_cast<TypedefType>(QT))
if (UTT->getDecl()->getTypedefNameForAnonDecl() == QTT->getDecl())
break;
ShouldAKA = true;
QT = Underlying;
}
if (const PointerType *Ty = QT->getAs<PointerType>()) {
QT = Context.getPointerType(
desugarForDiagnostic(Context, Ty->getPointeeType(), ShouldAKA));
} else if (const auto *Ty = QT->getAs<ObjCObjectPointerType>()) {
QT = Context.getObjCObjectPointerType(
desugarForDiagnostic(Context, Ty->getPointeeType(), ShouldAKA));
} else if (const LValueReferenceType *Ty = QT->getAs<LValueReferenceType>()) {
QT = Context.getLValueReferenceType(
desugarForDiagnostic(Context, Ty->getPointeeType(), ShouldAKA));
} else if (const RValueReferenceType *Ty = QT->getAs<RValueReferenceType>()) {
QT = Context.getRValueReferenceType(
desugarForDiagnostic(Context, Ty->getPointeeType(), ShouldAKA));
} else if (const auto *Ty = QT->getAs<ObjCObjectType>()) {
if (Ty->getBaseType().getTypePtr() != Ty && !ShouldAKA) {
QualType BaseType =
desugarForDiagnostic(Context, Ty->getBaseType(), ShouldAKA);
QT = Context.getObjCObjectType(
BaseType, Ty->getTypeArgsAsWritten(),
llvm::makeArrayRef(Ty->qual_begin(), Ty->getNumProtocols()),
Ty->isKindOfTypeAsWritten());
}
}
return QC.apply(Context, QT);
}
static std::string
ConvertTypeToDiagnosticString(ASTContext &Context, QualType Ty,
ArrayRef<DiagnosticsEngine::ArgumentValue> PrevArgs,
ArrayRef<intptr_t> QualTypeVals) {
bool ForceAKA = false;
QualType CanTy = Ty.getCanonicalType();
std::string S = Ty.getAsString(Context.getPrintingPolicy());
std::string CanS = CanTy.getAsString(Context.getPrintingPolicy());
for (const intptr_t &QualTypeVal : QualTypeVals) {
QualType CompareTy =
QualType::getFromOpaquePtr(reinterpret_cast<void *>(QualTypeVal));
if (CompareTy.isNull())
continue;
if (CompareTy == Ty)
continue; QualType CompareCanTy = CompareTy.getCanonicalType();
if (CompareCanTy == CanTy)
continue; std::string CompareS = CompareTy.getAsString(Context.getPrintingPolicy());
bool ShouldAKA = false;
QualType CompareDesugar =
desugarForDiagnostic(Context, CompareTy, ShouldAKA);
std::string CompareDesugarStr =
CompareDesugar.getAsString(Context.getPrintingPolicy());
if (CompareS != S && CompareDesugarStr != S)
continue; std::string CompareCanS =
CompareCanTy.getAsString(Context.getPrintingPolicy());
if (CompareCanS == CanS)
continue;
ForceAKA = true;
break;
}
bool Repeated = false;
for (const auto &PrevArg : PrevArgs) {
if (PrevArg.first == DiagnosticsEngine::ak_qualtype) {
QualType PrevTy(
QualType::getFromOpaquePtr(reinterpret_cast<void *>(PrevArg.second)));
if (PrevTy == Ty) {
Repeated = true;
break;
}
}
}
if (!Repeated) {
bool ShouldAKA = false;
QualType DesugaredTy = desugarForDiagnostic(Context, Ty, ShouldAKA);
if (ShouldAKA || ForceAKA) {
if (DesugaredTy == Ty) {
DesugaredTy = Ty.getCanonicalType();
}
std::string akaStr = DesugaredTy.getAsString(Context.getPrintingPolicy());
if (akaStr != S) {
S = "'" + S + "' (aka '" + akaStr + "')";
return S;
}
}
if (const auto *VTy = Ty->getAs<VectorType>()) {
std::string DecoratedString;
llvm::raw_string_ostream OS(DecoratedString);
const char *Values = VTy->getNumElements() > 1 ? "values" : "value";
OS << "'" << S << "' (vector of " << VTy->getNumElements() << " '"
<< VTy->getElementType().getAsString(Context.getPrintingPolicy())
<< "' " << Values << ")";
return DecoratedString;
}
}
S = "'" + S + "'";
return S;
}
static bool FormatTemplateTypeDiff(ASTContext &Context, QualType FromType,
QualType ToType, bool PrintTree,
bool PrintFromType, bool ElideType,
bool ShowColors, raw_ostream &OS);
void clang::FormatASTNodeDiagnosticArgument(
DiagnosticsEngine::ArgumentKind Kind,
intptr_t Val,
StringRef Modifier,
StringRef Argument,
ArrayRef<DiagnosticsEngine::ArgumentValue> PrevArgs,
SmallVectorImpl<char> &Output,
void *Cookie,
ArrayRef<intptr_t> QualTypeVals) {
ASTContext &Context = *static_cast<ASTContext*>(Cookie);
size_t OldEnd = Output.size();
llvm::raw_svector_ostream OS(Output);
bool NeedQuotes = true;
switch (Kind) {
default: llvm_unreachable("unknown ArgumentKind");
case DiagnosticsEngine::ak_addrspace: {
assert(Modifier.empty() && Argument.empty() &&
"Invalid modifier for Qualifiers argument");
auto S = Qualifiers::getAddrSpaceAsString(static_cast<LangAS>(Val));
if (S.empty()) {
OS << (Context.getLangOpts().OpenCL ? "default" : "generic");
OS << " address space";
} else {
OS << "address space";
OS << " '" << S << "'";
}
NeedQuotes = false;
break;
}
case DiagnosticsEngine::ak_qual: {
assert(Modifier.empty() && Argument.empty() &&
"Invalid modifier for Qualifiers argument");
Qualifiers Q(Qualifiers::fromOpaqueValue(Val));
auto S = Q.getAsString();
if (S.empty()) {
OS << "unqualified";
NeedQuotes = false;
} else {
OS << S;
}
break;
}
case DiagnosticsEngine::ak_qualtype_pair: {
TemplateDiffTypes &TDT = *reinterpret_cast<TemplateDiffTypes*>(Val);
QualType FromType =
QualType::getFromOpaquePtr(reinterpret_cast<void*>(TDT.FromType));
QualType ToType =
QualType::getFromOpaquePtr(reinterpret_cast<void*>(TDT.ToType));
if (FormatTemplateTypeDiff(Context, FromType, ToType, TDT.PrintTree,
TDT.PrintFromType, TDT.ElideType,
TDT.ShowColors, OS)) {
NeedQuotes = !TDT.PrintTree;
TDT.TemplateDiffUsed = true;
break;
}
if (TDT.PrintTree)
return;
Val = TDT.PrintFromType ? TDT.FromType : TDT.ToType;
Modifier = StringRef();
Argument = StringRef();
LLVM_FALLTHROUGH;
}
case DiagnosticsEngine::ak_qualtype: {
assert(Modifier.empty() && Argument.empty() &&
"Invalid modifier for QualType argument");
QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val)));
OS << ConvertTypeToDiagnosticString(Context, Ty, PrevArgs, QualTypeVals);
NeedQuotes = false;
break;
}
case DiagnosticsEngine::ak_declarationname: {
if (Modifier == "objcclass" && Argument.empty())
OS << '+';
else if (Modifier == "objcinstance" && Argument.empty())
OS << '-';
else
assert(Modifier.empty() && Argument.empty() &&
"Invalid modifier for DeclarationName argument");
OS << DeclarationName::getFromOpaqueInteger(Val);
break;
}
case DiagnosticsEngine::ak_nameddecl: {
bool Qualified;
if (Modifier == "q" && Argument.empty())
Qualified = true;
else {
assert(Modifier.empty() && Argument.empty() &&
"Invalid modifier for NamedDecl* argument");
Qualified = false;
}
const NamedDecl *ND = reinterpret_cast<const NamedDecl*>(Val);
ND->getNameForDiagnostic(OS, Context.getPrintingPolicy(), Qualified);
break;
}
case DiagnosticsEngine::ak_nestednamespec: {
NestedNameSpecifier *NNS = reinterpret_cast<NestedNameSpecifier*>(Val);
NNS->print(OS, Context.getPrintingPolicy());
NeedQuotes = false;
break;
}
case DiagnosticsEngine::ak_declcontext: {
DeclContext *DC = reinterpret_cast<DeclContext *> (Val);
assert(DC && "Should never have a null declaration context");
NeedQuotes = false;
if (DC->isTranslationUnit()) {
if (Context.getLangOpts().CPlusPlus)
OS << "the global namespace";
else
OS << "the global scope";
} else if (DC->isClosure()) {
OS << "block literal";
} else if (isLambdaCallOperator(DC)) {
OS << "lambda expression";
} else if (TypeDecl *Type = dyn_cast<TypeDecl>(DC)) {
OS << ConvertTypeToDiagnosticString(Context,
Context.getTypeDeclType(Type),
PrevArgs, QualTypeVals);
} else {
assert(isa<NamedDecl>(DC) && "Expected a NamedDecl");
NamedDecl *ND = cast<NamedDecl>(DC);
if (isa<NamespaceDecl>(ND))
OS << "namespace ";
else if (isa<ObjCMethodDecl>(ND))
OS << "method ";
else if (isa<FunctionDecl>(ND))
OS << "function ";
OS << '\'';
ND->getNameForDiagnostic(OS, Context.getPrintingPolicy(), true);
OS << '\'';
}
break;
}
case DiagnosticsEngine::ak_attr: {
const Attr *At = reinterpret_cast<Attr *>(Val);
assert(At && "Received null Attr object!");
OS << '\'' << At->getSpelling() << '\'';
NeedQuotes = false;
break;
}
}
if (NeedQuotes) {
Output.insert(Output.begin()+OldEnd, '\'');
Output.push_back('\'');
}
}
namespace {
class TemplateDiff {
ASTContext &Context;
PrintingPolicy Policy;
bool ElideType;
bool PrintTree;
bool ShowColor;
QualType FromTemplateType;
QualType ToTemplateType;
raw_ostream &OS;
bool IsBold;
class DiffTree {
public:
enum DiffKind {
Invalid,
Template,
Type,
Expression,
TemplateTemplate,
Integer,
Declaration,
FromIntegerAndToDeclaration,
FromDeclarationAndToInteger
};
private:
struct TemplateArgumentInfo {
QualType ArgType;
Qualifiers Qual;
llvm::APSInt Val;
bool IsValidInt = false;
Expr *ArgExpr = nullptr;
TemplateDecl *TD = nullptr;
ValueDecl *VD = nullptr;
bool NeedAddressOf = false;
bool IsNullPtr = false;
bool IsDefault = false;
};
struct DiffNode {
DiffKind Kind = Invalid;
unsigned NextNode = 0;
unsigned ChildNode = 0;
unsigned ParentNode = 0;
TemplateArgumentInfo FromArgInfo, ToArgInfo;
bool Same = false;
DiffNode(unsigned ParentNode = 0) : ParentNode(ParentNode) {}
};
SmallVector<DiffNode, 16> FlatTree;
unsigned CurrentNode;
unsigned NextFreeNode;
unsigned ReadNode;
public:
DiffTree() : CurrentNode(0), NextFreeNode(1), ReadNode(0) {
FlatTree.push_back(DiffNode());
}
void SetTemplateDiff(TemplateDecl *FromTD, TemplateDecl *ToTD,
Qualifiers FromQual, Qualifiers ToQual,
bool FromDefault, bool ToDefault) {
assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
FlatTree[CurrentNode].Kind = Template;
FlatTree[CurrentNode].FromArgInfo.TD = FromTD;
FlatTree[CurrentNode].ToArgInfo.TD = ToTD;
FlatTree[CurrentNode].FromArgInfo.Qual = FromQual;
FlatTree[CurrentNode].ToArgInfo.Qual = ToQual;
SetDefault(FromDefault, ToDefault);
}
void SetTypeDiff(QualType FromType, QualType ToType, bool FromDefault,
bool ToDefault) {
assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
FlatTree[CurrentNode].Kind = Type;
FlatTree[CurrentNode].FromArgInfo.ArgType = FromType;
FlatTree[CurrentNode].ToArgInfo.ArgType = ToType;
SetDefault(FromDefault, ToDefault);
}
void SetExpressionDiff(Expr *FromExpr, Expr *ToExpr, bool FromDefault,
bool ToDefault) {
assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
FlatTree[CurrentNode].Kind = Expression;
FlatTree[CurrentNode].FromArgInfo.ArgExpr = FromExpr;
FlatTree[CurrentNode].ToArgInfo.ArgExpr = ToExpr;
SetDefault(FromDefault, ToDefault);
}
void SetTemplateTemplateDiff(TemplateDecl *FromTD, TemplateDecl *ToTD,
bool FromDefault, bool ToDefault) {
assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
FlatTree[CurrentNode].Kind = TemplateTemplate;
FlatTree[CurrentNode].FromArgInfo.TD = FromTD;
FlatTree[CurrentNode].ToArgInfo.TD = ToTD;
SetDefault(FromDefault, ToDefault);
}
void SetIntegerDiff(const llvm::APSInt &FromInt, const llvm::APSInt &ToInt,
bool IsValidFromInt, bool IsValidToInt,
QualType FromIntType, QualType ToIntType,
Expr *FromExpr, Expr *ToExpr, bool FromDefault,
bool ToDefault) {
assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
FlatTree[CurrentNode].Kind = Integer;
FlatTree[CurrentNode].FromArgInfo.Val = FromInt;
FlatTree[CurrentNode].ToArgInfo.Val = ToInt;
FlatTree[CurrentNode].FromArgInfo.IsValidInt = IsValidFromInt;
FlatTree[CurrentNode].ToArgInfo.IsValidInt = IsValidToInt;
FlatTree[CurrentNode].FromArgInfo.ArgType = FromIntType;
FlatTree[CurrentNode].ToArgInfo.ArgType = ToIntType;
FlatTree[CurrentNode].FromArgInfo.ArgExpr = FromExpr;
FlatTree[CurrentNode].ToArgInfo.ArgExpr = ToExpr;
SetDefault(FromDefault, ToDefault);
}
void SetDeclarationDiff(ValueDecl *FromValueDecl, ValueDecl *ToValueDecl,
bool FromAddressOf, bool ToAddressOf,
bool FromNullPtr, bool ToNullPtr, Expr *FromExpr,
Expr *ToExpr, bool FromDefault, bool ToDefault) {
assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
FlatTree[CurrentNode].Kind = Declaration;
FlatTree[CurrentNode].FromArgInfo.VD = FromValueDecl;
FlatTree[CurrentNode].ToArgInfo.VD = ToValueDecl;
FlatTree[CurrentNode].FromArgInfo.NeedAddressOf = FromAddressOf;
FlatTree[CurrentNode].ToArgInfo.NeedAddressOf = ToAddressOf;
FlatTree[CurrentNode].FromArgInfo.IsNullPtr = FromNullPtr;
FlatTree[CurrentNode].ToArgInfo.IsNullPtr = ToNullPtr;
FlatTree[CurrentNode].FromArgInfo.ArgExpr = FromExpr;
FlatTree[CurrentNode].ToArgInfo.ArgExpr = ToExpr;
SetDefault(FromDefault, ToDefault);
}
void SetFromDeclarationAndToIntegerDiff(
ValueDecl *FromValueDecl, bool FromAddressOf, bool FromNullPtr,
Expr *FromExpr, const llvm::APSInt &ToInt, bool IsValidToInt,
QualType ToIntType, Expr *ToExpr, bool FromDefault, bool ToDefault) {
assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
FlatTree[CurrentNode].Kind = FromDeclarationAndToInteger;
FlatTree[CurrentNode].FromArgInfo.VD = FromValueDecl;
FlatTree[CurrentNode].FromArgInfo.NeedAddressOf = FromAddressOf;
FlatTree[CurrentNode].FromArgInfo.IsNullPtr = FromNullPtr;
FlatTree[CurrentNode].FromArgInfo.ArgExpr = FromExpr;
FlatTree[CurrentNode].ToArgInfo.Val = ToInt;
FlatTree[CurrentNode].ToArgInfo.IsValidInt = IsValidToInt;
FlatTree[CurrentNode].ToArgInfo.ArgType = ToIntType;
FlatTree[CurrentNode].ToArgInfo.ArgExpr = ToExpr;
SetDefault(FromDefault, ToDefault);
}
void SetFromIntegerAndToDeclarationDiff(
const llvm::APSInt &FromInt, bool IsValidFromInt, QualType FromIntType,
Expr *FromExpr, ValueDecl *ToValueDecl, bool ToAddressOf,
bool ToNullPtr, Expr *ToExpr, bool FromDefault, bool ToDefault) {
assert(FlatTree[CurrentNode].Kind == Invalid && "Node is not empty.");
FlatTree[CurrentNode].Kind = FromIntegerAndToDeclaration;
FlatTree[CurrentNode].FromArgInfo.Val = FromInt;
FlatTree[CurrentNode].FromArgInfo.IsValidInt = IsValidFromInt;
FlatTree[CurrentNode].FromArgInfo.ArgType = FromIntType;
FlatTree[CurrentNode].FromArgInfo.ArgExpr = FromExpr;
FlatTree[CurrentNode].ToArgInfo.VD = ToValueDecl;
FlatTree[CurrentNode].ToArgInfo.NeedAddressOf = ToAddressOf;
FlatTree[CurrentNode].ToArgInfo.IsNullPtr = ToNullPtr;
FlatTree[CurrentNode].ToArgInfo.ArgExpr = ToExpr;
SetDefault(FromDefault, ToDefault);
}
void SetDefault(bool FromDefault, bool ToDefault) {
assert((!FromDefault || !ToDefault) && "Both arguments cannot be default.");
FlatTree[CurrentNode].FromArgInfo.IsDefault = FromDefault;
FlatTree[CurrentNode].ToArgInfo.IsDefault = ToDefault;
}
void SetSame(bool Same) {
FlatTree[CurrentNode].Same = Same;
}
void SetKind(DiffKind Kind) {
FlatTree[CurrentNode].Kind = Kind;
}
void Up() {
assert(FlatTree[CurrentNode].Kind != Invalid &&
"Cannot exit node before setting node information.");
CurrentNode = FlatTree[CurrentNode].ParentNode;
}
void AddNode() {
assert(FlatTree[CurrentNode].Kind == Template &&
"Only Template nodes can have children nodes.");
FlatTree.push_back(DiffNode(CurrentNode));
DiffNode &Node = FlatTree[CurrentNode];
if (Node.ChildNode == 0) {
Node.ChildNode = NextFreeNode;
} else {
unsigned i;
for (i = Node.ChildNode; FlatTree[i].NextNode != 0;
i = FlatTree[i].NextNode) {
}
FlatTree[i].NextNode = NextFreeNode;
}
CurrentNode = NextFreeNode;
++NextFreeNode;
}
void StartTraverse() {
ReadNode = 0;
CurrentNode = NextFreeNode;
NextFreeNode = 0;
}
void Parent() {
ReadNode = FlatTree[ReadNode].ParentNode;
}
void GetTemplateDiff(TemplateDecl *&FromTD, TemplateDecl *&ToTD,
Qualifiers &FromQual, Qualifiers &ToQual) {
assert(FlatTree[ReadNode].Kind == Template && "Unexpected kind.");
FromTD = FlatTree[ReadNode].FromArgInfo.TD;
ToTD = FlatTree[ReadNode].ToArgInfo.TD;
FromQual = FlatTree[ReadNode].FromArgInfo.Qual;
ToQual = FlatTree[ReadNode].ToArgInfo.Qual;
}
void GetTypeDiff(QualType &FromType, QualType &ToType) {
assert(FlatTree[ReadNode].Kind == Type && "Unexpected kind");
FromType = FlatTree[ReadNode].FromArgInfo.ArgType;
ToType = FlatTree[ReadNode].ToArgInfo.ArgType;
}
void GetExpressionDiff(Expr *&FromExpr, Expr *&ToExpr) {
assert(FlatTree[ReadNode].Kind == Expression && "Unexpected kind");
FromExpr = FlatTree[ReadNode].FromArgInfo.ArgExpr;
ToExpr = FlatTree[ReadNode].ToArgInfo.ArgExpr;
}
void GetTemplateTemplateDiff(TemplateDecl *&FromTD, TemplateDecl *&ToTD) {
assert(FlatTree[ReadNode].Kind == TemplateTemplate && "Unexpected kind.");
FromTD = FlatTree[ReadNode].FromArgInfo.TD;
ToTD = FlatTree[ReadNode].ToArgInfo.TD;
}
void GetIntegerDiff(llvm::APSInt &FromInt, llvm::APSInt &ToInt,
bool &IsValidFromInt, bool &IsValidToInt,
QualType &FromIntType, QualType &ToIntType,
Expr *&FromExpr, Expr *&ToExpr) {
assert(FlatTree[ReadNode].Kind == Integer && "Unexpected kind.");
FromInt = FlatTree[ReadNode].FromArgInfo.Val;
ToInt = FlatTree[ReadNode].ToArgInfo.Val;
IsValidFromInt = FlatTree[ReadNode].FromArgInfo.IsValidInt;
IsValidToInt = FlatTree[ReadNode].ToArgInfo.IsValidInt;
FromIntType = FlatTree[ReadNode].FromArgInfo.ArgType;
ToIntType = FlatTree[ReadNode].ToArgInfo.ArgType;
FromExpr = FlatTree[ReadNode].FromArgInfo.ArgExpr;
ToExpr = FlatTree[ReadNode].ToArgInfo.ArgExpr;
}
void GetDeclarationDiff(ValueDecl *&FromValueDecl, ValueDecl *&ToValueDecl,
bool &FromAddressOf, bool &ToAddressOf,
bool &FromNullPtr, bool &ToNullPtr, Expr *&FromExpr,
Expr *&ToExpr) {
assert(FlatTree[ReadNode].Kind == Declaration && "Unexpected kind.");
FromValueDecl = FlatTree[ReadNode].FromArgInfo.VD;
ToValueDecl = FlatTree[ReadNode].ToArgInfo.VD;
FromAddressOf = FlatTree[ReadNode].FromArgInfo.NeedAddressOf;
ToAddressOf = FlatTree[ReadNode].ToArgInfo.NeedAddressOf;
FromNullPtr = FlatTree[ReadNode].FromArgInfo.IsNullPtr;
ToNullPtr = FlatTree[ReadNode].ToArgInfo.IsNullPtr;
FromExpr = FlatTree[ReadNode].FromArgInfo.ArgExpr;
ToExpr = FlatTree[ReadNode].ToArgInfo.ArgExpr;
}
void GetFromDeclarationAndToIntegerDiff(
ValueDecl *&FromValueDecl, bool &FromAddressOf, bool &FromNullPtr,
Expr *&FromExpr, llvm::APSInt &ToInt, bool &IsValidToInt,
QualType &ToIntType, Expr *&ToExpr) {
assert(FlatTree[ReadNode].Kind == FromDeclarationAndToInteger &&
"Unexpected kind.");
FromValueDecl = FlatTree[ReadNode].FromArgInfo.VD;
FromAddressOf = FlatTree[ReadNode].FromArgInfo.NeedAddressOf;
FromNullPtr = FlatTree[ReadNode].FromArgInfo.IsNullPtr;
FromExpr = FlatTree[ReadNode].FromArgInfo.ArgExpr;
ToInt = FlatTree[ReadNode].ToArgInfo.Val;
IsValidToInt = FlatTree[ReadNode].ToArgInfo.IsValidInt;
ToIntType = FlatTree[ReadNode].ToArgInfo.ArgType;
ToExpr = FlatTree[ReadNode].ToArgInfo.ArgExpr;
}
void GetFromIntegerAndToDeclarationDiff(
llvm::APSInt &FromInt, bool &IsValidFromInt, QualType &FromIntType,
Expr *&FromExpr, ValueDecl *&ToValueDecl, bool &ToAddressOf,
bool &ToNullPtr, Expr *&ToExpr) {
assert(FlatTree[ReadNode].Kind == FromIntegerAndToDeclaration &&
"Unexpected kind.");
FromInt = FlatTree[ReadNode].FromArgInfo.Val;
IsValidFromInt = FlatTree[ReadNode].FromArgInfo.IsValidInt;
FromIntType = FlatTree[ReadNode].FromArgInfo.ArgType;
FromExpr = FlatTree[ReadNode].FromArgInfo.ArgExpr;
ToValueDecl = FlatTree[ReadNode].ToArgInfo.VD;
ToAddressOf = FlatTree[ReadNode].ToArgInfo.NeedAddressOf;
ToNullPtr = FlatTree[ReadNode].ToArgInfo.IsNullPtr;
ToExpr = FlatTree[ReadNode].ToArgInfo.ArgExpr;
}
bool FromDefault() {
return FlatTree[ReadNode].FromArgInfo.IsDefault;
}
bool ToDefault() {
return FlatTree[ReadNode].ToArgInfo.IsDefault;
}
bool NodeIsSame() {
return FlatTree[ReadNode].Same;
}
bool HasChildren() {
return FlatTree[ReadNode].ChildNode != 0;
}
void MoveToChild() {
ReadNode = FlatTree[ReadNode].ChildNode;
}
bool AdvanceSibling() {
if (FlatTree[ReadNode].NextNode == 0)
return false;
ReadNode = FlatTree[ReadNode].NextNode;
return true;
}
bool HasNextSibling() {
return FlatTree[ReadNode].NextNode != 0;
}
bool Empty() {
return GetKind() == Invalid;
}
DiffKind GetKind() {
return FlatTree[ReadNode].Kind;
}
};
DiffTree Tree;
class TSTiterator {
typedef const TemplateArgument& reference;
typedef const TemplateArgument* pointer;
struct InternalIterator {
const TemplateSpecializationType *TST;
unsigned Index;
TemplateArgument::pack_iterator CurrentTA;
TemplateArgument::pack_iterator EndTA;
InternalIterator(const TemplateSpecializationType *TST)
: TST(TST), Index(0), CurrentTA(nullptr), EndTA(nullptr) {
if (!TST) return;
if (isEnd()) return;
TemplateArgument TA = TST->getArg(0);
if (TA.getKind() != TemplateArgument::Pack) return;
CurrentTA = TA.pack_begin();
EndTA = TA.pack_end();
if (CurrentTA != EndTA) return;
++(*this);
}
bool isValid() const { return TST; }
bool isEnd() const {
assert(TST && "InternalIterator is invalid with a null TST.");
return Index >= TST->getNumArgs();
}
InternalIterator &operator++() {
assert(TST && "InternalIterator is invalid with a null TST.");
if (isEnd()) {
return *this;
}
if (CurrentTA != EndTA) {
++CurrentTA;
if (CurrentTA != EndTA)
return *this;
}
while (true) {
if (++Index == TST->getNumArgs())
break;
TemplateArgument TA = TST->getArg(Index);
if (TA.getKind() != TemplateArgument::Pack)
break;
CurrentTA = TA.pack_begin();
EndTA = TA.pack_end();
if (CurrentTA != EndTA)
break;
}
return *this;
}
reference operator*() const {
assert(TST && "InternalIterator is invalid with a null TST.");
assert(!isEnd() && "Index exceeds number of arguments.");
if (CurrentTA == EndTA)
return TST->getArg(Index);
else
return *CurrentTA;
}
pointer operator->() const {
assert(TST && "InternalIterator is invalid with a null TST.");
return &operator*();
}
};
InternalIterator SugaredIterator;
InternalIterator DesugaredIterator;
public:
TSTiterator(ASTContext &Context, const TemplateSpecializationType *TST)
: SugaredIterator(TST),
DesugaredIterator(
(TST->isSugared() && !TST->isTypeAlias())
? GetTemplateSpecializationType(Context, TST->desugar())
: nullptr) {}
TSTiterator &operator++() {
++SugaredIterator;
if (DesugaredIterator.isValid())
++DesugaredIterator;
return *this;
}
reference operator*() const {
return *SugaredIterator;
}
pointer operator->() const {
return &operator*();
}
bool isEnd() const {
return SugaredIterator.isEnd();
}
bool hasDesugaredTA() const {
return DesugaredIterator.isValid() && !DesugaredIterator.isEnd();
}
reference getDesugaredTA() const {
assert(DesugaredIterator.isValid() &&
"Desugared TemplateArgument should not be used.");
return *DesugaredIterator;
}
};
static const TemplateSpecializationType *GetTemplateSpecializationType(
ASTContext &Context, QualType Ty) {
if (const TemplateSpecializationType *TST =
Ty->getAs<TemplateSpecializationType>())
return TST;
if (const auto* SubstType = Ty->getAs<SubstTemplateTypeParmType>())
Ty = SubstType->getReplacementType();
const RecordType *RT = Ty->getAs<RecordType>();
if (!RT)
return nullptr;
const ClassTemplateSpecializationDecl *CTSD =
dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl());
if (!CTSD)
return nullptr;
Ty = Context.getTemplateSpecializationType(
TemplateName(CTSD->getSpecializedTemplate()),
CTSD->getTemplateArgs().asArray(),
Ty.getLocalUnqualifiedType().getCanonicalType());
return Ty->getAs<TemplateSpecializationType>();
}
static bool OnlyPerformTypeDiff(ASTContext &Context, QualType FromType,
QualType ToType,
const TemplateSpecializationType *&FromArgTST,
const TemplateSpecializationType *&ToArgTST) {
if (FromType.isNull() || ToType.isNull())
return true;
if (Context.hasSameType(FromType, ToType))
return true;
FromArgTST = GetTemplateSpecializationType(Context, FromType);
ToArgTST = GetTemplateSpecializationType(Context, ToType);
if (!FromArgTST || !ToArgTST)
return true;
if (!hasSameTemplate(FromArgTST, ToArgTST))
return true;
return false;
}
void DiffTypes(const TSTiterator &FromIter, const TSTiterator &ToIter) {
QualType FromType = GetType(FromIter);
QualType ToType = GetType(ToIter);
bool FromDefault = FromIter.isEnd() && !FromType.isNull();
bool ToDefault = ToIter.isEnd() && !ToType.isNull();
const TemplateSpecializationType *FromArgTST = nullptr;
const TemplateSpecializationType *ToArgTST = nullptr;
if (OnlyPerformTypeDiff(Context, FromType, ToType, FromArgTST, ToArgTST)) {
Tree.SetTypeDiff(FromType, ToType, FromDefault, ToDefault);
Tree.SetSame(!FromType.isNull() && !ToType.isNull() &&
Context.hasSameType(FromType, ToType));
} else {
assert(FromArgTST && ToArgTST &&
"Both template specializations need to be valid.");
Qualifiers FromQual = FromType.getQualifiers(),
ToQual = ToType.getQualifiers();
FromQual -= QualType(FromArgTST, 0).getQualifiers();
ToQual -= QualType(ToArgTST, 0).getQualifiers();
Tree.SetTemplateDiff(FromArgTST->getTemplateName().getAsTemplateDecl(),
ToArgTST->getTemplateName().getAsTemplateDecl(),
FromQual, ToQual, FromDefault, ToDefault);
DiffTemplate(FromArgTST, ToArgTST);
}
}
void DiffTemplateTemplates(const TSTiterator &FromIter,
const TSTiterator &ToIter) {
TemplateDecl *FromDecl = GetTemplateDecl(FromIter);
TemplateDecl *ToDecl = GetTemplateDecl(ToIter);
Tree.SetTemplateTemplateDiff(FromDecl, ToDecl, FromIter.isEnd() && FromDecl,
ToIter.isEnd() && ToDecl);
Tree.SetSame(FromDecl && ToDecl &&
FromDecl->getCanonicalDecl() == ToDecl->getCanonicalDecl());
}
static void InitializeNonTypeDiffVariables(ASTContext &Context,
const TSTiterator &Iter,
NonTypeTemplateParmDecl *Default,
llvm::APSInt &Value, bool &HasInt,
QualType &IntType, bool &IsNullPtr,
Expr *&E, ValueDecl *&VD,
bool &NeedAddressOf) {
if (!Iter.isEnd()) {
switch (Iter->getKind()) {
default:
llvm_unreachable("unknown ArgumentKind");
case TemplateArgument::Integral:
Value = Iter->getAsIntegral();
HasInt = true;
IntType = Iter->getIntegralType();
return;
case TemplateArgument::Declaration: {
VD = Iter->getAsDecl();
QualType ArgType = Iter->getParamTypeForDecl();
QualType VDType = VD->getType();
if (ArgType->isPointerType() &&
Context.hasSameType(ArgType->getPointeeType(), VDType))
NeedAddressOf = true;
return;
}
case TemplateArgument::NullPtr:
IsNullPtr = true;
return;
case TemplateArgument::Expression:
E = Iter->getAsExpr();
}
} else if (!Default->isParameterPack()) {
E = Default->getDefaultArgument();
}
if (!Iter.hasDesugaredTA()) return;
const TemplateArgument& TA = Iter.getDesugaredTA();
switch (TA.getKind()) {
default:
llvm_unreachable("unknown ArgumentKind");
case TemplateArgument::Integral:
Value = TA.getAsIntegral();
HasInt = true;
IntType = TA.getIntegralType();
return;
case TemplateArgument::Declaration: {
VD = TA.getAsDecl();
QualType ArgType = TA.getParamTypeForDecl();
QualType VDType = VD->getType();
if (ArgType->isPointerType() &&
Context.hasSameType(ArgType->getPointeeType(), VDType))
NeedAddressOf = true;
return;
}
case TemplateArgument::NullPtr:
IsNullPtr = true;
return;
case TemplateArgument::Expression:
if (!E)
E = TA.getAsExpr();
return;
}
}
void DiffNonTypes(const TSTiterator &FromIter, const TSTiterator &ToIter,
NonTypeTemplateParmDecl *FromDefaultNonTypeDecl,
NonTypeTemplateParmDecl *ToDefaultNonTypeDecl) {
Expr *FromExpr = nullptr, *ToExpr = nullptr;
llvm::APSInt FromInt, ToInt;
QualType FromIntType, ToIntType;
ValueDecl *FromValueDecl = nullptr, *ToValueDecl = nullptr;
bool HasFromInt = false, HasToInt = false, FromNullPtr = false,
ToNullPtr = false, NeedFromAddressOf = false, NeedToAddressOf = false;
InitializeNonTypeDiffVariables(
Context, FromIter, FromDefaultNonTypeDecl, FromInt, HasFromInt,
FromIntType, FromNullPtr, FromExpr, FromValueDecl, NeedFromAddressOf);
InitializeNonTypeDiffVariables(Context, ToIter, ToDefaultNonTypeDecl, ToInt,
HasToInt, ToIntType, ToNullPtr, ToExpr,
ToValueDecl, NeedToAddressOf);
bool FromDefault = FromIter.isEnd() &&
(FromExpr || FromValueDecl || HasFromInt || FromNullPtr);
bool ToDefault = ToIter.isEnd() &&
(ToExpr || ToValueDecl || HasToInt || ToNullPtr);
bool FromDeclaration = FromValueDecl || FromNullPtr;
bool ToDeclaration = ToValueDecl || ToNullPtr;
if (FromDeclaration && HasToInt) {
Tree.SetFromDeclarationAndToIntegerDiff(
FromValueDecl, NeedFromAddressOf, FromNullPtr, FromExpr, ToInt,
HasToInt, ToIntType, ToExpr, FromDefault, ToDefault);
Tree.SetSame(false);
return;
}
if (HasFromInt && ToDeclaration) {
Tree.SetFromIntegerAndToDeclarationDiff(
FromInt, HasFromInt, FromIntType, FromExpr, ToValueDecl,
NeedToAddressOf, ToNullPtr, ToExpr, FromDefault, ToDefault);
Tree.SetSame(false);
return;
}
if (HasFromInt || HasToInt) {
Tree.SetIntegerDiff(FromInt, ToInt, HasFromInt, HasToInt, FromIntType,
ToIntType, FromExpr, ToExpr, FromDefault, ToDefault);
if (HasFromInt && HasToInt) {
Tree.SetSame(Context.hasSameType(FromIntType, ToIntType) &&
FromInt == ToInt);
}
return;
}
if (FromDeclaration || ToDeclaration) {
Tree.SetDeclarationDiff(FromValueDecl, ToValueDecl, NeedFromAddressOf,
NeedToAddressOf, FromNullPtr, ToNullPtr, FromExpr,
ToExpr, FromDefault, ToDefault);
bool BothNull = FromNullPtr && ToNullPtr;
bool SameValueDecl =
FromValueDecl && ToValueDecl &&
NeedFromAddressOf == NeedToAddressOf &&
FromValueDecl->getCanonicalDecl() == ToValueDecl->getCanonicalDecl();
Tree.SetSame(BothNull || SameValueDecl);
return;
}
assert((FromExpr || ToExpr) && "Both template arguments cannot be empty.");
Tree.SetExpressionDiff(FromExpr, ToExpr, FromDefault, ToDefault);
Tree.SetSame(IsEqualExpr(Context, FromExpr, ToExpr));
}
void DiffTemplate(const TemplateSpecializationType *FromTST,
const TemplateSpecializationType *ToTST) {
TemplateParameterList *ParamsFrom =
FromTST->getTemplateName().getAsTemplateDecl()->getTemplateParameters();
TemplateParameterList *ParamsTo =
ToTST->getTemplateName().getAsTemplateDecl()->getTemplateParameters();
unsigned TotalArgs = 0;
for (TSTiterator FromIter(Context, FromTST), ToIter(Context, ToTST);
!FromIter.isEnd() || !ToIter.isEnd(); ++TotalArgs) {
Tree.AddNode();
unsigned FromParamIndex = std::min(TotalArgs, ParamsFrom->size() - 1);
unsigned ToParamIndex = std::min(TotalArgs, ParamsTo->size() - 1);
NamedDecl *FromParamND = ParamsFrom->getParam(FromParamIndex);
NamedDecl *ToParamND = ParamsTo->getParam(ToParamIndex);
assert(FromParamND->getKind() == ToParamND->getKind() &&
"Parameter Decl are not the same kind.");
if (isa<TemplateTypeParmDecl>(FromParamND)) {
DiffTypes(FromIter, ToIter);
} else if (isa<TemplateTemplateParmDecl>(FromParamND)) {
DiffTemplateTemplates(FromIter, ToIter);
} else if (isa<NonTypeTemplateParmDecl>(FromParamND)) {
NonTypeTemplateParmDecl *FromDefaultNonTypeDecl =
cast<NonTypeTemplateParmDecl>(FromParamND);
NonTypeTemplateParmDecl *ToDefaultNonTypeDecl =
cast<NonTypeTemplateParmDecl>(ToParamND);
DiffNonTypes(FromIter, ToIter, FromDefaultNonTypeDecl,
ToDefaultNonTypeDecl);
} else {
llvm_unreachable("Unexpected Decl type.");
}
++FromIter;
++ToIter;
Tree.Up();
}
}
static void makeTemplateList(
SmallVectorImpl<const TemplateSpecializationType *> &TemplateList,
const TemplateSpecializationType *TST) {
while (TST) {
TemplateList.push_back(TST);
if (!TST->isTypeAlias())
return;
TST = TST->getAliasedType()->getAs<TemplateSpecializationType>();
}
}
static bool hasSameBaseTemplate(const TemplateSpecializationType *FromTST,
const TemplateSpecializationType *ToTST) {
return FromTST->getTemplateName().getAsTemplateDecl()->getCanonicalDecl() ==
ToTST->getTemplateName().getAsTemplateDecl()->getCanonicalDecl();
}
static bool hasSameTemplate(const TemplateSpecializationType *&FromTST,
const TemplateSpecializationType *&ToTST) {
if (hasSameBaseTemplate(FromTST, ToTST))
return true;
SmallVector<const TemplateSpecializationType*, 1> FromTemplateList,
ToTemplateList;
makeTemplateList(FromTemplateList, FromTST);
makeTemplateList(ToTemplateList, ToTST);
SmallVectorImpl<const TemplateSpecializationType *>::reverse_iterator
FromIter = FromTemplateList.rbegin(), FromEnd = FromTemplateList.rend(),
ToIter = ToTemplateList.rbegin(), ToEnd = ToTemplateList.rend();
if (!hasSameBaseTemplate(*FromIter, *ToIter))
return false;
for (; FromIter != FromEnd && ToIter != ToEnd; ++FromIter, ++ToIter) {
if (!hasSameBaseTemplate(*FromIter, *ToIter))
break;
}
FromTST = FromIter[-1];
ToTST = ToIter[-1];
return true;
}
static QualType GetType(const TSTiterator &Iter) {
if (!Iter.isEnd())
return Iter->getAsType();
if (Iter.hasDesugaredTA())
return Iter.getDesugaredTA().getAsType();
return QualType();
}
static TemplateDecl *GetTemplateDecl(const TSTiterator &Iter) {
if (!Iter.isEnd())
return Iter->getAsTemplate().getAsTemplateDecl();
if (Iter.hasDesugaredTA())
return Iter.getDesugaredTA().getAsTemplate().getAsTemplateDecl();
return nullptr;
}
static bool IsEqualExpr(ASTContext &Context, Expr *FromExpr, Expr *ToExpr) {
if (FromExpr == ToExpr)
return true;
if (!FromExpr || !ToExpr)
return false;
llvm::FoldingSetNodeID FromID, ToID;
FromExpr->Profile(FromID, Context, true);
ToExpr->Profile(ToID, Context, true);
return FromID == ToID;
}
void TreeToString(int Indent = 1) {
if (PrintTree) {
OS << '\n';
OS.indent(2 * Indent);
++Indent;
}
switch (Tree.GetKind()) {
case DiffTree::Invalid:
llvm_unreachable("Template diffing failed with bad DiffNode");
case DiffTree::Type: {
QualType FromType, ToType;
Tree.GetTypeDiff(FromType, ToType);
PrintTypeNames(FromType, ToType, Tree.FromDefault(), Tree.ToDefault(),
Tree.NodeIsSame());
return;
}
case DiffTree::Expression: {
Expr *FromExpr, *ToExpr;
Tree.GetExpressionDiff(FromExpr, ToExpr);
PrintExpr(FromExpr, ToExpr, Tree.FromDefault(), Tree.ToDefault(),
Tree.NodeIsSame());
return;
}
case DiffTree::TemplateTemplate: {
TemplateDecl *FromTD, *ToTD;
Tree.GetTemplateTemplateDiff(FromTD, ToTD);
PrintTemplateTemplate(FromTD, ToTD, Tree.FromDefault(),
Tree.ToDefault(), Tree.NodeIsSame());
return;
}
case DiffTree::Integer: {
llvm::APSInt FromInt, ToInt;
Expr *FromExpr, *ToExpr;
bool IsValidFromInt, IsValidToInt;
QualType FromIntType, ToIntType;
Tree.GetIntegerDiff(FromInt, ToInt, IsValidFromInt, IsValidToInt,
FromIntType, ToIntType, FromExpr, ToExpr);
PrintAPSInt(FromInt, ToInt, IsValidFromInt, IsValidToInt, FromIntType,
ToIntType, FromExpr, ToExpr, Tree.FromDefault(),
Tree.ToDefault(), Tree.NodeIsSame());
return;
}
case DiffTree::Declaration: {
ValueDecl *FromValueDecl, *ToValueDecl;
bool FromAddressOf, ToAddressOf;
bool FromNullPtr, ToNullPtr;
Expr *FromExpr, *ToExpr;
Tree.GetDeclarationDiff(FromValueDecl, ToValueDecl, FromAddressOf,
ToAddressOf, FromNullPtr, ToNullPtr, FromExpr,
ToExpr);
PrintValueDecl(FromValueDecl, ToValueDecl, FromAddressOf, ToAddressOf,
FromNullPtr, ToNullPtr, FromExpr, ToExpr,
Tree.FromDefault(), Tree.ToDefault(), Tree.NodeIsSame());
return;
}
case DiffTree::FromDeclarationAndToInteger: {
ValueDecl *FromValueDecl;
bool FromAddressOf;
bool FromNullPtr;
Expr *FromExpr;
llvm::APSInt ToInt;
bool IsValidToInt;
QualType ToIntType;
Expr *ToExpr;
Tree.GetFromDeclarationAndToIntegerDiff(
FromValueDecl, FromAddressOf, FromNullPtr, FromExpr, ToInt,
IsValidToInt, ToIntType, ToExpr);
assert((FromValueDecl || FromNullPtr) && IsValidToInt);
PrintValueDeclAndInteger(FromValueDecl, FromAddressOf, FromNullPtr,
FromExpr, Tree.FromDefault(), ToInt, ToIntType,
ToExpr, Tree.ToDefault());
return;
}
case DiffTree::FromIntegerAndToDeclaration: {
llvm::APSInt FromInt;
bool IsValidFromInt;
QualType FromIntType;
Expr *FromExpr;
ValueDecl *ToValueDecl;
bool ToAddressOf;
bool ToNullPtr;
Expr *ToExpr;
Tree.GetFromIntegerAndToDeclarationDiff(
FromInt, IsValidFromInt, FromIntType, FromExpr, ToValueDecl,
ToAddressOf, ToNullPtr, ToExpr);
assert(IsValidFromInt && (ToValueDecl || ToNullPtr));
PrintIntegerAndValueDecl(FromInt, FromIntType, FromExpr,
Tree.FromDefault(), ToValueDecl, ToAddressOf,
ToNullPtr, ToExpr, Tree.ToDefault());
return;
}
case DiffTree::Template: {
TemplateDecl *FromTD, *ToTD;
Qualifiers FromQual, ToQual;
Tree.GetTemplateDiff(FromTD, ToTD, FromQual, ToQual);
PrintQualifiers(FromQual, ToQual);
if (!Tree.HasChildren()) {
OS << FromTD->getDeclName() << "<>";
return;
}
OS << FromTD->getDeclName() << '<';
Tree.MoveToChild();
unsigned NumElideArgs = 0;
bool AllArgsElided = true;
do {
if (ElideType) {
if (Tree.NodeIsSame()) {
++NumElideArgs;
continue;
}
AllArgsElided = false;
if (NumElideArgs > 0) {
PrintElideArgs(NumElideArgs, Indent);
NumElideArgs = 0;
OS << ", ";
}
}
TreeToString(Indent);
if (Tree.HasNextSibling())
OS << ", ";
} while (Tree.AdvanceSibling());
if (NumElideArgs > 0) {
if (AllArgsElided)
OS << "...";
else
PrintElideArgs(NumElideArgs, Indent);
}
Tree.Parent();
OS << ">";
return;
}
}
}
void Bold() {
assert(!IsBold && "Attempting to bold text that is already bold.");
IsBold = true;
if (ShowColor)
OS << ToggleHighlight;
}
void Unbold() {
assert(IsBold && "Attempting to remove bold from unbold text.");
IsBold = false;
if (ShowColor)
OS << ToggleHighlight;
}
void PrintTypeNames(QualType FromType, QualType ToType,
bool FromDefault, bool ToDefault, bool Same) {
assert((!FromType.isNull() || !ToType.isNull()) &&
"Only one template argument may be missing.");
if (Same) {
OS << FromType.getAsString(Policy);
return;
}
if (!FromType.isNull() && !ToType.isNull() &&
FromType.getLocalUnqualifiedType() ==
ToType.getLocalUnqualifiedType()) {
Qualifiers FromQual = FromType.getLocalQualifiers(),
ToQual = ToType.getLocalQualifiers();
PrintQualifiers(FromQual, ToQual);
FromType.getLocalUnqualifiedType().print(OS, Policy);
return;
}
std::string FromTypeStr = FromType.isNull() ? "(no argument)"
: FromType.getAsString(Policy);
std::string ToTypeStr = ToType.isNull() ? "(no argument)"
: ToType.getAsString(Policy);
if (FromTypeStr == ToTypeStr) {
std::string FromCanTypeStr =
FromType.getCanonicalType().getAsString(Policy);
std::string ToCanTypeStr = ToType.getCanonicalType().getAsString(Policy);
if (FromCanTypeStr != ToCanTypeStr) {
FromTypeStr = FromCanTypeStr;
ToTypeStr = ToCanTypeStr;
}
}
if (PrintTree) OS << '[';
OS << (FromDefault ? "(default) " : "");
Bold();
OS << FromTypeStr;
Unbold();
if (PrintTree) {
OS << " != " << (ToDefault ? "(default) " : "");
Bold();
OS << ToTypeStr;
Unbold();
OS << "]";
}
}
void PrintExpr(const Expr *FromExpr, const Expr *ToExpr, bool FromDefault,
bool ToDefault, bool Same) {
assert((FromExpr || ToExpr) &&
"Only one template argument may be missing.");
if (Same) {
PrintExpr(FromExpr);
} else if (!PrintTree) {
OS << (FromDefault ? "(default) " : "");
Bold();
PrintExpr(FromExpr);
Unbold();
} else {
OS << (FromDefault ? "[(default) " : "[");
Bold();
PrintExpr(FromExpr);
Unbold();
OS << " != " << (ToDefault ? "(default) " : "");
Bold();
PrintExpr(ToExpr);
Unbold();
OS << ']';
}
}
void PrintExpr(const Expr *E) {
if (E) {
E->printPretty(OS, nullptr, Policy);
return;
}
OS << "(no argument)";
}
void PrintTemplateTemplate(TemplateDecl *FromTD, TemplateDecl *ToTD,
bool FromDefault, bool ToDefault, bool Same) {
assert((FromTD || ToTD) && "Only one template argument may be missing.");
std::string FromName =
std::string(FromTD ? FromTD->getName() : "(no argument)");
std::string ToName = std::string(ToTD ? ToTD->getName() : "(no argument)");
if (FromTD && ToTD && FromName == ToName) {
FromName = FromTD->getQualifiedNameAsString();
ToName = ToTD->getQualifiedNameAsString();
}
if (Same) {
OS << "template " << FromTD->getDeclName();
} else if (!PrintTree) {
OS << (FromDefault ? "(default) template " : "template ");
Bold();
OS << FromName;
Unbold();
} else {
OS << (FromDefault ? "[(default) template " : "[template ");
Bold();
OS << FromName;
Unbold();
OS << " != " << (ToDefault ? "(default) template " : "template ");
Bold();
OS << ToName;
Unbold();
OS << ']';
}
}
void PrintAPSInt(const llvm::APSInt &FromInt, const llvm::APSInt &ToInt,
bool IsValidFromInt, bool IsValidToInt, QualType FromIntType,
QualType ToIntType, Expr *FromExpr, Expr *ToExpr,
bool FromDefault, bool ToDefault, bool Same) {
assert((IsValidFromInt || IsValidToInt) &&
"Only one integral argument may be missing.");
if (Same) {
if (FromIntType->isBooleanType()) {
OS << ((FromInt == 0) ? "false" : "true");
} else {
OS << toString(FromInt, 10);
}
return;
}
bool PrintType = IsValidFromInt && IsValidToInt &&
!Context.hasSameType(FromIntType, ToIntType);
if (!PrintTree) {
OS << (FromDefault ? "(default) " : "");
PrintAPSInt(FromInt, FromExpr, IsValidFromInt, FromIntType, PrintType);
} else {
OS << (FromDefault ? "[(default) " : "[");
PrintAPSInt(FromInt, FromExpr, IsValidFromInt, FromIntType, PrintType);
OS << " != " << (ToDefault ? "(default) " : "");
PrintAPSInt(ToInt, ToExpr, IsValidToInt, ToIntType, PrintType);
OS << ']';
}
}
void PrintAPSInt(const llvm::APSInt &Val, Expr *E, bool Valid,
QualType IntType, bool PrintType) {
Bold();
if (Valid) {
if (HasExtraInfo(E)) {
PrintExpr(E);
Unbold();
OS << " aka ";
Bold();
}
if (PrintType) {
Unbold();
OS << "(";
Bold();
IntType.print(OS, Context.getPrintingPolicy());
Unbold();
OS << ") ";
Bold();
}
if (IntType->isBooleanType()) {
OS << ((Val == 0) ? "false" : "true");
} else {
OS << toString(Val, 10);
}
} else if (E) {
PrintExpr(E);
} else {
OS << "(no argument)";
}
Unbold();
}
bool HasExtraInfo(Expr *E) {
if (!E) return false;
E = E->IgnoreImpCasts();
if (isa<IntegerLiteral>(E)) return false;
if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E))
if (UO->getOpcode() == UO_Minus)
if (isa<IntegerLiteral>(UO->getSubExpr()))
return false;
if (isa<CXXBoolLiteralExpr>(E))
return false;
return true;
}
void PrintValueDecl(ValueDecl *VD, bool AddressOf, Expr *E, bool NullPtr) {
if (VD) {
if (AddressOf)
OS << "&";
else if (auto *TPO = dyn_cast<TemplateParamObjectDecl>(VD)) {
TPO->printAsInit(OS, Policy);
return;
}
VD->printName(OS);
return;
}
if (NullPtr) {
if (E && !isa<CXXNullPtrLiteralExpr>(E)) {
PrintExpr(E);
if (IsBold) {
Unbold();
OS << " aka ";
Bold();
} else {
OS << " aka ";
}
}
OS << "nullptr";
return;
}
OS << "(no argument)";
}
void PrintValueDecl(ValueDecl *FromValueDecl, ValueDecl *ToValueDecl,
bool FromAddressOf, bool ToAddressOf, bool FromNullPtr,
bool ToNullPtr, Expr *FromExpr, Expr *ToExpr,
bool FromDefault, bool ToDefault, bool Same) {
assert((FromValueDecl || FromNullPtr || ToValueDecl || ToNullPtr) &&
"Only one Decl argument may be NULL");
if (Same) {
PrintValueDecl(FromValueDecl, FromAddressOf, FromExpr, FromNullPtr);
} else if (!PrintTree) {
OS << (FromDefault ? "(default) " : "");
Bold();
PrintValueDecl(FromValueDecl, FromAddressOf, FromExpr, FromNullPtr);
Unbold();
} else {
OS << (FromDefault ? "[(default) " : "[");
Bold();
PrintValueDecl(FromValueDecl, FromAddressOf, FromExpr, FromNullPtr);
Unbold();
OS << " != " << (ToDefault ? "(default) " : "");
Bold();
PrintValueDecl(ToValueDecl, ToAddressOf, ToExpr, ToNullPtr);
Unbold();
OS << ']';
}
}
void PrintValueDeclAndInteger(ValueDecl *VD, bool NeedAddressOf,
bool IsNullPtr, Expr *VDExpr, bool DefaultDecl,
const llvm::APSInt &Val, QualType IntType,
Expr *IntExpr, bool DefaultInt) {
if (!PrintTree) {
OS << (DefaultDecl ? "(default) " : "");
Bold();
PrintValueDecl(VD, NeedAddressOf, VDExpr, IsNullPtr);
Unbold();
} else {
OS << (DefaultDecl ? "[(default) " : "[");
Bold();
PrintValueDecl(VD, NeedAddressOf, VDExpr, IsNullPtr);
Unbold();
OS << " != " << (DefaultInt ? "(default) " : "");
PrintAPSInt(Val, IntExpr, true , IntType, false );
OS << ']';
}
}
void PrintIntegerAndValueDecl(const llvm::APSInt &Val, QualType IntType,
Expr *IntExpr, bool DefaultInt, ValueDecl *VD,
bool NeedAddressOf, bool IsNullPtr,
Expr *VDExpr, bool DefaultDecl) {
if (!PrintTree) {
OS << (DefaultInt ? "(default) " : "");
PrintAPSInt(Val, IntExpr, true , IntType, false );
} else {
OS << (DefaultInt ? "[(default) " : "[");
PrintAPSInt(Val, IntExpr, true , IntType, false );
OS << " != " << (DefaultDecl ? "(default) " : "");
Bold();
PrintValueDecl(VD, NeedAddressOf, VDExpr, IsNullPtr);
Unbold();
OS << ']';
}
}
void PrintElideArgs(unsigned NumElideArgs, unsigned Indent) {
if (PrintTree) {
OS << '\n';
for (unsigned i = 0; i < Indent; ++i)
OS << " ";
}
if (NumElideArgs == 0) return;
if (NumElideArgs == 1)
OS << "[...]";
else
OS << "[" << NumElideArgs << " * ...]";
}
void PrintQualifiers(Qualifiers FromQual, Qualifiers ToQual) {
if (FromQual.empty() && ToQual.empty())
return;
if (FromQual == ToQual) {
PrintQualifier(FromQual, false);
return;
}
Qualifiers CommonQual = Qualifiers::removeCommonQualifiers(FromQual,
ToQual);
if (PrintTree) {
OS << "[";
if (CommonQual.empty() && FromQual.empty()) {
Bold();
OS << "(no qualifiers) ";
Unbold();
} else {
PrintQualifier(CommonQual, false);
PrintQualifier(FromQual, true);
}
OS << "!= ";
if (CommonQual.empty() && ToQual.empty()) {
Bold();
OS << "(no qualifiers)";
Unbold();
} else {
PrintQualifier(CommonQual, false,
!ToQual.empty());
PrintQualifier(ToQual, true,
false);
}
OS << "] ";
} else {
PrintQualifier(CommonQual, false);
PrintQualifier(FromQual, true);
}
}
void PrintQualifier(Qualifiers Q, bool ApplyBold,
bool AppendSpaceIfNonEmpty = true) {
if (Q.empty()) return;
if (ApplyBold) Bold();
Q.print(OS, Policy, AppendSpaceIfNonEmpty);
if (ApplyBold) Unbold();
}
public:
TemplateDiff(raw_ostream &OS, ASTContext &Context, QualType FromType,
QualType ToType, bool PrintTree, bool PrintFromType,
bool ElideType, bool ShowColor)
: Context(Context),
Policy(Context.getLangOpts()),
ElideType(ElideType),
PrintTree(PrintTree),
ShowColor(ShowColor),
FromTemplateType(PrintFromType ? FromType : ToType),
ToTemplateType(PrintFromType ? ToType : FromType),
OS(OS),
IsBold(false) {
}
void DiffTemplate() {
Qualifiers FromQual = FromTemplateType.getQualifiers(),
ToQual = ToTemplateType.getQualifiers();
const TemplateSpecializationType *FromOrigTST =
GetTemplateSpecializationType(Context, FromTemplateType);
const TemplateSpecializationType *ToOrigTST =
GetTemplateSpecializationType(Context, ToTemplateType);
if (!FromOrigTST || !ToOrigTST)
return;
if (!hasSameTemplate(FromOrigTST, ToOrigTST)) {
return;
}
FromQual -= QualType(FromOrigTST, 0).getQualifiers();
ToQual -= QualType(ToOrigTST, 0).getQualifiers();
Tree.SetTemplateDiff(FromOrigTST->getTemplateName().getAsTemplateDecl(),
ToOrigTST->getTemplateName().getAsTemplateDecl(),
FromQual, ToQual, false ,
false );
DiffTemplate(FromOrigTST, ToOrigTST);
}
bool Emit() {
Tree.StartTraverse();
if (Tree.Empty())
return false;
TreeToString();
assert(!IsBold && "Bold is applied to end of string.");
return true;
}
}; }
static bool FormatTemplateTypeDiff(ASTContext &Context, QualType FromType,
QualType ToType, bool PrintTree,
bool PrintFromType, bool ElideType,
bool ShowColors, raw_ostream &OS) {
if (PrintTree)
PrintFromType = true;
TemplateDiff TD(OS, Context, FromType, ToType, PrintTree, PrintFromType,
ElideType, ShowColors);
TD.DiffTemplate();
return TD.Emit();
}