diff --git a/clang-tools-extra/unittests/clang-tidy/ClangTidyTest.h b/clang-tools-extra/unittests/clang-tidy/ClangTidyTest.h --- a/clang-tools-extra/unittests/clang-tidy/ClangTidyTest.h +++ b/clang-tools-extra/unittests/clang-tidy/ClangTidyTest.h @@ -27,6 +27,25 @@ namespace tidy { namespace test { +template struct CheckFactory { + static void + createChecks(ClangTidyContext *Context, + SmallVectorImpl> &Result) { + CheckFactory::createChecks(Context, Result); + CheckFactory::createChecks(Context, Result); + } +}; + +template struct CheckFactory { + static void + createChecks(ClangTidyContext *Context, + SmallVectorImpl> &Result) { + Result.emplace_back(llvm::make_unique( + "test-check-" + std::to_string(Result.size()), Context)); + } +}; + +template class TestClangTidyAction : public ASTFrontendAction { public: TestClangTidyAction(SmallVectorImpl> &Checks, @@ -42,6 +61,9 @@ Context.setASTContext(&Compiler.getASTContext()); Preprocessor *PP = &Compiler.getPreprocessor(); + + // Checks must be created here, *after* `Context` has been initialized. + CheckFactory::createChecks(&Context, Checks); for (auto &Check : Checks) { Check->registerMatchers(&Finder); Check->registerPPCallbacks(Compiler.getSourceManager(), PP, PP); @@ -54,24 +76,6 @@ ClangTidyContext &Context; }; -template struct CheckFactory { - static void - createChecks(ClangTidyContext *Context, - SmallVectorImpl> &Result) { - CheckFactory::createChecks(Context, Result); - CheckFactory::createChecks(Context, Result); - } -}; - -template struct CheckFactory { - static void - createChecks(ClangTidyContext *Context, - SmallVectorImpl> &Result) { - Result.emplace_back(llvm::make_unique( - "test-check-" + std::to_string(Result.size()), Context)); - } -}; - template std::string runCheckOnCode(StringRef Code, std::vector *Errors = nullptr, @@ -110,9 +114,9 @@ new FileManager(FileSystemOptions(), InMemoryFileSystem)); SmallVector, 1> Checks; - CheckFactory::createChecks(&Context, Checks); tooling::ToolInvocation Invocation( - Args, new TestClangTidyAction(Checks, Finder, Context), Files.get()); + Args, new TestClangTidyAction(Checks, Finder, Context), + Files.get()); InMemoryFileSystem->addFile(Filename, 0, llvm::MemoryBuffer::getMemBuffer(Code)); for (const auto &FileContent : PathsToContent) {