[clang-tools-extra] r310050 - [clang-tidy] Ignore macros in make-unique check.
Haojian Wu via cfe-commits
cfe-commits at lists.llvm.org
Fri Aug 4 04:18:00 PDT 2017
Author: hokein
Date: Fri Aug 4 04:18:00 2017
New Revision: 310050
URL: http://llvm.org/viewvc/llvm-project?rev=310050&view=rev
Log:
[clang-tidy] Ignore macros in make-unique check.
Summary:
The check doesn't fully support smart-ptr usages inside macros, which
may cause incorrect fixes, or even crashes, ignore them for now.
Reviewers: alexfh
Reviewed By: alexfh
Subscribers: JDevlieghere, xazax.hun, cfe-commits
Differential Revision: https://reviews.llvm.org/D36264
Added:
clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique-macros.cpp
Modified:
clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.cpp
clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.h
clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-make-unique.rst
clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique.cpp
Modified: clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.cpp?rev=310050&r1=310049&r2=310050&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.cpp Fri Aug 4 04:18:00 2017
@@ -42,7 +42,8 @@ const char MakeSmartPtrCheck::Constructo
const char MakeSmartPtrCheck::ResetCall[] = "resetCall";
const char MakeSmartPtrCheck::NewExpression[] = "newExpression";
-MakeSmartPtrCheck::MakeSmartPtrCheck(StringRef Name, ClangTidyContext *Context,
+MakeSmartPtrCheck::MakeSmartPtrCheck(StringRef Name,
+ ClangTidyContext* Context,
StringRef MakeSmartPtrFunctionName)
: ClangTidyCheck(Name, Context),
IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
@@ -50,12 +51,14 @@ MakeSmartPtrCheck::MakeSmartPtrCheck(Str
MakeSmartPtrFunctionHeader(
Options.get("MakeSmartPtrFunctionHeader", StdMemoryHeader)),
MakeSmartPtrFunctionName(
- Options.get("MakeSmartPtrFunction", MakeSmartPtrFunctionName)) {}
+ Options.get("MakeSmartPtrFunction", MakeSmartPtrFunctionName)),
+ IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", true)) {}
void MakeSmartPtrCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "IncludeStyle", IncludeStyle);
Options.store(Opts, "MakeSmartPtrFunctionHeader", MakeSmartPtrFunctionHeader);
Options.store(Opts, "MakeSmartPtrFunction", MakeSmartPtrFunctionName);
+ Options.store(Opts, "IgnoreMacros", IgnoreMacros);
}
void MakeSmartPtrCheck::registerPPCallbacks(CompilerInstance &Compiler) {
@@ -122,6 +125,11 @@ void MakeSmartPtrCheck::checkConstruct(S
const QualType *Type,
const CXXNewExpr *New) {
SourceLocation ConstructCallStart = Construct->getExprLoc();
+ bool InMacro = ConstructCallStart.isMacroID();
+
+ if (InMacro && IgnoreMacros) {
+ return;
+ }
bool Invalid = false;
StringRef ExprStr = Lexer::getSourceText(
@@ -134,6 +142,11 @@ void MakeSmartPtrCheck::checkConstruct(S
auto Diag = diag(ConstructCallStart, "use %0 instead")
<< MakeSmartPtrFunctionName;
+ // Disable the fix in macros.
+ if (InMacro) {
+ return;
+ }
+
// Find the location of the template's left angle.
size_t LAngle = ExprStr.find("<");
SourceLocation ConstructCallEnd;
@@ -180,9 +193,20 @@ void MakeSmartPtrCheck::checkReset(Sourc
SourceLocation ExprEnd =
Lexer::getLocForEndOfToken(Expr->getLocEnd(), 0, SM, getLangOpts());
+ bool InMacro = ExprStart.isMacroID();
+
+ if (InMacro && IgnoreMacros) {
+ return;
+ }
+
auto Diag = diag(ResetCallStart, "use %0 instead")
<< MakeSmartPtrFunctionName;
+ // Disable the fix in macros.
+ if (InMacro) {
+ return;
+ }
+
Diag << FixItHint::CreateReplacement(
CharSourceRange::getCharRange(OperatorLoc, ExprEnd),
(llvm::Twine(" = ") + MakeSmartPtrFunctionName + "<" +
Modified: clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.h?rev=310050&r1=310049&r2=310050&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.h (original)
+++ clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.h Fri Aug 4 04:18:00 2017
@@ -50,6 +50,7 @@ private:
const utils::IncludeSorter::IncludeStyle IncludeStyle;
const std::string MakeSmartPtrFunctionHeader;
const std::string MakeSmartPtrFunctionName;
+ const bool IgnoreMacros;
void checkConstruct(SourceManager &SM, const CXXConstructExpr *Construct,
const QualType *Type, const CXXNewExpr *New);
Modified: clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-make-unique.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-make-unique.rst?rev=310050&r1=310049&r2=310050&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-make-unique.rst (original)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-make-unique.rst Fri Aug 4 04:18:00 2017
@@ -43,3 +43,8 @@ Options
A string specifying which include-style is used, `llvm` or `google`. Default
is `llvm`.
+
+.. option:: IgnoreMacros
+
+ If set to non-zero, the check will not give warnings inside macros. Default
+ is `1`.
Added: clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique-macros.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique-macros.cpp?rev=310050&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique-macros.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique-macros.cpp Fri Aug 4 04:18:00 2017
@@ -0,0 +1,28 @@
+// RUN: %check_clang_tidy %s modernize-make-unique %t -- \
+// RUN: -config="{CheckOptions: [{key: modernize-make-unique.IgnoreMacros, value: 0}]}" \
+// RUN: -- -std=c++11 -I%S/Inputs/modernize-smart-ptr
+
+#include "unique_ptr.h"
+
+class Foo {};
+class Bar {};
+#define DEFINE(...) __VA_ARGS__
+// CHECK-FIXES: {{^}}#define DEFINE(...) __VA_ARGS__{{$}}
+template<typename T>
+void g2(std::unique_ptr<Foo> *t) {
+ DEFINE(
+ // CHECK-FIXES: {{^ *}}DEFINE({{$}}
+ auto p = std::unique_ptr<Foo>(new Foo);
+ // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: use std::make_unique instead
+ // CHECK-FIXES: {{^ *}}auto p = std::unique_ptr<Foo>(new Foo);{{$}}
+ t->reset(new Foo);
+ // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use std::make_unique instead
+ // CHECK-FIXES: {{^ *}}t->reset(new Foo);{{$}}
+ );
+ // CHECK-FIXES: {{^ *}});{{$}}
+}
+void macro() {
+ std::unique_ptr<Foo> *t;
+ g2<Bar>(t);
+}
+#undef DEFINE
Modified: clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique.cpp?rev=310050&r1=310049&r2=310050&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique.cpp Fri Aug 4 04:18:00 2017
@@ -404,3 +404,14 @@ void reset() {
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead
// CHECK-FIXES: *Q = std::make_unique<int>();
}
+
+#define DEFINE(...) __VA_ARGS__
+template<typename T>
+void g2(std::unique_ptr<Foo> *t) {
+ DEFINE(auto p = std::unique_ptr<Foo>(new Foo); t->reset(new Foo););
+}
+void macro() {
+ std::unique_ptr<Foo> *t;
+ g2<bar::Bar>(t);
+}
+#undef DEFINE
More information about the cfe-commits
mailing list