2011-07-29 03:46:48 +08:00
|
|
|
//===--- SemaFixItUtils.cpp - Sema FixIts ---------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines helper classes for generation of Sema FixItHints.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-07-05 01:04:04 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2011-07-29 03:46:48 +08:00
|
|
|
#include "clang/AST/ExprCXX.h"
|
|
|
|
#include "clang/AST/ExprObjC.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
#include "clang/Sema/Sema.h"
|
|
|
|
#include "clang/Sema/SemaFixItUtils.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
bool ConversionFixItGenerator::compareTypesSimple(CanQualType From,
|
|
|
|
CanQualType To,
|
|
|
|
Sema &S,
|
|
|
|
SourceLocation Loc,
|
|
|
|
ExprValueKind FromVK) {
|
|
|
|
if (!To.isAtLeastAsQualifiedAs(From))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
From = From.getNonReferenceType();
|
|
|
|
To = To.getNonReferenceType();
|
|
|
|
|
|
|
|
// If both are pointer types, work with the pointee types.
|
|
|
|
if (isa<PointerType>(From) && isa<PointerType>(To)) {
|
|
|
|
From = S.Context.getCanonicalType(
|
|
|
|
(cast<PointerType>(From))->getPointeeType());
|
|
|
|
To = S.Context.getCanonicalType(
|
|
|
|
(cast<PointerType>(To))->getPointeeType());
|
|
|
|
}
|
|
|
|
|
|
|
|
const CanQualType FromUnq = From.getUnqualifiedType();
|
|
|
|
const CanQualType ToUnq = To.getUnqualifiedType();
|
|
|
|
|
2015-12-19 05:45:41 +08:00
|
|
|
if ((FromUnq == ToUnq || (S.IsDerivedFrom(Loc, FromUnq, ToUnq)) ) &&
|
2011-07-29 03:46:48 +08:00
|
|
|
To.isAtLeastAsQualifiedAs(From))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConversionFixItGenerator::tryToFixConversion(const Expr *FullExpr,
|
|
|
|
const QualType FromTy,
|
|
|
|
const QualType ToTy,
|
|
|
|
Sema &S) {
|
|
|
|
if (!FullExpr)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const CanQualType FromQTy = S.Context.getCanonicalType(FromTy);
|
|
|
|
const CanQualType ToQTy = S.Context.getCanonicalType(ToTy);
|
|
|
|
const SourceLocation Begin = FullExpr->getSourceRange().getBegin();
|
2015-11-15 10:31:46 +08:00
|
|
|
const SourceLocation End = S.getLocForEndOfToken(FullExpr->getSourceRange()
|
|
|
|
.getEnd());
|
2011-07-29 03:46:48 +08:00
|
|
|
|
|
|
|
// Strip the implicit casts - those are implied by the compiler, not the
|
|
|
|
// original source code.
|
|
|
|
const Expr* Expr = FullExpr->IgnoreImpCasts();
|
|
|
|
|
|
|
|
bool NeedParen = true;
|
|
|
|
if (isa<ArraySubscriptExpr>(Expr) ||
|
|
|
|
isa<CallExpr>(Expr) ||
|
|
|
|
isa<DeclRefExpr>(Expr) ||
|
|
|
|
isa<CastExpr>(Expr) ||
|
|
|
|
isa<CXXNewExpr>(Expr) ||
|
|
|
|
isa<CXXConstructExpr>(Expr) ||
|
|
|
|
isa<CXXDeleteExpr>(Expr) ||
|
|
|
|
isa<CXXNoexceptExpr>(Expr) ||
|
|
|
|
isa<CXXPseudoDestructorExpr>(Expr) ||
|
|
|
|
isa<CXXScalarValueInitExpr>(Expr) ||
|
|
|
|
isa<CXXThisExpr>(Expr) ||
|
|
|
|
isa<CXXTypeidExpr>(Expr) ||
|
|
|
|
isa<CXXUnresolvedConstructExpr>(Expr) ||
|
|
|
|
isa<ObjCMessageExpr>(Expr) ||
|
|
|
|
isa<ObjCPropertyRefExpr>(Expr) ||
|
|
|
|
isa<ObjCProtocolExpr>(Expr) ||
|
|
|
|
isa<MemberExpr>(Expr) ||
|
|
|
|
isa<ParenExpr>(FullExpr) ||
|
|
|
|
isa<ParenListExpr>(Expr) ||
|
|
|
|
isa<SizeOfPackExpr>(Expr) ||
|
|
|
|
isa<UnaryOperator>(Expr))
|
|
|
|
NeedParen = false;
|
|
|
|
|
|
|
|
// Check if the argument needs to be dereferenced:
|
|
|
|
// (type * -> type) or (type * -> type &).
|
|
|
|
if (const PointerType *FromPtrTy = dyn_cast<PointerType>(FromQTy)) {
|
|
|
|
OverloadFixItKind FixKind = OFIK_Dereference;
|
|
|
|
|
|
|
|
bool CanConvert = CompareTypes(
|
|
|
|
S.Context.getCanonicalType(FromPtrTy->getPointeeType()), ToQTy,
|
|
|
|
S, Begin, VK_LValue);
|
|
|
|
if (CanConvert) {
|
|
|
|
// Do not suggest dereferencing a Null pointer.
|
|
|
|
if (Expr->IgnoreParenCasts()->
|
|
|
|
isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(Expr)) {
|
|
|
|
if (UO->getOpcode() == UO_AddrOf) {
|
|
|
|
FixKind = OFIK_RemoveTakeAddress;
|
|
|
|
Hints.push_back(FixItHint::CreateRemoval(
|
|
|
|
CharSourceRange::getTokenRange(Begin, Begin)));
|
|
|
|
}
|
|
|
|
} else if (NeedParen) {
|
|
|
|
Hints.push_back(FixItHint::CreateInsertion(Begin, "*("));
|
|
|
|
Hints.push_back(FixItHint::CreateInsertion(End, ")"));
|
|
|
|
} else {
|
|
|
|
Hints.push_back(FixItHint::CreateInsertion(Begin, "*"));
|
|
|
|
}
|
|
|
|
|
|
|
|
NumConversionsFixed++;
|
|
|
|
if (NumConversionsFixed == 1)
|
|
|
|
Kind = FixKind;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the pointer to the argument needs to be passed:
|
|
|
|
// (type -> type *) or (type & -> type *).
|
|
|
|
if (isa<PointerType>(ToQTy)) {
|
|
|
|
bool CanConvert = false;
|
|
|
|
OverloadFixItKind FixKind = OFIK_TakeAddress;
|
|
|
|
|
|
|
|
// Only suggest taking address of L-values.
|
|
|
|
if (!Expr->isLValue() || Expr->getObjectKind() != OK_Ordinary)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
CanConvert = CompareTypes(S.Context.getPointerType(FromQTy), ToQTy,
|
|
|
|
S, Begin, VK_RValue);
|
|
|
|
if (CanConvert) {
|
|
|
|
|
|
|
|
if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(Expr)) {
|
|
|
|
if (UO->getOpcode() == UO_Deref) {
|
|
|
|
FixKind = OFIK_RemoveDereference;
|
|
|
|
Hints.push_back(FixItHint::CreateRemoval(
|
|
|
|
CharSourceRange::getTokenRange(Begin, Begin)));
|
|
|
|
}
|
|
|
|
} else if (NeedParen) {
|
|
|
|
Hints.push_back(FixItHint::CreateInsertion(Begin, "&("));
|
|
|
|
Hints.push_back(FixItHint::CreateInsertion(End, ")"));
|
|
|
|
} else {
|
|
|
|
Hints.push_back(FixItHint::CreateInsertion(Begin, "&"));
|
|
|
|
}
|
|
|
|
|
|
|
|
NumConversionsFixed++;
|
|
|
|
if (NumConversionsFixed == 1)
|
|
|
|
Kind = FixKind;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
Improve 0-argument -Wvexing-parse diagnostic by adding notes with fix-its:
- If the declarator is at the start of a line, and the previous line contained
another declarator and ended with a comma, then that comma was probably a
typo for a semicolon:
int n = 0, m = 1, l = 2, // k = 5;
myImportantFunctionCall(); // oops!
- If removing the parentheses would correctly initialize the object, then
produce a note suggesting that fix.
- Otherwise, if there is a simple initializer we can suggest which performs
value-initialization, then provide a note suggesting a correction to that
initializer.
Sema::Declarator now tracks the location of the comma prior to the declarator in
the declaration, if there is one, to facilitate providing the note. The code to
determine an appropriate initializer from the -Wuninitialized warning has been
factored out to allow use in both that and -Wvexing-parse.
llvm-svn: 148072
2012-01-13 07:53:29 +08:00
|
|
|
|
2013-09-20 08:27:40 +08:00
|
|
|
static bool isMacroDefined(const Sema &S, SourceLocation Loc, StringRef Name) {
|
2015-04-30 07:20:19 +08:00
|
|
|
return (bool)S.PP.getMacroDefinitionAtLoc(&S.getASTContext().Idents.get(Name),
|
|
|
|
Loc);
|
2012-01-14 03:34:55 +08:00
|
|
|
}
|
|
|
|
|
2013-09-20 08:27:40 +08:00
|
|
|
static std::string getScalarZeroExpressionForType(
|
|
|
|
const Type &T, SourceLocation Loc, const Sema &S) {
|
2012-05-01 02:27:22 +08:00
|
|
|
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()) &&
|
2013-09-20 08:27:40 +08:00
|
|
|
isMacroDefined(S, Loc, "nil"))
|
2012-05-01 02:27:22 +08:00
|
|
|
return "nil";
|
|
|
|
if (T.isRealFloatingType())
|
|
|
|
return "0.0";
|
2013-09-20 08:27:40 +08:00
|
|
|
if (T.isBooleanType() &&
|
|
|
|
(S.LangOpts.CPlusPlus || isMacroDefined(S, Loc, "false")))
|
2012-05-01 02:27:22 +08:00
|
|
|
return "false";
|
|
|
|
if (T.isPointerType() || T.isMemberPointerType()) {
|
2013-01-02 19:42:31 +08:00
|
|
|
if (S.LangOpts.CPlusPlus11)
|
2012-05-01 02:27:22 +08:00
|
|
|
return "nullptr";
|
2013-09-20 08:27:40 +08:00
|
|
|
if (isMacroDefined(S, Loc, "NULL"))
|
2012-05-01 02:27:22 +08:00
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
2013-09-20 08:27:40 +08:00
|
|
|
std::string
|
|
|
|
Sema::getFixItZeroInitializerForType(QualType T, SourceLocation Loc) const {
|
2012-01-14 03:34:55 +08:00
|
|
|
if (T->isScalarType()) {
|
2013-09-20 08:27:40 +08:00
|
|
|
std::string s = getScalarZeroExpressionForType(*T, Loc, *this);
|
2012-05-01 02:27:22 +08:00
|
|
|
if (!s.empty())
|
|
|
|
s = " = " + s;
|
|
|
|
return s;
|
2012-01-14 03:34:55 +08:00
|
|
|
}
|
|
|
|
|
Improve 0-argument -Wvexing-parse diagnostic by adding notes with fix-its:
- If the declarator is at the start of a line, and the previous line contained
another declarator and ended with a comma, then that comma was probably a
typo for a semicolon:
int n = 0, m = 1, l = 2, // k = 5;
myImportantFunctionCall(); // oops!
- If removing the parentheses would correctly initialize the object, then
produce a note suggesting that fix.
- Otherwise, if there is a simple initializer we can suggest which performs
value-initialization, then provide a note suggesting a correction to that
initializer.
Sema::Declarator now tracks the location of the comma prior to the declarator in
the declaration, if there is one, to facilitate providing the note. The code to
determine an appropriate initializer from the -Wuninitialized warning has been
factored out to allow use in both that and -Wvexing-parse.
llvm-svn: 148072
2012-01-13 07:53:29 +08:00
|
|
|
const CXXRecordDecl *RD = T->getAsCXXRecordDecl();
|
2012-01-13 10:14:39 +08:00
|
|
|
if (!RD || !RD->hasDefinition())
|
2012-05-01 02:27:22 +08:00
|
|
|
return std::string();
|
2013-01-02 19:42:31 +08:00
|
|
|
if (LangOpts.CPlusPlus11 && !RD->hasUserProvidedDefaultConstructor())
|
Improve 0-argument -Wvexing-parse diagnostic by adding notes with fix-its:
- If the declarator is at the start of a line, and the previous line contained
another declarator and ended with a comma, then that comma was probably a
typo for a semicolon:
int n = 0, m = 1, l = 2, // k = 5;
myImportantFunctionCall(); // oops!
- If removing the parentheses would correctly initialize the object, then
produce a note suggesting that fix.
- Otherwise, if there is a simple initializer we can suggest which performs
value-initialization, then provide a note suggesting a correction to that
initializer.
Sema::Declarator now tracks the location of the comma prior to the declarator in
the declaration, if there is one, to facilitate providing the note. The code to
determine an appropriate initializer from the -Wuninitialized warning has been
factored out to allow use in both that and -Wvexing-parse.
llvm-svn: 148072
2012-01-13 07:53:29 +08:00
|
|
|
return "{}";
|
2012-01-13 10:14:39 +08:00
|
|
|
if (RD->isAggregate())
|
Improve 0-argument -Wvexing-parse diagnostic by adding notes with fix-its:
- If the declarator is at the start of a line, and the previous line contained
another declarator and ended with a comma, then that comma was probably a
typo for a semicolon:
int n = 0, m = 1, l = 2, // k = 5;
myImportantFunctionCall(); // oops!
- If removing the parentheses would correctly initialize the object, then
produce a note suggesting that fix.
- Otherwise, if there is a simple initializer we can suggest which performs
value-initialization, then provide a note suggesting a correction to that
initializer.
Sema::Declarator now tracks the location of the comma prior to the declarator in
the declaration, if there is one, to facilitate providing the note. The code to
determine an appropriate initializer from the -Wuninitialized warning has been
factored out to allow use in both that and -Wvexing-parse.
llvm-svn: 148072
2012-01-13 07:53:29 +08:00
|
|
|
return " = {}";
|
2012-05-01 02:27:22 +08:00
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
2013-09-20 08:27:40 +08:00
|
|
|
std::string
|
|
|
|
Sema::getFixItZeroLiteralForType(QualType T, SourceLocation Loc) const {
|
|
|
|
return getScalarZeroExpressionForType(*T, Loc, *this);
|
Improve 0-argument -Wvexing-parse diagnostic by adding notes with fix-its:
- If the declarator is at the start of a line, and the previous line contained
another declarator and ended with a comma, then that comma was probably a
typo for a semicolon:
int n = 0, m = 1, l = 2, // k = 5;
myImportantFunctionCall(); // oops!
- If removing the parentheses would correctly initialize the object, then
produce a note suggesting that fix.
- Otherwise, if there is a simple initializer we can suggest which performs
value-initialization, then provide a note suggesting a correction to that
initializer.
Sema::Declarator now tracks the location of the comma prior to the declarator in
the declaration, if there is one, to facilitate providing the note. The code to
determine an appropriate initializer from the -Wuninitialized warning has been
factored out to allow use in both that and -Wvexing-parse.
llvm-svn: 148072
2012-01-13 07:53:29 +08:00
|
|
|
}
|