[PATCH] D11677: [clang-tidy] Support replacements in macro arguments in misc-inefficient-algorithm

Alexander Kornienko alexfh at google.com
Fri Jul 31 04:32:37 PDT 2015


alexfh created this revision.
alexfh added a reviewer: klimek.
alexfh added a subscriber: cfe-commits.

Support replacements in macro arguments in the
misc-inefficient-algorithm check.

http://reviews.llvm.org/D11677

Files:
  clang-tidy/misc/InefficientAlgorithmCheck.cpp
  test/clang-tidy/misc-inefficient-algorithm.cpp

Index: test/clang-tidy/misc-inefficient-algorithm.cpp
===================================================================
--- test/clang-tidy/misc-inefficient-algorithm.cpp
+++ test/clang-tidy/misc-inefficient-algorithm.cpp
@@ -76,6 +76,12 @@
   auto c = count(s.begin(), s.end(), 43);
   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: this STL algorithm call should be
   // CHECK-FIXES: {{^  }}auto c = s.count(43);{{$}}
+
+#define SECOND(x, y, z) y
+  SECOND(q,std::count(s.begin(), s.end(), 22),w);
+  // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: this STL algorithm call should be
+  // CHECK-FIXES: {{^  }}SECOND(q,s.count(22),w);{{$}}
+
   it = find_if(s.begin(), s.end(), [](int) { return false; });
 
   std::multiset<int> ms;
Index: clang-tidy/misc/InefficientAlgorithmCheck.cpp
===================================================================
--- clang-tidy/misc/InefficientAlgorithmCheck.cpp
+++ clang-tidy/misc/InefficientAlgorithmCheck.cpp
@@ -105,18 +105,30 @@
   const auto *IneffContExpr = Result.Nodes.getNodeAs<Expr>("IneffContExpr");
   FixItHint Hint;
 
-  if (!AlgCall->getLocStart().isMacroID() && !Maplike && CompatibleTypes) {
+  SourceManager &SM = *Result.SourceManager;
+  LangOptions LangOpts = Result.Context->getLangOpts();
+
+  CharSourceRange CallRange =
+      CharSourceRange::getTokenRange(AlgCall->getSourceRange());
+
+  if (SM.isMacroArgExpansion(CallRange.getBegin()) &&
+      SM.isMacroArgExpansion(CallRange.getEnd())) {
+    CallRange.setBegin(SM.getSpellingLoc(CallRange.getBegin()));
+    CallRange.setEnd(SM.getSpellingLoc(CallRange.getEnd()));
+  }
+
+  if (!CallRange.getBegin().isMacroID() && !Maplike && CompatibleTypes) {
+    StringRef ContainerText = Lexer::getSourceText(
+        CharSourceRange::getTokenRange(IneffContExpr->getSourceRange()), SM,
+        LangOpts);
+    StringRef ParamText = Lexer::getSourceText(
+        CharSourceRange::getTokenRange(AlgParam->getSourceRange()), SM,
+        LangOpts);
     std::string ReplacementText =
-        (llvm::Twine(Lexer::getSourceText(
-             CharSourceRange::getTokenRange(IneffContExpr->getSourceRange()),
-             *Result.SourceManager, Result.Context->getLangOpts())) +
-         (PtrToContainer ? "->" : ".") + AlgDecl->getName() + "(" +
-         Lexer::getSourceText(
-             CharSourceRange::getTokenRange(AlgParam->getSourceRange()),
-             *Result.SourceManager, Result.Context->getLangOpts()) +
-         ")").str();
-    Hint = FixItHint::CreateReplacement(AlgCall->getSourceRange(),
-                                        ReplacementText);
+        (llvm::Twine(ContainerText) + (PtrToContainer ? "->" : ".") +
+         AlgDecl->getName() + "(" + ParamText + ")")
+            .str();
+    Hint = FixItHint::CreateReplacement(CallRange, ReplacementText);
   }
 
   diag(AlgCall->getLocStart(),


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D11677.31117.patch
Type: text/x-patch
Size: 2845 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150731/af3cc0c2/attachment.bin>


More information about the cfe-commits mailing list