UsePragmaOnceCheck.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. /* This code was originally taken from part of the Clang-Tidy LLVM project and
  4. * modified for use with CMake under the following original license: */
  5. //===--- HeaderGuard.h - clang-tidy -----------------------------*- C++
  6. //-*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM
  9. // Exceptions. See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #pragma once
  14. #include <clang-tidy/ClangTidyCheck.h>
  15. #include <clang-tidy/utils/FileExtensionsUtils.h>
  16. #if LLVM_VERSION_MAJOR >= 17
  17. # include <clang-tidy/FileExtensionsSet.h>
  18. #else
  19. namespace clang {
  20. namespace tidy {
  21. using utils::FileExtensionsSet;
  22. } // namespace tidy
  23. } // namespace clang
  24. #endif
  25. namespace clang {
  26. namespace tidy {
  27. namespace cmake {
  28. /// Finds and replaces header guards with pragma once.
  29. /// The check supports these options:
  30. /// - `HeaderFileExtensions`: a semicolon-separated list of filename
  31. /// extensions of header files (The filename extension should not contain
  32. /// "." prefix). ";h;hh;hpp;hxx" by default.
  33. ///
  34. /// For extension-less header files, using an empty string or leaving an
  35. /// empty string between ";" if there are other filename extensions.
  36. class UsePragmaOnceCheck : public ClangTidyCheck
  37. {
  38. public:
  39. UsePragmaOnceCheck(StringRef Name, ClangTidyContext* Context)
  40. : ClangTidyCheck(Name, Context)
  41. , RawStringHeaderFileExtensions(Options.getLocalOrGlobal(
  42. "HeaderFileExtensions", utils::defaultHeaderFileExtensions()))
  43. {
  44. utils::parseFileExtensions(RawStringHeaderFileExtensions,
  45. HeaderFileExtensions,
  46. utils::defaultFileExtensionDelimiters());
  47. }
  48. void storeOptions(ClangTidyOptions::OptionMap& Opts) override;
  49. void registerPPCallbacks(SourceManager const& SM, Preprocessor* PP,
  50. Preprocessor* ModuleExpanderPP) override;
  51. /// Returns ``true`` if the check should add pragma once to the file
  52. /// if it has none.
  53. virtual bool shouldSuggestToAddPragmaOnce(StringRef Filename);
  54. private:
  55. std::string RawStringHeaderFileExtensions;
  56. FileExtensionsSet HeaderFileExtensions;
  57. };
  58. } // namespace cmake
  59. } // namespace tidy
  60. } // namespace clang