summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHaojian Wu <hokein@google.com>2017-08-04 11:18:00 +0000
committerHaojian Wu <hokein@google.com>2017-08-04 11:18:00 +0000
commit115b707584ef942d7c52fd6226d140619e03fdd8 (patch)
treea7b3d44d2b86e1f319ff75fc3403e2652c6082e7
parent70ecb827b48957a6aeb3fe72930e824693a48c91 (diff)
downloadbcm5719-llvm-115b707584ef942d7c52fd6226d140619e03fdd8.tar.gz
bcm5719-llvm-115b707584ef942d7c52fd6226d140619e03fdd8.zip
[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 llvm-svn: 310050
-rw-r--r--clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp28
-rw-r--r--clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.h1
-rw-r--r--clang-tools-extra/docs/clang-tidy/checks/modernize-make-unique.rst5
-rw-r--r--clang-tools-extra/test/clang-tidy/modernize-make-unique-macros.cpp28
-rw-r--r--clang-tools-extra/test/clang-tidy/modernize-make-unique.cpp11
5 files changed, 71 insertions, 2 deletions
diff --git a/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp b/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp
index 88a7ab11f64..c73462c537a 100644
--- a/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp
@@ -42,7 +42,8 @@ const char MakeSmartPtrCheck::ConstructorCall[] = "constructorCall";
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(StringRef Name, ClangTidyContext *Context,
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(SourceManager &SM,
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(SourceManager &SM,
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(SourceManager &SM,
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 + "<" +
diff --git a/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.h b/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.h
index f01550e28fd..e8dc8577e5c 100644
--- a/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.h
+++ b/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.h
@@ -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);
diff --git a/clang-tools-extra/docs/clang-tidy/checks/modernize-make-unique.rst b/clang-tools-extra/docs/clang-tidy/checks/modernize-make-unique.rst
index e6b17db1f57..1961f05ed9c 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/modernize-make-unique.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/modernize-make-unique.rst
@@ -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`.
diff --git a/clang-tools-extra/test/clang-tidy/modernize-make-unique-macros.cpp b/clang-tools-extra/test/clang-tidy/modernize-make-unique-macros.cpp
new file mode 100644
index 00000000000..e3e547969ec
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/modernize-make-unique-macros.cpp
@@ -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
diff --git a/clang-tools-extra/test/clang-tidy/modernize-make-unique.cpp b/clang-tools-extra/test/clang-tidy/modernize-make-unique.cpp
index cd4d057dac2..f1264cbb607 100644
--- a/clang-tools-extra/test/clang-tidy/modernize-make-unique.cpp
+++ b/clang-tools-extra/test/clang-tidy/modernize-make-unique.cpp
@@ -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
OpenPOWER on IntegriCloud