[PATCH] D63784: [clang-tidy] Fix ClangTidyTest to initialize context before checks.
Yitzhak Mandelbaum via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Jun 26 08:04:53 PDT 2019
This revision was automatically updated to reflect the committed changes.
Closed by commit rL364435: [clang-tidy] Fix ClangTidyTest to initialize context before checks. (authored by ymandel, committed by ).
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
Changed prior to commit:
https://reviews.llvm.org/D63784?vs=206667&id=206675#toc
Repository:
rL LLVM
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D63784/new/
https://reviews.llvm.org/D63784
Files:
clang-tools-extra/trunk/unittests/clang-tidy/ClangTidyTest.h
Index: clang-tools-extra/trunk/unittests/clang-tidy/ClangTidyTest.h
===================================================================
--- clang-tools-extra/trunk/unittests/clang-tidy/ClangTidyTest.h
+++ clang-tools-extra/trunk/unittests/clang-tidy/ClangTidyTest.h
@@ -27,6 +27,25 @@
namespace tidy {
namespace test {
+template <typename Check, typename... Checks> struct CheckFactory {
+ static void
+ createChecks(ClangTidyContext *Context,
+ SmallVectorImpl<std::unique_ptr<ClangTidyCheck>> &Result) {
+ CheckFactory<Check>::createChecks(Context, Result);
+ CheckFactory<Checks...>::createChecks(Context, Result);
+ }
+};
+
+template <typename Check> struct CheckFactory<Check> {
+ static void
+ createChecks(ClangTidyContext *Context,
+ SmallVectorImpl<std::unique_ptr<ClangTidyCheck>> &Result) {
+ Result.emplace_back(llvm::make_unique<Check>(
+ "test-check-" + std::to_string(Result.size()), Context));
+ }
+};
+
+template <typename... CheckTypes>
class TestClangTidyAction : public ASTFrontendAction {
public:
TestClangTidyAction(SmallVectorImpl<std::unique_ptr<ClangTidyCheck>> &Checks,
@@ -42,6 +61,11 @@
Context.setASTContext(&Compiler.getASTContext());
Preprocessor *PP = &Compiler.getPreprocessor();
+
+ // Checks must be created here, _after_ `Context` has been initialized, so
+ // that check constructors can access the context (for example, through
+ // `getLangOpts()`).
+ CheckFactory<CheckTypes...>::createChecks(&Context, Checks);
for (auto &Check : Checks) {
Check->registerMatchers(&Finder);
Check->registerPPCallbacks(Compiler.getSourceManager(), PP, PP);
@@ -54,25 +78,7 @@
ClangTidyContext &Context;
};
-template <typename Check, typename... Checks> struct CheckFactory {
- static void
- createChecks(ClangTidyContext *Context,
- SmallVectorImpl<std::unique_ptr<ClangTidyCheck>> &Result) {
- CheckFactory<Check>::createChecks(Context, Result);
- CheckFactory<Checks...>::createChecks(Context, Result);
- }
-};
-
-template <typename Check> struct CheckFactory<Check> {
- static void
- createChecks(ClangTidyContext *Context,
- SmallVectorImpl<std::unique_ptr<ClangTidyCheck>> &Result) {
- Result.emplace_back(llvm::make_unique<Check>(
- "test-check-" + std::to_string(Result.size()), Context));
- }
-};
-
-template <typename... CheckList>
+template <typename... CheckTypes>
std::string
runCheckOnCode(StringRef Code, std::vector<ClangTidyError> *Errors = nullptr,
const Twine &Filename = "input.cc",
@@ -110,9 +116,9 @@
new FileManager(FileSystemOptions(), InMemoryFileSystem));
SmallVector<std::unique_ptr<ClangTidyCheck>, 1> Checks;
- CheckFactory<CheckList...>::createChecks(&Context, Checks);
tooling::ToolInvocation Invocation(
- Args, new TestClangTidyAction(Checks, Finder, Context), Files.get());
+ Args, new TestClangTidyAction<CheckTypes...>(Checks, Finder, Context),
+ Files.get());
InMemoryFileSystem->addFile(Filename, 0,
llvm::MemoryBuffer::getMemBuffer(Code));
for (const auto &FileContent : PathsToContent) {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D63784.206675.patch
Type: text/x-patch
Size: 3194 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190626/5954c4b9/attachment.bin>
More information about the cfe-commits
mailing list