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
18using namespace clang::ast_matchers;
19
20namespace clang::tidy::openmp {
21
22void 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
33void UseDefaultNoneCheck::check(const MatchFinder::MatchResult &Result) {
34 const auto *Directive =
35 Result.Nodes.getNodeAs<OMPExecutableDirective>(ID: "directive");
36 assert(Directive != nullptr && "Expected to match some directive.");
37
38 if (const auto *Clause = Result.Nodes.getNodeAs<OMPDefaultClause>(ID: "clause")) {
39 diag(Loc: Directive->getBeginLoc(),
40 Description: "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(Loc: Directive->getBeginLoc(),
51 Description: "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

source code of clang-tools-extra/clang-tidy/openmp/UseDefaultNoneCheck.cpp