1
0

OstringstreamUseCmstrcatCheck.cxx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include "OstringstreamUseCmstrcatCheck.h"
  4. #include <clang/AST/Type.h>
  5. #include <clang/ASTMatchers/ASTMatchFinder.h>
  6. namespace clang {
  7. namespace tidy {
  8. namespace cmake {
  9. using namespace ast_matchers;
  10. OstringstreamUseCmstrcatCheck::OstringstreamUseCmstrcatCheck(
  11. StringRef Name, ClangTidyContext* Context)
  12. : ClangTidyCheck(Name, Context)
  13. {
  14. }
  15. void OstringstreamUseCmstrcatCheck::registerMatchers(MatchFinder* Finder)
  16. {
  17. Finder->addMatcher(
  18. typeLoc(unless(elaboratedTypeLoc()),
  19. optionally(hasParent(elaboratedTypeLoc().bind("parentType"))),
  20. loc(qualType(
  21. hasDeclaration(namedDecl(hasName("::std::ostringstream"))))))
  22. .bind("ostringstream"),
  23. this);
  24. }
  25. void OstringstreamUseCmstrcatCheck::check(
  26. MatchFinder::MatchResult const& Result)
  27. {
  28. TypeLoc const* ParentTypeNode =
  29. Result.Nodes.getNodeAs<TypeLoc>("parentType");
  30. TypeLoc const* RootNode = Result.Nodes.getNodeAs<TypeLoc>("ostringstream");
  31. if (ParentTypeNode != nullptr) {
  32. if (ParentTypeNode->getBeginLoc().isValid()) {
  33. this->diag(ParentTypeNode->getBeginLoc(),
  34. "use strings and cmStrCat() instead of std::ostringstream");
  35. }
  36. } else if (RootNode != nullptr) {
  37. if (RootNode->getBeginLoc().isValid()) {
  38. this->diag(RootNode->getBeginLoc(),
  39. "use strings and cmStrCat() instead of std::ostringstream");
  40. }
  41. }
  42. }
  43. }
  44. }
  45. }