diff --git a/clang-tools-extra/clang-tidy/mpi/BufferDerefCheck.h b/clang-tools-extra/clang-tidy/mpi/BufferDerefCheck.h --- a/clang-tools-extra/clang-tidy/mpi/BufferDerefCheck.h +++ b/clang-tools-extra/clang-tidy/mpi/BufferDerefCheck.h @@ -10,6 +10,7 @@ #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MPI_BUFFER_DEREF_H #include "../ClangTidyCheck.h" +#include "clang/StaticAnalyzer/Checkers/MPIFunctionClassifier.h" namespace clang { namespace tidy { @@ -30,6 +31,7 @@ : ClangTidyCheck(Name, Context) {} void registerMatchers(ast_matchers::MatchFinder *Finder) override; void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + void onEndOfTranslationUnit() override; private: /// Checks for all buffers in an MPI call if they are sufficiently @@ -41,6 +43,8 @@ ArrayRef BufferExprs); enum class IndirectionType : unsigned char { Pointer, Array }; + + Optional FuncClassifier; }; } // namespace mpi diff --git a/clang-tools-extra/clang-tidy/mpi/BufferDerefCheck.cpp b/clang-tools-extra/clang-tidy/mpi/BufferDerefCheck.cpp --- a/clang-tools-extra/clang-tidy/mpi/BufferDerefCheck.cpp +++ b/clang-tools-extra/clang-tidy/mpi/BufferDerefCheck.cpp @@ -9,7 +9,6 @@ #include "BufferDerefCheck.h" #include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" -#include "clang/StaticAnalyzer/Checkers/MPIFunctionClassifier.h" #include "clang/Tooling/FixIt.h" using namespace clang::ast_matchers; @@ -23,13 +22,15 @@ } void BufferDerefCheck::check(const MatchFinder::MatchResult &Result) { - static ento::mpi::MPIFunctionClassifier FuncClassifier(*Result.Context); const auto *CE = Result.Nodes.getNodeAs("CE"); if (!CE->getDirectCallee()) return; + if (!FuncClassifier) + FuncClassifier.emplace(*Result.Context); + const IdentifierInfo *Identifier = CE->getDirectCallee()->getIdentifier(); - if (!Identifier || !FuncClassifier.isMPIType(Identifier)) + if (!Identifier || !FuncClassifier->isMPIType(Identifier)) return; // These containers are used, to capture the type and expression of a buffer. @@ -60,18 +61,18 @@ // Collect buffer types and argument expressions for all buffers used in the // MPI call expression. The number passed to the lambda corresponds to the // argument index of the currently verified MPI function call. - if (FuncClassifier.isPointToPointType(Identifier)) { + if (FuncClassifier->isPointToPointType(Identifier)) { AddBuffer(0); - } else if (FuncClassifier.isCollectiveType(Identifier)) { - if (FuncClassifier.isReduceType(Identifier)) { + } else if (FuncClassifier->isCollectiveType(Identifier)) { + if (FuncClassifier->isReduceType(Identifier)) { AddBuffer(0); AddBuffer(1); - } else if (FuncClassifier.isScatterType(Identifier) || - FuncClassifier.isGatherType(Identifier) || - FuncClassifier.isAlltoallType(Identifier)) { + } else if (FuncClassifier->isScatterType(Identifier) || + FuncClassifier->isGatherType(Identifier) || + FuncClassifier->isAlltoallType(Identifier)) { AddBuffer(0); AddBuffer(3); - } else if (FuncClassifier.isBcastType(Identifier)) { + } else if (FuncClassifier->isBcastType(Identifier)) { AddBuffer(0); } } @@ -126,6 +127,7 @@ } } +void BufferDerefCheck::onEndOfTranslationUnit() { FuncClassifier.reset(); } } // namespace mpi } // namespace tidy } // namespace clang diff --git a/clang-tools-extra/clang-tidy/mpi/TypeMismatchCheck.h b/clang-tools-extra/clang-tidy/mpi/TypeMismatchCheck.h --- a/clang-tools-extra/clang-tidy/mpi/TypeMismatchCheck.h +++ b/clang-tools-extra/clang-tidy/mpi/TypeMismatchCheck.h @@ -11,6 +11,7 @@ #include "../ClangTidyCheck.h" #include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/StaticAnalyzer/Checkers/MPIFunctionClassifier.h" namespace clang { namespace tidy { @@ -31,6 +32,8 @@ void registerMatchers(ast_matchers::MatchFinder *Finder) override; void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + void onEndOfTranslationUnit() override; + private: /// Check if the buffer type MPI datatype pairs match. /// @@ -41,6 +44,8 @@ void checkArguments(ArrayRef BufferTypes, ArrayRef BufferExprs, ArrayRef MPIDatatypes, const LangOptions &LO); + + Optional FuncClassifier; }; } // namespace mpi diff --git a/clang-tools-extra/clang-tidy/mpi/TypeMismatchCheck.cpp b/clang-tools-extra/clang-tidy/mpi/TypeMismatchCheck.cpp --- a/clang-tools-extra/clang-tidy/mpi/TypeMismatchCheck.cpp +++ b/clang-tools-extra/clang-tidy/mpi/TypeMismatchCheck.cpp @@ -8,7 +8,6 @@ #include "TypeMismatchCheck.h" #include "clang/Lex/Lexer.h" -#include "clang/StaticAnalyzer/Checkers/MPIFunctionClassifier.h" #include "clang/Tooling/FixIt.h" #include #include @@ -241,13 +240,15 @@ } void TypeMismatchCheck::check(const MatchFinder::MatchResult &Result) { - static ento::mpi::MPIFunctionClassifier FuncClassifier(*Result.Context); const auto *const CE = Result.Nodes.getNodeAs("CE"); if (!CE->getDirectCallee()) return; + if (!FuncClassifier) + FuncClassifier.emplace(*Result.Context); + const IdentifierInfo *Identifier = CE->getDirectCallee()->getIdentifier(); - if (!Identifier || !FuncClassifier.isMPIType(Identifier)) + if (!Identifier || !FuncClassifier->isMPIType(Identifier)) return; // These containers are used, to capture buffer, MPI datatype pairs. @@ -281,18 +282,18 @@ }; // Collect all buffer, MPI datatype pairs for the inspected call expression. - if (FuncClassifier.isPointToPointType(Identifier)) { + if (FuncClassifier->isPointToPointType(Identifier)) { AddPair(0, 2); - } else if (FuncClassifier.isCollectiveType(Identifier)) { - if (FuncClassifier.isReduceType(Identifier)) { + } else if (FuncClassifier->isCollectiveType(Identifier)) { + if (FuncClassifier->isReduceType(Identifier)) { AddPair(0, 3); AddPair(1, 3); - } else if (FuncClassifier.isScatterType(Identifier) || - FuncClassifier.isGatherType(Identifier) || - FuncClassifier.isAlltoallType(Identifier)) { + } else if (FuncClassifier->isScatterType(Identifier) || + FuncClassifier->isGatherType(Identifier) || + FuncClassifier->isAlltoallType(Identifier)) { AddPair(0, 2); AddPair(3, 5); - } else if (FuncClassifier.isBcastType(Identifier)) { + } else if (FuncClassifier->isBcastType(Identifier)) { AddPair(0, 2); } } @@ -331,6 +332,7 @@ } } +void TypeMismatchCheck::onEndOfTranslationUnit() { FuncClassifier.reset(); } } // namespace mpi } // namespace tidy } // namespace clang