DefaultArgumentsDeclarationsCheck.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //===--- DefaultArgumentsDeclarationsCheck.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 "DefaultArgumentsDeclarationsCheck.h"
  9. #include "clang/Lex/Lexer.h"
  10. using namespace clang::ast_matchers;
  11. namespace clang::tidy::fuchsia {
  12. void DefaultArgumentsDeclarationsCheck::registerMatchers(MatchFinder *Finder) {
  13. // Declaring default parameters is disallowed.
  14. Finder->addMatcher(parmVarDecl(hasDefaultArgument()).bind("decl"), this);
  15. }
  16. void DefaultArgumentsDeclarationsCheck::check(
  17. const MatchFinder::MatchResult &Result) {
  18. const auto *D = Result.Nodes.getNodeAs<ParmVarDecl>("decl");
  19. if (!D)
  20. return;
  21. SourceRange DefaultArgRange = D->getDefaultArgRange();
  22. if (DefaultArgRange.getEnd() != D->getEndLoc())
  23. return;
  24. if (DefaultArgRange.getBegin().isMacroID()) {
  25. diag(D->getBeginLoc(),
  26. "declaring a parameter with a default argument is disallowed");
  27. return;
  28. }
  29. SourceLocation StartLocation =
  30. D->getName().empty() ? D->getBeginLoc() : D->getLocation();
  31. SourceRange RemovalRange(
  32. Lexer::getLocForEndOfToken(StartLocation, 0, *Result.SourceManager,
  33. Result.Context->getLangOpts()),
  34. DefaultArgRange.getEnd());
  35. diag(D->getBeginLoc(),
  36. "declaring a parameter with a default argument is disallowed")
  37. << D << FixItHint::CreateRemoval(RemovalRange);
  38. }
  39. } // namespace clang::tidy::fuchsia