[clang-tools-extra] 13c9bbc - [clang-tidy] Refactor IncludeInserter
Nathan James via cfe-commits
cfe-commits at lists.llvm.org
Mon Jul 27 04:49:05 PDT 2020
Author: Nathan James
Date: 2020-07-27T12:48:55+01:00
New Revision: 13c9bbc28ef9cf9976a0962e6c930a7dfc52c877
URL: https://github.com/llvm/llvm-project/commit/13c9bbc28ef9cf9976a0962e6c930a7dfc52c877
DIFF: https://github.com/llvm/llvm-project/commit/13c9bbc28ef9cf9976a0962e6c930a7dfc52c877.diff
LOG: [clang-tidy] Refactor IncludeInserter
Simplified how `IncludeInserter` is used in Checks by abstracting away the SourceManager and PPCallbacks inside the method `registerPreprocessor`.
Changed checks that use `IncludeInserter` to no longer use a `std::unique_ptr`, instead the IncludeInserter is just a member of the class thats initialized with an `IncludeStyle`.
Saving an unnecessary allocation.
This results in the removal of the field `IncludeSorter::IncludeStyle` from the checks, as its wrapped in the `IncludeInserter`.
No longer need to create an instance of the `IncludeInserter` in the registerPPCallbacks, now that method only needs to contain:
```
Inserter.registerPreprocessor(PP);
```
Also added a helper method to `IncludeInserter` called `createMainFileInclusionInsertion`, purely sugar but does better express intentions.
Reviewed By: aaron.ballman
Differential Revision: https://reviews.llvm.org/D83680
Added:
Modified:
clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.cpp
clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h
clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.h
clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp
clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.h
clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp
clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.h
clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp
clang-tools-extra/clang-tidy/modernize/PassByValueCheck.h
clang-tools-extra/clang-tidy/modernize/ReplaceAutoPtrCheck.cpp
clang-tools-extra/clang-tidy/modernize/ReplaceAutoPtrCheck.h
clang-tools-extra/clang-tidy/modernize/ReplaceRandomShuffleCheck.cpp
clang-tools-extra/clang-tidy/modernize/ReplaceRandomShuffleCheck.h
clang-tools-extra/clang-tidy/performance/MoveConstructorInitCheck.cpp
clang-tools-extra/clang-tidy/performance/MoveConstructorInitCheck.h
clang-tools-extra/clang-tidy/performance/TypePromotionInMathFnCheck.cpp
clang-tools-extra/clang-tidy/performance/TypePromotionInMathFnCheck.h
clang-tools-extra/clang-tidy/performance/UnnecessaryValueParamCheck.cpp
clang-tools-extra/clang-tidy/performance/UnnecessaryValueParamCheck.h
clang-tools-extra/clang-tidy/utils/IncludeInserter.cpp
clang-tools-extra/clang-tidy/utils/IncludeInserter.h
clang-tools-extra/clang-tidy/utils/TransformerClangTidyCheck.cpp
clang-tools-extra/clang-tidy/utils/TransformerClangTidyCheck.h
clang-tools-extra/unittests/clang-tidy/IncludeInserterTest.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.cpp b/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.cpp
index 11bbcbcb527f..e775fc21d2d0 100644
--- a/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.cpp
+++ b/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.cpp
@@ -26,8 +26,8 @@ StringFindStartswithCheck::StringFindStartswithCheck(StringRef Name,
: ClangTidyCheck(Name, Context),
StringLikeClasses(utils::options::parseStringList(
Options.get("StringLikeClasses", "::std::basic_string"))),
- IncludeStyle(Options.getLocalOrGlobal("IncludeStyle",
- utils::IncludeSorter::IS_LLVM)),
+ IncludeInserter(Options.getLocalOrGlobal("IncludeStyle",
+ utils::IncludeSorter::IS_LLVM)),
AbseilStringsMatchHeader(
Options.get("AbseilStringsMatchHeader", "absl/strings/match.h")) {}
@@ -105,23 +105,21 @@ void StringFindStartswithCheck::check(const MatchFinder::MatchResult &Result) {
// Create a preprocessor #include FixIt hint (CreateIncludeInsertion checks
// whether this already exists).
- Diagnostic << IncludeInserter->CreateIncludeInsertion(
+ Diagnostic << IncludeInserter.createIncludeInsertion(
Source.getFileID(ComparisonExpr->getBeginLoc()), AbseilStringsMatchHeader,
false);
}
void StringFindStartswithCheck::registerPPCallbacks(
const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
- IncludeInserter = std::make_unique<utils::IncludeInserter>(SM, getLangOpts(),
- IncludeStyle);
- PP->addPPCallbacks(IncludeInserter->CreatePPCallbacks());
+ IncludeInserter.registerPreprocessor(PP);
}
void StringFindStartswithCheck::storeOptions(
ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "StringLikeClasses",
utils::options::serializeStringList(StringLikeClasses));
- Options.store(Opts, "IncludeStyle", IncludeStyle);
+ Options.store(Opts, "IncludeStyle", IncludeInserter.getStyle());
Options.store(Opts, "AbseilStringsMatchHeader", AbseilStringsMatchHeader);
}
diff --git a/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h b/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h
index d232d3b3efb6..2bb20f78b68c 100644
--- a/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h
+++ b/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h
@@ -35,9 +35,8 @@ class StringFindStartswithCheck : public ClangTidyCheck {
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
private:
- std::unique_ptr<clang::tidy::utils::IncludeInserter> IncludeInserter;
const std::vector<std::string> StringLikeClasses;
- const utils::IncludeSorter::IncludeStyle IncludeStyle;
+ utils::IncludeInserter IncludeInserter;
const std::string AbseilStringsMatchHeader;
};
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
index f1755d3f9b85..3f51ef595b46 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
@@ -26,12 +26,12 @@ AST_MATCHER(VarDecl, isLocalVarDecl) { return Node.isLocalVarDecl(); }
InitVariablesCheck::InitVariablesCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
- IncludeStyle(Options.getLocalOrGlobal("IncludeStyle",
- utils::IncludeSorter::IS_LLVM)),
+ IncludeInserter(Options.getLocalOrGlobal("IncludeStyle",
+ utils::IncludeSorter::IS_LLVM)),
MathHeader(Options.get("MathHeader", "math.h")) {}
void InitVariablesCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
- Options.store(Opts, "IncludeStyle", IncludeStyle);
+ Options.store(Opts, "IncludeStyle", IncludeInserter.getStyle());
Options.store(Opts, "MathHeader", MathHeader);
}
@@ -51,9 +51,7 @@ void InitVariablesCheck::registerMatchers(MatchFinder *Finder) {
void InitVariablesCheck::registerPPCallbacks(const SourceManager &SM,
Preprocessor *PP,
Preprocessor *ModuleExpanderPP) {
- IncludeInserter =
- std::make_unique<utils::IncludeInserter>(SM, getLangOpts(), IncludeStyle);
- PP->addPPCallbacks(IncludeInserter->CreatePPCallbacks());
+ IncludeInserter.registerPreprocessor(PP);
}
void InitVariablesCheck::check(const MatchFinder::MatchResult &Result) {
@@ -104,7 +102,7 @@ void InitVariablesCheck::check(const MatchFinder::MatchResult &Result) {
MatchedDecl->getName().size()),
InitializationString);
if (AddMathInclude) {
- Diagnostic << IncludeInserter->CreateIncludeInsertion(
+ Diagnostic << IncludeInserter.createIncludeInsertion(
Source.getFileID(MatchedDecl->getBeginLoc()), MathHeader, false);
}
}
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.h b/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.h
index 61521b118a99..0f778104ce39 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.h
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.h
@@ -31,8 +31,7 @@ class InitVariablesCheck : public ClangTidyCheck {
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
private:
- std::unique_ptr<clang::tidy::utils::IncludeInserter> IncludeInserter;
- const utils::IncludeSorter::IncludeStyle IncludeStyle;
+ utils::IncludeInserter IncludeInserter;
const std::string MathHeader;
};
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp
index 96b0bb0f9b02..f45801f1ea72 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp
@@ -21,20 +21,18 @@ namespace cppcoreguidelines {
ProBoundsConstantArrayIndexCheck::ProBoundsConstantArrayIndexCheck(
StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context), GslHeader(Options.get("GslHeader", "")),
- IncludeStyle(Options.getLocalOrGlobal("IncludeStyle",
- utils::IncludeSorter::IS_LLVM)) {}
+ Inserter(Options.getLocalOrGlobal("IncludeStyle",
+ utils::IncludeSorter::IS_LLVM)) {}
void ProBoundsConstantArrayIndexCheck::storeOptions(
ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "GslHeader", GslHeader);
- Options.store(Opts, "IncludeStyle", IncludeStyle);
+ Options.store(Opts, "IncludeStyle", Inserter.getStyle());
}
void ProBoundsConstantArrayIndexCheck::registerPPCallbacks(
const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
- Inserter = std::make_unique<utils::IncludeInserter>(SM, getLangOpts(),
- IncludeStyle);
- PP->addPPCallbacks(Inserter->CreatePPCallbacks());
+ Inserter.registerPreprocessor(PP);
}
void ProBoundsConstantArrayIndexCheck::registerMatchers(MatchFinder *Finder) {
@@ -87,9 +85,8 @@ void ProBoundsConstantArrayIndexCheck::check(
IndexRange.getBegin().getLocWithOffset(-1)),
", ")
<< FixItHint::CreateReplacement(Matched->getEndLoc(), ")")
- << Inserter->CreateIncludeInsertion(
- Result.SourceManager->getMainFileID(), GslHeader,
- /*IsAngled=*/false);
+ << Inserter.createMainFileIncludeInsertion(GslHeader,
+ /*IsAngled=*/false);
}
return;
}
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.h b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.h
index ac7475b4372d..04a51b93a04c 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.h
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.h
@@ -23,8 +23,7 @@ namespace cppcoreguidelines {
/// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines-pro-bounds-constant-array-index.html
class ProBoundsConstantArrayIndexCheck : public ClangTidyCheck {
const std::string GslHeader;
- const utils::IncludeSorter::IncludeStyle IncludeStyle;
- std::unique_ptr<utils::IncludeInserter> Inserter;
+ utils::IncludeInserter Inserter;
public:
ProBoundsConstantArrayIndexCheck(StringRef Name, ClangTidyContext *Context);
diff --git a/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp b/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp
index c677043946f7..5818b8cd06b5 100644
--- a/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp
@@ -44,8 +44,8 @@ const char MakeSmartPtrCheck::PointerType[] = "pointerType";
MakeSmartPtrCheck::MakeSmartPtrCheck(StringRef Name, ClangTidyContext *Context,
StringRef MakeSmartPtrFunctionName)
: ClangTidyCheck(Name, Context),
- IncludeStyle(Options.getLocalOrGlobal("IncludeStyle",
- utils::IncludeSorter::IS_LLVM)),
+ Inserter(Options.getLocalOrGlobal("IncludeStyle",
+ utils::IncludeSorter::IS_LLVM)),
MakeSmartPtrFunctionHeader(
Options.get("MakeSmartPtrFunctionHeader", StdMemoryHeader)),
MakeSmartPtrFunctionName(
@@ -53,7 +53,7 @@ MakeSmartPtrCheck::MakeSmartPtrCheck(StringRef Name, ClangTidyContext *Context,
IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", true)) {}
void MakeSmartPtrCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
- Options.store(Opts, "IncludeStyle", IncludeStyle);
+ Options.store(Opts, "IncludeStyle", Inserter.getStyle());
Options.store(Opts, "MakeSmartPtrFunctionHeader", MakeSmartPtrFunctionHeader);
Options.store(Opts, "MakeSmartPtrFunction", MakeSmartPtrFunctionName);
Options.store(Opts, "IgnoreMacros", IgnoreMacros);
@@ -67,9 +67,7 @@ bool MakeSmartPtrCheck::isLanguageVersionSupported(
void MakeSmartPtrCheck::registerPPCallbacks(const SourceManager &SM,
Preprocessor *PP,
Preprocessor *ModuleExpanderPP) {
- Inserter = std::make_unique<utils::IncludeInserter>(SM, getLangOpts(),
- IncludeStyle);
- PP->addPPCallbacks(Inserter->CreatePPCallbacks());
+ Inserter.registerPreprocessor(PP);
}
void MakeSmartPtrCheck::registerMatchers(ast_matchers::MatchFinder *Finder) {
@@ -432,7 +430,7 @@ void MakeSmartPtrCheck::insertHeader(DiagnosticBuilder &Diag, FileID FD) {
if (MakeSmartPtrFunctionHeader.empty()) {
return;
}
- Diag << Inserter->CreateIncludeInsertion(
+ Diag << Inserter.createIncludeInsertion(
FD, MakeSmartPtrFunctionHeader,
/*IsAngled=*/MakeSmartPtrFunctionHeader == StdMemoryHeader);
}
diff --git a/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.h b/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.h
index 1f738737ab26..7a1bba624c53 100644
--- a/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.h
+++ b/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.h
@@ -46,8 +46,7 @@ class MakeSmartPtrCheck : public ClangTidyCheck {
static const char PointerType[];
private:
- std::unique_ptr<utils::IncludeInserter> Inserter;
- const utils::IncludeSorter::IncludeStyle IncludeStyle;
+ utils::IncludeInserter Inserter;
const std::string MakeSmartPtrFunctionHeader;
const std::string MakeSmartPtrFunctionName;
const bool IgnoreMacros;
diff --git a/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp b/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp
index b6dedfbc2b6e..b955ea7f7572 100644
--- a/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp
@@ -120,12 +120,12 @@ collectParamDecls(const CXXConstructorDecl *Ctor,
PassByValueCheck::PassByValueCheck(StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
- IncludeStyle(Options.getLocalOrGlobal("IncludeStyle",
- utils::IncludeSorter::IS_LLVM)),
+ Inserter(Options.getLocalOrGlobal("IncludeStyle",
+ utils::IncludeSorter::IS_LLVM)),
ValuesOnly(Options.get("ValuesOnly", false)) {}
void PassByValueCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
- Options.store(Opts, "IncludeStyle", IncludeStyle);
+ Options.store(Opts, "IncludeStyle", Inserter.getStyle());
Options.store(Opts, "ValuesOnly", ValuesOnly);
}
@@ -167,9 +167,7 @@ void PassByValueCheck::registerMatchers(MatchFinder *Finder) {
void PassByValueCheck::registerPPCallbacks(const SourceManager &SM,
Preprocessor *PP,
Preprocessor *ModuleExpanderPP) {
- Inserter = std::make_unique<utils::IncludeInserter>(SM, getLangOpts(),
- IncludeStyle);
- PP->addPPCallbacks(Inserter->CreatePPCallbacks());
+ Inserter.registerPreprocessor(PP);
}
void PassByValueCheck::check(const MatchFinder::MatchResult &Result) {
@@ -216,7 +214,7 @@ void PassByValueCheck::check(const MatchFinder::MatchResult &Result) {
Diag << FixItHint::CreateInsertion(Initializer->getRParenLoc(), ")")
<< FixItHint::CreateInsertion(
Initializer->getLParenLoc().getLocWithOffset(1), "std::move(")
- << Inserter->CreateIncludeInsertion(
+ << Inserter.createIncludeInsertion(
Result.SourceManager->getFileID(Initializer->getSourceLocation()),
"utility",
/*IsAngled=*/true);
diff --git a/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.h b/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.h
index 7abda91ac563..82cd9d44c5e3 100644
--- a/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.h
+++ b/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.h
@@ -31,8 +31,7 @@ class PassByValueCheck : public ClangTidyCheck {
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
private:
- std::unique_ptr<utils::IncludeInserter> Inserter;
- const utils::IncludeSorter::IncludeStyle IncludeStyle;
+ utils::IncludeInserter Inserter;
const bool ValuesOnly;
};
diff --git a/clang-tools-extra/clang-tidy/modernize/ReplaceAutoPtrCheck.cpp b/clang-tools-extra/clang-tidy/modernize/ReplaceAutoPtrCheck.cpp
index f98254dbf7c8..25ffbe2b8738 100644
--- a/clang-tools-extra/clang-tidy/modernize/ReplaceAutoPtrCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/ReplaceAutoPtrCheck.cpp
@@ -74,11 +74,11 @@ AST_MATCHER(Decl, isFromStdNamespace) {
ReplaceAutoPtrCheck::ReplaceAutoPtrCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
- IncludeStyle(Options.getLocalOrGlobal("IncludeStyle",
- utils::IncludeSorter::IS_LLVM)) {}
+ Inserter(Options.getLocalOrGlobal("IncludeStyle",
+ utils::IncludeSorter::IS_LLVM)) {}
void ReplaceAutoPtrCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
- Options.store(Opts, "IncludeStyle", IncludeStyle);
+ Options.store(Opts, "IncludeStyle", Inserter.getStyle());
}
void ReplaceAutoPtrCheck::registerMatchers(MatchFinder *Finder) {
@@ -131,9 +131,7 @@ void ReplaceAutoPtrCheck::registerMatchers(MatchFinder *Finder) {
void ReplaceAutoPtrCheck::registerPPCallbacks(const SourceManager &SM,
Preprocessor *PP,
Preprocessor *ModuleExpanderPP) {
- Inserter = std::make_unique<utils::IncludeInserter>(SM, getLangOpts(),
- IncludeStyle);
- PP->addPPCallbacks(Inserter->CreatePPCallbacks());
+ Inserter.registerPreprocessor(PP);
}
void ReplaceAutoPtrCheck::check(const MatchFinder::MatchResult &Result) {
@@ -146,12 +144,11 @@ void ReplaceAutoPtrCheck::check(const MatchFinder::MatchResult &Result) {
if (Range.isInvalid())
return;
- auto Diag =
- diag(Range.getBegin(), "use std::move to transfer ownership")
- << FixItHint::CreateInsertion(Range.getBegin(), "std::move(")
- << FixItHint::CreateInsertion(Range.getEnd(), ")")
- << Inserter->CreateIncludeInsertion(SM.getMainFileID(), "utility",
- /*IsAngled=*/true);
+ auto Diag = diag(Range.getBegin(), "use std::move to transfer ownership")
+ << FixItHint::CreateInsertion(Range.getBegin(), "std::move(")
+ << FixItHint::CreateInsertion(Range.getEnd(), ")")
+ << Inserter.createMainFileIncludeInsertion("utility",
+ /*IsAngled=*/true);
return;
}
diff --git a/clang-tools-extra/clang-tidy/modernize/ReplaceAutoPtrCheck.h b/clang-tools-extra/clang-tidy/modernize/ReplaceAutoPtrCheck.h
index e2b04073a65f..8288c7e47d35 100644
--- a/clang-tools-extra/clang-tidy/modernize/ReplaceAutoPtrCheck.h
+++ b/clang-tools-extra/clang-tidy/modernize/ReplaceAutoPtrCheck.h
@@ -53,8 +53,7 @@ class ReplaceAutoPtrCheck : public ClangTidyCheck {
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
private:
- std::unique_ptr<utils::IncludeInserter> Inserter;
- const utils::IncludeSorter::IncludeStyle IncludeStyle;
+ utils::IncludeInserter Inserter;
};
} // namespace modernize
diff --git a/clang-tools-extra/clang-tidy/modernize/ReplaceRandomShuffleCheck.cpp b/clang-tools-extra/clang-tidy/modernize/ReplaceRandomShuffleCheck.cpp
index 66917df3e91d..0191f5d5c5de 100644
--- a/clang-tools-extra/clang-tidy/modernize/ReplaceRandomShuffleCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/ReplaceRandomShuffleCheck.cpp
@@ -23,8 +23,9 @@ namespace modernize {
ReplaceRandomShuffleCheck::ReplaceRandomShuffleCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
- IncludeStyle(Options.getLocalOrGlobal("IncludeStyle",
- utils::IncludeSorter::IS_LLVM)) {}
+ IncludeInserter(Options.getLocalOrGlobal("IncludeStyle",
+ utils::IncludeSorter::IS_LLVM)) {
+}
void ReplaceRandomShuffleCheck::registerMatchers(MatchFinder *Finder) {
const auto Begin = hasArgument(0, expr());
@@ -44,14 +45,12 @@ void ReplaceRandomShuffleCheck::registerMatchers(MatchFinder *Finder) {
void ReplaceRandomShuffleCheck::registerPPCallbacks(
const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
- IncludeInserter = std::make_unique<utils::IncludeInserter>(SM, getLangOpts(),
- IncludeStyle);
- PP->addPPCallbacks(IncludeInserter->CreatePPCallbacks());
+ IncludeInserter.registerPreprocessor(PP);
}
void ReplaceRandomShuffleCheck::storeOptions(
ClangTidyOptions::OptionMap &Opts) {
- Options.store(Opts, "IncludeStyle", IncludeStyle);
+ Options.store(Opts, "IncludeStyle", IncludeInserter.getStyle());
}
void ReplaceRandomShuffleCheck::check(const MatchFinder::MatchResult &Result) {
@@ -92,7 +91,7 @@ void ReplaceRandomShuffleCheck::check(const MatchFinder::MatchResult &Result) {
Diag << FixItHint::CreateRemoval(MatchedDecl->getSourceRange());
Diag << FixItHint::CreateInsertion(MatchedDecl->getBeginLoc(), NewName);
- Diag << IncludeInserter->CreateIncludeInsertion(
+ Diag << IncludeInserter.createIncludeInsertion(
Result.Context->getSourceManager().getFileID(
MatchedCallExpr->getBeginLoc()),
"random", /*IsAngled=*/true);
diff --git a/clang-tools-extra/clang-tidy/modernize/ReplaceRandomShuffleCheck.h b/clang-tools-extra/clang-tidy/modernize/ReplaceRandomShuffleCheck.h
index c4ac74d6e662..990dcffc79a6 100644
--- a/clang-tools-extra/clang-tidy/modernize/ReplaceRandomShuffleCheck.h
+++ b/clang-tools-extra/clang-tidy/modernize/ReplaceRandomShuffleCheck.h
@@ -34,8 +34,7 @@ class ReplaceRandomShuffleCheck : public ClangTidyCheck {
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
private:
- std::unique_ptr<utils::IncludeInserter> IncludeInserter;
- const utils::IncludeSorter::IncludeStyle IncludeStyle;
+ utils::IncludeInserter IncludeInserter;
};
} // namespace modernize
diff --git a/clang-tools-extra/clang-tidy/performance/MoveConstructorInitCheck.cpp b/clang-tools-extra/clang-tidy/performance/MoveConstructorInitCheck.cpp
index 4cbb014867c4..6b42cd3180d7 100644
--- a/clang-tools-extra/clang-tidy/performance/MoveConstructorInitCheck.cpp
+++ b/clang-tools-extra/clang-tidy/performance/MoveConstructorInitCheck.cpp
@@ -23,8 +23,8 @@ namespace performance {
MoveConstructorInitCheck::MoveConstructorInitCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
- IncludeStyle(Options.getLocalOrGlobal("IncludeStyle",
- utils::IncludeSorter::IS_LLVM)) {}
+ Inserter(Options.getLocalOrGlobal("IncludeStyle",
+ utils::IncludeSorter::IS_LLVM)) {}
void MoveConstructorInitCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(
@@ -90,13 +90,11 @@ void MoveConstructorInitCheck::check(const MatchFinder::MatchResult &Result) {
void MoveConstructorInitCheck::registerPPCallbacks(
const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
- Inserter = std::make_unique<utils::IncludeInserter>(SM, getLangOpts(),
- IncludeStyle);
- PP->addPPCallbacks(Inserter->CreatePPCallbacks());
+ Inserter.registerPreprocessor(PP);
}
void MoveConstructorInitCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
- Options.store(Opts, "IncludeStyle", IncludeStyle);
+ Options.store(Opts, "IncludeStyle", Inserter.getStyle());
}
} // namespace performance
diff --git a/clang-tools-extra/clang-tidy/performance/MoveConstructorInitCheck.h b/clang-tools-extra/clang-tidy/performance/MoveConstructorInitCheck.h
index 0473978f29db..0b637b617782 100644
--- a/clang-tools-extra/clang-tidy/performance/MoveConstructorInitCheck.h
+++ b/clang-tools-extra/clang-tidy/performance/MoveConstructorInitCheck.h
@@ -36,8 +36,7 @@ class MoveConstructorInitCheck : public ClangTidyCheck {
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
private:
- std::unique_ptr<utils::IncludeInserter> Inserter;
- const utils::IncludeSorter::IncludeStyle IncludeStyle;
+ utils::IncludeInserter Inserter;
};
} // namespace performance
diff --git a/clang-tools-extra/clang-tidy/performance/TypePromotionInMathFnCheck.cpp b/clang-tools-extra/clang-tidy/performance/TypePromotionInMathFnCheck.cpp
index 597445d0fc26..2105aa9947bb 100644
--- a/clang-tools-extra/clang-tidy/performance/TypePromotionInMathFnCheck.cpp
+++ b/clang-tools-extra/clang-tidy/performance/TypePromotionInMathFnCheck.cpp
@@ -31,19 +31,18 @@ AST_MATCHER_P(Type, isBuiltinType, BuiltinType::Kind, Kind) {
TypePromotionInMathFnCheck::TypePromotionInMathFnCheck(
StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
- IncludeStyle(Options.getLocalOrGlobal("IncludeStyle",
- utils::IncludeSorter::IS_LLVM)) {}
+ IncludeInserter(Options.getLocalOrGlobal("IncludeStyle",
+ utils::IncludeSorter::IS_LLVM)) {
+}
void TypePromotionInMathFnCheck::registerPPCallbacks(
const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
- IncludeInserter = std::make_unique<utils::IncludeInserter>(SM, getLangOpts(),
- IncludeStyle);
- PP->addPPCallbacks(IncludeInserter->CreatePPCallbacks());
+ IncludeInserter.registerPreprocessor(PP);
}
void TypePromotionInMathFnCheck::storeOptions(
ClangTidyOptions::OptionMap &Opts) {
- Options.store(Opts, "IncludeStyle", IncludeStyle);
+ Options.store(Opts, "IncludeStyle", IncludeInserter.getStyle());
}
void TypePromotionInMathFnCheck::registerMatchers(MatchFinder *Finder) {
@@ -191,7 +190,7 @@ void TypePromotionInMathFnCheck::check(const MatchFinder::MatchResult &Result) {
// <math.h>, because the functions we're suggesting moving away from are all
// declared in <math.h>.
if (FnInCmath)
- Diag << IncludeInserter->CreateIncludeInsertion(
+ Diag << IncludeInserter.createIncludeInsertion(
Result.Context->getSourceManager().getFileID(Call->getBeginLoc()),
"cmath", /*IsAngled=*/true);
}
diff --git a/clang-tools-extra/clang-tidy/performance/TypePromotionInMathFnCheck.h b/clang-tools-extra/clang-tidy/performance/TypePromotionInMathFnCheck.h
index d1cc042ca6a3..dd7c1c090bad 100644
--- a/clang-tools-extra/clang-tidy/performance/TypePromotionInMathFnCheck.h
+++ b/clang-tools-extra/clang-tidy/performance/TypePromotionInMathFnCheck.h
@@ -36,8 +36,7 @@ class TypePromotionInMathFnCheck : public ClangTidyCheck {
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
private:
- std::unique_ptr<utils::IncludeInserter> IncludeInserter;
- const utils::IncludeSorter::IncludeStyle IncludeStyle;
+ utils::IncludeInserter IncludeInserter;
};
} // namespace performance
diff --git a/clang-tools-extra/clang-tidy/performance/UnnecessaryValueParamCheck.cpp b/clang-tools-extra/clang-tidy/performance/UnnecessaryValueParamCheck.cpp
index 5de53b1840f1..9aef5a868169 100644
--- a/clang-tools-extra/clang-tidy/performance/UnnecessaryValueParamCheck.cpp
+++ b/clang-tools-extra/clang-tidy/performance/UnnecessaryValueParamCheck.cpp
@@ -68,8 +68,8 @@ bool isExplicitTemplateSpecialization(const FunctionDecl &Function) {
UnnecessaryValueParamCheck::UnnecessaryValueParamCheck(
StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
- IncludeStyle(Options.getLocalOrGlobal("IncludeStyle",
- utils::IncludeSorter::IS_LLVM)),
+ Inserter(Options.getLocalOrGlobal("IncludeStyle",
+ utils::IncludeSorter::IS_LLVM)),
AllowedTypes(
utils::options::parseStringList(Options.get("AllowedTypes", ""))) {}
@@ -173,14 +173,12 @@ void UnnecessaryValueParamCheck::check(const MatchFinder::MatchResult &Result) {
void UnnecessaryValueParamCheck::registerPPCallbacks(
const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
- Inserter = std::make_unique<utils::IncludeInserter>(SM, getLangOpts(),
- IncludeStyle);
- PP->addPPCallbacks(Inserter->CreatePPCallbacks());
+ Inserter.registerPreprocessor(PP);
}
void UnnecessaryValueParamCheck::storeOptions(
ClangTidyOptions::OptionMap &Opts) {
- Options.store(Opts, "IncludeStyle", IncludeStyle);
+ Options.store(Opts, "IncludeStyle", Inserter.getStyle());
Options.store(Opts, "AllowedTypes",
utils::options::serializeStringList(AllowedTypes));
}
@@ -204,7 +202,7 @@ void UnnecessaryValueParamCheck::handleMoveFix(const ParmVarDecl &Var,
Context.getLangOpts());
Diag << FixItHint::CreateInsertion(CopyArgument.getBeginLoc(), "std::move(")
<< FixItHint::CreateInsertion(EndLoc, ")")
- << Inserter->CreateIncludeInsertion(
+ << Inserter.createIncludeInsertion(
SM.getFileID(CopyArgument.getBeginLoc()), "utility",
/*IsAngled=*/true);
}
diff --git a/clang-tools-extra/clang-tidy/performance/UnnecessaryValueParamCheck.h b/clang-tools-extra/clang-tidy/performance/UnnecessaryValueParamCheck.h
index 1d2367148ef8..a84079e7da22 100644
--- a/clang-tools-extra/clang-tidy/performance/UnnecessaryValueParamCheck.h
+++ b/clang-tools-extra/clang-tidy/performance/UnnecessaryValueParamCheck.h
@@ -41,8 +41,7 @@ class UnnecessaryValueParamCheck : public ClangTidyCheck {
llvm::DenseMap<const FunctionDecl *, FunctionParmMutationAnalyzer>
MutationAnalyzers;
- std::unique_ptr<utils::IncludeInserter> Inserter;
- const utils::IncludeSorter::IncludeStyle IncludeStyle;
+ utils::IncludeInserter Inserter;
const std::vector<std::string> AllowedTypes;
};
diff --git a/clang-tools-extra/clang-tidy/utils/IncludeInserter.cpp b/clang-tools-extra/clang-tidy/utils/IncludeInserter.cpp
index df87dbe49cff..268692c3ba42 100644
--- a/clang-tools-extra/clang-tidy/utils/IncludeInserter.cpp
+++ b/clang-tools-extra/clang-tidy/utils/IncludeInserter.cpp
@@ -7,6 +7,8 @@
//===----------------------------------------------------------------------===//
#include "IncludeInserter.h"
+#include "clang/Lex/PPCallbacks.h"
+#include "clang/Lex/Preprocessor.h"
#include "clang/Lex/Token.h"
namespace clang {
@@ -26,7 +28,7 @@ class IncludeInserterCallback : public PPCallbacks {
StringRef /*SearchPath*/, StringRef /*RelativePath*/,
const Module * /*ImportedModule*/,
SrcMgr::CharacteristicKind /*FileType*/) override {
- Inserter->AddInclude(FileNameRef, IsAngled, HashLocation,
+ Inserter->addInclude(FileNameRef, IsAngled, HashLocation,
IncludeToken.getEndLoc());
}
@@ -34,45 +36,61 @@ class IncludeInserterCallback : public PPCallbacks {
IncludeInserter *Inserter;
};
-IncludeInserter::IncludeInserter(const SourceManager &SourceMgr,
- const LangOptions &LangOpts,
- IncludeSorter::IncludeStyle Style)
- : SourceMgr(SourceMgr), Style(Style) {}
+IncludeInserter::IncludeInserter(IncludeSorter::IncludeStyle Style)
+ : Style(Style) {}
-IncludeInserter::~IncludeInserter() {}
+void IncludeInserter::registerPreprocessor(Preprocessor *PP) {
+ assert(PP && "PP shouldn't be null");
+ SourceMgr = &PP->getSourceManager();
-std::unique_ptr<PPCallbacks> IncludeInserter::CreatePPCallbacks() {
- return std::make_unique<IncludeInserterCallback>(this);
+ // If this gets registered multiple times, clear the maps
+ if (!IncludeSorterByFile.empty())
+ IncludeSorterByFile.clear();
+ if (!InsertedHeaders.empty())
+ InsertedHeaders.clear();
+ PP->addPPCallbacks(std::make_unique<IncludeInserterCallback>(this));
}
IncludeSorter &IncludeInserter::getOrCreate(FileID FileID) {
+ assert(SourceMgr && "SourceMgr shouldn't be null; did you remember to call "
+ "registerPreprocessor()?");
// std::unique_ptr is cheap to construct, so force a construction now to save
// the lookup needed if we were to insert into the map.
std::unique_ptr<IncludeSorter> &Entry = IncludeSorterByFile[FileID];
if (!Entry) {
// If it wasn't found, Entry will be default constructed to nullptr.
Entry = std::make_unique<IncludeSorter>(
- &SourceMgr, FileID,
- SourceMgr.getFilename(SourceMgr.getLocForStartOfFile(FileID)), Style);
+ SourceMgr, FileID,
+ SourceMgr->getFilename(SourceMgr->getLocForStartOfFile(FileID)), Style);
}
return *Entry;
}
llvm::Optional<FixItHint>
-IncludeInserter::CreateIncludeInsertion(FileID FileID, StringRef Header,
+IncludeInserter::createIncludeInsertion(FileID FileID, StringRef Header,
bool IsAngled) {
// We assume the same Header will never be included both angled and not
// angled.
- if (!InsertedHeaders[FileID].insert(std::string(Header)).second)
+ if (!InsertedHeaders[FileID].insert(Header).second)
return llvm::None;
return getOrCreate(FileID).CreateIncludeInsertion(Header, IsAngled);
}
-void IncludeInserter::AddInclude(StringRef FileName, bool IsAngled,
+llvm::Optional<FixItHint>
+IncludeInserter::createMainFileIncludeInsertion(StringRef Header,
+ bool IsAngled) {
+ assert(SourceMgr && "SourceMgr shouldn't be null; did you remember to call "
+ "registerPreprocessor()?");
+ return createIncludeInsertion(SourceMgr->getMainFileID(), Header, IsAngled);
+}
+
+void IncludeInserter::addInclude(StringRef FileName, bool IsAngled,
SourceLocation HashLocation,
SourceLocation EndLocation) {
- FileID FileID = SourceMgr.getFileID(HashLocation);
+ assert(SourceMgr && "SourceMgr shouldn't be null; did you remember to call "
+ "registerPreprocessor()?");
+ FileID FileID = SourceMgr->getFileID(HashLocation);
getOrCreate(FileID).AddInclude(FileName, IsAngled, HashLocation, EndLocation);
}
diff --git a/clang-tools-extra/clang-tidy/utils/IncludeInserter.h b/clang-tools-extra/clang-tidy/utils/IncludeInserter.h
index 0d4b951beb1f..70c36ce8895c 100644
--- a/clang-tools-extra/clang-tidy/utils/IncludeInserter.h
+++ b/clang-tools-extra/clang-tidy/utils/IncludeInserter.h
@@ -11,13 +11,11 @@
#include "IncludeSorter.h"
#include "clang/Basic/Diagnostic.h"
-#include "clang/Basic/LangOptions.h"
-#include "clang/Basic/SourceManager.h"
-#include "clang/Lex/PPCallbacks.h"
+#include "llvm/ADT/StringSet.h"
#include <memory>
-#include <string>
namespace clang {
+class Preprocessor;
namespace tidy {
namespace utils {
@@ -26,16 +24,17 @@ namespace utils {
///
/// ``IncludeInserter`` can be used in clang-tidy checks in the following way:
/// \code
+/// #include "../ClangTidyCheck.h"
/// #include "../utils/IncludeInserter.h"
-/// #include "clang/Frontend/CompilerInstance.h"
+///
+/// namespace clang {
+/// namespace tidy {
///
/// class MyCheck : public ClangTidyCheck {
/// public:
/// void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
/// Preprocessor *ModuleExpanderPP) override {
-/// Inserter = std::make_unique<IncludeInserter>(
-/// SM, getLangOpts(), utils::IncludeSorter::IS_Google);
-/// PP->addPPCallbacks(Inserter->CreatePPCallbacks());
+/// Inserter.registerPreprocessor();
/// }
///
/// void registerMatchers(ast_matchers::MatchFinder* Finder) override { ... }
@@ -43,39 +42,53 @@ namespace utils {
/// void check(
/// const ast_matchers::MatchFinder::MatchResult& Result) override {
/// ...
-/// Inserter->CreateIncludeInsertion(
-/// Result.SourceManager->getMainFileID(), "path/to/Header.h",
-/// /*IsAngled=*/false);
+/// Inserter.createMainFileIncludeInsertion("path/to/Header.h",
+/// /*IsAngled=*/false);
/// ...
/// }
///
/// private:
-/// std::unique_ptr<clang::tidy::utils::IncludeInserter> Inserter;
+/// utils::IncludeInserter Inserter{utils::IncludeSorter::IS_Google};
/// };
+/// } // namespace tidy
+/// } // namespace clang
/// \endcode
class IncludeInserter {
public:
- IncludeInserter(const SourceManager &SourceMgr, const LangOptions &LangOpts,
- IncludeSorter::IncludeStyle Style);
- ~IncludeInserter();
+ /// Initializes the IncludeInserter using the IncludeStyle \p Style.
+ /// In most cases the \p Style will be retrieved from the ClangTidyOptions
+ /// using \code
+ /// Options.getLocalOrGlobal("IncludeStyle", <DefaultStyle>)
+ /// \endcode
+ explicit IncludeInserter(IncludeSorter::IncludeStyle Style);
+
+ /// Registers this with the Preprocessor \p PP, must be called before this
+ /// class is used.
+ void registerPreprocessor(Preprocessor *PP);
- /// Create ``PPCallbacks`` for registration with the compiler's preprocessor.
- std::unique_ptr<PPCallbacks> CreatePPCallbacks();
+ /// Creates a \p Header inclusion directive fixit in the File \p FileID.
+ /// Returns ``llvm::None`` on error or if the inclusion directive already
+ /// exists.
+ llvm::Optional<FixItHint>
+ createIncludeInsertion(FileID FileID, llvm::StringRef Header, bool IsAngled);
- /// Creates a \p Header inclusion directive fixit. Returns ``llvm::None`` on
- /// error or if inclusion directive already exists.
+ /// Creates a \p Header inclusion directive fixit in the main file.
+ /// Returns``llvm::None`` on error or if the inclusion directive already
+ /// exists.
llvm::Optional<FixItHint>
- CreateIncludeInsertion(FileID FileID, llvm::StringRef Header, bool IsAngled);
+ createMainFileIncludeInsertion(llvm::StringRef Header, bool IsAngled);
+
+ IncludeSorter::IncludeStyle getStyle() const { return Style; }
private:
- void AddInclude(StringRef FileName, bool IsAngled,
+ void addInclude(StringRef FileName, bool IsAngled,
SourceLocation HashLocation, SourceLocation EndLocation);
IncludeSorter &getOrCreate(FileID FileID);
llvm::DenseMap<FileID, std::unique_ptr<IncludeSorter>> IncludeSorterByFile;
- llvm::DenseMap<FileID, std::set<std::string>> InsertedHeaders;
- const SourceManager &SourceMgr;
+ llvm::DenseMap<FileID, llvm::StringSet<>> InsertedHeaders;
+ const SourceManager *SourceMgr{nullptr};
const IncludeSorter::IncludeStyle Style;
friend class IncludeInserterCallback;
};
diff --git a/clang-tools-extra/clang-tidy/utils/TransformerClangTidyCheck.cpp b/clang-tools-extra/clang-tidy/utils/TransformerClangTidyCheck.cpp
index 03af5dd1565f..2c116b210d05 100644
--- a/clang-tools-extra/clang-tidy/utils/TransformerClangTidyCheck.cpp
+++ b/clang-tools-extra/clang-tidy/utils/TransformerClangTidyCheck.cpp
@@ -32,8 +32,8 @@ TransformerClangTidyCheck::TransformerClangTidyCheck(
MakeRule,
StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context), Rule(MakeRule(getLangOpts(), Options)),
- IncludeStyle(Options.getLocalOrGlobal("IncludeStyle",
- IncludeSorter::IS_LLVM)) {
+ Inserter(
+ Options.getLocalOrGlobal("IncludeStyle", IncludeSorter::IS_LLVM)) {
if (Rule)
assert(llvm::all_of(Rule->Cases, hasExplanation) &&
"clang-tidy checks must have an explanation by default;"
@@ -44,8 +44,8 @@ TransformerClangTidyCheck::TransformerClangTidyCheck(RewriteRule R,
StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context), Rule(std::move(R)),
- IncludeStyle(Options.getLocalOrGlobal("IncludeStyle",
- IncludeSorter::IS_LLVM)) {
+ Inserter(
+ Options.getLocalOrGlobal("IncludeStyle", IncludeSorter::IS_LLVM)) {
assert(llvm::all_of(Rule->Cases, hasExplanation) &&
"clang-tidy checks must have an explanation by default;"
" explicitly provide an empty explanation if none is desired");
@@ -53,15 +53,12 @@ TransformerClangTidyCheck::TransformerClangTidyCheck(RewriteRule R,
void TransformerClangTidyCheck::registerPPCallbacks(
const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
- // Only allocate and register the IncludeInsert when some `Case` will add
+ // Only 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 =
- std::make_unique<IncludeInserter>(SM, getLangOpts(), IncludeStyle);
- PP->addPPCallbacks(Inserter->CreatePPCallbacks());
- }
+ }))
+ Inserter.registerPreprocessor(PP);
}
void TransformerClangTidyCheck::registerMatchers(
@@ -102,15 +99,15 @@ void TransformerClangTidyCheck::check(
Diag << FixItHint::CreateReplacement(T.Range, T.Replacement);
for (const auto &I : Case.AddedIncludes) {
- Diag << Inserter->CreateIncludeInsertion(
- Result.SourceManager->getMainFileID(), I.first,
+ Diag << Inserter.createMainFileIncludeInsertion(
+ I.first,
/*IsAngled=*/I.second == transformer::IncludeFormat::Angled);
}
}
void TransformerClangTidyCheck::storeOptions(
ClangTidyOptions::OptionMap &Opts) {
- Options.store(Opts, "IncludeStyle", IncludeStyle);
+ Options.store(Opts, "IncludeStyle", Inserter.getStyle());
}
} // namespace utils
diff --git a/clang-tools-extra/clang-tidy/utils/TransformerClangTidyCheck.h b/clang-tools-extra/clang-tidy/utils/TransformerClangTidyCheck.h
index 829a22fe8e2c..404f474a24ca 100644
--- a/clang-tools-extra/clang-tidy/utils/TransformerClangTidyCheck.h
+++ b/clang-tools-extra/clang-tidy/utils/TransformerClangTidyCheck.h
@@ -70,8 +70,7 @@ class TransformerClangTidyCheck : public ClangTidyCheck {
private:
Optional<transformer::RewriteRule> Rule;
- const IncludeSorter::IncludeStyle IncludeStyle;
- std::unique_ptr<IncludeInserter> Inserter;
+ IncludeInserter Inserter;
};
} // namespace utils
diff --git a/clang-tools-extra/unittests/clang-tidy/IncludeInserterTest.cpp b/clang-tools-extra/unittests/clang-tidy/IncludeInserterTest.cpp
index ed5f02576f04..e70d3fb91bf2 100644
--- a/clang-tools-extra/unittests/clang-tidy/IncludeInserterTest.cpp
+++ b/clang-tools-extra/unittests/clang-tidy/IncludeInserterTest.cpp
@@ -33,9 +33,7 @@ class IncludeInserterCheckBase : public ClangTidyCheck {
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
Preprocessor *ModuleExpanderPP) override {
- Inserter = std::make_unique<utils::IncludeInserter>(
- SM, getLangOpts(), utils::IncludeSorter::IS_Google);
- PP->addPPCallbacks(Inserter->CreatePPCallbacks());
+ Inserter.registerPreprocessor(PP);
}
void registerMatchers(ast_matchers::MatchFinder *Finder) override {
@@ -46,15 +44,15 @@ class IncludeInserterCheckBase : public ClangTidyCheck {
auto Diag = diag(Result.Nodes.getNodeAs<DeclStmt>("stmt")->getBeginLoc(),
"foo, bar");
for (StringRef Header : HeadersToInclude()) {
- Diag << Inserter->CreateIncludeInsertion(
- Result.SourceManager->getMainFileID(), Header, IsAngledInclude());
+ Diag << Inserter.createMainFileIncludeInsertion(Header,
+ IsAngledInclude());
}
}
virtual std::vector<StringRef> HeadersToInclude() const = 0;
virtual bool IsAngledInclude() const = 0;
- std::unique_ptr<utils::IncludeInserter> Inserter;
+ utils::IncludeInserter Inserter{utils::IncludeSorter::IS_Google};
};
class NonSystemHeaderInserterCheck : public IncludeInserterCheckBase {
More information about the cfe-commits
mailing list