xref: /llvm-project/clang-tools-extra/clang-tidy/openmp/UseDefaultNoneCheck.cpp (revision df0c8f25145047731fb95b4ce7153ce6fb5b6f5d)
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/ASTContext.h"
11 #include "clang/AST/OpenMPClause.h"
12 #include "clang/AST/Stmt.h"
13 #include "clang/AST/StmtOpenMP.h"
14 #include "clang/ASTMatchers/ASTMatchFinder.h"
15 #include "clang/ASTMatchers/ASTMatchers.h"
16 #include "clang/ASTMatchers/ASTMatchersMacros.h"
17 
18 using namespace clang::ast_matchers;
19 
20 namespace clang::tidy::openmp {
21 
registerMatchers(MatchFinder * Finder)22 void UseDefaultNoneCheck::registerMatchers(MatchFinder *Finder) {
23   Finder->addMatcher(
24       ompExecutableDirective(
25           isAllowedToContainClauseKind(llvm::omp::OMPC_default),
26           anyOf(unless(hasAnyClause(ompDefaultClause())),
27                 hasAnyClause(
28                     ompDefaultClause(unless(isNoneKind())).bind("clause"))))
29           .bind("directive"),
30       this);
31 }
32 
check(const MatchFinder::MatchResult & Result)33 void UseDefaultNoneCheck::check(const MatchFinder::MatchResult &Result) {
34   const auto *Directive =
35       Result.Nodes.getNodeAs<OMPExecutableDirective>("directive");
36   assert(Directive != nullptr && "Expected to match some directive.");
37 
38   if (const auto *Clause = Result.Nodes.getNodeAs<OMPDefaultClause>("clause")) {
39     diag(Directive->getBeginLoc(),
40          "OpenMP directive '%0' specifies 'default(%1)' clause, consider using "
41          "'default(none)' clause instead")
42         << getOpenMPDirectiveName(Directive->getDirectiveKind())
43         << getOpenMPSimpleClauseTypeName(Clause->getClauseKind(),
44                                          unsigned(Clause->getDefaultKind()));
45     diag(Clause->getBeginLoc(), "existing 'default' clause specified here",
46          DiagnosticIDs::Note);
47     return;
48   }
49 
50   diag(Directive->getBeginLoc(),
51        "OpenMP directive '%0' does not specify 'default' clause, consider "
52        "specifying 'default(none)' clause")
53       << getOpenMPDirectiveName(Directive->getDirectiveKind());
54 }
55 
56 } // namespace clang::tidy::openmp
57