ymandel created this revision. ymandel added a reviewer: gribozavr. Herald added a subscriber: xazax.hun. Herald added a project: clang.
Currently, `clang::tidy::test::runCheckOnCode()` constructs the check instances *before* initializing the ClangTidyContext. This ordering causes problems when the check's constructor accesses the context, for example, through `getLangOpts()`. This revision moves the construction to after the context initialization, which follows the pattern used in the clang tidy tool itself. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D63784 Files: clang-tools-extra/unittests/clang-tidy/ClangTidyTest.h Index: clang-tools-extra/unittests/clang-tidy/ClangTidyTest.h =================================================================== --- clang-tools-extra/unittests/clang-tidy/ClangTidyTest.h +++ clang-tools-extra/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... CheckList> class TestClangTidyAction : public ASTFrontendAction { public: TestClangTidyAction(SmallVectorImpl<std::unique_ptr<ClangTidyCheck>> &Checks, @@ -42,6 +61,9 @@ Context.setASTContext(&Compiler.getASTContext()); Preprocessor *PP = &Compiler.getPreprocessor(); + + // Checks must be created here, *after* `Context` has been initialized. + CheckFactory<CheckList...>::createChecks(&Context, Checks); for (auto &Check : Checks) { Check->registerMatchers(&Finder); Check->registerPPCallbacks(Compiler.getSourceManager(), PP, PP); @@ -54,24 +76,6 @@ 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> std::string runCheckOnCode(StringRef Code, std::vector<ClangTidyError> *Errors = nullptr, @@ -110,9 +114,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<CheckList...>(Checks, Finder, Context), + Files.get()); InMemoryFileSystem->addFile(Filename, 0, llvm::MemoryBuffer::getMemBuffer(Code)); for (const auto &FileContent : PathsToContent) {
Index: clang-tools-extra/unittests/clang-tidy/ClangTidyTest.h =================================================================== --- clang-tools-extra/unittests/clang-tidy/ClangTidyTest.h +++ clang-tools-extra/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... CheckList> class TestClangTidyAction : public ASTFrontendAction { public: TestClangTidyAction(SmallVectorImpl<std::unique_ptr<ClangTidyCheck>> &Checks, @@ -42,6 +61,9 @@ Context.setASTContext(&Compiler.getASTContext()); Preprocessor *PP = &Compiler.getPreprocessor(); + + // Checks must be created here, *after* `Context` has been initialized. + CheckFactory<CheckList...>::createChecks(&Context, Checks); for (auto &Check : Checks) { Check->registerMatchers(&Finder); Check->registerPPCallbacks(Compiler.getSourceManager(), PP, PP); @@ -54,24 +76,6 @@ 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> std::string runCheckOnCode(StringRef Code, std::vector<ClangTidyError> *Errors = nullptr, @@ -110,9 +114,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<CheckList...>(Checks, Finder, Context), + Files.get()); InMemoryFileSystem->addFile(Filename, 0, llvm::MemoryBuffer::getMemBuffer(Code)); for (const auto &FileContent : PathsToContent) {
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits