Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 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 | |
| 9 | #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_HEADERGUARD_H |
| 10 | #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_HEADERGUARD_H |
| 11 | |
| 12 | #include "../ClangTidyCheck.h" |
| 13 | #include "../utils/FileExtensionsUtils.h" |
| 14 | |
| 15 | namespace clang { |
| 16 | namespace tidy { |
| 17 | namespace utils { |
| 18 | |
| 19 | /// Finds and fixes header guards. |
| 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 HeaderGuardCheck : public ClangTidyCheck { |
| 28 | public: |
| 29 | HeaderGuardCheck(StringRef Name, ClangTidyContext *Context) |
| 30 | : ClangTidyCheck(Name, Context), |
| 31 | RawStringHeaderFileExtensions(Options.getLocalOrGlobal( |
| 32 | "HeaderFileExtensions", utils::defaultHeaderFileExtensions())) { |
| 33 | utils::parseFileExtensions(RawStringHeaderFileExtensions, |
| 34 | HeaderFileExtensions, |
| 35 | utils::defaultFileExtensionDelimiters()); |
| 36 | } |
| 37 | void storeOptions(ClangTidyOptions::OptionMap &Opts) override; |
| 38 | void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, |
| 39 | Preprocessor *ModuleExpanderPP) override; |
| 40 | |
| 41 | /// Returns ``true`` if the check should suggest inserting a trailing comment |
| 42 | /// on the ``#endif`` of the header guard. It will use the same name as |
| 43 | /// returned by ``HeaderGuardCheck::getHeaderGuard``. |
| 44 | virtual bool shouldSuggestEndifComment(StringRef Filename); |
| 45 | /// Returns ``true`` if the check should suggest changing an existing header |
| 46 | /// guard to the string returned by ``HeaderGuardCheck::getHeaderGuard``. |
| 47 | virtual bool shouldFixHeaderGuard(StringRef Filename); |
| 48 | /// Returns ``true`` if the check should add a header guard to the file |
| 49 | /// if it has none. |
| 50 | virtual bool shouldSuggestToAddHeaderGuard(StringRef Filename); |
| 51 | /// Returns a replacement for the ``#endif`` line with a comment mentioning |
| 52 | /// \p HeaderGuard. The replacement should start with ``endif``. |
| 53 | virtual std::string formatEndIf(StringRef HeaderGuard); |
| 54 | /// Gets the canonical header guard for a file. |
| 55 | virtual std::string getHeaderGuard(StringRef Filename, |
| 56 | StringRef OldGuard = StringRef()) = 0; |
| 57 | |
| 58 | private: |
| 59 | std::string RawStringHeaderFileExtensions; |
| 60 | utils::FileExtensionsSet HeaderFileExtensions; |
| 61 | }; |
| 62 | |
| 63 | } // namespace utils |
| 64 | } // namespace tidy |
| 65 | } // namespace clang |
| 66 | |
| 67 | #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_HEADERGUARD_H |