clang-tools 10.0.0

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
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 {
21 namespace tidy {
22 namespace openmp {
23
24 void UseDefaultNoneCheck::registerMatchers(MatchFinder *Finder) {
25 // Don't register the check if OpenMP is not enabled; the OpenMP pragmas are
26 // completely ignored then, so no OpenMP entires will be present in the AST.
27 if (!getLangOpts().OpenMP)
28 return;
29
30 Finder->addMatcher(
31 ompExecutableDirective(
32 allOf(isAllowedToContainClauseKind(OMPC_default),
33 anyOf(unless(hasAnyClause(ompDefaultClause())),
34 hasAnyClause(ompDefaultClause(unless(isNoneKind()))
35 .bind("clause")))))
36 .bind("directive"),
37 this);
38 }
39
40 void UseDefaultNoneCheck::check(const MatchFinder::MatchResult &Result) {
41 const auto *Directive =
42 Result.Nodes.getNodeAs<OMPExecutableDirective>("directive");
43 assert(Directive != nullptr && "Expected to match some directive.");
44
45 if (const auto *Clause = Result.Nodes.getNodeAs<OMPDefaultClause>("clause")) {
46 diag(Directive->getBeginLoc(),
47 "OpenMP directive '%0' specifies 'default(%1)' clause, consider using "
48 "'default(none)' clause instead")
49 << getOpenMPDirectiveName(Directive->getDirectiveKind())
50 << getOpenMPSimpleClauseTypeName(Clause->getClauseKind(),
51 Clause->getDefaultKind());
52 diag(Clause->getBeginLoc(), "existing 'default' clause specified here",
53 DiagnosticIDs::Note);
54 return;
55 }
56
57 diag(Directive->getBeginLoc(),
58 "OpenMP directive '%0' does not specify 'default' clause, consider "
59 "specifying 'default(none)' clause")
60 << getOpenMPDirectiveName(Directive->getDirectiveKind());
61 }
62
63 } // namespace openmp
64 } // namespace tidy
65 } // namespace clang
clang::ast_matchers
Definition: AbseilMatcher.h:14
UseDefaultNoneCheck.h
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27