[clang-tools-extra] r364922 - [clang-tidy] Extend TransformerClangTidyCheck to support adding includes.

Yitzhak Mandelbaum via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 2 06:25:07 PDT 2019


Author: ymandel
Date: Tue Jul  2 06:25:07 2019
New Revision: 364922

URL: http://llvm.org/viewvc/llvm-project?rev=364922&view=rev
Log:
[clang-tidy] Extend TransformerClangTidyCheck to support adding includes.

Summary:
This revision implements support for the `AddedIncludes` field in
RewriteRule cases; that is, it supports specifying the addition of include
directives in files modified by the clang tidy check.

Reviewers: ilya-biryukov, gribozavr

Subscribers: xazax.hun, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D63893

Modified:
    clang-tools-extra/trunk/clang-tidy/utils/TransformerClangTidyCheck.cpp
    clang-tools-extra/trunk/clang-tidy/utils/TransformerClangTidyCheck.h
    clang-tools-extra/trunk/unittests/clang-tidy/TransformerClangTidyCheckTest.cpp

Modified: clang-tools-extra/trunk/clang-tidy/utils/TransformerClangTidyCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/utils/TransformerClangTidyCheck.cpp?rev=364922&r1=364921&r2=364922&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/utils/TransformerClangTidyCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/utils/TransformerClangTidyCheck.cpp Tue Jul  2 06:25:07 2019
@@ -46,6 +46,19 @@ TransformerClangTidyCheck::TransformerCl
          " explicitly provide an empty explanation if none is desired");
 }
 
+void TransformerClangTidyCheck::registerPPCallbacks(
+    const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
+  // Only allocate and register the IncludeInsert when some `Case` will add
+  // includes.
+  if (Rule && llvm::any_of(Rule->Cases, [](const RewriteRule::Case &C) {
+        return !C.AddedIncludes.empty();
+      })) {
+    Inserter = llvm::make_unique<IncludeInserter>(
+        SM, getLangOpts(), utils::IncludeSorter::IS_LLVM);
+    PP->addPPCallbacks(Inserter->CreatePPCallbacks());
+  }
+}
+
 void TransformerClangTidyCheck::registerMatchers(
     ast_matchers::MatchFinder *Finder) {
   if (Rule)
@@ -89,6 +102,15 @@ void TransformerClangTidyCheck::check(
   for (const auto &T : *Transformations) {
     Diag << FixItHint::CreateReplacement(T.Range, T.Replacement);
   }
+
+  for (const auto &I : Case.AddedIncludes) {
+    auto &Header = I.first;
+    if (Optional<FixItHint> Fix = Inserter->CreateIncludeInsertion(
+            Result.SourceManager->getMainFileID(), Header,
+            /*IsAngled=*/I.second == tooling::IncludeFormat::Angled)) {
+      Diag << *Fix;
+    }
+  }
 }
 
 } // namespace utils

Modified: clang-tools-extra/trunk/clang-tidy/utils/TransformerClangTidyCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/utils/TransformerClangTidyCheck.h?rev=364922&r1=364921&r2=364922&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/utils/TransformerClangTidyCheck.h (original)
+++ clang-tools-extra/trunk/clang-tidy/utils/TransformerClangTidyCheck.h Tue Jul  2 06:25:07 2019
@@ -10,7 +10,9 @@
 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_TRANSFORMER_CLANG_TIDY_CHECK_H
 
 #include "../ClangTidy.h"
+#include "../utils/IncludeInserter.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Frontend/CompilerInstance.h"
 #include "clang/Tooling/Refactoring/Transformer.h"
 #include <deque>
 #include <vector>
@@ -52,11 +54,14 @@ public:
   TransformerClangTidyCheck(tooling::RewriteRule R, StringRef Name,
                             ClangTidyContext *Context);
 
+  void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
+                           Preprocessor *ModuleExpanderPP) override;
   void registerMatchers(ast_matchers::MatchFinder *Finder) final;
   void check(const ast_matchers::MatchFinder::MatchResult &Result) final;
 
 private:
   Optional<tooling::RewriteRule> Rule;
+  std::unique_ptr<clang::tidy::utils::IncludeInserter> Inserter;
 };
 
 } // namespace utils

Modified: clang-tools-extra/trunk/unittests/clang-tidy/TransformerClangTidyCheckTest.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clang-tidy/TransformerClangTidyCheckTest.cpp?rev=364922&r1=364921&r2=364922&view=diff
==============================================================================
--- clang-tools-extra/trunk/unittests/clang-tidy/TransformerClangTidyCheckTest.cpp (original)
+++ clang-tools-extra/trunk/unittests/clang-tidy/TransformerClangTidyCheckTest.cpp Tue Jul  2 06:25:07 2019
@@ -19,6 +19,7 @@ namespace tidy {
 namespace utils {
 namespace {
 using tooling::change;
+using tooling::IncludeFormat;
 using tooling::RewriteRule;
 using tooling::text;
 using tooling::stencil::cat;
@@ -121,6 +122,54 @@ TEST(TransformerClangTidyCheckTest, Disa
                           Input, nullptr, "input.cc", None, Options));
 }
 
+RewriteRule replaceCall(IncludeFormat Format) {
+  using namespace ::clang::ast_matchers;
+  RewriteRule Rule = makeRule(callExpr(callee(functionDecl(hasName("f")))),
+                              change(text("other()")), text("no message"));
+  addInclude(Rule, "clang/OtherLib.h", Format);
+  return Rule;
+}
+
+template <IncludeFormat Format>
+class IncludeCheck : public TransformerClangTidyCheck {
+public:
+  IncludeCheck(StringRef Name, ClangTidyContext *Context)
+      : TransformerClangTidyCheck(replaceCall(Format), Name, Context) {}
+};
+
+TEST(TransformerClangTidyCheckTest, AddIncludeQuoted) {
+
+  std::string Input = R"cc(
+    int f(int x);
+    int h(int x) { return f(x); }
+  )cc";
+  std::string Expected = R"cc(#include "clang/OtherLib.h"
+
+
+    int f(int x);
+    int h(int x) { return other(); }
+  )cc";
+
+  EXPECT_EQ(Expected,
+            test::runCheckOnCode<IncludeCheck<IncludeFormat::Quoted>>(Input));
+}
+
+TEST(TransformerClangTidyCheckTest, AddIncludeAngled) {
+  std::string Input = R"cc(
+    int f(int x);
+    int h(int x) { return f(x); }
+  )cc";
+  std::string Expected = R"cc(#include <clang/OtherLib.h>
+
+
+    int f(int x);
+    int h(int x) { return other(); }
+  )cc";
+
+  EXPECT_EQ(Expected,
+            test::runCheckOnCode<IncludeCheck<IncludeFormat::Angled>>(Input));
+}
+
 } // namespace
 } // namespace utils
 } // namespace tidy




More information about the cfe-commits mailing list