Add support for sending QualType's directly into diags and convert two
diags over to use this. QualTypes implicitly print single quotes around
them for uniformity and future extension.
Doing this requires a little function pointer dance to prevent libbasic
from depending on libast.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@59907 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Basic/Diagnostic.cpp b/lib/Basic/Diagnostic.cpp
index e1183c6..8a897f5 100644
--- a/lib/Basic/Diagnostic.cpp
+++ b/lib/Basic/Diagnostic.cpp
@@ -116,6 +116,14 @@
// Common Diagnostic implementation
//===----------------------------------------------------------------------===//
+static void DummyQTToStringFnTy(intptr_t QT, const char *Modifier, unsigned ML,
+ const char *Argument, unsigned ArgLen,
+ llvm::SmallVectorImpl<char> &Output) {
+ const char *Str = "<can't format QualType>";
+ Output.append(Str, Str+strlen(Str));
+}
+
+
Diagnostic::Diagnostic(DiagnosticClient *client) : Client(client) {
IgnoreAllWarnings = false;
WarningsAsErrors = false;
@@ -130,6 +138,8 @@
NumErrors = 0;
CustomDiagInfo = 0;
CurDiagID = ~0U;
+
+ QualTypeToString = DummyQTToStringFnTy;
}
Diagnostic::~Diagnostic() {
@@ -468,29 +478,29 @@
}
assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
- unsigned StrNo = *DiagStr++ - '0';
+ unsigned ArgNo = *DiagStr++ - '0';
- switch (getArgKind(StrNo)) {
+ switch (getArgKind(ArgNo)) {
case Diagnostic::ak_std_string: {
- const std::string &S = getArgStdStr(StrNo);
+ const std::string &S = getArgStdStr(ArgNo);
assert(ModifierLen == 0 && "No modifiers for strings yet");
OutStr.append(S.begin(), S.end());
break;
}
case Diagnostic::ak_c_string: {
- const char *S = getArgCStr(StrNo);
+ const char *S = getArgCStr(ArgNo);
assert(ModifierLen == 0 && "No modifiers for strings yet");
OutStr.append(S, S + strlen(S));
break;
}
case Diagnostic::ak_identifierinfo: {
- const IdentifierInfo *II = getArgIdentifier(StrNo);
+ const IdentifierInfo *II = getArgIdentifier(ArgNo);
assert(ModifierLen == 0 && "No modifiers for strings yet");
OutStr.append(II->getName(), II->getName() + II->getLength());
break;
}
case Diagnostic::ak_sint: {
- int Val = getArgSInt(StrNo);
+ int Val = getArgSInt(ArgNo);
if (ModifierIs(Modifier, ModifierLen, "select")) {
HandleSelectModifier((unsigned)Val, Argument, ArgumentLen, OutStr);
@@ -507,7 +517,7 @@
break;
}
case Diagnostic::ak_uint: {
- unsigned Val = getArgUInt(StrNo);
+ unsigned Val = getArgUInt(ArgNo);
if (ModifierIs(Modifier, ModifierLen, "select")) {
HandleSelectModifier(Val, Argument, ArgumentLen, OutStr);
@@ -521,9 +531,16 @@
// FIXME: Optimize
std::string S = llvm::utostr_32(Val);
OutStr.append(S.begin(), S.end());
- break;
}
+ break;
}
+ case Diagnostic::ak_qualtype:
+ OutStr.push_back('\'');
+ getDiags()->ConvertQualTypeToString(getRawArg(ArgNo),
+ Modifier, ModifierLen,
+ Argument, ArgumentLen, OutStr);
+ OutStr.push_back('\'');
+ break;
}
}
}
diff --git a/lib/Sema/Sema.cpp b/lib/Sema/Sema.cpp
index 2edf08e..d43eadd 100644
--- a/lib/Sema/Sema.cpp
+++ b/lib/Sema/Sema.cpp
@@ -20,6 +20,22 @@
#include "clang/Basic/Diagnostic.h"
using namespace clang;
+/// ConvertQualTypeToStringFn - This function is used to pretty print the
+/// specified QualType as a string in diagnostics.
+static void ConvertQualTypeToStringFn(intptr_t QT,
+ const char *Modifier, unsigned ML,
+ const char *Argument, unsigned ArgLen,
+ llvm::SmallVectorImpl<char> &Output) {
+ assert(ML == 0 && ArgLen == 0 && "Invalid modifier for QualType argument");
+
+ QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(QT)));
+
+ // FIXME: Playing with std::string is really slow.
+ std::string S = Ty.getAsString();
+ Output.append(S.begin(), S.end());
+}
+
+
static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name) {
if (C.getLangOptions().CPlusPlus)
return CXXRecordDecl::Create(C, TagDecl::TK_struct,
@@ -108,6 +124,9 @@
TUScope = 0;
if (getLangOptions().CPlusPlus)
FieldCollector.reset(new CXXFieldCollector());
+
+ // Tell diagnostics how to render things from the AST library.
+ PP.getDiagnostics().SetQualTypeToStringFn(ConvertQualTypeToStringFn);
}
/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp
index ef3ead4..bebde38 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/lib/Sema/SemaExpr.cpp
@@ -2054,7 +2054,7 @@
QualType Sema::InvalidOperands(SourceLocation Loc, Expr *&lex, Expr *&rex) {
Diag(Loc, diag::err_typecheck_invalid_operands)
- << lex->getType().getAsString() << rex->getType().getAsString()
+ << lex->getType() << rex->getType()
<< lex->getSourceRange() << rex->getSourceRange();
return QualType();
}
@@ -2809,20 +2809,19 @@
return Context.getPointerType(op->getType());
}
-QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
- UsualUnaryConversions(op);
- QualType qType = op->getType();
+QualType Sema::CheckIndirectionOperand(Expr *Op, SourceLocation OpLoc) {
+ UsualUnaryConversions(Op);
+ QualType Ty = Op->getType();
- if (const PointerType *PT = qType->getAsPointerType()) {
- // Note that per both C89 and C99, this is always legal, even
- // if ptype is an incomplete type or void.
- // It would be possible to warn about dereferencing a
- // void pointer, but it's completely well-defined,
- // and such a warning is unlikely to catch any mistakes.
+ // Note that per both C89 and C99, this is always legal, even if ptype is an
+ // incomplete type or void. It would be possible to warn about dereferencing
+ // a void pointer, but it's completely well-defined, and such a warning is
+ // unlikely to catch any mistakes.
+ if (const PointerType *PT = Ty->getAsPointerType())
return PT->getPointeeType();
- }
+
Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer)
- << qType.getAsString() << op->getSourceRange();
+ << Ty << Op->getSourceRange();
return QualType();
}