summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYitzhak Mandelbaum <yitzhakm@google.com>2019-06-26 15:04:33 +0000
committerYitzhak Mandelbaum <yitzhakm@google.com>2019-06-26 15:04:33 +0000
commit83b1580e1b14fc719e10c5ac885a4c993be3fff6 (patch)
treef4e88bd3a5ddb99f88792d4da3b1bbc861b2e2e2
parent435ee9fb1f86a8f3798c8953b5d1a0b29849fd4e (diff)
downloadbcm5719-llvm-83b1580e1b14fc719e10c5ac885a4c993be3fff6.tar.gz
bcm5719-llvm-83b1580e1b14fc719e10c5ac885a4c993be3fff6.zip
[clang-tidy] Fix ClangTidyTest to initialize context before checks.
Summary: 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. Reviewers: gribozavr Subscribers: xazax.hun, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D63784 llvm-svn: 364435
-rw-r--r--clang-tools-extra/unittests/clang-tidy/ClangTidyTest.h48
1 files changed, 27 insertions, 21 deletions
diff --git a/clang-tools-extra/unittests/clang-tidy/ClangTidyTest.h b/clang-tools-extra/unittests/clang-tidy/ClangTidyTest.h
index 560b0ff893c..d1180d4ebfd 100644
--- a/clang-tools-extra/unittests/clang-tidy/ClangTidyTest.h
+++ b/clang-tools-extra/unittests/clang-tidy/ClangTidyTest.h
@@ -27,6 +27,25 @@ namespace clang {
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 @@ private:
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 @@ private:
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 @@ runCheckOnCode(StringRef Code, std::vector<ClangTidyError> *Errors = nullptr,
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) {
OpenPOWER on IntegriCloud