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 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,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::createChecks(&Context, Checks); for (auto &Check : Checks) { Check->registerMatchers(&Finder); Check->registerPPCallbacks(Compiler.getSourceManager(), PP, PP); @@ -54,25 +78,7 @@ 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 +template std::string runCheckOnCode(StringRef Code, std::vector *Errors = nullptr, const Twine &Filename = "input.cc", @@ -110,9 +116,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) {