2010-02-10 06:26:47 +08:00
|
|
|
//===--- ASTDiagnostic.cpp - Diagnostic Printing Hooks for AST Nodes ------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements a diagnostic formatting hook for AST elements.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "clang/AST/ASTDiagnostic.h"
|
|
|
|
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/DeclObjC.h"
|
|
|
|
#include "clang/AST/Type.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2010-05-13 19:37:24 +08:00
|
|
|
// Returns a desugared version of the QualType, and marks ShouldAKA as true
|
|
|
|
// whenever we remove significant sugar from the type.
|
|
|
|
static QualType Desugar(ASTContext &Context, QualType QT, bool &ShouldAKA) {
|
|
|
|
QualifierCollector QC;
|
|
|
|
|
2010-02-10 06:26:47 +08:00
|
|
|
while (true) {
|
2010-05-13 19:37:24 +08:00
|
|
|
const Type *Ty = QC.strip(QT);
|
|
|
|
|
2010-02-10 06:26:47 +08:00
|
|
|
// Don't aka just because we saw an elaborated type...
|
2011-02-20 11:19:35 +08:00
|
|
|
if (const ElaboratedType *ET = dyn_cast<ElaboratedType>(Ty)) {
|
|
|
|
QT = ET->desugar();
|
2010-02-10 06:26:47 +08:00
|
|
|
continue;
|
|
|
|
}
|
2010-12-11 00:29:40 +08:00
|
|
|
// ... or a paren type ...
|
2011-02-20 11:19:35 +08:00
|
|
|
if (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
|
|
|
|
QT = PT->desugar();
|
2010-12-11 00:29:40 +08:00
|
|
|
continue;
|
|
|
|
}
|
2011-02-20 11:19:35 +08:00
|
|
|
// ...or a substituted template type parameter ...
|
|
|
|
if (const SubstTemplateTypeParmType *ST =
|
|
|
|
dyn_cast<SubstTemplateTypeParmType>(Ty)) {
|
|
|
|
QT = ST->desugar();
|
|
|
|
continue;
|
|
|
|
}
|
2011-03-04 12:00:19 +08:00
|
|
|
// ...or an attributed type...
|
|
|
|
if (const AttributedType *AT = dyn_cast<AttributedType>(Ty)) {
|
|
|
|
QT = AT->desugar();
|
|
|
|
continue;
|
|
|
|
}
|
2011-02-20 11:19:35 +08:00
|
|
|
// ... or an auto type.
|
|
|
|
if (const AutoType *AT = dyn_cast<AutoType>(Ty)) {
|
|
|
|
if (!AT->isSugared())
|
|
|
|
break;
|
|
|
|
QT = AT->desugar();
|
2010-02-10 06:26:47 +08:00
|
|
|
continue;
|
|
|
|
}
|
2010-05-13 19:37:24 +08:00
|
|
|
|
2011-05-06 05:57:07 +08:00
|
|
|
// Don't desugar template specializations, unless it's an alias template.
|
|
|
|
if (const TemplateSpecializationType *TST
|
|
|
|
= dyn_cast<TemplateSpecializationType>(Ty))
|
|
|
|
if (!TST->isTypeAlias())
|
|
|
|
break;
|
2010-05-13 19:37:24 +08:00
|
|
|
|
2010-02-10 06:26:47 +08:00
|
|
|
// Don't desugar magic Objective-C types.
|
|
|
|
if (QualType(Ty,0) == Context.getObjCIdType() ||
|
|
|
|
QualType(Ty,0) == Context.getObjCClassType() ||
|
|
|
|
QualType(Ty,0) == Context.getObjCSelType() ||
|
|
|
|
QualType(Ty,0) == Context.getObjCProtoType())
|
|
|
|
break;
|
2010-05-13 19:37:24 +08:00
|
|
|
|
2010-02-10 06:26:47 +08:00
|
|
|
// Don't desugar va_list.
|
|
|
|
if (QualType(Ty,0) == Context.getBuiltinVaListType())
|
|
|
|
break;
|
2010-05-13 19:37:24 +08:00
|
|
|
|
2010-02-10 06:26:47 +08:00
|
|
|
// Otherwise, do a single-step desugar.
|
|
|
|
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.def"
|
|
|
|
}
|
2010-05-13 19:37:24 +08:00
|
|
|
|
2010-02-10 06:26:47 +08:00
|
|
|
// If it wasn't sugared, we're done.
|
|
|
|
if (!IsSugar)
|
|
|
|
break;
|
2010-05-13 19:37:24 +08:00
|
|
|
|
2010-02-10 06:26:47 +08:00
|
|
|
// If the desugared type is a vector type, we don't want to expand
|
|
|
|
// it, it will turn into an attribute mess. People want their "vec4".
|
|
|
|
if (isa<VectorType>(Underlying))
|
|
|
|
break;
|
2010-05-13 19:37:24 +08:00
|
|
|
|
2010-02-10 06:26:47 +08:00
|
|
|
// Don't desugar through the primary typedef of an anonymous type.
|
2010-09-05 07:16:01 +08:00
|
|
|
if (const TagType *UTT = Underlying->getAs<TagType>())
|
|
|
|
if (const TypedefType *QTT = dyn_cast<TypedefType>(QT))
|
2011-04-15 22:24:37 +08:00
|
|
|
if (UTT->getDecl()->getTypedefNameForAnonDecl() == QTT->getDecl())
|
2010-09-05 07:16:01 +08:00
|
|
|
break;
|
2010-05-13 19:37:24 +08:00
|
|
|
|
|
|
|
// Record that we actually looked through an opaque type here.
|
|
|
|
ShouldAKA = true;
|
2010-02-10 06:26:47 +08:00
|
|
|
QT = Underlying;
|
|
|
|
}
|
2010-05-13 19:37:24 +08:00
|
|
|
|
|
|
|
// If we have a pointer-like type, desugar the pointee as well.
|
|
|
|
// FIXME: Handle other pointer-like types.
|
|
|
|
if (const PointerType *Ty = QT->getAs<PointerType>()) {
|
2010-09-05 07:16:01 +08:00
|
|
|
QT = Context.getPointerType(Desugar(Context, Ty->getPointeeType(),
|
|
|
|
ShouldAKA));
|
2010-05-13 19:37:24 +08:00
|
|
|
} else if (const LValueReferenceType *Ty = QT->getAs<LValueReferenceType>()) {
|
2010-09-05 07:16:01 +08:00
|
|
|
QT = Context.getLValueReferenceType(Desugar(Context, Ty->getPointeeType(),
|
|
|
|
ShouldAKA));
|
2011-01-21 00:08:06 +08:00
|
|
|
} else if (const RValueReferenceType *Ty = QT->getAs<RValueReferenceType>()) {
|
|
|
|
QT = Context.getRValueReferenceType(Desugar(Context, Ty->getPointeeType(),
|
|
|
|
ShouldAKA));
|
2010-02-10 06:26:47 +08:00
|
|
|
}
|
2010-05-13 19:37:24 +08:00
|
|
|
|
2010-12-10 19:01:00 +08:00
|
|
|
return QC.apply(Context, QT);
|
2010-02-10 06:26:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Convert the given type to a string suitable for printing as part of
|
2010-05-13 19:37:24 +08:00
|
|
|
/// a diagnostic.
|
|
|
|
///
|
2011-07-12 01:49:21 +08:00
|
|
|
/// There are four main criteria when determining whether we should have an
|
2010-05-13 19:37:24 +08:00
|
|
|
/// a.k.a. clause when pretty-printing a type:
|
|
|
|
///
|
|
|
|
/// 1) Some types provide very minimal sugar that doesn't impede the
|
|
|
|
/// user's understanding --- for example, elaborated type
|
|
|
|
/// specifiers. If this is all the sugar we see, we don't want an
|
|
|
|
/// a.k.a. clause.
|
|
|
|
/// 2) Some types are technically sugared but are much more familiar
|
|
|
|
/// when seen in their sugared form --- for example, va_list,
|
|
|
|
/// vector types, and the magic Objective C types. We don't
|
|
|
|
/// want to desugar these, even if we do produce an a.k.a. clause.
|
|
|
|
/// 3) Some types may have already been desugared previously in this diagnostic.
|
|
|
|
/// if this is the case, doing another "aka" would just be clutter.
|
2011-07-12 01:49:21 +08:00
|
|
|
/// 4) Two different types within the same diagnostic have the same output
|
|
|
|
/// string. In this case, force an a.k.a with the desugared type when
|
|
|
|
/// doing so will provide additional information.
|
2010-02-10 06:26:47 +08:00
|
|
|
///
|
|
|
|
/// \param Context the context in which the type was allocated
|
|
|
|
/// \param Ty the type to print
|
2011-07-12 01:49:21 +08:00
|
|
|
/// \param QualTypeVals pointer values to QualTypes which are used in the
|
|
|
|
/// diagnostic message
|
2010-02-10 06:26:47 +08:00
|
|
|
static std::string
|
|
|
|
ConvertTypeToDiagnosticString(ASTContext &Context, QualType Ty,
|
2011-09-26 07:23:43 +08:00
|
|
|
const DiagnosticsEngine::ArgumentValue *PrevArgs,
|
2011-07-12 01:49:21 +08:00
|
|
|
unsigned NumPrevArgs,
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVectorImpl<intptr_t> &QualTypeVals) {
|
2010-02-10 06:26:47 +08:00
|
|
|
// FIXME: Playing with std::string is really slow.
|
2011-07-12 01:49:21 +08:00
|
|
|
bool ForceAKA = false;
|
|
|
|
QualType CanTy = Ty.getCanonicalType();
|
2011-09-28 06:38:19 +08:00
|
|
|
std::string S = Ty.getAsString(Context.getPrintingPolicy());
|
|
|
|
std::string CanS = CanTy.getAsString(Context.getPrintingPolicy());
|
2011-07-12 01:49:21 +08:00
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
for (SmallVectorImpl<intptr_t>::iterator I = QualTypeVals.begin(),
|
2011-07-12 01:49:21 +08:00
|
|
|
E = QualTypeVals.end(); I != E; ++I) {
|
|
|
|
QualType CompareTy =
|
|
|
|
QualType::getFromOpaquePtr(reinterpret_cast<void*>(*I));
|
|
|
|
if (CompareTy == Ty)
|
|
|
|
continue; // Same types
|
|
|
|
QualType CompareCanTy = CompareTy.getCanonicalType();
|
|
|
|
if (CompareCanTy == CanTy)
|
|
|
|
continue; // Same canonical types
|
2011-09-28 06:38:19 +08:00
|
|
|
std::string CompareS = CompareTy.getAsString(Context.getPrintingPolicy());
|
2011-07-12 01:49:21 +08:00
|
|
|
if (CompareS != S)
|
|
|
|
continue; // Original strings are different
|
2011-09-28 06:38:19 +08:00
|
|
|
std::string CompareCanS = CompareCanTy.getAsString(Context.getPrintingPolicy());
|
2011-07-12 01:49:21 +08:00
|
|
|
if (CompareCanS == CanS)
|
|
|
|
continue; // No new info from canonical type
|
|
|
|
|
|
|
|
ForceAKA = true;
|
|
|
|
break;
|
|
|
|
}
|
2010-05-13 19:37:24 +08:00
|
|
|
|
|
|
|
// Check to see if we already desugared this type in this
|
|
|
|
// diagnostic. If so, don't do it again.
|
|
|
|
bool Repeated = false;
|
|
|
|
for (unsigned i = 0; i != NumPrevArgs; ++i) {
|
|
|
|
// TODO: Handle ak_declcontext case.
|
2011-09-26 07:23:43 +08:00
|
|
|
if (PrevArgs[i].first == DiagnosticsEngine::ak_qualtype) {
|
2010-05-13 19:37:24 +08:00
|
|
|
void *Ptr = (void*)PrevArgs[i].second;
|
|
|
|
QualType PrevTy(QualType::getFromOpaquePtr(Ptr));
|
|
|
|
if (PrevTy == Ty) {
|
|
|
|
Repeated = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-10 06:26:47 +08:00
|
|
|
// Consider producing an a.k.a. clause if removing all the direct
|
|
|
|
// sugar gives us something "significantly different".
|
2010-05-13 19:37:24 +08:00
|
|
|
if (!Repeated) {
|
|
|
|
bool ShouldAKA = false;
|
|
|
|
QualType DesugaredTy = Desugar(Context, Ty, ShouldAKA);
|
2011-07-12 01:49:21 +08:00
|
|
|
if (ShouldAKA || ForceAKA) {
|
|
|
|
if (DesugaredTy == Ty) {
|
|
|
|
DesugaredTy = Ty.getCanonicalType();
|
|
|
|
}
|
2011-09-28 06:38:19 +08:00
|
|
|
std::string akaStr = DesugaredTy.getAsString(Context.getPrintingPolicy());
|
2011-07-12 01:49:21 +08:00
|
|
|
if (akaStr != S) {
|
|
|
|
S = "'" + S + "' (aka '" + akaStr + "')";
|
|
|
|
return S;
|
|
|
|
}
|
2010-05-13 19:37:24 +08:00
|
|
|
}
|
2010-02-10 06:26:47 +08:00
|
|
|
}
|
2010-05-13 19:37:24 +08:00
|
|
|
|
2010-02-10 06:26:47 +08:00
|
|
|
S = "'" + S + "'";
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
2011-07-12 01:49:21 +08:00
|
|
|
void clang::FormatASTNodeDiagnosticArgument(
|
2011-09-26 07:23:43 +08:00
|
|
|
DiagnosticsEngine::ArgumentKind Kind,
|
2011-07-12 01:49:21 +08:00
|
|
|
intptr_t Val,
|
|
|
|
const char *Modifier,
|
|
|
|
unsigned ModLen,
|
|
|
|
const char *Argument,
|
|
|
|
unsigned ArgLen,
|
2011-09-26 07:23:43 +08:00
|
|
|
const DiagnosticsEngine::ArgumentValue *PrevArgs,
|
2011-07-12 01:49:21 +08:00
|
|
|
unsigned NumPrevArgs,
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVectorImpl<char> &Output,
|
2011-07-12 01:49:21 +08:00
|
|
|
void *Cookie,
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVectorImpl<intptr_t> &QualTypeVals) {
|
2010-02-10 06:26:47 +08:00
|
|
|
ASTContext &Context = *static_cast<ASTContext*>(Cookie);
|
|
|
|
|
|
|
|
std::string S;
|
|
|
|
bool NeedQuotes = true;
|
|
|
|
|
|
|
|
switch (Kind) {
|
2011-09-23 13:06:16 +08:00
|
|
|
default: llvm_unreachable("unknown ArgumentKind");
|
2011-09-26 07:23:43 +08:00
|
|
|
case DiagnosticsEngine::ak_qualtype: {
|
2010-02-10 06:26:47 +08:00
|
|
|
assert(ModLen == 0 && ArgLen == 0 &&
|
|
|
|
"Invalid modifier for QualType argument");
|
|
|
|
|
|
|
|
QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val)));
|
2011-07-12 01:49:21 +08:00
|
|
|
S = ConvertTypeToDiagnosticString(Context, Ty, PrevArgs, NumPrevArgs,
|
|
|
|
QualTypeVals);
|
2010-02-10 06:26:47 +08:00
|
|
|
NeedQuotes = false;
|
|
|
|
break;
|
|
|
|
}
|
2011-09-26 07:23:43 +08:00
|
|
|
case DiagnosticsEngine::ak_declarationname: {
|
2010-02-10 06:26:47 +08:00
|
|
|
DeclarationName N = DeclarationName::getFromOpaqueInteger(Val);
|
|
|
|
S = N.getAsString();
|
|
|
|
|
|
|
|
if (ModLen == 9 && !memcmp(Modifier, "objcclass", 9) && ArgLen == 0)
|
|
|
|
S = '+' + S;
|
|
|
|
else if (ModLen == 12 && !memcmp(Modifier, "objcinstance", 12)
|
|
|
|
&& ArgLen==0)
|
|
|
|
S = '-' + S;
|
|
|
|
else
|
|
|
|
assert(ModLen == 0 && ArgLen == 0 &&
|
|
|
|
"Invalid modifier for DeclarationName argument");
|
|
|
|
break;
|
|
|
|
}
|
2011-09-26 07:23:43 +08:00
|
|
|
case DiagnosticsEngine::ak_nameddecl: {
|
2010-02-10 06:26:47 +08:00
|
|
|
bool Qualified;
|
|
|
|
if (ModLen == 1 && Modifier[0] == 'q' && ArgLen == 0)
|
|
|
|
Qualified = true;
|
|
|
|
else {
|
|
|
|
assert(ModLen == 0 && ArgLen == 0 &&
|
|
|
|
"Invalid modifier for NamedDecl* argument");
|
|
|
|
Qualified = false;
|
|
|
|
}
|
2011-08-31 17:01:53 +08:00
|
|
|
const NamedDecl *ND = reinterpret_cast<const NamedDecl*>(Val);
|
2011-09-28 06:38:19 +08:00
|
|
|
ND->getNameForDiagnostic(S, Context.getPrintingPolicy(), Qualified);
|
2010-02-10 06:26:47 +08:00
|
|
|
break;
|
|
|
|
}
|
2011-09-26 07:23:43 +08:00
|
|
|
case DiagnosticsEngine::ak_nestednamespec: {
|
2010-02-10 06:26:47 +08:00
|
|
|
llvm::raw_string_ostream OS(S);
|
|
|
|
reinterpret_cast<NestedNameSpecifier*>(Val)->print(OS,
|
2011-09-28 06:38:19 +08:00
|
|
|
Context.getPrintingPolicy());
|
2010-02-10 06:26:47 +08:00
|
|
|
NeedQuotes = false;
|
|
|
|
break;
|
|
|
|
}
|
2011-09-26 07:23:43 +08:00
|
|
|
case DiagnosticsEngine::ak_declcontext: {
|
2010-02-10 06:26:47 +08:00
|
|
|
DeclContext *DC = reinterpret_cast<DeclContext *> (Val);
|
|
|
|
assert(DC && "Should never have a null declaration context");
|
|
|
|
|
|
|
|
if (DC->isTranslationUnit()) {
|
|
|
|
// FIXME: Get these strings from some localized place
|
|
|
|
if (Context.getLangOptions().CPlusPlus)
|
|
|
|
S = "the global namespace";
|
|
|
|
else
|
|
|
|
S = "the global scope";
|
|
|
|
} else if (TypeDecl *Type = dyn_cast<TypeDecl>(DC)) {
|
|
|
|
S = ConvertTypeToDiagnosticString(Context,
|
|
|
|
Context.getTypeDeclType(Type),
|
2011-07-12 01:49:21 +08:00
|
|
|
PrevArgs, NumPrevArgs, QualTypeVals);
|
2010-02-10 06:26:47 +08:00
|
|
|
} else {
|
|
|
|
// FIXME: Get these strings from some localized place
|
|
|
|
NamedDecl *ND = cast<NamedDecl>(DC);
|
|
|
|
if (isa<NamespaceDecl>(ND))
|
|
|
|
S += "namespace ";
|
|
|
|
else if (isa<ObjCMethodDecl>(ND))
|
|
|
|
S += "method ";
|
|
|
|
else if (isa<FunctionDecl>(ND))
|
|
|
|
S += "function ";
|
|
|
|
|
|
|
|
S += "'";
|
2011-09-28 06:38:19 +08:00
|
|
|
ND->getNameForDiagnostic(S, Context.getPrintingPolicy(), true);
|
2010-02-10 06:26:47 +08:00
|
|
|
S += "'";
|
|
|
|
}
|
|
|
|
NeedQuotes = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NeedQuotes)
|
|
|
|
Output.push_back('\'');
|
|
|
|
|
|
|
|
Output.append(S.begin(), S.end());
|
|
|
|
|
|
|
|
if (NeedQuotes)
|
|
|
|
Output.push_back('\'');
|
|
|
|
}
|