Add FixItHint for -Wnull-conversion to initialize with an appropriate literal.

Reviewed by Doug Gregor.

llvm-svn: 155839
This commit is contained in:
David Blaikie
2012-04-30 18:27:22 +00:00
parent f68e809c5e
commit 7665a62cf7
6 changed files with 58 additions and 40 deletions

View File

@@ -163,42 +163,54 @@ static bool isMacroDefined(const Sema &S, StringRef Name) {
return S.PP.getMacroInfo(&S.getASTContext().Idents.get(Name));
}
const char *Sema::getFixItZeroInitializerForType(QualType T) const {
static std::string getScalarZeroExpressionForType(const Type& T, const Sema& S) {
assert(T.isScalarType() && "use scalar types only");
// Suggest "0" for non-enumeration scalar types, unless we can find a
// better initializer.
if (T.isEnumeralType())
return std::string();
if ((T.isObjCObjectPointerType() || T.isBlockPointerType()) &&
isMacroDefined(S, "nil"))
return "nil";
if (T.isRealFloatingType())
return "0.0";
if (T.isBooleanType() && S.LangOpts.CPlusPlus)
return "false";
if (T.isPointerType() || T.isMemberPointerType()) {
if (S.LangOpts.CPlusPlus0x)
return "nullptr";
if (isMacroDefined(S, "NULL"))
return "NULL";
}
if (T.isCharType())
return "'\\0'";
if (T.isWideCharType())
return "L'\\0'";
if (T.isChar16Type())
return "u'\\0'";
if (T.isChar32Type())
return "U'\\0'";
return "0";
}
std::string Sema::getFixItZeroInitializerForType(QualType T) const {
if (T->isScalarType()) {
// Suggest " = 0" for non-enumeration scalar types, unless we can find a
// better initializer.
if (T->isEnumeralType())
return 0;
if ((T->isObjCObjectPointerType() || T->isBlockPointerType()) &&
isMacroDefined(*this, "nil"))
return " = nil";
if (T->isRealFloatingType())
return " = 0.0";
if (T->isBooleanType() && LangOpts.CPlusPlus)
return " = false";
if (T->isPointerType() || T->isMemberPointerType()) {
if (LangOpts.CPlusPlus0x)
return " = nullptr";
else if (isMacroDefined(*this, "NULL"))
return " = NULL";
}
if (T->isCharType())
return " = '\\0'";
if (T->isWideCharType())
return " = L'\\0'";
if (T->isChar16Type())
return " = u'\\0'";
if (T->isChar32Type())
return " = U'\\0'";
return " = 0";
std::string s = getScalarZeroExpressionForType(*T, *this);
if (!s.empty())
s = " = " + s;
return s;
}
const CXXRecordDecl *RD = T->getAsCXXRecordDecl();
if (!RD || !RD->hasDefinition())
return 0;
return std::string();
if (LangOpts.CPlusPlus0x && !RD->hasUserProvidedDefaultConstructor())
return "{}";
if (RD->isAggregate())
return " = {}";
return 0;
return std::string();
}
std::string Sema::getFixItZeroLiteralForType(QualType T) const {
return getScalarZeroExpressionForType(*T, *this);
}