UsePragmaOnceCheck.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt 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. namespace clang {
  17. namespace tidy {
  18. namespace cmake {
  19. /// Finds and replaces header guards with pragma once.
  20. /// The check supports these options:
  21. /// - `HeaderFileExtensions`: a semicolon-separated list of filename
  22. /// extensions of header files (The filename extension should not contain
  23. /// "." prefix). ";h;hh;hpp;hxx" by default.
  24. ///
  25. /// For extension-less header files, using an empty string or leaving an
  26. /// empty string between ";" if there are other filename extensions.
  27. class UsePragmaOnceCheck : public ClangTidyCheck
  28. {
  29. public:
  30. UsePragmaOnceCheck(StringRef Name, ClangTidyContext* Context)
  31. : ClangTidyCheck(Name, Context)
  32. , RawStringHeaderFileExtensions(Options.getLocalOrGlobal(
  33. "HeaderFileExtensions", utils::defaultHeaderFileExtensions()))
  34. {
  35. utils::parseFileExtensions(RawStringHeaderFileExtensions,
  36. HeaderFileExtensions,
  37. utils::defaultFileExtensionDelimiters());
  38. }
  39. void storeOptions(ClangTidyOptions::OptionMap& Opts) override;
  40. void registerPPCallbacks(const SourceManager& SM, Preprocessor* PP,
  41. Preprocessor* ModuleExpanderPP) override;
  42. /// Returns ``true`` if the check should add pragma once to the file
  43. /// if it has none.
  44. virtual bool shouldSuggestToAddPragmaOnce(StringRef Filename);
  45. private:
  46. std::string RawStringHeaderFileExtensions;
  47. utils::FileExtensionsSet HeaderFileExtensions;
  48. };
  49. } // namespace cmake
  50. } // namespace tidy
  51. } // namespace clang