UseDefaultNoneCheck.cpp 2.2 KB

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