clang-tools 20.0.0git
UseDefaultNoneCheck.cpp
Go to the documentation of this file.
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
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>("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
const MacroDirective * Directive
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.