HeaderGuard.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //===--- HeaderGuard.h - clang-tidy -----------------------------*- C++ -*-===//
  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. #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_HEADERGUARD_H
  9. #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_HEADERGUARD_H
  10. #include "../ClangTidyCheck.h"
  11. #include "../utils/FileExtensionsUtils.h"
  12. namespace clang::tidy::utils {
  13. /// Finds and fixes header guards.
  14. /// The check supports these options:
  15. /// - `HeaderFileExtensions`: a semicolon-separated list of filename
  16. /// extensions of header files (The filename extension should not contain
  17. /// "." prefix). ";h;hh;hpp;hxx" by default.
  18. ///
  19. /// For extension-less header files, using an empty string or leaving an
  20. /// empty string between ";" if there are other filename extensions.
  21. class HeaderGuardCheck : public ClangTidyCheck {
  22. public:
  23. HeaderGuardCheck(StringRef Name, ClangTidyContext *Context)
  24. : ClangTidyCheck(Name, Context),
  25. RawStringHeaderFileExtensions(Options.getLocalOrGlobal(
  26. "HeaderFileExtensions", utils::defaultHeaderFileExtensions())) {
  27. utils::parseFileExtensions(RawStringHeaderFileExtensions,
  28. HeaderFileExtensions,
  29. utils::defaultFileExtensionDelimiters());
  30. }
  31. void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
  32. void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
  33. Preprocessor *ModuleExpanderPP) override;
  34. /// Ensure that the provided header guard is a non-reserved identifier.
  35. std::string sanitizeHeaderGuard(StringRef Guard);
  36. /// Returns ``true`` if the check should suggest inserting a trailing comment
  37. /// on the ``#endif`` of the header guard. It will use the same name as
  38. /// returned by ``HeaderGuardCheck::getHeaderGuard``.
  39. virtual bool shouldSuggestEndifComment(StringRef Filename);
  40. /// Returns ``true`` if the check should suggest changing an existing header
  41. /// guard to the string returned by ``HeaderGuardCheck::getHeaderGuard``.
  42. virtual bool shouldFixHeaderGuard(StringRef Filename);
  43. /// Returns ``true`` if the check should add a header guard to the file
  44. /// if it has none.
  45. virtual bool shouldSuggestToAddHeaderGuard(StringRef Filename);
  46. /// Returns a replacement for the ``#endif`` line with a comment mentioning
  47. /// \p HeaderGuard. The replacement should start with ``endif``.
  48. virtual std::string formatEndIf(StringRef HeaderGuard);
  49. /// Gets the canonical header guard for a file.
  50. virtual std::string getHeaderGuard(StringRef Filename,
  51. StringRef OldGuard = StringRef()) = 0;
  52. private:
  53. std::string RawStringHeaderFileExtensions;
  54. utils::FileExtensionsSet HeaderFileExtensions;
  55. };
  56. } // namespace clang::tidy::utils
  57. #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_HEADERGUARD_H