| 1 | //===--- UseDefaultNoneCheck.cpp - clang-tidy -----------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "UseDefaultNoneCheck.h" |
| 10 | #include "clang/AST/OpenMPClause.h" |
| 11 | #include "clang/AST/StmtOpenMP.h" |
| 12 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 13 | #include "clang/ASTMatchers/ASTMatchers.h" |
| 14 | |
| 15 | using namespace clang::ast_matchers; |
| 16 | |
| 17 | namespace clang::tidy::openmp { |
| 18 | |
| 19 | void UseDefaultNoneCheck::registerMatchers(MatchFinder *Finder) { |
| 20 | Finder->addMatcher( |
| 21 | ompExecutableDirective( |
| 22 | isAllowedToContainClauseKind(llvm::omp::OMPC_default), |
| 23 | anyOf(unless(hasAnyClause(ompDefaultClause())), |
| 24 | hasAnyClause( |
| 25 | ompDefaultClause(unless(isNoneKind())).bind("clause" )))) |
| 26 | .bind("directive" ), |
| 27 | this); |
| 28 | } |
| 29 | |
| 30 | void UseDefaultNoneCheck::check(const MatchFinder::MatchResult &Result) { |
| 31 | const auto *Directive = |
| 32 | Result.Nodes.getNodeAs<OMPExecutableDirective>(ID: "directive" ); |
| 33 | assert(Directive != nullptr && "Expected to match some directive." ); |
| 34 | |
| 35 | if (const auto *Clause = Result.Nodes.getNodeAs<OMPDefaultClause>(ID: "clause" )) { |
| 36 | diag(Loc: Directive->getBeginLoc(), |
| 37 | Description: "OpenMP directive '%0' specifies 'default(%1)' clause, consider using " |
| 38 | "'default(none)' clause instead" ) |
| 39 | << getOpenMPDirectiveName(Directive->getDirectiveKind()) |
| 40 | << getOpenMPSimpleClauseTypeName(Clause->getClauseKind(), |
| 41 | unsigned(Clause->getDefaultKind())); |
| 42 | diag(Clause->getBeginLoc(), "existing 'default' clause specified here" , |
| 43 | DiagnosticIDs::Note); |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | diag(Loc: Directive->getBeginLoc(), |
| 48 | Description: "OpenMP directive '%0' does not specify 'default' clause, consider " |
| 49 | "specifying 'default(none)' clause" ) |
| 50 | << getOpenMPDirectiveName(Directive->getDirectiveKind()); |
| 51 | } |
| 52 | |
| 53 | } // namespace clang::tidy::openmp |
| 54 | |