namespace {
constexpr char StdMemoryHeader[] = "memory";
+constexpr char ConstructorCall[] = "constructorCall";
+constexpr char ResetCall[] = "resetCall";
+constexpr char NewExpression[] = "newExpression";
std::string GetNewExprName(const CXXNewExpr *NewExpr,
const SourceManager &SM,
NewExpr->getAllocatedTypeSourceInfo()->getTypeLoc().getSourceRange()),
SM, Lang);
if (NewExpr->isArray()) {
- return WrittenName.str() + "[]";
+ return (WrittenName + "[]").str();
}
return WrittenName.str();
}
} // namespace
const char MakeSmartPtrCheck::PointerType[] = "pointerType";
-const char MakeSmartPtrCheck::ConstructorCall[] = "constructorCall";
-const char MakeSmartPtrCheck::ResetCall[] = "resetCall";
-const char MakeSmartPtrCheck::NewExpression[] = "newExpression";
MakeSmartPtrCheck::MakeSmartPtrCheck(StringRef Name,
ClangTidyContext* Context,
void MakeSmartPtrCheck::registerPPCallbacks(CompilerInstance &Compiler) {
if (isLanguageVersionSupported(getLangOpts())) {
- Inserter.reset(new utils::IncludeInserter(
- Compiler.getSourceManager(), Compiler.getLangOpts(), IncludeStyle));
+ Inserter = llvm::make_unique<utils::IncludeInserter>(
+ Compiler.getSourceManager(), Compiler.getLangOpts(), IncludeStyle);
Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks());
}
}
return;
if (Construct)
- checkConstruct(SM, Construct, Type, New);
+ checkConstruct(SM, Result.Context, Construct, Type, New);
else if (Reset)
- checkReset(SM, Reset, New);
+ checkReset(SM, Result.Context, Reset, New);
}
-void MakeSmartPtrCheck::checkConstruct(SourceManager &SM,
+void MakeSmartPtrCheck::checkConstruct(SourceManager &SM, ASTContext *Ctx,
const CXXConstructExpr *Construct,
const QualType *Type,
const CXXNewExpr *New) {
return;
}
- if (!replaceNew(Diag, New, SM)) {
+ if (!replaceNew(Diag, New, SM, Ctx)) {
return;
}
insertHeader(Diag, SM.getFileID(ConstructCallStart));
}
-void MakeSmartPtrCheck::checkReset(SourceManager &SM,
+void MakeSmartPtrCheck::checkReset(SourceManager &SM, ASTContext *Ctx,
const CXXMemberCallExpr *Reset,
const CXXNewExpr *New) {
const auto *Expr = cast<MemberExpr>(Reset->getCallee());
return;
}
- if (!replaceNew(Diag, New, SM)) {
+ if (!replaceNew(Diag, New, SM, Ctx)) {
return;
}
}
bool MakeSmartPtrCheck::replaceNew(DiagnosticBuilder &Diag,
- const CXXNewExpr *New,
- SourceManager& SM) {
- SourceLocation NewStart = New->getSourceRange().getBegin();
- SourceLocation NewEnd = New->getSourceRange().getEnd();
+ const CXXNewExpr *New, SourceManager &SM,
+ ASTContext *Ctx) {
+ auto SkipParensParents = [&](const Expr *E) {
+ for (const Expr *OldE = nullptr; E != OldE;) {
+ OldE = E;
+ for (const auto &Node : Ctx->getParents(*E)) {
+ if (const Expr *Parent = Node.get<ParenExpr>()) {
+ E = Parent;
+ break;
+ }
+ }
+ }
+ return E;
+ };
+
+ SourceRange NewRange = SkipParensParents(New)->getSourceRange();
+ SourceLocation NewStart = NewRange.getBegin();
+ SourceLocation NewEnd = NewRange.getEnd();
// Skip when the source location of the new expression is invalid.
if (NewStart.isInvalid() || NewEnd.isInvalid())
virtual bool isLanguageVersionSupported(const LangOptions &LangOpts) const;
static const char PointerType[];
- static const char ConstructorCall[];
- static const char ResetCall[];
- static const char NewExpression[];
private:
std::unique_ptr<utils::IncludeInserter> Inserter;
const std::string MakeSmartPtrFunctionName;
const bool IgnoreMacros;
- void checkConstruct(SourceManager &SM, const CXXConstructExpr *Construct,
- const QualType *Type, const CXXNewExpr *New);
- void checkReset(SourceManager &SM, const CXXMemberCallExpr *Member,
- const CXXNewExpr *New);
+ void checkConstruct(SourceManager &SM, ASTContext *Ctx,
+ const CXXConstructExpr *Construct, const QualType *Type,
+ const CXXNewExpr *New);
+ void checkReset(SourceManager &SM, ASTContext *Ctx,
+ const CXXMemberCallExpr *Member, const CXXNewExpr *New);
/// Returns true when the fixes for replacing CXXNewExpr are generated.
bool replaceNew(DiagnosticBuilder &Diag, const CXXNewExpr *New,
- SourceManager &SM);
+ SourceManager &SM, ASTContext *Ctx);
void insertHeader(DiagnosticBuilder &Diag, FileID FD);
};
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead
// CHECK-FIXES: auto P3 = std::make_unique<int>();
+ std::unique_ptr<int> P4 = std::unique_ptr<int>((new int));
+ // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
+ // CHECK-FIXES: std::unique_ptr<int> P4 = std::make_unique<int>();
+ P4.reset((new int));
+ // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]
+ // CHECK-FIXES: P4 = std::make_unique<int>();
+ std::unique_ptr<int> P5 = std::unique_ptr<int>((((new int))));
+ // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
+ // CHECK-FIXES: std::unique_ptr<int> P5 = std::make_unique<int>();
+ P5.reset(((((new int)))));
+ // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]
+ // CHECK-FIXES: P5 = std::make_unique<int>();
+
{
// No std.
using namespace std;